1 /* network.c -- network management stuff
3 * author: hackbard@hackdaworld.dyndns.org
9 int network_init(t_net *net,int outfd) {
11 dprintf(net->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))==-1) {
259 perror("[network] write call");
268 int network_receive(int fd,unsigned char *data,int datasize) {
276 if((count=read(fd,data,datasize))==-1) {
277 perror("[network] read call");
286 int network_receive_chan(t_net *net,int chan,unsigned char *data,int datasize) {
288 return(network_receive(net->connection[chan].fd,data,datasize));
291 int network_udp_listen_init(t_net *net) {
293 struct sockaddr_in addr;
296 if((net->l_udp_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
297 perror("[network] socket call (udp-receive)");
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) {
308 if(setsockopt(net->l_udp_fd,SOL_SOCKET,SO_REUSEADDR,
309 &true,sizeof(true))==-1) {
310 perror("[network] setsockopt call (udp)");
313 dprintf(net->outfd,"[network] reused address (udp)\n");
316 dprintf(net->outfd,"[network] listening on port %d (udp)\n",
319 if((net->s_udp_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
320 perror("[network] socket call (udp-send)");
327 int network_udp_receive(t_net *net,int channel, unsigned char *data,int count) {
329 struct sockaddr_in addr;
332 if((count=recvfrom(net->l_udp_fd,data,count,0,
333 (struct sockaddr *)&addr,&len))==-1) {
334 perror("[network] recvfrom call");
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;
347 int network_udp_send(t_net *net,int channel, unsigned char *data,int size) {
350 struct sockaddr_in addr;
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));
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");
372 int network_udp_shutdown(t_net *net) {
374 if(close(net->l_udp_fd)==-1) {
375 perror("[network] close call (udp-receive)");
379 if(close(net->s_udp_fd)==-1) {
380 perror("[network] close call (udp-send)");