From 381fa5ff27a695149f7b807bc3ae123ce639512b Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Thu, 10 Jul 2025 13:24:49 -0400 Subject: [PATCH] Starting to implement lock table function --- lib/Makefile | 7 ++++- lib/devsql.c | 13 ++++++++++ lib/devsql.h | 3 +++ lib/gessql.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/gessql.h | 17 +++++++++++++ lib/gestcp.c | 2 +- lib/gestcp.h | 3 ++- lib/modrec.c | 12 ++++++--- lib/unisql.c | 22 +++++++++++++++- lib/unisql.h | 14 +++++++++- sql/mailleur.sql | 4 +-- 11 files changed, 152 insertions(+), 11 deletions(-) create mode 100644 lib/gessql.c create mode 100644 lib/gessql.h diff --git a/lib/Makefile b/lib/Makefile index 45a07b2..185a0f4 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -21,7 +21,7 @@ clean : OBJS= \ modrec.o \ lvleml.o \ - gesspf.o gestcp.o geseml.o \ + geseml.o gestcp.o gesspf.o gessql.o \ devlog.o devsoc.o devsql.o \ unidig.o unidns.o unieml.o unipar.o \ uniprc.o unisig.o unisql.o unitls.o \ @@ -46,6 +46,7 @@ modrec.o: \ lvleml.o: \ subcnv.h subrou.h \ unidig.h unidns.h unieml.h \ + gestcp.h gessql.h \ lvleml.h lvleml.c gesspf.o: \ @@ -65,6 +66,10 @@ gestcp.o: \ unisig.h \ gestcp.h gestcp.c +gessql.o: \ + devsql.h \ + gessql.h gessql.c + devlog.o: \ subrou.h \ uniprc.h \ diff --git a/lib/devsql.c b/lib/devsql.c index 47d543e..6f9ad80 100644 --- a/lib/devsql.c +++ b/lib/devsql.c @@ -884,3 +884,16 @@ return isok; #undef DELACT #undef OPEP } +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to lock access to a table. */ +/* */ +/********************************************************/ +PUBLIC _Bool sql_locktable(SQLPTR *sqlptr,char *tblname,_Bool action) + +{ +return action; +} diff --git a/lib/devsql.h b/lib/devsql.h index a9b68fa..212ba3d 100644 --- a/lib/devsql.h +++ b/lib/devsql.h @@ -32,4 +32,7 @@ extern _Bool sql_mngses(SQLPTR *sqlptr,SQLENUM action,SESTYP **ses); //procedure to manage action stats on email exhange status extern _Bool sql_mngact(SQLPTR *sqlptr,SQLENUM action,ACTTYP *act); +//procedure to locak access to a full table +extern _Bool sql_locktable(SQLPTR *sqlptr,char *tblname,_Bool action); + #endif diff --git a/lib/gessql.c b/lib/gessql.c new file mode 100644 index 0000000..b2fba5a --- /dev/null +++ b/lib/gessql.c @@ -0,0 +1,66 @@ +// vim: smarttab tabstop=8 shiftwidth=2 expandtab +/********************************************************/ +/* */ +/* Module to manage complexe SQL request */ +/* */ +/********************************************************/ + +#include "subrou.h" +#include "gessql.h" + +#define REMOTES "remotes" //Table about remotes site + +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to increase (of decrease the remote */ +/* IP connection number. */ +/* */ +/********************************************************/ +PUBLIC int sql_newconnect(SQLPTR *sqlptr,char *rmtip,int delta) + +{ +#define OPEP "gessql.c:sql_newconnect," + +int links; +_Bool locked; +SMTTYP *smtpdata; +int phase; +_Bool proceed; + +links=0; +locked=false; +smtpdata=(SMTTYP *)0; +phase=0; +proceed=true; +while (proceed==true) { + switch (phase) { + case 0 : //Locking table + if ((locked=sql_locktable(sqlptr,REMOTES,true))==false) { + (void) rou_alert(0,"%s Unable to lock %s table (Bug?)", + OPEP,REMOTES); + phase=999; //Trouble trouble + } + break; + case 1 : //loading record + break; + case 2 : //creating record if not exiting + break; + case 3 : //updating record + smtpdata->links+=delta; + links=smtpdata->links; + smtpdata=sql_freesmtp(smtpdata); + break; + default : //SAFE Guard + if (locked==true) + (void) sql_locktable(sqlptr,REMOTES,false); + proceed=false; + break; + } + phase++; + } +return links; +#undef OPEP +} diff --git a/lib/gessql.h b/lib/gessql.h new file mode 100644 index 0000000..7d0ab05 --- /dev/null +++ b/lib/gessql.h @@ -0,0 +1,17 @@ +// vim: smarttab tabstop=8 shiftwidth=2 expandtab +/************************************************/ +/* */ +/* gestion level fto manage sql request */ +/* */ +/************************************************/ +#ifndef GESSQL +#define GESSQL + +#include + +#include "devsql.h" + +//Procedure to increment (or decrement) the remoteip +//connection number +extern int sql_newconnect(SQLPTR *sqlptr,char *rmtip,int delta); +#endif diff --git a/lib/gestcp.c b/lib/gestcp.c index 30095fc..2252aaa 100644 --- a/lib/gestcp.c +++ b/lib/gestcp.c @@ -1,7 +1,7 @@ // vim: smarttab tabstop=8 shiftwidth=2 expandtab /********************************************************/ /* */ -/* Module to handle TCP communication */ +/* Module to manage TCP communication */ /* */ /********************************************************/ #include diff --git a/lib/gestcp.h b/lib/gestcp.h index 43e5d99..af3b656 100644 --- a/lib/gestcp.h +++ b/lib/gestcp.h @@ -1,7 +1,8 @@ // vim: smarttab tabstop=8 shiftwidth=2 expandtab /************************************************/ /* */ -/* Unit level to handle TCP communication */ +/* gestion level to handle TCP */ +/* communication */ /* */ /************************************************/ #ifndef GESTCP diff --git a/lib/modrec.c b/lib/modrec.c index e9da599..adfddeb 100644 --- a/lib/modrec.c +++ b/lib/modrec.c @@ -17,6 +17,7 @@ #include "unieml.h" #include "unisig.h" #include "devsoc.h" +#include "gessql.h" #include "gestcp.h" #include "lvleml.h" #include "modrec.h" @@ -174,13 +175,16 @@ while (proceed==true) { //(void) rou_alert(0,"JMPDBG %s phase='%d'",OPEP,phase); switch (phase) { case 0 : //waiting contact - (void) rou_checkleak(true); if ((contact=eml_getcontact(socptr,pos))==(CONTYP *)0) phase=999; //No contact! break; case 1 : //within forked process (void) prc_settitle("%s, incoming contact from [%s] on [%s:%s]", - appname,contact->peerip,contact->locname,contact->locserv); + appname,contact->peerip, + contact->locname,contact->locserv); + if (sql_newconnect(contact->sqlptr,contact->peerip,1)>1) { + phase=999; //need to explain no contact; + } break; case 2 : //do contact intstat=eml_docontact(contact); @@ -206,10 +210,10 @@ while (proceed==true) { } break; case 4 : //connection terminated - contact=eml_dropcontact(contact); - (void) rou_checkleak(false); break; default : //SAFE guard + if (contact!=(CONTYP *)0) + contact=eml_dropcontact(contact); proceed=false; break; } diff --git a/lib/unisql.c b/lib/unisql.c index 975aba6..52ec4fa 100644 --- a/lib/unisql.c +++ b/lib/unisql.c @@ -119,7 +119,7 @@ return usr; */ /********************************************************/ /* */ -/* Procedure to free memory used by a usesion */ +/* Procedure to free memory used by a session */ /* definition. */ /* */ /********************************************************/ @@ -140,6 +140,26 @@ return ses; /* */ +/********************************************************/ +/* */ +/* Procedure to free memory used by a remote SMTP */ +/* data record definition. */ +/* */ +/********************************************************/ +PUBLIC SMTTYP *sql_freesmtp(SMTTYP *smtpdata) + +{ +if (smtpdata!=(SMTTYP *)0) { + smtpdata->status=rou_freestr(smtpdata->status); + smtpdata->rmtip=rou_freestr(smtpdata->rmtip); + (void) free(smtpdata); + smtpdata=(SMTTYP *)0; + } +return smtpdata; +} +/* + +*/ /************************************************/ /* */ /* Routine to check if string is properly */ diff --git a/lib/unisql.h b/lib/unisql.h index 7903c54..dde32c4 100644 --- a/lib/unisql.h +++ b/lib/unisql.h @@ -39,7 +39,7 @@ typedef struct { u_long taille; //Email size }SESTYP; -//Tructure about email action status (managing email) +//Structure about email action status (managing email) typedef struct { char *sessid; //session id char code; //Email status code @@ -47,12 +47,24 @@ typedef struct { char **resp; //Multiline status }ACTTYP; +//Structure about remotes SMTP server +typedef struct { + time_t lupdate; //last update time + char *rmtip; //Remote IP number + int links; //Number of connection from this remote + int credit; //Remote credit number + char *status; //Remote record current status + }SMTTYP; + //procedure to free space used by an USRTYP extern USRTYP *sql_freeusr(USRTYP *usr); //procedure to free memory used by a SESTYP extern SESTYP *sql_freeses(SESTYP *ses); +//procedure to free memory used by a SMTTYP +extern SMTTYP *sql_freesmtp(SMTTYP *smtpdata); + //Procedure to check if string is properly coded according DB_LANG extern char *sql_checkencoding(char *strencoded); diff --git a/sql/mailleur.sql b/sql/mailleur.sql index b44c5d0..c5956c7 100644 --- a/sql/mailleur.sql +++ b/sql/mailleur.sql @@ -100,8 +100,8 @@ CREATE TABLE remotes ( lastupdate DBTIMESTAMP //record creation DFLT NOW(), remoteip TEXTUNIQUE, //remote IP number - connections INTEGER - DFLT 1, //how many time the remote connected + links INTEGER + DFLT 0, //how many time the remote connected credit INTEGER //Remote IP current credit (-100..+100) DFLT -100, status TEXT //'toscan','inscan','valide' -- 2.47.3