Mercurial > pidgin.yaz
annotate libgaim/protocols/msn/servconn.c @ 20390:d634f88e25d8
msn.tgz from SF Patch #1621854 from Ka-Hing Cheung
"This tarball brings soc-2006-msnp13 up to head. In addition to that it also
fixes a crash with sending offline messages.
I wasn't able to generate a diff against that branch, svn seems to insist
on diff'ing against HEAD after I run the merge command. After running `svn
merge -r 16309:HEAD https://gaim.svn.sourceforge.net/svnroot/gaim/trunk` on
the soc-2006-msnp13 you can replace the msn directory with the attached
tarball. The fix for offline messaging is on msn.c:901:
if (!session->oim)
session->oim = msn_oim_new(session)"
committer: Richard Laager <rlaager@wiktel.com>
author | Ka-Hing Cheung <khc@hxbc.us> |
---|---|
date | Sun, 15 Apr 2007 02:18:17 +0000 |
parents | 118fd0dc5b6e |
children | 0b0ecee55091 9755b2f7bb0f |
rev | line source |
---|---|
14192 | 1 /** |
2 * @file servconn.c Server connection functions | |
3 * | |
4 * gaim | |
5 * | |
6 * Gaim is the legal property of its developers, whose names are too numerous | |
7 * to list here. Please refer to the COPYRIGHT file distributed with this | |
8 * source distribution. | |
9 * | |
10 * This program is free software; you can redistribute it and/or modify | |
11 * it under the terms of the GNU General Public License as published by | |
12 * the Free Software Foundation; either version 2 of the License, or | |
13 * (at your option) any later version. | |
14 * | |
15 * This program is distributed in the hope that it will be useful, | |
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 * GNU General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program; if not, write to the Free Software | |
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
23 */ | |
24 #include "msn.h" | |
25 #include "servconn.h" | |
26 #include "error.h" | |
27 | |
28 static void read_cb(gpointer data, gint source, GaimInputCondition cond); | |
29 | |
30 /************************************************************************** | |
31 * Main | |
32 **************************************************************************/ | |
33 | |
34 MsnServConn * | |
35 msn_servconn_new(MsnSession *session, MsnServConnType type) | |
36 { | |
37 MsnServConn *servconn; | |
38 | |
39 g_return_val_if_fail(session != NULL, NULL); | |
40 | |
41 servconn = g_new0(MsnServConn, 1); | |
42 | |
43 servconn->type = type; | |
44 | |
45 servconn->session = session; | |
46 servconn->cmdproc = msn_cmdproc_new(session); | |
47 servconn->cmdproc->servconn = servconn; | |
48 | |
49 servconn->httpconn = msn_httpconn_new(servconn); | |
50 | |
51 servconn->num = session->servconns_count++; | |
52 | |
53 servconn->tx_buf = gaim_circ_buffer_new(MSN_BUF_LEN); | |
54 servconn->tx_handler = -1; | |
55 | |
56 return servconn; | |
57 } | |
58 | |
59 void | |
60 msn_servconn_destroy(MsnServConn *servconn) | |
61 { | |
62 g_return_if_fail(servconn != NULL); | |
63 | |
64 if (servconn->processing) | |
65 { | |
66 servconn->wasted = TRUE; | |
67 return; | |
68 } | |
69 | |
70 if (servconn->connected) | |
71 msn_servconn_disconnect(servconn); | |
72 | |
73 if (servconn->destroy_cb) | |
74 servconn->destroy_cb(servconn); | |
75 | |
76 if (servconn->httpconn != NULL) | |
77 msn_httpconn_destroy(servconn->httpconn); | |
78 | |
79 g_free(servconn->host); | |
80 | |
81 gaim_circ_buffer_destroy(servconn->tx_buf); | |
82 if (servconn->tx_handler > 0) | |
83 gaim_input_remove(servconn->tx_handler); | |
84 | |
85 msn_cmdproc_destroy(servconn->cmdproc); | |
86 g_free(servconn); | |
87 } | |
88 | |
89 void | |
90 msn_servconn_set_connect_cb(MsnServConn *servconn, | |
91 void (*connect_cb)(MsnServConn *)) | |
92 { | |
93 g_return_if_fail(servconn != NULL); | |
94 servconn->connect_cb = connect_cb; | |
95 } | |
96 | |
97 void | |
98 msn_servconn_set_disconnect_cb(MsnServConn *servconn, | |
99 void (*disconnect_cb)(MsnServConn *)) | |
100 { | |
101 g_return_if_fail(servconn != NULL); | |
102 | |
103 servconn->disconnect_cb = disconnect_cb; | |
104 } | |
105 | |
106 void | |
107 msn_servconn_set_destroy_cb(MsnServConn *servconn, | |
108 void (*destroy_cb)(MsnServConn *)) | |
109 { | |
110 g_return_if_fail(servconn != NULL); | |
111 | |
112 servconn->destroy_cb = destroy_cb; | |
113 } | |
114 | |
115 /************************************************************************** | |
116 * Utility | |
117 **************************************************************************/ | |
118 | |
119 void | |
120 msn_servconn_got_error(MsnServConn *servconn, MsnServConnError error) | |
121 { | |
122 char *tmp; | |
123 const char *reason; | |
124 | |
125 const char *names[] = { "Notification", "Switchboard" }; | |
126 const char *name; | |
127 | |
128 name = names[servconn->type]; | |
129 | |
130 switch (error) | |
131 { | |
132 case MSN_SERVCONN_ERROR_CONNECT: | |
133 reason = _("Unable to connect"); break; | |
134 case MSN_SERVCONN_ERROR_WRITE: | |
135 reason = _("Writing error"); break; | |
136 case MSN_SERVCONN_ERROR_READ: | |
137 reason = _("Reading error"); break; | |
138 default: | |
139 reason = _("Unknown error"); break; | |
140 } | |
141 | |
142 gaim_debug_error("msn", "Connection error from %s server (%s): %s\n", | |
143 name, servconn->host, reason); | |
144 tmp = g_strdup_printf(_("Connection error from %s server:\n%s"), | |
145 name, reason); | |
146 | |
147 if (servconn->type == MSN_SERVCONN_NS) | |
148 { | |
149 msn_session_set_error(servconn->session, MSN_ERROR_SERVCONN, tmp); | |
150 } | |
151 else if (servconn->type == MSN_SERVCONN_SB) | |
152 { | |
153 MsnSwitchBoard *swboard; | |
154 swboard = servconn->cmdproc->data; | |
155 if (swboard != NULL) | |
156 swboard->error = MSN_SB_ERROR_CONNECTION; | |
157 } | |
158 | |
159 msn_servconn_disconnect(servconn); | |
160 | |
161 g_free(tmp); | |
162 } | |
163 | |
164 /************************************************************************** | |
165 * Connect | |
166 **************************************************************************/ | |
167 | |
168 static void | |
20390
d634f88e25d8
msn.tgz from SF Patch #1621854 from Ka-Hing Cheung
Ka-Hing Cheung <khc@hxbc.us>
parents:
14837
diff
changeset
|
169 connect_cb(gpointer data, gint source, GaimInputCondition cond) |
14192 | 170 { |
20390
d634f88e25d8
msn.tgz from SF Patch #1621854 from Ka-Hing Cheung
Ka-Hing Cheung <khc@hxbc.us>
parents:
14837
diff
changeset
|
171 MsnServConn *servconn = data; |
14192 | 172 |
173 servconn = data; | |
14262 | 174 servconn->connect_data = NULL; |
14192 | 175 servconn->processing = FALSE; |
176 | |
177 if (servconn->wasted) | |
178 { | |
14281 | 179 if (source >= 0) |
180 close(source); | |
14192 | 181 msn_servconn_destroy(servconn); |
182 return; | |
183 } | |
184 | |
185 servconn->fd = source; | |
186 | |
14281 | 187 if (source >= 0) |
14192 | 188 { |
189 servconn->connected = TRUE; | |
190 | |
191 /* Someone wants to know we connected. */ | |
192 servconn->connect_cb(servconn); | |
193 servconn->inpa = gaim_input_add(servconn->fd, GAIM_INPUT_READ, | |
194 read_cb, data); | |
195 } | |
196 else | |
197 { | |
198 msn_servconn_got_error(servconn, MSN_SERVCONN_ERROR_CONNECT); | |
199 } | |
200 } | |
201 | |
202 gboolean | |
203 msn_servconn_connect(MsnServConn *servconn, const char *host, int port) | |
204 { | |
205 MsnSession *session; | |
20390
d634f88e25d8
msn.tgz from SF Patch #1621854 from Ka-Hing Cheung
Ka-Hing Cheung <khc@hxbc.us>
parents:
14837
diff
changeset
|
206 int r; |
14192 | 207 |
208 g_return_val_if_fail(servconn != NULL, FALSE); | |
209 g_return_val_if_fail(host != NULL, FALSE); | |
210 g_return_val_if_fail(port > 0, FALSE); | |
211 | |
212 session = servconn->session; | |
213 | |
214 if (servconn->connected) | |
215 msn_servconn_disconnect(servconn); | |
216 | |
14837 | 217 g_free(servconn->host); |
14192 | 218 servconn->host = g_strdup(host); |
219 | |
220 if (session->http_method) | |
221 { | |
222 /* HTTP Connection. */ | |
223 | |
224 if (!servconn->httpconn->connected) | |
225 if (!msn_httpconn_connect(servconn->httpconn, host, port)) | |
226 return FALSE;; | |
227 | |
228 servconn->connected = TRUE; | |
229 servconn->httpconn->virgin = TRUE; | |
230 | |
231 /* Someone wants to know we connected. */ | |
232 servconn->connect_cb(servconn); | |
233 | |
234 return TRUE; | |
235 } | |
236 | |
14837 | 237 servconn->connect_data = gaim_proxy_connect(NULL, session->account, |
238 host, port, connect_cb, servconn); | |
14192 | 239 |
14262 | 240 if (servconn->connect_data != NULL) |
14192 | 241 { |
242 servconn->processing = TRUE; | |
243 return TRUE; | |
20390
d634f88e25d8
msn.tgz from SF Patch #1621854 from Ka-Hing Cheung
Ka-Hing Cheung <khc@hxbc.us>
parents:
14837
diff
changeset
|
244 }else{ |
d634f88e25d8
msn.tgz from SF Patch #1621854 from Ka-Hing Cheung
Ka-Hing Cheung <khc@hxbc.us>
parents:
14837
diff
changeset
|
245 return FALSE; |
14192 | 246 } |
247 } | |
248 | |
249 void | |
250 msn_servconn_disconnect(MsnServConn *servconn) | |
251 { | |
252 g_return_if_fail(servconn != NULL); | |
253 | |
254 if (!servconn->connected) | |
255 { | |
256 /* We could not connect. */ | |
257 if (servconn->disconnect_cb != NULL) | |
258 servconn->disconnect_cb(servconn); | |
259 | |
260 return; | |
261 } | |
262 | |
263 if (servconn->session->http_method) | |
264 { | |
265 /* Fake disconnection. */ | |
266 if (servconn->disconnect_cb != NULL) | |
267 servconn->disconnect_cb(servconn); | |
268 | |
269 return; | |
270 } | |
271 | |
14262 | 272 if (servconn->connect_data != NULL) |
14192 | 273 { |
14262 | 274 gaim_proxy_connect_cancel(servconn->connect_data); |
275 servconn->connect_data = NULL; | |
14192 | 276 } |
277 | |
278 if (servconn->inpa > 0) | |
279 { | |
280 gaim_input_remove(servconn->inpa); | |
281 servconn->inpa = 0; | |
282 } | |
283 | |
284 close(servconn->fd); | |
285 | |
286 servconn->rx_buf = NULL; | |
287 servconn->rx_len = 0; | |
288 servconn->payload_len = 0; | |
289 | |
290 servconn->connected = FALSE; | |
291 | |
292 if (servconn->disconnect_cb != NULL) | |
293 servconn->disconnect_cb(servconn); | |
294 } | |
295 | |
296 static void | |
297 servconn_write_cb(gpointer data, gint source, GaimInputCondition cond) | |
298 { | |
299 MsnServConn *servconn = data; | |
300 int ret, writelen; | |
301 | |
302 writelen = gaim_circ_buffer_get_max_read(servconn->tx_buf); | |
303 | |
304 if (writelen == 0) { | |
305 gaim_input_remove(servconn->tx_handler); | |
306 servconn->tx_handler = -1; | |
307 return; | |
308 } | |
309 | |
310 ret = write(servconn->fd, servconn->tx_buf->outptr, writelen); | |
311 | |
312 if (ret < 0 && errno == EAGAIN) | |
313 return; | |
314 else if (ret <= 0) { | |
315 msn_servconn_got_error(servconn, MSN_SERVCONN_ERROR_WRITE); | |
316 return; | |
317 } | |
318 | |
319 gaim_circ_buffer_mark_read(servconn->tx_buf, ret); | |
320 } | |
321 | |
322 ssize_t | |
323 msn_servconn_write(MsnServConn *servconn, const char *buf, size_t len) | |
324 { | |
325 ssize_t ret = 0; | |
326 | |
327 g_return_val_if_fail(servconn != NULL, 0); | |
328 | |
329 if (!servconn->session->http_method) | |
330 { | |
331 if (servconn->tx_handler == -1) { | |
332 switch (servconn->type) | |
333 { | |
334 case MSN_SERVCONN_NS: | |
335 case MSN_SERVCONN_SB: | |
336 ret = write(servconn->fd, buf, len); | |
337 break; | |
338 #if 0 | |
339 case MSN_SERVCONN_DC: | |
340 ret = write(servconn->fd, &buf, sizeof(len)); | |
341 ret = write(servconn->fd, buf, len); | |
342 break; | |
343 #endif | |
344 default: | |
345 ret = write(servconn->fd, buf, len); | |
346 break; | |
347 } | |
348 } else { | |
349 ret = -1; | |
350 errno = EAGAIN; | |
351 } | |
352 | |
353 if (ret < 0 && errno == EAGAIN) | |
354 ret = 0; | |
355 if (ret < len) { | |
356 if (servconn->tx_handler == -1) | |
357 servconn->tx_handler = gaim_input_add( | |
358 servconn->fd, GAIM_INPUT_WRITE, | |
359 servconn_write_cb, servconn); | |
360 gaim_circ_buffer_append(servconn->tx_buf, buf + ret, | |
361 len - ret); | |
362 } | |
363 } | |
364 else | |
365 { | |
366 ret = msn_httpconn_write(servconn->httpconn, buf, len); | |
367 } | |
368 | |
369 if (ret == -1) | |
370 { | |
371 msn_servconn_got_error(servconn, MSN_SERVCONN_ERROR_WRITE); | |
372 } | |
373 | |
374 return ret; | |
375 } | |
376 | |
377 static void | |
378 read_cb(gpointer data, gint source, GaimInputCondition cond) | |
379 { | |
380 MsnServConn *servconn; | |
381 MsnSession *session; | |
382 char buf[MSN_BUF_LEN]; | |
383 char *cur, *end, *old_rx_buf; | |
384 int len, cur_len; | |
385 | |
386 servconn = data; | |
387 session = servconn->session; | |
388 | |
389 len = read(servconn->fd, buf, sizeof(buf) - 1); | |
390 | |
391 if (len < 0 && errno == EAGAIN) | |
392 return; | |
393 else if (len <= 0) | |
394 { | |
395 gaim_debug_error("msn", "servconn read error, len: %d error: %s\n", len, strerror(errno)); | |
396 msn_servconn_got_error(servconn, MSN_SERVCONN_ERROR_READ); | |
397 | |
398 return; | |
399 } | |
400 | |
401 buf[len] = '\0'; | |
402 | |
403 servconn->rx_buf = g_realloc(servconn->rx_buf, len + servconn->rx_len + 1); | |
404 memcpy(servconn->rx_buf + servconn->rx_len, buf, len + 1); | |
405 servconn->rx_len += len; | |
406 | |
407 end = old_rx_buf = servconn->rx_buf; | |
408 | |
409 servconn->processing = TRUE; | |
410 | |
411 do | |
412 { | |
413 cur = end; | |
414 | |
415 if (servconn->payload_len) | |
416 { | |
417 if (servconn->payload_len > servconn->rx_len) | |
418 /* The payload is still not complete. */ | |
419 break; | |
420 | |
421 cur_len = servconn->payload_len; | |
422 end += cur_len; | |
423 } | |
424 else | |
425 { | |
426 end = strstr(cur, "\r\n"); | |
427 | |
428 if (end == NULL) | |
429 /* The command is still not complete. */ | |
430 break; | |
431 | |
432 *end = '\0'; | |
433 end += 2; | |
434 cur_len = end - cur; | |
435 } | |
436 | |
437 servconn->rx_len -= cur_len; | |
438 | |
20390
d634f88e25d8
msn.tgz from SF Patch #1621854 from Ka-Hing Cheung
Ka-Hing Cheung <khc@hxbc.us>
parents:
14837
diff
changeset
|
439 if (servconn->payload_len){ |
14192 | 440 msn_cmdproc_process_payload(servconn->cmdproc, cur, cur_len); |
441 servconn->payload_len = 0; | |
20390
d634f88e25d8
msn.tgz from SF Patch #1621854 from Ka-Hing Cheung
Ka-Hing Cheung <khc@hxbc.us>
parents:
14837
diff
changeset
|
442 }else{ |
14192 | 443 msn_cmdproc_process_cmd_text(servconn->cmdproc, cur); |
20390
d634f88e25d8
msn.tgz from SF Patch #1621854 from Ka-Hing Cheung
Ka-Hing Cheung <khc@hxbc.us>
parents:
14837
diff
changeset
|
444 servconn->payload_len = servconn->cmdproc->last_cmd->payload_len; |
14192 | 445 } |
446 } while (servconn->connected && !servconn->wasted && servconn->rx_len > 0); | |
447 | |
448 if (servconn->connected && !servconn->wasted) | |
449 { | |
450 if (servconn->rx_len > 0) | |
451 servconn->rx_buf = g_memdup(cur, servconn->rx_len); | |
452 else | |
453 servconn->rx_buf = NULL; | |
454 } | |
455 | |
456 servconn->processing = FALSE; | |
457 | |
458 if (servconn->wasted) | |
459 msn_servconn_destroy(servconn); | |
460 | |
461 g_free(old_rx_buf); | |
462 } | |
463 | |
464 #if 0 | |
465 static int | |
466 create_listener(int port) | |
467 { | |
468 int fd; | |
469 const int on = 1; | |
470 | |
471 #if 0 | |
472 struct addrinfo hints; | |
473 struct addrinfo *c, *res; | |
474 char port_str[5]; | |
475 | |
476 snprintf(port_str, sizeof(port_str), "%d", port); | |
477 | |
478 memset(&hints, 0, sizeof(hints)); | |
479 | |
480 hints.ai_flags = AI_PASSIVE; | |
481 hints.ai_family = AF_UNSPEC; | |
482 hints.ai_socktype = SOCK_STREAM; | |
483 | |
484 if (getaddrinfo(NULL, port_str, &hints, &res) != 0) | |
485 { | |
486 gaim_debug_error("msn", "Could not get address info: %s.\n", | |
487 port_str); | |
488 return -1; | |
489 } | |
490 | |
491 for (c = res; c != NULL; c = c->ai_next) | |
492 { | |
493 fd = socket(c->ai_family, c->ai_socktype, c->ai_protocol); | |
494 | |
495 if (fd < 0) | |
496 continue; | |
497 | |
498 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); | |
499 | |
500 if (bind(fd, c->ai_addr, c->ai_addrlen) == 0) | |
501 break; | |
502 | |
503 close(fd); | |
504 } | |
505 | |
506 if (c == NULL) | |
507 { | |
508 gaim_debug_error("msn", "Could not find socket: %s.\n", port_str); | |
509 return -1; | |
510 } | |
511 | |
512 freeaddrinfo(res); | |
513 #else | |
514 struct sockaddr_in sockin; | |
515 | |
516 fd = socket(AF_INET, SOCK_STREAM, 0); | |
517 | |
518 if (fd < 0) | |
519 return -1; | |
520 | |
521 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) != 0) | |
522 { | |
523 close(fd); | |
524 return -1; | |
525 } | |
526 | |
527 memset(&sockin, 0, sizeof(struct sockaddr_in)); | |
528 sockin.sin_family = AF_INET; | |
529 sockin.sin_port = htons(port); | |
530 | |
531 if (bind(fd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) | |
532 { | |
533 close(fd); | |
534 return -1; | |
535 } | |
536 #endif | |
537 | |
538 if (listen (fd, 4) != 0) | |
539 { | |
540 close (fd); | |
541 return -1; | |
542 } | |
543 | |
544 fcntl(fd, F_SETFL, O_NONBLOCK); | |
545 | |
546 return fd; | |
547 } | |
548 #endif |