]> SAFE projects GIT repository - jmp/mailleur/commitdiff
working on reading tcp socket
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sat, 3 Aug 2024 01:03:44 +0000 (21:03 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sat, 3 Aug 2024 01:03:44 +0000 (21:03 -0400)
lib/gestcp.c
lib/gestcp.h
lib/subrou.c

index 4df72513c81c379afeee0cccae5264f3ead999f2..20bb4ee34f9470b2dfc1282ffe5ecd070c43547a 100644 (file)
@@ -92,6 +92,10 @@ if (contact!=(CONTYP *)0) {
                             OPEP,strerror(errno));
       }
     }
+  if (contact->carpile!=(char *)0)
+    (void) free(contact->carpile);
+  if (contact->EOL!=(char *)0)
+    (void) free(contact->EOL);
   if (contact->peerip!=(char *)0)
     (void) free(contact->peerip);
   if (contact->locname!=(char *)0)
@@ -107,6 +111,112 @@ return contact;
 */
 /********************************************************/
 /*                                                      */
+/*     Procedure to locate CRLF within the current line*/
+/*      assigne memory and store carpile contents       */
+/*                                                      */
+/********************************************************/
+PUBLIC int getnextline(CONTYP *tact,char **lineptr)
+
+{
+int got;
+char *eol;
+int phase;
+_Bool proceed;
+
+*lineptr=(char *)0;
+got=0;
+eol=(char *)0;
+phase=0;
+proceed=true;
+while (proceed==true) {
+  switch (phase) {
+    case 0      :       //Do we have dat in carpile
+      if (tact->carin==0)
+        phase=999;      //No char,no need to check for line
+      break;
+    case 1      :       //Do we have a CRLF
+      eol=strstr(tact->carpile,tact->EOL);
+      if (eol==(char *)0) 
+        phase=999;      //No End Of Line yet
+      break;
+    case 2      :       //duplicating carpile
+      *lineptr=calloc(tact->carin,sizeof(char));
+      *eol='\000';
+      (void) strcpy(*lineptr,tact->carpile);
+      (void) strcat(*lineptr,tact->EOL);
+      break;
+    case 3      :       //managing carpile
+      tact->carin-=strlen(*lineptr);
+      if (tact->carin>0) 
+        (void) memove(tact->carpile,tact->carpile+strlen(*lineptr),tact->carin);
+      tact->carpile[tact->carin]='\000';
+      break;
+    default     :       //SAFE guard
+      proceed=false;
+      break;
+    }
+  phase++;
+  }
+return got;
+}
+/*
+^L
+*/
+/********************************************************/
+/*                                                      */
+/*     Procedure to read char from remote peer, wait   */
+/*      for a CRLF AS EOL.                              */
+/*                                                      */
+/********************************************************/
+PUBLIC int tcp_getline(CONTYP *contact,u_long usec,char **lineptr)
+
+{
+#define OPEP    "gestcp.c:tcp_getline"
+
+int got;
+int phase;
+_Bool proceed;
+
+got=0;
+phase=0;
+proceed=true;
+while (proceed==true) {
+  switch (phase) {
+    case 0      :
+      if (contact==(CONTYP *)0) {
+        (void) rou_alert(0,"%s socket pointer is NULL (Bug!?)",OPEP);
+        phase=999;      //trouble trouble
+        }
+      break;
+    case 1      :       //get nextline
+      if ((got=getnextline(contact,lineptr))>0)
+        phase=999;      //we got a line.
+      break;
+    case 2      :       //waiting for new character presence
+      switch (waitforchar(contact,&usec)) {
+        case -1         :       //trouble? signal?
+        case  0         :       //normal time out
+          break;                //no need to read line
+        default         :       //char available
+          (void) readmaxchar(contact);
+          phase=0;              //check for new line
+          break;
+        }
+      break;
+    default     :       //SAFE Guard
+      proceed=false;
+      break;
+    }
+  phase++;
+  }
+return got;
+#undef  OPEP
+}
+/*
+^L
+*/
+/********************************************************/
+/*                                                      */
 /*     Procedure to send data to a tcp socket.         */
 /*      return the number of character transmitted, is  */
 /*      unable to send char return -1;                  */
@@ -153,6 +263,8 @@ PUBLIC CONTYP *tcp_getcontact(SOCPTR *socptr,int pos)
 
 {
 #define OPEP    "gestcp.c:tcp_getcontact"
+#define MXCARIN 200             //maximun number of char 
+                                //within carpile
 
 CONTYP *contact;
 int phase;
@@ -172,6 +284,10 @@ while (proceed==true) {
     case 1      :       //waiting from contact
       contact=calloc(1,sizeof(CONTYP));
       contact->socptr=socptr;
+      contact->maxcarin=MXCARIN;
+      contact->carin=0;
+      contact->EOL=strdup(CRLF);
+      contact->carpile=(char *)calloc(contact->maxcarin,sizeof(char));
       if ((contact->channel=soc_accept(socptr,pos))<0) {
         (void) rou_alert(0,"%s Unable to open contact",OPEP);
         contact=freecontact(contact);
@@ -212,6 +328,7 @@ while (proceed==true) {
   phase++;
   }
 return contact;
+#undef  MXCARIN
 #undef  OPEP
 }
 /*
index efc3a5af82ebf6ee7aff1334a674ac3295091f6e..c06b30023cee76618817231f7a94c4db6e6656e8 100644 (file)
@@ -16,10 +16,14 @@ typedef struct  {
         char *locname;  //socket local hostname
         char *peerip;   //socket remote peer IP
         SOCPTR *socptr; //established contact context
+        int maxcarin;   //absolute number within carin
+        char *EOL;      //End of line marker
+        int carin;      //number of char within incpt;
+        char *carpile;  //area to store incoming char
         }CONTYP;
 
-//procedure to free contact
-extern CONTYP *tcp_freecontact(CONTYP *contact);
+//read a line from contact up to CRLF
+extern int tcp_getline(CONTYP *contact,u_long usec,char **lineptr);
 
 //Transmit formated data to the contact channel
 extern int tcp_write(CONTYP *contact,char *buffer,int parnum);
index d55f440a16ec7226541a74ac88076aaa385591dd..290f7e44570a8fdd51de4aff3aa28787eec65da3 100644 (file)
@@ -20,7 +20,7 @@
 
 //version definition 
 #define VERSION "0.3"
-#define RELEASE "16"
+#define RELEASE "17"
 
 //Public variables
 PUBLIC  int debug=0;            //debug level