]> www.hackdaworld.org Git - my-code/ivac.git/blob - src/network.c
3293a570adad366fb9f28fdac6ee4ee587e0e235
[my-code/ivac.git] / src / 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) {
10   
11   struct sockaddr_in addr;
12   int true;
13  
14   puts("[network] initializing network ...");
15
16   memset(net->connection,0,MAX_CONNECTIONS*sizeof(t_connection));
17   net->c_count=0;
18   net->sendmask=0;
19
20   if((net->l_fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
21     perror("[network] socket call");
22     return N_ERROR;
23   }
24
25   memset(&addr,0,sizeof(struct sockaddr));
26   addr.sin_family=AF_INET;
27   addr.sin_port=htons(net->l_port);
28   addr.sin_addr.s_addr=INADDR_ANY;
29
30   /* prevent addres in use error message */
31   true=1;
32   if(setsockopt(net->l_fd,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(true))==-1) {
33     perror("[network] setsockopt call");
34     return N_ERROR;
35   }
36
37   if(bind(net->l_fd,(struct sockaddr *)&addr,
38           sizeof(struct sockaddr))==-1) {
39     perror("[network] bind call");
40     return N_ERROR;
41   }
42
43   if(listen(net->l_fd,MAX_LISTEN_QUEUE)==-1) {
44     perror("[network] listen call");
45     return N_ERROR;
46   }
47
48   printf("[network] listen on %s port %d\n",inet_ntoa(addr.sin_addr),
49                                           net->l_port);
50
51   return N_SUCCESS;
52 }
53
54 int network_shutdown(t_net *net) {
55
56   int channel;
57
58   for(channel=0;channel<MAX_CONNECTIONS;channel++)
59     if(net->connection[channel].status&C_SOCKET)
60       close(net->connection[channel].fd);
61
62   if(close(net->l_fd)==-1) {
63     perror("[network] close call");
64     return N_ERROR;
65   }
66
67   puts("[network] shutdown");
68
69   return N_SUCCESS;
70 }
71
72 int network_set_listen_port(t_net *net,in_port_t port) {
73
74   net->l_port=port;
75
76   return N_SUCCESS;
77 }
78
79 int network_manage_connection(t_net *net) {
80
81   int i;
82   struct sockaddr_in addr;
83   
84   for(i=0;i<MAX_CONNECTIONS;i++) {
85
86     if(net->connection[i].status&C_IN_USE) {
87
88       if(net->connection[i].status&C_HANGUP) {
89         if(close(net->connection[i].fd)==-1) {
90           perror("[network] close call");
91           return N_E_CLOSE;
92         }
93         printf("[network] connection %d closed\n",i);
94         net->connection[i].status=0;
95       }
96
97       if(net->connection[i].status&C_INFO_A) {
98
99         if(!(net->connection[i].status&C_SOCKET)) {
100           if((net->connection[i].fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
101             perror("[network] socket call");
102             return N_ERROR;
103           }
104           net->connection[i].status|=C_SOCKET;
105         }
106
107         if((!(net->connection[i].status&C_ESTABL))&&
108            (net->connection[i].status&C_SOCKET)) {
109
110           memset(&addr,0,sizeof(struct sockaddr));
111           addr.sin_family=AF_INET;
112           addr.sin_port=htons(net->connection[i].port);
113           if(!inet_aton(net->connection[i].ip,&(addr.sin_addr))) {
114             perror("[network] inet_aton call");
115             return N_ERROR;
116           }
117           
118           if(connect(net->connection[i].fd,(struct sockaddr *)&addr,
119                      sizeof(struct sockaddr))==-1) {
120             perror("[network] connect call");
121             return N_E_CONNECT;
122           }
123
124           printf("[network] established connection to ");
125           printf("%s port %d on channel %d\n",net->connection[i].ip,
126                                               net->connection[i].port,i);
127           net->connection[i].status|=C_ESTABL;
128
129         }
130
131       }
132
133     }
134
135   }
136
137   return N_SUCCESS;
138 }
139
140 int network_connect(t_net *net,int channel) {
141
142   if(net->connection[channel].status&C_IN_USE) {
143     printf("[network] connect failed, channel %02d in use\n",channel);
144     return N_E_IN_USE;
145   }
146   if(!(net->connection[channel].status&C_INFO_A)) {
147     printf("[network] connect failed, missing configuration for channel %02d",
148            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     printf("[network] close failed, channel %02d not active",channel);
161     return N_E_NC;
162   }
163   
164   net->connection[channel].status|=C_HANGUP;
165   return(network_manage_connection(net)); /* could be other channel too */
166 }
167
168 int network_set_connection_info(t_net *net,int channel,char *ip,int port) {
169
170   if(net->connection[channel].status&C_IN_USE) {
171     printf("[network] set connection failed, channel %02d in use\n",channel);
172     return N_E_IN_USE;
173   }
174
175   strncpy(net->connection[channel].ip,ip,IP_DIGITS);
176   net->connection[channel].port=port;
177   net->connection[channel].status|=C_INFO_A;
178
179   return N_SUCCESS;
180 }
181
182 int network_select(t_net *net,int channel) {
183
184   int mask;
185
186   if(channel==MAX_CONNECTIONS) mask=0xffffffff;
187   else mask=(1<<channel);
188   net->sendmask|=mask;
189
190   return N_SUCCESS;
191 }
192
193 int network_deselect(t_net *net,int channel) {
194
195   int mask;
196
197   if(channel==MAX_CONNECTIONS) mask=0;
198   else mask=~(1<<channel);
199   net->sendmask&=mask;
200
201   return N_SUCCESS;
202 }
203
204 int network_manage_incoming(t_net *net) {
205
206   int channel;
207   struct sockaddr_in addr;
208   int len;
209
210   for(channel=0;channel<MAX_CONNECTIONS;channel++) {
211     if(!(net->connection[channel].status&C_IN_USE)) {
212       if((net->connection[channel].fd=accept(net->l_fd,
213                                        (struct sockaddr *)&addr,
214                                        &len))==-1) {
215         perror("[network] accept call");
216         return N_E_ACCEPT;
217       }
218       strncpy(net->connection[channel].ip,inet_ntoa(addr.sin_addr),IP_DIGITS);
219       net->connection[channel].port=ntohs(addr.sin_port);
220       net->connection[channel].status=C_IN_USE|C_INFO_A|C_SOCKET|C_ESTABL;
221       printf("[network] established connection from %s port %d on channel %d\n",
222              net->connection[channel].ip,net->connection[channel].port,channel);
223       return channel;
224     }
225   }
226
227   puts("[network] maximum connections reached");
228   return N_E_MAXC;
229 }
230  
231 int network_send(int fd,unsigned char *data,int datasize) {
232
233   int count,left;
234
235   count=0;
236   left=datasize;
237
238   while(left) {
239     if((count=write(fd,data+datasize-left,left))==-1) {
240       perror("[network] write call");
241       return N_ERROR;
242     }
243     left-=count;
244   }
245
246   return N_SUCCESS;
247 }
248
249 int network_receive(int fd,unsigned char *data,int datasize) {
250
251   int count;
252
253   if((count=read(fd,data,datasize))==-1) {
254     perror("[network] read call");
255     return N_ERROR;
256   }
257
258   return count;
259 }