]> hackdaworld.org Git - my-code/api.git/blob - network/network.c
added list_del_current and network_receive_chan function
[my-code/api.git] / network / network.c
1 /* network.c -- network management stuff
2  *
3  * author: hackbard@hackdaworld.dyndns.org
4  *
5  */
6
7 #include "network.h"
8
9 int network_init(t_net *net,int outfd) {
10
11   dprintf(net->outfd,"[network] initializing network api ...\n");
12
13   memset(net->connection,0,MAX_CONNECTIONS*sizeof(t_connection));
14   net->c_count=0;
15   net->sendmask=0;
16   net->outfd=outfd;
17
18   return N_SUCCESS;
19 }
20
21 int network_listen(t_net *net) {
22   
23   struct sockaddr_in addr;
24   int true;
25
26   if((net->l_fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
27     perror("[network] socket call");
28     return N_ERROR;
29   }
30
31   memset(&addr,0,sizeof(struct sockaddr));
32   addr.sin_family=AF_INET;
33   addr.sin_port=htons(net->l_port);
34   addr.sin_addr.s_addr=INADDR_ANY;
35
36   if(bind(net->l_fd,(struct sockaddr *)&addr,
37           sizeof(struct sockaddr))==-1) {
38     /* try harder ... */
39     true=1;
40     if(setsockopt(net->l_fd,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(true))==-1) {
41       perror("[network] setsockopt call");
42       return N_ERROR;
43     }
44     dprintf(net->outfd,"[network] reused address\n");
45   }
46
47   if(listen(net->l_fd,MAX_LISTEN_QUEUE)==-1) {
48     perror("[network] listen call");
49     return N_ERROR;
50   }
51
52   dprintf(net->outfd,"[network] listen on %s port %d\n",
53           inet_ntoa(addr.sin_addr),net->l_port);
54
55   return N_SUCCESS;
56 }
57
58 int network_shutdown(t_net *net) {
59
60   int channel;
61
62   for(channel=0;channel<MAX_CONNECTIONS;channel++)
63     if(net->connection[channel].status&C_SOCKET)
64       close(net->connection[channel].fd);
65
66   if(close(net->l_fd)==-1) {
67     perror("[network] close call");
68     return N_ERROR;
69   }
70
71   dprintf(net->outfd,"[network] shutdown\n");
72
73   return N_SUCCESS;
74 }
75
76 int network_set_listen_port(t_net *net,in_port_t port) {
77
78   net->l_port=port;
79
80   return N_SUCCESS;
81 }
82
83 int network_manage_connection(t_net *net) {
84
85   int i;
86   struct sockaddr_in addr;
87   
88   for(i=0;i<MAX_CONNECTIONS;i++) {
89
90     if(net->connection[i].status&C_IN_USE) {
91
92       if(net->connection[i].status&C_HANGUP) {
93         if(close(net->connection[i].fd)==-1) {
94           perror("[network] close call");
95           return N_E_CLOSE;
96         }
97         dprintf(net->outfd,"[network] connection %d closed\n",i);
98         net->connection[i].status=0;
99       }
100
101       if(net->connection[i].status&C_INFO_A) {
102
103         if(!(net->connection[i].status&C_SOCKET)) {
104           if((net->connection[i].fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
105             perror("[network] socket call");
106             return N_ERROR;
107           }
108           net->connection[i].status|=C_SOCKET;
109         }
110
111         if((!(net->connection[i].status&C_ESTABL))&&
112            (net->connection[i].status&C_SOCKET)) {
113
114           memset(&addr,0,sizeof(struct sockaddr));
115           addr.sin_family=AF_INET;
116           addr.sin_port=htons(net->connection[i].port);
117           if(!inet_aton(net->connection[i].ip,&(addr.sin_addr))) {
118             perror("[network] inet_aton call");
119             return N_ERROR;
120           }
121           
122           if(connect(net->connection[i].fd,(struct sockaddr *)&addr,
123                      sizeof(struct sockaddr))==-1) {
124             perror("[network] connect call");
125             return N_E_CONNECT;
126           }
127
128           dprintf(net->outfd,"[network] established connection to ");
129           dprintf(net->outfd,"%s port %d on channel %d\n",
130                   net->connection[i].ip,net->connection[i].port,i);
131           net->connection[i].status|=C_ESTABL;
132
133         }
134
135       }
136
137     }
138
139   }
140
141   return N_SUCCESS;
142 }
143
144 int network_connect(t_net *net,int channel) {
145
146   if(net->connection[channel].status&C_IN_USE) {
147     dprintf(net->outfd,"[network] connect failed, channel %02d in use\n",
148             channel);
149     return N_E_IN_USE;
150   }
151   if(!(net->connection[channel].status&C_INFO_A)) {
152     dprintf(net->outfd,
153             "[network] connect failed, missing config for chan %02d\n",channel);
154     return N_E_NO_INFO;
155   }
156
157   /* try connect & return result */
158   net->connection[channel].status|=C_IN_USE;
159   return(network_manage_connection(net)); /* could be other channel too */
160 }
161
162 int network_close(t_net *net,int channel) {
163
164   if(!(net->connection[channel].status&C_ESTABL)) {
165     dprintf(net->outfd,"[network] close failed, channel %02d not active\n",
166             channel);
167     return N_E_NC;
168   }
169   
170   net->connection[channel].status|=C_HANGUP;
171   return(network_manage_connection(net)); /* could be other channel too */
172 }
173
174 int network_close_all(t_net *net) {
175
176   int channel;
177
178   for(channel=0;channel<MAX_CONNECTIONS;channel++)
179     if(net->connection[channel].status&C_ESTABL) network_close(net,channel);
180
181   return N_SUCCESS;
182 }
183
184 int network_set_connection_info(t_net *net,int channel,char *ip,int port) {
185
186   if(net->connection[channel].status&C_IN_USE) {
187     dprintf(net->outfd,
188             "[network] set connection failed, channel %02d in use\n",channel);
189     return N_E_IN_USE;
190   }
191
192   strncpy(net->connection[channel].ip,ip,IP_DIGITS);
193   net->connection[channel].port=port;
194   net->connection[channel].status|=C_INFO_A;
195
196   return N_SUCCESS;
197 }
198
199 int network_select(t_net *net,int channel) {
200
201   int mask;
202
203   if(channel==MAX_CONNECTIONS) mask=0xffffffff;
204   else mask=(1<<channel);
205   net->sendmask|=mask;
206
207   return N_SUCCESS;
208 }
209
210 int network_deselect(t_net *net,int channel) {
211
212   int mask;
213
214   if(channel==MAX_CONNECTIONS) mask=0;
215   else mask=~(1<<channel);
216   net->sendmask&=mask;
217
218   return N_SUCCESS;
219 }
220
221 int network_manage_incoming(t_net *net) {
222
223   int channel;
224   struct sockaddr_in addr;
225   int len;
226
227   for(channel=0;channel<MAX_CONNECTIONS;channel++) {
228     if(!(net->connection[channel].status&C_IN_USE)) {
229       if((net->connection[channel].fd=accept(net->l_fd,
230                                        (struct sockaddr *)&addr,
231                                        &len))==-1) {
232         perror("[network] accept call");
233         return N_E_ACCEPT;
234       }
235       strncpy(net->connection[channel].ip,inet_ntoa(addr.sin_addr),IP_DIGITS);
236       net->connection[channel].port=ntohs(addr.sin_port);
237       net->connection[channel].status=C_IN_USE|C_INFO_A|C_SOCKET|C_ESTABL;
238       dprintf(net->outfd,
239               "[network] established connection from %s:%d, channel %d\n",
240               net->connection[channel].ip,net->connection[channel].port,
241               channel);
242       return channel;
243     }
244   }
245
246   dprintf(net->outfd,"[network] maximum connections reached\n");
247   return N_E_MAXC;
248 }
249  
250 int network_send(int fd,unsigned char *data,int datasize) {
251
252   int count,left;
253
254   count=0;
255   left=datasize;
256
257   while(left) {
258     if((count=write(fd,data+datasize-left,left))==-1) {
259       perror("[network] write call");
260       return N_ERROR;
261     }
262     left-=count;
263   }
264
265   return N_SUCCESS;
266 }
267
268 int network_receive(int fd,unsigned char *data,int datasize) {
269
270   int count,left;
271
272   count=0;
273   left=datasize;
274
275   while(left) {
276     if((count=read(fd,data,datasize))==-1) {
277       perror("[network] read call");
278       return N_ERROR;
279     }
280     left-=count;
281   }
282
283   return datasize;
284 }
285
286 int network_receive_chan(t_net *net,int chan,unsigned char *data,int datasize) {
287
288   return(network_receive(net->connection[chan].fd,data,datasize));
289 }
290
291 int network_udp_listen_init(t_net *net) {
292
293   struct sockaddr_in addr;
294   int true;
295
296   if((net->l_udp_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
297     perror("[network] socket call (udp-receive)");
298     return N_ERROR;
299   }
300
301   memset(&addr,0,sizeof(struct sockaddr));
302   addr.sin_family=AF_INET;
303   addr.sin_port=htons(net->l_udp_port);
304   addr.sin_addr.s_addr=INADDR_ANY;
305   if(bind(net->l_udp_fd,(struct sockaddr *)&addr,sizeof(struct sockaddr))==-1) {
306     /* try harder ... */
307     true=1;
308     if(setsockopt(net->l_udp_fd,SOL_SOCKET,SO_REUSEADDR,
309                   &true,sizeof(true))==-1) {
310       perror("[network] setsockopt call (udp)");
311       return N_ERROR;
312     }
313     dprintf(net->outfd,"[network] reused address (udp)\n");
314   }
315
316   dprintf(net->outfd,"[network] listening on port %d (udp)\n",
317           net->l_udp_port);
318
319   if((net->s_udp_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
320     perror("[network] socket call (udp-send)");
321     return N_ERROR;
322   }
323
324   return N_SUCCESS;
325 }
326
327 int network_udp_receive(t_net *net,int channel, unsigned char *data,int count) {
328
329   struct sockaddr_in addr;
330   socklen_t len;
331
332   if((count=recvfrom(net->l_udp_fd,data,count,0,
333                      (struct sockaddr *)&addr,&len))==-1) {
334     perror("[network] recvfrom call");
335     return N_ERROR;
336   }
337
338   if(strncmp(net->connection[channel].ip,inet_ntoa(addr.sin_addr),IP_DIGITS)) {
339     dprintf(net->outfd,"[network] packet from unknown: %s\n",
340             inet_ntoa(addr.sin_addr));
341     return N_UDP_WRONG_SENDER;
342   }
343
344   return N_SUCCESS;
345 }
346
347 int network_udp_send(t_net *net,int channel, unsigned char *data,int size) {
348
349   int count,left;
350   struct sockaddr_in addr;
351
352   count=0;
353   left=count;
354
355   memset(&addr,0,sizeof(struct sockaddr));
356   addr.sin_family=AF_INET;
357   addr.sin_port=htons(net->l_udp_port);
358   inet_aton(net->connection[channel].ip,&(addr.sin_addr));
359
360   while(left) {
361     if((count=sendto(net->s_udp_fd,data+size-left,left,0,
362                      (struct sockaddr *)&addr,sizeof(struct sockaddr)))==-1) {
363       perror("[network] sendto call");
364       return N_ERROR;
365     }
366     left-=count;
367   }
368
369   return N_SUCCESS;
370 }
371
372 int network_udp_shutdown(t_net *net) {
373   
374   if(close(net->l_udp_fd)==-1) {
375     perror("[network] close call (udp-receive)");
376     return N_ERROR;
377   }
378
379   if(close(net->s_udp_fd)==-1) {
380     perror("[network] close call (udp-send)");
381     return N_ERROR;
382   }
383
384   return N_SUCCESS;
385 }