From 2de6261f1f60d2fdf6b085965b7da0bb910c4aa1 Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Thu, 11 Jul 2024 00:51:49 -0400 Subject: [PATCH] Need to open socket --- lib/Makefile | 11 +++- lib/modrec.c | 14 +++++- lib/unipar.c | 3 +- lib/unipar.h | 4 +- lib/unisoc.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/unisoc.h | 26 ++++++++++ 6 files changed, 190 insertions(+), 7 deletions(-) create mode 100644 lib/unisoc.c create mode 100644 lib/unisoc.h diff --git a/lib/Makefile b/lib/Makefile index 99c8e8c..21649ac 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -17,7 +17,7 @@ clean : #-------------------------------------------------------------------- OBJS= \ modrec.o \ - unipar.o uniprc.o unisig.o \ + unipar.o uniprc.o unisig.o unisoc.o \ subrou.o objs : $(OBJS) @@ -27,6 +27,9 @@ objs : $(OBJS) #Dependances modrec.o: \ subrou.h \ + uniprc.h \ + unisig.h \ + unisoc.h \ modrec.h modrec.c unipar.o: \ @@ -41,6 +44,10 @@ unisig.o: \ subrou.h \ unisig.h unisig.c +unisoc.o: \ + subrou.h \ + unisoc.h unisoc.c + subrou.o: \ subrou.h subrou.c @@ -57,7 +64,7 @@ toremake: Makefile #-------------------------------------------------------------------- CC = gcc LD = gcc -CFLAGS = -Wall $(OPTIME) +CFLAGS = -Wall -D_GNU_SOURCE $(OPTIME) LIBAI = libAI PAR = -j`/usr/bin/getconf _NPROCESSORS_ONLN` #-------------------------------------------------------------------- diff --git a/lib/modrec.c b/lib/modrec.c index 9b68d17..243d0a9 100644 --- a/lib/modrec.c +++ b/lib/modrec.c @@ -5,11 +5,13 @@ /* */ /********************************************************/ #include +#include #include #include "subrou.h" #include "uniprc.h" #include "unisig.h" +#include "unisoc.h" #include "modrec.h" static _Bool modopen; //boolean module open/close @@ -24,9 +26,13 @@ static _Bool modopen; //boolean module open/close PUBLIC void rec_handlesmtp() { +BINTYP binding; int phase; _Bool proceed; +(void) memset(&binding,'\000',sizeof(BINTYP)); +binding.ip=strdup("127.0.0.25"); +binding.port=strdup("2525"); phase=0; proceed=true; while (proceed==true) { @@ -34,7 +40,11 @@ while (proceed==true) { case 0 : //looping forever email receiving processes (void) prc_settitle("emlrec with pid='%06d'",getpid()); break; - case 1 : //Terminating all remaining process + case 1 : //Opening a channel + if (soc_setbind(&binding)<0) + phase=999; //doc not open! + break; + case 2 : //Terminating all remaining process (void) sleep(1); if (hangup==false) phase--; //looping forever @@ -70,8 +80,10 @@ if (mode!=modopen) { (void) rou_modesubrou(mode); (void) prc_modeuniprc(mode); (void) sig_modeunisig(mode); + (void) soc_modeunisoc(mode); break; case false : + (void) soc_modeunisoc(mode); (void) sig_modeunisig(mode); (void) prc_modeuniprc(mode); (void) rou_modesubrou(mode); diff --git a/lib/unipar.c b/lib/unipar.c index 2c9b3ca..8911b5e 100644 --- a/lib/unipar.c +++ b/lib/unipar.c @@ -2,7 +2,7 @@ /********************************************************/ /* */ /* Low level subroutine declaration */ -/* to hanle an argv list and extract */ +/* to handle an argv list and extract */ /* parameters. */ /* */ /********************************************************/ @@ -19,7 +19,6 @@ static _Bool modopen; //module open/close status ^L */ /********************************************************/ -/* */ /* */ /* Procedure to init the argument list */ /* */ diff --git a/lib/unipar.h b/lib/unipar.h index 097bf09..e992c9f 100644 --- a/lib/unipar.h +++ b/lib/unipar.h @@ -2,7 +2,7 @@ /********************************************************/ /* */ /* Low level subroutine declaration */ -/* to hanle an argv list and extract */ +/* to handle an argv list and extract */ /* parameters. */ /* */ /********************************************************/ @@ -23,7 +23,7 @@ extern ARGTYP *par_freeparams(ARGTYP *params); //allocated memory and parse an argment list extern ARGTYP *par_getparams(int argc,char * const argv[],const char *optstring); -//homework to be done before starting/stoping module. +//homework to be done before starting/stopping module. extern int par_modeunipar(_Bool mode); #endif diff --git a/lib/unisoc.c b/lib/unisoc.c new file mode 100644 index 0000000..0a1e82e --- /dev/null +++ b/lib/unisoc.c @@ -0,0 +1,139 @@ +// vim: smarttab tabstop=8 shiftwidth=2 expandtab +/********************************************************/ +/* */ +/* Low level subroutine declaration */ +/* to handle TCP socket. */ +/* */ +/********************************************************/ +#include +#include +#include +#include +#include + +#include "subrou.h" +#include "unisoc.h" + +//Need to have GNU_SOURCE define within CFLAGS +#ifdef AI_ALL +#define HINTFLG AI_ALL|AI_CANONNAME|AI_CANONIDN +#else /*AI_ALL is not defined in linux <= rh9 */ +#define HINTFLG AI_CANONNAME +#endif + + +static _Bool modopen; //module open/close status +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to bind a sockt to a specific */ +/* IP/PORT. */ +/* */ +/********************************************************/ +static int bindhandle(struct addrinfo *ai,BINTYP *binding) + +{ +return -1; +} +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to bind the socketc. */ +/* IP/PORT. */ +/* */ +/********************************************************/ +PUBLIC int soc_setbind(BINTYP *binding) + +{ +#define OPEP "unisoc.c:soc_setbind" + + +int soc; +struct addrinfo hints; +struct addrinfo *tobind; +int phase; +_Bool proceed; + +soc=-1; +(void) memset(&hints,'\000',sizeof(hints)); +hints.ai_family=PF_UNSPEC; +hints.ai_flags=HINTFLG; +hints.ai_socktype=SOCK_STREAM; +tobind=(struct addrinfo *)0; +phase=0; +proceed=true; +while (proceed==true) { + switch (phase) { + case 0 : //sanity check + if (binding==(BINTYP *)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)) + 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) { + (void) rou_alert(0,"%s, Unable to find a address about " + "IP:port '%s:%s' (error='%s')", + binding->ip,binding->port,gai_strerror(status)); + phase=999; //no need to go further + } + break; + case 3 : //binding socket + soc=bindhandle(tobind,binding); + break; + default : //SAFE Guard + proceed=false; + break; + } + phase++; + } +return soc; +#undef OPEP +} +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to "open/close" module and do */ +/* homework purpose */ +/* return zero if everything right */ +/* */ +/********************************************************/ +int soc_modeunisoc(_Bool mode) + +{ +#define OPEP "unidoc.c:soc_modeunisoc" + +int status; + +status=0; +if (mode!=modopen) { + (void) rou_modesubrou(mode); + switch ((int)mode) { + case true : + break; + case false : + break; + default : + (void) fprintf(stderr,"Calling %s with wrong mode='%d' (Bug?!):", + OPEP,(int)mode); + status=-1; + break; + } + modopen=mode; + } +return status; +#undef OPEP +} diff --git a/lib/unisoc.h b/lib/unisoc.h new file mode 100644 index 0000000..b02b943 --- /dev/null +++ b/lib/unisoc.h @@ -0,0 +1,26 @@ +// vim: smarttab tabstop=8 shiftwidth=2 expandtab +/********************************************************/ +/* */ +/* Low level subroutine declaration */ +/* to handle tcp Socket. */ +/* */ +/********************************************************/ +#ifndef UNISOC +#define UNISOC + +#include +#include + +typedef struct { + char *ip; //Binding IP //IPV4 or IPV6 + char *port; //Binding Port + time_t lasttry; //successful binding last time + }BINTYP; + +//procedure to open a socketand retunr handle to socket +extern int soc_setbind(BINTYP *binding); + +//homework to be done before starting/stopping module. +extern int soc_modeunisoc(_Bool mode); + +#endif -- 2.47.3