5 * Does all this gloriously nifty connection handling stuff...
13 #include <sys/socket.h>
14 #include <netinet/in.h>
18 * Clears out connection list, killing remaining connections.
20 faim_internal void aim_connrst(struct aim_session_t *sess)
22 faim_mutex_init(&sess->connlistlock);
24 struct aim_conn_t *cur = sess->connlist, *tmp;
33 sess->connlist = NULL;
38 * Gets a new connection structure.
40 faim_internal struct aim_conn_t *aim_conn_getnext(struct aim_session_t *sess)
42 struct aim_conn_t *newconn, *cur;
44 if (!(newconn = malloc(sizeof(struct aim_conn_t))))
47 memset(newconn, 0, sizeof(struct aim_conn_t));
48 aim_conn_close(newconn);
51 faim_mutex_lock(&sess->connlistlock);
52 if (sess->connlist == NULL)
53 sess->connlist = newconn;
55 for (cur = sess->connlist; cur->next; cur = cur->next)
59 faim_mutex_unlock(&sess->connlistlock);
64 static void aim_conn_init(struct aim_conn_t *deadconn)
70 deadconn->subtype = -1;
73 deadconn->lastactivity = 0;
74 deadconn->forcedlatency = 0;
75 deadconn->handlerlist = NULL;
76 deadconn->priv = NULL;
77 faim_mutex_init(&deadconn->active);
78 faim_mutex_init(&deadconn->seqnum_lock);
83 faim_export void aim_conn_kill(struct aim_session_t *sess, struct aim_conn_t **deadconn)
85 struct aim_conn_t *cur;
87 if (!deadconn || !*deadconn)
90 faim_mutex_lock(&sess->connlistlock);
91 if (sess->connlist == NULL)
93 else if (sess->connlist->next == NULL) {
94 if (sess->connlist == *deadconn)
95 sess->connlist = NULL;
99 if (cur->next == *deadconn) {
100 cur->next = cur->next->next;
106 faim_mutex_unlock(&sess->connlistlock);
108 /* XXX: do we need this for txqueue too? */
109 aim_rxqueue_cleanbyconn(sess, *deadconn);
111 aim_conn_close(*deadconn);
112 if ((*deadconn)->priv)
113 free((*deadconn)->priv);
120 faim_export void aim_conn_close(struct aim_conn_t *deadconn)
122 int typesav = -1, subtypesav = -1;
123 void *privsav = NULL;
125 faim_mutex_destroy(&deadconn->active);
126 faim_mutex_destroy(&deadconn->seqnum_lock);
127 if (deadconn->fd >= 3)
129 if (deadconn->handlerlist)
130 aim_clearhandlers(deadconn);
132 typesav = deadconn->type;
133 subtypesav = deadconn->subtype;
135 if (deadconn->priv && (deadconn->type != AIM_CONN_TYPE_RENDEZVOUS)) {
136 free(deadconn->priv);
137 deadconn->priv = NULL;
139 privsav = deadconn->priv;
141 aim_conn_init(deadconn);
143 deadconn->type = typesav;
144 deadconn->subtype = subtypesav;
145 deadconn->priv = privsav;
150 faim_internal struct aim_conn_t *aim_getconn_type(struct aim_session_t *sess,
153 struct aim_conn_t *cur;
155 faim_mutex_lock(&sess->connlistlock);
156 for (cur = sess->connlist; cur; cur = cur->next) {
157 if (cur->type == type)
160 faim_mutex_unlock(&sess->connlistlock);
165 * An extrememly quick and dirty SOCKS5 interface.
167 static int aim_proxyconnect(struct aim_session_t *sess,
168 char *host, unsigned short port,
173 if (strlen(sess->socksproxy.server)) { /* connecting via proxy */
175 unsigned char buf[512];
176 struct sockaddr_in sa;
179 unsigned short proxyport = 1080;
181 for(i=0;i<(int)strlen(sess->socksproxy.server);i++) {
182 if (sess->socksproxy.server[i] == ':') {
183 proxyport = atoi(&(sess->socksproxy.server[i+1]));
187 proxy = (char *)malloc(i+1);
188 strncpy(proxy, sess->socksproxy.server, i);
191 if (!(hp = gethostbyname(proxy))) {
192 printf("proxyconnect: unable to resolve proxy name\n");
193 *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR);
198 memset(&sa.sin_zero, 0, 8);
199 sa.sin_port = htons(proxyport);
200 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
201 sa.sin_family = hp->h_addrtype;
203 fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
204 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
205 printf("proxyconnect: unable to connect to proxy\n");
211 buf[0] = 0x05; /* SOCKS version 5 */
212 if (strlen(sess->socksproxy.username)) {
213 buf[1] = 0x02; /* two methods */
214 buf[2] = 0x00; /* no authentication */
215 buf[3] = 0x02; /* username/password authentication */
223 if (write(fd, buf, i) < i) {
229 if (read(fd, buf, 2) < 2) {
235 if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
241 /* check if we're doing username authentication */
242 if (buf[1] == 0x02) {
243 i = aimutil_put8(buf, 0x01); /* version 1 */
244 i += aimutil_put8(buf+i, strlen(sess->socksproxy.username));
245 i += aimutil_putstr(buf+i, sess->socksproxy.username, strlen(sess->socksproxy.username));
246 i += aimutil_put8(buf+i, strlen(sess->socksproxy.password));
247 i += aimutil_putstr(buf+i, sess->socksproxy.password, strlen(sess->socksproxy.password));
248 if (write(fd, buf, i) < i) {
253 if (read(fd, buf, 2) < 2) {
258 if ((buf[0] != 0x01) || (buf[1] != 0x00)) {
265 i = aimutil_put8(buf, 0x05);
266 i += aimutil_put8(buf+i, 0x01); /* CONNECT */
267 i += aimutil_put8(buf+i, 0x00); /* reserved */
268 i += aimutil_put8(buf+i, 0x03); /* address type: host name */
269 i += aimutil_put8(buf+i, strlen(host));
270 i += aimutil_putstr(buf+i, host, strlen(host));
271 i += aimutil_put16(buf+i, port);
273 if (write(fd, buf, i) < i) {
278 if (read(fd, buf, 10) < 10) {
283 if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
289 } else { /* connecting directly */
290 struct sockaddr_in sa;
293 if (!(hp = gethostbyname(host))) {
294 *statusret = (h_errno | AIM_CONN_STATUS_RESOLVERR);
298 memset(&sa.sin_zero, 0, 8);
299 sa.sin_port = htons(port);
300 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
301 sa.sin_family = hp->h_addrtype;
303 fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
304 if (connect(fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
313 * aim_newconn(type, dest)
315 * Opens a new connection to the specified dest host of type type.
317 * TODO: fix for proxies
318 * FIXME: Return errors in a more sane way.
321 faim_export struct aim_conn_t *aim_newconn(struct aim_session_t *sess,
322 int type, char *dest)
324 struct aim_conn_t *connstruct;
326 struct sockaddr_in sa;
328 u_short port = FAIM_LOGIN_PORT;
332 if ((connstruct=aim_conn_getnext(sess))==NULL)
335 faim_mutex_lock(&connstruct->active);
337 connstruct->type = type;
339 if (!dest) { /* just allocate a struct */
341 connstruct->status = 0;
342 faim_mutex_unlock(&connstruct->active);
347 * As of 23 Jul 1999, AOL now sends the port number, preceded by a
348 * colon, in the BOS redirect. This fatally breaks all previous
349 * libfaims. Bad, bad AOL.
351 * We put this here to catch every case.
355 for(i=0;i<(int)strlen(dest);i++) {
356 if (dest[i] == ':') {
357 port = atoi(&(dest[i+1]));
361 host = (char *)malloc(i+1);
362 strncpy(host, dest, i);
365 if ((ret = aim_proxyconnect(sess, host, port, &connstruct->status)) < 0) {
367 connstruct->status = (errno | AIM_CONN_STATUS_CONNERR);
369 faim_mutex_unlock(&connstruct->active);
372 connstruct->fd = ret;
374 faim_mutex_unlock(&connstruct->active);
381 faim_export int aim_conngetmaxfd(struct aim_session_t *sess)
384 struct aim_conn_t *cur;
386 faim_mutex_lock(&sess->connlistlock);
387 for (cur = sess->connlist; cur; cur = cur->next) {
391 faim_mutex_unlock(&sess->connlistlock);
396 static int aim_countconn(struct aim_session_t *sess)
399 struct aim_conn_t *cur;
401 faim_mutex_lock(&sess->connlistlock);
402 for (cur = sess->connlist; cur; cur = cur->next)
404 faim_mutex_unlock(&sess->connlistlock);
410 * aim_select(timeout)
412 * Waits for a socket with data or for timeout, whichever comes first.
415 * Return codes in *status:
416 * -1 error in select() (NULL returned)
417 * 0 no events pending (NULL returned)
418 * 1 outgoing data pending (NULL returned)
419 * 2 incoming data pending (connection with pending data returned)
421 * XXX: we could probably stand to do a little courser locking here.
424 faim_export struct aim_conn_t *aim_select(struct aim_session_t *sess,
425 struct timeval *timeout, int *status)
427 struct aim_conn_t *cur;
432 faim_mutex_lock(&sess->connlistlock);
433 if (sess->connlist == NULL) {
434 faim_mutex_unlock(&sess->connlistlock);
438 faim_mutex_unlock(&sess->connlistlock);
441 * If we have data waiting to be sent, return immediatly
443 if (sess->queue_outgoing != NULL) {
451 faim_mutex_lock(&sess->connlistlock);
452 for (cur = sess->connlist; cur; cur = cur->next) {
453 FD_SET(cur->fd, &fds);
457 faim_mutex_unlock(&sess->connlistlock);
459 if ((i = select(maxfd+1, &fds, NULL, NULL, timeout))>=1) {
460 faim_mutex_lock(&sess->connlistlock);
461 for (cur = sess->connlist; cur; cur = cur->next) {
462 if (FD_ISSET(cur->fd, &fds)) {
464 faim_mutex_unlock(&sess->connlistlock);
468 *status = 0; /* shouldn't happen */
469 } else if ((i == -1) && (errno == EINTR)) /* treat interrupts as a timeout */
472 *status = i; /* can be 0 or -1 */
474 faim_mutex_unlock(&sess->connlistlock);
475 return NULL; /* no waiting or error, return */
478 faim_export int aim_conn_isready(struct aim_conn_t *conn)
481 return (conn->status & 0x0001);
485 faim_export int aim_conn_setstatus(struct aim_conn_t *conn, int status)
492 faim_mutex_lock(&conn->active);
493 val = conn->status ^= status;
494 faim_mutex_unlock(&conn->active);
498 faim_export int aim_conn_setlatency(struct aim_conn_t *conn, int newval)
503 faim_mutex_lock(&conn->active);
504 conn->forcedlatency = newval;
505 conn->lastactivity = 0; /* reset this just to make sure */
506 faim_mutex_unlock(&conn->active);
512 * Call this with your SOCKS5 proxy server parameters before
513 * the first call to aim_newconn(). If called with all NULL
514 * args, it will clear out a previously set proxy.
516 * Set username and password to NULL if not applicable.
519 faim_export void aim_setupproxy(struct aim_session_t *sess, char *server, char *username, char *password)
521 /* clear out the proxy info */
522 if (!server || !strlen(server)) {
523 memset(sess->socksproxy.server, 0, sizeof(sess->socksproxy.server));
524 memset(sess->socksproxy.username, 0, sizeof(sess->socksproxy.username));
525 memset(sess->socksproxy.password, 0, sizeof(sess->socksproxy.password));
529 strncpy(sess->socksproxy.server, server, sizeof(sess->socksproxy.server));
530 if (username && strlen(username))
531 strncpy(sess->socksproxy.username, username, sizeof(sess->socksproxy.username));
532 if (password && strlen(password))
533 strncpy(sess->socksproxy.password, password, sizeof(sess->socksproxy.password));
537 faim_export void aim_session_init(struct aim_session_t *sess)
542 memset(sess, 0, sizeof(struct aim_session_t));
544 sess->queue_outgoing = NULL;
545 sess->queue_incoming = NULL;
546 sess->pendingjoin = NULL;
547 aim_initsnachash(sess);
548 sess->snac_nextid = 0x00000001;
551 * This must always be set. Default to the queue-based
552 * version for back-compatibility.
554 sess->tx_enqueue = &aim_tx_enqueue__queuebased;