From 423274b8da8d07ca0e921aec5c4a15558e55acd7 Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Fri, 12 Jul 2024 02:58:59 -0400 Subject: [PATCH] Able to open and close multiple socket --- lib/modrec.c | 16 +++-- lib/unipar.c | 12 ++-- lib/uniprc.c | 18 +++--- lib/unisig.c | 6 +- lib/unisoc.c | 163 +++++++++++++++++++++++++++++++++++++++++++++++++-- lib/unisoc.h | 14 ++++- 6 files changed, 201 insertions(+), 28 deletions(-) diff --git a/lib/modrec.c b/lib/modrec.c index 0ec202b..e0bd281 100644 --- a/lib/modrec.c +++ b/lib/modrec.c @@ -4,6 +4,7 @@ /* Module to handle all email incoming */ /* */ /********************************************************/ +#include #include #include #include @@ -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" diff --git a/lib/unipar.c b/lib/unipar.c index 8911b5e..72ec658 100644 --- a/lib/unipar.c +++ b/lib/unipar.c @@ -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" diff --git a/lib/uniprc.c b/lib/uniprc.c index 8b00246..040eb6b 100644 --- a/lib/uniprc.c +++ b/lib/uniprc.c @@ -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" diff --git a/lib/unisig.c b/lib/unisig.c index 01c96c6..1f26107 100644 --- a/lib/unisig.c +++ b/lib/unisig.c @@ -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" diff --git a/lib/unisoc.c b/lib/unisoc.c index 22f9fbb..f9c5efb 100644 --- a/lib/unisoc.c +++ b/lib/unisoc.c @@ -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 */ diff --git a/lib/unisoc.h b/lib/unisoc.h index b636517..975948b 100644 --- a/lib/unisoc.h +++ b/lib/unisoc.h @@ -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); -- 2.47.3