+++ /dev/null
-/* inet.c -- network management stuff
- *
- * author: hackbard@hackdaworld.dyndns.org
- *
- */
-
-#include "inet.h"
-
-int network_init(t_ivac *ivac) {
-
- struct sockaddr_in addr;
-
- 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");
- return ERROR;
- }
-
- memset(&addr,0,sizeof(struct sockaddr));
- addr.sin_family=AF_INET;
- addr.sin_port=htons(ivac->net.l_port);
- addr.sin_addr.s_addr=INADDR_ANY;
-
- if(bind(ivac->net.l_fd,(struct sockaddr *)&addr,
- sizeof(struct sockaddr))==-1) {
- perror("[ivac] inet.c: bind call");
- return ERROR;
- }
-
- printf("[ivac] inet: listen on %s port %d.\n",inet_ntoa(addr.sin_addr)
- ,ivac->net.l_port);
-
- return SUCCESS;
-}
-
-int network_manage_connection(t_ivac *ivac) {
-
- int i;
- struct sockaddr_in addr;
-
- for(i=0;i<MAX_CONNECTIONS;i++) {
-
- if(ivac->net.connection[i].status&C_IN_USE) {
-
- if(ivac->net.connection[i].status&C_HANGUP) {
- if(close(ivac->net.connection[i].fd)==-1) {
- perror("[ivac] inet.c: close call");
- return ERROR;
- }
- ivac->net.connection[i].status=0;
- }
-
- if(ivac->net.connection[i].status&C_INFO_A) {
-
- if(!ivac->net.connection[i].status&C_SOCKET) {
- if((ivac->net.connection[i].fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
- perror("[ivac] inet.c: socket call");
- return ERROR;
- }
- }
-
- if(!ivac->net.connection[i].status&C_ESTABL) {
-
- memset(&addr,0,sizeof(struct sockaddr));
- addr.sin_family=AF_INET;
- addr.sin_port=htons(ivac->net.connection[i].port);
- if(!inet_aton(ivac->net.connection[i].ip,&(addr.sin_addr))) {
- perror("[ivac] inet.c: inet_aton call");
- return ERROR;
- }
-
- if(connect(ivac->net.connection[i].fd,(struct sockaddr *)&addr,
- sizeof(struct sockaddr))==-1) {
- perror("[ivac] inet.c: connect call");
- return ERROR;
- }
-
- }
-
- }
-
- }
-
- }
-
- return SUCCESS;
-}
-
-int network_manage_incoming(t_ivac *ivac) {
-
- int i;
- struct sockaddr_in addr;
- int len;
-
- for(i=0;i<MAX_CONNECTIONS;i++) {
- if(!ivac->net.connection[i].status&C_IN_USE) {
- if((ivac->net.connection[i].fd=accept(ivac->net.l_fd,
- (struct sockaddr *)&addr,
- &len))==-1) {
- perror("[ivac] inet.c: accept call");
- return ERROR;
- }
- strncpy(ivac->net.connection[i].ip,inet_ntoa(addr.sin_addr),IP_DIGITS);
- ivac->net.connection[i].port=ntohs(addr.sin_port);
- ivac->net.connection[i].status=C_IN_USE|C_INFO_A|C_SOCKET|C_ESTABL;
- return i;
- }
- }
-
- puts("[ivac] inet: maximum connections reached");
- return ERROR;
-}
-
-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;
-}