5 * Does all this gloriously nifty connection handling stuff...
11 void aim_connrst(struct aim_session_t *sess)
14 for (i = 0; i < AIM_CONN_MAX; i++) {
15 aim_conn_close(&sess->conns[i]);
19 struct aim_conn_t *aim_conn_getnext(struct aim_session_t *sess)
22 for (i=0;i<AIM_CONN_MAX;i++)
23 if (sess->conns[i].fd == -1)
24 return &(sess->conns[i]);
28 void aim_conn_close(struct aim_conn_t *deadconn)
30 if (deadconn->fd >= 3)
35 deadconn->lastactivity = 0;
36 deadconn->forcedlatency = 0;
37 aim_clearhandlers(deadconn);
38 deadconn->handlerlist = NULL;
41 deadconn->priv = NULL;
42 faim_mutex_init(&deadconn->active, NULL);
43 faim_mutex_init(&deadconn->seqnum_lock, NULL);
46 struct aim_conn_t *aim_getconn_type(struct aim_session_t *sess,
50 for (i=0; i<AIM_CONN_MAX; i++)
51 if (sess->conns[i].type == type)
52 return &(sess->conns[i]);
57 * aim_newconn(type, dest)
59 * Opens a new connection to the specified dest host of type type.
61 * TODO: fix for proxies
62 * FIXME: Return errors in a more sane way.
65 struct aim_conn_t *aim_newconn(struct aim_session_t *sess,
68 struct aim_conn_t *connstruct;
70 struct sockaddr_in sa;
72 u_short port = FAIM_LOGIN_PORT;
76 if ((connstruct=aim_conn_getnext(sess))==NULL)
79 connstruct->type = type;
81 if (!dest) { /* just allocate a struct */
83 connstruct->status = 0;
88 * As of 23 Jul 1999, AOL now sends the port number, preceded by a
89 * colon, in the BOS redirect. This fatally breaks all previous
90 * libfaims. Bad, bad AOL.
92 * We put this here to catch every case.
96 for(i=0;i<strlen(dest);i++) {
98 port = atoi(&(dest[i+1]));
102 host = (char *)malloc(i+1);
103 strncpy(host, dest, i);
106 hp = gethostbyname2(host, AF_INET);
110 connstruct->status = (h_errno | AIM_CONN_STATUS_RESOLVERR);
114 memset(&sa.sin_zero, 0, 8);
115 sa.sin_port = htons(port);
116 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
117 sa.sin_family = hp->h_addrtype;
119 connstruct->fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
120 ret = connect(connstruct->fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in));
123 connstruct->status = (errno | AIM_CONN_STATUS_CONNERR);
130 int aim_conngetmaxfd(struct aim_session_t *sess)
134 for (i=0,j=0;i<AIM_CONN_MAX;i++)
135 if(sess->conns[i].fd > j)
136 j = sess->conns[i].fd;
140 int aim_countconn(struct aim_session_t *sess)
144 for (i=0,cnt=0;i<AIM_CONN_MAX;i++)
145 if (sess->conns[i].fd > -1)
151 * aim_select(timeout)
153 * Waits for a socket with data or for timeout, whichever comes first.
156 * Return codes in *status:
157 * -1 error in select() (NULL returned)
158 * 0 no events pending (NULL returned)
159 * 1 outgoing data pending (NULL returned)
160 * 2 incoming data pending (connection with pending data returned)
163 struct aim_conn_t *aim_select(struct aim_session_t *sess,
164 struct timeval *timeout, int *status)
169 if (aim_countconn(sess) <= 0)
173 * If we have data waiting to be sent, return immediatly
175 if (sess->queue_outgoing != NULL) {
182 for(i=0;i<AIM_CONN_MAX;i++)
183 if (sess->conns[i].fd>-1)
184 FD_SET(sess->conns[i].fd, &fds);
186 if ((i = select(aim_conngetmaxfd(sess)+1, &fds, NULL, NULL, timeout))>=1) {
188 for (j=0;j<AIM_CONN_MAX;j++) {
189 if (sess->conns[j].fd > -1) {
190 if ((FD_ISSET(sess->conns[j].fd, &fds))) {
192 return &(sess->conns[j]); /* return the first waiting struct */
196 /* should never get here */
199 *status = i; /* may be 0 or -1 */
200 return NULL; /* no waiting or error, return */
203 int aim_conn_isready(struct aim_conn_t *conn)
206 return (conn->status & 0x0001);
211 int aim_conn_setstatus(struct aim_conn_t *conn, int status)
214 return (conn->status ^= status);
219 int aim_conn_setlatency(struct aim_conn_t *conn, int newval)
224 conn->forcedlatency = newval;
225 conn->lastactivity = 0; /* reset this just to make sure */
230 void aim_session_init(struct aim_session_t *sess)
235 memset(sess->logininfo.screen_name, 0x00, MAXSNLEN);
236 sess->logininfo.BOSIP = NULL;
237 memset(sess->logininfo.cookie, 0x00, AIM_COOKIELEN);
238 sess->logininfo.email = NULL;
239 sess->logininfo.regstatus = 0x00;
243 sess->queue_outgoing = NULL;
244 sess->queue_incoming = NULL;
245 sess->pendingjoin = NULL;
246 sess->outstanding_snacs = NULL;
247 sess->snac_nextid = 0x00000001;
250 * This must always be set. Default to the queue-based
251 * version for back-compatibility.
253 sess->tx_enqueue = &aim_tx_enqueue__queuebased;