Back to index

courier  0.68.2
testmxlookup.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 2011 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #include      "config.h"
00007 #include      "courier.h"
00008 #include      "rfc1035/config.h"
00009 #include      "rfc1035/rfc1035.h"
00010 #include      "rfc1035/rfc1035mxlist.h"
00011 #include      <sys/types.h>
00012 #include      <stdio.h>
00013 #include      <stdlib.h>
00014 #include      <string.h>
00015 #include      <time.h>
00016 #include      <arpa/inet.h>
00017 
00018 
00019 static void setns(const char *p, struct rfc1035_res *res)
00020 {
00021 RFC1035_ADDR ia[4];
00022 int    i=0;
00023 char   *q=malloc(strlen(p)+1), *r;
00024 
00025        strcpy(q, p);
00026        for (r=q; (r=strtok(r, ", ")) != 0; r=0)
00027               if (i < 4)
00028               {
00029                      if (rfc1035_aton(r, &ia[i]) == 0)
00030                      {
00031                             ++i;
00032                      }
00033                      else
00034                      {
00035                             fprintf(stderr, "%s: invalid IP address\n",
00036                                    r);
00037                      }
00038               }
00039        rfc1035_init_ns(res, ia, i);
00040 }
00041 
00042 int main(int argc, char **argv)
00043 {
00044        int    argn;
00045        const char *q_name;
00046        struct rfc1035_mxlist *mxlist, *p;
00047        struct rfc1035_res res;
00048        int rc;
00049 
00050        argn=1;
00051        srand(time(NULL));
00052 
00053        rfc1035_init_resolv(&res);
00054        while (argn < argc)
00055        {
00056               if (argv[argn][0] == '@')
00057               {
00058                      setns(argv[argn]+1, &res);
00059                      ++argn;
00060                      continue;
00061               }
00062 
00063               if (strcmp(argv[argn], "--dnssec") == 0)
00064               {
00065                      rfc1035_init_dnssec_enable(&res, 1);
00066                      ++argn;
00067                      continue;
00068               }
00069 
00070               if (strcmp(argv[argn], "--udpsize") == 0)
00071               {
00072                      ++argn;
00073 
00074                      if (argn < argc)
00075                      {
00076                             rfc1035_init_edns_payload(&res,
00077                                                    atoi(argv[argn]));
00078                             ++argn;
00079                      }
00080                      continue;
00081               }
00082 
00083               break;
00084        }
00085 
00086        if (argn >= argc)    exit(0);
00087 
00088        q_name=argv[argn++];
00089 
00090        rc=rfc1035_mxlist_create_x(&res, q_name,
00091                                RFC1035_MX_AFALLBACK |
00092                                RFC1035_MX_IGNORESOFTERR,
00093                                &mxlist);
00094        rfc1035_destroy_resolv(&res);
00095 
00096        switch (rc)   {
00097        case   RFC1035_MX_OK:
00098               break;
00099        case   RFC1035_MX_SOFTERR:
00100               printf("%s: soft error.\n", q_name);
00101               exit(0);
00102        case   RFC1035_MX_HARDERR:
00103               printf("%s: hard error.\n", q_name);
00104               exit(0);
00105        case   RFC1035_MX_INTERNAL:
00106               printf("%s: internal error.\n", q_name);
00107               exit(0);
00108        case   RFC1035_MX_BADDNS:
00109               printf("%s: bad DNS records (recursive CNAME).\n", q_name);
00110               exit(0);
00111        }
00112 
00113        printf("Domain %s:\n", q_name);
00114        for (p=mxlist; p; p=p->next)
00115        {
00116        RFC1035_ADDR  addr;
00117        char   buf[RFC1035_NTOABUFSIZE];
00118 
00119               if (rfc1035_sockaddrip(&p->address, sizeof(p->address),
00120                      &addr)<0)
00121                      continue;
00122               rfc1035_ntoa(&addr, buf);
00123 
00124               printf("Relay: %s, Priority: %d, Address: %s%s%s%s\n",
00125                      p->hostname,
00126                      p->priority, buf,
00127                      config_islocal(p->hostname, NULL) ? " [ LOCAL ]":"",
00128                      strcmp(p->hostname, buf) ? "":" [ ERROR ]",
00129                      p->ad ? " (DNSSEC)":"");
00130        }
00131 
00132        rfc1035_mxlist_free(mxlist);
00133        return (0);
00134 }