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 static void aim_connrst(aim_session_t *sess)
28 faim_mutex_init(&sess->connlistlock);
31 aim_conn_t *cur = sess->connlist, *tmp;
41 sess->connlist = NULL;
47 * aim_conn_init - Reset a connection to default values.
48 * @deadconn: Connection to be reset
50 * Initializes and/or resets a connection structure.
53 static void aim_conn_init(aim_conn_t *deadconn)
60 deadconn->subtype = -1;
63 deadconn->lastactivity = 0;
64 deadconn->forcedlatency = 0;
65 deadconn->handlerlist = NULL;
66 deadconn->priv = NULL;
67 faim_mutex_init(&deadconn->active);
68 faim_mutex_init(&deadconn->seqnum_lock);
74 * aim_conn_getnext - Gets a new connection structure.
77 * Allocate a new empty connection structure.
80 static aim_conn_t *aim_conn_getnext(aim_session_t *sess)
82 aim_conn_t *newconn, *cur;
84 if (!(newconn = malloc(sizeof(aim_conn_t))))
86 memset(newconn, 0, sizeof(aim_conn_t));
88 aim_conn_init(newconn);
91 faim_mutex_lock(&sess->connlistlock);
93 sess->connlist = newconn;
95 for (cur = sess->connlist; cur->next; cur = cur->next)
99 faim_mutex_unlock(&sess->connlistlock);
105 * aim_conn_kill - Close and free a connection.
106 * @sess: Session for the connection
107 * @deadconn: Connection to be freed
109 * Close, clear, and free a connection structure. Should never be
110 * called from within libfaim.
113 faim_export void aim_conn_kill(aim_session_t *sess, aim_conn_t **deadconn)
117 if (!deadconn || !*deadconn)
120 aim_tx_cleanqueue(sess, *deadconn);
122 faim_mutex_lock(&sess->connlistlock);
123 if (sess->connlist == NULL)
125 else if (sess->connlist->next == NULL) {
126 if (sess->connlist == *deadconn)
127 sess->connlist = NULL;
129 cur = sess->connlist;
131 if (cur->next == *deadconn) {
132 cur->next = cur->next->next;
138 faim_mutex_unlock(&sess->connlistlock);
140 /* XXX: do we need this for txqueue too? */
141 aim_rxqueue_cleanbyconn(sess, *deadconn);
143 if ((*deadconn)->fd != -1)
144 aim_conn_close(*deadconn);
145 if ((*deadconn)->priv)
146 free((*deadconn)->priv);
154 * aim_conn_close - Close a connection
155 * @deadconn: Connection to close
157 * Close (but not free) a connection.
159 * This leaves everything untouched except for clearing the
160 * handler list and setting the fd to -1 (used to recognize
164 faim_export void aim_conn_close(aim_conn_t *deadconn)
167 faim_mutex_destroy(&deadconn->active);
168 faim_mutex_destroy(&deadconn->seqnum_lock);
169 if (deadconn->fd >= 3)
172 if (deadconn->handlerlist)
173 aim_clearhandlers(deadconn);
179 * aim_getconn_type - Find a connection of a specific type
180 * @sess: Session to search
181 * @type: Type of connection to look for
183 * Searches for a connection of the specified type in the
184 * specified session. Returns the first connection of that
188 faim_export aim_conn_t *aim_getconn_type(aim_session_t *sess, int type)
192 faim_mutex_lock(&sess->connlistlock);
193 for (cur = sess->connlist; cur; cur = cur->next) {
194 if ((cur->type == type) &&
195 !(cur->status & AIM_CONN_STATUS_INPROGRESS))
198 faim_mutex_unlock(&sess->connlistlock);
203 faim_export aim_conn_t *aim_getconn_type_all(aim_session_t *sess, int type)
207 faim_mutex_lock(&sess->connlistlock);
208 for (cur = sess->connlist; cur; cur = cur->next) {
209 if (cur->type == type)
212 faim_mutex_unlock(&sess->connlistlock);
217 /* If you pass -1 for the fd, you'll get what you ask for. Gibberish. */
218 faim_export aim_conn_t *aim_getconn_fd(aim_session_t *sess, int fd)
222 faim_mutex_lock(&sess->connlistlock);
223 for (cur = sess->connlist; cur; cur = cur->next) {
227 faim_mutex_unlock(&sess->connlistlock);
233 * aim_proxyconnect - An extrememly quick and dirty SOCKS5 interface.
234 * @sess: Session to connect
235 * @host: Host to connect to
236 * @port: Port to connect to
237 * @statusret: Return value of the connection
239 * Attempts to connect to the specified host via the configured
240 * proxy settings, if present. If no proxy is configured for
241 * this session, the connection is done directly.
243 * XXX this is really awful.
246 static int aim_proxyconnect(aim_session_t *sess, const char *host, fu16_t port, fu32_t *statusret)
250 if (strlen(sess->socksproxy.server)) { /* connecting via proxy */
252 unsigned char buf[512];
253 struct sockaddr_in sa;
256 unsigned short proxyport = 1080;
258 for(i=0;i<(int)strlen(sess->socksproxy.server);i++) {
259 if (sess->socksproxy.server[i] == ':') {
260 proxyport = atoi(&(sess->socksproxy.server[i+1]));
265 proxy = (char *)malloc(i+1);
266 strncpy(proxy, sess->socksproxy.server, i);
269 if (!(hp = gethostbyname(proxy))) {
270 faimdprintf(sess, 0, "proxyconnect: unable to resolve proxy name\n");
271 *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR);
276 memset(&sa.sin_zero, 0, 8);
277 sa.sin_port = htons(proxyport);
278 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
279 sa.sin_family = hp->h_addrtype;
281 fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
282 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
283 faimdprintf(sess, 0, "proxyconnect: unable to connect to proxy\n");
289 buf[0] = 0x05; /* SOCKS version 5 */
290 if (strlen(sess->socksproxy.username)) {
291 buf[1] = 0x02; /* two methods */
292 buf[2] = 0x00; /* no authentication */
293 buf[3] = 0x02; /* username/password authentication */
301 if (write(fd, buf, i) < i) {
307 if (read(fd, buf, 2) < 2) {
313 if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
319 /* check if we're doing username authentication */
320 if (buf[1] == 0x02) {
321 i = aimutil_put8(buf, 0x01); /* version 1 */
322 i += aimutil_put8(buf+i, strlen(sess->socksproxy.username));
323 i += aimutil_putstr(buf+i, sess->socksproxy.username, strlen(sess->socksproxy.username));
324 i += aimutil_put8(buf+i, strlen(sess->socksproxy.password));
325 i += aimutil_putstr(buf+i, sess->socksproxy.password, strlen(sess->socksproxy.password));
326 if (write(fd, buf, i) < i) {
331 if (read(fd, buf, 2) < 2) {
336 if ((buf[0] != 0x01) || (buf[1] != 0x00)) {
343 i = aimutil_put8(buf, 0x05);
344 i += aimutil_put8(buf+i, 0x01); /* CONNECT */
345 i += aimutil_put8(buf+i, 0x00); /* reserved */
346 i += aimutil_put8(buf+i, 0x03); /* address type: host name */
347 i += aimutil_put8(buf+i, strlen(host));
348 i += aimutil_putstr(buf+i, host, strlen(host));
349 i += aimutil_put16(buf+i, port);
351 if (write(fd, buf, i) < i) {
356 if (read(fd, buf, 10) < 10) {
361 if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
367 } else { /* connecting directly */
368 struct sockaddr_in sa;
371 if (!(hp = gethostbyname(host))) {
372 *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR);
376 memset(&sa, 0, sizeof(struct sockaddr_in));
377 sa.sin_port = htons(port);
378 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
379 sa.sin_family = hp->h_addrtype;
381 fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
383 if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT)
384 fcntl(fd, F_SETFL, O_NONBLOCK); /* XXX save flags */
386 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
387 if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT) {
388 if ((errno == EINPROGRESS) || (errno == EINTR)) {
390 *statusret |= AIM_CONN_STATUS_INPROGRESS;
402 * aim_cloneconn - clone an aim_conn_t
403 * @sess: session containing parent
404 * @src: connection to clone
406 * A new connection is allocated, and the values are filled in
407 * appropriately. Note that this function sets the new connnection's
408 * ->priv pointer to be equal to that of its parent: only the pointer
409 * is copied, not the data it points to.
411 * This function returns a pointer to the new aim_conn_t, or %NULL on
414 faim_internal aim_conn_t *aim_cloneconn(aim_session_t *sess, aim_conn_t *src)
418 if (!(conn = aim_conn_getnext(sess)))
421 faim_mutex_lock(&conn->active);
424 conn->type = src->type;
425 conn->subtype = src->subtype;
426 conn->seqnum = src->seqnum;
427 conn->priv = src->priv;
428 conn->lastactivity = src->lastactivity;
429 conn->forcedlatency = src->forcedlatency;
430 conn->sessv = src->sessv;
431 aim_clonehandlers(sess, conn, src);
433 faim_mutex_unlock(&conn->active);
439 * aim_newconn - Open a new connection
440 * @sess: Session to create connection in
441 * @type: Type of connection to create
442 * @dest: Host to connect to (in "host:port" syntax)
444 * Opens a new connection to the specified dest host of specified
445 * type, using the proxy settings if available. If @host is %NULL,
446 * the connection is allocated and returned, but no connection
449 * FIXME: Return errors in a more sane way.
452 faim_export aim_conn_t *aim_newconn(aim_session_t *sess, int type, const char *dest)
454 aim_conn_t *connstruct;
455 fu16_t port = FAIM_LOGIN_PORT;
459 if (!(connstruct = aim_conn_getnext(sess)))
462 faim_mutex_lock(&connstruct->active);
464 connstruct->sessv = (void *)sess;
465 connstruct->type = type;
467 if (!dest) { /* just allocate a struct */
469 connstruct->status = 0;
470 faim_mutex_unlock(&connstruct->active);
475 * As of 23 Jul 1999, AOL now sends the port number, preceded by a
476 * colon, in the BOS redirect. This fatally breaks all previous
477 * libfaims. Bad, bad AOL.
479 * We put this here to catch every case.
483 for(i = 0; i < (int)strlen(dest); i++) {
484 if (dest[i] == ':') {
485 port = atoi(&(dest[i+1]));
490 host = (char *)malloc(i+1);
491 strncpy(host, dest, i);
494 if ((ret = aim_proxyconnect(sess, host, port, &connstruct->status)) < 0) {
496 connstruct->status = (errno | AIM_CONN_STATUS_CONNERR);
498 faim_mutex_unlock(&connstruct->active);
501 connstruct->fd = ret;
503 faim_mutex_unlock(&connstruct->active);
511 * aim_conngetmaxfd - Return the highest valued file discriptor in session
512 * @sess: Session to search
514 * Returns the highest valued filed descriptor of all open
515 * connections in @sess.
518 faim_export int aim_conngetmaxfd(aim_session_t *sess)
523 faim_mutex_lock(&sess->connlistlock);
524 for (cur = sess->connlist, j = 0; cur; cur = cur->next) {
528 faim_mutex_unlock(&sess->connlistlock);
534 * aim_conn_in_sess - Predicate to test the precense of a connection in a sess
535 * @sess: Session to look in
536 * @conn: Connection to look for
538 * Searches @sess for the passed connection. Returns 1 if its present,
542 faim_export int aim_conn_in_sess(aim_session_t *sess, aim_conn_t *conn)
546 faim_mutex_lock(&sess->connlistlock);
547 for (cur = sess->connlist; cur; cur = cur->next) {
549 faim_mutex_unlock(&sess->connlistlock);
553 faim_mutex_unlock(&sess->connlistlock);
559 * aim_select - Wait for a socket with data or timeout
560 * @sess: Session to wait on
561 * @timeout: How long to wait
562 * @status: Return status
564 * Waits for a socket with data or for timeout, whichever comes first.
567 * Return codes in *status:
568 * -1 error in select() (%NULL returned)
569 * 0 no events pending (%NULL returned)
570 * 1 outgoing data pending (%NULL returned)
571 * 2 incoming data pending (connection with pending data returned)
573 * XXX: we could probably stand to do a little courser locking here.
576 faim_export aim_conn_t *aim_select(aim_session_t *sess, struct timeval *timeout, int *status)
580 int maxfd, i, haveconnecting = 0;
582 faim_mutex_lock(&sess->connlistlock);
583 if (!sess->connlist) {
584 faim_mutex_unlock(&sess->connlistlock);
588 faim_mutex_unlock(&sess->connlistlock);
593 faim_mutex_lock(&sess->connlistlock);
594 for (cur = sess->connlist, maxfd = 0; cur; cur = cur->next) {
596 /* don't let invalid/dead connections sit around */
598 faim_mutex_unlock(&sess->connlistlock);
600 } else if (cur->status & AIM_CONN_STATUS_INPROGRESS) {
601 FD_SET(cur->fd, &wfds);
605 FD_SET(cur->fd, &fds);
609 faim_mutex_unlock(&sess->connlistlock);
612 * If we have data waiting to be sent, return
614 * We have to not do this if theres at least one
615 * connection thats still connecting, since that connection
616 * may have queued data and this return would prevent
617 * the connection from ever completing! This is a major
618 * inadequacy of the libfaim way of doing things. It means
619 * that nothing can transmit as long as there's connecting
622 * But its still better than having blocking connects.
625 if (!haveconnecting && sess->queue_outgoing) {
630 if ((i = select(maxfd+1, &fds, &wfds, NULL, timeout))>=1) {
631 faim_mutex_lock(&sess->connlistlock);
632 for (cur = sess->connlist; cur; cur = cur->next) {
633 if ((FD_ISSET(cur->fd, &fds)) ||
634 ((cur->status & AIM_CONN_STATUS_INPROGRESS) &&
635 FD_ISSET(cur->fd, &wfds))) {
637 faim_mutex_unlock(&sess->connlistlock);
638 return cur; /* XXX race condition here -- shouldnt unlock connlist */
641 *status = 0; /* shouldn't happen */
642 } else if ((i == -1) && (errno == EINTR)) /* treat interrupts as a timeout */
645 *status = i; /* can be 0 or -1 */
647 faim_mutex_unlock(&sess->connlistlock);
649 return NULL; /* no waiting or error, return */
653 * aim_conn_setlatency - Set a forced latency value for connection
654 * @conn: Conn to set latency for
655 * @newval: Number of seconds to force between transmits
657 * Causes @newval seconds to be spent between transmits on a connection.
659 * This is my lame attempt at overcoming not understanding the rate
662 * XXX: This should really be replaced with something that scales and
663 * backs off like the real rate limiting does.
666 faim_export int aim_conn_setlatency(aim_conn_t *conn, int newval)
672 faim_mutex_lock(&conn->active);
673 conn->forcedlatency = newval;
674 conn->lastactivity = 0; /* reset this just to make sure */
675 faim_mutex_unlock(&conn->active);
681 * aim_setupproxy - Configure a proxy for this session
682 * @sess: Session to set proxy for
683 * @server: SOCKS server
684 * @username: SOCKS username
685 * @password: SOCKS password
687 * Call this with your SOCKS5 proxy server parameters before
688 * the first call to aim_newconn(). If called with all %NULL
689 * args, it will clear out a previously set proxy.
691 * Set username and password to %NULL if not applicable.
694 faim_export void aim_setupproxy(aim_session_t *sess, const char *server, const char *username, const char *password)
696 /* clear out the proxy info */
697 if (!server || !strlen(server)) {
698 memset(sess->socksproxy.server, 0, sizeof(sess->socksproxy.server));
699 memset(sess->socksproxy.username, 0, sizeof(sess->socksproxy.username));
700 memset(sess->socksproxy.password, 0, sizeof(sess->socksproxy.password));
704 strncpy(sess->socksproxy.server, server, sizeof(sess->socksproxy.server));
705 if (username && strlen(username))
706 strncpy(sess->socksproxy.username, username, sizeof(sess->socksproxy.username));
707 if (password && strlen(password))
708 strncpy(sess->socksproxy.password, password, sizeof(sess->socksproxy.password));
713 static void defaultdebugcb(aim_session_t *sess, int level, const char *format, va_list va)
716 vfprintf(stderr, format, va);
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(aim_session_t *sess, fu32_t flags, int debuglevel)
736 memset(sess, 0, sizeof(aim_session_t));
738 sess->queue_outgoing = NULL;
739 sess->queue_incoming = NULL;
740 sess->pendingjoin = NULL;
741 sess->pendingjoinexchange = 0;
742 aim_initsnachash(sess);
743 sess->msgcookies = NULL;
744 sess->snacid_next = 0x00000001;
747 sess->debug = debuglevel;
748 sess->debugcb = defaultdebugcb;
750 sess->modlistv = NULL;
753 * Default to SNAC login unless XORLOGIN is explicitly set.
755 if (!(flags & AIM_SESS_FLAGS_XORLOGIN))
756 sess->flags |= AIM_SESS_FLAGS_SNACLOGIN;
757 sess->flags |= flags;
760 * This must always be set. Default to the queue-based
761 * version for back-compatibility.
763 aim_tx_setenqueue(sess, AIM_TX_QUEUED, NULL);
767 * Register all the modules for this session...
769 aim__registermodule(sess, misc_modfirst); /* load the catch-all first */
770 aim__registermodule(sess, buddylist_modfirst);
771 aim__registermodule(sess, admin_modfirst);
772 aim__registermodule(sess, bos_modfirst);
773 aim__registermodule(sess, search_modfirst);
774 aim__registermodule(sess, stats_modfirst);
775 aim__registermodule(sess, auth_modfirst);
776 aim__registermodule(sess, msg_modfirst);
777 aim__registermodule(sess, chatnav_modfirst);
778 aim__registermodule(sess, chat_modfirst);
779 aim__registermodule(sess, locate_modfirst);
780 aim__registermodule(sess, general_modfirst);
786 * aim_session_kill - Deallocate a session
787 * @sess: Session to kill
790 faim_export void aim_session_kill(aim_session_t *sess)
795 aim__shutdownmodules(sess);
801 * aim_setdebuggingcb - Set the function to call when outputting debugging info
802 * @sess: Session to change
803 * @cb: Function to call
805 * The function specified is called whenever faimdprintf() is used within
806 * libfaim, and the session's debugging level is greater tha nor equal to
807 * the value faimdprintf was called with.
810 faim_export int aim_setdebuggingcb(aim_session_t *sess, faim_debugging_callback_t cb)
822 * aim_conn_isconnecting - Determine if a connection is connecting
823 * @conn: Connection to examine
825 * Returns nonzero if the connection is in the process of
826 * connecting (or if it just completed and aim_conn_completeconnect()
827 * has yet to be called on it).
830 faim_export int aim_conn_isconnecting(aim_conn_t *conn)
836 return !!(conn->status & AIM_CONN_STATUS_INPROGRESS);
840 * XXX this is nearly as ugly as proxyconnect().
842 faim_export int aim_conn_completeconnect(aim_session_t *sess, aim_conn_t *conn)
846 int res, error = ETIMEDOUT;
847 aim_rxcallback_t userfunc;
849 if (!conn || (conn->fd == -1))
852 if (!(conn->status & AIM_CONN_STATUS_INPROGRESS))
856 FD_SET(conn->fd, &fds);
858 FD_SET(conn->fd, &wfds);
862 if ((res = select(conn->fd+1, &fds, &wfds, NULL, &tv)) == -1) {
864 aim_conn_close(conn);
867 } else if (res == 0) {
868 faimdprintf(sess, 0, "aim_conn_completeconnect: false alarm on %d\n", conn->fd);
869 return 0; /* hasn't really completed yet... */
872 if (FD_ISSET(conn->fd, &fds) || FD_ISSET(conn->fd, &wfds)) {
873 int len = sizeof(error);
875 if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
880 aim_conn_close(conn);
885 fcntl(conn->fd, F_SETFL, 0); /* XXX should restore original flags */
887 conn->status &= ~AIM_CONN_STATUS_INPROGRESS;
889 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNCOMPLETE)))
890 userfunc(sess, NULL, conn);
892 /* Flush out the queues if there was something waiting for this conn */
893 aim_tx_flushqueue(sess);
898 faim_export aim_session_t *aim_conn_getsess(aim_conn_t *conn)
904 return (aim_session_t *)conn->sessv;
910 * Closes -ALL- open connections.
913 faim_export int aim_logoff(aim_session_t *sess)
916 aim_connrst(sess); /* in case we want to connect again */