1 /* network.c -- network management stuff
3 * author: hackbard@hackdaworld.dyndns.org
9 int network_init(t_net *net,int outfd) {
11 dprintf(outfd,"[network] initializing network api ...\n");
13 memset(net->connection,0,MAX_CONNECTIONS*sizeof(t_connection));
21 int network_listen(t_net *net) {
23 struct sockaddr_in addr;
26 if((net->l_fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
27 perror("[network] socket call");
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;
36 if(bind(net->l_fd,(struct sockaddr *)&addr,
37 sizeof(struct sockaddr))==-1) {
40 if(setsockopt(net->l_fd,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(true))==-1) {
41 perror("[network] setsockopt call");
44 dprintf(net->outfd,"[network] reused address\n");
47 if(listen(net->l_fd,MAX_LISTEN_QUEUE)==-1) {
48 perror("[network] listen call");
52 dprintf(net->outfd,"[network] listen on %s port %d\n",
53 inet_ntoa(addr.sin_addr),net->l_port);
58 int network_shutdown(t_net *net) {
62 for(channel=0;channel<MAX_CONNECTIONS;channel++)
63 if(net->connection[channel].status&C_SOCKET)
64 close(net->connection[channel].fd);
66 if(close(net->l_fd)==-1) {
67 perror("[network] close call");
71 dprintf(net->outfd,"[network] shutdown\n");
76 int network_set_listen_port(t_net *net,in_port_t port) {
83 int network_manage_connection(t_net *net) {
86 struct sockaddr_in addr;
88 for(i=0;i<MAX_CONNECTIONS;i++) {
90 if(net->connection[i].status&C_IN_USE) {
92 if(net->connection[i].status&C_HANGUP) {
93 if(close(net->connection[i].fd)==-1) {
94 perror("[network] close call");
97 dprintf(net->outfd,"[network] connection %d closed\n",i);
98 net->connection[i].status=0;
101 if(net->connection[i].status&C_INFO_A) {
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");
108 net->connection[i].status|=C_SOCKET;
111 if((!(net->connection[i].status&C_ESTABL))&&
112 (net->connection[i].status&C_SOCKET)) {
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");
122 if(connect(net->connection[i].fd,(struct sockaddr *)&addr,
123 sizeof(struct sockaddr))==-1) {
124 perror("[network] connect call");
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;
144 int network_connect(t_net *net,int channel) {
146 if(net->connection[channel].status&C_IN_USE) {
147 dprintf(net->outfd,"[network] connect failed, channel %02d in use\n",
151 if(!(net->connection[channel].status&C_INFO_A)) {
153 "[network] connect failed, missing config for chan %02d\n",channel);
157 /* try connect & return result */
158 net->connection[channel].status|=C_IN_USE;
159 return(network_manage_connection(net)); /* could be other channel too */
162 int network_close(t_net *net,int channel) {
164 if(!(net->connection[channel].status&C_ESTABL)) {
165 dprintf(net->outfd,"[network] close failed, channel %02d not active\n",
170 net->connection[channel].status|=C_HANGUP;
171 return(network_manage_connection(net)); /* could be other channel too */
174 int network_close_all(t_net *net) {
178 for(channel=0;channel<MAX_CONNECTIONS;channel++)
179 if(net->connection[channel].status&C_ESTABL) network_close(net,channel);
184 int network_set_connection_info(t_net *net,int channel,char *ip,int port) {
186 if(net->connection[channel].status&C_IN_USE) {
188 "[network] set connection failed, channel %02d in use\n",channel);
192 strncpy(net->connection[channel].ip,ip,IP_DIGITS);
193 net->connection[channel].port=port;
194 net->connection[channel].status|=C_INFO_A;
199 int network_select(t_net *net,int channel) {
203 if(channel==MAX_CONNECTIONS) mask=0xffffffff;
204 else mask=(1<<channel);
210 int network_deselect(t_net *net,int channel) {
214 if(channel==MAX_CONNECTIONS) mask=0;
215 else mask=~(1<<channel);
221 int network_manage_incoming(t_net *net) {
224 struct sockaddr_in addr;
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,
232 perror("[network] accept call");
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;
239 "[network] established connection from %s:%d, channel %d\n",
240 net->connection[channel].ip,net->connection[channel].port,
246 dprintf(net->outfd,"[network] maximum connections reached\n");
250 int network_send(int fd,unsigned char *data,int datasize) {
258 if((count=write(fd,data+datasize-left,left<MTU?left:MTU))==-1) {
259 perror("[network] write call");
268 int network_send_chan(t_net *net,int chan,unsigned char *data,int datasize) {
270 return(network_send(net->connection[chan].fd,data,datasize));
273 int network_receive(int fd,unsigned char *data,int datasize) {
281 if((count=read(fd,data+datasize-left,left<MTU?left:MTU))==-1) {
282 perror("[network] read call");
291 int network_receive_chan(t_net *net,int chan,unsigned char *data,int datasize) {
293 return(network_receive(net->connection[chan].fd,data,datasize));
296 int network_udp_listen_init(t_net *net) {
298 struct sockaddr_in addr;
301 if((net->l_udp_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
302 perror("[network] socket call (udp-receive)");
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) {
313 if(setsockopt(net->l_udp_fd,SOL_SOCKET,SO_REUSEADDR,
314 &true,sizeof(true))==-1) {
315 perror("[network] setsockopt call (udp)");
318 dprintf(net->outfd,"[network] reused address (udp)\n");
321 dprintf(net->outfd,"[network] listening on port %d (udp)\n",
324 if((net->s_udp_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
325 perror("[network] socket call (udp-send)");
332 int network_udp_receive(t_net *net,int channel, unsigned char *data,int count) {
334 struct sockaddr_in addr;
337 if((count=recvfrom(net->l_udp_fd,data,count,0,
338 (struct sockaddr *)&addr,&len))==-1) {
339 perror("[network] recvfrom call");
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;
352 int network_udp_send(t_net *net,int channel, unsigned char *data,int size) {
355 struct sockaddr_in addr;
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));
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");
377 int network_udp_shutdown(t_net *net) {
379 if(close(net->l_udp_fd)==-1) {
380 perror("[network] close call (udp-receive)");
384 if(close(net->s_udp_fd)==-1) {
385 perror("[network] close call (udp-send)");