]> hackdaworld.org Git - my-code/ivac.git/commitdiff
some network functions (to be continued)
authorhackbard <hackbard>
Mon, 3 May 2004 12:00:57 +0000 (12:00 +0000)
committerhackbard <hackbard>
Mon, 3 May 2004 12:00:57 +0000 (12:00 +0000)
src/inet.c
src/inet.h
src/ivac.h

index 7a9e41a94d1da42bcd7d18c38967550df3ae3780..255423d625105acc783d44a2720ca4ce0debf512 100644 (file)
@@ -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;i<MAX_CONNECTIONS;i++) {
+
+    if(ivac->net.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;
+}
index 0f6df5c3e2a014f16bef1710e193859346ea60c6..7f949acee819d4628bdeb28714251dacf13590e0 100644 (file)
@@ -3,13 +3,41 @@
 #ifndef INET_H
 #define INET_H
 
+/* includes */
+#include "ivac.h"
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
 /* net specific includes */
 #include <netinet/in.h>
 
+/* 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
index 9e171bd5894c4ac715c985019e86153cfe09ea79..2a104290f8832ad67cdd8324d8a1453f0b874ca8 100644 (file)
@@ -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