]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Able to open and close multiple socket
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Fri, 12 Jul 2024 06:58:59 +0000 (02:58 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Fri, 12 Jul 2024 12:02:26 +0000 (08:02 -0400)
lib/modrec.c
lib/unipar.c
lib/uniprc.c
lib/unisig.c
lib/unisoc.c
lib/unisoc.h

index 0ec202b253e7349f3b21bc028eeef59f5e522910..e0bd28190c076845bc0b1faf22c6db77621dbfe2 100644 (file)
@@ -4,6 +4,7 @@
 /*     Module to handle all email incoming             */
 /*                                                     */
 /********************************************************/
+#include        <errno.h>
 #include        <stdio.h>
 #include        <string.h>
 #include        <unistd.h>
@@ -26,12 +27,15 @@ static  _Bool modopen;          //boolean module open/close
 PUBLIC void rec_handlesmtp()
 
 {
+#define OPEP    "modrec.c:rec_handlesmtp"
+
 SOCTYP **bindings;
 int phase;
 _Bool proceed;
 
 bindings=(SOCTYP **)0;
 bindings=soc_mkbindinf(bindings,pro_smtp,"127.0.0.25","2525",10);
+bindings=soc_mkbindinf(bindings,pro_smtp,"127.0.0.26","2626",10);
 phase=0;
 proceed=true;
 while (proceed==true) {
@@ -39,15 +43,18 @@ while (proceed==true) {
     case 0      :       //looping forever email receiving processes
       (void) prc_settitle("emlrec with pid='%06d'",getpid());
       break;
-    case 1      :       //Opening ONE channel
-      if (soc_setbind(*bindings)<0)
-        phase=999;      //doc not open!
+    case 1      :       //Opening ALL channels
+      if (soc_mullisten(bindings)==false) 
+        (void) rou_alert(0,"%s, Unable to open ALL socket (config?)",OPEP);
       break;
     case 2      :       //Terminating all remaining process
       (void) fprintf(stderr,"JMPDBG wait 5 sec\n");
       (void) sleep(5);
       (void) fprintf(stderr,"JMPDBG wleep completed\n");
       break;
+    case 3      :       //closing all socket openend
+      (void) soc_mulclose(bindings);
+      break;
     default     :       //SAFE Guard
       proceed=false;
       break;
@@ -55,6 +62,7 @@ while (proceed==true) {
   phase++;
   }
 bindings=soc_freebindinf(bindings);
+#undef  OPEP
 }
 /*
 ^L
@@ -66,7 +74,7 @@ bindings=soc_freebindinf(bindings);
 /*      return zero if everything right                 */
 /*                                                      */
 /********************************************************/
-int rec_modemodrec(_Bool mode)
+PUBLIC int rec_modemodrec(_Bool mode)
 
 {
 #define OPEP    "mode.c:rec_modemoderec"
index 8911b5e920f89d3eaaf27df668ca26e1ad479dbf..72ec658e204edff6428830f710f70742a23507d2 100644 (file)
@@ -67,7 +67,7 @@ static void usage_aid(char *name)
 /*     list                                            */
 /*                                                     */
 /********************************************************/
-ARGTYP *par_freeparams(ARGTYP *params)
+PUBLIC ARGTYP *par_freeparams(ARGTYP *params)
 
 {
 if (params!=(ARGTYP *)0) {
@@ -97,7 +97,7 @@ return params;
 /*     on error return an null argtype                 */
 /*                                                     */
 /********************************************************/
-ARGTYP *par_getparams(int argc,char * const argv[],const char *optstring)
+PUBLIC ARGTYP *par_getparams(int argc,char * const argv[],const char *optstring)
 
 {
 ARGTYP *params;
@@ -118,7 +118,8 @@ while (((c=getopt(argc,argv,optstring))!=EOF)&&(params!=(ARGTYP *)0)) {
       break;
     case 'f'   :       //background/daemon mode
       foreground=true;
-      (void) fprintf(stderr,"%s-%s, foreground mode requested\n",shortname,rou_getversion());
+      (void) fprintf(stderr,"%s-%s, foreground mode requested\n",
+                            shortname,rou_getversion());
       break;
     case 'h'   :       //requestion program help
       (void) fprintf(stderr,"%s-%s\n",shortname,rou_getversion());
@@ -131,7 +132,8 @@ while (((c=getopt(argc,argv,optstring))!=EOF)&&(params!=(ARGTYP *)0)) {
       rootdir=strdup(optarg);
       break;
     case 'v'   :
-      (void) fprintf(stderr,"%s:\tVersion <%s>\n",shortname,rou_getversion());
+      (void) fprintf(stderr,"%s:\tVersion <%s>\n",
+                            shortname,rou_getversion());
       (void) exit(0);           //just display version
       break;
     default    :
@@ -164,7 +166,7 @@ return params;
 /*      return zero if everything right                 */
 /*                                                      */
 /********************************************************/
-int par_modeunipar(_Bool mode)
+PUBLIC int par_modeunipar(_Bool mode)
 
 {
 #define OPEP    "unipar.c:uni_modeunipar"
index 8b00246b48a9d3d22847853a904246e7f75854d5..040eb6bb7907418a353bd55657f2d71b1541ba1f 100644 (file)
@@ -51,7 +51,7 @@ static  _Bool modopen;                  //boolean module open/close
 /*          kernel.core_pattern=./core.%e.%p            */
 /*                                                     */
 /********************************************************/
-void prc_allow_core_dump()
+PUBLIC void prc_allow_core_dump()
 
 {
 struct rlimit limites;
@@ -75,7 +75,7 @@ if (setrlimit(RLIMIT_CORE,&limites)<0) {
 /*     to leave the zombie stat.                       */
 /*                                                     */
 /********************************************************/
-void prc_nozombie()
+PUBLIC void prc_nozombie()
 
 {
 while (waitpid(-1,(int *)0,WNOHANG)>0);
@@ -89,7 +89,7 @@ while (waitpid(-1,(int *)0,WNOHANG)>0);
 /*     still up and running.                           */
 /*                                                      */
 /********************************************************/
-_Bool prc_checkprocess(pid_t pidnum)
+PUBLIC _Bool prc_checkprocess(pid_t pidnum)
 
 {
 #define        SIGCHECK 0      //signal to check if process
@@ -123,7 +123,7 @@ return status;
 /*     false otherwise.                                */
 /*                                                      */
 /********************************************************/
-_Bool prc_locking(const char *lockname,int lock,int tentative)
+PUBLIC _Bool prc_locking(const char *lockname,int lock,int tentative)
 
 {
 #define        OPEP    "uniprc.c:lck_locking,"
@@ -257,7 +257,7 @@ return done;
 /*     Return the child process id.                    */
 /*                                                      */
 /********************************************************/
-pid_t prc_divedivedive()
+PUBLIC pid_t prc_divedivedive()
 
 {
 #define        OPEP    "uniprc:rou_divedivedive,"
@@ -289,7 +289,7 @@ return childpid;
 /*     Procedure to free used by "title" struct memory.*/
 /*                                                      */
 /********************************************************/
-void prc_cleantitle()
+PUBLIC void prc_cleantitle()
 
 {
 if (title!=(TITLTYP *)0) {
@@ -318,7 +318,7 @@ if (title!=(TITLTYP *)0) {
 /*     from proc (ps ax)                               */
 /*                                                      */
 /********************************************************/
-void prc_preptitle(int argc,char *argv[],char *env[])
+PUBLIC void prc_preptitle(int argc,char *argv[],char *env[])
 
 {
 char *lastend;
@@ -359,7 +359,7 @@ title->max=lastend-title->title;
 /*      CLI (commande line interface) "ps"              */
 /*                                                      */
 /********************************************************/
-void prc_settitle(const char *fmt,...)
+PUBLIC void prc_settitle(const char *fmt,...)
 
 {
 va_list args;
@@ -381,7 +381,7 @@ va_end(args);
 /*      return zero if everything right                 */
 /*                                                      */
 /********************************************************/
-int prc_modeuniprc(_Bool mode)
+PUBLIC int prc_modeuniprc(_Bool mode)
 
 {
 #define OPEP    "unipar.c:prc_modeuniprc"
index 01c96c659bef1ec307e44d1f3024121225f2593a..1f26107dbaf35e548f8c867b0260ef3ba2d236b9 100644 (file)
@@ -80,7 +80,7 @@ prvon=onoff;
 /*      flag accordingly;                               */
 /*                                                      */
 /********************************************************/
-void sig_alrm(int sig)
+PUBLIC void sig_alrm(int sig)
 
 {
 #define OPEP    "unisig.c:sig_alrm"
@@ -134,7 +134,7 @@ switch (sig)
 /*      bay application                                 */
 /*                                                      */
 /********************************************************/
-void sig_trapsignal(_Bool onoff,sighandler_t trap)
+PUBLIC void sig_trapsignal(_Bool onoff,sighandler_t trap)
 
 {
 #define OPEP    "modbck.c:settrap"
@@ -205,7 +205,7 @@ alldone=onoff;
 /*      return zero if everything right                 */
 /*                                                      */
 /********************************************************/
-int sig_modeunisig(_Bool mode)
+PUBLIC int sig_modeunisig(_Bool mode)
 
 {
 #define OPEP    "unisig.c:sig_modeunisig"
index 22f9fbbfca13ea8fdf3339fbe4c635dabb65cea8..f9c5efbdd6de0c50f9e319b570a56db92dece21a 100644 (file)
@@ -17,6 +17,7 @@
 
 #include       "subrou.h"
 #include       "unisoc.h"
+#include       "unisoc.h"
 
 //Need to have GNU_SOURCE define within CFLAGS
 #ifdef  AI_ALL
@@ -207,15 +208,14 @@ return bindings;
 */
 /********************************************************/
 /*                                                     */
-/*      Procedure to bind the socketc.                  */
-/*      IP/PORT.                                        */
+/*      Procedure to bind to one socket and return the  */
+/*      socket handle                                   */
 /*                                                     */
 /********************************************************/
-PUBLIC int soc_setbind(SOCTYP *binding)
+PUBLIC int soc_onelisten(SOCTYP *binding)
 
 {
-#define OPEP    "unisoc.c:soc_setbind"
-
+#define OPEP    "unisoc.c:soc_onelisten"
 
 int soc;
 struct addrinfo hints;
@@ -271,6 +271,159 @@ return soc;
 ^L
 */
 /********************************************************/
+/*                                                     */
+/*      Procedure to unbind from one socket.            */
+/*                                                     */
+/********************************************************/
+PUBLIC void soc_oneclose(SOCTYP *binding)
+
+{
+#define OPEP    "unisoc.c:soc_oneclose"
+
+int phase;
+_Bool proceed;
+
+phase=0;
+proceed=true;
+while (proceed==true) {
+  switch (phase) {
+    case 0      :       //sanity check
+      if (binding==(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) {
+        phase=999;      //yes, no need to go further
+        }
+      break;
+    case 2      :       //check if time to retry binding
+      if (shutdown(binding->handle,SHUT_RDWR)<0) 
+        (void) rou_alert(0,"%s Unbale to shutdown socket (Bug!?, error=<%s>)",
+                            OPEP,strerror(errno));
+      
+      break;
+    case 3      :       //closing the the socket
+      if (close(binding->handle)<0) 
+        (void) rou_alert(0,"%s Unbale to close socket (Bug!?, error=<%s>)",
+                            OPEP,strerror(errno));
+      binding->handle=-1;
+      break;
+    default     :       //SAFE Guard
+      proceed=false;
+      break;
+    }
+  phase++;
+  }
+#undef  OPEP
+}
+/*
+^L
+*/
+/********************************************************/
+/*                                                     */
+/*      Procedure to bind to a set of socket and return */
+/*      true if all socket are open ready.              */
+/*                                                     */
+/********************************************************/
+PUBLIC int soc_mullisten(SOCTYP **bindings)
+
+{
+#define OPEP    "unisoc.c:soc_mullisten"
+
+_Bool ready;
+uid_t gid;
+uid_t uid;
+int phase;
+_Bool proceed;
+
+ready=false;
+gid=getegid();        //let be back to root if needed
+uid=geteuid();        //to open device on < 1024 por
+phase=0;
+proceed=true;
+while (proceed==true) {
+  switch (phase) {
+    case 0      :       //checking if the point is ready; 
+      if (bindings==(SOCTYP **)0) {
+        (void) rou_alert(0,"%s bindings list pointer is NULL (Bug!?)",OPEP);
+        phase=999;      //not going further
+        }
+      break;
+    case 1      :       //
+      if ((setegid(getgid())<0)||(seteuid(getuid())<0)) {
+        (void) rou_core_dump("%s, Unable to get root privilege! "
+                             "(program config? error=<%s>)",
+                             OPEP,strerror(errno));
+        //program is crashing HARD
+        }
+      break;
+    case 2      :       //binding on all interface
+      ready=true;
+      while (*bindings!=(SOCTYP *)0) {
+        (*bindings)->handle=soc_onelisten(*bindings);
+        if ((*bindings)->handle<0)
+          ready=false;  //Trouble?
+        bindings++;
+        }
+      break;
+    default     :       //SAFE Guard
+      proceed=false;
+      break;
+    }
+  phase++;
+  }
+(void) setegid(gid);    //set back the user,group ID
+(void) seteuid(uid);
+return ready;
+#undef  OPEP
+}
+/*
+^L
+*/
+/********************************************************/
+/*                                                     */
+/*      Procedure to close all socket within a socket   */
+/*      list.                                           */
+/*                                                     */
+/********************************************************/
+PUBLIC void soc_mulclose(SOCTYP **bindings)
+
+{
+#define OPEP    "unisoc.c:soc_mulclose"
+
+int phase;
+_Bool proceed;
+
+phase=0;
+proceed=true;
+while (proceed==true) {
+  switch (phase) {
+    case 0      :       //checking if the point is ready; 
+      if (bindings==(SOCTYP **)0) {
+        (void) rou_alert(0,"%s bindings list pointer is NULL (Bug!?)",OPEP);
+        phase=999;      //not going further
+        }
+      break;
+    case 1      :       //
+      while (*bindings!=(SOCTYP *)0) {
+        (void) soc_oneclose(*bindings);
+        bindings++;
+        }
+      break;
+    default     :       //SAFE Guard
+      proceed=false;
+      break;
+    }
+  phase++;
+  }
+#undef  OPEP
+}
+/*
+^L
+*/
+/********************************************************/
 /*                                                      */
 /*     Procedure to "open/close" module and do         */
 /*      homework purpose                                */
index b636517841f51a4d1fec699d70ba24a8f1a47fac..975948b5dfcb33c5b81a5fcf6f6ba7680a3e98ec 100644 (file)
@@ -37,8 +37,18 @@ extern SOCTYP **soc_freebindinf(SOCTYP **bindref);
 extern SOCTYP **soc_mkbindinf(SOCTYP **bindings,PROTYP proto,
                               const char *ip,const char *port,int iteration);
 
-//procedure to open a socketand retunr handle to socket
-extern int soc_setbind(SOCTYP *binding);
+//procedure to open ONE socket and return handle to socket
+extern int soc_onelisten(SOCTYP *binding);
+
+//procedure to close ONE socket
+extern void soc_oneclose(SOCTYP *binding);
+
+//procedure to open multiple socket and return true if
+//all socket are open
+extern int soc_mullisten(SOCTYP **bindings);
+
+//procedure to close all sockets
+extern void soc_mulclose(SOCTYP **bindings);
 
 //homework to be done before starting/stopping module.
 extern int soc_modeunisoc(_Bool mode);