int network_shutdown(t_net *net) {
+ int channel;
+
+ for(channel=0;channel<MAX_CONNECTIONS;channel++)
+ if(net->connection[channel].status&C_SOCKET)
+ close(net->connection[channel].fd);
+
if(close(net->l_fd)==-1) {
perror("[network] close call");
return N_ERROR;
if(net->connection[i].status&C_HANGUP) {
if(close(net->connection[i].fd)==-1) {
perror("[network] close call");
- return N_ERROR;
+ return N_E_CLOSE;
}
printf("[network] connection %d closed\n",i);
net->connection[i].status=0;
if(net->connection[i].status&C_INFO_A) {
- if(!net->connection[i].status&C_SOCKET) {
+ if(!(net->connection[i].status&C_SOCKET)) {
if((net->connection[i].fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
perror("[network] socket call");
return N_ERROR;
}
+ net->connection[i].status|=C_SOCKET;
}
- if(!net->connection[i].status&C_ESTABL) {
+ if((!(net->connection[i].status&C_ESTABL))&&
+ (net->connection[i].status&C_SOCKET)) {
memset(&addr,0,sizeof(struct sockaddr));
addr.sin_family=AF_INET;
if(connect(net->connection[i].fd,(struct sockaddr *)&addr,
sizeof(struct sockaddr))==-1) {
perror("[network] connect call");
- return N_ERROR;
+ return N_E_CONNECT;
}
- printf("[network] established connection to %s port %d on channel %d\n",
- net->connection[i].ip,net->connection[i].port,i);
+ printf("[network] established connection to ");
+ printf("%s port %d on channel %d\n",net->connection[i].ip,
+ net->connection[i].port,i);
+ net->connection[i].status|=C_ESTABL;
}
return N_SUCCESS;
}
+int network_connect(t_net *net,int channel) {
+
+ if(net->connection[channel].status&C_IN_USE) {
+ printf("[network] connect failed, channel %02d in use\n",channel);
+ return N_E_IN_USE;
+ }
+ if(!(net->connection[channel].status&C_INFO_A)) {
+ printf("[network] connect failed, missing configuration for channel %02d",
+ channel);
+ return N_E_NO_INFO;
+ }
+
+ /* try connect & return result */
+ net->connection[channel].status|=C_IN_USE;
+ return(network_manage_connection(net)); /* could be other channel too */
+}
+
+int network_close(t_net *net,int channel) {
+
+ if(!(net->connection[channel].status&C_ESTABL)) {
+ printf("[network] close failed, channel %02d not active",channel);
+ return N_E_NC;
+ }
+
+ net->connection[channel].status|=C_HANGUP;
+ return(network_manage_connection(net)); /* could be other channel too */
+}
+
+int network_set_connection_info(t_net *net,int channel,char *ip,int port) {
+
+ if(net->connection[channel].status&C_IN_USE) {
+ printf("[network] set connection failed, channel %02d in use\n",channel);
+ return N_E_IN_USE;
+ }
+
+ strncpy(net->connection[channel].ip,ip,IP_DIGITS);
+ net->connection[channel].port=port;
+ net->connection[channel].status|=C_INFO_A;
+
+ return N_SUCCESS;
+}
+
+int network_select(t_net *net,int channel) {
+
+ int mask;
+
+ if(channel==MAX_CONNECTIONS) mask=0xffffffff;
+ else mask=(1<<channel);
+ net->sendmask|=mask;
+
+ return N_SUCCESS;
+}
+
+int network_deselect(t_net *net,int channel) {
+
+ int mask;
+
+ if(channel==MAX_CONNECTIONS) mask=0;
+ else mask=~(1<<channel);
+ net->sendmask&=mask;
+
+ return N_SUCCESS;
+}
+
int network_manage_incoming(t_net *net) {
int channel;
int len;
for(channel=0;channel<MAX_CONNECTIONS;channel++) {
- if(!net->connection[channel].status&C_IN_USE) {
+ if(!(net->connection[channel].status&C_IN_USE)) {
if((net->connection[channel].fd=accept(net->l_fd,
(struct sockaddr *)&addr,
&len))==-1) {
perror("[network] accept call");
- return N_ERROR;
+ return N_E_ACCEPT;
}
strncpy(net->connection[channel].ip,inet_ntoa(addr.sin_addr),IP_DIGITS);
net->connection[channel].port=ntohs(addr.sin_port);
}
puts("[network] maximum connections reached");
- return N_ERROR;
+ return N_E_MAXC;
}
int network_send(int fd,unsigned char *data,int datasize) {
return count;
}
-
-int send_info(int channel,t_net *net,char *name) {
-
- char data[SEND_N_MAX];
- int size;
-
- size=strlen(name);
-
- data[0]=SEND_N_NAME;
- data[1]=size;
- strncpy(data+2,name,size);
- size+=2;
-
- data[size]=SEND_N_G_CAP;
- data[size+1]=sizeof(unsigned char);
- data[size+1+sizeof(unsigned char)]=net->cap;
- size+=(sizeof(unsigned char)+2);
-
- data[size]=SEND_N_AV_CAP;
- data[size+1]=sizeof(unsigned short);
- data[size+1+sizeof(unsigned short)]=net->avcap;
- size+=(sizeof(unsigned short)+2);
-
- if(network_send(net->connection[channel].fd,data,size)==N_ERROR) {
- puts("[network] send_info failed");
- return N_ERROR;
- }
-
- return N_SUCCESS;
-}
-
-int receive_info(int channel,t_net *net) {
-
- char data[CHAR_N_UNAME+2];
- int count,length;
-
- count=0;
-
- if((length=network_receive(net->connection[channel].fd,
- data,SEND_N_MAX))==N_ERROR) {
- puts("[network] receive_info failed");
- return N_ERROR;
- }
-
- while(length-count) {
- switch(data[count]) {
- case SEND_N_NAME:
- strncpy(net->connection[channel].name,&data[count+2],data[count+1]);
- net->connection[channel].name[(int)data[count+2]]='\0';
- count+=(data[count+2]+2);
- break;
- case SEND_N_G_CAP:
- net->connection[channel].cap=data[count+4];
- count+=(sizeof(unsigned char)+2);
- break;
- case SEND_N_AV_CAP:
- net->connection[channel].avcap=data[count+3]<<8;
- net->connection[channel].avcap|=data[count+4];
- count+=(sizeof(unsigned short)+2);
- break;
- default:
- puts("[network] receive_info, unknown character");
- return N_ERROR;
- }
- }
-
- return N_SUCCESS;
-}