]> www.hackdaworld.org Git - my-code/ivac.git/commitdiff
bugfixed inet_{aton,ntoa} usage + implemented network_manage_incoming function
authorhackbard <hackbard>
Tue, 4 May 2004 10:43:22 +0000 (10:43 +0000)
committerhackbard <hackbard>
Tue, 4 May 2004 10:43:22 +0000 (10:43 +0000)
src/inet.c

index 99f83d16d1d0cb90db1e1a015ac11d8c633aad28..4f02370fbd60d934d027c982cd602c3f3279d86b 100644 (file)
@@ -8,7 +8,7 @@
 
 int network_init(t_ivac *ivac) {
   
-  sockaddr_in addr;
+  struct sockaddr_in addr;
  
   puts("[ivac] inet: initializing network ...");
 
@@ -32,7 +32,7 @@ int network_init(t_ivac *ivac) {
     return ERROR;
   }
 
-  printf("[ivac] inet: listen on %s port %d.\n",inet_ntoa(addr.sin_addr.s_addr)
+  printf("[ivac] inet: listen on %s port %d.\n",inet_ntoa(addr.sin_addr)
                                                ,ivac->net.l_port);
 
   return SUCCESS;
@@ -41,7 +41,7 @@ int network_init(t_ivac *ivac) {
 int network_manage_connection(t_ivac *ivac) {
 
   int i;
-  sockaddr_in addr;
+  struct sockaddr_in addr;
   
   for(i=0;i<MAX_CONNECTIONS;i++) {
 
@@ -69,7 +69,10 @@ int network_manage_connection(t_ivac *ivac) {
           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(!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) {
@@ -87,6 +90,31 @@ int network_manage_connection(t_ivac *ivac) {
 
   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) {