-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc.c
296 lines (238 loc) · 7.74 KB
/
rpc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* rpc.c - RPC calls to a motmot server
*/
#include <glib.h>
#include <msgpack.h>
#include <stdint.h>
#include <string.h>
#include "msgpackutil.h"
#include "prpl.h"
#include "rpc.h"
#include "sslconn.h"
// A return-nonzero assert instead of the typical abort() one
#define assert(x) \
if (!(x)) { \
g_warning("RPC Error: %s (%s:%d)", #x, __FILE__, __LINE__); \
return 1; \
}
////////////////////////////////////////////////////////////////////////////
//
// Protocol helper macros
//
// The work of checking and responding to protocol requests is as boring as it
// is verbose. Let's make our lives easier with some macros
//
// Most of what we're actually doing is typechecking
#define typecheck(obj, tipe) assert((obj)->type == MSGPACK_OBJECT_##tipe)
// msgpack's type names are unfortunately long
#define MSGPACK_OBJECT_INT MSGPACK_OBJECT_INTEGER
#define MSGPACK_OBJECT_UINT MSGPACK_OBJECT_POSITIVE_INTEGER
#define MSGPACK_OBJECT_STRING MSGPACK_OBJECT_RAW
// Strings require a bit more work to be useful. Remember to g_free the result
#define cstring(obj) g_strndup((obj)->via.raw.ptr, (obj)->via.raw.size)
// Doing this cast by ourselves is sort of tedious
#define pdata(buddy) ((struct pm_buddy *)(buddy)->proto_data)
// We commonly want to replace an old object (in a struct) with a new one,
// freeing whatever was there before if applicable
#define swapout(old, new) \
if (old != NULL) g_free((void *)old); \
old = new
static int rpc_all_statuses(struct pm_account *, const msgpack_object *);
static int rpc_get_status_resp(struct pm_account *, const msgpack_object *);
// TODO: This file needs a lot of error handling. In particular,
// purple_ssl_write returns the number of bytes written, which might at some
// point involve some connection buffer that ensures yaks get written
int
rpc_dispatch(struct pm_account *account, const msgpack_object *obj)
{
int64_t opcode;
// Do a little sanity-checking of the received object
// TODO: replace asserts with some sort of throw-to-the-user error handling
assert(obj->type == MSGPACK_OBJECT_ARRAY);
assert(obj->via.array.size >= 1);
assert(obj->via.array.ptr[0].type == MSGPACK_OBJECT_NEGATIVE_INTEGER);
opcode = obj->via.array.ptr[0].via.i64;
switch (opcode) {
// TODO: import from purplemot.c:867
case OP_ALL_STATUS_RESPONSE:
return rpc_all_statuses(account, obj->via.array.ptr + 1);
case OP_GET_STATUS_RESP:
return rpc_get_status_resp(account, obj->via.array.ptr + 1);
/* TODO: finish importing all of this
case OP_PUSH_FRIEND_ACCEPT:
case OP_PUSH_CLIENT_STATUS:
friend_name = deser_get_string(ar, 1, &error);
if (error == PURPLEMOT_ERROR) {
return;
}
status = deser_get_pos_int(ar, 2, &error);
if (error == PURPLEMOT_ERROR) {
g_free(friend_name);
return;
}
update_remote_status(a, friend_name, status);
if (opcode == OP_PUSH_FRIEND_ACCEPT) {
break;
}
addr = deser_get_string(tuple, 2, &error);
if (error == PURPLEMOT_ERROR) {
return;
}
port = deser_get_pos_int(tuple, 3, &error);
if (error == PURPLEMOT_ERROR) {
return;
}
bud = purple_find_buddy(a, friend_name);
if (bud == NULL) {
return;
}
proto = bud->proto_data;
proto->addr = addr;
proto->port = port;
g_free(friend_name);
break;
case OP_PUSH_FRIEND_REQUEST:
purple_debug_info("purplemot", "getting friend request");
friend_name = deser_get_string(ar, 1, &error);
if (error == PURPLEMOT_ERROR) {
return;
}
if (purple_find_buddy(a, friend_name) != NULL) {
break;
}
conn->data = friend_name;
purple_account_request_authorization(a, friend_name, NULL, NULL, NULL, FALSE, auth_cb, deny_cb, gc);
break;
case OP_ACCESS_DENIED:
case OP_AUTH_FAILED:
purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, _("Authentication failed, please check that your username and password are correct"));
break;
*/
default:
return 1;
}
}
////////////////////////////////////////////////////////////////////////////
//
// RPC response callbacks
//
static int
rpc_all_statuses(struct pm_account *account, const msgpack_object *obj)
{
const msgpack_object *s, *e, *a;
const char *name;
PurpleBuddy *buddy;
typecheck(obj, ARRAY);
for (s = obj->via.array.ptr, e = s + obj->via.array.size; s != e; s++) {
typecheck(s, ARRAY);
assert(s->via.array.size == 3);
a = s->via.array.ptr;
// TODO(carl): This can be converted into a preprocessor map.
// TODO(carl): Change the server to match this
typecheck(a + 0, STRING);
typecheck(a + 1, UINT);
typecheck(a + 2, STRING);
typecheck(a + 3, UINT);
name = cstring(a + 0);
if ((buddy = purple_find_buddy(account->pa, name)) == NULL) {
buddy = purple_buddy_new(account->pa, name, NULL /* TODO: alias? */);
buddy->proto_data = g_malloc0(sizeof(struct pm_buddy));
}
swapout(pdata(buddy)->ip, cstring(a + 2));
pdata(buddy)->port = a[3].via.u64;
//update_remote_status(account->pa, name, a[1].via.u64);
g_free((void *)name);
}
return 0;
}
static int
rpc_get_status_resp(struct pm_account *account, const msgpack_object *obj)
{
const msgpack_object *a;
const char *name;
PurpleBuddy *buddy = NULL;
// TODO(carl): WTF. The data format here is different than the one given
// above, which does the same thing (modulo a list). I don't get it.
typecheck(obj, ARRAY);
assert(obj->via.array.size == 4);
a = obj->via.array.ptr;
typecheck(a + 0, STRING); // name
typecheck(a + 1, UINT); // status
typecheck(a + 2, STRING); // ip
typecheck(a + 3, UINT); // port
name = cstring(a + 0);
buddy = purple_find_buddy(account->pa, name);
if (buddy == NULL) {
return 1; // TODO(carl): what should we do here?
}
//update_remote_status(account->pa, name, a[2].via.u64);
if (pdata(buddy)->ip != NULL) {
g_free((void *)pdata(buddy)->ip);
}
swapout(pdata(buddy)->ip, cstring(a + 2));
pdata(buddy)->port = a[2].via.u64;
g_free((void *)name);
return 0;
}
// TODO: the rest of them
////////////////////////////////////////////////////////////////////////////
//
// Client -> Server RPC calls
//
// Helper macros
#define rpc_op(y, op, nargs) \
struct yak y; \
yak_init(&y, 1 + (nargs)); \
yak_pack(&y, int, op);
#define rpc_op_end(y, account) \
purple_ssl_write(account->gsc, UNYAK(&yak)); \
yak_destroy(&y);
void
rpc_login(struct pm_account *account)
{
rpc_op(yak, OP_AUTHENTICATE_USER, 2);
yak_cstring(&yak, purple_account_get_username(account->pa));
yak_cstring(&yak, purple_account_get_password(account->pa));
rpc_op_end(yak, account);
}
void
rpc_get_all_statuses(struct pm_account *account)
{
rpc_op(yak, OP_GET_ALL_STATUSES, 0);
rpc_op_end(yak, account);
}
void
rpc_register_friend(struct pm_account *account, const char *name)
{
rpc_op(yak, OP_REGISTER_FRIEND, 1);
yak_cstring(&yak, name);
rpc_op_end(yak, account);
}
void
rpc_unregister_friend(struct pm_account *account, const char *name)
{
rpc_op(yak, OP_UNREGISTER_FRIEND, 1);
yak_cstring(&yak, name);
rpc_op_end(yak, account);
}
void
rpc_get_status(struct pm_account *account, const char *name)
{
rpc_op(yak, OP_GET_USER_STATUS, 1);
yak_cstring(&yak, name);
rpc_op_end(yak, account);
}
void
rpc_register_status(struct pm_account *account, int status)
{
rpc_op(yak, OP_REGISTER_STATUS, 1);
yak_pack(&yak, int, status);
rpc_op_end(yak, account);
}
void
rpc_accept_friend(struct pm_account *account, const char *name)
{
rpc_op(yak, OP_ACCEPT_FRIEND, 1);
yak_cstring(&yak, name);
rpc_op_end(yak, account);
}