From bb9cdc2fda3ddb421d78eface7da34b835942fbe Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Wed, 16 Jul 2025 09:04:33 -0400 Subject: [PATCH] Defining afn-getoneipnum --- lib/gesspf.c | 2 +- lib/subafn.c | 227 +++++++++++++++++++++++++++++++-------------------- lib/subafn.h | 10 ++- 3 files changed, 147 insertions(+), 92 deletions(-) diff --git a/lib/gesspf.c b/lib/gesspf.c index db50b62..8a0f5c1 100644 --- a/lib/gesspf.c +++ b/lib/gesspf.c @@ -352,7 +352,7 @@ while (proceed==true) { } break; case 2 : //computing the target ipnumber - if ((target=afn_getipnum(seq))==(AFNTYP **)0) { + if ((target=afn_getipnums(seq))==(AFNTYP **)0) { (void) rou_alert(0,"%s Unable to convert <%s> from " "domain <%s> (SPF bug?)", OPEP,seq,domain); diff --git a/lib/subafn.c b/lib/subafn.c index 9da2d0e..50d16e9 100644 --- a/lib/subafn.c +++ b/lib/subafn.c @@ -140,85 +140,6 @@ return areequal; */ /********************************************************/ /* */ -/* Procedure to transforme ONE clean IP to an AFN */ -/* structure. */ -/* */ -/********************************************************/ -static AFNTYP *getoneafn(char *onecleanipstr) - -{ -#define OPEP "subafn.c:getoneanf," -#define ZIPV4 "0.0.0.0" - -AFNTYP *afnnum; -int phase; -int proceed; - -afnnum=(AFNTYP *)0; -phase=0; -proceed=true; -while (proceed==true) { -#ifdef JMPDBG - (void) fprintf(stderr,"%s DEBUGING phase='%d', onecleanipstr=<%s>\n", - OPEP,phase,onecleanipstr); -#endif - switch (phase) { - case 0 : /*lets say ip format ok */ - afnnum=(AFNTYP *)calloc(1,sizeof(AFNTYP)); - afnnum->afntype=AF_INET; - afnnum->afnmask=32; - if (strchr(onecleanipstr,'|')==(char *)0) - phase++; //This is an IPV4 number - break; - case 1 : //IP is an IPV6 format - if (strcasestr(onecleanipstr,STRIPV6)==onecleanipstr) - onecleanipstr+=strlen(STRIPV6); - afnnum->afntype=AF_INET6; - afnnum->afnmask=128; - break; - case 2 : //lets convert it - switch (inet_pton(afnnum->afntype,onecleanipstr,afnnum->ip)) { - case 1 : //success - break; - case 0 : //Invalid IP number - (void) fprintf(stderr,"Unable to convert IP <%s> (Invalid IP number)\n", - onecleanipstr); - afnnum=afn_freeipnum(afnnum); - phase=999; //no need to go further - break; - case -1 : //Invalid IP number - (void) fprintf(stderr,"Unable to convert IP <%s> (error=<%s>)\n", - onecleanipstr,strerror(errno)); - afnnum=afn_freeipnum(afnnum); - phase=999; //no need to go further - break; - default : //Unexpected Result - (void) fprintf(stderr,"Unexpected status to convert IP <%s> " - "(error=<%s> BUG!?)\n", - onecleanipstr,strerror(errno)); - afnnum=afn_freeipnum(afnnum); - phase=999; //no need to go further - } - break; - case 3 : //lets convert it - afnnum->strnumip=afn_getstrip(afnnum); - //(void) printf("JMPDBG <%s> -> to <%s>\n", onecleanipstr,afnnum->strnumip); - break; - default : /*SAFE guard */ - proceed=false; - break; - } - phase++; - } -return afnnum; - -#undef OPEP -} -/* - -*/ -/********************************************************/ -/* */ /* Routine to fee memory used by an AFNTYP */ /* */ /********************************************************/ @@ -345,14 +266,91 @@ return afnnum; */ /********************************************************/ /* */ -/* Routine to convert clean string to be */ -/* IP number in AFNTYP structure. */ +/* Procedure to transforme ONE clean IP to an AFN */ +/* structure. */ /* */ /********************************************************/ -PUBLIC AFNTYP **afn_getipnum(char *cleanipstr) +PUBLIC AFNTYP *afn_getoneipnum(char *oneipstr) { -#define OPEP "subafn.c:afn_getipnum" +#define OPEP "subafn.c:afn_getoneipnum," +#define ZIPV4 "0.0.0.0" + +AFNTYP *afnnum; +int phase; +int proceed; + +afnnum=(AFNTYP *)0; +phase=0; +proceed=true; +while (proceed==true) { + (void) fprintf(stderr,"%s JMPDBG DEBUGING phase='%d', oneipstr=<%s>\n", + OPEP,phase,oneipstr); + switch (phase) { + case 0 : /*lets say ip format ok */ + afnnum=(AFNTYP *)calloc(1,sizeof(AFNTYP)); + afnnum->afntype=AF_INET; + afnnum->afnmask=32; + if (strchr(oneipstr,'|')==(char *)0) + phase++; //This is an IPV4 number + break; + case 1 : //IP is an IPV6 format + if (strcasestr(oneipstr,STRIPV6)==oneipstr) + oneipstr+=strlen(STRIPV6); + afnnum->afntype=AF_INET6; + afnnum->afnmask=128; + break; + case 2 : //lets convert it + switch (inet_pton(afnnum->afntype,oneipstr,afnnum->ip)) { + case 1 : //success + break; + case 0 : //Invalid IP number + (void) fprintf(stderr,"Unable to convert IP <%s> (Invalid IP number)\n", + oneipstr); + afnnum=afn_freeipnum(afnnum); + phase=999; //no need to go further + break; + case -1 : //Invalid IP number + (void) fprintf(stderr,"Unable to convert IP <%s> (error=<%s>)\n", + oneipstr,strerror(errno)); + afnnum=afn_freeipnum(afnnum); + phase=999; //no need to go further + break; + default : //Unexpected Result + (void) fprintf(stderr,"Unexpected status to convert IP <%s> " + "(error=<%s> BUG!?)\n", + oneipstr,strerror(errno)); + afnnum=afn_freeipnum(afnnum); + phase=999; //no need to go further + } + break; + case 3 : //lets convert it + afnnum->strnumip=afn_getstrip(afnnum); + //(void) printf("JMPDBG <%s> -> to <%s>\n", onecleanipstr,afnnum->strnumip); + break; + default : /*SAFE guard */ + proceed=false; + break; + } + phase++; + } +return afnnum; + +#undef OPEP +} +/* + +*/ +/********************************************************/ +/* */ +/* Routine to convert clean string list to an */ +/* IP number in AFNTYP list. */ +/* */ +/********************************************************/ +PUBLIC AFNTYP **afn_getipnums(char *seqipstr) + +{ +#define OPEP "subafn.c:afn_getipnums" #define ZIPV4 "0.0.0.0" AFNTYP **afnlist; @@ -367,17 +365,17 @@ proceed=true; while (proceed==true) { switch (phase) { case 0 : //Do we have an IP string - if (cleanipstr==(char *)0) { + if (seqipstr==(char *)0) { (void) fprintf(stderr,"%s No ip provided (bug?)\n",OPEP); phase=999; //No need to go further } break; case 1 : //checking '0' situation - if (strcmp(cleanipstr,"0")==0) - cleanipstr=ZIPV4; + if (strcmp(seqipstr,"0")==0) + seqipstr=ZIPV4; break; case 2 : //checking '0' situation - iplist=strdup(cleanipstr); + iplist=strdup(seqipstr); if (strcmp(iplist,ZIPV4)==0) { (void) free(iplist); iplist=getlocalipstr(); @@ -399,7 +397,7 @@ while (proceed==true) { *next='\000'; next++; } - if ((afnloc=getoneafn(ptr))!=(AFNTYP *)0) { + if ((afnloc=afn_getoneipnum(ptr))!=(AFNTYP *)0) { afnlist=(AFNTYP **)realloc(afnlist,(num+2)*sizeof(AFNTYP *)); afnlist[num]=afnloc; num++; @@ -463,7 +461,7 @@ if (afnnum!=(AFNTYP *)0) { afnnum->ip[14],afnnum->ip[15]); break; default : - (void) fprintf(stderr,"%s, unable to fin IP type (bug?)",OPEP); + (void) fprintf(stderr,"%s, unable to find IP type (bug?)",OPEP); } } if (strlen(buffer)>0) @@ -583,3 +581,54 @@ while (proceed==true) { } return areequal; } +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to check if a remote IP is part of */ +/* acceptable to be relayed list. */ +/* */ +/********************************************************/ +PUBLIC _Bool afn_ip_rlylist(char *iptocheck,AFNTYP **list) + +{ +#define OPEP "subafn.c:afn_ip_inlist," + +_Bool isok; +AFNTYP *afn; +int phase; +_Bool proceed; + +isok=false; +afn=(AFNTYP *)0; +phase=0; +proceed=false; +while (proceed==true) { + switch (phase) { + case 0 : //converting IP + if ((afn=afn_getoneipnum(iptocheck))==(AFNTYP *)0) { + (void) fprintf(stderr,"%s, Unable to convert IP=<%s> (bug?)", + OPEP,iptocheck); + phase=999; + } + break; + case 1 : //do we have a list + if (list==(AFNTYP **)0) + phase=999; //No need to go further + break; + case 2 : //do we have a list + while (*list!=(AFNTYP *)0) { + list++; + } + break; + default : //SAFE Guard + proceed=false; + break; + } + phase++; + } +return isok; + +#undef OPEP +} diff --git a/lib/subafn.h b/lib/subafn.h index bc5a615..f19a494 100644 --- a/lib/subafn.h +++ b/lib/subafn.h @@ -45,8 +45,11 @@ extern char *afn_getstrip(AFNTYP *afnnum); //IP number in AFNTYP structure. extern AFNTYP *afn_getaddrinfo(struct addrinfo *rp); -//procedure to convert a IP as string to an AFNTYP structure -extern AFNTYP **afn_getipnum(char *cleanipstr); +//procedure to convert on IP seen as a string to an AFNTYP structure +extern AFNTYP *afn_getoneipnum(char *oneipstr); + +//procedure to convert multiple IP seen as a string to an AFNTYP list +extern AFNTYP **afn_getipnums(char *listipstr); //procedure to 'compute' a reverss-addres from the IP extern char *afn_reversipnum(AFNTYP *afnnum); @@ -54,4 +57,7 @@ extern char *afn_reversipnum(AFNTYP *afnnum); //procedure to compare 2 same class IP according a mask value extern int afn_cmpipnum(AFNTYP *afnnum1,AFNTYP *afnnum2,int mask); +//procedure to check if a remote ip is part of the relayable list +extern _Bool afn_ip_inrlylist(char *iptocheck,AFNTYP **list); + #endif -- 2.47.3