From: hackbard Date: Mon, 3 May 2004 12:00:57 +0000 (+0000) Subject: some network functions (to be continued) X-Git-Url: https://hackdaworld.org/gitweb/?a=commitdiff_plain;h=96a136a4a5ac3321cf3d72328b9fd8167c007a17;p=my-code%2Fivac.git some network functions (to be continued) --- diff --git a/src/inet.c b/src/inet.c index 7a9e41a..255423d 100644 --- a/src/inet.c +++ b/src/inet.c @@ -6,5 +6,85 @@ #include "inet.h" +int network_init(t_ivac *ivac) { + + sockaddr_in addr; + + puts("[ivac] inet: initializing network ..."); -int + memset(ivac->net.connection,0,MAX_CONNECTIONS*sizeof(t_connection)); + + 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.s_addr) + ,ivac->net.l_port); + + return SUCCESS; +} + +int network_manage_connection(t_ivac *ivac) { + + int i; + sockaddr_in addr; + + for(i=0;inet.connection[i].status&C_IN_USE) { + + if(ivac->net.connection[i].status&C_HANGUP) { + close(ivac->net.connection[i].fd); + 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); + addr.sin_addr_s_addr=inet_aton(ivac->net.connection[i].ip); + + 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_send(unsigned char *data,int datasize,int sendmask,t_ivac *ivac) { + + + return SUCCESS; +} diff --git a/src/inet.h b/src/inet.h index 0f6df5c..7f949ac 100644 --- a/src/inet.h +++ b/src/inet.h @@ -3,13 +3,41 @@ #ifndef INET_H #define INET_H +/* includes */ +#include "ivac.h" +#include +#include +#include +#include +#include + /* net specific includes */ #include +/* defines */ +#define MAX_CONNECTIONS 32 +#define IP_DIGITS 16 +#define C_IN_USE (1<<0) +#define C_INFO_A (1<<1) +#define C_SOCKET (1<<2) +#define C_ESTABL (1<<3) +#define C_HANGUP (1<<4) + /* net specific variables */ +typedef s_connection { + int fd; + char name[CHAR_USERNAME]; + char ip[IP_DIGITS]; + in_port_t port; + unsigned char status; +} t_connection; + typedef s_net { - int l_fd,c_fd; - + int l_fd; /* listen file descriptor */ + in_port_t l_port; + /* limited connections by now -- replaced by list management later */ + int c_count; + t_connection connection[MAX_CONNECTIONS]; } t_net; #endif diff --git a/src/ivac.h b/src/ivac.h index 9e171bd..2a10429 100644 --- a/src/ivac.h +++ b/src/ivac.h @@ -9,13 +9,14 @@ /* defines */ #define CHAR_USERNAME 32 +#define ERROR -1 +#define SUCCESS 1 /* variables */ typedef s_ivac { char username[CHAR_USERNAME]; t_net net; - } t_ivac; #endif