From: Jean-Marc Pigeon (Delson) Date: Sat, 3 Aug 2024 01:03:44 +0000 (-0400) Subject: working on reading tcp socket X-Git-Tag: tag-0.4~25 X-Git-Url: https://jmp-git.ovh.safe.ca/?a=commitdiff_plain;h=b61f2eee1b50261b60948768e26a05e5ae296d1d;p=jmp%2Fmailleur working on reading tcp socket --- diff --git a/lib/gestcp.c b/lib/gestcp.c index 4df7251..20bb4ee 100644 --- a/lib/gestcp.c +++ b/lib/gestcp.c @@ -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 } /* diff --git a/lib/gestcp.h b/lib/gestcp.h index efc3a5a..c06b300 100644 --- a/lib/gestcp.h +++ b/lib/gestcp.h @@ -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); diff --git a/lib/subrou.c b/lib/subrou.c index d55f440..290f7e4 100644 --- a/lib/subrou.c +++ b/lib/subrou.c @@ -20,7 +20,7 @@ //version definition #define VERSION "0.3" -#define RELEASE "16" +#define RELEASE "17" //Public variables PUBLIC int debug=0; //debug level