5 * Does all this gloriously nifty connection handling stuff...
14 #include <sys/socket.h>
15 #include <netinet/in.h>
19 * In OSCAR, every connection has a set of SNAC groups associated
20 * with it. These are the groups that you can send over this connection
21 * without being guarenteed a "Not supported" SNAC error.
23 * The grand theory of things says that these associations transcend
24 * what libfaim calls "connection types" (conn->type). You can probably
25 * see the elegance here, but since I want to revel in it for a bit, you
26 * get to hear it all spelled out.
28 * So let us say that you have your core BOS connection running. One
29 * of your modules has just given you a SNAC of the group 0x0004 to send
30 * you. Maybe an IM destined for some twit in Greenland. So you start
31 * at the top of your connection list, looking for a connection that
32 * claims to support group 0x0004. You find one. Why, that neat BOS
33 * connection of yours can do that. So you send it on its way.
35 * Now, say, that fellow from Greenland has friends and they all want to
36 * meet up with you in a lame chat room. This has landed you a SNAC
37 * in the family 0x000e and you have to admit you're a bit lost. You've
38 * searched your connection list for someone who wants to make your life
39 * easy and deliver this SNAC for you, but there isn't one there.
41 * Here comes the good bit. Without even letting anyone know, particularly
42 * the module that decided to send this SNAC, and definitly not that twit
43 * in Greenland, you send out a service request. In this request, you have
44 * marked the need for a connection supporting group 0x000e. A few seconds
45 * later, you receive a service redirect with an IP address and a cookie in
46 * it. Great, you say. Now I have something to do. Off you go, making
47 * that connection. One of the first things you get from this new server
48 * is a message saying that indeed it does support the group you were looking
49 * for. So you continue and send rate confirmation and all that.
51 * Then you remember you had that SNAC to send, and now you have a means to
52 * do it, and you do, and everyone is happy. Except the Greenlander, who is
53 * still stuck in the bitter cold.
55 * Oh, and this is useful for building the Migration SNACs, too. In the
56 * future, this may help convince me to implement rate limit mitigation
57 * for real. We'll see.
59 * Just to make me look better, I'll say that I've known about this great
60 * scheme for quite some time now. But I still haven't convinced myself
61 * to make libfaim work that way. It would take a fair amount of effort,
62 * and probably some client API changes as well. (Whenever I don't want
63 * to do something, I just say it would change the client API. Then I
64 * instantly have a couple of supporters of not doing it.)
66 * Generally, addgroup is only called by the internal handling of the
67 * server ready SNAC. So if you want to do something before that, you'll
68 * have to be more creative. That is done rather early, though, so I don't
69 * think you have to worry about it. Unless you're me. I care deeply
70 * about such inane things.
73 faim_internal void aim_conn_addgroup(aim_conn_t *conn, fu16_t group)
75 aim_conn_inside_t *ins = (aim_conn_inside_t *)conn->inside;
78 if (!(sg = malloc(sizeof(struct snacgroup))))
81 faimdprintf(aim_conn_getsess(conn), 1, "adding group 0x%04x\n", group);
84 sg->next = ins->groups;
90 faim_export aim_conn_t *aim_conn_findbygroup(aim_session_t *sess, fu16_t group)
94 for (cur = sess->connlist; cur; cur = cur->next) {
95 aim_conn_inside_t *ins = (aim_conn_inside_t *)cur->inside;
98 for (sg = ins->groups; sg; sg = sg->next) {
99 if (sg->group == group)
107 static struct snacgroup *connkill_snacgroups(struct snacgroup *sg)
111 struct snacgroup *tmp;
121 static void connkill_real(aim_session_t *sess, aim_conn_t **deadconn)
124 aim_rxqueue_cleanbyconn(sess, *deadconn);
125 aim_tx_cleanqueue(sess, *deadconn);
127 if ((*deadconn)->fd != -1)
128 aim_conn_close(*deadconn);
131 * XXX ->priv should never be touched by the library. I know
132 * it used to be, but I'm getting rid of all that. Use
133 * ->internal instead.
135 if ((*deadconn)->priv)
136 free((*deadconn)->priv);
139 * This will free ->internal if it necessary...
141 if ((*deadconn)->type == AIM_CONN_TYPE_RENDEZVOUS)
142 aim_conn_kill_rend(sess, *deadconn);
144 if ((*deadconn)->inside) {
145 aim_conn_inside_t *inside = (aim_conn_inside_t *)(*deadconn)->inside;
147 inside->groups = connkill_snacgroups(inside->groups);
159 * aim_connrst - Clears out connection list, killing remaining connections.
160 * @sess: Session to be cleared
162 * Clears out the connection list and kills any connections left.
165 static void aim_connrst(aim_session_t *sess)
168 if (sess->connlist) {
169 aim_conn_t *cur = sess->connlist, *tmp;
174 connkill_real(sess, &cur);
179 sess->connlist = NULL;
185 * aim_conn_init - Reset a connection to default values.
186 * @deadconn: Connection to be reset
188 * Initializes and/or resets a connection structure.
191 static void aim_conn_init(aim_conn_t *deadconn)
198 deadconn->subtype = -1;
200 deadconn->seqnum = 0;
201 deadconn->lastactivity = 0;
202 deadconn->forcedlatency = 0;
203 deadconn->handlerlist = NULL;
204 deadconn->priv = NULL;
205 memset(deadconn->inside, 0, sizeof(aim_conn_inside_t));
211 * aim_conn_getnext - Gets a new connection structure.
214 * Allocate a new empty connection structure.
217 static aim_conn_t *aim_conn_getnext(aim_session_t *sess)
221 if (!(newconn = malloc(sizeof(aim_conn_t))))
223 memset(newconn, 0, sizeof(aim_conn_t));
225 if (!(newconn->inside = malloc(sizeof(aim_conn_inside_t)))) {
229 memset(newconn->inside, 0, sizeof(aim_conn_inside_t));
231 aim_conn_init(newconn);
233 newconn->next = sess->connlist;
234 sess->connlist = newconn;
240 * aim_conn_kill - Close and free a connection.
241 * @sess: Session for the connection
242 * @deadconn: Connection to be freed
244 * Close, clear, and free a connection structure. Should never be
245 * called from within libfaim.
248 faim_export void aim_conn_kill(aim_session_t *sess, aim_conn_t **deadconn)
250 aim_conn_t *cur, **prev;
252 if (!deadconn || !*deadconn)
255 for (prev = &sess->connlist; (cur = *prev); ) {
256 if (cur == *deadconn) {
266 connkill_real(sess, &cur);
272 * aim_conn_close - Close a connection
273 * @deadconn: Connection to close
275 * Close (but not free) a connection.
277 * This leaves everything untouched except for clearing the
278 * handler list and setting the fd to -1 (used to recognize
279 * dead connections). It will also remove cookies if necessary.
282 faim_export void aim_conn_close(aim_conn_t *deadconn)
285 if (deadconn->fd >= 3)
288 if (deadconn->handlerlist)
289 aim_clearhandlers(deadconn);
290 if (deadconn->type == AIM_CONN_TYPE_RENDEZVOUS)
291 aim_conn_close_rend((aim_session_t *)deadconn->sessv, deadconn);
297 * aim_getconn_type - Find a connection of a specific type
298 * @sess: Session to search
299 * @type: Type of connection to look for
301 * Searches for a connection of the specified type in the
302 * specified session. Returns the first connection of that
305 * XXX except for RENDEZVOUS, all uses of this should be removed and
306 * use aim_conn_findbygroup() instead.
308 faim_export aim_conn_t *aim_getconn_type(aim_session_t *sess, int type)
312 for (cur = sess->connlist; cur; cur = cur->next) {
313 if ((cur->type == type) &&
314 !(cur->status & AIM_CONN_STATUS_INPROGRESS))
321 faim_export aim_conn_t *aim_getconn_type_all(aim_session_t *sess, int type)
325 for (cur = sess->connlist; cur; cur = cur->next) {
326 if (cur->type == type)
333 /* If you pass -1 for the fd, you'll get what you ask for. Gibberish. */
334 faim_export aim_conn_t *aim_getconn_fd(aim_session_t *sess, int fd)
338 for (cur = sess->connlist; cur; cur = cur->next) {
347 * aim_proxyconnect - An extrememly quick and dirty SOCKS5 interface.
348 * @sess: Session to connect
349 * @host: Host to connect to
350 * @port: Port to connect to
351 * @statusret: Return value of the connection
353 * Attempts to connect to the specified host via the configured
354 * proxy settings, if present. If no proxy is configured for
355 * this session, the connection is done directly.
357 * XXX this is really awful.
360 static int aim_proxyconnect(aim_session_t *sess, const char *host, fu16_t port, fu32_t *statusret)
364 if (strlen(sess->socksproxy.server)) { /* connecting via proxy */
366 unsigned char buf[512];
367 struct sockaddr_in sa;
370 unsigned short proxyport = 1080;
372 for(i=0;i<(int)strlen(sess->socksproxy.server);i++) {
373 if (sess->socksproxy.server[i] == ':') {
374 proxyport = atoi(&(sess->socksproxy.server[i+1]));
379 proxy = (char *)malloc(i+1);
380 strncpy(proxy, sess->socksproxy.server, i);
383 if (!(hp = gethostbyname(proxy))) {
384 faimdprintf(sess, 0, "proxyconnect: unable to resolve proxy name\n");
385 *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR);
390 memset(&sa.sin_zero, 0, 8);
391 sa.sin_port = htons(proxyport);
392 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
393 sa.sin_family = hp->h_addrtype;
395 fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
396 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
397 faimdprintf(sess, 0, "proxyconnect: unable to connect to proxy\n");
403 buf[0] = 0x05; /* SOCKS version 5 */
404 if (strlen(sess->socksproxy.username)) {
405 buf[1] = 0x02; /* two methods */
406 buf[2] = 0x00; /* no authentication */
407 buf[3] = 0x02; /* username/password authentication */
415 if (write(fd, buf, i) < i) {
421 if (read(fd, buf, 2) < 2) {
427 if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
433 /* check if we're doing username authentication */
434 if (buf[1] == 0x02) {
435 i = aimutil_put8(buf, 0x01); /* version 1 */
436 i += aimutil_put8(buf+i, strlen(sess->socksproxy.username));
437 i += aimutil_putstr(buf+i, sess->socksproxy.username, strlen(sess->socksproxy.username));
438 i += aimutil_put8(buf+i, strlen(sess->socksproxy.password));
439 i += aimutil_putstr(buf+i, sess->socksproxy.password, strlen(sess->socksproxy.password));
440 if (write(fd, buf, i) < i) {
445 if (read(fd, buf, 2) < 2) {
450 if ((buf[0] != 0x01) || (buf[1] != 0x00)) {
457 i = aimutil_put8(buf, 0x05);
458 i += aimutil_put8(buf+i, 0x01); /* CONNECT */
459 i += aimutil_put8(buf+i, 0x00); /* reserved */
460 i += aimutil_put8(buf+i, 0x03); /* address type: host name */
461 i += aimutil_put8(buf+i, strlen(host));
462 i += aimutil_putstr(buf+i, host, strlen(host));
463 i += aimutil_put16(buf+i, port);
465 if (write(fd, buf, i) < i) {
470 if (read(fd, buf, 10) < 10) {
475 if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
481 } else { /* connecting directly */
482 struct sockaddr_in sa;
485 if (!(hp = gethostbyname(host))) {
486 *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR);
490 memset(&sa, 0, sizeof(struct sockaddr_in));
491 sa.sin_port = htons(port);
492 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
493 sa.sin_family = hp->h_addrtype;
495 fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
497 if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT)
498 fcntl(fd, F_SETFL, O_NONBLOCK); /* XXX save flags */
500 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
501 if (sess->flags & AIM_SESS_FLAGS_NONBLOCKCONNECT) {
502 if ((errno == EINPROGRESS) || (errno == EINTR)) {
504 *statusret |= AIM_CONN_STATUS_INPROGRESS;
516 * aim_cloneconn - clone an aim_conn_t
517 * @sess: session containing parent
518 * @src: connection to clone
520 * A new connection is allocated, and the values are filled in
521 * appropriately. Note that this function sets the new connnection's
522 * ->priv pointer to be equal to that of its parent: only the pointer
523 * is copied, not the data it points to.
525 * This function returns a pointer to the new aim_conn_t, or %NULL on
528 faim_internal aim_conn_t *aim_cloneconn(aim_session_t *sess, aim_conn_t *src)
532 if (!(conn = aim_conn_getnext(sess)))
536 conn->type = src->type;
537 conn->subtype = src->subtype;
538 conn->seqnum = src->seqnum;
539 conn->priv = src->priv;
540 conn->internal = src->internal;
541 conn->lastactivity = src->lastactivity;
542 conn->forcedlatency = src->forcedlatency;
543 conn->sessv = src->sessv;
544 aim_clonehandlers(sess, conn, src);
548 * XXX should clone this section as well, but since currently
549 * this function only gets called for some of that rendezvous
550 * crap, and not on SNAC connections, its probably okay for
560 * aim_newconn - Open a new connection
561 * @sess: Session to create connection in
562 * @type: Type of connection to create
563 * @dest: Host to connect to (in "host:port" syntax)
565 * Opens a new connection to the specified dest host of specified
566 * type, using the proxy settings if available. If @host is %NULL,
567 * the connection is allocated and returned, but no connection
570 * FIXME: Return errors in a more sane way.
573 faim_export aim_conn_t *aim_newconn(aim_session_t *sess, int type, const char *dest)
575 aim_conn_t *connstruct;
576 fu16_t port = FAIM_LOGIN_PORT;
580 if (!(connstruct = aim_conn_getnext(sess)))
583 connstruct->sessv = (void *)sess;
584 connstruct->type = type;
586 if (!dest) { /* just allocate a struct */
588 connstruct->status = 0;
593 * As of 23 Jul 1999, AOL now sends the port number, preceded by a
594 * colon, in the BOS redirect. This fatally breaks all previous
595 * libfaims. Bad, bad AOL.
597 * We put this here to catch every case.
601 for(i = 0; i < (int)strlen(dest); i++) {
602 if (dest[i] == ':') {
603 port = atoi(&(dest[i+1]));
608 host = (char *)malloc(i+1);
609 strncpy(host, dest, i);
612 if ((ret = aim_proxyconnect(sess, host, port, &connstruct->status)) < 0) {
614 connstruct->status = (errno | AIM_CONN_STATUS_CONNERR);
618 connstruct->fd = ret;
626 * aim_conngetmaxfd - Return the highest valued file discriptor in session
627 * @sess: Session to search
629 * Returns the highest valued filed descriptor of all open
630 * connections in @sess.
633 faim_export int aim_conngetmaxfd(aim_session_t *sess)
638 for (cur = sess->connlist, j = 0; cur; cur = cur->next) {
647 * aim_conn_in_sess - Predicate to test the precense of a connection in a sess
648 * @sess: Session to look in
649 * @conn: Connection to look for
651 * Searches @sess for the passed connection. Returns 1 if its present,
655 faim_export int aim_conn_in_sess(aim_session_t *sess, aim_conn_t *conn)
659 for (cur = sess->connlist; cur; cur = cur->next) {
668 * aim_select - Wait for a socket with data or timeout
669 * @sess: Session to wait on
670 * @timeout: How long to wait
671 * @status: Return status
673 * Waits for a socket with data or for timeout, whichever comes first.
676 * Return codes in *status:
677 * -1 error in select() (%NULL returned)
678 * 0 no events pending (%NULL returned)
679 * 1 outgoing data pending (%NULL returned)
680 * 2 incoming data pending (connection with pending data returned)
683 faim_export aim_conn_t *aim_select(aim_session_t *sess, struct timeval *timeout, int *status)
687 int maxfd, i, haveconnecting = 0;
689 if (!sess->connlist) {
697 for (cur = sess->connlist, maxfd = 0; cur; cur = cur->next) {
699 /* don't let invalid/dead connections sit around */
702 } else if (cur->status & AIM_CONN_STATUS_INPROGRESS) {
703 FD_SET(cur->fd, &wfds);
707 FD_SET(cur->fd, &fds);
713 * If we have data waiting to be sent, return
715 * We have to not do this if theres at least one
716 * connection thats still connecting, since that connection
717 * may have queued data and this return would prevent
718 * the connection from ever completing! This is a major
719 * inadequacy of the libfaim way of doing things. It means
720 * that nothing can transmit as long as there's connecting
723 * But its still better than having blocking connects.
726 if (!haveconnecting && sess->queue_outgoing) {
731 if ((i = select(maxfd+1, &fds, &wfds, NULL, timeout))>=1) {
732 for (cur = sess->connlist; cur; cur = cur->next) {
733 if ((FD_ISSET(cur->fd, &fds)) ||
734 ((cur->status & AIM_CONN_STATUS_INPROGRESS) &&
735 FD_ISSET(cur->fd, &wfds))) {
740 *status = 0; /* shouldn't happen */
741 } else if ((i == -1) && (errno == EINTR)) /* treat interrupts as a timeout */
744 *status = i; /* can be 0 or -1 */
746 return NULL; /* no waiting or error, return */
750 * aim_conn_setlatency - Set a forced latency value for connection
751 * @conn: Conn to set latency for
752 * @newval: Number of seconds to force between transmits
754 * Causes @newval seconds to be spent between transmits on a connection.
756 * This is my lame attempt at overcoming not understanding the rate
759 * XXX: This should really be replaced with something that scales and
760 * backs off like the real rate limiting does.
763 faim_export int aim_conn_setlatency(aim_conn_t *conn, int newval)
769 conn->forcedlatency = newval;
770 conn->lastactivity = 0; /* reset this just to make sure */
776 * aim_setupproxy - Configure a proxy for this session
777 * @sess: Session to set proxy for
778 * @server: SOCKS server
779 * @username: SOCKS username
780 * @password: SOCKS password
782 * Call this with your SOCKS5 proxy server parameters before
783 * the first call to aim_newconn(). If called with all %NULL
784 * args, it will clear out a previously set proxy.
786 * Set username and password to %NULL if not applicable.
789 faim_export void aim_setupproxy(aim_session_t *sess, const char *server, const char *username, const char *password)
791 /* clear out the proxy info */
792 if (!server || !strlen(server)) {
793 memset(sess->socksproxy.server, 0, sizeof(sess->socksproxy.server));
794 memset(sess->socksproxy.username, 0, sizeof(sess->socksproxy.username));
795 memset(sess->socksproxy.password, 0, sizeof(sess->socksproxy.password));
799 strncpy(sess->socksproxy.server, server, sizeof(sess->socksproxy.server));
800 if (username && strlen(username))
801 strncpy(sess->socksproxy.username, username, sizeof(sess->socksproxy.username));
802 if (password && strlen(password))
803 strncpy(sess->socksproxy.password, password, sizeof(sess->socksproxy.password));
808 static void defaultdebugcb(aim_session_t *sess, int level, const char *format, va_list va)
811 vfprintf(stderr, format, va);
817 * aim_session_init - Initializes a session structure
818 * @sess: Session to initialize
819 * @flags: Flags to use. Any of %AIM_SESS_FLAGS %OR'd together.
820 * @debuglevel: Level of debugging output (zero is least)
822 * Sets up the initial values for a session.
825 faim_export void aim_session_init(aim_session_t *sess, fu32_t flags, int debuglevel)
831 memset(sess, 0, sizeof(aim_session_t));
833 sess->queue_outgoing = NULL;
834 sess->queue_incoming = NULL;
835 sess->pendingjoin = NULL;
836 sess->pendingjoinexchange = 0;
837 aim_initsnachash(sess);
838 sess->msgcookies = NULL;
839 sess->snacid_next = 0x00000001;
842 sess->debug = debuglevel;
843 sess->debugcb = defaultdebugcb;
845 sess->modlistv = NULL;
848 * Default to SNAC login unless XORLOGIN is explicitly set.
850 if (!(flags & AIM_SESS_FLAGS_XORLOGIN))
851 sess->flags |= AIM_SESS_FLAGS_SNACLOGIN;
852 sess->flags |= flags;
855 * This must always be set. Default to the queue-based
856 * version for back-compatibility.
858 aim_tx_setenqueue(sess, AIM_TX_QUEUED, NULL);
862 * Register all the modules for this session...
864 aim__registermodule(sess, misc_modfirst); /* load the catch-all first */
865 aim__registermodule(sess, buddylist_modfirst);
866 aim__registermodule(sess, admin_modfirst);
867 aim__registermodule(sess, bos_modfirst);
868 aim__registermodule(sess, search_modfirst);
869 aim__registermodule(sess, stats_modfirst);
870 aim__registermodule(sess, auth_modfirst);
871 aim__registermodule(sess, msg_modfirst);
872 aim__registermodule(sess, chatnav_modfirst);
873 aim__registermodule(sess, chat_modfirst);
874 aim__registermodule(sess, locate_modfirst);
875 aim__registermodule(sess, general_modfirst);
881 * aim_session_kill - Deallocate a session
882 * @sess: Session to kill
885 faim_export void aim_session_kill(aim_session_t *sess)
890 aim__shutdownmodules(sess);
896 * aim_setdebuggingcb - Set the function to call when outputting debugging info
897 * @sess: Session to change
898 * @cb: Function to call
900 * The function specified is called whenever faimdprintf() is used within
901 * libfaim, and the session's debugging level is greater tha nor equal to
902 * the value faimdprintf was called with.
905 faim_export int aim_setdebuggingcb(aim_session_t *sess, faim_debugging_callback_t cb)
917 * aim_conn_isconnecting - Determine if a connection is connecting
918 * @conn: Connection to examine
920 * Returns nonzero if the connection is in the process of
921 * connecting (or if it just completed and aim_conn_completeconnect()
922 * has yet to be called on it).
925 faim_export int aim_conn_isconnecting(aim_conn_t *conn)
931 return !!(conn->status & AIM_CONN_STATUS_INPROGRESS);
935 * XXX this is nearly as ugly as proxyconnect().
937 faim_export int aim_conn_completeconnect(aim_session_t *sess, aim_conn_t *conn)
941 int res, error = ETIMEDOUT;
942 aim_rxcallback_t userfunc;
944 if (!conn || (conn->fd == -1))
947 if (!(conn->status & AIM_CONN_STATUS_INPROGRESS))
951 FD_SET(conn->fd, &fds);
953 FD_SET(conn->fd, &wfds);
957 if ((res = select(conn->fd+1, &fds, &wfds, NULL, &tv)) == -1) {
959 aim_conn_close(conn);
962 } else if (res == 0) {
963 faimdprintf(sess, 0, "aim_conn_completeconnect: false alarm on %d\n", conn->fd);
964 return 0; /* hasn't really completed yet... */
967 if (FD_ISSET(conn->fd, &fds) || FD_ISSET(conn->fd, &wfds)) {
968 int len = sizeof(error);
970 if (getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
975 aim_conn_close(conn);
980 fcntl(conn->fd, F_SETFL, 0); /* XXX should restore original flags */
982 conn->status &= ~AIM_CONN_STATUS_INPROGRESS;
984 if ((userfunc = aim_callhandler(sess, conn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNCOMPLETE)))
985 userfunc(sess, NULL, conn);
987 /* Flush out the queues if there was something waiting for this conn */
988 aim_tx_flushqueue(sess);
993 faim_export aim_session_t *aim_conn_getsess(aim_conn_t *conn)
999 return (aim_session_t *)conn->sessv;
1005 * Closes -ALL- open connections.
1008 faim_export int aim_logoff(aim_session_t *sess)
1011 aim_connrst(sess); /* in case we want to connect again */