9193
|
1 #include "msn.h"
|
|
2 #include "directconn.h"
|
|
3
|
|
4 #include "slp.h"
|
|
5 #include "slpmsg.h"
|
|
6
|
|
7 /**************************************************************************
|
|
8 * Directconn Specific
|
|
9 **************************************************************************/
|
|
10
|
|
11 void
|
|
12 msn_directconn_send_handshake(MsnDirectConn *directconn)
|
|
13 {
|
|
14 MsnSlpLink *slplink;
|
|
15 MsnSlpMessage *slpmsg;
|
|
16
|
|
17 g_return_if_fail(directconn != NULL);
|
|
18
|
|
19 slplink = directconn->slplink;
|
|
20
|
|
21 slpmsg = msn_slpmsg_new(slplink);
|
|
22 slpmsg->flags = 0x100;
|
|
23
|
|
24 if (directconn->nonce != NULL)
|
|
25 {
|
|
26 guint32 t1;
|
|
27 guint16 t2;
|
|
28 guint16 t3;
|
|
29 guint16 t4;
|
|
30 guint64 t5;
|
|
31
|
|
32 sscanf (directconn->nonce, "%08X-%04hX-%04hX-%04hX-%012llX", &t1, &t2, &t3, &t4, &t5);
|
|
33
|
|
34 t1 = GUINT32_TO_LE(t1);
|
|
35 t2 = GUINT16_TO_LE(t2);
|
|
36 t3 = GUINT16_TO_LE(t3);
|
|
37 t4 = GUINT16_TO_BE(t4);
|
|
38 t5 = GUINT64_TO_BE(t5);
|
|
39
|
|
40 slpmsg->ack_id = t1;
|
|
41 slpmsg->ack_sub_id = t2 | (t3 << 16);
|
|
42 slpmsg->ack_size = t4 | t5;
|
|
43 }
|
|
44
|
|
45 g_free(directconn->nonce);
|
|
46
|
|
47 msn_slplink_send_slpmsg(slplink, slpmsg);
|
|
48
|
|
49 directconn->acked =TRUE;
|
|
50 }
|
|
51
|
|
52 /**************************************************************************
|
|
53 * Connection Functions
|
|
54 **************************************************************************/
|
|
55
|
|
56 static int
|
|
57 create_listener(int port)
|
|
58 {
|
|
59 int fd;
|
|
60 const int on = 1;
|
|
61
|
|
62 #if 0
|
|
63 struct addrinfo hints;
|
|
64 struct addrinfo *c, *res;
|
|
65 char port_str[5];
|
|
66
|
|
67 snprintf(port_str, sizeof(port_str), "%d", port);
|
|
68
|
|
69 memset(&hints, 0, sizeof(hints));
|
|
70
|
|
71 hints.ai_flags = AI_PASSIVE;
|
|
72 hints.ai_family = AF_UNSPEC;
|
|
73 hints.ai_socktype = SOCK_STREAM;
|
|
74
|
|
75 if (getaddrinfo(NULL, port_str, &hints, &res) != 0)
|
|
76 {
|
|
77 gaim_debug_error("msn", "Could not get address info: %s.\n",
|
|
78 port_str);
|
|
79 return -1;
|
|
80 }
|
|
81
|
|
82 for (c = res; c != NULL; c = c->ai_next)
|
|
83 {
|
|
84 fd = socket(c->ai_family, c->ai_socktype, c->ai_protocol);
|
|
85
|
|
86 if (fd < 0)
|
|
87 continue;
|
|
88
|
|
89 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
|
90
|
|
91 if (bind(fd, c->ai_addr, c->ai_addrlen) == 0)
|
|
92 break;
|
|
93
|
|
94 close(fd);
|
|
95 }
|
|
96
|
|
97 if (c == NULL)
|
|
98 {
|
|
99 gaim_debug_error("msn", "Could not find socket: %s.\n", port_str);
|
|
100 return -1;
|
|
101 }
|
|
102
|
|
103 freeaddrinfo(res);
|
|
104 #else
|
|
105 struct sockaddr_in sockin;
|
|
106
|
|
107 fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
108
|
|
109 if (fd < 0)
|
|
110 return -1;
|
|
111
|
|
112 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) != 0)
|
|
113 {
|
|
114 close(fd);
|
|
115 return -1;
|
|
116 }
|
|
117
|
|
118 memset(&sockin, 0, sizeof(struct sockaddr_in));
|
|
119 sockin.sin_family = AF_INET;
|
|
120 sockin.sin_port = htons(port);
|
|
121
|
|
122 if (bind(fd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0)
|
|
123 {
|
|
124 close(fd);
|
|
125 return -1;
|
|
126 }
|
|
127 #endif
|
|
128
|
|
129 if (listen (fd, 4) != 0)
|
|
130 {
|
|
131 close (fd);
|
|
132 return -1;
|
|
133 }
|
|
134
|
|
135 fcntl(fd, F_SETFL, O_NONBLOCK);
|
|
136
|
|
137 return fd;
|
|
138 }
|
|
139
|
|
140 size_t
|
|
141 msn_directconn_write(MsnDirectConn *directconn,
|
|
142 const char *data, size_t len)
|
|
143 {
|
|
144 char *buffer, *tmp;
|
|
145 size_t buf_size;
|
|
146 size_t ret;
|
|
147 guint32 sent_len;
|
|
148
|
|
149 g_return_val_if_fail(directconn != NULL, 0);
|
|
150
|
|
151 buf_size = len + 4;
|
|
152 buffer = tmp = g_malloc(buf_size);
|
|
153
|
|
154 sent_len = GUINT32_TO_LE(len);
|
|
155
|
|
156 memcpy(tmp, &sent_len, 4);
|
|
157 tmp += 4;
|
|
158 memcpy(tmp, data, len);
|
|
159 tmp += len;
|
|
160
|
|
161 ret = write(directconn->fd, buffer, buf_size);
|
|
162
|
|
163 #ifdef DEBUG_DC
|
|
164 char *str;
|
|
165 str = g_strdup_printf("%s/msntest/w%.4d.bin", g_get_home_dir(), directconn->c);
|
|
166
|
|
167 FILE *tf = fopen(str, "w");
|
|
168 fwrite(buffer, 1, buf_size, tf);
|
|
169 fclose(tf);
|
|
170
|
|
171 g_free(str);
|
|
172 #endif
|
|
173
|
|
174 g_free(buffer);
|
|
175
|
|
176 #if 0
|
|
177 /* Let's write the length of the data. */
|
|
178 ret = write(directconn->fd, &len, sizeof(len));
|
|
179
|
|
180 /* Let's write the data. */
|
|
181 ret = write(directconn->fd, data, len);
|
|
182
|
|
183 char *str;
|
|
184 str = g_strdup_printf("/home/revo/msntest/w%.4d.bin", directconn->c);
|
|
185
|
|
186 FILE *tf = fopen(str, "w");
|
|
187 fwrite(&len, 1, sizeof(len), tf);
|
|
188 fwrite(data, 1, len, tf);
|
|
189 fclose(tf);
|
|
190
|
|
191 g_free(str);
|
|
192 #endif
|
|
193
|
|
194 directconn->c++;
|
|
195
|
|
196 return ret;
|
|
197 }
|
|
198
|
|
199 #if 0
|
|
200 void
|
|
201 msn_directconn_parse_nonce(MsnDirectConn *directconn, const char *nonce)
|
|
202 {
|
|
203 guint32 t1;
|
|
204 guint16 t2;
|
|
205 guint16 t3;
|
|
206 guint16 t4;
|
|
207 guint64 t5;
|
|
208
|
|
209 g_return_if_fail(directconn != NULL);
|
|
210 g_return_if_fail(nonce != NULL);
|
|
211
|
|
212 sscanf (nonce, "%08X-%04hX-%04hX-%04hX-%012llX", &t1, &t2, &t3, &t4, &t5);
|
|
213
|
|
214 t1 = GUINT32_TO_LE(t1);
|
|
215 t2 = GUINT16_TO_LE(t2);
|
|
216 t3 = GUINT16_TO_LE(t3);
|
|
217 t4 = GUINT16_TO_BE(t4);
|
|
218 t5 = GUINT64_TO_BE(t5);
|
|
219
|
|
220 directconn->slpheader = g_new0(MsnSlpHeader, 1);
|
|
221
|
|
222 directconn->slpheader->ack_id = t1;
|
|
223 directconn->slpheader->ack_sub_id = t2 | (t3 << 16);
|
|
224 directconn->slpheader->ack_size = t4 | t5;
|
|
225 }
|
|
226 #endif
|
|
227
|
|
228 void
|
|
229 msn_directconn_send_msg(MsnDirectConn *directconn, MsnMessage *msg)
|
|
230 {
|
|
231 char *body;
|
|
232 size_t body_len;
|
|
233
|
|
234 body = msn_message_gen_slp_body(msg, &body_len);
|
|
235
|
|
236 msn_directconn_write(directconn, body, body_len);
|
|
237 }
|
|
238
|
|
239 void
|
|
240 msn_directconn_process_msg(MsnDirectConn *directconn, MsnMessage *msg)
|
|
241 {
|
|
242 gaim_debug_info("msn", "directconn: process_msg\n");
|
|
243
|
|
244 msn_slplink_process_msg(directconn->slplink, msg);
|
|
245 }
|
|
246
|
|
247 static void
|
|
248 read_cb(gpointer data, gint source, GaimInputCondition cond)
|
|
249 {
|
|
250 MsnDirectConn* directconn;
|
|
251 char *body;
|
|
252 size_t len, body_len;
|
|
253
|
|
254 gaim_debug_info("msn", "read_cb: %d, %d\n", source, cond);
|
|
255
|
|
256 directconn = data;
|
|
257
|
|
258 /* Let's read the length of the data. */
|
|
259 len = read(directconn->fd, &body_len, sizeof(body_len));
|
|
260
|
|
261 if (len <= 0)
|
|
262 {
|
|
263 /* ERROR */
|
|
264 gaim_debug_error("msn", "error reading\n");
|
|
265
|
|
266 if (directconn->inpa)
|
|
267 gaim_input_remove(directconn->inpa);
|
|
268
|
|
269 close(directconn->fd);
|
|
270
|
|
271 msn_directconn_destroy(directconn);
|
|
272
|
|
273 return;
|
|
274 }
|
|
275
|
|
276 body_len = GUINT32_FROM_LE(body_len);
|
|
277
|
|
278 gaim_debug_info("msn", "body_len=%d\n", body_len);
|
|
279
|
|
280 if (body_len <= 0)
|
|
281 {
|
|
282 /* ERROR */
|
|
283 gaim_debug_error("msn", "error reading\n");
|
|
284
|
|
285 if (directconn->inpa)
|
|
286 gaim_input_remove(directconn->inpa);
|
|
287
|
|
288 close(directconn->fd);
|
|
289
|
|
290 msn_directconn_destroy(directconn);
|
|
291
|
|
292 return;
|
|
293 }
|
|
294
|
|
295 body = g_malloc(body_len);
|
|
296
|
|
297 /* Let's read the data. */
|
|
298 len = read(directconn->fd, body, body_len);
|
|
299
|
|
300 gaim_debug_info("msn", "len=%d\n", len);
|
|
301
|
|
302 if (len > 0)
|
|
303 {
|
|
304 MsnMessage *msg;
|
|
305
|
|
306 #ifdef DEBUG_DC
|
|
307 str = g_strdup_printf("/home/revo/msntest/r%.4d.bin", directconn->c);
|
|
308
|
|
309 FILE *tf = fopen(str, "w");
|
|
310 fwrite(body, 1, len, tf);
|
|
311 fclose(tf);
|
|
312
|
|
313 g_free(str);
|
|
314 #endif
|
|
315
|
|
316 directconn->c++;
|
|
317
|
|
318 msg = msn_message_new_msnslp();
|
|
319 msn_message_parse_slp_body(msg, body, body_len);
|
|
320
|
|
321 msn_directconn_process_msg(directconn, msg);
|
|
322 }
|
|
323 else
|
|
324 {
|
|
325 /* ERROR */
|
|
326 gaim_debug_error("msn", "error reading\n");
|
|
327
|
|
328 if (directconn->inpa)
|
|
329 gaim_input_remove(directconn->inpa);
|
|
330
|
|
331 close(directconn->fd);
|
|
332
|
|
333 msn_directconn_destroy(directconn);
|
|
334 }
|
|
335 }
|
|
336
|
|
337 static void
|
|
338 connect_cb(gpointer data, gint source, GaimInputCondition cond)
|
|
339 {
|
|
340 MsnDirectConn* directconn;
|
|
341 int fd;
|
|
342
|
|
343 gaim_debug_misc("msn", "directconn: connect_cb: %d, %d.\n", source, cond);
|
|
344
|
|
345 directconn = data;
|
|
346
|
|
347 if (TRUE)
|
|
348 {
|
|
349 fd = source;
|
|
350 }
|
|
351 else
|
|
352 {
|
|
353 struct sockaddr_in client_addr;
|
|
354 int client;
|
|
355 fd = accept (source, (struct sockaddr *)&client_addr, &client);
|
|
356 }
|
|
357
|
|
358 directconn->fd = fd;
|
|
359
|
|
360 if (fd > 0)
|
|
361 {
|
|
362 directconn->inpa = gaim_input_add(fd, GAIM_INPUT_READ, read_cb,
|
|
363 directconn);
|
|
364
|
|
365 if (TRUE)
|
|
366 {
|
|
367 /* Send foo. */
|
|
368 msn_directconn_write(directconn, "foo", strlen("foo") + 1);
|
|
369
|
|
370 /* Send Handshake */
|
|
371 msn_directconn_send_handshake(directconn);
|
|
372 }
|
|
373 else
|
|
374 {
|
|
375 }
|
|
376 }
|
|
377 else
|
|
378 {
|
|
379 /* ERROR */
|
|
380 gaim_debug_error("msn", "could not add input\n");
|
|
381
|
|
382 if (directconn->inpa)
|
|
383 gaim_input_remove(directconn->inpa);
|
|
384
|
|
385 close(directconn->fd);
|
|
386 }
|
|
387 }
|
|
388
|
|
389 gboolean
|
|
390 msn_directconn_connect(MsnDirectConn *directconn, const char *host, int port)
|
|
391 {
|
|
392 MsnSession *session;
|
|
393 int r;
|
|
394
|
|
395 g_return_val_if_fail(directconn != NULL, FALSE);
|
|
396 g_return_val_if_fail(host != NULL, TRUE);
|
|
397 g_return_val_if_fail(port > 0, FALSE);
|
|
398
|
|
399 session = directconn->slplink->session;
|
|
400
|
|
401 #if 0
|
|
402 if (session->http_method)
|
|
403 {
|
|
404 servconn->http_data->gateway_host = g_strdup(host);
|
|
405 }
|
|
406 #endif
|
|
407
|
|
408 r = gaim_proxy_connect(session->account, host, port, connect_cb,
|
|
409 directconn);
|
|
410
|
|
411 if (r == 0)
|
|
412 {
|
|
413 return TRUE;
|
|
414 }
|
|
415 else
|
|
416 return FALSE;
|
|
417 }
|
|
418
|
|
419 void
|
|
420 msn_directconn_listen(MsnDirectConn *directconn)
|
|
421 {
|
|
422 int port;
|
|
423 int fd;
|
|
424
|
|
425 port = 7000;
|
|
426
|
|
427 for (fd = -1; fd < 0;)
|
|
428 fd = create_listener(++port);
|
|
429
|
|
430 directconn->fd = fd;
|
|
431
|
|
432 directconn->inpa = gaim_input_add(fd, GAIM_INPUT_READ,
|
|
433 connect_cb, directconn);
|
|
434
|
|
435 directconn->port = port;
|
|
436 directconn->c = 0;
|
|
437 }
|
|
438
|
|
439 MsnDirectConn*
|
|
440 msn_directconn_new(MsnSlpLink *slplink)
|
|
441 {
|
|
442 MsnDirectConn *directconn;
|
|
443
|
|
444 directconn = g_new0(MsnDirectConn, 1);
|
|
445
|
|
446 directconn->slplink = slplink;
|
|
447
|
|
448 if (slplink->directconn != NULL)
|
|
449 gaim_debug_info("msn", "got_transresp: LEAK\n");
|
|
450
|
|
451 slplink->directconn = directconn;
|
|
452
|
|
453 return directconn;
|
|
454 }
|
|
455
|
|
456 void
|
|
457 msn_directconn_destroy(MsnDirectConn *directconn)
|
|
458 {
|
|
459 if (directconn->nonce != NULL)
|
|
460 g_free(directconn->nonce);
|
|
461
|
|
462 directconn->slplink->directconn = NULL;
|
|
463
|
|
464 g_free(directconn);
|
|
465 }
|