From 1194eac60016bd35054b94135d5309376a84d64c Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Fri, 16 Aug 2024 07:52:47 -0400 Subject: [PATCH] Working on checkmx --- lib/subrou.c | 2 +- lib/unidns.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/unidns.h | 10 ++++++ 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/lib/subrou.c b/lib/subrou.c index 6286fee..2682e3a 100644 --- a/lib/subrou.c +++ b/lib/subrou.c @@ -21,7 +21,7 @@ //version definition #define VERSION "0.4.2" -#define RELEASE "14" +#define RELEASE "15" //Public variables PUBLIC int debug=0; //debug level diff --git a/lib/unidns.c b/lib/unidns.c index 394abab..3e99255 100644 --- a/lib/unidns.c +++ b/lib/unidns.c @@ -66,6 +66,25 @@ if (debug>-dbg) { */ /********************************************************/ /* */ +/* Procedure to compare 2 MX and return */ +/* a value according preference value */ +/* */ +/********************************************************/ +static int cmpmx(const void *p1,const void *p2) + +{ +MXTYP *m1; +MXTYP *m2; + +m1=((MXTYP **)p1)[0]; +m2=((MXTYP **)p2)[0]; +return m1->preference-m2->preference; +} +/* + +*/ +/********************************************************/ +/* */ /* Procedure to parse mechanisme string */ /* */ /********************************************************/ @@ -690,6 +709,84 @@ switch (spf) { } return ascii; +#undef OPEP +} +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to extract a domain MX. */ +/* If the domain doesn't exist or domain */ +/* do not have a MX, a void pointer is */ +/* returned. */ +/* Retuned list is ordered according */ +/* MX preference (low preference first) */ +/* */ +/********************************************************/ +PUBLIC MXTYP **dns_getmx(char *domain) + +{ +#define OPEP "unidns.c:dns_getmx" + +MXTYP **mxlist; +char **list; +int answer; +RSPTYP rsp; +int phase; +_Bool proceed; + +mxlist=(MXTYP **)0; +list=(char **)0; +answer=0; +(void) memset(&rsp,'\000',sizeof(rsp)); +phase=0; +proceed=true; +while (proceed==true) { + switch (phase) { + case 0 : //Are the parameters available + if ((domain==(char *)0)||(strlen(domain)==0)) { + (void) rou_alert(0,"%s missing domainame",OPEP); + phase=999; //trouble trouble + } + break; + case 1 : //Requesting MX for domain + answer=myquery(domain,C_IN,T_MX,rsp.buf,sizeof(rsp.buf)); + if (answer<=0) + phase=999; //Trouble trouble to get MX + break; + case 2 : //extracting result + if (answer>=sizeof(rsp.buf)) + answer=sizeof(rsp.buf)-1; + list=extracting(&rsp,answer,domain,"MX"); + if (list==(char **)0) + phase=999; //list empty? + break; + case 3 : //ordering list + answer=0; + for (int i=0;list[i]!=(char *)0;i+=2) { + if ((list[i+1]!=(char *)0)&&(strlen(list[i+1])>0)) { + MXTYP *mx; + + mx=(MXTYP *)calloc(1,sizeof(MXTYP)); + mx->preference=atoi(list[i]); + mx->mxname=strdup(list[i+1]); + mxlist=(MXTYP **)rou_addlist((void **)mxlist,(void *)mx); + answer++; + } + } + if (answer>0) + (void) qsort((void *)mxlist,answer,sizeof(MXTYP *),cmpmx); + list=(char **)rou_freelist((void **)list,(freehandler_t)rou_freestr); + break; + default : //SAFE guard + proceed=false; + break; + } + phase++; + } +return mxlist; + #undef OPEP } /* diff --git a/lib/unidns.h b/lib/unidns.h index 4c8342a..c83fcdc 100644 --- a/lib/unidns.h +++ b/lib/unidns.h @@ -21,9 +21,19 @@ typedef enum { spf_unknown //Unknown SPF directive }SPFENU; +//defining an MX structure +typedef struct { + int preference; /*MX preference */ + char *mxname; /*MX hostname */ + }MXTYP; + //procedure to return spf status as an ASCII string extern const char *dns_spfASCII(SPFENU spf); +//procedure to get a list of MX IP releated to a specific +//domain. +extern MXTYP **dns_getmx(char *domain); + //get the SPF status for a specific domain and a remote peer IP extern SPFENU dns_get_spf_status(int *try,char *domain,AFNTYP *afnnum); -- 2.47.3