]> andersk Git - libfaim.git/blame - src/conn.c
- Mon Mar 5 01:19:48 UTC 2001
[libfaim.git] / src / conn.c
CommitLineData
9de3ca7e 1
2/*
3 * aim_conn.c
4 *
5 * Does all this gloriously nifty connection handling stuff...
6 *
7 */
8
37ee990e 9#define FAIM_INTERNAL
dd60ff8b 10#include <aim.h>
9de3ca7e 11
67a264ef 12#ifndef _WIN32
78b3fb13 13#include <netdb.h>
14#include <sys/socket.h>
15#include <netinet/in.h>
67a264ef 16#endif
78b3fb13 17
be67fdd0 18/**
19 * aim_connrst - Clears out connection list, killing remaining connections.
20 * @sess: Session to be cleared
21 *
22 * Clears out the connection list and kills any connections left.
23 *
040457cc 24 */
78b3fb13 25faim_internal void aim_connrst(struct aim_session_t *sess)
9de3ca7e 26{
b13c9e13 27 faim_mutex_init(&sess->connlistlock);
040457cc 28 if (sess->connlist) {
29 struct aim_conn_t *cur = sess->connlist, *tmp;
30
31 while(cur) {
32 tmp = cur->next;
33 aim_conn_close(cur);
34 free(cur);
35 cur = tmp;
36 }
e88ba395 37 }
040457cc 38 sess->connlist = NULL;
39 return;
9de3ca7e 40}
41
9d2a3582 42/**
43 * aim_conn_init - Reset a connection to default values.
44 * @deadconn: Connection to be reset
45 *
46 * Initializes and/or resets a connection structure.
47 *
48 */
49static void aim_conn_init(struct aim_conn_t *deadconn)
50{
51 if (!deadconn)
52 return;
53
54 deadconn->fd = -1;
55 deadconn->subtype = -1;
56 deadconn->type = -1;
57 deadconn->seqnum = 0;
58 deadconn->lastactivity = 0;
59 deadconn->forcedlatency = 0;
60 deadconn->handlerlist = NULL;
61 deadconn->priv = NULL;
62 faim_mutex_init(&deadconn->active);
63 faim_mutex_init(&deadconn->seqnum_lock);
64
65 return;
66}
67
be67fdd0 68/**
69 * aim_conn_getnext - Gets a new connection structure.
70 * @sess: Session
71 *
72 * Allocate a new empty connection structure.
73 *
040457cc 74 */
78b3fb13 75faim_internal struct aim_conn_t *aim_conn_getnext(struct aim_session_t *sess)
9de3ca7e 76{
040457cc 77 struct aim_conn_t *newconn, *cur;
78
79 if (!(newconn = malloc(sizeof(struct aim_conn_t))))
80 return NULL;
81
82 memset(newconn, 0, sizeof(struct aim_conn_t));
9d2a3582 83 aim_conn_init(newconn);
040457cc 84 newconn->next = NULL;
85
86 faim_mutex_lock(&sess->connlistlock);
87 if (sess->connlist == NULL)
88 sess->connlist = newconn;
89 else {
90 for (cur = sess->connlist; cur->next; cur = cur->next)
91 ;
92 cur->next = newconn;
93 }
94 faim_mutex_unlock(&sess->connlistlock);
95
96 return newconn;
97}
98
be67fdd0 99/**
100 * aim_conn_kill - Close and free a connection.
101 * @sess: Session for the connection
102 * @deadconn: Connection to be freed
103 *
646c6b52 104 * Close, clear, and free a connection structure. Should never be
105 * called from within libfaim.
be67fdd0 106 *
107 */
78b3fb13 108faim_export void aim_conn_kill(struct aim_session_t *sess, struct aim_conn_t **deadconn)
040457cc 109{
110 struct aim_conn_t *cur;
111
112 if (!deadconn || !*deadconn)
113 return;
114
115 faim_mutex_lock(&sess->connlistlock);
116 if (sess->connlist == NULL)
117 ;
118 else if (sess->connlist->next == NULL) {
119 if (sess->connlist == *deadconn)
120 sess->connlist = NULL;
121 } else {
122 cur = sess->connlist;
123 while (cur->next) {
124 if (cur->next == *deadconn) {
125 cur->next = cur->next->next;
126 break;
127 }
128 cur = cur->next;
129 }
130 }
131 faim_mutex_unlock(&sess->connlistlock);
132
68ac63c2 133 /* XXX: do we need this for txqueue too? */
134 aim_rxqueue_cleanbyconn(sess, *deadconn);
135
9d2a3582 136 if ((*deadconn)->fd != -1)
137 aim_conn_close(*deadconn);
f8ac5020 138 if ((*deadconn)->priv)
139 free((*deadconn)->priv);
040457cc 140 free(*deadconn);
141 deadconn = NULL;
142
143 return;
9de3ca7e 144}
145
be67fdd0 146/**
147 * aim_conn_close - Close a connection
148 * @deadconn: Connection to close
149 *
150 * Close (but not free) a connection.
151 *
9d2a3582 152 * This leaves everything untouched except for clearing the
153 * handler list and setting the fd to -1 (used to recognize
154 * dead connections).
155 *
be67fdd0 156 */
78b3fb13 157faim_export void aim_conn_close(struct aim_conn_t *deadconn)
9de3ca7e 158{
4d7c1bfb 159
160 faim_mutex_destroy(&deadconn->active);
161 faim_mutex_destroy(&deadconn->seqnum_lock);
9de3ca7e 162 if (deadconn->fd >= 3)
163 close(deadconn->fd);
9d2a3582 164 deadconn->fd = -1;
0252e8c0 165 if (deadconn->handlerlist)
166 aim_clearhandlers(deadconn);
3369f8d4 167
4d7c1bfb 168 return;
9de3ca7e 169}
170
be67fdd0 171/**
172 * aim_getconn_type - Find a connection of a specific type
173 * @sess: Session to search
174 * @type: Type of connection to look for
175 *
176 * Searches for a connection of the specified type in the
177 * specified session. Returns the first connection of that
178 * type found.
179 *
180 */
37ee990e 181faim_export struct aim_conn_t *aim_getconn_type(struct aim_session_t *sess,
182 int type)
9de3ca7e 183{
040457cc 184 struct aim_conn_t *cur;
185
186 faim_mutex_lock(&sess->connlistlock);
187 for (cur = sess->connlist; cur; cur = cur->next) {
22517493 188 if ((cur->type == type) && !(cur->status & AIM_CONN_STATUS_INPROGRESS))
040457cc 189 break;
190 }
191 faim_mutex_unlock(&sess->connlistlock);
192 return cur;
9de3ca7e 193}
194
be67fdd0 195/**
196 * aim_proxyconnect - An extrememly quick and dirty SOCKS5 interface.
197 * @sess: Session to connect
198 * @host: Host to connect to
199 * @port: Port to connect to
200 * @statusret: Return value of the connection
201 *
202 * Attempts to connect to the specified host via the configured
203 * proxy settings, if present. If no proxy is configured for
204 * this session, the connection is done directly.
205 *
bb0dc593 206 */
207static int aim_proxyconnect(struct aim_session_t *sess,
208 char *host, unsigned short port,
209 int *statusret)
210{
211 int fd = -1;
212
213 if (strlen(sess->socksproxy.server)) { /* connecting via proxy */
214 int i;
215 unsigned char buf[512];
216 struct sockaddr_in sa;
217 struct hostent *hp;
218 char *proxy;
219 unsigned short proxyport = 1080;
220
221 for(i=0;i<(int)strlen(sess->socksproxy.server);i++) {
222 if (sess->socksproxy.server[i] == ':') {
223 proxyport = atoi(&(sess->socksproxy.server[i+1]));
224 break;
225 }
226 }
227 proxy = (char *)malloc(i+1);
228 strncpy(proxy, sess->socksproxy.server, i);
229 proxy[i] = '\0';
230
231 if (!(hp = gethostbyname(proxy))) {
646c6b52 232 faimdprintf(sess, 0, "proxyconnect: unable to resolve proxy name\n");
bb0dc593 233 *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR);
234 return -1;
235 }
236 free(proxy);
237
238 memset(&sa.sin_zero, 0, 8);
239 sa.sin_port = htons(proxyport);
240 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
241 sa.sin_family = hp->h_addrtype;
242
243 fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
244 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
646c6b52 245 faimdprintf(sess, 0, "proxyconnect: unable to connect to proxy\n");
bb0dc593 246 close(fd);
247 return -1;
248 }
249
250 i = 0;
251 buf[0] = 0x05; /* SOCKS version 5 */
252 if (strlen(sess->socksproxy.username)) {
253 buf[1] = 0x02; /* two methods */
254 buf[2] = 0x00; /* no authentication */
255 buf[3] = 0x02; /* username/password authentication */
256 i = 4;
257 } else {
258 buf[1] = 0x01;
259 buf[2] = 0x00;
260 i = 3;
261 }
262
263 if (write(fd, buf, i) < i) {
264 *statusret = errno;
265 close(fd);
266 return -1;
267 }
268
269 if (read(fd, buf, 2) < 2) {
270 *statusret = errno;
271 close(fd);
272 return -1;
273 }
274
275 if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
276 *statusret = EINVAL;
277 close(fd);
278 return -1;
279 }
280
281 /* check if we're doing username authentication */
282 if (buf[1] == 0x02) {
283 i = aimutil_put8(buf, 0x01); /* version 1 */
284 i += aimutil_put8(buf+i, strlen(sess->socksproxy.username));
285 i += aimutil_putstr(buf+i, sess->socksproxy.username, strlen(sess->socksproxy.username));
286 i += aimutil_put8(buf+i, strlen(sess->socksproxy.password));
287 i += aimutil_putstr(buf+i, sess->socksproxy.password, strlen(sess->socksproxy.password));
288 if (write(fd, buf, i) < i) {
289 *statusret = errno;
290 close(fd);
291 return -1;
292 }
293 if (read(fd, buf, 2) < 2) {
294 *statusret = errno;
295 close(fd);
296 return -1;
297 }
298 if ((buf[0] != 0x01) || (buf[1] != 0x00)) {
299 *statusret = EINVAL;
300 close(fd);
301 return -1;
302 }
303 }
304
305 i = aimutil_put8(buf, 0x05);
306 i += aimutil_put8(buf+i, 0x01); /* CONNECT */
307 i += aimutil_put8(buf+i, 0x00); /* reserved */
308 i += aimutil_put8(buf+i, 0x03); /* address type: host name */
309 i += aimutil_put8(buf+i, strlen(host));
310 i += aimutil_putstr(buf+i, host, strlen(host));
311 i += aimutil_put16(buf+i, port);
312
313 if (write(fd, buf, i) < i) {
314 *statusret = errno;
315 close(fd);
316 return -1;
317 }
318 if (read(fd, buf, 10) < 10) {
319 *statusret = errno;
320 close(fd);
321 return -1;
322 }
323 if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
324 *statusret = EINVAL;
325 close(fd);
326 return -1;
327 }
328
329 } else { /* connecting directly */
330 struct sockaddr_in sa;
331 struct hostent *hp;
332
333 if (!(hp = gethostbyname(host))) {
334 *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR);
335 return -1;
336 }
337
22517493 338 memset(&sa, 0, sizeof(struct sockaddr_in));
bb0dc593 339 sa.sin_port = htons(port);
340 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
341 sa.sin_family = hp->h_addrtype;
342
343 fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
22517493 344
345 if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT)
346 fcntl(fd, F_SETFL, O_NONBLOCK); /* XXX save flags */
347
bb0dc593 348 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
22517493 349 if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT) {
350 if ((errno == EINPROGRESS) || (errno == EINTR)) {
351 if (statusret)
352 *statusret |= AIM_CONN_STATUS_INPROGRESS;
353 return fd;
354 }
355 }
bb0dc593 356 close(fd);
357 fd = -1;
358 }
359 }
360 return fd;
361}
362
646c6b52 363/**
364 * aim_cloneconn - clone an aim_conn_t
365 * @sess: session containing parent
366 * @src: connection to clone
367 *
368 * A new connection is allocated, and the values are filled in
369 * appropriately. Note that this function sets the new connnection's
370 * ->priv pointer to be equal to that of its parent: only the pointer
371 * is copied, not the data it points to.
372 *
373 * This function returns a pointer to the new aim_conn_t, or %NULL on
374 * error
375 */
37ee990e 376faim_internal struct aim_conn_t *aim_cloneconn(struct aim_session_t *sess,
377 struct aim_conn_t *src)
378{
379 struct aim_conn_t *conn;
380 struct aim_rxcblist_t *cur;
381
382 if (!(conn = aim_conn_getnext(sess)))
383 return NULL;
37ee990e 384
385 faim_mutex_lock(&conn->active);
386
387 conn->fd = src->fd;
388 conn->type = src->type;
389 conn->subtype = src->subtype;
390 conn->seqnum = src->seqnum;
391 conn->priv = src->priv;
392 conn->lastactivity = src->lastactivity;
393 conn->forcedlatency = src->forcedlatency;
394
395 /* clone handler list */
396 for (cur = src->handlerlist; cur; cur = cur->next) {
397 aim_conn_addhandler(sess, conn, cur->family, cur->type,
398 cur->handler, cur->flags);
399 }
400
401 faim_mutex_unlock(&conn->active);
402
403 return conn;
404}
405
be67fdd0 406/**
407 * aim_newconn - Open a new connection
408 * @sess: Session to create connection in
409 * @type: Type of connection to create
410 * @dest: Host to connect to (in "host:port" syntax)
9de3ca7e 411 *
be67fdd0 412 * Opens a new connection to the specified dest host of specified
413 * type, using the proxy settings if available. If @host is %NULL,
414 * the connection is allocated and returned, but no connection
415 * is made.
9de3ca7e 416 *
9de3ca7e 417 * FIXME: Return errors in a more sane way.
418 *
419 */
78b3fb13 420faim_export struct aim_conn_t *aim_newconn(struct aim_session_t *sess,
421 int type, char *dest)
9de3ca7e 422{
423 struct aim_conn_t *connstruct;
424 int ret;
9de3ca7e 425 u_short port = FAIM_LOGIN_PORT;
a25832e6 426 char *host = NULL;
9de3ca7e 427 int i=0;
040457cc 428
e6b05d80 429 if ((connstruct=aim_conn_getnext(sess))==NULL)
9de3ca7e 430 return NULL;
431
040457cc 432 faim_mutex_lock(&connstruct->active);
433
9de3ca7e 434 connstruct->type = type;
435
e6b05d80 436 if (!dest) { /* just allocate a struct */
437 connstruct->fd = -1;
438 connstruct->status = 0;
040457cc 439 faim_mutex_unlock(&connstruct->active);
e6b05d80 440 return connstruct;
441 }
442
9de3ca7e 443 /*
444 * As of 23 Jul 1999, AOL now sends the port number, preceded by a
445 * colon, in the BOS redirect. This fatally breaks all previous
446 * libfaims. Bad, bad AOL.
447 *
448 * We put this here to catch every case.
449 *
450 */
451
5ac21963 452 for(i=0;i<(int)strlen(dest);i++) {
e88ba395 453 if (dest[i] == ':') {
454 port = atoi(&(dest[i+1]));
455 break;
9de3ca7e 456 }
e88ba395 457 }
a25832e6 458 host = (char *)malloc(i+1);
459 strncpy(host, dest, i);
9ba272ca 460 host[i] = '\0';
461
bb0dc593 462 if ((ret = aim_proxyconnect(sess, host, port, &connstruct->status)) < 0) {
e88ba395 463 connstruct->fd = -1;
464 connstruct->status = (errno | AIM_CONN_STATUS_CONNERR);
bb0dc593 465 free(host);
040457cc 466 faim_mutex_unlock(&connstruct->active);
e88ba395 467 return connstruct;
bb0dc593 468 } else
469 connstruct->fd = ret;
a25832e6 470
040457cc 471 faim_mutex_unlock(&connstruct->active);
472
bb0dc593 473 free(host);
474
9de3ca7e 475 return connstruct;
476}
477
be67fdd0 478/**
479 * aim_conngetmaxfd - Return the highest valued file discriptor in session
480 * @sess: Session to search
481 *
482 * Returns the highest valued filed descriptor of all open
483 * connections in @sess.
484 *
485 */
78b3fb13 486faim_export int aim_conngetmaxfd(struct aim_session_t *sess)
9de3ca7e 487{
040457cc 488 int j = 0;
489 struct aim_conn_t *cur;
490
491 faim_mutex_lock(&sess->connlistlock);
492 for (cur = sess->connlist; cur; cur = cur->next) {
493 if (cur->fd > j)
494 j = cur->fd;
495 }
496 faim_mutex_unlock(&sess->connlistlock);
e88ba395 497
9de3ca7e 498 return j;
499}
500
be67fdd0 501/**
502 * aim_conn_in_sess - Predicate to test the precense of a connection in a sess
503 * @sess: Session to look in
504 * @conn: Connection to look for
505 *
506 * Searches @sess for the passed connection. Returns 1 if its present,
507 * zero otherwise.
508 *
509 */
3b101546 510faim_export int aim_conn_in_sess(struct aim_session_t *sess, struct aim_conn_t *conn)
511{
512 struct aim_conn_t *cur;
513
514 faim_mutex_lock(&sess->connlistlock);
515 for(cur = sess->connlist; cur; cur = cur->next)
516 if(cur == conn) {
517 faim_mutex_unlock(&sess->connlistlock);
518 return 1;
519 }
520 faim_mutex_unlock(&sess->connlistlock);
521 return 0;
522}
523
be67fdd0 524/**
525 * aim_select - Wait for a socket with data or timeout
526 * @sess: Session to wait on
527 * @timeout: How long to wait
528 * @status: Return status
9de3ca7e 529 *
530 * Waits for a socket with data or for timeout, whichever comes first.
646c6b52 531 * See select(2).
9de3ca7e 532 *
b8d0da45 533 * Return codes in *status:
be67fdd0 534 * -1 error in select() (%NULL returned)
535 * 0 no events pending (%NULL returned)
536 * 1 outgoing data pending (%NULL returned)
b8d0da45 537 * 2 incoming data pending (connection with pending data returned)
538 *
040457cc 539 * XXX: we could probably stand to do a little courser locking here.
540 *
9de3ca7e 541 */
78b3fb13 542faim_export struct aim_conn_t *aim_select(struct aim_session_t *sess,
543 struct timeval *timeout, int *status)
9de3ca7e 544{
040457cc 545 struct aim_conn_t *cur;
22517493 546 fd_set fds, wfds;
040457cc 547 int maxfd = 0;
22517493 548 int i, haveconnecting = 0;
9de3ca7e 549
040457cc 550 faim_mutex_lock(&sess->connlistlock);
551 if (sess->connlist == NULL) {
552 faim_mutex_unlock(&sess->connlistlock);
68ac63c2 553 *status = -1;
554 return NULL;
040457cc 555 }
556 faim_mutex_unlock(&sess->connlistlock);
f1a5efe0 557
9de3ca7e 558 FD_ZERO(&fds);
22517493 559 FD_ZERO(&wfds);
040457cc 560 maxfd = 0;
561
562 faim_mutex_lock(&sess->connlistlock);
563 for (cur = sess->connlist; cur; cur = cur->next) {
9d2a3582 564 if (cur->fd == -1) {
565 /* don't let invalid/dead connections sit around */
566 *status = 2;
567 faim_mutex_unlock(&sess->connlistlock);
568 return cur;
569 } else if (cur->status & AIM_CONN_STATUS_INPROGRESS) {
22517493 570 FD_SET(cur->fd, &wfds);
571 haveconnecting++;
572 }
040457cc 573 FD_SET(cur->fd, &fds);
574 if (cur->fd > maxfd)
575 maxfd = cur->fd;
576 }
577 faim_mutex_unlock(&sess->connlistlock);
578
22517493 579 /*
580 * If we have data waiting to be sent, return
581 *
582 * We have to not do this if theres at least one
583 * connection thats still connecting, since that connection
584 * may have queued data and this return would prevent
585 * the connection from ever completing! This is a major
586 * inadequacy of the libfaim way of doing things. It means
587 * that nothing can transmit as long as there's connecting
588 * sockets. Evil.
589 *
590 * But its still better than having blocking connects.
591 *
592 */
593 if (!haveconnecting && (sess->queue_outgoing != NULL)) {
594 *status = 1;
595 return NULL;
596 }
597
598 if ((i = select(maxfd+1, &fds, &wfds, NULL, timeout))>=1) {
040457cc 599 faim_mutex_lock(&sess->connlistlock);
600 for (cur = sess->connlist; cur; cur = cur->next) {
22517493 601 if ((FD_ISSET(cur->fd, &fds)) ||
602 ((cur->status & AIM_CONN_STATUS_INPROGRESS) &&
603 FD_ISSET(cur->fd, &wfds))) {
040457cc 604 *status = 2;
605 faim_mutex_unlock(&sess->connlistlock);
22517493 606 return cur; /* XXX race condition here -- shouldnt unlock connlist */
040457cc 607 }
608 }
9797852c 609 *status = 0; /* shouldn't happen */
610 } else if ((i == -1) && (errno == EINTR)) /* treat interrupts as a timeout */
611 *status = 0;
612 else
613 *status = i; /* can be 0 or -1 */
b8d0da45 614
040457cc 615 faim_mutex_unlock(&sess->connlistlock);
646c6b52 616
b8d0da45 617 return NULL; /* no waiting or error, return */
9de3ca7e 618}
619
be67fdd0 620/**
621 * aim_conn_isready - Test if a connection is marked ready
622 * @conn: Connection to test
623 *
624 * Returns true if the connection is ready, false otherwise.
625 * Returns -1 if the connection is invalid.
626 *
627 * XXX: This is deprecated.
628 *
629 */
78b3fb13 630faim_export int aim_conn_isready(struct aim_conn_t *conn)
9de3ca7e 631{
632 if (conn)
633 return (conn->status & 0x0001);
040457cc 634 return -1;
9de3ca7e 635}
636
be67fdd0 637/**
638 * aim_conn_setstatus - Set the status of a connection
639 * @conn: Connection
640 * @status: New status
641 *
642 * @newstatus is %XOR'd with the previous value of the connection
643 * status and returned. Returns -1 if the connection is invalid.
644 *
22517493 645 * This isn't real useful.
646 *
be67fdd0 647 */
78b3fb13 648faim_export int aim_conn_setstatus(struct aim_conn_t *conn, int status)
9de3ca7e 649{
040457cc 650 int val;
651
652 if (!conn)
9de3ca7e 653 return -1;
040457cc 654
655 faim_mutex_lock(&conn->active);
656 val = conn->status ^= status;
657 faim_mutex_unlock(&conn->active);
646c6b52 658
040457cc 659 return val;
9de3ca7e 660}
661
be67fdd0 662/**
663 * aim_conn_setlatency - Set a forced latency value for connection
664 * @conn: Conn to set latency for
665 * @newval: Number of seconds to force between transmits
666 *
667 * Causes @newval seconds to be spent between transmits on a connection.
668 *
669 * This is my lame attempt at overcoming not understanding the rate
670 * limiting.
671 *
672 * XXX: This should really be replaced with something that scales and
673 * backs off like the real rate limiting does.
674 *
675 */
78b3fb13 676faim_export int aim_conn_setlatency(struct aim_conn_t *conn, int newval)
9de3ca7e 677{
678 if (!conn)
679 return -1;
040457cc 680
681 faim_mutex_lock(&conn->active);
9de3ca7e 682 conn->forcedlatency = newval;
683 conn->lastactivity = 0; /* reset this just to make sure */
040457cc 684 faim_mutex_unlock(&conn->active);
9de3ca7e 685
686 return 0;
687}
a25832e6 688
be67fdd0 689/**
690 * aim_setupproxy - Configure a proxy for this session
691 * @sess: Session to set proxy for
692 * @server: SOCKS server
693 * @username: SOCKS username
694 * @password: SOCKS password
695 *
bb0dc593 696 * Call this with your SOCKS5 proxy server parameters before
be67fdd0 697 * the first call to aim_newconn(). If called with all %NULL
bb0dc593 698 * args, it will clear out a previously set proxy.
699 *
be67fdd0 700 * Set username and password to %NULL if not applicable.
bb0dc593 701 *
702 */
703faim_export void aim_setupproxy(struct aim_session_t *sess, char *server, char *username, char *password)
704{
705 /* clear out the proxy info */
706 if (!server || !strlen(server)) {
707 memset(sess->socksproxy.server, 0, sizeof(sess->socksproxy.server));
708 memset(sess->socksproxy.username, 0, sizeof(sess->socksproxy.username));
709 memset(sess->socksproxy.password, 0, sizeof(sess->socksproxy.password));
710 return;
711 }
712
713 strncpy(sess->socksproxy.server, server, sizeof(sess->socksproxy.server));
714 if (username && strlen(username))
715 strncpy(sess->socksproxy.username, username, sizeof(sess->socksproxy.username));
716 if (password && strlen(password))
717 strncpy(sess->socksproxy.password, password, sizeof(sess->socksproxy.password));
718 return;
719}
720
be67fdd0 721/**
722 * aim_session_init - Initializes a session structure
723 * @sess: Session to initialize
22517493 724 * @flags: Flags to use. Any of %AIM_SESS_FLAGS %OR'd together.
646c6b52 725 * @debuglevel: Level of debugging output (zero is least)
be67fdd0 726 *
727 * Sets up the initial values for a session.
728 *
729 */
646c6b52 730faim_export void aim_session_init(struct aim_session_t *sess, unsigned long flags, int debuglevel)
a25832e6 731{
a25832e6 732 if (!sess)
733 return;
734
040457cc 735 memset(sess, 0, sizeof(struct aim_session_t));
e88ba395 736 aim_connrst(sess);
a25832e6 737 sess->queue_outgoing = NULL;
738 sess->queue_incoming = NULL;
0c20631f 739 sess->pendingjoin = NULL;
9f20a4e3 740 sess->pendingjoinexchange = 0;
b13c9e13 741 aim_initsnachash(sess);
9f20a4e3 742 sess->msgcookies = NULL;
a25832e6 743 sess->snac_nextid = 0x00000001;
22517493 744
745 sess->flags = 0;
646c6b52 746 sess->debug = 0;
747 sess->debugcb = NULL;
22517493 748
749 /*
750 * Default to SNAC login unless XORLOGIN is explicitly set.
751 */
752 if (!(flags & AIM_SESS_FLAGS_XORLOGIN))
753 sess->flags |= AIM_SESS_FLAGS_SNACLOGIN;
754 sess->flags |= flags;
a25832e6 755
e88ba395 756 /*
757 * This must always be set. Default to the queue-based
758 * version for back-compatibility.
759 */
b8c79ca7 760 aim_tx_setenqueue(sess, AIM_TX_QUEUED, NULL);
e88ba395 761
a25832e6 762 return;
763}
22517493 764
646c6b52 765/**
766 * aim_setdebuggingcb - Set the function to call when outputting debugging info
767 * @sess: Session to change
768 * @cb: Function to call
769 *
770 * The function specified is called whenever faimdprintf() is used within
771 * libfaim, and the session's debugging level is greater tha nor equal to
772 * the value faimdprintf was called with.
773 *
774 */
775faim_export int aim_setdebuggingcb(struct aim_session_t *sess, faim_debugging_callback_t cb)
776{
777
778 if (!sess)
779 return -1;
780
781 sess->debugcb = cb;
782
783 return 0;
784}
785
22517493 786/**
787 * aim_conn_isconnecting - Determine if a connection is connecting
788 * @conn: Connection to examine
789 *
790 * Returns nonzero if the connection is in the process of
791 * connecting (or if it just completed and aim_conn_completeconnect()
792 * has yet to be called on it).
793 *
794 */
795faim_export int aim_conn_isconnecting(struct aim_conn_t *conn)
796{
797 if (!conn)
798 return 0;
799 return (conn->status & AIM_CONN_STATUS_INPROGRESS)?1:0;
800}
801
802faim_export int aim_conn_completeconnect(struct aim_session_t *sess, struct aim_conn_t *conn)
803{
804 fd_set fds, wfds;
805 struct timeval tv;
806 int res, error = ETIMEDOUT;
807 rxcallback_t userfunc;
808
809 if (!conn || (conn->fd == -1))
810 return -1;
811
812 if (!(conn->status & AIM_CONN_STATUS_INPROGRESS))
813 return -1;
814
815 FD_ZERO(&fds);
816 FD_SET(conn->fd, &fds);
817 FD_ZERO(&wfds);
818 FD_SET(conn->fd, &wfds);
819 tv.tv_sec = 0;
820 tv.tv_usec = 0;
821
822 if ((res = select(conn->fd+1, &fds, &wfds, NULL, &tv)) == -1) {
823 error = errno;
824 aim_conn_close(conn);
825 errno = error;
826 return -1;
827 } else if (res == 0) {
646c6b52 828 faimdprintf(sess, 0, "aim_conn_completeconnect: false alarm on %d\n", conn->fd);
22517493 829 return 0; /* hasn't really completed yet... */
830 }
831
832 if (FD_ISSET(conn->fd, &fds) || FD_ISSET(conn->fd, &wfds)) {
833 int len = sizeof(error);
834
835 if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
836 error = errno;
837 }
838
839 if (error) {
840 aim_conn_close(conn);
841 errno = error;
842 return -1;
843 }
844
845 fcntl(conn->fd, F_SETFL, 0); /* XXX should restore original flags */
846
847 conn->status &= ~AIM_CONN_STATUS_INPROGRESS;
848
646c6b52 849 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNCOMPLETE)))
22517493 850 userfunc(sess, NULL, conn);
851
852 /* Flush out the queues if there was something waiting for this conn */
853 aim_tx_flushqueue(sess);
854
855 return 0;
856}
dd60ff8b 857
858/*
859 * aim_logoff()
860 *
861 * Closes -ALL- open connections.
862 *
863 */
864faim_export int aim_logoff(struct aim_session_t *sess)
865{
646c6b52 866
dd60ff8b 867 aim_connrst(sess); /* in case we want to connect again */
868
869 return 0;
870
871}
872
This page took 3.218601 seconds and 5 git commands to generate.