2
|
1
|
|
2 /*
|
|
3 * aim_conn.c
|
|
4 *
|
|
5 * Does all this gloriously nifty connection handling stuff...
|
|
6 *
|
|
7 */
|
|
8
|
237
|
9 #include <aim.h>
|
2
|
10
|
237
|
11 void aim_connrst(struct aim_session_t *sess)
|
2
|
12 {
|
|
13 int i;
|
|
14 for (i = 0; i < AIM_CONN_MAX; i++)
|
|
15 {
|
237
|
16 sess->conns[i].fd = -1;
|
|
17 sess->conns[i].type = -1;
|
|
18 sess->conns[i].status = 0;
|
|
19 sess->conns[i].seqnum = 0;
|
|
20 sess->conns[i].lastactivity = 0;
|
|
21 sess->conns[i].forcedlatency = 0;
|
|
22 aim_clearhandlers(&(sess->conns[i]));
|
|
23 sess->conns[i].handlerlist = NULL;
|
2
|
24 }
|
|
25
|
|
26 }
|
|
27
|
237
|
28 struct aim_conn_t *aim_conn_getnext(struct aim_session_t *sess)
|
2
|
29 {
|
|
30 int i;
|
|
31 for (i=0;i<AIM_CONN_MAX;i++)
|
237
|
32 if (sess->conns[i].fd == -1)
|
|
33 return &(sess->conns[i]);
|
2
|
34 return NULL;
|
|
35 }
|
|
36
|
|
37 void aim_conn_close(struct aim_conn_t *deadconn)
|
|
38 {
|
|
39 if (deadconn->fd >= 3)
|
|
40 close(deadconn->fd);
|
|
41 deadconn->fd = -1;
|
|
42 deadconn->type = -1;
|
237
|
43 deadconn->seqnum = 0;
|
|
44 deadconn->lastactivity = 0;
|
|
45 deadconn->forcedlatency = 0;
|
|
46 aim_clearhandlers(deadconn);
|
|
47 deadconn->handlerlist = NULL;
|
|
48 if (deadconn->priv)
|
|
49 free(deadconn->priv);
|
|
50 deadconn->priv = NULL;
|
2
|
51 }
|
|
52
|
237
|
53 struct aim_conn_t *aim_getconn_type(struct aim_session_t *sess,
|
|
54 int type)
|
2
|
55 {
|
|
56 int i;
|
|
57 for (i=0; i<AIM_CONN_MAX; i++)
|
237
|
58 if (sess->conns[i].type == type)
|
|
59 return &(sess->conns[i]);
|
2
|
60 return NULL;
|
|
61 }
|
|
62
|
|
63 /*
|
|
64 * aim_newconn(type, dest)
|
|
65 *
|
|
66 * Opens a new connection to the specified dest host of type type.
|
|
67 *
|
|
68 * TODO: fix for proxies
|
|
69 * FIXME: Return errors in a more sane way.
|
|
70 *
|
|
71 */
|
237
|
72 struct aim_conn_t *aim_newconn(struct aim_session_t *sess,
|
|
73 int type, char *dest)
|
2
|
74 {
|
|
75 struct aim_conn_t *connstruct;
|
|
76 int ret;
|
|
77 struct sockaddr_in sa;
|
|
78 struct hostent *hp;
|
237
|
79 u_short port = FAIM_LOGIN_PORT;
|
|
80 char *host = NULL;
|
2
|
81 int i=0;
|
|
82
|
237
|
83 if ((connstruct=aim_conn_getnext(sess))==NULL)
|
2
|
84 return NULL;
|
|
85
|
|
86 connstruct->type = type;
|
|
87
|
237
|
88 if (!dest) { /* just allocate a struct */
|
|
89 connstruct->fd = -1;
|
|
90 connstruct->status = 0;
|
|
91 return connstruct;
|
|
92 }
|
|
93
|
2
|
94 /*
|
|
95 * As of 23 Jul 1999, AOL now sends the port number, preceded by a
|
|
96 * colon, in the BOS redirect. This fatally breaks all previous
|
|
97 * libfaims. Bad, bad AOL.
|
|
98 *
|
|
99 * We put this here to catch every case.
|
|
100 *
|
|
101 */
|
237
|
102
|
|
103 for(i=0;i<strlen(dest);i++)
|
|
104 {
|
|
105 if (dest[i] == ':') {
|
|
106 port = atoi(&(dest[i+1]));
|
|
107 break;
|
2
|
108 }
|
237
|
109 }
|
|
110 host = (char *)malloc(i+1);
|
|
111 strncpy(host, dest, i);
|
|
112 host[i] = '\0';
|
2
|
113
|
237
|
114 hp = gethostbyname2(host, AF_INET);
|
|
115 free(host);
|
|
116
|
2
|
117 if (hp == NULL)
|
|
118 {
|
|
119 connstruct->status = (h_errno | AIM_CONN_STATUS_RESOLVERR);
|
|
120 return connstruct;
|
|
121 }
|
|
122
|
|
123 memset(&sa.sin_zero, 0, 8);
|
|
124 sa.sin_port = htons(port);
|
|
125 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
|
|
126 sa.sin_family = hp->h_addrtype;
|
|
127
|
|
128 connstruct->fd = socket(hp->h_addrtype, SOCK_STREAM, 0);
|
|
129 ret = connect(connstruct->fd, (struct sockaddr *)&sa, sizeof(struct sockaddr_in));
|
|
130 if( ret < 0)
|
|
131 {
|
|
132 connstruct->fd = -1;
|
|
133 connstruct->status = (errno | AIM_CONN_STATUS_CONNERR);
|
|
134 return connstruct;
|
|
135 }
|
237
|
136
|
2
|
137 return connstruct;
|
|
138 }
|
|
139
|
237
|
140 int aim_conngetmaxfd(struct aim_session_t *sess)
|
2
|
141 {
|
|
142 int i,j;
|
|
143 j=0;
|
|
144 for (i=0;i<AIM_CONN_MAX;i++)
|
237
|
145 if(sess->conns[i].fd > j)
|
|
146 j = sess->conns[i].fd;
|
2
|
147 return j;
|
|
148 }
|
|
149
|
237
|
150 int aim_countconn(struct aim_session_t *sess)
|
2
|
151 {
|
|
152 int i,cnt;
|
|
153 cnt = 0;
|
|
154 for (i=0;i<AIM_CONN_MAX;i++)
|
237
|
155 if (sess->conns[i].fd > -1)
|
2
|
156 cnt++;
|
|
157 return cnt;
|
|
158 }
|
|
159
|
|
160 /*
|
|
161 * aim_select(timeout)
|
|
162 *
|
|
163 * Waits for a socket with data or for timeout, whichever comes first.
|
|
164 * See select(2).
|
|
165 *
|
237
|
166 * Return codes in *status:
|
|
167 * -1 error in select() (NULL returned)
|
|
168 * 0 no events pending (NULL returned)
|
|
169 * 1 outgoing data pending (NULL returned)
|
|
170 * 2 incoming data pending (connection with pending data returned)
|
|
171 *
|
2
|
172 */
|
237
|
173 struct aim_conn_t *aim_select(struct aim_session_t *sess,
|
|
174 struct timeval *timeout, int *status)
|
2
|
175 {
|
|
176 fd_set fds;
|
|
177 int i;
|
|
178
|
237
|
179 if (aim_countconn(sess) <= 0)
|
2
|
180 return 0;
|
237
|
181
|
|
182 /*
|
|
183 * If we have data waiting to be sent, return immediatly
|
|
184 */
|
|
185 if (sess->queue_outgoing != NULL) {
|
|
186 *status = 1;
|
|
187 return NULL;
|
|
188 }
|
|
189
|
2
|
190 FD_ZERO(&fds);
|
|
191
|
|
192 for(i=0;i<AIM_CONN_MAX;i++)
|
237
|
193 if (sess->conns[i].fd>-1)
|
|
194 FD_SET(sess->conns[i].fd, &fds);
|
2
|
195
|
237
|
196 if ((i = select(aim_conngetmaxfd(sess)+1, &fds, NULL, NULL, timeout))>=1) {
|
|
197 int j;
|
|
198 for (j=0;j<AIM_CONN_MAX;j++) {
|
|
199 if (sess->conns[j].fd > -1) {
|
|
200 if ((FD_ISSET(sess->conns[j].fd, &fds))) {
|
|
201 *status = 2;
|
|
202 return &(sess->conns[j]); /* return the first waiting struct */
|
2
|
203 }
|
237
|
204 }
|
|
205 }
|
|
206 /* should never get here */
|
|
207 }
|
|
208
|
|
209 *status = i; /* may be 0 or -1 */
|
|
210 return NULL; /* no waiting or error, return */
|
2
|
211 }
|
|
212
|
|
213 int aim_conn_isready(struct aim_conn_t *conn)
|
|
214 {
|
|
215 if (conn)
|
|
216 return (conn->status & 0x0001);
|
|
217 else
|
|
218 return -1;
|
|
219 }
|
|
220
|
|
221 int aim_conn_setstatus(struct aim_conn_t *conn, int status)
|
|
222 {
|
|
223 if (conn)
|
|
224 return (conn->status ^= status);
|
|
225 else
|
|
226 return -1;
|
|
227 }
|
|
228
|
237
|
229 int aim_conn_setlatency(struct aim_conn_t *conn, int newval)
|
|
230 {
|
|
231 if (!conn)
|
|
232 return -1;
|
|
233
|
|
234 conn->forcedlatency = newval;
|
|
235 conn->lastactivity = 0; /* reset this just to make sure */
|
|
236
|
|
237 return 0;
|
|
238 }
|
|
239
|
|
240 void aim_session_init(struct aim_session_t *sess)
|
|
241 {
|
|
242 int i;
|
|
243
|
|
244 if (!sess)
|
|
245 return;
|
|
246
|
|
247 memset(sess->logininfo.screen_name, 0x00, MAXSNLEN);
|
|
248 sess->logininfo.BOSIP = NULL;
|
|
249 memset(sess->logininfo.cookie, 0x00, AIM_COOKIELEN);
|
|
250 sess->logininfo.email = NULL;
|
|
251 sess->logininfo.regstatus = 0x00;
|
|
252
|
|
253 for (i = 0; i < AIM_CONN_MAX; i++)
|
|
254 {
|
|
255 sess->conns[i].fd = -1;
|
|
256 sess->conns[i].type = -1;
|
|
257 sess->conns[i].status = 0;
|
|
258 sess->conns[i].seqnum = 0;
|
|
259 sess->conns[i].lastactivity = 0;
|
|
260 sess->conns[i].forcedlatency = 0;
|
|
261 sess->conns[i].handlerlist = NULL;
|
|
262 sess->conns[i].priv = NULL;
|
|
263 }
|
|
264
|
|
265 sess->queue_outgoing = NULL;
|
|
266 sess->queue_incoming = NULL;
|
|
267 sess->pendingjoin = NULL;
|
|
268 sess->outstanding_snacs = NULL;
|
|
269 sess->snac_nextid = 0x00000001;
|
|
270
|
|
271 return;
|
|
272 }
|