]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Working on checkmx
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Fri, 16 Aug 2024 11:52:47 +0000 (07:52 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Fri, 16 Aug 2024 11:52:47 +0000 (07:52 -0400)
lib/subrou.c
lib/unidns.c
lib/unidns.h

index 6286fee336416aaa7e983c3181581fcd65409dde..2682e3a16bcd5037561b17327a6b8054b5cce19b 100644 (file)
@@ -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
index 394abab8c556549c1fc392f8f8092105ffa07c25..3e992550f2a93ffcc0e9e3c03884d3cad8a4acb4 100644 (file)
@@ -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;
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
 /*     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
 }
 /*
index 4c8342a38edcb6541a987c33414e7f39c608d625..c83fcdc1725934c5449142b22a924fe7ff62a0af 100644 (file)
@@ -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);