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