]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Adding module gestcp
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sat, 13 Jul 2024 21:01:09 +0000 (17:01 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sat, 13 Jul 2024 21:01:09 +0000 (17:01 -0400)
lib/Makefile
lib/gestcp.c [new file with mode: 0644]
lib/gestcp.h [new file with mode: 0644]
lib/modrec.c

index 21649ac27409c889dbe9062fe5a1d6d62f371ec4..c1066dc6dfa1571484674533b2f7bfe64f293282 100644 (file)
@@ -17,6 +17,7 @@ clean :
 #--------------------------------------------------------------------
 OBJS=                                          \
          modrec.o                              \
+         gestcp.o                              \
          unipar.o uniprc.o unisig.o unisoc.o   \
          subrou.o 
 
@@ -30,8 +31,13 @@ modrec.o:                                    \
           uniprc.h                             \
           unisig.h                             \
           unisoc.h                             \
+          gestcp.h                             \
           modrec.h modrec.c
 
+gestcp.o:                                      \
+          subrou.h                             \
+          gestcp.h gestcp.c
+
 unipar.o:                                      \
           subrou.h                             \
           unipar.h unipar.c
@@ -51,6 +57,12 @@ unisoc.o:                                    \
 subrou.o:                                      \
           subrou.h subrou.c
 
+gestcp.h:                                      \
+          unisoc.h                             \
+
+uniprc.h:                                      \
+          subrou.h                             \
+
 #--------------------------------------------------------------------
 
 toremake:  Makefile
diff --git a/lib/gestcp.c b/lib/gestcp.c
new file mode 100644 (file)
index 0000000..67db061
--- /dev/null
@@ -0,0 +1,86 @@
+// vim: smarttab tabstop=8 shiftwidth=2 expandtab
+/********************************************************/
+/*                                                     */
+/*     Module for signal handling level                */
+/*                                                     */
+/********************************************************/
+#include        <sys/wait.h>
+#include        <stdlib.h>
+#include        <stdio.h>
+#include        <string.h>
+
+#include        "subrou.h"
+#include        "gestcp.h"
+
+static  _Bool modopen;          //boolean module open/close
+/*
+^L
+*/
+/********************************************************/
+/*                                                      */
+/*     Procedure to wait for a remote client.          */
+/*      return all reference to the established contact.*/
+/*                                                      */
+/********************************************************/
+PUBLIC CONTYP *tcp_getcontact(SOCTYP *binding)
+
+{
+CONTYP *contact;
+
+contact=(CONTYP *)0;
+return contact;
+}
+/*
+^L
+*/
+/********************************************************/
+/*                                                      */
+/*     Procedure to wait for a remote client.          */
+/*      return all reference to contact.                 */
+/*                                                      */
+/********************************************************/
+PUBLIC CONTYP *tcp_dropcontact(CONTYP *contact)
+
+{
+return contact;
+}
+/*
+^L
+*/
+/********************************************************/
+/*                                                      */
+/*     Procedure to "open/close" module and do         */
+/*      homework purpose                                */
+/*      return zero if everything right                 */
+/*                                                      */
+/********************************************************/
+PUBLIC int tcp_modegestcp(_Bool mode)
+
+{
+#define OPEP    "unisig.c:sig_modeunisig"
+
+int status;
+
+status=0;
+if (mode!=modopen) {
+  (void) soc_modeunisoc(mode);
+  switch ((int)mode) {
+    case true     :
+      (void) rou_modesubrou(mode);
+      (void) soc_modeunisoc(mode);
+      break;
+    case false    :
+      (void) soc_modeunisoc(mode);
+      (void) rou_modesubrou(mode);
+      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/gestcp.h b/lib/gestcp.h
new file mode 100644 (file)
index 0000000..0cc20ef
--- /dev/null
@@ -0,0 +1,27 @@
+// vim: smarttab tabstop=8 shiftwidth=2 expandtab
+/************************************************/
+/*                                             */
+/*     Unit level to handle TCP communication  */
+/*                                             */
+/************************************************/
+#ifndef        GESTCP
+#define GESTCP
+
+#include        <stdbool.h>
+
+#include        "unisoc.h"
+
+typedef struct  {
+        void    *ptr;   //JMPDBG
+        }CONTYP;
+
+//wait for an incoming contact
+extern CONTYP *tcp_getcontact(SOCTYP *binding);
+
+//drop contact established by remote
+extern CONTYP *tcp_dropcontact(CONTYP *contact);
+
+//homework to be done before starting/stopping module.
+extern int tcp_modegestcp(_Bool mode);
+
+#endif
index 17ce74202b30f5ada80562d358e645b408d97e5d..e691a829171c5984c95973a6dc8063582917e7e0 100644 (file)
@@ -14,6 +14,7 @@
 #include        "uniprc.h"
 #include        "unisig.h"
 #include        "unisoc.h"
+#include        "gestcp.h"
 #include        "modrec.h"
 
 static  _Bool modopen;          //boolean module open/close
@@ -22,21 +23,41 @@ static  _Bool modopen;          //boolean module open/close
 */
 /********************************************************/
 /*                                                     */
-/*      kleenex procedure                               */
+/*      procedure to detect and accept a contact from   */
+/*      a remote TCP connection.                        */
 /*                                                     */
 /********************************************************/
-static void kludge(SOCTYP *binding,int attend)
+void contact(SOCTYP *binding) 
 
 {
-for (int i=0;i<attend;i++) {
-  (void) prc_settitle("Kluge for %d seconds, ip:port=<%s:%s>",
-                     attend-i,binding->ip,binding->port);
-  (void) sleep(1);
-  if ((hangup==true)||(reload==true)){
-    (void) rou_alert(0,"JMPDBG process terminated");
-    break;
+#define OPEP    "modrec.c:contact"
+
+CONTYP *contact;
+int phase;
+_Bool proceed;
+
+contact=(CONTYP *)0;
+phase=0;
+proceed=true;
+while (proceed==true) {
+  switch (phase) {
+    case 0      :       //do we have a contact
+      if ((contact=tcp_getcontact(binding))==(CONTYP *)0) {
+        phase=999;      //no going further
+        }
+      break;
+    case 1      :       //do contact
+      (void) sleep(40); //JMPDBG
+      break;
+    case 2      :       //close contact
+      contact=tcp_dropcontact(contact);
+      break;
+    default     :       //SAFE guard
+      break;
     }
+  phase++;
   }
+#undef  OPEP
 }
 /*
 \f
@@ -69,11 +90,8 @@ while (proceed==true) {
           }
         }
       break;
-    case 1      :       //dispating need process
+    case 1      :               //dispating need process
       for (int i=0;i<binding->iteration;i++) {
-        int attend;
-
-        attend=(random()%60)+10;
         if (binding->childs[i]!=(pid_t)0)
           continue;
         switch(binding->childs[i]=fork()) {
@@ -82,9 +100,9 @@ while (proceed==true) {
                                 "handle <%s:%s> socket (config?)",
                                 OPEP,binding->ip,binding->port);
             break;
-          case 0        :       //the dispatched process
-            (void) kludge(binding,attend);
-            (void) sleep(1);    //avoiding avalanche
+          case 0        :               //the dispatched process
+            (void) contact(binding);    //waiting, handling remote contact
+            (void) sleep(1);            //avoiding avalanche
             (void) exit(0);
             break;
           default       :
@@ -265,8 +283,10 @@ if (mode!=modopen) {
       (void) prc_modeuniprc(mode);
       (void) sig_modeunisig(mode);
       (void) soc_modeunisoc(mode);
+      (void) tcp_modegestcp(mode);
       break;
     case false    :
+      (void) tcp_modegestcp(mode);
       (void) soc_modeunisoc(mode);
       (void) sig_modeunisig(mode);
       (void) prc_modeuniprc(mode);