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
|
|
169 connect_cb(gpointer data, gint source, const gchar *error_message)
|
|
170 {
|
|
171 MsnServConn *servconn;
|
|
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;
|
|
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 if (servconn->host != NULL)
|
|
217 g_free(servconn->host);
|
|
218
|
|
219 servconn->host = g_strdup(host);
|
|
220
|
|
221 if (session->http_method)
|
|
222 {
|
|
223 /* HTTP Connection. */
|
|
224
|
|
225 if (!servconn->httpconn->connected)
|
|
226 if (!msn_httpconn_connect(servconn->httpconn, host, port))
|
|
227 return FALSE;;
|
|
228
|
|
229 servconn->connected = TRUE;
|
|
230 servconn->httpconn->virgin = TRUE;
|
|
231
|
|
232 /* Someone wants to know we connected. */
|
|
233 servconn->connect_cb(servconn);
|
|
234
|
|
235 return TRUE;
|
|
236 }
|
|
237
|
14262
|
238 servconn->connect_data = gaim_proxy_connect(session->account, host, port,
|
14192
|
239 connect_cb, servconn);
|
|
240
|
14262
|
241 if (servconn->connect_data != NULL)
|
14192
|
242 {
|
|
243 servconn->processing = TRUE;
|
|
244 return TRUE;
|
|
245 }
|
|
246 else
|
|
247 return FALSE;
|
|
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
|
14262
|
273 if (servconn->connect_data != NULL)
|
14192
|
274 {
|
14262
|
275 gaim_proxy_connect_cancel(servconn->connect_data);
|
|
276 servconn->connect_data = NULL;
|
14192
|
277 }
|
|
278
|
|
279 if (servconn->inpa > 0)
|
|
280 {
|
|
281 gaim_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, GaimInputCondition cond)
|
|
299 {
|
|
300 MsnServConn *servconn = data;
|
|
301 int ret, writelen;
|
|
302
|
|
303 writelen = gaim_circ_buffer_get_max_read(servconn->tx_buf);
|
|
304
|
|
305 if (writelen == 0) {
|
|
306 gaim_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 gaim_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 = gaim_input_add(
|
|
359 servconn->fd, GAIM_INPUT_WRITE,
|
|
360 servconn_write_cb, servconn);
|
|
361 gaim_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, GaimInputCondition 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 gaim_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 }
|
|
449 } while (servconn->connected && !servconn->wasted && servconn->rx_len > 0);
|
|
450
|
|
451 if (servconn->connected && !servconn->wasted)
|
|
452 {
|
|
453 if (servconn->rx_len > 0)
|
|
454 servconn->rx_buf = g_memdup(cur, servconn->rx_len);
|
|
455 else
|
|
456 servconn->rx_buf = NULL;
|
|
457 }
|
|
458
|
|
459 servconn->processing = FALSE;
|
|
460
|
|
461 if (servconn->wasted)
|
|
462 msn_servconn_destroy(servconn);
|
|
463
|
|
464 g_free(old_rx_buf);
|
|
465 }
|
|
466
|
|
467 #if 0
|
|
468 static int
|
|
469 create_listener(int port)
|
|
470 {
|
|
471 int fd;
|
|
472 const int on = 1;
|
|
473
|
|
474 #if 0
|
|
475 struct addrinfo hints;
|
|
476 struct addrinfo *c, *res;
|
|
477 char port_str[5];
|
|
478
|
|
479 snprintf(port_str, sizeof(port_str), "%d", port);
|
|
480
|
|
481 memset(&hints, 0, sizeof(hints));
|
|
482
|
|
483 hints.ai_flags = AI_PASSIVE;
|
|
484 hints.ai_family = AF_UNSPEC;
|
|
485 hints.ai_socktype = SOCK_STREAM;
|
|
486
|
|
487 if (getaddrinfo(NULL, port_str, &hints, &res) != 0)
|
|
488 {
|
|
489 gaim_debug_error("msn", "Could not get address info: %s.\n",
|
|
490 port_str);
|
|
491 return -1;
|
|
492 }
|
|
493
|
|
494 for (c = res; c != NULL; c = c->ai_next)
|
|
495 {
|
|
496 fd = socket(c->ai_family, c->ai_socktype, c->ai_protocol);
|
|
497
|
|
498 if (fd < 0)
|
|
499 continue;
|
|
500
|
|
501 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
|
502
|
|
503 if (bind(fd, c->ai_addr, c->ai_addrlen) == 0)
|
|
504 break;
|
|
505
|
|
506 close(fd);
|
|
507 }
|
|
508
|
|
509 if (c == NULL)
|
|
510 {
|
|
511 gaim_debug_error("msn", "Could not find socket: %s.\n", port_str);
|
|
512 return -1;
|
|
513 }
|
|
514
|
|
515 freeaddrinfo(res);
|
|
516 #else
|
|
517 struct sockaddr_in sockin;
|
|
518
|
|
519 fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
520
|
|
521 if (fd < 0)
|
|
522 return -1;
|
|
523
|
|
524 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) != 0)
|
|
525 {
|
|
526 close(fd);
|
|
527 return -1;
|
|
528 }
|
|
529
|
|
530 memset(&sockin, 0, sizeof(struct sockaddr_in));
|
|
531 sockin.sin_family = AF_INET;
|
|
532 sockin.sin_port = htons(port);
|
|
533
|
|
534 if (bind(fd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0)
|
|
535 {
|
|
536 close(fd);
|
|
537 return -1;
|
|
538 }
|
|
539 #endif
|
|
540
|
|
541 if (listen (fd, 4) != 0)
|
|
542 {
|
|
543 close (fd);
|
|
544 return -1;
|
|
545 }
|
|
546
|
|
547 fcntl(fd, F_SETFL, O_NONBLOCK);
|
|
548
|
|
549 return fd;
|
|
550 }
|
|
551 #endif
|