1 /* inet.c -- network management stuff
3 * author: hackbard@hackdaworld.dyndns.org
9 int network_init(t_ivac *ivac) {
11 struct sockaddr_in addr;
13 puts("[ivac] inet: initializing network ...");
15 memset(ivac->net.connection,0,MAX_CONNECTIONS*sizeof(t_connection));
19 if((ivac->net.l_fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
20 perror("[ivac] inet.c: socket call");
24 memset(&addr,0,sizeof(struct sockaddr));
25 addr.sin_family=AF_INET;
26 addr.sin_port=htons(ivac->net.l_port);
27 addr.sin_addr.s_addr=INADDR_ANY;
29 if(bind(ivac->net.l_fd,(struct sockaddr *)&addr,
30 sizeof(struct sockaddr))==-1) {
31 perror("[ivac] inet.c: bind call");
35 printf("[ivac] inet: listen on %s port %d.\n",inet_ntoa(addr.sin_addr)
41 int network_manage_connection(t_ivac *ivac) {
44 struct sockaddr_in addr;
46 for(i=0;i<MAX_CONNECTIONS;i++) {
48 if(ivac->net.connection[i].status&C_IN_USE) {
50 if(ivac->net.connection[i].status&C_HANGUP) {
51 if(close(ivac->net.connection[i].fd)==-1) {
52 perror("[ivac] inet.c: close call");
55 ivac->net.connection[i].status=0;
58 if(ivac->net.connection[i].status&C_INFO_A) {
60 if(!ivac->net.connection[i].status&C_SOCKET) {
61 if((ivac->net.connection[i].fd=socket(AF_INET,SOCK_STREAM,0))==-1) {
62 perror("[ivac] inet.c: socket call");
67 if(!ivac->net.connection[i].status&C_ESTABL) {
69 memset(&addr,0,sizeof(struct sockaddr));
70 addr.sin_family=AF_INET;
71 addr.sin_port=htons(ivac->net.connection[i].port);
72 if(!inet_aton(ivac->net.connection[i].ip,&(addr.sin_addr))) {
73 perror("[ivac] inet.c: inet_aton call");
77 if(connect(ivac->net.connection[i].fd,(struct sockaddr *)&addr,
78 sizeof(struct sockaddr))==-1) {
79 perror("[ivac] inet.c: connect call");
94 int network_manage_incoming(t_ivac *ivac) {
97 struct sockaddr_in addr;
100 for(i=0;i<MAX_CONNECTIONS;i++) {
101 if(!ivac->net.connection[i].status&C_IN_USE) {
102 if((ivac->net.connection[i].fd=accept(ivac->net.l_fd,
103 (struct sockaddr *)&addr,
105 perror("[ivac] inet.c: accept call");
108 strncpy(ivac->net.connection[i].ip,inet_ntoa(addr.sin_addr),IP_DIGITS);
109 ivac->net.connection[i].port=ntohs(addr.sin_port);
110 ivac->net.connection[i].status=C_IN_USE|C_INFO_A|C_SOCKET|C_ESTABL;
115 puts("[ivac] inet: maximum connections reached");
119 int network_send(int fd,unsigned char *data,int datasize) {
127 if((count=write(fd,data+datasize-left,left))==-1) {
128 perror("[ivac] inet.c: write call");
137 int network_receive(int fd,unsigned char *data,int datasize) {
145 if((retval=read(fd,data+count,datasize-count))==-1) {
146 perror("[ivac] inet.c: read call");
155 int send_info(int fd,t_ivac *ivac) {
157 char data[SEND_I_MAX];
160 size=strlen(ivac->name);
164 strncpy(data+2,ivac->name,size);
167 data[size+1]=SEND_I_CAP;
168 data[size+2]=sizeof(unsigned char);
169 data[size+3]=ivac->net.cap;
170 size+=(sizeof(unsigned char)+2);
172 data[size+1]=SEND_I_AVCAP;
173 data[size+2]=sizeof(unsigned short);
174 data[size+2+sizeof(unsigned short)];
175 size+=(sizeof(unsigned short)+2);
177 if(network_send(fd,data,size)==ERROR) {
178 puts("[ivac] inet.c: send_info failed");
185 int receive_info(int i,t_ivac *ivac) {
187 char data[CHAR_USERNAME+2];
190 if((length=network_receive(ivac->net.connection[i].fd,
191 data,SEND_I_MAX))==ERROR) {
192 puts("[ivac] inet.c: receive_info failed");
196 while(length-count) {
197 switch(data[count]) {
199 strncpy(ivac->net.connection[i].name,data[count+2],data[count+1]);
200 ivac->net.connection[i].name[data[count+2]]='\0';
201 count+=(data[count+2]+2);
204 ivac->net.connection[i].cap=data[count+4];
205 count+=(sizeof(unsigned char)+2);
208 ivac->net.connection[i].avcap=data[count+3]<<8;
209 ivac->net.connection[i].avcap|=data[count+4];
210 count+=(sizeof(unsigned short)+2);
213 puts("[ivac] inet.c: receive_info, unknown character");