From: hackbard Date: Tue, 4 May 2004 07:41:26 +0000 (+0000) Subject: implemented network_{receive,send} & {receive,send}_info X-Git-Url: https://hackdaworld.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=058c06c0b8b2dbbde5ee32b54ad8e502527cd155;p=my-code%2Fivac.git implemented network_{receive,send} & {receive,send}_info --- diff --git a/src/inet.c b/src/inet.c index 255423d..99f83d1 100644 --- a/src/inet.c +++ b/src/inet.c @@ -13,6 +13,8 @@ int network_init(t_ivac *ivac) { puts("[ivac] inet: initializing network ..."); memset(ivac->net.connection,0,MAX_CONNECTIONS*sizeof(t_connection)); + ivac->net.c_count=0; + ivac->net.sendmask=0; if((ivac->net.l_fd=socket(AF_INET,SOCK_STREAM,0))==-1) { perror("[ivac] inet.c: socket call"); @@ -46,7 +48,10 @@ int network_manage_connection(t_ivac *ivac) { if(ivac->net.connection[i].status&C_IN_USE) { if(ivac->net.connection[i].status&C_HANGUP) { - close(ivac->net.connection[i].fd); + if(close(ivac->net.connection[i].fd)==-1) { + perror("[ivac] inet.c: close call"); + return ERROR; + } ivac->net.connection[i].status=0; } @@ -83,8 +88,104 @@ int network_manage_connection(t_ivac *ivac) { return SUCCESS; } -int network_send(unsigned char *data,int datasize,int sendmask,t_ivac *ivac) { +int network_send(int fd,unsigned char *data,int datasize) { + + int count,left; + + count=0; + left=datasize; + + while(left) { + if((count=write(fd,data+datasize-left,left))==-1) { + perror("[ivac] inet.c: write call"); + return ERROR; + } + left-=count; + } + + return SUCCESS; +} + +int network_receive(int fd,unsigned char *data,int datasize) { + + int count,retval; + + retval=1; + count=0; + + while(retval) { + if((retval=read(fd,data+count,datasize-count))==-1) { + perror("[ivac] inet.c: read call"); + return ERROR; + } + count+=retval; + } + + return count; +} + +int send_info(int fd,t_ivac *ivac) { + char data[SEND_I_MAX]; + int size; + size=strlen(ivac->name); + + data[0]=SEND_I_NAME; + data[1]=size; + strncpy(data+2,ivac->name,size); + size+=2; + + data[size+1]=SEND_I_CAP; + data[size+2]=sizeof(unsigned char); + data[size+3]=ivac->net.cap; + size+=(sizeof(unsigned char)+2); + + data[size+1]=SEND_I_AVCAP; + data[size+2]=sizeof(unsigned short); + data[size+2+sizeof(unsigned short)]; + size+=(sizeof(unsigned short)+2); + + if(network_send(fd,data,size)==ERROR) { + puts("[ivac] inet.c: send_info failed"); + return ERROR; + } + + return SUCCESS; +} + +int receive_info(int i,t_ivac *ivac) { + + char data[CHAR_USERNAME+2]; + int count,length; + + if((length=network_receive(ivac->net.connection[i].fd, + data,SEND_I_MAX))==ERROR) { + puts("[ivac] inet.c: receive_info failed"); + return ERROR; + } + + while(length-count) { + switch(data[count]) { + case SEND_I_NAME: + strncpy(ivac->net.connection[i].name,data[count+2],data[count+1]); + ivac->net.connection[i].name[data[count+2]]='\0'; + count+=(data[count+2]+2); + break; + case SEND_I_G_CAP: + ivac->net.connection[i].cap=data[count+4]; + count+=(sizeof(unsigned char)+2); + break; + case SEND_I_AV_CAP: + ivac->net.connection[i].avcap=data[count+3]<<8; + ivac->net.connection[i].avcap|=data[count+4]; + count+=(sizeof(unsigned short)+2); + break; + default: + puts("[ivac] inet.c: receive_info, unknown character"); + return ERROR; + } + } + return SUCCESS; } diff --git a/src/inet.h b/src/inet.h index 7f949ac..e7c48b7 100644 --- a/src/inet.h +++ b/src/inet.h @@ -16,6 +16,7 @@ /* defines */ #define MAX_CONNECTIONS 32 + #define IP_DIGITS 16 #define C_IN_USE (1<<0) #define C_INFO_A (1<<1) @@ -23,6 +24,11 @@ #define C_ESTABL (1<<3) #define C_HANGUP (1<<4) +#define SEND_I_MAX 128 +#define SEND_I_NAME 'n' +#define SEND_I_G_CAP 'g' +#define SEND_I_AV_CAP 'c' + /* net specific variables */ typedef s_connection { int fd; @@ -30,14 +36,19 @@ typedef s_connection { char ip[IP_DIGITS]; in_port_t port; unsigned char status; + unsigned char cap; /* general capabilities */ + unsigned short avcap; /* audio/video capabilities */ } t_connection; typedef s_net { int l_fd; /* listen file descriptor */ in_port_t l_port; + unsigned char cap; + unsigned short avcap; /* limited connections by now -- replaced by list management later */ int c_count; t_connection connection[MAX_CONNECTIONS]; + unsigned int sendmask; /* 32 bits for maximum of 32 connections */ } t_net; #endif