1 /* network.c -- network management stuff
3 * author: hackbard@hackdaworld.dyndns.org
9 int network_init(t_net *net,int outfd) {
11 struct sockaddr_in addr;
14 dprintf(outfd,"[network] initializing network ...\n");
16 memset(net->connection,0,MAX_CONNECTIONS*sizeof(t_connection));
21 if((net->l_fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
22 perror("[network] socket call");
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;
31 if(bind(net->l_fd,(struct sockaddr *)&addr,
32 sizeof(struct sockaddr))==-1) {
35 if(setsockopt(net->l_fd,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(true))==-1) {
36 perror("[network] setsockopt call");
39 dprintf(event->outfd,"[network] reused address\n");
42 if(listen(net->l_fd,MAX_LISTEN_QUEUE)==-1) {
43 perror("[network] listen call");
47 dprintf(event->outfd,"[network] listen on %s port %d\n",
48 inet_ntoa(addr.sin_addr),net->l_port);
53 int network_shutdown(t_net *net) {
57 for(channel=0;channel<MAX_CONNECTIONS;channel++)
58 if(net->connection[channel].status&C_SOCKET)
59 close(net->connection[channel].fd);
61 if(close(net->l_fd)==-1) {
62 perror("[network] close call");
66 dprintf(event->outfd,"[network] shutdown\n");
71 int network_set_listen_port(t_net *net,in_port_t port) {
78 int network_manage_connection(t_net *net) {
81 struct sockaddr_in addr;
83 for(i=0;i<MAX_CONNECTIONS;i++) {
85 if(net->connection[i].status&C_IN_USE) {
87 if(net->connection[i].status&C_HANGUP) {
88 if(close(net->connection[i].fd)==-1) {
89 perror("[network] close call");
92 dprintf(event->outfd,"[network] connection %d closed\n",i);
93 net->connection[i].status=0;
96 if(net->connection[i].status&C_INFO_A) {
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");
103 net->connection[i].status|=C_SOCKET;
106 if((!(net->connection[i].status&C_ESTABL))&&
107 (net->connection[i].status&C_SOCKET)) {
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");
117 if(connect(net->connection[i].fd,(struct sockaddr *)&addr,
118 sizeof(struct sockaddr))==-1) {
119 perror("[network] connect call");
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;
139 int network_connect(t_net *net,int channel) {
141 if(net->connection[channel].status&C_IN_USE) {
142 dprintf(event->outfd,"[network] connect failed, channel %02d in use\n",
146 if(!(net->connection[channel].status&C_INFO_A)) {
147 dprintf(event->outfd,
148 "[network] connect failed, missing config for chan %02d\n",channel);
152 /* try connect & return result */
153 net->connection[channel].status|=C_IN_USE;
154 return(network_manage_connection(net)); /* could be other channel too */
157 int network_close(t_net *net,int channel) {
159 if(!(net->connection[channel].status&C_ESTABL)) {
160 dprintf(event->outfd,"[network] close failed, channel %02d not active\n",
165 net->connection[channel].status|=C_HANGUP;
166 return(network_manage_connection(net)); /* could be other channel too */
169 int network_close_all(t_net *net) {
173 for(channel=0;channel<MAX_CONNECTIONS;channel++)
174 if(net->connection[channel].status&C_ESTABL) network_close(net,channel);
179 int network_set_connection_info(t_net *net,int channel,char *ip,int port) {
181 if(net->connection[channel].status&C_IN_USE) {
182 dprintf(event->outfd,
183 "[network] set connection failed, channel %02d in use\n",channel);
187 strncpy(net->connection[channel].ip,ip,IP_DIGITS);
188 net->connection[channel].port=port;
189 net->connection[channel].status|=C_INFO_A;
194 int network_select(t_net *net,int channel) {
198 if(channel==MAX_CONNECTIONS) mask=0xffffffff;
199 else mask=(1<<channel);
205 int network_deselect(t_net *net,int channel) {
209 if(channel==MAX_CONNECTIONS) mask=0;
210 else mask=~(1<<channel);
216 int network_manage_incoming(t_net *net) {
219 struct sockaddr_in addr;
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,
227 perror("[network] accept call");
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,
241 dprintf(event->outfd,"[network] maximum connections reached\n");
245 int network_send(int fd,unsigned char *data,int datasize) {
253 if((count=write(fd,data+datasize-left,left))==-1) {
254 perror("[network] write call");
263 int network_receive(int fd,unsigned char *data,int datasize) {
267 if((count=read(fd,data,datasize))==-1) {
268 perror("[network] read call");
275 int network_udp_listen_init(t_net *net) {
277 struct sockaddr_in addr;
280 if((net->l_udp_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
281 perror("[network] socket call (udp-receive)");
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) {
292 if(setsockopt(net->l_udp_fd,SOL_SOCKET,SO_REUSEADDR,
293 &true,sizeof(true))==-1) {
294 perror("[network] setsockopt call (udp)");
297 dprintf(event->outfd,"[network] reused address (udp)\n");
300 dprintf(event->outfd,"[network] listening on port %d (udp)\n",
303 if((net->s_udp_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
304 perror("[network] socket call (udp-send)");
311 int network_udp_receive(t_net *net,int channel, unsigned char *data,int count) {
313 struct sockaddr_in addr;
316 if((count=recvfrom(net->l_udp_fd,data,count,0,
317 (struct sockaddr *)&addr,&len))==-1) {
318 perror("[network] recvfrom call");
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;
331 int network_udp_send(t_net *net,int channel, unsigned char *data,int size) {
334 struct sockaddr_in addr;
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));
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");
356 int network_udp_shutdown(t_net *net) {
358 if(close(net->l_udp_fd)==-1) {
359 perror("[network] close call (udp-receive)");
363 if(close(net->s_udp_fd)==-1) {
364 perror("[network] close call (udp-send)");