]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Need to open socket
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Thu, 11 Jul 2024 04:51:49 +0000 (00:51 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Thu, 11 Jul 2024 04:51:49 +0000 (00:51 -0400)
lib/Makefile
lib/modrec.c
lib/unipar.c
lib/unipar.h
lib/unisoc.c [new file with mode: 0644]
lib/unisoc.h [new file with mode: 0644]

index 99c8e8c821819bf02ec0fbc693c3ecc2284484c5..21649ac27409c889dbe9062fe5a1d6d62f371ec4 100644 (file)
@@ -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`
 #--------------------------------------------------------------------
index 9b68d17267d5569c67905690c9a9df4a963ad4b1..243d0a9800e95f8b34d0e3712d90ccb8217d9793 100644 (file)
@@ -5,11 +5,13 @@
 /*                                                     */
 /********************************************************/
 #include        <stdio.h>
+#include        <string.h>
 #include        <unistd.h>
 
 #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);
index 2c9b3ca2d5dbbae127b7111a8355ef9f99a33634..8911b5e920f89d3eaaf27df668ca26e1ad479dbf 100644 (file)
@@ -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             */
 /*                                                     */
index 097bf0944262061a95174b58c7bdcb9b6540e824..e992c9ffcc116b1682d4701e12957c53808ce6b5 100644 (file)
@@ -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 (file)
index 0000000..0a1e82e
--- /dev/null
@@ -0,0 +1,139 @@
+// vim: smarttab tabstop=8 shiftwidth=2 expandtab
+/********************************************************/
+/*                                                     */
+/*     Low level subroutine declaration                */
+/*     to handle TCP socket.                           */
+/*                                                     */
+/********************************************************/
+#include        <sys/types.h>
+#include        <sys/socket.h>
+#include        <netdb.h>
+#include        <stdio.h>
+#include        <string.h>
+
+#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 (file)
index 0000000..b02b943
--- /dev/null
@@ -0,0 +1,26 @@
+// vim: smarttab tabstop=8 shiftwidth=2 expandtab
+/********************************************************/
+/*                                                     */
+/*     Low level subroutine declaration                */
+/*     to handle tcp Socket.                           */
+/*                                                     */
+/********************************************************/
+#ifndef        UNISOC
+#define UNISOC
+
+#include        <stdbool.h>
+#include        <time.h>
+
+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