comparison libpurple/protocols/msn/servconn.c @ 20398:61d6a3dfbb3c

propagate from branch 'im.pidgin.rlaager.merging.msnp13-and-sf-1621854-4-rlaager-whitespace' (head 51cec0ffea45c8589dcb7bf0f9e36e749ed43017) to branch 'im.pidgin.rlaager.merging.msnp13-and-pidgin' (head d0d075250a037e5d0a268a39501bf169465061a4)
author Richard Laager <rlaager@wiktel.com>
date Sun, 15 Apr 2007 03:56:08 +0000
parents 4a099e4d0d09
children 6f986caeab59 4ddc27c18781
comparison
equal deleted inserted replaced
20397:6ac7e33fdabf 20398:61d6a3dfbb3c
1 /**
2 * @file servconn.c Server connection functions
3 *
4 * purple
5 *
6 * Purple 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, PurpleInputCondition 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 = purple_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 purple_circ_buffer_destroy(servconn->tx_buf);
82 if (servconn->tx_handler > 0)
83 purple_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 purple_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
169 connect_cb(gpointer data, gint source, GaimInputCondition cond)
170 {
171 MsnServConn *servconn = data;
172
173 servconn = data;
174 servconn->connect_data = NULL;
175 servconn->processing = FALSE;
176
177 if (servconn->wasted)
178 {
179 if (source >= 0)
180 close(source);
181 msn_servconn_destroy(servconn);
182 return;
183 }
184
185 servconn->fd = source;
186
187 if (source >= 0)
188 {
189 servconn->connected = TRUE;
190
191 /* Someone wants to know we connected. */
192 servconn->connect_cb(servconn);
193 servconn->inpa = purple_input_add(servconn->fd, PURPLE_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;
206
207 g_return_val_if_fail(servconn != NULL, FALSE);
208 g_return_val_if_fail(host != NULL, FALSE);
209 g_return_val_if_fail(port > 0, FALSE);
210
211 session = servconn->session;
212
213 if (servconn->connected)
214 msn_servconn_disconnect(servconn);
215
216 g_free(servconn->host);
217 servconn->host = g_strdup(host);
218
219 if (session->http_method)
220 {
221 /* HTTP Connection. */
222
223 if (!servconn->httpconn->connected)
224 if (!msn_httpconn_connect(servconn->httpconn, host, port))
225 return FALSE;;
226
227 servconn->connected = TRUE;
228 servconn->httpconn->virgin = TRUE;
229
230 /* Someone wants to know we connected. */
231 servconn->connect_cb(servconn);
232
233 return TRUE;
234 }
235
236 servconn->connect_data = purple_proxy_connect(NULL, session->account,
237 host, port, connect_cb, servconn);
238
239 if (servconn->connect_data != NULL)
240 {
241 servconn->processing = TRUE;
242 return TRUE;
243 }
244 else
245 {
246 return FALSE;
247 }
248 }
249
250 void
251 msn_servconn_disconnect(MsnServConn *servconn)
252 {
253 g_return_if_fail(servconn != NULL);
254
255 if (!servconn->connected)
256 {
257 /* We could not connect. */
258 if (servconn->disconnect_cb != NULL)
259 servconn->disconnect_cb(servconn);
260
261 return;
262 }
263
264 if (servconn->session->http_method)
265 {
266 /* Fake disconnection. */
267 if (servconn->disconnect_cb != NULL)
268 servconn->disconnect_cb(servconn);
269
270 return;
271 }
272
273 if (servconn->connect_data != NULL)
274 {
275 purple_proxy_connect_cancel(servconn->connect_data);
276 servconn->connect_data = NULL;
277 }
278
279 if (servconn->inpa > 0)
280 {
281 purple_input_remove(servconn->inpa);
282 servconn->inpa = 0;
283 }
284
285 close(servconn->fd);
286
287 servconn->rx_buf = NULL;
288 servconn->rx_len = 0;
289 servconn->payload_len = 0;
290
291 servconn->connected = FALSE;
292
293 if (servconn->disconnect_cb != NULL)
294 servconn->disconnect_cb(servconn);
295 }
296
297 static void
298 servconn_write_cb(gpointer data, gint source, PurpleInputCondition cond)
299 {
300 MsnServConn *servconn = data;
301 int ret, writelen;
302
303 writelen = purple_circ_buffer_get_max_read(servconn->tx_buf);
304
305 if (writelen == 0) {
306 purple_input_remove(servconn->tx_handler);
307 servconn->tx_handler = -1;
308 return;
309 }
310
311 ret = write(servconn->fd, servconn->tx_buf->outptr, writelen);
312
313 if (ret < 0 && errno == EAGAIN)
314 return;
315 else if (ret <= 0) {
316 msn_servconn_got_error(servconn, MSN_SERVCONN_ERROR_WRITE);
317 return;
318 }
319
320 purple_circ_buffer_mark_read(servconn->tx_buf, ret);
321 }
322
323 ssize_t
324 msn_servconn_write(MsnServConn *servconn, const char *buf, size_t len)
325 {
326 ssize_t ret = 0;
327
328 g_return_val_if_fail(servconn != NULL, 0);
329
330 if (!servconn->session->http_method)
331 {
332 if (servconn->tx_handler == -1) {
333 switch (servconn->type)
334 {
335 case MSN_SERVCONN_NS:
336 case MSN_SERVCONN_SB:
337 ret = write(servconn->fd, buf, len);
338 break;
339 #if 0
340 case MSN_SERVCONN_DC:
341 ret = write(servconn->fd, &buf, sizeof(len));
342 ret = write(servconn->fd, buf, len);
343 break;
344 #endif
345 default:
346 ret = write(servconn->fd, buf, len);
347 break;
348 }
349 } else {
350 ret = -1;
351 errno = EAGAIN;
352 }
353
354 if (ret < 0 && errno == EAGAIN)
355 ret = 0;
356 if (ret < len) {
357 if (servconn->tx_handler == -1)
358 servconn->tx_handler = purple_input_add(
359 servconn->fd, PURPLE_INPUT_WRITE,
360 servconn_write_cb, servconn);
361 purple_circ_buffer_append(servconn->tx_buf, buf + ret,
362 len - ret);
363 }
364 }
365 else
366 {
367 ret = msn_httpconn_write(servconn->httpconn, buf, len);
368 }
369
370 if (ret == -1)
371 {
372 msn_servconn_got_error(servconn, MSN_SERVCONN_ERROR_WRITE);
373 }
374
375 return ret;
376 }
377
378 static void
379 read_cb(gpointer data, gint source, PurpleInputCondition cond)
380 {
381 MsnServConn *servconn;
382 MsnSession *session;
383 char buf[MSN_BUF_LEN];
384 char *cur, *end, *old_rx_buf;
385 int len, cur_len;
386
387 servconn = data;
388 session = servconn->session;
389
390 len = read(servconn->fd, buf, sizeof(buf) - 1);
391
392 if (len < 0 && errno == EAGAIN)
393 return;
394 else if (len <= 0)
395 {
396 purple_debug_error("msn", "servconn read error, len: %d error: %s\n", len, strerror(errno));
397 msn_servconn_got_error(servconn, MSN_SERVCONN_ERROR_READ);
398
399 return;
400 }
401
402 buf[len] = '\0';
403
404 servconn->rx_buf = g_realloc(servconn->rx_buf, len + servconn->rx_len + 1);
405 memcpy(servconn->rx_buf + servconn->rx_len, buf, len + 1);
406 servconn->rx_len += len;
407
408 end = old_rx_buf = servconn->rx_buf;
409
410 servconn->processing = TRUE;
411
412 do
413 {
414 cur = end;
415
416 if (servconn->payload_len)
417 {
418 if (servconn->payload_len > servconn->rx_len)
419 /* The payload is still not complete. */
420 break;
421
422 cur_len = servconn->payload_len;
423 end += cur_len;
424 }
425 else
426 {
427 end = strstr(cur, "\r\n");
428
429 if (end == NULL)
430 /* The command is still not complete. */
431 break;
432
433 *end = '\0';
434 end += 2;
435 cur_len = end - cur;
436 }
437
438 servconn->rx_len -= cur_len;
439
440 if (servconn->payload_len)
441 {
442 msn_cmdproc_process_payload(servconn->cmdproc, cur, cur_len);
443 servconn->payload_len = 0;
444 }
445 else
446 {
447 msn_cmdproc_process_cmd_text(servconn->cmdproc, cur);
448 servconn->payload_len = servconn->cmdproc->last_cmd->payload_len;
449 }
450 } while (servconn->connected && !servconn->wasted && servconn->rx_len > 0);
451
452 if (servconn->connected && !servconn->wasted)
453 {
454 if (servconn->rx_len > 0)
455 servconn->rx_buf = g_memdup(cur, servconn->rx_len);
456 else
457 servconn->rx_buf = NULL;
458 }
459
460 servconn->processing = FALSE;
461
462 if (servconn->wasted)
463 msn_servconn_destroy(servconn);
464
465 g_free(old_rx_buf);
466 }
467
468 #if 0
469 static int
470 create_listener(int port)
471 {
472 int fd;
473 const int on = 1;
474
475 #if 0
476 struct addrinfo hints;
477 struct addrinfo *c, *res;
478 char port_str[5];
479
480 snprintf(port_str, sizeof(port_str), "%d", port);
481
482 memset(&hints, 0, sizeof(hints));
483
484 hints.ai_flags = AI_PASSIVE;
485 hints.ai_family = AF_UNSPEC;
486 hints.ai_socktype = SOCK_STREAM;
487
488 if (getaddrinfo(NULL, port_str, &hints, &res) != 0)
489 {
490 purple_debug_error("msn", "Could not get address info: %s.\n",
491 port_str);
492 return -1;
493 }
494
495 for (c = res; c != NULL; c = c->ai_next)
496 {
497 fd = socket(c->ai_family, c->ai_socktype, c->ai_protocol);
498
499 if (fd < 0)
500 continue;
501
502 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
503
504 if (bind(fd, c->ai_addr, c->ai_addrlen) == 0)
505 break;
506
507 close(fd);
508 }
509
510 if (c == NULL)
511 {
512 purple_debug_error("msn", "Could not find socket: %s.\n", port_str);
513 return -1;
514 }
515
516 freeaddrinfo(res);
517 #else
518 struct sockaddr_in sockin;
519
520 fd = socket(AF_INET, SOCK_STREAM, 0);
521
522 if (fd < 0)
523 return -1;
524
525 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) != 0)
526 {
527 close(fd);
528 return -1;
529 }
530
531 memset(&sockin, 0, sizeof(struct sockaddr_in));
532 sockin.sin_family = AF_INET;
533 sockin.sin_port = htons(port);
534
535 if (bind(fd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0)
536 {
537 close(fd);
538 return -1;
539 }
540 #endif
541
542 if (listen (fd, 4) != 0)
543 {
544 close (fd);
545 return -1;
546 }
547
548 fcntl(fd, F_SETFL, O_NONBLOCK);
549
550 return fd;
551 }
552 #endif