5 * Does all this gloriously nifty connection handling stuff...
14 #include <sys/socket.h>
15 #include <netinet/in.h>
19 * aim_connrst - Clears out connection list, killing remaining connections.
20 * @sess: Session to be cleared
22 * Clears out the connection list and kills any connections left.
25 faim_internal void aim_connrst(struct aim_session_t *sess)
27 faim_mutex_init(&sess->connlistlock);
29 struct aim_conn_t *cur = sess->connlist, *tmp;
38 sess->connlist = NULL;
43 * aim_conn_init - Reset a connection to default values.
44 * @deadconn: Connection to be reset
46 * Initializes and/or resets a connection structure.
49 static void aim_conn_init(struct aim_conn_t *deadconn)
55 deadconn->subtype = -1;
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);
69 * aim_conn_getnext - Gets a new connection structure.
72 * Allocate a new empty connection structure.
75 faim_internal struct aim_conn_t *aim_conn_getnext(struct aim_session_t *sess)
77 struct aim_conn_t *newconn, *cur;
79 if (!(newconn = malloc(sizeof(struct aim_conn_t))))
82 memset(newconn, 0, sizeof(struct aim_conn_t));
83 aim_conn_init(newconn);
86 faim_mutex_lock(&sess->connlistlock);
87 if (sess->connlist == NULL)
88 sess->connlist = newconn;
90 for (cur = sess->connlist; cur->next; cur = cur->next)
94 faim_mutex_unlock(&sess->connlistlock);
100 * aim_conn_kill - Close and free a connection.
101 * @sess: Session for the connection
102 * @deadconn: Connection to be freed
104 * Close, clear, and free a connection structure. Should never be
105 * called from within libfaim.
108 faim_export void aim_conn_kill(struct aim_session_t *sess, struct aim_conn_t **deadconn)
110 struct aim_conn_t *cur;
112 if (!deadconn || !*deadconn)
115 faim_mutex_lock(&sess->connlistlock);
116 if (sess->connlist == NULL)
118 else if (sess->connlist->next == NULL) {
119 if (sess->connlist == *deadconn)
120 sess->connlist = NULL;
122 cur = sess->connlist;
124 if (cur->next == *deadconn) {
125 cur->next = cur->next->next;
131 faim_mutex_unlock(&sess->connlistlock);
133 /* XXX: do we need this for txqueue too? */
134 aim_rxqueue_cleanbyconn(sess, *deadconn);
136 if ((*deadconn)->fd != -1)
137 aim_conn_close(*deadconn);
138 if ((*deadconn)->priv)
139 free((*deadconn)->priv);
147 * aim_conn_close - Close a connection
148 * @deadconn: Connection to close
150 * Close (but not free) a connection.
152 * This leaves everything untouched except for clearing the
153 * handler list and setting the fd to -1 (used to recognize
157 faim_export void aim_conn_close(struct aim_conn_t *deadconn)
160 faim_mutex_destroy(&deadconn->active);
161 faim_mutex_destroy(&deadconn->seqnum_lock);
162 if (deadconn->fd >= 3)
165 if (deadconn->handlerlist)
166 aim_clearhandlers(deadconn);
172 * aim_getconn_type - Find a connection of a specific type
173 * @sess: Session to search
174 * @type: Type of connection to look for
176 * Searches for a connection of the specified type in the
177 * specified session. Returns the first connection of that
181 faim_export struct aim_conn_t *aim_getconn_type(struct aim_session_t *sess,
184 struct aim_conn_t *cur;
186 faim_mutex_lock(&sess->connlistlock);
187 for (cur = sess->connlist; cur; cur = cur->next) {
188 if ((cur->type == type) && !(cur->status & AIM_CONN_STATUS_INPROGRESS))
191 faim_mutex_unlock(&sess->connlistlock);
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
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.
207 static int aim_proxyconnect(struct aim_session_t *sess,
208 char *host, unsigned short port,
213 if (strlen(sess->socksproxy.server)) { /* connecting via proxy */
215 unsigned char buf[512];
216 struct sockaddr_in sa;
219 unsigned short proxyport = 1080;
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]));
227 proxy = (char *)malloc(i+1);
228 strncpy(proxy, sess->socksproxy.server, i);
231 if (!(hp = gethostbyname(proxy))) {
232 faimdprintf(sess, 0, "proxyconnect: unable to resolve proxy name\n");
233 *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR);
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;
243 fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
244 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
245 faimdprintf(sess, 0, "proxyconnect: unable to connect to proxy\n");
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 */
263 if (write(fd, buf, i) < i) {
269 if (read(fd, buf, 2) < 2) {
275 if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
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) {
293 if (read(fd, buf, 2) < 2) {
298 if ((buf[0] != 0x01) || (buf[1] != 0x00)) {
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);
313 if (write(fd, buf, i) < i) {
318 if (read(fd, buf, 10) < 10) {
323 if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
329 } else { /* connecting directly */
330 struct sockaddr_in sa;
333 if (!(hp = gethostbyname(host))) {
334 *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR);
338 memset(&sa, 0, sizeof(struct sockaddr_in));
339 sa.sin_port = htons(port);
340 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
341 sa.sin_family = hp->h_addrtype;
343 fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
345 if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT)
346 fcntl(fd, F_SETFL, O_NONBLOCK); /* XXX save flags */
348 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
349 if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT) {
350 if ((errno == EINPROGRESS) || (errno == EINTR)) {
352 *statusret |= AIM_CONN_STATUS_INPROGRESS;
364 * aim_cloneconn - clone an aim_conn_t
365 * @sess: session containing parent
366 * @src: connection to clone
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.
373 * This function returns a pointer to the new aim_conn_t, or %NULL on
376 faim_internal struct aim_conn_t *aim_cloneconn(struct aim_session_t *sess,
377 struct aim_conn_t *src)
379 struct aim_conn_t *conn;
380 struct aim_rxcblist_t *cur;
382 if (!(conn = aim_conn_getnext(sess)))
385 faim_mutex_lock(&conn->active);
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;
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);
401 faim_mutex_unlock(&conn->active);
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)
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
417 * FIXME: Return errors in a more sane way.
420 faim_export struct aim_conn_t *aim_newconn(struct aim_session_t *sess,
421 int type, char *dest)
423 struct aim_conn_t *connstruct;
425 u_short port = FAIM_LOGIN_PORT;
429 if ((connstruct=aim_conn_getnext(sess))==NULL)
432 faim_mutex_lock(&connstruct->active);
434 connstruct->type = type;
436 if (!dest) { /* just allocate a struct */
438 connstruct->status = 0;
439 faim_mutex_unlock(&connstruct->active);
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.
448 * We put this here to catch every case.
452 for(i=0;i<(int)strlen(dest);i++) {
453 if (dest[i] == ':') {
454 port = atoi(&(dest[i+1]));
458 host = (char *)malloc(i+1);
459 strncpy(host, dest, i);
462 if ((ret = aim_proxyconnect(sess, host, port, &connstruct->status)) < 0) {
464 connstruct->status = (errno | AIM_CONN_STATUS_CONNERR);
466 faim_mutex_unlock(&connstruct->active);
469 connstruct->fd = ret;
471 faim_mutex_unlock(&connstruct->active);
479 * aim_conngetmaxfd - Return the highest valued file discriptor in session
480 * @sess: Session to search
482 * Returns the highest valued filed descriptor of all open
483 * connections in @sess.
486 faim_export int aim_conngetmaxfd(struct aim_session_t *sess)
489 struct aim_conn_t *cur;
491 faim_mutex_lock(&sess->connlistlock);
492 for (cur = sess->connlist; cur; cur = cur->next) {
496 faim_mutex_unlock(&sess->connlistlock);
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
506 * Searches @sess for the passed connection. Returns 1 if its present,
510 faim_export int aim_conn_in_sess(struct aim_session_t *sess, struct aim_conn_t *conn)
512 struct aim_conn_t *cur;
514 faim_mutex_lock(&sess->connlistlock);
515 for(cur = sess->connlist; cur; cur = cur->next)
517 faim_mutex_unlock(&sess->connlistlock);
520 faim_mutex_unlock(&sess->connlistlock);
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
530 * Waits for a socket with data or for timeout, whichever comes first.
533 * Return codes in *status:
534 * -1 error in select() (%NULL returned)
535 * 0 no events pending (%NULL returned)
536 * 1 outgoing data pending (%NULL returned)
537 * 2 incoming data pending (connection with pending data returned)
539 * XXX: we could probably stand to do a little courser locking here.
542 faim_export struct aim_conn_t *aim_select(struct aim_session_t *sess,
543 struct timeval *timeout, int *status)
545 struct aim_conn_t *cur;
548 int i, haveconnecting = 0;
550 faim_mutex_lock(&sess->connlistlock);
551 if (sess->connlist == NULL) {
552 faim_mutex_unlock(&sess->connlistlock);
556 faim_mutex_unlock(&sess->connlistlock);
562 faim_mutex_lock(&sess->connlistlock);
563 for (cur = sess->connlist; cur; cur = cur->next) {
565 /* don't let invalid/dead connections sit around */
567 faim_mutex_unlock(&sess->connlistlock);
569 } else if (cur->status & AIM_CONN_STATUS_INPROGRESS) {
570 FD_SET(cur->fd, &wfds);
573 FD_SET(cur->fd, &fds);
577 faim_mutex_unlock(&sess->connlistlock);
580 * If we have data waiting to be sent, return
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
590 * But its still better than having blocking connects.
593 if (!haveconnecting && (sess->queue_outgoing != NULL)) {
598 if ((i = select(maxfd+1, &fds, &wfds, NULL, timeout))>=1) {
599 faim_mutex_lock(&sess->connlistlock);
600 for (cur = sess->connlist; cur; cur = cur->next) {
601 if ((FD_ISSET(cur->fd, &fds)) ||
602 ((cur->status & AIM_CONN_STATUS_INPROGRESS) &&
603 FD_ISSET(cur->fd, &wfds))) {
605 faim_mutex_unlock(&sess->connlistlock);
606 return cur; /* XXX race condition here -- shouldnt unlock connlist */
609 *status = 0; /* shouldn't happen */
610 } else if ((i == -1) && (errno == EINTR)) /* treat interrupts as a timeout */
613 *status = i; /* can be 0 or -1 */
615 faim_mutex_unlock(&sess->connlistlock);
617 return NULL; /* no waiting or error, return */
621 * aim_conn_isready - Test if a connection is marked ready
622 * @conn: Connection to test
624 * Returns true if the connection is ready, false otherwise.
625 * Returns -1 if the connection is invalid.
627 * XXX: This is deprecated.
630 faim_export int aim_conn_isready(struct aim_conn_t *conn)
633 return (conn->status & 0x0001);
638 * aim_conn_setstatus - Set the status of a connection
640 * @status: New status
642 * @newstatus is %XOR'd with the previous value of the connection
643 * status and returned. Returns -1 if the connection is invalid.
645 * This isn't real useful.
648 faim_export int aim_conn_setstatus(struct aim_conn_t *conn, int status)
655 faim_mutex_lock(&conn->active);
656 val = conn->status ^= status;
657 faim_mutex_unlock(&conn->active);
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
667 * Causes @newval seconds to be spent between transmits on a connection.
669 * This is my lame attempt at overcoming not understanding the rate
672 * XXX: This should really be replaced with something that scales and
673 * backs off like the real rate limiting does.
676 faim_export int aim_conn_setlatency(struct aim_conn_t *conn, int newval)
681 faim_mutex_lock(&conn->active);
682 conn->forcedlatency = newval;
683 conn->lastactivity = 0; /* reset this just to make sure */
684 faim_mutex_unlock(&conn->active);
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
696 * Call this with your SOCKS5 proxy server parameters before
697 * the first call to aim_newconn(). If called with all %NULL
698 * args, it will clear out a previously set proxy.
700 * Set username and password to %NULL if not applicable.
703 faim_export void aim_setupproxy(struct aim_session_t *sess, char *server, char *username, char *password)
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));
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));
722 * aim_session_init - Initializes a session structure
723 * @sess: Session to initialize
724 * @flags: Flags to use. Any of %AIM_SESS_FLAGS %OR'd together.
725 * @debuglevel: Level of debugging output (zero is least)
727 * Sets up the initial values for a session.
730 faim_export void aim_session_init(struct aim_session_t *sess, unsigned long flags, int debuglevel)
735 memset(sess, 0, sizeof(struct aim_session_t));
737 sess->queue_outgoing = NULL;
738 sess->queue_incoming = NULL;
739 sess->pendingjoin = NULL;
740 sess->pendingjoinexchange = 0;
741 aim_initsnachash(sess);
742 sess->msgcookies = NULL;
743 sess->snac_nextid = 0x00000001;
747 sess->debugcb = NULL;
750 * Default to SNAC login unless XORLOGIN is explicitly set.
752 if (!(flags & AIM_SESS_FLAGS_XORLOGIN))
753 sess->flags |= AIM_SESS_FLAGS_SNACLOGIN;
754 sess->flags |= flags;
757 * This must always be set. Default to the queue-based
758 * version for back-compatibility.
760 aim_tx_setenqueue(sess, AIM_TX_QUEUED, NULL);
766 * aim_setdebuggingcb - Set the function to call when outputting debugging info
767 * @sess: Session to change
768 * @cb: Function to call
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.
775 faim_export int aim_setdebuggingcb(struct aim_session_t *sess, faim_debugging_callback_t cb)
787 * aim_conn_isconnecting - Determine if a connection is connecting
788 * @conn: Connection to examine
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).
795 faim_export int aim_conn_isconnecting(struct aim_conn_t *conn)
799 return (conn->status & AIM_CONN_STATUS_INPROGRESS)?1:0;
802 faim_export int aim_conn_completeconnect(struct aim_session_t *sess, struct aim_conn_t *conn)
806 int res, error = ETIMEDOUT;
807 rxcallback_t userfunc;
809 if (!conn || (conn->fd == -1))
812 if (!(conn->status & AIM_CONN_STATUS_INPROGRESS))
816 FD_SET(conn->fd, &fds);
818 FD_SET(conn->fd, &wfds);
822 if ((res = select(conn->fd+1, &fds, &wfds, NULL, &tv)) == -1) {
824 aim_conn_close(conn);
827 } else if (res == 0) {
828 faimdprintf(sess, 0, "aim_conn_completeconnect: false alarm on %d\n", conn->fd);
829 return 0; /* hasn't really completed yet... */
832 if (FD_ISSET(conn->fd, &fds) || FD_ISSET(conn->fd, &wfds)) {
833 int len = sizeof(error);
835 if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
840 aim_conn_close(conn);
845 fcntl(conn->fd, F_SETFL, 0); /* XXX should restore original flags */
847 conn->status &= ~AIM_CONN_STATUS_INPROGRESS;
849 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNCOMPLETE)))
850 userfunc(sess, NULL, conn);
852 /* Flush out the queues if there was something waiting for this conn */
853 aim_tx_flushqueue(sess);
861 * Closes -ALL- open connections.
864 faim_export int aim_logoff(struct aim_session_t *sess)
867 aim_connrst(sess); /* in case we want to connect again */