]> www.hackdaworld.org Git - my-code/api.git/blob - network/network.c
added dfb api (just starting, not yet working ...)
[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(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<MTU?left:MTU))==-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_send_chan(t_net *net,int chan,unsigned char *data,int datasize) {
269
270   return(network_send(net->connection[chan].fd,data,datasize));
271 }
272
273 int network_receive(int fd,unsigned char *data,int datasize) {
274
275   int count,left;
276
277   count=0;
278   left=datasize;
279
280   while(left) {
281     if((count=read(fd,data+datasize-left,left<MTU?left:MTU))==-1) {
282       perror("[network] read call");
283       return N_ERROR;
284     }
285     left-=count;
286   }
287
288   return datasize;
289 }
290
291 int network_receive_chan(t_net *net,int chan,unsigned char *data,int datasize) {
292
293   return(network_receive(net->connection[chan].fd,data,datasize));
294 }
295
296 int network_udp_listen_init(t_net *net) {
297
298   struct sockaddr_in addr;
299   int true;
300
301   if((net->l_udp_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
302     perror("[network] socket call (udp-receive)");
303     return N_ERROR;
304   }
305
306   memset(&addr,0,sizeof(struct sockaddr));
307   addr.sin_family=AF_INET;
308   addr.sin_port=htons(net->l_udp_port);
309   addr.sin_addr.s_addr=INADDR_ANY;
310   if(bind(net->l_udp_fd,(struct sockaddr *)&addr,sizeof(struct sockaddr))==-1) {
311     /* try harder ... */
312     true=1;
313     if(setsockopt(net->l_udp_fd,SOL_SOCKET,SO_REUSEADDR,
314                   &true,sizeof(true))==-1) {
315       perror("[network] setsockopt call (udp)");
316       return N_ERROR;
317     }
318     dprintf(net->outfd,"[network] reused address (udp)\n");
319   }
320
321   dprintf(net->outfd,"[network] listening on port %d (udp)\n",
322           net->l_udp_port);
323
324   if((net->s_udp_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
325     perror("[network] socket call (udp-send)");
326     return N_ERROR;
327   }
328
329   return N_SUCCESS;
330 }
331
332 int network_udp_receive(t_net *net,int channel, unsigned char *data,int count) {
333
334   struct sockaddr_in addr;
335   socklen_t len;
336
337   if((count=recvfrom(net->l_udp_fd,data,count,0,
338                      (struct sockaddr *)&addr,&len))==-1) {
339     perror("[network] recvfrom call");
340     return N_ERROR;
341   }
342
343   if(strncmp(net->connection[channel].ip,inet_ntoa(addr.sin_addr),IP_DIGITS)) {
344     dprintf(net->outfd,"[network] packet from unknown: %s\n",
345             inet_ntoa(addr.sin_addr));
346     return N_UDP_WRONG_SENDER;
347   }
348
349   return N_SUCCESS;
350 }
351
352 int network_udp_send(t_net *net,int channel, unsigned char *data,int size) {
353
354   int count,left;
355   struct sockaddr_in addr;
356
357   count=0;
358   left=count;
359
360   memset(&addr,0,sizeof(struct sockaddr));
361   addr.sin_family=AF_INET;
362   addr.sin_port=htons(net->l_udp_port);
363   inet_aton(net->connection[channel].ip,&(addr.sin_addr));
364
365   while(left) {
366     if((count=sendto(net->s_udp_fd,data+size-left,left,0,
367                      (struct sockaddr *)&addr,sizeof(struct sockaddr)))==-1) {
368       perror("[network] sendto call");
369       return N_ERROR;
370     }
371     left-=count;
372   }
373
374   return N_SUCCESS;
375 }
376
377 int network_udp_shutdown(t_net *net) {
378   
379   if(close(net->l_udp_fd)==-1) {
380     perror("[network] close call (udp-receive)");
381     return N_ERROR;
382   }
383
384   if(close(net->s_udp_fd)==-1) {
385     perror("[network] close call (udp-send)");
386     return N_ERROR;
387   }
388
389   return N_SUCCESS;
390 }