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