📄 conn.c
字号:
/** * Low-level connection handling. * * Does all this gloriously nifty connection handling stuff... * */#define FAIM_INTERNAL#define FAIM_NEED_CONN_INTERNAL#include <aim.h> #ifndef _WIN32#include <netdb.h>#include <sys/socket.h>#include <netinet/in.h>#endif#ifdef _WIN32#include "win32dep.h"#endif/** * In OSCAR, every connection has a set of SNAC groups associated * with it. These are the groups that you can send over this connection * without being guaranteed a "Not supported" SNAC error. * * The grand theory of things says that these associations transcend * what libfaim calls "connection types" (conn->type). You can probably * see the elegance here, but since I want to revel in it for a bit, you * get to hear it all spelled out. * * So let us say that you have your core BOS connection running. One * of your modules has just given you a SNAC of the group 0x0004 to send * you. Maybe an IM destined for some twit in Greenland. So you start * at the top of your connection list, looking for a connection that * claims to support group 0x0004. You find one. Why, that neat BOS * connection of yours can do that. So you send it on its way. * * Now, say, that fellow from Greenland has friends and they all want to * meet up with you in a lame chat room. This has landed you a SNAC * in the family 0x000e and you have to admit you're a bit lost. You've * searched your connection list for someone who wants to make your life * easy and deliver this SNAC for you, but there isn't one there. * * Here comes the good bit. Without even letting anyone know, particularly * the module that decided to send this SNAC, and definitely not that twit * in Greenland, you send out a service request. In this request, you have * marked the need for a connection supporting group 0x000e. A few seconds * later, you receive a service redirect with an IP address and a cookie in * it. Great, you say. Now I have something to do. Off you go, making * that connection. One of the first things you get from this new server * is a message saying that indeed it does support the group you were looking * for. So you continue and send rate confirmation and all that. * * Then you remember you had that SNAC to send, and now you have a means to * do it, and you do, and everyone is happy. Except the Greenlander, who is * still stuck in the bitter cold. * * Oh, and this is useful for building the Migration SNACs, too. In the * future, this may help convince me to implement rate limit mitigation * for real. We'll see. * * Just to make me look better, I'll say that I've known about this great * scheme for quite some time now. But I still haven't convinced myself * to make libfaim work that way. It would take a fair amount of effort, * and probably some client API changes as well. (Whenever I don't want * to do something, I just say it would change the client API. Then I * instantly have a couple of supporters of not doing it.) * * Generally, addgroup is only called by the internal handling of the * server ready SNAC. So if you want to do something before that, you'll * have to be more creative. That is done rather early, though, so I don't * think you have to worry about it. Unless you're me. I care deeply * about such inane things. * */faim_internal void aim_conn_addgroup(aim_conn_t *conn, fu16_t group){ aim_conn_inside_t *ins = (aim_conn_inside_t *)conn->inside; struct snacgroup *sg; if (!(sg = malloc(sizeof(struct snacgroup)))) return; faimdprintf(aim_conn_getsess(conn), 1, "adding group 0x%04x\n", group); sg->group = group; sg->next = ins->groups; ins->groups = sg; return;}faim_export aim_conn_t *aim_conn_findbygroup(aim_session_t *sess, fu16_t group){ aim_conn_t *cur; for (cur = sess->connlist; cur; cur = cur->next) { aim_conn_inside_t *ins = (aim_conn_inside_t *)cur->inside; struct snacgroup *sg; for (sg = ins->groups; sg; sg = sg->next) { if (sg->group == group) return cur; } } return NULL;}static void connkill_snacgroups(struct snacgroup **head){ struct snacgroup *sg; for (sg = *head; sg; ) { struct snacgroup *tmp; tmp = sg->next; free(sg); sg = tmp; } *head = NULL; return;}static void connkill_rates(struct rateclass **head){ struct rateclass *rc; for (rc = *head; rc; ) { struct rateclass *tmp; struct snacpair *sp; tmp = rc->next; for (sp = rc->members; sp; ) { struct snacpair *tmpsp; tmpsp = sp->next; free(sp); sp = tmpsp; } free(rc); rc = tmp; } *head = NULL; return;}static void connkill_real(aim_session_t *sess, aim_conn_t **deadconn){ aim_rxqueue_cleanbyconn(sess, *deadconn); aim_tx_cleanqueue(sess, *deadconn); if ((*deadconn)->fd != -1) aim_conn_close(*deadconn); /* * XXX ->priv should never be touched by the library. I know * it used to be, but I'm getting rid of all that. Use * ->internal instead. */ if ((*deadconn)->priv) free((*deadconn)->priv); /* * This will free ->internal if it necessary... */ if ((*deadconn)->type == AIM_CONN_TYPE_CHAT) aim_conn_kill_chat(sess, *deadconn); if ((*deadconn)->inside) { aim_conn_inside_t *inside = (aim_conn_inside_t *)(*deadconn)->inside; connkill_snacgroups(&inside->groups); connkill_rates(&inside->rates); free(inside); } free(*deadconn); *deadconn = NULL; return;}/** * Clears out the connection list, killing remaining connections. * * @param sess Session to be cleared. */static void aim_connrst(aim_session_t *sess){ if (sess->connlist) { aim_conn_t *cur = sess->connlist, *tmp; while (cur) { tmp = cur->next; aim_conn_close(cur); connkill_real(sess, &cur); cur = tmp; } } sess->connlist = NULL; return;}/** * Initializes and/or resets a connection structure to the default values. * * @param deadconn Connection to be reset. */static void aim_conn_init(aim_conn_t *deadconn){ if (!deadconn) return; deadconn->fd = -1; deadconn->subtype = -1; deadconn->type = -1; deadconn->seqnum = 0; deadconn->lastactivity = 0; deadconn->forcedlatency = 0; deadconn->handlerlist = NULL; deadconn->priv = NULL; memset(deadconn->inside, 0, sizeof(aim_conn_inside_t)); return;}/** * Allocate a new empty connection structure. * * @param sess Session * @return Returns the new connection structure. */static aim_conn_t *aim_conn_getnext(aim_session_t *sess){ aim_conn_t *newconn; if (!(newconn = malloc(sizeof(aim_conn_t)))) return NULL; memset(newconn, 0, sizeof(aim_conn_t)); if (!(newconn->inside = malloc(sizeof(aim_conn_inside_t)))) { free(newconn); return NULL; } memset(newconn->inside, 0, sizeof(aim_conn_inside_t)); aim_conn_init(newconn); newconn->next = sess->connlist; sess->connlist = newconn; return newconn;}/** * Close, clear, and free a connection structure. Should never be * called from within libfaim. * * @param sess Session for the connection. * @param deadconn Connection to be freed. */faim_export void aim_conn_kill(aim_session_t *sess, aim_conn_t **deadconn){ aim_conn_t *cur, **prev; if (!deadconn || !*deadconn) return; for (prev = &sess->connlist; (cur = *prev); ) { if (cur == *deadconn) { *prev = cur->next; break; } prev = &cur->next; } if (!cur) return; /* oops */ connkill_real(sess, &cur); return;}/** * Close (but not free) a connection. * * This leaves everything untouched except for clearing the * handler list and setting the fd to -1 (used to recognize * dead connections). It will also remove cookies if necessary. * * Why only if fd >= 3? Seems rather implementation specific... * fd's do not have to be distributed in a particular order, do they? * * @param deadconn The connection to close. */faim_export void aim_conn_close(aim_conn_t *deadconn){ aim_rxcallback_t userfunc; if (deadconn->fd >= 3) close(deadconn->fd); deadconn->fd = -1; if ((userfunc = aim_callhandler(deadconn->sessv, deadconn, AIM_CB_FAM_SPECIAL, AIM_CB_SPECIAL_CONNDEAD))) userfunc(deadconn->sessv, NULL, deadconn); if (deadconn->handlerlist) aim_clearhandlers(deadconn); return;}/** * Locates a connection of the specified type in the * specified session. * * XXX - Except for rendezvous, all uses of this should be removed and * aim_conn_findbygroup() should be used instead. * * @param sess The session to search. * @param type The type of connection to look for. * @return Returns the first connection found of the given target type, * or NULL if none could be found. */faim_export aim_conn_t *aim_getconn_type(aim_session_t *sess, int type){ aim_conn_t *cur; for (cur = sess->connlist; cur; cur = cur->next) { if ((cur->type == type) && !(cur->status & AIM_CONN_STATUS_INPROGRESS)) break; } return cur;}faim_export aim_conn_t *aim_getconn_type_all(aim_session_t *sess, int type){ aim_conn_t *cur; for (cur = sess->connlist; cur; cur = cur->next) { if (cur->type == type) break; } return cur;}/* If you pass -1 for the fd, you'll get what you ask for. Gibberish. */faim_export aim_conn_t *aim_getconn_fd(aim_session_t *sess, int fd){ aim_conn_t *cur; for (cur = sess->connlist; cur; cur = cur->next) { if (cur->fd == fd) break; } return cur;}/** * Handle normal connections or SOCKS5 via an extrememly quick and * dirty SOCKS5 interface. * * Attempts to connect to the specified host via the configured * proxy settings, if present. If no proxy is configured for * this session, the connection is done directly. * * XXX - this is really awful. * XXX - Split the SOCKS5 and the normal connection stuff into two * separate functions. * * @param sess Session to connect. * @param host Host to connect to. * @param port Port to connect to. * @param statusret Return value of the connection. */static int aim_proxyconnect(aim_session_t *sess, const char *host, fu16_t port, fu32_t *statusret){ int fd = -1; if (strlen(sess->socksproxy.server)) { /* connecting via proxy */ int i; unsigned char buf[512]; struct sockaddr_in sa; struct hostent *hp; char *proxy; unsigned short proxyport = 1080; for(i=0;i<(int)strlen(sess->socksproxy.server);i++) { if (sess->socksproxy.server[i] == ':') { proxyport = atoi(&(sess->socksproxy.server[i+1])); break; } } proxy = (char *)malloc(i+1); strncpy(proxy, sess->socksproxy.server, i); proxy[i] = '\0'; if (!(hp = gethostbyname(proxy))) { faimdprintf(sess, 0, "proxyconnect: unable to resolve proxy name\n"); *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR); return -1; } free(proxy); memset(&sa.sin_zero, 0, 8); sa.sin_port = htons(proxyport); memcpy(&sa.sin_addr, hp->h_addr, hp->h_length); sa.sin_family = hp->h_addrtype; fd = socket(hp->h_addrtype, SOCK_STREAM, 0); if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) { faimdprintf(sess, 0, "proxyconnect: unable to connect to proxy\n"); close(fd); return -1; } i = 0; buf[0] = 0x05; /* SOCKS version 5 */ if (strlen(sess->socksproxy.username)) { buf[1] = 0x02; /* two methods */ buf[2] = 0x00; /* no authentication */ buf[3] = 0x02; /* username/password authentication */ i = 4; } else { buf[1] = 0x01; buf[2] = 0x00; i = 3; } if (write(fd, buf, i) < i) { *statusret = errno; close(fd); return -1; } if (read(fd, buf, 2) < 2) { *statusret = errno; close(fd); return -1; } if ((buf[0] != 0x05) || (buf[1] == 0xff)) { *statusret = EINVAL; close(fd); return -1; } /* check if we're doing username authentication */ if (buf[1] == 0x02) { i = aimutil_put8(buf, 0x01); /* version 1 */ i += aimutil_put8(buf+i, strlen(sess->socksproxy.username)); i += aimutil_putstr(buf+i, sess->socksproxy.username, strlen(sess->socksproxy.username)); i += aimutil_put8(buf+i, strlen(sess->socksproxy.password)); i += aimutil_putstr(buf+i, sess->socksproxy.password, strlen(sess->socksproxy.password)); if (write(fd, buf, i) < i) { *statusret = errno; close(fd); return -1; } if (read(fd, buf, 2) < 2) { *statusret = errno; close(fd); return -1; } if ((buf[0] != 0x01) || (buf[1] != 0x00)) { *statusret = EINVAL; close(fd); return -1; } } i = aimutil_put8(buf, 0x05); i += aimutil_put8(buf+i, 0x01); /* CONNECT */ i += aimutil_put8(buf+i, 0x00); /* reserved */ i += aimutil_put8(buf+i, 0x03); /* address type: host name */ i += aimutil_put8(buf+i, strlen(host)); i += aimutil_putstr(buf+i, host, strlen(host)); i += aimutil_put16(buf+i, port); if (write(fd, buf, i) < i) { *statusret = errno; close(fd); return -1; } if (read(fd, buf, 10) < 10) { *statusret = errno; close(fd); return -1; } if ((buf[0] != 0x05) || (buf[1] != 0x00)) { *statusret = EINVAL; close(fd);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -