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 /* prevent addres in use error message */
32 if(setsockopt(net->l_fd,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(true))==-1) {
33 perror("[network] setsockopt call");
37 if(bind(net->l_fd,(struct sockaddr *)&addr,
38 sizeof(struct sockaddr))==-1) {
39 perror("[network] bind call");
43 if(listen(net->l_fd,MAX_LISTEN_QUEUE)==-1) {
44 perror("[network] listen call");
48 printf("[network] listen on %s port %d\n",inet_ntoa(addr.sin_addr),
54 int network_shutdown(t_net *net) {
58 for(channel=0;channel<MAX_CONNECTIONS;channel++)
59 if(net->connection[channel].status&C_SOCKET)
60 close(net->connection[channel].fd);
62 if(close(net->l_fd)==-1) {
63 perror("[network] close call");
67 puts("[network] shutdown");
72 int network_set_listen_port(t_net *net,in_port_t port) {
79 int network_manage_connection(t_net *net) {
82 struct sockaddr_in addr;
84 for(i=0;i<MAX_CONNECTIONS;i++) {
86 if(net->connection[i].status&C_IN_USE) {
88 if(net->connection[i].status&C_HANGUP) {
89 if(close(net->connection[i].fd)==-1) {
90 perror("[network] close call");
93 printf("[network] connection %d closed\n",i);
94 net->connection[i].status=0;
97 if(net->connection[i].status&C_INFO_A) {
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");
104 net->connection[i].status|=C_SOCKET;
107 if((!(net->connection[i].status&C_ESTABL))&&
108 (net->connection[i].status&C_SOCKET)) {
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");
118 if(connect(net->connection[i].fd,(struct sockaddr *)&addr,
119 sizeof(struct sockaddr))==-1) {
120 perror("[network] connect call");
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;
140 int network_connect(t_net *net,int channel) {
142 if(net->connection[channel].status&C_IN_USE) {
143 printf("[network] connect failed, channel %02d in use\n",channel);
146 if(!(net->connection[channel].status&C_INFO_A)) {
147 printf("[network] connect failed, missing configuration for channel %02d",
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 printf("[network] close failed, channel %02d not active",channel);
164 net->connection[channel].status|=C_HANGUP;
165 return(network_manage_connection(net)); /* could be other channel too */
168 int network_set_connection_info(t_net *net,int channel,char *ip,int port) {
170 if(net->connection[channel].status&C_IN_USE) {
171 printf("[network] set connection failed, channel %02d in use\n",channel);
175 strncpy(net->connection[channel].ip,ip,IP_DIGITS);
176 net->connection[channel].port=port;
177 net->connection[channel].status|=C_INFO_A;
182 int network_select(t_net *net,int channel) {
186 if(channel==MAX_CONNECTIONS) mask=0xffffffff;
187 else mask=(1<<channel);
193 int network_deselect(t_net *net,int channel) {
197 if(channel==MAX_CONNECTIONS) mask=0;
198 else mask=~(1<<channel);
204 int network_manage_incoming(t_net *net) {
207 struct sockaddr_in addr;
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,
215 perror("[network] accept call");
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);
227 puts("[network] maximum connections reached");
231 int network_send(int fd,unsigned char *data,int datasize) {
239 if((count=write(fd,data+datasize-left,left))==-1) {
240 perror("[network] write call");
249 int network_receive(int fd,unsigned char *data,int datasize) {
253 if((count=read(fd,data,datasize))==-1) {
254 perror("[network] read call");