5 * Does all this gloriously nifty connection handling stuff...
13 #include <sys/socket.h>
14 #include <netinet/in.h>
18 * aim_connrst - Clears out connection list, killing remaining connections.
19 * @sess: Session to be cleared
21 * Clears out the connection list and kills any connections left.
24 faim_internal void aim_connrst(struct aim_session_t *sess)
26 faim_mutex_init(&sess->connlistlock);
28 struct aim_conn_t *cur = sess->connlist, *tmp;
37 sess->connlist = NULL;
42 * aim_conn_init - Reset a connection to default values.
43 * @deadconn: Connection to be reset
45 * Initializes and/or resets a connection structure.
48 static void aim_conn_init(struct aim_conn_t *deadconn)
54 deadconn->subtype = -1;
57 deadconn->lastactivity = 0;
58 deadconn->forcedlatency = 0;
59 deadconn->handlerlist = NULL;
60 deadconn->priv = NULL;
61 faim_mutex_init(&deadconn->active);
62 faim_mutex_init(&deadconn->seqnum_lock);
68 * aim_conn_getnext - Gets a new connection structure.
71 * Allocate a new empty connection structure.
74 faim_internal struct aim_conn_t *aim_conn_getnext(struct aim_session_t *sess)
76 struct aim_conn_t *newconn, *cur;
78 if (!(newconn = malloc(sizeof(struct aim_conn_t))))
81 memset(newconn, 0, sizeof(struct aim_conn_t));
82 aim_conn_init(newconn);
85 faim_mutex_lock(&sess->connlistlock);
86 if (sess->connlist == NULL)
87 sess->connlist = newconn;
89 for (cur = sess->connlist; cur->next; cur = cur->next)
93 faim_mutex_unlock(&sess->connlistlock);
99 * aim_conn_kill - Close and free a connection.
100 * @sess: Session for the connection
101 * @deadconn: Connection to be freed
103 * Close, clear, and free a connection structure.
106 faim_export void aim_conn_kill(struct aim_session_t *sess, struct aim_conn_t **deadconn)
108 struct aim_conn_t *cur;
110 if (!deadconn || !*deadconn)
113 faim_mutex_lock(&sess->connlistlock);
114 if (sess->connlist == NULL)
116 else if (sess->connlist->next == NULL) {
117 if (sess->connlist == *deadconn)
118 sess->connlist = NULL;
120 cur = sess->connlist;
122 if (cur->next == *deadconn) {
123 cur->next = cur->next->next;
129 faim_mutex_unlock(&sess->connlistlock);
131 /* XXX: do we need this for txqueue too? */
132 aim_rxqueue_cleanbyconn(sess, *deadconn);
134 if ((*deadconn)->fd != -1)
135 aim_conn_close(*deadconn);
136 if ((*deadconn)->priv)
137 free((*deadconn)->priv);
145 * aim_conn_close - Close a connection
146 * @deadconn: Connection to close
148 * Close (but not free) a connection.
150 * This leaves everything untouched except for clearing the
151 * handler list and setting the fd to -1 (used to recognize
155 faim_export void aim_conn_close(struct aim_conn_t *deadconn)
158 faim_mutex_destroy(&deadconn->active);
159 faim_mutex_destroy(&deadconn->seqnum_lock);
160 if (deadconn->fd >= 3)
163 if (deadconn->handlerlist)
164 aim_clearhandlers(deadconn);
170 * aim_getconn_type - Find a connection of a specific type
171 * @sess: Session to search
172 * @type: Type of connection to look for
174 * Searches for a connection of the specified type in the
175 * specified session. Returns the first connection of that
179 faim_internal struct aim_conn_t *aim_getconn_type(struct aim_session_t *sess,
182 struct aim_conn_t *cur;
184 faim_mutex_lock(&sess->connlistlock);
185 for (cur = sess->connlist; cur; cur = cur->next) {
186 if ((cur->type == type) && !(cur->status & AIM_CONN_STATUS_INPROGRESS))
189 faim_mutex_unlock(&sess->connlistlock);
194 * aim_proxyconnect - An extrememly quick and dirty SOCKS5 interface.
195 * @sess: Session to connect
196 * @host: Host to connect to
197 * @port: Port to connect to
198 * @statusret: Return value of the connection
200 * Attempts to connect to the specified host via the configured
201 * proxy settings, if present. If no proxy is configured for
202 * this session, the connection is done directly.
205 static int aim_proxyconnect(struct aim_session_t *sess,
206 char *host, unsigned short port,
211 if (strlen(sess->socksproxy.server)) { /* connecting via proxy */
213 unsigned char buf[512];
214 struct sockaddr_in sa;
217 unsigned short proxyport = 1080;
219 for(i=0;i<(int)strlen(sess->socksproxy.server);i++) {
220 if (sess->socksproxy.server[i] == ':') {
221 proxyport = atoi(&(sess->socksproxy.server[i+1]));
225 proxy = (char *)malloc(i+1);
226 strncpy(proxy, sess->socksproxy.server, i);
229 if (!(hp = gethostbyname(proxy))) {
230 printf("proxyconnect: unable to resolve proxy name\n");
231 *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR);
236 memset(&sa.sin_zero, 0, 8);
237 sa.sin_port = htons(proxyport);
238 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
239 sa.sin_family = hp->h_addrtype;
241 fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
242 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
243 printf("proxyconnect: unable to connect to proxy\n");
249 buf[0] = 0x05; /* SOCKS version 5 */
250 if (strlen(sess->socksproxy.username)) {
251 buf[1] = 0x02; /* two methods */
252 buf[2] = 0x00; /* no authentication */
253 buf[3] = 0x02; /* username/password authentication */
261 if (write(fd, buf, i) < i) {
267 if (read(fd, buf, 2) < 2) {
273 if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
279 /* check if we're doing username authentication */
280 if (buf[1] == 0x02) {
281 i = aimutil_put8(buf, 0x01); /* version 1 */
282 i += aimutil_put8(buf+i, strlen(sess->socksproxy.username));
283 i += aimutil_putstr(buf+i, sess->socksproxy.username, strlen(sess->socksproxy.username));
284 i += aimutil_put8(buf+i, strlen(sess->socksproxy.password));
285 i += aimutil_putstr(buf+i, sess->socksproxy.password, strlen(sess->socksproxy.password));
286 if (write(fd, buf, i) < i) {
291 if (read(fd, buf, 2) < 2) {
296 if ((buf[0] != 0x01) || (buf[1] != 0x00)) {
303 i = aimutil_put8(buf, 0x05);
304 i += aimutil_put8(buf+i, 0x01); /* CONNECT */
305 i += aimutil_put8(buf+i, 0x00); /* reserved */
306 i += aimutil_put8(buf+i, 0x03); /* address type: host name */
307 i += aimutil_put8(buf+i, strlen(host));
308 i += aimutil_putstr(buf+i, host, strlen(host));
309 i += aimutil_put16(buf+i, port);
311 if (write(fd, buf, i) < i) {
316 if (read(fd, buf, 10) < 10) {
321 if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
327 } else { /* connecting directly */
328 struct sockaddr_in sa;
331 if (!(hp = gethostbyname(host))) {
332 *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR);
336 memset(&sa, 0, sizeof(struct sockaddr_in));
337 sa.sin_port = htons(port);
338 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
339 sa.sin_family = hp->h_addrtype;
341 fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
343 if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT)
344 fcntl(fd, F_SETFL, O_NONBLOCK); /* XXX save flags */
346 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
347 if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT) {
348 if ((errno == EINPROGRESS) || (errno == EINTR)) {
350 *statusret |= AIM_CONN_STATUS_INPROGRESS;
362 * aim_newconn - Open a new connection
363 * @sess: Session to create connection in
364 * @type: Type of connection to create
365 * @dest: Host to connect to (in "host:port" syntax)
367 * Opens a new connection to the specified dest host of specified
368 * type, using the proxy settings if available. If @host is %NULL,
369 * the connection is allocated and returned, but no connection
372 * FIXME: Return errors in a more sane way.
375 faim_export struct aim_conn_t *aim_newconn(struct aim_session_t *sess,
376 int type, char *dest)
378 struct aim_conn_t *connstruct;
380 u_short port = FAIM_LOGIN_PORT;
384 if ((connstruct=aim_conn_getnext(sess))==NULL)
387 faim_mutex_lock(&connstruct->active);
389 connstruct->type = type;
391 if (!dest) { /* just allocate a struct */
393 connstruct->status = 0;
394 faim_mutex_unlock(&connstruct->active);
399 * As of 23 Jul 1999, AOL now sends the port number, preceded by a
400 * colon, in the BOS redirect. This fatally breaks all previous
401 * libfaims. Bad, bad AOL.
403 * We put this here to catch every case.
407 for(i=0;i<(int)strlen(dest);i++) {
408 if (dest[i] == ':') {
409 port = atoi(&(dest[i+1]));
413 host = (char *)malloc(i+1);
414 strncpy(host, dest, i);
417 if ((ret = aim_proxyconnect(sess, host, port, &connstruct->status)) < 0) {
419 connstruct->status = (errno | AIM_CONN_STATUS_CONNERR);
421 faim_mutex_unlock(&connstruct->active);
424 connstruct->fd = ret;
426 faim_mutex_unlock(&connstruct->active);
434 * aim_conngetmaxfd - Return the highest valued file discriptor in session
435 * @sess: Session to search
437 * Returns the highest valued filed descriptor of all open
438 * connections in @sess.
441 faim_export int aim_conngetmaxfd(struct aim_session_t *sess)
444 struct aim_conn_t *cur;
446 faim_mutex_lock(&sess->connlistlock);
447 for (cur = sess->connlist; cur; cur = cur->next) {
451 faim_mutex_unlock(&sess->connlistlock);
457 * aim_countconn - Return the number of open connections in the session
458 * @sess: Session to look at
460 * Returns the number of number connections in @sess.
463 static int aim_countconn(struct aim_session_t *sess)
466 struct aim_conn_t *cur;
468 faim_mutex_lock(&sess->connlistlock);
469 for (cur = sess->connlist; cur; cur = cur->next)
471 faim_mutex_unlock(&sess->connlistlock);
477 * aim_conn_in_sess - Predicate to test the precense of a connection in a sess
478 * @sess: Session to look in
479 * @conn: Connection to look for
481 * Searches @sess for the passed connection. Returns 1 if its present,
485 faim_export int aim_conn_in_sess(struct aim_session_t *sess, struct aim_conn_t *conn)
487 struct aim_conn_t *cur;
489 faim_mutex_lock(&sess->connlistlock);
490 for(cur = sess->connlist; cur; cur = cur->next)
492 faim_mutex_unlock(&sess->connlistlock);
495 faim_mutex_unlock(&sess->connlistlock);
500 * aim_select - Wait for a socket with data or timeout
501 * @sess: Session to wait on
502 * @timeout: How long to wait
503 * @status: Return status
505 * Waits for a socket with data or for timeout, whichever comes first.
508 * Return codes in *status:
509 * -1 error in select() (%NULL returned)
510 * 0 no events pending (%NULL returned)
511 * 1 outgoing data pending (%NULL returned)
512 * 2 incoming data pending (connection with pending data returned)
514 * XXX: we could probably stand to do a little courser locking here.
517 faim_export struct aim_conn_t *aim_select(struct aim_session_t *sess,
518 struct timeval *timeout, int *status)
520 struct aim_conn_t *cur;
523 int i, haveconnecting = 0;
525 faim_mutex_lock(&sess->connlistlock);
526 if (sess->connlist == NULL) {
527 faim_mutex_unlock(&sess->connlistlock);
531 faim_mutex_unlock(&sess->connlistlock);
537 faim_mutex_lock(&sess->connlistlock);
538 for (cur = sess->connlist; cur; cur = cur->next) {
540 /* don't let invalid/dead connections sit around */
542 faim_mutex_unlock(&sess->connlistlock);
544 } else if (cur->status & AIM_CONN_STATUS_INPROGRESS) {
545 FD_SET(cur->fd, &wfds);
548 FD_SET(cur->fd, &fds);
552 faim_mutex_unlock(&sess->connlistlock);
555 * If we have data waiting to be sent, return
557 * We have to not do this if theres at least one
558 * connection thats still connecting, since that connection
559 * may have queued data and this return would prevent
560 * the connection from ever completing! This is a major
561 * inadequacy of the libfaim way of doing things. It means
562 * that nothing can transmit as long as there's connecting
565 * But its still better than having blocking connects.
568 if (!haveconnecting && (sess->queue_outgoing != NULL)) {
573 if ((i = select(maxfd+1, &fds, &wfds, NULL, timeout))>=1) {
574 faim_mutex_lock(&sess->connlistlock);
575 for (cur = sess->connlist; cur; cur = cur->next) {
576 if ((FD_ISSET(cur->fd, &fds)) ||
577 ((cur->status & AIM_CONN_STATUS_INPROGRESS) &&
578 FD_ISSET(cur->fd, &wfds))) {
580 faim_mutex_unlock(&sess->connlistlock);
581 return cur; /* XXX race condition here -- shouldnt unlock connlist */
584 *status = 0; /* shouldn't happen */
585 } else if ((i == -1) && (errno == EINTR)) /* treat interrupts as a timeout */
588 *status = i; /* can be 0 or -1 */
590 faim_mutex_unlock(&sess->connlistlock);
591 return NULL; /* no waiting or error, return */
595 * aim_conn_isready - Test if a connection is marked ready
596 * @conn: Connection to test
598 * Returns true if the connection is ready, false otherwise.
599 * Returns -1 if the connection is invalid.
601 * XXX: This is deprecated.
604 faim_export int aim_conn_isready(struct aim_conn_t *conn)
607 return (conn->status & 0x0001);
612 * aim_conn_setstatus - Set the status of a connection
614 * @status: New status
616 * @newstatus is %XOR'd with the previous value of the connection
617 * status and returned. Returns -1 if the connection is invalid.
619 * This isn't real useful.
622 faim_export int aim_conn_setstatus(struct aim_conn_t *conn, int status)
629 faim_mutex_lock(&conn->active);
630 val = conn->status ^= status;
631 faim_mutex_unlock(&conn->active);
636 * aim_conn_setlatency - Set a forced latency value for connection
637 * @conn: Conn to set latency for
638 * @newval: Number of seconds to force between transmits
640 * Causes @newval seconds to be spent between transmits on a connection.
642 * This is my lame attempt at overcoming not understanding the rate
645 * XXX: This should really be replaced with something that scales and
646 * backs off like the real rate limiting does.
649 faim_export int aim_conn_setlatency(struct aim_conn_t *conn, int newval)
654 faim_mutex_lock(&conn->active);
655 conn->forcedlatency = newval;
656 conn->lastactivity = 0; /* reset this just to make sure */
657 faim_mutex_unlock(&conn->active);
663 * aim_setupproxy - Configure a proxy for this session
664 * @sess: Session to set proxy for
665 * @server: SOCKS server
666 * @username: SOCKS username
667 * @password: SOCKS password
669 * Call this with your SOCKS5 proxy server parameters before
670 * the first call to aim_newconn(). If called with all %NULL
671 * args, it will clear out a previously set proxy.
673 * Set username and password to %NULL if not applicable.
676 faim_export void aim_setupproxy(struct aim_session_t *sess, char *server, char *username, char *password)
678 /* clear out the proxy info */
679 if (!server || !strlen(server)) {
680 memset(sess->socksproxy.server, 0, sizeof(sess->socksproxy.server));
681 memset(sess->socksproxy.username, 0, sizeof(sess->socksproxy.username));
682 memset(sess->socksproxy.password, 0, sizeof(sess->socksproxy.password));
686 strncpy(sess->socksproxy.server, server, sizeof(sess->socksproxy.server));
687 if (username && strlen(username))
688 strncpy(sess->socksproxy.username, username, sizeof(sess->socksproxy.username));
689 if (password && strlen(password))
690 strncpy(sess->socksproxy.password, password, sizeof(sess->socksproxy.password));
695 * aim_session_init - Initializes a session structure
696 * @sess: Session to initialize
697 * @flags: Flags to use. Any of %AIM_SESS_FLAGS %OR'd together.
699 * Sets up the initial values for a session.
702 faim_export void aim_session_init(struct aim_session_t *sess, unsigned long flags)
707 memset(sess, 0, sizeof(struct aim_session_t));
709 sess->queue_outgoing = NULL;
710 sess->queue_incoming = NULL;
711 sess->pendingjoin = NULL;
712 sess->pendingjoinexchange = 0;
713 aim_initsnachash(sess);
714 sess->msgcookies = NULL;
715 sess->snac_nextid = 0x00000001;
720 * Default to SNAC login unless XORLOGIN is explicitly set.
722 if (!(flags & AIM_SESS_FLAGS_XORLOGIN))
723 sess->flags |= AIM_SESS_FLAGS_SNACLOGIN;
724 sess->flags |= flags;
727 * This must always be set. Default to the queue-based
728 * version for back-compatibility.
730 sess->tx_enqueue = &aim_tx_enqueue__queuebased;
736 * aim_conn_isconnecting - Determine if a connection is connecting
737 * @conn: Connection to examine
739 * Returns nonzero if the connection is in the process of
740 * connecting (or if it just completed and aim_conn_completeconnect()
741 * has yet to be called on it).
744 faim_export int aim_conn_isconnecting(struct aim_conn_t *conn)
748 return (conn->status & AIM_CONN_STATUS_INPROGRESS)?1:0;
751 faim_export int aim_conn_completeconnect(struct aim_session_t *sess, struct aim_conn_t *conn)
755 int res, error = ETIMEDOUT;
756 rxcallback_t userfunc;
758 if (!conn || (conn->fd == -1))
761 if (!(conn->status & AIM_CONN_STATUS_INPROGRESS))
765 FD_SET(conn->fd, &fds);
767 FD_SET(conn->fd, &wfds);
771 if ((res = select(conn->fd+1, &fds, &wfds, NULL, &tv)) == -1) {
773 aim_conn_close(conn);
776 } else if (res == 0) {
777 printf("faim: aim_conn_completeconnect: false alarm on %d\n", conn->fd);
778 return 0; /* hasn't really completed yet... */
781 if (FD_ISSET(conn->fd, &fds) || FD_ISSET(conn->fd, &wfds)) {
782 int len = sizeof(error);
784 if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
789 aim_conn_close(conn);
794 fcntl(conn->fd, F_SETFL, 0); /* XXX should restore original flags */
796 conn->status &= ~AIM_CONN_STATUS_INPROGRESS;
798 if ((userfunc = aim_callhandler(conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNCOMPLETE)))
799 userfunc(sess, NULL, conn);
801 /* Flush out the queues if there was something waiting for this conn */
802 aim_tx_flushqueue(sess);