5 * Does all this gloriously nifty connection handling stuff...
12 * Clears out connection list, killing remaining connections.
14 void aim_connrst(struct aim_session_t *sess)
16 faim_mutex_init(&sess->connlistlock, NULL);
18 struct aim_conn_t *cur = sess->connlist, *tmp;
27 sess->connlist = NULL;
32 * Gets a new connection structure.
34 struct aim_conn_t *aim_conn_getnext(struct aim_session_t *sess)
36 struct aim_conn_t *newconn, *cur;
38 if (!(newconn = malloc(sizeof(struct aim_conn_t))))
41 memset(newconn, 0, sizeof(struct aim_conn_t));
42 aim_conn_close(newconn);
45 faim_mutex_lock(&sess->connlistlock);
46 if (sess->connlist == NULL)
47 sess->connlist = newconn;
49 for (cur = sess->connlist; cur->next; cur = cur->next)
53 faim_mutex_unlock(&sess->connlistlock);
58 void aim_conn_kill(struct aim_session_t *sess, struct aim_conn_t **deadconn)
60 struct aim_conn_t *cur;
62 if (!deadconn || !*deadconn)
65 faim_mutex_lock(&sess->connlistlock);
66 if (sess->connlist == NULL)
68 else if (sess->connlist->next == NULL) {
69 if (sess->connlist == *deadconn)
70 sess->connlist = NULL;
74 if (cur->next == *deadconn) {
75 cur->next = cur->next->next;
81 faim_mutex_unlock(&sess->connlistlock);
83 /* XXX: do we need this for txqueue too? */
84 aim_rxqueue_cleanbyconn(sess, *deadconn);
86 aim_conn_close(*deadconn);
93 void aim_conn_close(struct aim_conn_t *deadconn)
95 if (deadconn->fd >= 3)
100 deadconn->lastactivity = 0;
101 deadconn->forcedlatency = 0;
102 if (deadconn->handlerlist)
103 aim_clearhandlers(deadconn);
104 deadconn->handlerlist = NULL;
106 free(deadconn->priv);
107 deadconn->priv = NULL;
108 faim_mutex_init(&deadconn->active, NULL);
109 faim_mutex_init(&deadconn->seqnum_lock, NULL);
112 struct aim_conn_t *aim_getconn_type(struct aim_session_t *sess,
115 struct aim_conn_t *cur;
117 faim_mutex_lock(&sess->connlistlock);
118 for (cur = sess->connlist; cur; cur = cur->next) {
119 if (cur->type == type)
122 faim_mutex_unlock(&sess->connlistlock);
127 * aim_newconn(type, dest)
129 * Opens a new connection to the specified dest host of type type.
131 * TODO: fix for proxies
132 * FIXME: Return errors in a more sane way.
135 struct aim_conn_t *aim_newconn(struct aim_session_t *sess,
136 int type, char *dest)
138 struct aim_conn_t *connstruct;
140 struct sockaddr_in sa;
142 u_short port = FAIM_LOGIN_PORT;
146 if ((connstruct=aim_conn_getnext(sess))==NULL)
149 faim_mutex_lock(&connstruct->active);
151 connstruct->type = type;
153 if (!dest) { /* just allocate a struct */
155 connstruct->status = 0;
156 faim_mutex_unlock(&connstruct->active);
161 * As of 23 Jul 1999, AOL now sends the port number, preceded by a
162 * colon, in the BOS redirect. This fatally breaks all previous
163 * libfaims. Bad, bad AOL.
165 * We put this here to catch every case.
169 for(i=0;i<strlen(dest);i++) {
170 if (dest[i] == ':') {
171 port = atoi(&(dest[i+1]));
175 host = (char *)malloc(i+1);
176 strncpy(host, dest, i);
179 hp = gethostbyname(host);
183 connstruct->status = (h_errno | AIM_CONN_STATUS_RESOLVERR);
184 faim_mutex_unlock(&connstruct->active);
188 memset(&sa.sin_zero, 0, 8);
189 sa.sin_port = htons(port);
190 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
191 sa.sin_family = hp->h_addrtype;
193 connstruct->fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
194 ret = connect(connstruct->fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in));
197 connstruct->status = (errno | AIM_CONN_STATUS_CONNERR);
198 faim_mutex_unlock(&connstruct->active);
202 faim_mutex_unlock(&connstruct->active);
207 int aim_conngetmaxfd(struct aim_session_t *sess)
210 struct aim_conn_t *cur;
212 faim_mutex_lock(&sess->connlistlock);
213 for (cur = sess->connlist; cur; cur = cur->next) {
217 faim_mutex_unlock(&sess->connlistlock);
222 int aim_countconn(struct aim_session_t *sess)
225 struct aim_conn_t *cur;
227 faim_mutex_lock(&sess->connlistlock);
228 for (cur = sess->connlist; cur; cur = cur->next)
230 faim_mutex_unlock(&sess->connlistlock);
236 * aim_select(timeout)
238 * Waits for a socket with data or for timeout, whichever comes first.
241 * Return codes in *status:
242 * -1 error in select() (NULL returned)
243 * 0 no events pending (NULL returned)
244 * 1 outgoing data pending (NULL returned)
245 * 2 incoming data pending (connection with pending data returned)
247 * XXX: we could probably stand to do a little courser locking here.
250 struct aim_conn_t *aim_select(struct aim_session_t *sess,
251 struct timeval *timeout, int *status)
253 struct aim_conn_t *cur;
258 faim_mutex_lock(&sess->connlistlock);
259 if (sess->connlist == NULL) {
260 faim_mutex_unlock(&sess->connlistlock);
264 faim_mutex_unlock(&sess->connlistlock);
267 * If we have data waiting to be sent, return immediatly
269 if (sess->queue_outgoing != NULL) {
277 faim_mutex_lock(&sess->connlistlock);
278 for (cur = sess->connlist; cur; cur = cur->next) {
279 FD_SET(cur->fd, &fds);
283 faim_mutex_unlock(&sess->connlistlock);
285 if ((i = select(maxfd+1, &fds, NULL, NULL, timeout))>=1) {
286 faim_mutex_lock(&sess->connlistlock);
287 for (cur = sess->connlist; cur; cur = cur->next) {
288 if (FD_ISSET(cur->fd, &fds)) {
290 faim_mutex_unlock(&sess->connlistlock);
296 faim_mutex_unlock(&sess->connlistlock);
297 *status = i; /* may be 0 or -1 */
298 return NULL; /* no waiting or error, return */
301 int aim_conn_isready(struct aim_conn_t *conn)
304 return (conn->status & 0x0001);
308 int aim_conn_setstatus(struct aim_conn_t *conn, int status)
315 faim_mutex_lock(&conn->active);
316 val = conn->status ^= status;
317 faim_mutex_unlock(&conn->active);
321 int aim_conn_setlatency(struct aim_conn_t *conn, int newval)
326 faim_mutex_lock(&conn->active);
327 conn->forcedlatency = newval;
328 conn->lastactivity = 0; /* reset this just to make sure */
329 faim_mutex_unlock(&conn->active);
334 void aim_session_init(struct aim_session_t *sess)
339 memset(sess, 0, sizeof(struct aim_session_t));
341 sess->queue_outgoing = NULL;
342 sess->queue_incoming = NULL;
343 sess->pendingjoin = NULL;
344 sess->outstanding_snacs = NULL;
345 sess->snac_nextid = 0x00000001;
348 * This must always be set. Default to the queue-based
349 * version for back-compatibility.
351 sess->tx_enqueue = &aim_tx_enqueue__queuebased;