comparison libpurple/protocols/msn/servconn.c @ 20394:4a099e4d0d09

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