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