1 | /* |
---|
2 | * conn.c |
---|
3 | * |
---|
4 | * Does all this gloriously nifty connection handling stuff... |
---|
5 | * |
---|
6 | */ |
---|
7 | |
---|
8 | #define FAIM_INTERNAL |
---|
9 | #define FAIM_NEED_CONN_INTERNAL |
---|
10 | #include <aim.h> |
---|
11 | |
---|
12 | #ifndef _WIN32 |
---|
13 | #include <netdb.h> |
---|
14 | #include <sys/socket.h> |
---|
15 | #include <netinet/in.h> |
---|
16 | #endif |
---|
17 | |
---|
18 | #ifdef _WIN32 |
---|
19 | #include "win32dep.h" |
---|
20 | #endif |
---|
21 | |
---|
22 | /* |
---|
23 | * In OSCAR, every connection has a set of SNAC groups associated |
---|
24 | * with it. These are the groups that you can send over this connection |
---|
25 | * without being guarenteed a "Not supported" SNAC error. |
---|
26 | * |
---|
27 | * The grand theory of things says that these associations transcend |
---|
28 | * what libfaim calls "connection types" (conn->type). You can probably |
---|
29 | * see the elegance here, but since I want to revel in it for a bit, you |
---|
30 | * get to hear it all spelled out. |
---|
31 | * |
---|
32 | * So let us say that you have your core BOS connection running. One |
---|
33 | * of your modules has just given you a SNAC of the group 0x0004 to send |
---|
34 | * you. Maybe an IM destined for some twit in Greenland. So you start |
---|
35 | * at the top of your connection list, looking for a connection that |
---|
36 | * claims to support group 0x0004. You find one. Why, that neat BOS |
---|
37 | * connection of yours can do that. So you send it on its way. |
---|
38 | * |
---|
39 | * Now, say, that fellow from Greenland has friends and they all want to |
---|
40 | * meet up with you in a lame chat room. This has landed you a SNAC |
---|
41 | * in the family 0x000e and you have to admit you're a bit lost. You've |
---|
42 | * searched your connection list for someone who wants to make your life |
---|
43 | * easy and deliver this SNAC for you, but there isn't one there. |
---|
44 | * |
---|
45 | * Here comes the good bit. Without even letting anyone know, particularly |
---|
46 | * the module that decided to send this SNAC, and definitly not that twit |
---|
47 | * in Greenland, you send out a service request. In this request, you have |
---|
48 | * marked the need for a connection supporting group 0x000e. A few seconds |
---|
49 | * later, you receive a service redirect with an IP address and a cookie in |
---|
50 | * it. Great, you say. Now I have something to do. Off you go, making |
---|
51 | * that connection. One of the first things you get from this new server |
---|
52 | * is a message saying that indeed it does support the group you were looking |
---|
53 | * for. So you continue and send rate confirmation and all that. |
---|
54 | * |
---|
55 | * Then you remember you had that SNAC to send, and now you have a means to |
---|
56 | * do it, and you do, and everyone is happy. Except the Greenlander, who is |
---|
57 | * still stuck in the bitter cold. |
---|
58 | * |
---|
59 | * Oh, and this is useful for building the Migration SNACs, too. In the |
---|
60 | * future, this may help convince me to implement rate limit mitigation |
---|
61 | * for real. We'll see. |
---|
62 | * |
---|
63 | * Just to make me look better, I'll say that I've known about this great |
---|
64 | * scheme for quite some time now. But I still haven't convinced myself |
---|
65 | * to make libfaim work that way. It would take a fair amount of effort, |
---|
66 | * and probably some client API changes as well. (Whenever I don't want |
---|
67 | * to do something, I just say it would change the client API. Then I |
---|
68 | * instantly have a couple of supporters of not doing it.) |
---|
69 | * |
---|
70 | * Generally, addgroup is only called by the internal handling of the |
---|
71 | * server ready SNAC. So if you want to do something before that, you'll |
---|
72 | * have to be more creative. That is done rather early, though, so I don't |
---|
73 | * think you have to worry about it. Unless you're me. I care deeply |
---|
74 | * about such inane things. |
---|
75 | * |
---|
76 | */ |
---|
77 | faim_internal void aim_conn_addgroup(aim_conn_t *conn, fu16_t group) |
---|
78 | { |
---|
79 | aim_conn_inside_t *ins = (aim_conn_inside_t *)conn->inside; |
---|
80 | struct snacgroup *sg; |
---|
81 | |
---|
82 | if (!(sg = malloc(sizeof(struct snacgroup)))) |
---|
83 | return; |
---|
84 | |
---|
85 | faimdprintf(aim_conn_getsess(conn), 1, "adding group 0x%04x\n", group); |
---|
86 | sg->group = group; |
---|
87 | |
---|
88 | sg->next = ins->groups; |
---|
89 | ins->groups = sg; |
---|
90 | |
---|
91 | return; |
---|
92 | } |
---|
93 | |
---|
94 | faim_export aim_conn_t *aim_conn_findbygroup(aim_session_t *sess, fu16_t group) |
---|
95 | { |
---|
96 | aim_conn_t *cur; |
---|
97 | |
---|
98 | for (cur = sess->connlist; cur; cur = cur->next) { |
---|
99 | aim_conn_inside_t *ins = (aim_conn_inside_t *)cur->inside; |
---|
100 | struct snacgroup *sg; |
---|
101 | |
---|
102 | for (sg = ins->groups; sg; sg = sg->next) { |
---|
103 | if (sg->group == group) |
---|
104 | return cur; |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | return NULL; |
---|
109 | } |
---|
110 | |
---|
111 | static void connkill_snacgroups(struct snacgroup **head) |
---|
112 | { |
---|
113 | struct snacgroup *sg; |
---|
114 | |
---|
115 | for (sg = *head; sg; ) { |
---|
116 | struct snacgroup *tmp; |
---|
117 | |
---|
118 | tmp = sg->next; |
---|
119 | free(sg); |
---|
120 | sg = tmp; |
---|
121 | } |
---|
122 | |
---|
123 | *head = NULL; |
---|
124 | |
---|
125 | return; |
---|
126 | } |
---|
127 | |
---|
128 | static void connkill_rates(struct rateclass **head) |
---|
129 | { |
---|
130 | struct rateclass *rc; |
---|
131 | |
---|
132 | for (rc = *head; rc; ) { |
---|
133 | struct rateclass *tmp; |
---|
134 | struct snacpair *sp; |
---|
135 | |
---|
136 | tmp = rc->next; |
---|
137 | |
---|
138 | for (sp = rc->members; sp; ) { |
---|
139 | struct snacpair *tmpsp; |
---|
140 | |
---|
141 | tmpsp = sp->next; |
---|
142 | free(sp); |
---|
143 | sp = tmpsp; |
---|
144 | } |
---|
145 | free(rc); |
---|
146 | |
---|
147 | rc = tmp; |
---|
148 | } |
---|
149 | |
---|
150 | *head = NULL; |
---|
151 | |
---|
152 | return; |
---|
153 | } |
---|
154 | |
---|
155 | static void connkill_real(aim_session_t *sess, aim_conn_t **deadconn) |
---|
156 | { |
---|
157 | |
---|
158 | aim_rxqueue_cleanbyconn(sess, *deadconn); |
---|
159 | aim_tx_cleanqueue(sess, *deadconn); |
---|
160 | |
---|
161 | if ((*deadconn)->fd != -1) |
---|
162 | aim_conn_close(*deadconn); |
---|
163 | |
---|
164 | /* |
---|
165 | * XXX ->priv should never be touched by the library. I know |
---|
166 | * it used to be, but I'm getting rid of all that. Use |
---|
167 | * ->internal instead. |
---|
168 | */ |
---|
169 | if ((*deadconn)->priv) |
---|
170 | free((*deadconn)->priv); |
---|
171 | |
---|
172 | /* |
---|
173 | * This will free ->internal if it necessary... |
---|
174 | */ |
---|
175 | if ((*deadconn)->type == AIM_CONN_TYPE_RENDEZVOUS) |
---|
176 | aim_conn_kill_rend(sess, *deadconn); |
---|
177 | else if ((*deadconn)->type == AIM_CONN_TYPE_CHAT) |
---|
178 | aim_conn_kill_chat(sess, *deadconn); |
---|
179 | |
---|
180 | if ((*deadconn)->inside) { |
---|
181 | aim_conn_inside_t *inside = (aim_conn_inside_t *)(*deadconn)->inside; |
---|
182 | |
---|
183 | connkill_snacgroups(&inside->groups); |
---|
184 | connkill_rates(&inside->rates); |
---|
185 | |
---|
186 | free(inside); |
---|
187 | } |
---|
188 | |
---|
189 | free(*deadconn); |
---|
190 | *deadconn = NULL; |
---|
191 | |
---|
192 | return; |
---|
193 | } |
---|
194 | |
---|
195 | /** |
---|
196 | * aim_connrst - Clears out connection list, killing remaining connections. |
---|
197 | * @sess: Session to be cleared |
---|
198 | * |
---|
199 | * Clears out the connection list and kills any connections left. |
---|
200 | * |
---|
201 | */ |
---|
202 | static void aim_connrst(aim_session_t *sess) |
---|
203 | { |
---|
204 | |
---|
205 | if (sess->connlist) { |
---|
206 | aim_conn_t *cur = sess->connlist, *tmp; |
---|
207 | |
---|
208 | while (cur) { |
---|
209 | tmp = cur->next; |
---|
210 | aim_conn_close(cur); |
---|
211 | connkill_real(sess, &cur); |
---|
212 | cur = tmp; |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | sess->connlist = NULL; |
---|
217 | |
---|
218 | return; |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * aim_conn_init - Reset a connection to default values. |
---|
223 | * @deadconn: Connection to be reset |
---|
224 | * |
---|
225 | * Initializes and/or resets a connection structure. |
---|
226 | * |
---|
227 | */ |
---|
228 | static void aim_conn_init(aim_conn_t *deadconn) |
---|
229 | { |
---|
230 | |
---|
231 | if (!deadconn) |
---|
232 | return; |
---|
233 | |
---|
234 | deadconn->fd = -1; |
---|
235 | deadconn->subtype = -1; |
---|
236 | deadconn->type = -1; |
---|
237 | deadconn->seqnum = 0; |
---|
238 | deadconn->lastactivity = 0; |
---|
239 | deadconn->forcedlatency = 0; |
---|
240 | deadconn->handlerlist = NULL; |
---|
241 | deadconn->priv = NULL; |
---|
242 | memset(deadconn->inside, 0, sizeof(aim_conn_inside_t)); |
---|
243 | |
---|
244 | return; |
---|
245 | } |
---|
246 | |
---|
247 | /** |
---|
248 | * aim_conn_getnext - Gets a new connection structure. |
---|
249 | * @sess: Session |
---|
250 | * |
---|
251 | * Allocate a new empty connection structure. |
---|
252 | * |
---|
253 | */ |
---|
254 | static aim_conn_t *aim_conn_getnext(aim_session_t *sess) |
---|
255 | { |
---|
256 | aim_conn_t *newconn; |
---|
257 | |
---|
258 | if (!(newconn = malloc(sizeof(aim_conn_t)))) |
---|
259 | return NULL; |
---|
260 | memset(newconn, 0, sizeof(aim_conn_t)); |
---|
261 | |
---|
262 | if (!(newconn->inside = malloc(sizeof(aim_conn_inside_t)))) { |
---|
263 | free(newconn); |
---|
264 | return NULL; |
---|
265 | } |
---|
266 | memset(newconn->inside, 0, sizeof(aim_conn_inside_t)); |
---|
267 | |
---|
268 | aim_conn_init(newconn); |
---|
269 | |
---|
270 | newconn->next = sess->connlist; |
---|
271 | sess->connlist = newconn; |
---|
272 | |
---|
273 | return newconn; |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * aim_conn_kill - Close and free a connection. |
---|
278 | * @sess: Session for the connection |
---|
279 | * @deadconn: Connection to be freed |
---|
280 | * |
---|
281 | * Close, clear, and free a connection structure. Should never be |
---|
282 | * called from within libfaim. |
---|
283 | * |
---|
284 | */ |
---|
285 | faim_export void aim_conn_kill(aim_session_t *sess, aim_conn_t **deadconn) |
---|
286 | { |
---|
287 | aim_conn_t *cur, **prev; |
---|
288 | |
---|
289 | if (!deadconn || !*deadconn) |
---|
290 | return; |
---|
291 | |
---|
292 | for (prev = &sess->connlist; (cur = *prev); ) { |
---|
293 | if (cur == *deadconn) { |
---|
294 | *prev = cur->next; |
---|
295 | break; |
---|
296 | } |
---|
297 | prev = &cur->next; |
---|
298 | } |
---|
299 | |
---|
300 | if (!cur) |
---|
301 | return; /* oops */ |
---|
302 | |
---|
303 | connkill_real(sess, &cur); |
---|
304 | |
---|
305 | return; |
---|
306 | } |
---|
307 | |
---|
308 | /** |
---|
309 | * aim_conn_close - Close a connection |
---|
310 | * @deadconn: Connection to close |
---|
311 | * |
---|
312 | * Close (but not free) a connection. |
---|
313 | * |
---|
314 | * This leaves everything untouched except for clearing the |
---|
315 | * handler list and setting the fd to -1 (used to recognize |
---|
316 | * dead connections). It will also remove cookies if necessary. |
---|
317 | * |
---|
318 | */ |
---|
319 | faim_export void aim_conn_close(aim_conn_t *deadconn) |
---|
320 | { |
---|
321 | |
---|
322 | if (deadconn->fd >= 3) |
---|
323 | close(deadconn->fd); |
---|
324 | deadconn->fd = -1; |
---|
325 | if (deadconn->handlerlist) |
---|
326 | aim_clearhandlers(deadconn); |
---|
327 | if (deadconn->type == AIM_CONN_TYPE_RENDEZVOUS) |
---|
328 | aim_conn_close_rend((aim_session_t *)deadconn->sessv, deadconn); |
---|
329 | |
---|
330 | return; |
---|
331 | } |
---|
332 | |
---|
333 | /** |
---|
334 | * aim_getconn_type - Find a connection of a specific type |
---|
335 | * @sess: Session to search |
---|
336 | * @type: Type of connection to look for |
---|
337 | * |
---|
338 | * Searches for a connection of the specified type in the |
---|
339 | * specified session. Returns the first connection of that |
---|
340 | * type found. |
---|
341 | * |
---|
342 | * XXX except for RENDEZVOUS, all uses of this should be removed and |
---|
343 | * use aim_conn_findbygroup() instead. |
---|
344 | */ |
---|
345 | faim_export aim_conn_t *aim_getconn_type(aim_session_t *sess, int type) |
---|
346 | { |
---|
347 | aim_conn_t *cur; |
---|
348 | |
---|
349 | for (cur = sess->connlist; cur; cur = cur->next) { |
---|
350 | if ((cur->type == type) && |
---|
351 | !(cur->status & AIM_CONN_STATUS_INPROGRESS)) |
---|
352 | break; |
---|
353 | } |
---|
354 | |
---|
355 | return cur; |
---|
356 | } |
---|
357 | |
---|
358 | faim_export aim_conn_t *aim_getconn_type_all(aim_session_t *sess, int type) |
---|
359 | { |
---|
360 | aim_conn_t *cur; |
---|
361 | |
---|
362 | for (cur = sess->connlist; cur; cur = cur->next) { |
---|
363 | if (cur->type == type) |
---|
364 | break; |
---|
365 | } |
---|
366 | |
---|
367 | return cur; |
---|
368 | } |
---|
369 | |
---|
370 | /* If you pass -1 for the fd, you'll get what you ask for. Gibberish. */ |
---|
371 | faim_export aim_conn_t *aim_getconn_fd(aim_session_t *sess, int fd) |
---|
372 | { |
---|
373 | aim_conn_t *cur; |
---|
374 | |
---|
375 | for (cur = sess->connlist; cur; cur = cur->next) { |
---|
376 | if (cur->fd == fd) |
---|
377 | break; |
---|
378 | } |
---|
379 | |
---|
380 | return cur; |
---|
381 | } |
---|
382 | |
---|
383 | /** |
---|
384 | * aim_proxyconnect - An extrememly quick and dirty SOCKS5 interface. |
---|
385 | * @sess: Session to connect |
---|
386 | * @host: Host to connect to |
---|
387 | * @port: Port to connect to |
---|
388 | * @statusret: Return value of the connection |
---|
389 | * |
---|
390 | * Attempts to connect to the specified host via the configured |
---|
391 | * proxy settings, if present. If no proxy is configured for |
---|
392 | * this session, the connection is done directly. |
---|
393 | * |
---|
394 | * XXX this is really awful. |
---|
395 | * |
---|
396 | */ |
---|
397 | static int aim_proxyconnect(aim_session_t *sess, const char *host, fu16_t port, fu32_t *statusret) |
---|
398 | { |
---|
399 | int fd = -1; |
---|
400 | |
---|
401 | if (strlen(sess->socksproxy.server)) { /* connecting via proxy */ |
---|
402 | int i; |
---|
403 | unsigned char buf[512]; |
---|
404 | struct sockaddr_in sa; |
---|
405 | struct hostent *hp; |
---|
406 | char *proxy; |
---|
407 | unsigned short proxyport = 1080; |
---|
408 | |
---|
409 | for(i=0;i<(int)strlen(sess->socksproxy.server);i++) { |
---|
410 | if (sess->socksproxy.server[i] == ':') { |
---|
411 | proxyport = atoi(&(sess->socksproxy.server[i+1])); |
---|
412 | break; |
---|
413 | } |
---|
414 | } |
---|
415 | |
---|
416 | proxy = (char *)malloc(i+1); |
---|
417 | strncpy(proxy, sess->socksproxy.server, i); |
---|
418 | proxy[i] = '\0'; |
---|
419 | |
---|
420 | if (!(hp = gethostbyname(proxy))) { |
---|
421 | faimdprintf(sess, 0, "proxyconnect: unable to resolve proxy name\n"); |
---|
422 | *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR); |
---|
423 | return -1; |
---|
424 | } |
---|
425 | free(proxy); |
---|
426 | |
---|
427 | memset(&sa.sin_zero, 0, 8); |
---|
428 | sa.sin_port = htons(proxyport); |
---|
429 | memcpy(&sa.sin_addr, hp->h_addr, hp->h_length); |
---|
430 | sa.sin_family = hp->h_addrtype; |
---|
431 | |
---|
432 | fd = socket(hp->h_addrtype, SOCK_STREAM, 0); |
---|
433 | if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) { |
---|
434 | faimdprintf(sess, 0, "proxyconnect: unable to connect to proxy\n"); |
---|
435 | close(fd); |
---|
436 | return -1; |
---|
437 | } |
---|
438 | |
---|
439 | i = 0; |
---|
440 | buf[0] = 0x05; /* SOCKS version 5 */ |
---|
441 | if (strlen(sess->socksproxy.username)) { |
---|
442 | buf[1] = 0x02; /* two methods */ |
---|
443 | buf[2] = 0x00; /* no authentication */ |
---|
444 | buf[3] = 0x02; /* username/password authentication */ |
---|
445 | i = 4; |
---|
446 | } else { |
---|
447 | buf[1] = 0x01; |
---|
448 | buf[2] = 0x00; |
---|
449 | i = 3; |
---|
450 | } |
---|
451 | if (write(fd, buf, i) < i) { |
---|
452 | *statusret = errno; |
---|
453 | close(fd); |
---|
454 | return -1; |
---|
455 | } |
---|
456 | if (read(fd, buf, 2) < 2) { |
---|
457 | *statusret = errno; |
---|
458 | close(fd); |
---|
459 | return -1; |
---|
460 | } |
---|
461 | |
---|
462 | if ((buf[0] != 0x05) || (buf[1] == 0xff)) { |
---|
463 | *statusret = EINVAL; |
---|
464 | close(fd); |
---|
465 | return -1; |
---|
466 | } |
---|
467 | |
---|
468 | /* check if we're doing username authentication */ |
---|
469 | if (buf[1] == 0x02) { |
---|
470 | i = aimutil_put8(buf, 0x01); /* version 1 */ |
---|
471 | i += aimutil_put8(buf+i, strlen(sess->socksproxy.username)); |
---|
472 | i += aimutil_putstr(buf+i, sess->socksproxy.username, strlen(sess->socksproxy.username)); |
---|
473 | i += aimutil_put8(buf+i, strlen(sess->socksproxy.password)); |
---|
474 | i += aimutil_putstr(buf+i, sess->socksproxy.password, strlen(sess->socksproxy.password)); |
---|
475 | if (write(fd, buf, i) < i) { |
---|
476 | *statusret = errno; |
---|
477 | close(fd); |
---|
478 | return -1; |
---|
479 | } |
---|
480 | if (read(fd, buf, 2) < 2) { |
---|
481 | *statusret = errno; |
---|
482 | close(fd); |
---|
483 | return -1; |
---|
484 | } |
---|
485 | if ((buf[0] != 0x01) || (buf[1] != 0x00)) { |
---|
486 | *statusret = EINVAL; |
---|
487 | close(fd); |
---|
488 | return -1; |
---|
489 | } |
---|
490 | } |
---|
491 | |
---|
492 | i = aimutil_put8(buf, 0x05); |
---|
493 | i += aimutil_put8(buf+i, 0x01); /* CONNECT */ |
---|
494 | i += aimutil_put8(buf+i, 0x00); /* reserved */ |
---|
495 | i += aimutil_put8(buf+i, 0x03); /* address type: host name */ |
---|
496 | i += aimutil_put8(buf+i, strlen(host)); |
---|
497 | i += aimutil_putstr(buf+i, host, strlen(host)); |
---|
498 | i += aimutil_put16(buf+i, port); |
---|
499 | |
---|
500 | if (write(fd, buf, i) < i) { |
---|
501 | *statusret = errno; |
---|
502 | close(fd); |
---|
503 | return -1; |
---|
504 | } |
---|
505 | |
---|
506 | if (read(fd, buf, 10) < 10) { |
---|
507 | *statusret = errno; |
---|
508 | close(fd); |
---|
509 | return -1; |
---|
510 | } |
---|
511 | if ((buf[0] != 0x05) || (buf[1] != 0x00)) { |
---|
512 | *statusret = EINVAL; |
---|
513 | close(fd); |
---|
514 | return -1; |
---|
515 | } |
---|
516 | |
---|
517 | } else { /* connecting directly */ |
---|
518 | struct sockaddr_in sa; |
---|
519 | struct hostent *hp; |
---|
520 | |
---|
521 | if (!(hp = gethostbyname(host))) { |
---|
522 | *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR); |
---|
523 | return -1; |
---|
524 | } |
---|
525 | |
---|
526 | memset(&sa, 0, sizeof(struct sockaddr_in)); |
---|
527 | sa.sin_port = htons(port); |
---|
528 | memcpy(&sa.sin_addr, hp->h_addr, hp->h_length); |
---|
529 | sa.sin_family = hp->h_addrtype; |
---|
530 | |
---|
531 | fd = socket(hp->h_addrtype, SOCK_STREAM, 0); |
---|
532 | |
---|
533 | if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT) |
---|
534 | fcntl(fd, F_SETFL, O_NONBLOCK); /* XXX save flags */ |
---|
535 | |
---|
536 | if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) { |
---|
537 | if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT) { |
---|
538 | if ((errno == EINPROGRESS) || (errno == EINTR)) { |
---|
539 | if (statusret) |
---|
540 | *statusret |= AIM_CONN_STATUS_INPROGRESS; |
---|
541 | return fd; |
---|
542 | } |
---|
543 | } |
---|
544 | close(fd); |
---|
545 | fd = -1; |
---|
546 | } |
---|
547 | } |
---|
548 | return fd; |
---|
549 | } |
---|
550 | |
---|
551 | /** |
---|
552 | * aim_cloneconn - clone an aim_conn_t |
---|
553 | * @sess: session containing parent |
---|
554 | * @src: connection to clone |
---|
555 | * |
---|
556 | * A new connection is allocated, and the values are filled in |
---|
557 | * appropriately. Note that this function sets the new connnection's |
---|
558 | * ->priv pointer to be equal to that of its parent: only the pointer |
---|
559 | * is copied, not the data it points to. |
---|
560 | * |
---|
561 | * This function returns a pointer to the new aim_conn_t, or %NULL on |
---|
562 | * error |
---|
563 | */ |
---|
564 | faim_internal aim_conn_t *aim_cloneconn(aim_session_t *sess, aim_conn_t *src) |
---|
565 | { |
---|
566 | aim_conn_t *conn; |
---|
567 | |
---|
568 | if (!(conn = aim_conn_getnext(sess))) |
---|
569 | return NULL; |
---|
570 | |
---|
571 | conn->fd = src->fd; |
---|
572 | conn->type = src->type; |
---|
573 | conn->subtype = src->subtype; |
---|
574 | conn->seqnum = src->seqnum; |
---|
575 | conn->priv = src->priv; |
---|
576 | conn->internal = src->internal; |
---|
577 | conn->lastactivity = src->lastactivity; |
---|
578 | conn->forcedlatency = src->forcedlatency; |
---|
579 | conn->sessv = src->sessv; |
---|
580 | aim_clonehandlers(sess, conn, src); |
---|
581 | |
---|
582 | if (src->inside) { |
---|
583 | /* |
---|
584 | * XXX should clone this section as well, but since currently |
---|
585 | * this function only gets called for some of that rendezvous |
---|
586 | * crap, and not on SNAC connections, its probably okay for |
---|
587 | * now. |
---|
588 | * |
---|
589 | */ |
---|
590 | } |
---|
591 | |
---|
592 | return conn; |
---|
593 | } |
---|
594 | |
---|
595 | /** |
---|
596 | * aim_newconn - Open a new connection |
---|
597 | * @sess: Session to create connection in |
---|
598 | * @type: Type of connection to create |
---|
599 | * @dest: Host to connect to (in "host:port" syntax) |
---|
600 | * |
---|
601 | * Opens a new connection to the specified dest host of specified |
---|
602 | * type, using the proxy settings if available. If @host is %NULL, |
---|
603 | * the connection is allocated and returned, but no connection |
---|
604 | * is made. |
---|
605 | * |
---|
606 | * FIXME: Return errors in a more sane way. |
---|
607 | * |
---|
608 | */ |
---|
609 | faim_export aim_conn_t *aim_newconn(aim_session_t *sess, int type, const char *dest) |
---|
610 | { |
---|
611 | aim_conn_t *connstruct; |
---|
612 | fu16_t port = FAIM_LOGIN_PORT; |
---|
613 | char *host; |
---|
614 | int i, ret; |
---|
615 | |
---|
616 | if (!(connstruct = aim_conn_getnext(sess))) |
---|
617 | return NULL; |
---|
618 | |
---|
619 | connstruct->sessv = (void *)sess; |
---|
620 | connstruct->type = type; |
---|
621 | |
---|
622 | if (!dest) { /* just allocate a struct */ |
---|
623 | connstruct->fd = -1; |
---|
624 | connstruct->status = 0; |
---|
625 | return connstruct; |
---|
626 | } |
---|
627 | |
---|
628 | /* |
---|
629 | * As of 23 Jul 1999, AOL now sends the port number, preceded by a |
---|
630 | * colon, in the BOS redirect. This fatally breaks all previous |
---|
631 | * libfaims. Bad, bad AOL. |
---|
632 | * |
---|
633 | * We put this here to catch every case. |
---|
634 | * |
---|
635 | */ |
---|
636 | |
---|
637 | for(i = 0; i < (int)strlen(dest); i++) { |
---|
638 | if (dest[i] == ':') { |
---|
639 | port = atoi(&(dest[i+1])); |
---|
640 | break; |
---|
641 | } |
---|
642 | } |
---|
643 | |
---|
644 | host = (char *)malloc(i+1); |
---|
645 | strncpy(host, dest, i); |
---|
646 | host[i] = '\0'; |
---|
647 | |
---|
648 | if ((ret = aim_proxyconnect(sess, host, port, &connstruct->status)) < 0) { |
---|
649 | connstruct->fd = -1; |
---|
650 | connstruct->status = (errno | AIM_CONN_STATUS_CONNERR); |
---|
651 | free(host); |
---|
652 | return connstruct; |
---|
653 | } else |
---|
654 | connstruct->fd = ret; |
---|
655 | |
---|
656 | free(host); |
---|
657 | |
---|
658 | return connstruct; |
---|
659 | } |
---|
660 | |
---|
661 | /** |
---|
662 | * aim_conngetmaxfd - Return the highest valued file discriptor in session |
---|
663 | * @sess: Session to search |
---|
664 | * |
---|
665 | * Returns the highest valued filed descriptor of all open |
---|
666 | * connections in @sess. |
---|
667 | * |
---|
668 | */ |
---|
669 | faim_export int aim_conngetmaxfd(aim_session_t *sess) |
---|
670 | { |
---|
671 | int j; |
---|
672 | aim_conn_t *cur; |
---|
673 | |
---|
674 | for (cur = sess->connlist, j = 0; cur; cur = cur->next) { |
---|
675 | if (cur->fd > j) |
---|
676 | j = cur->fd; |
---|
677 | } |
---|
678 | |
---|
679 | return j; |
---|
680 | } |
---|
681 | |
---|
682 | /** |
---|
683 | * aim_conn_in_sess - Predicate to test the precense of a connection in a sess |
---|
684 | * @sess: Session to look in |
---|
685 | * @conn: Connection to look for |
---|
686 | * |
---|
687 | * Searches @sess for the passed connection. Returns 1 if its present, |
---|
688 | * zero otherwise. |
---|
689 | * |
---|
690 | */ |
---|
691 | faim_export int aim_conn_in_sess(aim_session_t *sess, aim_conn_t *conn) |
---|
692 | { |
---|
693 | aim_conn_t *cur; |
---|
694 | |
---|
695 | for (cur = sess->connlist; cur; cur = cur->next) { |
---|
696 | if (cur == conn) |
---|
697 | return 1; |
---|
698 | } |
---|
699 | |
---|
700 | return 0; |
---|
701 | } |
---|
702 | |
---|
703 | /** |
---|
704 | * aim_select - Wait for a socket with data or timeout |
---|
705 | * @sess: Session to wait on |
---|
706 | * @timeout: How long to wait |
---|
707 | * @status: Return status |
---|
708 | * |
---|
709 | * Waits for a socket with data or for timeout, whichever comes first. |
---|
710 | * See select(2). |
---|
711 | * |
---|
712 | * Return codes in *status: |
---|
713 | * -1 error in select() (%NULL returned) |
---|
714 | * 0 no events pending (%NULL returned) |
---|
715 | * 1 outgoing data pending (%NULL returned) |
---|
716 | * 2 incoming data pending (connection with pending data returned) |
---|
717 | * |
---|
718 | */ |
---|
719 | faim_export aim_conn_t *aim_select(aim_session_t *sess, struct timeval *timeout, int *status) |
---|
720 | { |
---|
721 | aim_conn_t *cur; |
---|
722 | fd_set fds, wfds; |
---|
723 | int maxfd, i, haveconnecting = 0; |
---|
724 | |
---|
725 | if (!sess->connlist) { |
---|
726 | *status = -1; |
---|
727 | return NULL; |
---|
728 | } |
---|
729 | |
---|
730 | FD_ZERO(&fds); |
---|
731 | FD_ZERO(&wfds); |
---|
732 | |
---|
733 | for (cur = sess->connlist, maxfd = 0; cur; cur = cur->next) { |
---|
734 | if (cur->fd == -1) { |
---|
735 | /* don't let invalid/dead connections sit around */ |
---|
736 | *status = 2; |
---|
737 | return cur; |
---|
738 | } else if (cur->status & AIM_CONN_STATUS_INPROGRESS) { |
---|
739 | FD_SET(cur->fd, &wfds); |
---|
740 | |
---|
741 | haveconnecting++; |
---|
742 | } |
---|
743 | FD_SET(cur->fd, &fds); |
---|
744 | if (cur->fd > maxfd) |
---|
745 | maxfd = cur->fd; |
---|
746 | } |
---|
747 | |
---|
748 | /* |
---|
749 | * If we have data waiting to be sent, return |
---|
750 | * |
---|
751 | * We have to not do this if theres at least one |
---|
752 | * connection thats still connecting, since that connection |
---|
753 | * may have queued data and this return would prevent |
---|
754 | * the connection from ever completing! This is a major |
---|
755 | * inadequacy of the libfaim way of doing things. It means |
---|
756 | * that nothing can transmit as long as there's connecting |
---|
757 | * sockets. Evil. |
---|
758 | * |
---|
759 | * But its still better than having blocking connects. |
---|
760 | * |
---|
761 | */ |
---|
762 | if (!haveconnecting && sess->queue_outgoing) { |
---|
763 | *status = 1; |
---|
764 | return NULL; |
---|
765 | } |
---|
766 | |
---|
767 | if ((i = select(maxfd+1, &fds, &wfds, NULL, timeout))>=1) { |
---|
768 | for (cur = sess->connlist; cur; cur = cur->next) { |
---|
769 | if ((FD_ISSET(cur->fd, &fds)) || |
---|
770 | ((cur->status & AIM_CONN_STATUS_INPROGRESS) && |
---|
771 | FD_ISSET(cur->fd, &wfds))) { |
---|
772 | *status = 2; |
---|
773 | return cur; |
---|
774 | } |
---|
775 | } |
---|
776 | *status = 0; /* shouldn't happen */ |
---|
777 | } else if ((i == -1) && (errno == EINTR)) /* treat interrupts as a timeout */ |
---|
778 | *status = 0; |
---|
779 | else |
---|
780 | *status = i; /* can be 0 or -1 */ |
---|
781 | |
---|
782 | return NULL; /* no waiting or error, return */ |
---|
783 | } |
---|
784 | |
---|
785 | /** |
---|
786 | * aim_conn_setlatency - Set a forced latency value for connection |
---|
787 | * @conn: Conn to set latency for |
---|
788 | * @newval: Number of seconds to force between transmits |
---|
789 | * |
---|
790 | * Causes @newval seconds to be spent between transmits on a connection. |
---|
791 | * |
---|
792 | * This is my lame attempt at overcoming not understanding the rate |
---|
793 | * limiting. |
---|
794 | * |
---|
795 | * XXX: This should really be replaced with something that scales and |
---|
796 | * backs off like the real rate limiting does. |
---|
797 | * |
---|
798 | */ |
---|
799 | faim_export int aim_conn_setlatency(aim_conn_t *conn, int newval) |
---|
800 | { |
---|
801 | |
---|
802 | if (!conn) |
---|
803 | return -1; |
---|
804 | |
---|
805 | conn->forcedlatency = newval; |
---|
806 | conn->lastactivity = 0; /* reset this just to make sure */ |
---|
807 | |
---|
808 | return 0; |
---|
809 | } |
---|
810 | |
---|
811 | /** |
---|
812 | * aim_setupproxy - Configure a proxy for this session |
---|
813 | * @sess: Session to set proxy for |
---|
814 | * @server: SOCKS server |
---|
815 | * @username: SOCKS username |
---|
816 | * @password: SOCKS password |
---|
817 | * |
---|
818 | * Call this with your SOCKS5 proxy server parameters before |
---|
819 | * the first call to aim_newconn(). If called with all %NULL |
---|
820 | * args, it will clear out a previously set proxy. |
---|
821 | * |
---|
822 | * Set username and password to %NULL if not applicable. |
---|
823 | * |
---|
824 | */ |
---|
825 | faim_export void aim_setupproxy(aim_session_t *sess, const char *server, const char *username, const char *password) |
---|
826 | { |
---|
827 | /* clear out the proxy info */ |
---|
828 | if (!server || !strlen(server)) { |
---|
829 | memset(sess->socksproxy.server, 0, sizeof(sess->socksproxy.server)); |
---|
830 | memset(sess->socksproxy.username, 0, sizeof(sess->socksproxy.username)); |
---|
831 | memset(sess->socksproxy.password, 0, sizeof(sess->socksproxy.password)); |
---|
832 | return; |
---|
833 | } |
---|
834 | |
---|
835 | strncpy(sess->socksproxy.server, server, sizeof(sess->socksproxy.server)); |
---|
836 | if (username && strlen(username)) |
---|
837 | strncpy(sess->socksproxy.username, username, sizeof(sess->socksproxy.username)); |
---|
838 | if (password && strlen(password)) |
---|
839 | strncpy(sess->socksproxy.password, password, sizeof(sess->socksproxy.password)); |
---|
840 | |
---|
841 | return; |
---|
842 | } |
---|
843 | |
---|
844 | static void defaultdebugcb(aim_session_t *sess, int level, const char *format, va_list va) |
---|
845 | { |
---|
846 | |
---|
847 | vfprintf(stderr, format, va); |
---|
848 | |
---|
849 | return; |
---|
850 | } |
---|
851 | |
---|
852 | /** |
---|
853 | * aim_session_init - Initializes a session structure |
---|
854 | * @sess: Session to initialize |
---|
855 | * @flags: Flags to use. Any of %AIM_SESS_FLAGS %OR'd together. |
---|
856 | * @debuglevel: Level of debugging output (zero is least) |
---|
857 | * |
---|
858 | * Sets up the initial values for a session. |
---|
859 | * |
---|
860 | */ |
---|
861 | faim_export void aim_session_init(aim_session_t *sess, fu32_t flags, int debuglevel) |
---|
862 | { |
---|
863 | |
---|
864 | if (!sess) |
---|
865 | return; |
---|
866 | |
---|
867 | memset(sess, 0, sizeof(aim_session_t)); |
---|
868 | aim_connrst(sess); |
---|
869 | sess->queue_outgoing = NULL; |
---|
870 | sess->queue_incoming = NULL; |
---|
871 | aim_initsnachash(sess); |
---|
872 | sess->msgcookies = NULL; |
---|
873 | sess->snacid_next = 0x00000001; |
---|
874 | |
---|
875 | sess->flags = 0; |
---|
876 | sess->debug = debuglevel; |
---|
877 | sess->debugcb = defaultdebugcb; |
---|
878 | |
---|
879 | sess->modlistv = NULL; |
---|
880 | |
---|
881 | sess->ssi.received_data = 0; |
---|
882 | sess->ssi.waiting_for_ack = 0; |
---|
883 | sess->ssi.holding_queue = NULL; |
---|
884 | sess->ssi.revision = 0; |
---|
885 | sess->ssi.items = NULL; |
---|
886 | sess->ssi.timestamp = (time_t)0; |
---|
887 | |
---|
888 | sess->emailinfo = NULL; |
---|
889 | |
---|
890 | /* |
---|
891 | * Default to SNAC login unless XORLOGIN is explicitly set. |
---|
892 | */ |
---|
893 | if (!(flags & AIM_SESS_FLAGS_XORLOGIN)) |
---|
894 | sess->flags |= AIM_SESS_FLAGS_SNACLOGIN; |
---|
895 | sess->flags |= flags; |
---|
896 | |
---|
897 | /* |
---|
898 | * This must always be set. Default to the queue-based |
---|
899 | * version for back-compatibility. |
---|
900 | */ |
---|
901 | aim_tx_setenqueue(sess, AIM_TX_QUEUED, NULL); |
---|
902 | |
---|
903 | |
---|
904 | /* |
---|
905 | * Register all the modules for this session... |
---|
906 | */ |
---|
907 | aim__registermodule(sess, misc_modfirst); /* load the catch-all first */ |
---|
908 | aim__registermodule(sess, general_modfirst); |
---|
909 | aim__registermodule(sess, locate_modfirst); |
---|
910 | aim__registermodule(sess, buddylist_modfirst); |
---|
911 | aim__registermodule(sess, msg_modfirst); |
---|
912 | aim__registermodule(sess, adverts_modfirst); |
---|
913 | aim__registermodule(sess, invite_modfirst); |
---|
914 | aim__registermodule(sess, admin_modfirst); |
---|
915 | aim__registermodule(sess, popups_modfirst); |
---|
916 | aim__registermodule(sess, bos_modfirst); |
---|
917 | aim__registermodule(sess, search_modfirst); |
---|
918 | aim__registermodule(sess, stats_modfirst); |
---|
919 | aim__registermodule(sess, translate_modfirst); |
---|
920 | aim__registermodule(sess, chatnav_modfirst); |
---|
921 | aim__registermodule(sess, chat_modfirst); |
---|
922 | aim__registermodule(sess, newsearch_modfirst); |
---|
923 | /* missing 0x10 - 0x12 */ |
---|
924 | aim__registermodule(sess, ssi_modfirst); |
---|
925 | /* missing 0x14 */ |
---|
926 | aim__registermodule(sess, icq_modfirst); /* XXX - Make sure this isn't sent for AIM */ |
---|
927 | /* missing 0x16 */ |
---|
928 | aim__registermodule(sess, auth_modfirst); |
---|
929 | aim__registermodule(sess, email_modfirst); |
---|
930 | |
---|
931 | return; |
---|
932 | } |
---|
933 | |
---|
934 | /** |
---|
935 | * aim_session_kill - Deallocate a session |
---|
936 | * @sess: Session to kill |
---|
937 | * |
---|
938 | */ |
---|
939 | faim_export void aim_session_kill(aim_session_t *sess) |
---|
940 | { |
---|
941 | aim_cleansnacs(sess, -1); |
---|
942 | |
---|
943 | aim_logoff(sess); |
---|
944 | |
---|
945 | aim__shutdownmodules(sess); |
---|
946 | |
---|
947 | return; |
---|
948 | } |
---|
949 | |
---|
950 | /** |
---|
951 | * aim_setdebuggingcb - Set the function to call when outputting debugging info |
---|
952 | * @sess: Session to change |
---|
953 | * @cb: Function to call |
---|
954 | * |
---|
955 | * The function specified is called whenever faimdprintf() is used within |
---|
956 | * libfaim, and the session's debugging level is greater tha nor equal to |
---|
957 | * the value faimdprintf was called with. |
---|
958 | * |
---|
959 | */ |
---|
960 | faim_export int aim_setdebuggingcb(aim_session_t *sess, faim_debugging_callback_t cb) |
---|
961 | { |
---|
962 | |
---|
963 | if (!sess) |
---|
964 | return -1; |
---|
965 | |
---|
966 | sess->debugcb = cb; |
---|
967 | |
---|
968 | return 0; |
---|
969 | } |
---|
970 | |
---|
971 | /** |
---|
972 | * aim_conn_isconnecting - Determine if a connection is connecting |
---|
973 | * @conn: Connection to examine |
---|
974 | * |
---|
975 | * Returns nonzero if the connection is in the process of |
---|
976 | * connecting (or if it just completed and aim_conn_completeconnect() |
---|
977 | * has yet to be called on it). |
---|
978 | * |
---|
979 | */ |
---|
980 | faim_export int aim_conn_isconnecting(aim_conn_t *conn) |
---|
981 | { |
---|
982 | |
---|
983 | if (!conn) |
---|
984 | return 0; |
---|
985 | |
---|
986 | return !!(conn->status & AIM_CONN_STATUS_INPROGRESS); |
---|
987 | } |
---|
988 | |
---|
989 | /* |
---|
990 | * XXX this is nearly as ugly as proxyconnect(). |
---|
991 | */ |
---|
992 | faim_export int aim_conn_completeconnect(aim_session_t *sess, aim_conn_t *conn) |
---|
993 | { |
---|
994 | fd_set fds, wfds; |
---|
995 | struct timeval tv; |
---|
996 | int res; |
---|
997 | int error = ETIMEDOUT; |
---|
998 | |
---|
999 | aim_rxcallback_t userfunc; |
---|
1000 | |
---|
1001 | if (!conn || (conn->fd == -1)) |
---|
1002 | return -1; |
---|
1003 | |
---|
1004 | if (!(conn->status & AIM_CONN_STATUS_INPROGRESS)) |
---|
1005 | return -1; |
---|
1006 | |
---|
1007 | FD_ZERO(&fds); |
---|
1008 | FD_SET(conn->fd, &fds); |
---|
1009 | FD_ZERO(&wfds); |
---|
1010 | FD_SET(conn->fd, &wfds); |
---|
1011 | tv.tv_sec = 0; |
---|
1012 | tv.tv_usec = 0; |
---|
1013 | |
---|
1014 | if ((res = select(conn->fd+1, &fds, &wfds, NULL, &tv)) == -1) { |
---|
1015 | error = errno; |
---|
1016 | aim_conn_close(conn); |
---|
1017 | errno = error; |
---|
1018 | return -1; |
---|
1019 | } else if (res == 0) { |
---|
1020 | faimdprintf(sess, 0, "aim_conn_completeconnect: false alarm on %d\n", conn->fd); |
---|
1021 | return 0; /* hasn't really completed yet... */ |
---|
1022 | } |
---|
1023 | |
---|
1024 | if (FD_ISSET(conn->fd, &fds) || FD_ISSET(conn->fd, &wfds)) { |
---|
1025 | int len = sizeof(error); |
---|
1026 | if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) |
---|
1027 | error = errno; |
---|
1028 | } |
---|
1029 | |
---|
1030 | if (error) { |
---|
1031 | aim_conn_close(conn); |
---|
1032 | errno = error; |
---|
1033 | return -1; |
---|
1034 | } |
---|
1035 | fcntl(conn->fd, F_SETFL, 0); /* XXX should restore original flags */ |
---|
1036 | |
---|
1037 | conn->status &= ~AIM_CONN_STATUS_INPROGRESS; |
---|
1038 | |
---|
1039 | if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNCOMPLETE))) |
---|
1040 | userfunc(sess, NULL, conn); |
---|
1041 | |
---|
1042 | /* Flush out the queues if there was something waiting for this conn */ |
---|
1043 | aim_tx_flushqueue(sess); |
---|
1044 | |
---|
1045 | return 0; |
---|
1046 | } |
---|
1047 | |
---|
1048 | faim_export aim_session_t *aim_conn_getsess(aim_conn_t *conn) |
---|
1049 | { |
---|
1050 | |
---|
1051 | if (!conn) |
---|
1052 | return NULL; |
---|
1053 | |
---|
1054 | return (aim_session_t *)conn->sessv; |
---|
1055 | } |
---|
1056 | |
---|
1057 | /* |
---|
1058 | * aim_logoff() |
---|
1059 | * |
---|
1060 | * Closes -ALL- open connections. |
---|
1061 | * |
---|
1062 | */ |
---|
1063 | faim_export int aim_logoff(aim_session_t *sess) |
---|
1064 | { |
---|
1065 | |
---|
1066 | aim_connrst(sess); /* in case we want to connect again */ |
---|
1067 | |
---|
1068 | return 0; |
---|
1069 | } |
---|
1070 | |
---|
1071 | /* |
---|
1072 | * aim_flap_nop() |
---|
1073 | * |
---|
1074 | * No-op. WinAIM 4.x sends these _every minute_ to keep |
---|
1075 | * the connection alive. |
---|
1076 | */ |
---|
1077 | faim_export int aim_flap_nop(aim_session_t *sess, aim_conn_t *conn) |
---|
1078 | { |
---|
1079 | aim_frame_t *fr; |
---|
1080 | |
---|
1081 | if (!(fr = aim_tx_new(sess, conn, AIM_FRAMETYPE_FLAP, 0x05, 0))) |
---|
1082 | return -ENOMEM; |
---|
1083 | |
---|
1084 | aim_tx_enqueue(sess, fr); |
---|
1085 | |
---|
1086 | return 0; |
---|
1087 | } |
---|