| |
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; |
| |
174 servconn->connect_info = NULL; |
| |
175 servconn->processing = FALSE; |
| |
176 |
| |
177 if (servconn->wasted) |
| |
178 { |
| |
179 msn_servconn_destroy(servconn); |
| |
180 return; |
| |
181 } |
| |
182 |
| |
183 servconn->fd = source; |
| |
184 |
| |
185 if (source > 0) |
| |
186 { |
| |
187 servconn->connected = TRUE; |
| |
188 |
| |
189 /* Someone wants to know we connected. */ |
| |
190 servconn->connect_cb(servconn); |
| |
191 servconn->inpa = gaim_input_add(servconn->fd, GAIM_INPUT_READ, |
| |
192 read_cb, data); |
| |
193 } |
| |
194 else |
| |
195 { |
| |
196 msn_servconn_got_error(servconn, MSN_SERVCONN_ERROR_CONNECT); |
| |
197 } |
| |
198 } |
| |
199 |
| |
200 gboolean |
| |
201 msn_servconn_connect(MsnServConn *servconn, const char *host, int port) |
| |
202 { |
| |
203 MsnSession *session; |
| |
204 |
| |
205 g_return_val_if_fail(servconn != NULL, FALSE); |
| |
206 g_return_val_if_fail(host != NULL, FALSE); |
| |
207 g_return_val_if_fail(port > 0, FALSE); |
| |
208 |
| |
209 session = servconn->session; |
| |
210 |
| |
211 if (servconn->connected) |
| |
212 msn_servconn_disconnect(servconn); |
| |
213 |
| |
214 if (servconn->host != NULL) |
| |
215 g_free(servconn->host); |
| |
216 |
| |
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_info = gaim_proxy_connect(session->account, host, port, |
| |
237 connect_cb, servconn); |
| |
238 |
| |
239 if (servconn->connect_info != NULL) |
| |
240 { |
| |
241 servconn->processing = TRUE; |
| |
242 return TRUE; |
| |
243 } |
| |
244 else |
| |
245 return FALSE; |
| |
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_info != NULL) |
| |
272 { |
| |
273 gaim_proxy_connect_cancel(servconn->connect_info); |
| |
274 servconn->connect_info = NULL; |
| |
275 } |
| |
276 |
| |
277 if (servconn->inpa > 0) |
| |
278 { |
| |
279 gaim_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, GaimInputCondition cond) |
| |
297 { |
| |
298 MsnServConn *servconn = data; |
| |
299 int ret, writelen; |
| |
300 |
| |
301 writelen = gaim_circ_buffer_get_max_read(servconn->tx_buf); |
| |
302 |
| |
303 if (writelen == 0) { |
| |
304 gaim_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 gaim_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 = gaim_input_add( |
| |
357 servconn->fd, GAIM_INPUT_WRITE, |
| |
358 servconn_write_cb, servconn); |
| |
359 gaim_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, GaimInputCondition 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 gaim_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 { |
| |
440 msn_cmdproc_process_payload(servconn->cmdproc, cur, cur_len); |
| |
441 servconn->payload_len = 0; |
| |
442 } |
| |
443 else |
| |
444 { |
| |
445 msn_cmdproc_process_cmd_text(servconn->cmdproc, cur); |
| |
446 } |
| |
447 } while (servconn->connected && !servconn->wasted && servconn->rx_len > 0); |
| |
448 |
| |
449 if (servconn->connected && !servconn->wasted) |
| |
450 { |
| |
451 if (servconn->rx_len > 0) |
| |
452 servconn->rx_buf = g_memdup(cur, servconn->rx_len); |
| |
453 else |
| |
454 servconn->rx_buf = NULL; |
| |
455 } |
| |
456 |
| |
457 servconn->processing = FALSE; |
| |
458 |
| |
459 if (servconn->wasted) |
| |
460 msn_servconn_destroy(servconn); |
| |
461 |
| |
462 g_free(old_rx_buf); |
| |
463 } |
| |
464 |
| |
465 #if 0 |
| |
466 static int |
| |
467 create_listener(int port) |
| |
468 { |
| |
469 int fd; |
| |
470 const int on = 1; |
| |
471 |
| |
472 #if 0 |
| |
473 struct addrinfo hints; |
| |
474 struct addrinfo *c, *res; |
| |
475 char port_str[5]; |
| |
476 |
| |
477 snprintf(port_str, sizeof(port_str), "%d", port); |
| |
478 |
| |
479 memset(&hints, 0, sizeof(hints)); |
| |
480 |
| |
481 hints.ai_flags = AI_PASSIVE; |
| |
482 hints.ai_family = AF_UNSPEC; |
| |
483 hints.ai_socktype = SOCK_STREAM; |
| |
484 |
| |
485 if (getaddrinfo(NULL, port_str, &hints, &res) != 0) |
| |
486 { |
| |
487 gaim_debug_error("msn", "Could not get address info: %s.\n", |
| |
488 port_str); |
| |
489 return -1; |
| |
490 } |
| |
491 |
| |
492 for (c = res; c != NULL; c = c->ai_next) |
| |
493 { |
| |
494 fd = socket(c->ai_family, c->ai_socktype, c->ai_protocol); |
| |
495 |
| |
496 if (fd < 0) |
| |
497 continue; |
| |
498 |
| |
499 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| |
500 |
| |
501 if (bind(fd, c->ai_addr, c->ai_addrlen) == 0) |
| |
502 break; |
| |
503 |
| |
504 close(fd); |
| |
505 } |
| |
506 |
| |
507 if (c == NULL) |
| |
508 { |
| |
509 gaim_debug_error("msn", "Could not find socket: %s.\n", port_str); |
| |
510 return -1; |
| |
511 } |
| |
512 |
| |
513 freeaddrinfo(res); |
| |
514 #else |
| |
515 struct sockaddr_in sockin; |
| |
516 |
| |
517 fd = socket(AF_INET, SOCK_STREAM, 0); |
| |
518 |
| |
519 if (fd < 0) |
| |
520 return -1; |
| |
521 |
| |
522 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) != 0) |
| |
523 { |
| |
524 close(fd); |
| |
525 return -1; |
| |
526 } |
| |
527 |
| |
528 memset(&sockin, 0, sizeof(struct sockaddr_in)); |
| |
529 sockin.sin_family = AF_INET; |
| |
530 sockin.sin_port = htons(port); |
| |
531 |
| |
532 if (bind(fd, (struct sockaddr *)&sockin, sizeof(struct sockaddr_in)) != 0) |
| |
533 { |
| |
534 close(fd); |
| |
535 return -1; |
| |
536 } |
| |
537 #endif |
| |
538 |
| |
539 if (listen (fd, 4) != 0) |
| |
540 { |
| |
541 close (fd); |
| |
542 return -1; |
| |
543 } |
| |
544 |
| |
545 fcntl(fd, F_SETFL, O_NONBLOCK); |
| |
546 |
| |
547 return fd; |
| |
548 } |
| |
549 #endif |