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.
107 faim_export void aim_conn_kill(struct aim_session_t *sess, struct aim_conn_t **deadconn)
109 struct aim_conn_t *cur;
111 if (!deadconn || !*deadconn)
114 faim_mutex_lock(&sess->connlistlock);
115 if (sess->connlist == NULL)
117 else if (sess->connlist->next == NULL) {
118 if (sess->connlist == *deadconn)
119 sess->connlist = NULL;
121 cur = sess->connlist;
123 if (cur->next == *deadconn) {
124 cur->next = cur->next->next;
130 faim_mutex_unlock(&sess->connlistlock);
132 /* XXX: do we need this for txqueue too? */
133 aim_rxqueue_cleanbyconn(sess, *deadconn);
135 if ((*deadconn)->fd != -1)
136 aim_conn_close(*deadconn);
137 if ((*deadconn)->priv)
138 free((*deadconn)->priv);
146 * aim_conn_close - Close a connection
147 * @deadconn: Connection to close
149 * Close (but not free) a connection.
151 * This leaves everything untouched except for clearing the
152 * handler list and setting the fd to -1 (used to recognize
156 faim_export void aim_conn_close(struct aim_conn_t *deadconn)
159 faim_mutex_destroy(&deadconn->active);
160 faim_mutex_destroy(&deadconn->seqnum_lock);
161 if (deadconn->fd >= 3)
164 if (deadconn->handlerlist)
165 aim_clearhandlers(deadconn);
171 * aim_getconn_type - Find a connection of a specific type
172 * @sess: Session to search
173 * @type: Type of connection to look for
175 * Searches for a connection of the specified type in the
176 * specified session. Returns the first connection of that
180 faim_export struct aim_conn_t *aim_getconn_type(struct aim_session_t *sess,
183 struct aim_conn_t *cur;
185 faim_mutex_lock(&sess->connlistlock);
186 for (cur = sess->connlist; cur; cur = cur->next) {
187 if ((cur->type == type) && !(cur->status & AIM_CONN_STATUS_INPROGRESS))
190 faim_mutex_unlock(&sess->connlistlock);
195 * aim_proxyconnect - An extrememly quick and dirty SOCKS5 interface.
196 * @sess: Session to connect
197 * @host: Host to connect to
198 * @port: Port to connect to
199 * @statusret: Return value of the connection
201 * Attempts to connect to the specified host via the configured
202 * proxy settings, if present. If no proxy is configured for
203 * this session, the connection is done directly.
206 static int aim_proxyconnect(struct aim_session_t *sess,
207 char *host, unsigned short port,
212 if (strlen(sess->socksproxy.server)) { /* connecting via proxy */
214 unsigned char buf[512];
215 struct sockaddr_in sa;
218 unsigned short proxyport = 1080;
220 for(i=0;i<(int)strlen(sess->socksproxy.server);i++) {
221 if (sess->socksproxy.server[i] == ':') {
222 proxyport = atoi(&(sess->socksproxy.server[i+1]));
226 proxy = (char *)malloc(i+1);
227 strncpy(proxy, sess->socksproxy.server, i);
230 if (!(hp = gethostbyname(proxy))) {
231 printf("proxyconnect: unable to resolve proxy name\n");
232 *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR);
237 memset(&sa.sin_zero, 0, 8);
238 sa.sin_port = htons(proxyport);
239 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
240 sa.sin_family = hp->h_addrtype;
242 fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
243 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
244 printf("proxyconnect: unable to connect to proxy\n");
250 buf[0] = 0x05; /* SOCKS version 5 */
251 if (strlen(sess->socksproxy.username)) {
252 buf[1] = 0x02; /* two methods */
253 buf[2] = 0x00; /* no authentication */
254 buf[3] = 0x02; /* username/password authentication */
262 if (write(fd, buf, i) < i) {
268 if (read(fd, buf, 2) < 2) {
274 if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
280 /* check if we're doing username authentication */
281 if (buf[1] == 0x02) {
282 i = aimutil_put8(buf, 0x01); /* version 1 */
283 i += aimutil_put8(buf+i, strlen(sess->socksproxy.username));
284 i += aimutil_putstr(buf+i, sess->socksproxy.username, strlen(sess->socksproxy.username));
285 i += aimutil_put8(buf+i, strlen(sess->socksproxy.password));
286 i += aimutil_putstr(buf+i, sess->socksproxy.password, strlen(sess->socksproxy.password));
287 if (write(fd, buf, i) < i) {
292 if (read(fd, buf, 2) < 2) {
297 if ((buf[0] != 0x01) || (buf[1] != 0x00)) {
304 i = aimutil_put8(buf, 0x05);
305 i += aimutil_put8(buf+i, 0x01); /* CONNECT */
306 i += aimutil_put8(buf+i, 0x00); /* reserved */
307 i += aimutil_put8(buf+i, 0x03); /* address type: host name */
308 i += aimutil_put8(buf+i, strlen(host));
309 i += aimutil_putstr(buf+i, host, strlen(host));
310 i += aimutil_put16(buf+i, port);
312 if (write(fd, buf, i) < i) {
317 if (read(fd, buf, 10) < 10) {
322 if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
328 } else { /* connecting directly */
329 struct sockaddr_in sa;
332 if (!(hp = gethostbyname(host))) {
333 *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR);
337 memset(&sa, 0, sizeof(struct sockaddr_in));
338 sa.sin_port = htons(port);
339 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
340 sa.sin_family = hp->h_addrtype;
342 fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
344 if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT)
345 fcntl(fd, F_SETFL, O_NONBLOCK); /* XXX save flags */
347 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
348 if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT) {
349 if ((errno == EINPROGRESS) || (errno == EINTR)) {
351 *statusret |= AIM_CONN_STATUS_INPROGRESS;
362 faim_internal struct aim_conn_t *aim_cloneconn(struct aim_session_t *sess,
363 struct aim_conn_t *src)
365 struct aim_conn_t *conn;
366 struct aim_rxcblist_t *cur;
368 if (!(conn = aim_conn_getnext(sess)))
372 faim_mutex_lock(&conn->active);
375 conn->type = src->type;
376 conn->subtype = src->subtype;
377 conn->seqnum = src->seqnum;
378 conn->priv = src->priv;
379 conn->lastactivity = src->lastactivity;
380 conn->forcedlatency = src->forcedlatency;
382 /* clone handler list */
383 for (cur = src->handlerlist; cur; cur = cur->next) {
384 aim_conn_addhandler(sess, conn, cur->family, cur->type,
385 cur->handler, cur->flags);
388 faim_mutex_unlock(&conn->active);
394 * aim_newconn - Open a new connection
395 * @sess: Session to create connection in
396 * @type: Type of connection to create
397 * @dest: Host to connect to (in "host:port" syntax)
399 * Opens a new connection to the specified dest host of specified
400 * type, using the proxy settings if available. If @host is %NULL,
401 * the connection is allocated and returned, but no connection
404 * FIXME: Return errors in a more sane way.
407 faim_export struct aim_conn_t *aim_newconn(struct aim_session_t *sess,
408 int type, char *dest)
410 struct aim_conn_t *connstruct;
412 u_short port = FAIM_LOGIN_PORT;
416 if ((connstruct=aim_conn_getnext(sess))==NULL)
419 faim_mutex_lock(&connstruct->active);
421 connstruct->type = type;
423 if (!dest) { /* just allocate a struct */
425 connstruct->status = 0;
426 faim_mutex_unlock(&connstruct->active);
431 * As of 23 Jul 1999, AOL now sends the port number, preceded by a
432 * colon, in the BOS redirect. This fatally breaks all previous
433 * libfaims. Bad, bad AOL.
435 * We put this here to catch every case.
439 for(i=0;i<(int)strlen(dest);i++) {
440 if (dest[i] == ':') {
441 port = atoi(&(dest[i+1]));
445 host = (char *)malloc(i+1);
446 strncpy(host, dest, i);
449 if ((ret = aim_proxyconnect(sess, host, port, &connstruct->status)) < 0) {
451 connstruct->status = (errno | AIM_CONN_STATUS_CONNERR);
453 faim_mutex_unlock(&connstruct->active);
456 connstruct->fd = ret;
458 faim_mutex_unlock(&connstruct->active);
466 * aim_conngetmaxfd - Return the highest valued file discriptor in session
467 * @sess: Session to search
469 * Returns the highest valued filed descriptor of all open
470 * connections in @sess.
473 faim_export int aim_conngetmaxfd(struct aim_session_t *sess)
476 struct aim_conn_t *cur;
478 faim_mutex_lock(&sess->connlistlock);
479 for (cur = sess->connlist; cur; cur = cur->next) {
483 faim_mutex_unlock(&sess->connlistlock);
489 * aim_conn_in_sess - Predicate to test the precense of a connection in a sess
490 * @sess: Session to look in
491 * @conn: Connection to look for
493 * Searches @sess for the passed connection. Returns 1 if its present,
497 faim_export int aim_conn_in_sess(struct aim_session_t *sess, struct aim_conn_t *conn)
499 struct aim_conn_t *cur;
501 faim_mutex_lock(&sess->connlistlock);
502 for(cur = sess->connlist; cur; cur = cur->next)
504 faim_mutex_unlock(&sess->connlistlock);
507 faim_mutex_unlock(&sess->connlistlock);
512 * aim_select - Wait for a socket with data or timeout
513 * @sess: Session to wait on
514 * @timeout: How long to wait
515 * @status: Return status
517 * Waits for a socket with data or for timeout, whichever comes first.
520 * Return codes in *status:
521 * -1 error in select() (%NULL returned)
522 * 0 no events pending (%NULL returned)
523 * 1 outgoing data pending (%NULL returned)
524 * 2 incoming data pending (connection with pending data returned)
526 * XXX: we could probably stand to do a little courser locking here.
529 faim_export struct aim_conn_t *aim_select(struct aim_session_t *sess,
530 struct timeval *timeout, int *status)
532 struct aim_conn_t *cur;
535 int i, haveconnecting = 0;
537 faim_mutex_lock(&sess->connlistlock);
538 if (sess->connlist == NULL) {
539 faim_mutex_unlock(&sess->connlistlock);
543 faim_mutex_unlock(&sess->connlistlock);
549 faim_mutex_lock(&sess->connlistlock);
550 for (cur = sess->connlist; cur; cur = cur->next) {
552 /* don't let invalid/dead connections sit around */
554 faim_mutex_unlock(&sess->connlistlock);
556 } else if (cur->status & AIM_CONN_STATUS_INPROGRESS) {
557 FD_SET(cur->fd, &wfds);
560 FD_SET(cur->fd, &fds);
564 faim_mutex_unlock(&sess->connlistlock);
567 * If we have data waiting to be sent, return
569 * We have to not do this if theres at least one
570 * connection thats still connecting, since that connection
571 * may have queued data and this return would prevent
572 * the connection from ever completing! This is a major
573 * inadequacy of the libfaim way of doing things. It means
574 * that nothing can transmit as long as there's connecting
577 * But its still better than having blocking connects.
580 if (!haveconnecting && (sess->queue_outgoing != NULL)) {
585 if ((i = select(maxfd+1, &fds, &wfds, NULL, timeout))>=1) {
586 faim_mutex_lock(&sess->connlistlock);
587 for (cur = sess->connlist; cur; cur = cur->next) {
588 if ((FD_ISSET(cur->fd, &fds)) ||
589 ((cur->status & AIM_CONN_STATUS_INPROGRESS) &&
590 FD_ISSET(cur->fd, &wfds))) {
592 faim_mutex_unlock(&sess->connlistlock);
593 return cur; /* XXX race condition here -- shouldnt unlock connlist */
596 *status = 0; /* shouldn't happen */
597 } else if ((i == -1) && (errno == EINTR)) /* treat interrupts as a timeout */
600 *status = i; /* can be 0 or -1 */
602 faim_mutex_unlock(&sess->connlistlock);
603 return NULL; /* no waiting or error, return */
607 * aim_conn_isready - Test if a connection is marked ready
608 * @conn: Connection to test
610 * Returns true if the connection is ready, false otherwise.
611 * Returns -1 if the connection is invalid.
613 * XXX: This is deprecated.
616 faim_export int aim_conn_isready(struct aim_conn_t *conn)
619 return (conn->status & 0x0001);
624 * aim_conn_setstatus - Set the status of a connection
626 * @status: New status
628 * @newstatus is %XOR'd with the previous value of the connection
629 * status and returned. Returns -1 if the connection is invalid.
631 * This isn't real useful.
634 faim_export int aim_conn_setstatus(struct aim_conn_t *conn, int status)
641 faim_mutex_lock(&conn->active);
642 val = conn->status ^= status;
643 faim_mutex_unlock(&conn->active);
648 * aim_conn_setlatency - Set a forced latency value for connection
649 * @conn: Conn to set latency for
650 * @newval: Number of seconds to force between transmits
652 * Causes @newval seconds to be spent between transmits on a connection.
654 * This is my lame attempt at overcoming not understanding the rate
657 * XXX: This should really be replaced with something that scales and
658 * backs off like the real rate limiting does.
661 faim_export int aim_conn_setlatency(struct aim_conn_t *conn, int newval)
666 faim_mutex_lock(&conn->active);
667 conn->forcedlatency = newval;
668 conn->lastactivity = 0; /* reset this just to make sure */
669 faim_mutex_unlock(&conn->active);
675 * aim_setupproxy - Configure a proxy for this session
676 * @sess: Session to set proxy for
677 * @server: SOCKS server
678 * @username: SOCKS username
679 * @password: SOCKS password
681 * Call this with your SOCKS5 proxy server parameters before
682 * the first call to aim_newconn(). If called with all %NULL
683 * args, it will clear out a previously set proxy.
685 * Set username and password to %NULL if not applicable.
688 faim_export void aim_setupproxy(struct aim_session_t *sess, char *server, char *username, char *password)
690 /* clear out the proxy info */
691 if (!server || !strlen(server)) {
692 memset(sess->socksproxy.server, 0, sizeof(sess->socksproxy.server));
693 memset(sess->socksproxy.username, 0, sizeof(sess->socksproxy.username));
694 memset(sess->socksproxy.password, 0, sizeof(sess->socksproxy.password));
698 strncpy(sess->socksproxy.server, server, sizeof(sess->socksproxy.server));
699 if (username && strlen(username))
700 strncpy(sess->socksproxy.username, username, sizeof(sess->socksproxy.username));
701 if (password && strlen(password))
702 strncpy(sess->socksproxy.password, password, sizeof(sess->socksproxy.password));
707 * aim_session_init - Initializes a session structure
708 * @sess: Session to initialize
709 * @flags: Flags to use. Any of %AIM_SESS_FLAGS %OR'd together.
711 * Sets up the initial values for a session.
714 faim_export void aim_session_init(struct aim_session_t *sess, unsigned long flags)
719 memset(sess, 0, sizeof(struct aim_session_t));
721 sess->queue_outgoing = NULL;
722 sess->queue_incoming = NULL;
723 sess->pendingjoin = NULL;
724 sess->pendingjoinexchange = 0;
725 aim_initsnachash(sess);
726 sess->msgcookies = NULL;
727 sess->snac_nextid = 0x00000001;
732 * Default to SNAC login unless XORLOGIN is explicitly set.
734 if (!(flags & AIM_SESS_FLAGS_XORLOGIN))
735 sess->flags |= AIM_SESS_FLAGS_SNACLOGIN;
736 sess->flags |= flags;
739 * This must always be set. Default to the queue-based
740 * version for back-compatibility.
742 aim_tx_setenqueue(sess, AIM_TX_QUEUED, NULL);
748 * aim_conn_isconnecting - Determine if a connection is connecting
749 * @conn: Connection to examine
751 * Returns nonzero if the connection is in the process of
752 * connecting (or if it just completed and aim_conn_completeconnect()
753 * has yet to be called on it).
756 faim_export int aim_conn_isconnecting(struct aim_conn_t *conn)
760 return (conn->status & AIM_CONN_STATUS_INPROGRESS)?1:0;
763 faim_export int aim_conn_completeconnect(struct aim_session_t *sess, struct aim_conn_t *conn)
767 int res, error = ETIMEDOUT;
768 rxcallback_t userfunc;
770 if (!conn || (conn->fd == -1))
773 if (!(conn->status & AIM_CONN_STATUS_INPROGRESS))
777 FD_SET(conn->fd, &fds);
779 FD_SET(conn->fd, &wfds);
783 if ((res = select(conn->fd+1, &fds, &wfds, NULL, &tv)) == -1) {
785 aim_conn_close(conn);
788 } else if (res == 0) {
789 printf("faim: aim_conn_completeconnect: false alarm on %d\n", conn->fd);
790 return 0; /* hasn't really completed yet... */
793 if (FD_ISSET(conn->fd, &fds) || FD_ISSET(conn->fd, &wfds)) {
794 int len = sizeof(error);
796 if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
801 aim_conn_close(conn);
806 fcntl(conn->fd, F_SETFL, 0); /* XXX should restore original flags */
808 conn->status &= ~AIM_CONN_STATUS_INPROGRESS;
810 if ((userfunc = aim_callhandler(conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNCOMPLETE)))
811 userfunc(sess, NULL, conn);
813 /* Flush out the queues if there was something waiting for this conn */
814 aim_tx_flushqueue(sess);