]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Improving code about devsoc
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Fri, 2 Aug 2024 18:42:51 +0000 (14:42 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Fri, 2 Aug 2024 18:42:51 +0000 (14:42 -0400)
lib/Makefile
lib/devsoc.c [moved from lib/unisoc.c with 65% similarity]
lib/devsoc.h [moved from lib/unisoc.h with 60% similarity]
lib/gestcp.c
lib/gestcp.h
lib/modrec.c
lib/subrou.c

index c6d8a47e7efe9ad7bb4b1f1e82f98e8018c1def9..eec4efbaaba14e585a59c59c0565b6b14e65f8ec 100644 (file)
@@ -18,8 +18,9 @@ clean :
 OBJS=                                          \
          modrec.o                              \
          gestcp.o                              \
+         devsoc.o                              \
          unieml.o                              \
-         unipar.o uniprc.o unisig.o unisoc.o   \
+         unipar.o uniprc.o unisig.o            \
          subrou.o 
 
 objs   : $(OBJS)
@@ -31,7 +32,7 @@ modrec.o:                                     \
           subrou.h                             \
           uniprc.h                             \
           unisig.h                             \
-          unisoc.h                             \
+          devsoc.h                             \
           gestcp.h                             \
           modrec.h modrec.c
 
@@ -42,6 +43,11 @@ gestcp.o:                                    \
           unisig.h                             \
           gestcp.h gestcp.c
 
+devsoc.o:                                      \
+          subrou.h                             \
+          uniprc.h                             \
+          devsoc.h devsoc.c
+
 unieml.o:                                      \
           unieml.h unieml.c
 
@@ -57,15 +63,11 @@ unisig.o:                                   \
           subrou.h                             \
           unisig.h unisig.c
 
-unisoc.o:                                      \
-          subrou.h                             \
-          unisoc.h unisoc.c
-
 subrou.o:                                      \
           subrou.h subrou.c
 
 gestcp.h:                                      \
-          unisoc.h                             \
+          devsoc.h                             \
 
 uniprc.h:                                      \
           subrou.h                             \
similarity index 65%
rename from lib/unisoc.c
rename to lib/devsoc.c
index eb2d39f3b30d3256e19c08c4c7aa862f1216b1b8..0113200bfbdd922047d0c68037a9f345aa5b5ae0 100644 (file)
@@ -16,7 +16,8 @@
 #include        <unistd.h>
 
 #include       "subrou.h"
-#include       "unisoc.h"
+#include       "uniprc.h"
+#include       "devsoc.h"
 
 //Need to have GNU_SOURCE define within CFLAGS
 #ifdef  AI_ALL
 #define HINTFLG AI_CANONNAME
 #endif
 
+typedef struct  {
+        PROTYP proto;   //Connexion protocol type
+        char *ip;       //Binding IP    //IPV4 or IPV6
+        char *port;     //Binding Port
+        time_t lasttry; //successful binding last time
+        int iteration;  //number of soc slot used on the IP
+        int handle;     //connexion handle
+        }SOCTYP;
 
 static  _Bool modopen;        //module open/close status
 /*
@@ -37,16 +46,19 @@ static  _Bool modopen;        //module open/close status
 /*                                                     */
 /*                                                      */
 /********************************************************/
-static SOCTYP *freebinding(SOCTYP *binding)
+static SOCPTR *freebinding(SOCPTR *socptr)
 
 {
-if (binding!=(SOCTYP *)0) {
-  binding->port=rou_freestr(binding->port);
-  binding->ip=rou_freestr(binding->ip);
-  (void) free(binding);
-  binding=(SOCTYP *)0; 
+if (socptr!=(SOCPTR *)0) {
+  register SOCTYP *soc;
+
+  soc=(SOCTYP *)socptr;
+  soc->port=rou_freestr(soc->port);
+  soc->ip=rou_freestr(soc->ip);
+  (void) free(soc);
+  socptr=(SOCPTR *)0; 
   }
-return binding;
+return socptr;
 }
 /*
 \f
@@ -57,7 +69,7 @@ return binding;
 /*     address and return the linked handle            */
 /*                                                      */
 /********************************************************/
-static int bindhandle(struct addrinfo *ai,SOCTYP *binding)
+static int bindhandle(struct addrinfo *ai,SOCTYP *soc)
 
 {
 #define        OPEP    "unisoc.c:bindhandle,"
@@ -115,7 +127,7 @@ while (proceed==true) {
       server_addr.sin_port = htons(2525);
       if (bind(handle,ai->ai_addr,ai->ai_addrlen)<0) {
        (void) rou_alert(0,"%s Unable to bind on IP/port <%s/%s> (error='%s')",
-                          OPEP,ai->ai_canonname,binding->port,strerror(errno));
+                          OPEP,ai->ai_canonname,soc->port,strerror(errno));
         phase=999;     //trouble cleanup phase
        }
       break;
@@ -139,95 +151,68 @@ return handle;
 */
 /********************************************************/
 /*                                                     */
-/*     Procedure fine the hostname related to an       */
-/*      address, if local is true return the local      */
-/*      hostname.                                       */
-/*      if trouble, return a NULL pointer.              */
+/*     Procedure to free memory used by a bind         */
+/*     definition.                                     */
 /*                                                     */
 /********************************************************/
-PUBLIC char *soc_getaddrname(int handle,_Bool local)
+PUBLIC SOCPTR **soc_freebindinf(SOCPTR **socptr)
 
 {
-#define OPEP    "unisoc.c:soc_getaddrname"
-
-char *name;
-struct sockaddr connip;
-socklen_t taille;
-int status;
-char host[NI_MAXHOST];
+if (socptr!=(SOCPTR **)0) {
+  register SOCTYP **socs;
+  int i;
 
-name=(char *)0;
-taille=sizeof(connip);
-if (local==true) {
-  if ((status=getsockname(handle,&connip,&taille))==0) {
-    if (getnameinfo(&connip,taille,host,sizeof(host),(char *)0,0,NI_NAMEREQD)==0)
-      name=strdup(host);
-    }
-  }
-else {
-  if ((status=getpeername(handle,&connip,&taille))==0) {
-    if (getnameinfo(&connip,taille,host,sizeof(host),(char *)0,0,NI_NUMERICHOST)==0)
-      name=strdup(host);
+  socs=(SOCTYP **)socptr;
+  for (i=0;socs[i]!=(SOCTYP *)0;i++) {
+    socs[i]=freebinding(socs[i]);
     }
+  (void) free(socs);
+  socptr=(SOCPTR **)0;
   }
-if (status<0) {
-  switch (errno) {
-    case ENOTCONN       :       //other side just vanished
-      break;
-    default             :
-      (void) rou_alert(0,"%s, Unable to find reverse (local=%d, error=<%s>)",
-                          OPEP,local,strerror(errno));
-      break;
-    }
-  }
-return name;
-#undef  OPEP
+return socptr;
 }
 /*
 \f
 */
 /********************************************************/
 /*                                                     */
-/*     Procedure to free memory used by a bind         */
+/*     Procedure to set memory used by a bin           */
 /*     definition.                                     */
 /*                                                     */
 /********************************************************/
-PUBLIC SOCTYP **soc_freebindinf(SOCTYP **bindref)
+PUBLIC SOCPTR **soc_mkbindinf(SOCPTR **socptr,PROTYP proto,
+                              const char *ip,const char *port,int iteration)
 
 {
-if (bindref!=(SOCTYP **)0) {
-  int i;
+SOCTYP *soc;
 
-  for (i=0;bindref[i]!=(SOCTYP *)0;i++) {
-    bindref[i]=freebinding(bindref[i]);
-    }
-  (void) free(bindref);
-  bindref=(SOCTYP **)0;
-  }
-return bindref;
+soc=(SOCTYP *)calloc(1,sizeof(SOCTYP));
+soc->proto=proto;
+soc->ip=strdup(ip);
+soc->port=strdup(port);
+soc->iteration=iteration;
+socptr=(SOCPTR **)rou_addlist((void **)socptr,(void *)soc);
+return socptr;
 }
 /*
-\f
+^L
 */
 /********************************************************/
 /*                                                     */
-/*     Procedure to set memory used by a bin           */
-/*     definition.                                     */
+/*      Procedure to return the number of iterations    */
+/*      within socket definition.                       */
 /*                                                     */
 /********************************************************/
-PUBLIC SOCTYP **soc_mkbindinf(SOCTYP **bindings,PROTYP proto,
-                              const char *ip,const char *port,int iteration)
+PUBLIC int  soc_getiterations(SOCPTR *socptr)
 
 {
-SOCTYP *bininf;
+int iterations;
+SOCTYP *soc;
 
-bininf=(SOCTYP *)calloc(1,sizeof(SOCTYP));
-bininf->proto=proto;
-bininf->ip=strdup(ip);
-bininf->port=strdup(port);
-bininf->iteration=iteration;
-bindings=(SOCTYP **)rou_addlist((void **)bindings,(void *)bininf);
-return bindings;
+soc=(SOCTYP *)socptr;
+if (soc!=(SOCTYP *)0)
+  iterations=soc->iteration;
+return iterations;
 }
 /*
 ^L
@@ -238,18 +223,20 @@ return bindings;
 /*      socket handle                                   */
 /*                                                     */
 /********************************************************/
-PUBLIC _Bool  soc_openbinding(SOCTYP *binding)
+PUBLIC _Bool  soc_openbinding(SOCPTR *socptr)
 
 {
 #define OPEP    "unisoc.c:soc_openbinding"
 
 _Bool done;
+SOCTYP *soc;
 struct addrinfo hints;
 struct addrinfo *tobind;
 int phase;
 _Bool proceed;
 
 done=false;
+soc=(SOCTYP *)socptr;
 (void) memset(&hints,'\000',sizeof(hints));
 hints.ai_family=PF_UNSPEC;
 hints.ai_flags=HINTFLG;
@@ -260,38 +247,40 @@ proceed=true;
 while (proceed==true) {
   switch (phase) {
     case 0      :       //sanity check
-      if (binding==(SOCTYP *)0) {
+      if (soc==(SOCTYP *)0) {
         (void) rou_alert(0,"%s, socket binding reference is NULL (Bug!?)",OPEP);
         phase=999;      //no need to go further
         }
       break;
     case 1      :       //check if time to retry binding
-      if ((binding->lasttry+5)>time((time_t)0))
+      if ((soc->lasttry+5)>time((time_t)0))
         phase=999;      //no need to go further
       break;
     case 2      :       //to have the right address infp
       int status;
 
-      binding->lasttry=time((time_t *)0);
-      if ((status=getaddrinfo(binding->ip,binding->port,&hints,&tobind))!=0) {
+      soc->lasttry=time((time_t *)0);
+      if ((status=getaddrinfo(soc->ip,soc->port,&hints,&tobind))!=0) {
         (void) rou_alert(0,"%s, Unable to find a address about "
                            "IP:port '%s:%s' (error='%s')",
-                            binding->ip,binding->port,gai_strerror(status));
+                            soc->ip,soc->port,gai_strerror(status));
         phase=999;      //no need to go further
         }
       break;
     case 3      :       //binding socket
-      if ((binding->handle=bindhandle(tobind,binding))<0)
+      if ((soc->handle=bindhandle(tobind,soc))<0)
         phase=999;      //no need to go further
       (void) freeaddrinfo(tobind);
       break;
     case 4      :       //listening on socket
-      if (listen(binding->handle,binding->iteration+4)<0) {
+      (void) prc_settitle("monitoring %02d contacts  on %s:%s",
+                           soc->iteration,soc->ip,soc->port);
+      if (listen(soc->handle,soc->iteration+4)<0) {
         (void) rou_alert(0,"%s, Unable to listen at address "
                            "IP:port '%s:%s' (error='%s')",
-                            binding->ip,binding->port,strerror(errno));
-        (void) close(binding->handle);
-        binding->handle=-1;
+                            soc->ip,soc->port,strerror(errno));
+        (void) close(soc->handle);
+        soc->handle=-1;
         phase=999;      //no need to go further
         }
       break;
@@ -315,34 +304,36 @@ return done;
 /*      Procedure to unbind from one socket.            */
 /*                                                     */
 /********************************************************/
-PUBLIC void soc_closebinding(SOCTYP *binding)
+PUBLIC void soc_closebinding(SOCPTR *socptr)
 
 {
 #define OPEP    "unisoc.c:soc_closebinding"
 
+SOCTYP *soc;
 int phase;
 _Bool proceed;
 
+soc=(SOCTYP *)socptr;
 phase=0;
 proceed=true;
 while (proceed==true) {
   switch (phase) {
     case 0      :       //sanity check
-      if (binding==(SOCTYP *)0) {
+      if (soc==(SOCTYP *)0) {
         (void) rou_alert(0,"%s binding pointer is NULL (Bug!?)",OPEP);
         phase=999;      //not going further
         }
       break;
     case 1      :       //check if socket already close
-      if (binding->handle<0) {
+      if (soc->handle<0) {
         phase=999;      //yes, no need to go further
         }
       break;
     case 2      :       //closing the the socket
-      if (close(binding->handle)<0) 
+      if (close(soc->handle)<0) 
         (void) rou_alert(0,"%s Unable to close socket (Bug!?, error=<%s>)",
                             OPEP,strerror(errno));
-      binding->handle=-1;
+      soc->handle=-1;
       break;
     default     :       //SAFE Guard
       proceed=false;
@@ -362,51 +353,56 @@ while (proceed==true) {
 /*      return the handle                               */
 /*                                                     */
 /********************************************************/
-PUBLIC int soc_accept(SOCTYP *binding,SOCKADDR *addr)
+PUBLIC int soc_accept(SOCPTR *socptr,int pos)
 
 {
 #define OPEP    "unisoc.c:soc_accept"
+
 int newhandle;
-socklen_t taille;
+register SOCTYP *soc;
 
 newhandle=-1;
-taille=sizeof(struct sockaddr);
-if ((newhandle=accept(binding->handle,addr,&taille))<0) {
-  if (errno==EAGAIN)
-    errno=EWOULDBLOCK;
-  switch (errno) {
-    case EWOULDBLOCK    :
-      (void) rou_alert(0,"%s Socket on IP/PORT <%s/%s> is in no_block mode "
-                         "(Bug?!, errno=<%s>)",
-                         OPEP,binding->ip,binding->port,strerror(errno));
-      break;
-    case EINVAL         :
-      (void) rou_alert(0,"%s Socket on IP/PORT <%s/%s> not available "
-                         "(Bug?!, errno=<%s>)",
-                         OPEP,binding->ip,binding->port,strerror(errno));
-      break;
-    case EINTR          :
-      (void) rou_alert(0,"%s Socket on IP/PORT <%s/%s> got a signal "
-                         "(errno=<%s>)",
-                         OPEP,binding->ip,binding->port,strerror(errno));
-      break;
-    default             :
-      (void) rou_alert(0,"%s Unexpected error on IP/PORT <%s/%s> (errno=<%s>)",
-                         OPEP,binding->ip,binding->port,strerror(errno));
+soc=(SOCTYP *)socptr;
+if (soc!=(SOCTYP *)0) {
+  (void) prc_settitle("Waiting contact (%02d/%02d) on %s:%s",
+                       pos,soc->iteration,soc->ip,soc->port);
+  if ((newhandle=accept(soc->handle,(SOCKADDR *)0,(socklen_t *)0))<0) {
+    if (errno==EAGAIN)
+      errno=EWOULDBLOCK;
+    switch (errno) {
+      case EWOULDBLOCK    :
+        (void) rou_alert(0,"%s Socket on IP/PORT <%s/%s> is in no_block mode "
+                           "(Bug?!, errno=<%s>)",
+                           OPEP,soc->ip,soc->port,strerror(errno));
       break;
+      case EINVAL         :
+        (void) rou_alert(0,"%s Socket on IP/PORT <%s/%s> not available "
+                           "(Bug?!, errno=<%s>)",
+                           OPEP,soc->ip,soc->port,strerror(errno));
+        break;
+      case EINTR          :
+        (void) rou_alert(0,"%s Socket on IP/PORT <%s/%s> got a signal "
+                           "(errno=<%s>)",
+                           OPEP,soc->ip,soc->port,strerror(errno));
+        break;
+      default             :
+        (void) rou_alert(0,"%s Unexpected error on IP/PORT <%s/%s> (errno=<%s>)",
+                           OPEP,soc->ip,soc->port,strerror(errno));
+        break;
+      }
     }
-  }
-else {
-  int flags;
+  else {
+    int flags;
 
-  if ((flags=fcntl(newhandle,F_GETFL,0))<0)
-    (void) rou_core_dump("%s, Unable to get socket descripteur on "
-                         "IP/PORT <%s/%s> (Bug? error=<%s>)",
-                          OPEP,binding->ip,binding->port,strerror(errno));
-  if ((flags=fcntl(newhandle,F_SETFL,flags|O_NONBLOCK|O_ASYNC))<0)
-    (void) rou_core_dump("%s, Unable to set socket descripteur on "
-                         "IP/PORT <%s/%s> (Bug? error=<%s>)",
-                          OPEP,binding->ip,binding->port,strerror(errno));
+    if ((flags=fcntl(newhandle,F_GETFL,0))<0)
+      (void) rou_core_dump("%s, Unable to get socket descripteur on "
+                           "IP/PORT <%s/%s> (Bug? error=<%s>)",
+                            OPEP,soc->ip,soc->port,strerror(errno));
+    if ((flags=fcntl(newhandle,F_SETFL,flags|O_NONBLOCK|O_ASYNC))<0)
+      (void) rou_core_dump("%s, Unable to set socket descripteur on "
+                           "IP/PORT <%s/%s> (Bug? error=<%s>)",
+                            OPEP,soc->ip,soc->port,strerror(errno));
+    }
   }
 return newhandle;
 #undef  OPEP
@@ -421,7 +417,7 @@ return newhandle;
 /*      return zero if everything right                 */
 /*                                                      */
 /********************************************************/
-int soc_modeunisoc(_Bool mode)
+int soc_modedevsoc(_Bool mode)
 
 {
 #define OPEP    "unidoc.c:soc_modeunisoc"
@@ -430,11 +426,14 @@ int status;
 
 status=0;
 if (mode!=modopen) {
-  (void) rou_modesubrou(mode);
   switch ((int)mode) {
     case true     :
+      (void) rou_modesubrou(mode);
+      (void) prc_modeuniprc(mode);
       break;
     case false    :
+      (void) prc_modeuniprc(mode);
+      (void) rou_modesubrou(mode);
       break;
     default       :
       (void) fprintf(stderr,"Calling %s with wrong mode='%d' (Bug?!):",
similarity index 60%
rename from lib/unisoc.h
rename to lib/devsoc.h
index f9dc0eac1060e3c5b9a1dc1ecf791a5366d019b5..d15fb63932c5f6c724946b406cdfb62a5d75184b 100644 (file)
@@ -5,8 +5,8 @@
 /*     to handle tcp Socket.                           */
 /*                                                     */
 /********************************************************/
-#ifndef        UNISOC
-#define UNISOC
+#ifndef        DEVSOC
+#define DEVSOC
 
 #include        <sys/socket.h>
 #include        <stdbool.h>
@@ -23,37 +23,31 @@ typedef enum    {
         pro_unknwn      //Protcole undefined
         }PROTYP;
 
-typedef struct  {
-        PROTYP proto;   //Connexion protocol type
-        char *ip;       //Binding IP    //IPV4 or IPV6
-        char *port;     //Binding Port
-        time_t lasttry; //successful binding last time
-        int iteration;  //number of soc slot used on the IP
-        int handle;     //connexion handle
-        }SOCTYP;
-
-//procedure to get peer or local name related to a socket
-extern char *soc_getaddrname(int handle,_Bool local);
+//reference to a socket definition
+typedef void SOCPTR;
 
 //procedure to free all memory used by a TCP socket
 //definition (once closed) 
-extern SOCTYP **soc_freebindinf(SOCTYP **bindref);
+extern SOCPTR **soc_freebindinf(SOCPTR **socptr);
 
 //procedure to assign memory to be used by a TCP socket
 //definition
-extern SOCTYP **soc_mkbindinf(SOCTYP **bindings,PROTYP proto,
+extern SOCPTR **soc_mkbindinf(SOCPTR **s,PROTYP proto,
                               const char *ip,const char *port,int iteration);
 
+//procedure to return the number of channel to open on the soc
+extern int soc_getiterations(SOCPTR *socptr);
+
 //procedure to open ONE socket and return handle to socket
-extern _Bool soc_openbinding(SOCTYP *binding);
+extern _Bool soc_openbinding(SOCPTR *socptr);
 
 //procedure to close ONE socket
-extern void soc_closebinding(SOCTYP *binding);
+extern void soc_closebinding(SOCPTR *socptr);
 
 //procedure to wait and accept remote connexion
-extern int soc_accept(SOCTYP *binding,SOCKADDR *addr);
+extern int soc_accept(SOCPTR *socptr,int pos);
 
 //homework to be done before starting/stopping module.
-extern int soc_modeunisoc(_Bool mode);
+extern int soc_modedevsoc(_Bool mode);
 
 #endif
index 9a99bd75a6c549a9a9e6a9c222b6f773dc43da32..4df72513c81c379afeee0cccae5264f3ead999f2 100644 (file)
 
 static  _Bool modopen;          //boolean module open/close
 /*
+\f
+*/
+/********************************************************/
+/*                                                     */
+/*     Procedure to find the hostname related to an    */
+/*      socket, if local is true return the local       */
+/*      hostname else return the remote PID.            */
+/*      if trouble, return a NULL pointer.              */
+/*                                                     */
+/********************************************************/
+static char *getaddrname(int handle,_Bool local)
+
+{
+#define OPEP    "gestcp.c:soc_getaddrname"
+
+char *name;
+
+struct sockaddr connip;
+socklen_t taille;
+int status;
+char host[NI_MAXHOST];
+
+name=(char *)0;
+taille=sizeof(connip);
+status=-1;
+if (local==true) {
+  if ((status=getsockname(handle,&connip,&taille))==0) {
+    if (getnameinfo(&connip,taille,host,sizeof(host),(char *)0,0,NI_NAMEREQD)==0)
+      name=strdup(host);
+    }
+  }
+else {
+  if ((status=getpeername(handle,&connip,&taille))==0) {
+    if (getnameinfo(&connip,taille,host,sizeof(host),(char *)0,0,NI_NUMERICHOST)==0)
+      name=strdup(host);
+    }
+  }
+if (status<0) {
+  switch (errno) {
+    case ENOTCONN       :       //other side just vanished
+      break;
+    default             :
+      (void) rou_alert(0,"%s, Unable to find reverse (local=%d, error=<%s>)",
+                          OPEP,local,strerror(errno));
+      break;
+    }
+  }
+return name;
+#undef  OPEP
+}
+/*
 ^L
 */
 /********************************************************/
@@ -29,12 +80,18 @@ static  _Bool modopen;          //boolean module open/close
 /*     Procedure to free memory used by contact        */
 /*                                                      */
 /********************************************************/
-PUBLIC CONTYP *tcp_freecontact(CONTYP *contact)
+static CONTYP *freecontact(CONTYP *contact)
 
 {
+#define OPEP    "gesttcp.c:freecontact"
+
 if (contact!=(CONTYP *)0) {
-  if (contact->channel>=0)
-    (void) close(contact->channel);
+  if (contact->channel>=0) {
+    if (close(contact->channel)<0) {
+      (void) rou_alert(0,"%s unable to close channel properly (errno=<%s>)",
+                            OPEP,strerror(errno));
+      }
+    }
   if (contact->peerip!=(char *)0)
     (void) free(contact->peerip);
   if (contact->locname!=(char *)0)
@@ -43,6 +100,7 @@ if (contact!=(CONTYP *)0) {
   contact=(CONTYP *)0;
   }
 return contact;
+#undef OPEP
 }
 /*
 ^L
@@ -91,41 +149,41 @@ return sent;
 /*      return all reference to the established contact.*/
 /*                                                      */
 /********************************************************/
-PUBLIC CONTYP *tcp_getcontact(SOCTYP *binding)
+PUBLIC CONTYP *tcp_getcontact(SOCPTR *socptr,int pos)
 
 {
 #define OPEP    "gestcp.c:tcp_getcontact"
 
 CONTYP *contact;
-SOCKADDR addr;
 int phase;
 _Bool proceed;
 
-contact=calloc(1,sizeof(CONTYP));
-contact->binding=binding;
+contact=(CONTYP *)0;
 phase=0;
 proceed=true;
 while (proceed==true) {
   switch (phase){
     case 0      :       //check for binding
-      if (binding==(SOCTYP *)0) {
-        (void) rou_alert(0,"%s binding pointer is NULL (Bug!?)",OPEP);
+      if (socptr==(SOCPTR *)0) {
+        (void) rou_alert(0,"%s socket pointer is NULL (Bug!?)",OPEP);
         phase=999;      //not going further
         }
       break;
     case 1      :       //waiting from contact
-      if ((contact->channel=soc_accept(binding,&addr))<0) {
+      contact=calloc(1,sizeof(CONTYP));
+      contact->socptr=socptr;
+      if ((contact->channel=soc_accept(socptr,pos))<0) {
         (void) rou_alert(0,"%s Unable to open contact",OPEP);
-        contact=tcp_freecontact(contact);
+        contact=freecontact(contact);
         phase=999;      //no contact 
         }
       break;
     case 2      :       //check socket components
-      contact->locname=soc_getaddrname(contact->channel,true);
-      contact->peerip=soc_getaddrname(contact->channel,false);
+      contact->locname=getaddrname(contact->channel,true);
+      contact->peerip=getaddrname(contact->channel,false);
       if ((contact->locname==(char *)0)||(contact->peerip==(char *)0)) {
         (void) rou_alert(0,"%s Unable to establish contact entities",OPEP);
-        contact=tcp_freecontact(contact);
+        contact=freecontact(contact);
         phase=999;      //no identity
         }
       break;
@@ -141,7 +199,7 @@ while (proceed==true) {
                                       rou_ascsysstamp(time((time_t *)0)),CRLF);
         if (tcp_write(contact,signon,strlen(signon))<0) {
           (void) rou_alert(0,"%s Unable to send signon to remote",OPEP);
-          contact=tcp_freecontact(contact);
+          contact=freecontact(contact);
           phase=999;      //no contact 
           }
         #undef  FMT
@@ -184,6 +242,7 @@ while (proceed==true) {
         }
       break;
     case 1      :       //properly closing remote contact
+      (void) rou_alert(0,"Contact with IP=<%s> Exiting",contact->peerip);
       if (shutdown(contact->channel,SHUT_RDWR)<0) {
         switch (errno) {
           case ENOTCONN :       //already disconnect by other side!
@@ -196,14 +255,9 @@ while (proceed==true) {
             break;
           }
         }
-      if (close(contact->channel)<0) {
-        (void) rou_alert(0,"%s unable to close channel properly (errno=<%s>)",
-                            OPEP,strerror(errno));
-        }
       break;
     case 2      :       //freeing contact memory
-      (void) free(contact);              
-      contact=(CONTYP *)0;
+      contact=freecontact(contact);              
       break;
     default     :       //SAFE guard
       proceed=false;
@@ -233,20 +287,19 @@ int status;
 
 status=0;
 if (mode!=modopen) {
-  (void) soc_modeunisoc(mode);
   switch ((int)mode) {
     case true     :
       (void) rou_modesubrou(mode);
-      (void) soc_modeunisoc(mode);
       (void) sig_modeunisig(mode);
       (void) prc_modeuniprc(mode);
       (void) eml_modeunieml(mode);
+      (void) soc_modedevsoc(mode);
       break;
     case false    :
+      (void) soc_modedevsoc(mode);
       (void) eml_modeunieml(mode);
       (void) sig_modeunisig(mode);
       (void) prc_modeuniprc(mode);
-      (void) soc_modeunisoc(mode);
       (void) rou_modesubrou(mode);
       break;
     default       :
index 7e35a9a45137d44625fd986479dd37f99c45f655..efc3a5af82ebf6ee7aff1334a674ac3295091f6e 100644 (file)
@@ -9,14 +9,13 @@
 
 #include        <stdbool.h>
 
-#include        "unisoc.h"
+#include        "devsoc.h"
 
 typedef struct  {
         int channel;    //exchange channel handle
         char *locname;  //socket local hostname
         char *peerip;   //socket remote peer IP
-        SOCKADDR addr;  //remote address (see getnameinfo)
-        SOCTYP *binding;//established contact context
+        SOCPTR *socptr; //established contact context
         }CONTYP;
 
 //procedure to free contact
@@ -26,7 +25,7 @@ extern CONTYP *tcp_freecontact(CONTYP *contact);
 extern int tcp_write(CONTYP *contact,char *buffer,int parnum);
 
 //wait for an incoming contact
-extern CONTYP *tcp_getcontact(SOCTYP *binding);
+extern CONTYP *tcp_getcontact(SOCPTR *socptr,int pos);
 
 //drop contact established by remote
 extern CONTYP *tcp_dropcontact(CONTYP *contact);
index 0748c14c8acf5d2df7f0bae60d5c05eefe4bb947..7f672205a92ea1b540173cc6f6af968da3d861b7 100644 (file)
@@ -14,7 +14,7 @@
 #include        "subrou.h"
 #include        "uniprc.h"
 #include        "unisig.h"
-#include        "unisoc.h"
+#include        "devsoc.h"
 #include        "gestcp.h"
 #include        "modrec.h"
 
@@ -28,7 +28,7 @@ static  _Bool modopen;          //boolean module open/close
 /*      a remote TCP connection.                        */
 /*                                                     */
 /********************************************************/
-void docontact(SOCTYP *binding,int pos) 
+void docontact(SOCPTR *socptr,int pos) 
 
 {
 #define OPEP    "modrec.c:contact"
@@ -43,18 +43,14 @@ phase=0;
 proceed=true;
 while (proceed==true) {
   switch (phase) {
-    case 0      :       //display waiting contact
-      (void) prc_settitle("Waiting contact (%02d/%02d) on %s:%s",
-                          pos,binding->iteration,binding->ip,binding->port);
-      break;
-    case 1      :       //do we have a contact
-      if ((contact=tcp_getcontact(binding))==(CONTYP *)0)
+    case 0      :       //waiting contact
+      if ((contact=tcp_getcontact(socptr,pos))==(CONTYP *)0)
         phase=999;      //No contact!
       break;
-    case 2      :       //forking process
+    case 1      :       //forking process
       printf("New client connected: %d\n",contact->channel);
       break;
-    case 3      :       //do contact
+    case 2      :       //do contact
       printf("Client channel=%d (pid=%d)\n",contact->channel,getpid());
       for (int i=0;i<TESTL;i++) {
         int sent;
@@ -71,9 +67,7 @@ while (proceed==true) {
         (void) sleep(1);
         }
       break;
-    case 4      :       //connection terminated
-      (void) rou_alert(0,"Contact IP/PORT=<%s/%s> Exiting",
-                          binding->ip,binding->port);
+    case 3      :       //connection terminated
       contact=tcp_dropcontact(contact);
       break;
     default     :       //SAFE guard
@@ -92,27 +86,32 @@ while (proceed==true) {
 /*      Procedure to activate child process if necessary*/
 /*                                                     */
 /********************************************************/
-static void setready(SOCTYP *binding)
+static void setready(SOCPTR *socptr)
 
 {
 #define OPEP    "modrec.c:setready"
 
-pid_t *childs;
 int maxretry;
+pid_t *childs;
+int iterations;
 int phase;
 _Bool proceed;
 
-childs=(pid_t *)calloc(binding->iteration,sizeof(pid_t));
 maxretry=5;
+childs=(pid_t *)0;
+iterations=soc_getiterations(socptr);
 phase=0;
 proceed=true;
 while (proceed==true) {
-  //(void) fprintf(stderr,"setready phase='%d'\n",phase);
   switch (phase) {
     case 0      :       //empty phase
+      if (iterations>0) 
+        childs=(pid_t *)calloc(iterations,sizeof(pid_t));
+      else
+        proceed=false;  //Empty Soc!?!
       break;
     case 1      :       //check need to dispatch a process
-      for (int i=0;i<binding->iteration;i++) {
+      for (int i=0;i<iterations;i++) {
         (void)  prc_nozombie();
         if (prc_checkprocess(childs[i])==true)
           continue;
@@ -123,7 +122,8 @@ while (proceed==true) {
             (void) sleep(1);    //Weathering the storm
             break;
           case   0      :       //Child process itself
-            (void) docontact(binding,i+1);
+            (void) free(childs);
+            (void) docontact(socptr,i+1);
             (void) exit(0);
             break;
           default       :       //Main process
@@ -140,13 +140,13 @@ while (proceed==true) {
         phase=0;        //lets continue to check childs
       break;
     default     :       //SAFE Guard
-      (void) prc_killchilds(childs,binding->iteration,maxretry);
+      (void) prc_killchilds(childs,iterations,maxretry);
+      (void) free(childs);
       proceed=false;
       break;
     }
   phase++;
   }
-(void) free(childs);
 #undef  OPEP
 }
 /*
@@ -157,7 +157,7 @@ while (proceed==true) {
 /*      Procedure to start a binding+waiting process    */
 /*                                                     */
 /********************************************************/
-PUBLIC void startwaiter(SOCTYP *binding,int offset)
+PUBLIC void startwaiter(SOCPTR *socptr,int offset)
 
 {
 #define OPEP    "moderec.c:startwaiter"
@@ -171,23 +171,20 @@ while (proceed==true) {
   switch (phase) {
     case 0      :       //Opening logs
       (void) openlog(appname,LOG_NDELAY|LOG_PID,LOG_DAEMON);
-      (void) prc_settitle("Daemon Waiting on %s:%s (Num of channels='%02d')",
-                          binding->ip,binding->port,binding->iteration);
       break;
     case 1      :       //binding on channel
-      if (soc_openbinding(binding)==false) {
-        (void) rou_alert(0,"%s Aborting binding on <%s:%s> (config?)",
-                           OPEP,binding->ip,binding->port);
+      if (soc_openbinding(socptr)==false) {
+        (void) rou_alert(0,"%s Aborting binding (config?)",OPEP);
         phase=999;      //no need to go further
         }
       break;
     case 2      :       //waiting
       (void) rou_alert(0,"JMPDBG waiter starting");
-      (void) setready(binding);
+      (void) setready(socptr);
       (void) rou_alert(0,"JMPDBG waiter exiting");
       break;
     case 3      :       //stop binding
-      (void) soc_closebinding(binding);
+      (void) soc_closebinding(socptr);
       break;
     default     :       //SAFE Guard
       (void) closelog();
@@ -214,12 +211,12 @@ PUBLIC void rec_handlesmtp()
 
 pid_t *childs;
 int nbrbind;
-SOCTYP **bindings;
+SOCPTR **bindings;
 int phase;
 _Bool proceed;
 
 childs=(pid_t)0;
-bindings=(SOCTYP **)0;
+bindings=(SOCPTR **)0;
 bindings=soc_mkbindinf(bindings,pro_smtp,"127.0.0.1","2525",3);
 bindings=soc_mkbindinf(bindings,pro_smtp,"192.219.254.70","2525",3);
 bindings=soc_mkbindinf(bindings,pro_smtp,"127.0.0.26","2626",1);
@@ -309,12 +306,12 @@ if (mode!=modopen) {
       (void) rou_modesubrou(mode);
       (void) prc_modeuniprc(mode);
       (void) sig_modeunisig(mode);
-      (void) soc_modeunisoc(mode);
+      (void) soc_modedevsoc(mode);
       (void) tcp_modegestcp(mode);
       break;
     case false    :
       (void) tcp_modegestcp(mode);
-      (void) soc_modeunisoc(mode);
+      (void) soc_modedevsoc(mode);
       (void) sig_modeunisig(mode);
       (void) prc_modeuniprc(mode);
       (void) rou_modesubrou(mode);
index e1493ac9c9785c20e21614cb8e10ff79bd33fb19..d55f440a16ec7226541a74ac88076aaa385591dd 100644 (file)
@@ -20,7 +20,7 @@
 
 //version definition 
 #define VERSION "0.3"
-#define RELEASE "15"
+#define RELEASE "16"
 
 //Public variables
 PUBLIC  int debug=0;            //debug level