]>
Commit | Line | Data |
---|---|---|
9de3ca7e | 1 | |
2 | /* | |
3 | * aim_conn.c | |
4 | * | |
5 | * Does all this gloriously nifty connection handling stuff... | |
6 | * | |
7 | */ | |
8 | ||
a25832e6 | 9 | #include <faim/aim.h> |
9de3ca7e | 10 | |
040457cc | 11 | /* |
12 | * Clears out connection list, killing remaining connections. | |
13 | */ | |
a25832e6 | 14 | void aim_connrst(struct aim_session_t *sess) |
9de3ca7e | 15 | { |
040457cc | 16 | faim_mutex_init(&sess->connlistlock, NULL); |
17 | if (sess->connlist) { | |
18 | struct aim_conn_t *cur = sess->connlist, *tmp; | |
19 | ||
20 | while(cur) { | |
21 | tmp = cur->next; | |
22 | aim_conn_close(cur); | |
23 | free(cur); | |
24 | cur = tmp; | |
25 | } | |
e88ba395 | 26 | } |
040457cc | 27 | sess->connlist = NULL; |
28 | return; | |
9de3ca7e | 29 | } |
30 | ||
040457cc | 31 | /* |
32 | * Gets a new connection structure. | |
33 | */ | |
a25832e6 | 34 | struct aim_conn_t *aim_conn_getnext(struct aim_session_t *sess) |
9de3ca7e | 35 | { |
040457cc | 36 | struct aim_conn_t *newconn, *cur; |
37 | ||
38 | if (!(newconn = malloc(sizeof(struct aim_conn_t)))) | |
39 | return NULL; | |
40 | ||
41 | memset(newconn, 0, sizeof(struct aim_conn_t)); | |
42 | aim_conn_close(newconn); | |
43 | newconn->next = NULL; | |
44 | ||
45 | faim_mutex_lock(&sess->connlistlock); | |
46 | if (sess->connlist == NULL) | |
47 | sess->connlist = newconn; | |
48 | else { | |
49 | for (cur = sess->connlist; cur->next; cur = cur->next) | |
50 | ; | |
51 | cur->next = newconn; | |
52 | } | |
53 | faim_mutex_unlock(&sess->connlistlock); | |
54 | ||
55 | return newconn; | |
56 | } | |
57 | ||
4d7c1bfb | 58 | static void aim_conn_init(struct aim_conn_t *deadconn) |
59 | { | |
60 | if (!deadconn) | |
61 | return; | |
62 | ||
63 | deadconn->fd = -1; | |
64 | deadconn->type = -1; | |
65 | deadconn->seqnum = 0; | |
66 | deadconn->lastactivity = 0; | |
67 | deadconn->forcedlatency = 0; | |
68 | deadconn->handlerlist = NULL; | |
69 | deadconn->priv = NULL; | |
70 | faim_mutex_init(&deadconn->active, NULL); | |
71 | faim_mutex_init(&deadconn->seqnum_lock, NULL); | |
72 | ||
73 | return; | |
74 | } | |
75 | ||
040457cc | 76 | void aim_conn_kill(struct aim_session_t *sess, struct aim_conn_t **deadconn) |
77 | { | |
78 | struct aim_conn_t *cur; | |
79 | ||
80 | if (!deadconn || !*deadconn) | |
81 | return; | |
82 | ||
83 | faim_mutex_lock(&sess->connlistlock); | |
84 | if (sess->connlist == NULL) | |
85 | ; | |
86 | else if (sess->connlist->next == NULL) { | |
87 | if (sess->connlist == *deadconn) | |
88 | sess->connlist = NULL; | |
89 | } else { | |
90 | cur = sess->connlist; | |
91 | while (cur->next) { | |
92 | if (cur->next == *deadconn) { | |
93 | cur->next = cur->next->next; | |
94 | break; | |
95 | } | |
96 | cur = cur->next; | |
97 | } | |
98 | } | |
99 | faim_mutex_unlock(&sess->connlistlock); | |
100 | ||
68ac63c2 | 101 | /* XXX: do we need this for txqueue too? */ |
102 | aim_rxqueue_cleanbyconn(sess, *deadconn); | |
103 | ||
4d7c1bfb | 104 | aim_conn_init(*deadconn); |
040457cc | 105 | free(*deadconn); |
106 | deadconn = NULL; | |
107 | ||
108 | return; | |
9de3ca7e | 109 | } |
110 | ||
111 | void aim_conn_close(struct aim_conn_t *deadconn) | |
112 | { | |
4d7c1bfb | 113 | int typesav = -1; |
114 | ||
115 | faim_mutex_destroy(&deadconn->active); | |
116 | faim_mutex_destroy(&deadconn->seqnum_lock); | |
9de3ca7e | 117 | if (deadconn->fd >= 3) |
118 | close(deadconn->fd); | |
4d7c1bfb | 119 | typesav = deadconn->type; |
0252e8c0 | 120 | if (deadconn->handlerlist) |
121 | aim_clearhandlers(deadconn); | |
0c20631f | 122 | if (deadconn->priv) |
123 | free(deadconn->priv); | |
4d7c1bfb | 124 | |
125 | aim_conn_init(deadconn); | |
126 | ||
127 | deadconn->type = typesav; | |
128 | ||
129 | return; | |
9de3ca7e | 130 | } |
131 | ||
a25832e6 | 132 | struct aim_conn_t *aim_getconn_type(struct aim_session_t *sess, |
133 | int type) | |
9de3ca7e | 134 | { |
040457cc | 135 | struct aim_conn_t *cur; |
136 | ||
137 | faim_mutex_lock(&sess->connlistlock); | |
138 | for (cur = sess->connlist; cur; cur = cur->next) { | |
139 | if (cur->type == type) | |
140 | break; | |
141 | } | |
142 | faim_mutex_unlock(&sess->connlistlock); | |
143 | return cur; | |
9de3ca7e | 144 | } |
145 | ||
146 | /* | |
147 | * aim_newconn(type, dest) | |
148 | * | |
149 | * Opens a new connection to the specified dest host of type type. | |
150 | * | |
151 | * TODO: fix for proxies | |
152 | * FIXME: Return errors in a more sane way. | |
153 | * | |
154 | */ | |
a25832e6 | 155 | struct aim_conn_t *aim_newconn(struct aim_session_t *sess, |
156 | int type, char *dest) | |
9de3ca7e | 157 | { |
158 | struct aim_conn_t *connstruct; | |
159 | int ret; | |
160 | struct sockaddr_in sa; | |
161 | struct hostent *hp; | |
162 | u_short port = FAIM_LOGIN_PORT; | |
a25832e6 | 163 | char *host = NULL; |
9de3ca7e | 164 | int i=0; |
040457cc | 165 | |
e6b05d80 | 166 | if ((connstruct=aim_conn_getnext(sess))==NULL) |
9de3ca7e | 167 | return NULL; |
168 | ||
040457cc | 169 | faim_mutex_lock(&connstruct->active); |
170 | ||
9de3ca7e | 171 | connstruct->type = type; |
172 | ||
e6b05d80 | 173 | if (!dest) { /* just allocate a struct */ |
174 | connstruct->fd = -1; | |
175 | connstruct->status = 0; | |
040457cc | 176 | faim_mutex_unlock(&connstruct->active); |
e6b05d80 | 177 | return connstruct; |
178 | } | |
179 | ||
9de3ca7e | 180 | /* |
181 | * As of 23 Jul 1999, AOL now sends the port number, preceded by a | |
182 | * colon, in the BOS redirect. This fatally breaks all previous | |
183 | * libfaims. Bad, bad AOL. | |
184 | * | |
185 | * We put this here to catch every case. | |
186 | * | |
187 | */ | |
188 | ||
e88ba395 | 189 | for(i=0;i<strlen(dest);i++) { |
190 | if (dest[i] == ':') { | |
191 | port = atoi(&(dest[i+1])); | |
192 | break; | |
9de3ca7e | 193 | } |
e88ba395 | 194 | } |
a25832e6 | 195 | host = (char *)malloc(i+1); |
196 | strncpy(host, dest, i); | |
9ba272ca | 197 | host[i] = '\0'; |
198 | ||
040457cc | 199 | hp = gethostbyname(host); |
a25832e6 | 200 | free(host); |
9de3ca7e | 201 | |
e88ba395 | 202 | if (hp == NULL) { |
203 | connstruct->status = (h_errno | AIM_CONN_STATUS_RESOLVERR); | |
040457cc | 204 | faim_mutex_unlock(&connstruct->active); |
e88ba395 | 205 | return connstruct; |
206 | } | |
9de3ca7e | 207 | |
208 | memset(&sa.sin_zero, 0, 8); | |
209 | sa.sin_port = htons(port); | |
210 | memcpy(&sa.sin_addr, hp->h_addr, hp->h_length); | |
211 | sa.sin_family = hp->h_addrtype; | |
212 | ||
213 | connstruct->fd = socket(hp->h_addrtype, SOCK_STREAM, 0); | |
214 | ret = connect(connstruct->fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)); | |
e88ba395 | 215 | if(ret < 0) { |
216 | connstruct->fd = -1; | |
217 | connstruct->status = (errno | AIM_CONN_STATUS_CONNERR); | |
040457cc | 218 | faim_mutex_unlock(&connstruct->active); |
e88ba395 | 219 | return connstruct; |
220 | } | |
a25832e6 | 221 | |
040457cc | 222 | faim_mutex_unlock(&connstruct->active); |
223 | ||
9de3ca7e | 224 | return connstruct; |
225 | } | |
226 | ||
a25832e6 | 227 | int aim_conngetmaxfd(struct aim_session_t *sess) |
9de3ca7e | 228 | { |
040457cc | 229 | int j = 0; |
230 | struct aim_conn_t *cur; | |
231 | ||
232 | faim_mutex_lock(&sess->connlistlock); | |
233 | for (cur = sess->connlist; cur; cur = cur->next) { | |
234 | if (cur->fd > j) | |
235 | j = cur->fd; | |
236 | } | |
237 | faim_mutex_unlock(&sess->connlistlock); | |
e88ba395 | 238 | |
9de3ca7e | 239 | return j; |
240 | } | |
241 | ||
a25832e6 | 242 | int aim_countconn(struct aim_session_t *sess) |
9de3ca7e | 243 | { |
040457cc | 244 | int cnt = 0; |
245 | struct aim_conn_t *cur; | |
246 | ||
247 | faim_mutex_lock(&sess->connlistlock); | |
248 | for (cur = sess->connlist; cur; cur = cur->next) | |
249 | cnt++; | |
250 | faim_mutex_unlock(&sess->connlistlock); | |
e88ba395 | 251 | |
9de3ca7e | 252 | return cnt; |
253 | } | |
254 | ||
255 | /* | |
256 | * aim_select(timeout) | |
257 | * | |
258 | * Waits for a socket with data or for timeout, whichever comes first. | |
259 | * See select(2). | |
260 | * | |
b8d0da45 | 261 | * Return codes in *status: |
262 | * -1 error in select() (NULL returned) | |
263 | * 0 no events pending (NULL returned) | |
264 | * 1 outgoing data pending (NULL returned) | |
265 | * 2 incoming data pending (connection with pending data returned) | |
266 | * | |
040457cc | 267 | * XXX: we could probably stand to do a little courser locking here. |
268 | * | |
9de3ca7e | 269 | */ |
a25832e6 | 270 | struct aim_conn_t *aim_select(struct aim_session_t *sess, |
b8d0da45 | 271 | struct timeval *timeout, int *status) |
9de3ca7e | 272 | { |
040457cc | 273 | struct aim_conn_t *cur; |
9de3ca7e | 274 | fd_set fds; |
040457cc | 275 | int maxfd = 0; |
9de3ca7e | 276 | int i; |
277 | ||
040457cc | 278 | faim_mutex_lock(&sess->connlistlock); |
279 | if (sess->connlist == NULL) { | |
280 | faim_mutex_unlock(&sess->connlistlock); | |
68ac63c2 | 281 | *status = -1; |
282 | return NULL; | |
040457cc | 283 | } |
284 | faim_mutex_unlock(&sess->connlistlock); | |
f1a5efe0 | 285 | |
9de3ca7e | 286 | /* |
287 | * If we have data waiting to be sent, return immediatly | |
288 | */ | |
b8d0da45 | 289 | if (sess->queue_outgoing != NULL) { |
290 | *status = 1; | |
291 | return NULL; | |
292 | } | |
9de3ca7e | 293 | |
294 | FD_ZERO(&fds); | |
040457cc | 295 | maxfd = 0; |
296 | ||
297 | faim_mutex_lock(&sess->connlistlock); | |
298 | for (cur = sess->connlist; cur; cur = cur->next) { | |
299 | FD_SET(cur->fd, &fds); | |
300 | if (cur->fd > maxfd) | |
301 | maxfd = cur->fd; | |
302 | } | |
303 | faim_mutex_unlock(&sess->connlistlock); | |
304 | ||
305 | if ((i = select(maxfd+1, &fds, NULL, NULL, timeout))>=1) { | |
306 | faim_mutex_lock(&sess->connlistlock); | |
307 | for (cur = sess->connlist; cur; cur = cur->next) { | |
308 | if (FD_ISSET(cur->fd, &fds)) { | |
309 | *status = 2; | |
310 | faim_mutex_unlock(&sess->connlistlock); | |
311 | return cur; | |
312 | } | |
313 | } | |
b8d0da45 | 314 | } |
315 | ||
040457cc | 316 | faim_mutex_unlock(&sess->connlistlock); |
b8d0da45 | 317 | *status = i; /* may be 0 or -1 */ |
318 | return NULL; /* no waiting or error, return */ | |
9de3ca7e | 319 | } |
320 | ||
321 | int aim_conn_isready(struct aim_conn_t *conn) | |
322 | { | |
323 | if (conn) | |
324 | return (conn->status & 0x0001); | |
040457cc | 325 | return -1; |
9de3ca7e | 326 | } |
327 | ||
328 | int aim_conn_setstatus(struct aim_conn_t *conn, int status) | |
329 | { | |
040457cc | 330 | int val; |
331 | ||
332 | if (!conn) | |
9de3ca7e | 333 | return -1; |
040457cc | 334 | |
335 | faim_mutex_lock(&conn->active); | |
336 | val = conn->status ^= status; | |
337 | faim_mutex_unlock(&conn->active); | |
338 | return val; | |
9de3ca7e | 339 | } |
340 | ||
341 | int aim_conn_setlatency(struct aim_conn_t *conn, int newval) | |
342 | { | |
343 | if (!conn) | |
344 | return -1; | |
040457cc | 345 | |
346 | faim_mutex_lock(&conn->active); | |
9de3ca7e | 347 | conn->forcedlatency = newval; |
348 | conn->lastactivity = 0; /* reset this just to make sure */ | |
040457cc | 349 | faim_mutex_unlock(&conn->active); |
9de3ca7e | 350 | |
351 | return 0; | |
352 | } | |
a25832e6 | 353 | |
354 | void aim_session_init(struct aim_session_t *sess) | |
355 | { | |
a25832e6 | 356 | if (!sess) |
357 | return; | |
358 | ||
040457cc | 359 | memset(sess, 0, sizeof(struct aim_session_t)); |
e88ba395 | 360 | aim_connrst(sess); |
a25832e6 | 361 | sess->queue_outgoing = NULL; |
362 | sess->queue_incoming = NULL; | |
0c20631f | 363 | sess->pendingjoin = NULL; |
a25832e6 | 364 | sess->outstanding_snacs = NULL; |
365 | sess->snac_nextid = 0x00000001; | |
366 | ||
e88ba395 | 367 | /* |
368 | * This must always be set. Default to the queue-based | |
369 | * version for back-compatibility. | |
370 | */ | |
371 | sess->tx_enqueue = &aim_tx_enqueue__queuebased; | |
372 | ||
a25832e6 | 373 | return; |
374 | } |