1 /* network.c -- network management stuff
3 * author: hackbard@hackdaworld.dyndns.org
9 int network_init(t_net *net) {
11 struct sockaddr_in addr;
14 puts("[network] initializing network ...");
16 memset(net->connection,0,MAX_CONNECTIONS*sizeof(t_connection));
20 if((net->l_fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
21 perror("[network] socket call");
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;
30 if(bind(net->l_fd,(struct sockaddr *)&addr,
31 sizeof(struct sockaddr))==-1) {
34 if(setsockopt(net->l_fd,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(true))==-1) {
35 perror("[network] setsockopt call");
38 puts("[network] reused address");
41 if(listen(net->l_fd,MAX_LISTEN_QUEUE)==-1) {
42 perror("[network] listen call");
46 printf("[network] listen on %s port %d\n",inet_ntoa(addr.sin_addr),
52 int network_shutdown(t_net *net) {
56 for(channel=0;channel<MAX_CONNECTIONS;channel++)
57 if(net->connection[channel].status&C_SOCKET)
58 close(net->connection[channel].fd);
60 if(close(net->l_fd)==-1) {
61 perror("[network] close call");
65 puts("[network] shutdown");
70 int network_set_listen_port(t_net *net,in_port_t port) {
77 int network_manage_connection(t_net *net) {
80 struct sockaddr_in addr;
82 for(i=0;i<MAX_CONNECTIONS;i++) {
84 if(net->connection[i].status&C_IN_USE) {
86 if(net->connection[i].status&C_HANGUP) {
87 if(close(net->connection[i].fd)==-1) {
88 perror("[network] close call");
91 printf("[network] connection %d closed\n",i);
92 net->connection[i].status=0;
95 if(net->connection[i].status&C_INFO_A) {
97 if(!(net->connection[i].status&C_SOCKET)) {
98 if((net->connection[i].fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
99 perror("[network] socket call");
102 net->connection[i].status|=C_SOCKET;
105 if((!(net->connection[i].status&C_ESTABL))&&
106 (net->connection[i].status&C_SOCKET)) {
108 memset(&addr,0,sizeof(struct sockaddr));
109 addr.sin_family=AF_INET;
110 addr.sin_port=htons(net->connection[i].port);
111 if(!inet_aton(net->connection[i].ip,&(addr.sin_addr))) {
112 perror("[network] inet_aton call");
116 if(connect(net->connection[i].fd,(struct sockaddr *)&addr,
117 sizeof(struct sockaddr))==-1) {
118 perror("[network] connect call");
122 printf("[network] established connection to ");
123 printf("%s port %d on channel %d\n",net->connection[i].ip,
124 net->connection[i].port,i);
125 net->connection[i].status|=C_ESTABL;
138 int network_connect(t_net *net,int channel) {
140 if(net->connection[channel].status&C_IN_USE) {
141 printf("[network] connect failed, channel %02d in use\n",channel);
144 if(!(net->connection[channel].status&C_INFO_A)) {
145 printf("[network] connect failed, missing configuration for channel %02d\n",
150 /* try connect & return result */
151 net->connection[channel].status|=C_IN_USE;
152 return(network_manage_connection(net)); /* could be other channel too */
155 int network_close(t_net *net,int channel) {
157 if(!(net->connection[channel].status&C_ESTABL)) {
158 printf("[network] close failed, channel %02d not active\n",channel);
162 net->connection[channel].status|=C_HANGUP;
163 return(network_manage_connection(net)); /* could be other channel too */
166 int network_close_all(t_net *net) {
170 for(channel=0;channel<MAX_CONNECTIONS;channel++)
171 if(net->connection[channel].status&C_ESTABL) network_close(net,channel);
176 int network_set_connection_info(t_net *net,int channel,char *ip,int port) {
178 if(net->connection[channel].status&C_IN_USE) {
179 printf("[network] set connection failed, channel %02d in use\n",channel);
183 strncpy(net->connection[channel].ip,ip,IP_DIGITS);
184 net->connection[channel].port=port;
185 net->connection[channel].status|=C_INFO_A;
190 int network_select(t_net *net,int channel) {
194 if(channel==MAX_CONNECTIONS) mask=0xffffffff;
195 else mask=(1<<channel);
201 int network_deselect(t_net *net,int channel) {
205 if(channel==MAX_CONNECTIONS) mask=0;
206 else mask=~(1<<channel);
212 int network_manage_incoming(t_net *net) {
215 struct sockaddr_in addr;
218 for(channel=0;channel<MAX_CONNECTIONS;channel++) {
219 if(!(net->connection[channel].status&C_IN_USE)) {
220 if((net->connection[channel].fd=accept(net->l_fd,
221 (struct sockaddr *)&addr,
223 perror("[network] accept call");
226 strncpy(net->connection[channel].ip,inet_ntoa(addr.sin_addr),IP_DIGITS);
227 net->connection[channel].port=ntohs(addr.sin_port);
228 net->connection[channel].status=C_IN_USE|C_INFO_A|C_SOCKET|C_ESTABL;
229 printf("[network] established connection from %s port %d on channel %d\n",
230 net->connection[channel].ip,net->connection[channel].port,channel);
235 puts("[network] maximum connections reached");
239 int network_send(int fd,unsigned char *data,int datasize) {
247 if((count=write(fd,data+datasize-left,left))==-1) {
248 perror("[network] write call");
257 int network_receive(int fd,unsigned char *data,int datasize) {
261 if((count=read(fd,data,datasize))==-1) {
262 perror("[network] read call");
269 int network_udp_listen_init(t_net *net,int port) {
271 struct sockaddr_in addr;
273 if((net->l_udp_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
274 perror("[network] socket call (udp-receive)");
278 memset(&addr,0,sizeof(struct sockaddr));
279 addr.sin_family=AF_INET;
280 addr.sin_port=htons(net->l_udp_port);
281 addr.sin_addr.s_addr=INADDR_ANY;
282 if(bind(net->l_udp_fd,(struct sockaddr *)&addr,sizeof(struct sockaddr))==-1) {
283 perror("[network] bind call (udp)");
287 printf("[network] listening on port %d (udp)\n",net->l_udp_port);
289 if((net->s_udp_fd=socket(AF_INET,SOCK_DGRAM,0))==-1) {
290 perror("[network] socket call (udp-send)");
297 int network_set_udp_ports(t_net *net,int port) {
299 net->l_udp_port=port;
304 int network_udp_receive(t_net *net,int channel, unsigned char *data,int count) {
306 struct sockaddr_in addr;
309 if((count=recvfrom(net->l_udp_fd,data,count,0,
310 (struct sockaddr *)&addr,&len))==-1) {
311 perror("[network] recvfrom call");
315 if(strncmp(net->connection[channel].ip,inet_ntoa(addr.sin_addr),IP_DIGITS)) {
316 printf("[network] packet from unknown: %s\n",inet_ntoa(addr.sin_addr));
317 return N_UDP_WRONG_SENDER;
323 int network_udp_send(t_net *net,int channel, unsigned char *data,int size) {
326 struct sockaddr_in addr;
331 memset(&addr,0,sizeof(struct sockaddr));
332 addr.sin_family=AF_INET;
333 addr.sin_port=htons(net->l_udp_port);
334 inet_aton(net->connection[channel].ip,&(addr.sin_addr));
337 if((count=sendto(net->s_udp_fd,data+size-left,left,0,
338 (struct sockaddr *)&addr,sizeof(struct sockaddr)))==-1) {
339 perror("[network] sendto call");
348 int network_udp_shutdown(t_net *net) {
350 if(close(net->l_udp_fd)==-1) {
351 perror("[network] close call (udp-receive)");
355 if(close(net->s_udp_fd)==-1) {
356 perror("[network] close call (udp-send)");