* arrgh resolver sucks
[citadel.git] / citadel / domain.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <netinet/in.h>
4 #include <arpa/nameser.h>
5 #include <resolv.h>
6 #include "sysdep_decls.h"
7 #include "domain.h"
8
9 #define SMART_HOST      "gatekeeper.wdcs.com"
10
11 /* 
12  * getmx()
13  *
14  * Return one or more MX's for a mail destination.
15  *
16  * Upon success, it fills 'mxbuf' with one or more MX hosts, separated by
17  * vertical bar characters, and returns the number of hosts as its return
18  * value.  If no MX's are found, it returns 0.
19  *
20  */
21 int getmx(char *mxbuf, char *dest) {
22         char answer[1024];
23         int ret;
24         unsigned char *startptr, *endptr, *ptr;
25         int expanded_size;
26         char expanded_buf[1024];
27         unsigned short pref, type;
28         int n;
29
30
31         /* If we're configured to send all mail to a smart-host, then our
32          * job here is really easy.
33          */
34         if (1) {        /* FIX */
35                 strcpy(mxbuf, SMART_HOST);
36                 return(1);
37         }
38
39         /* No smart-host?  Look up the best MX for a site.
40          */
41         ret = res_query(
42                 dest,
43                 C_IN, T_MX, answer, sizeof(answer)  );
44
45         lprintf(9, "res_query() returned %d\n", ret);
46
47         if (ret < 0) {
48                 lprintf(5, "No MX found\n");
49                 return(0);
50         }
51
52         /* If we had to truncate, shrink the number to avoid fireworks */
53         if (ret > sizeof(answer))
54                 ret = sizeof(answer);
55         lprintf(9, "size of answer is %d\n", ret);
56
57         startptr = &answer[0];          /* start and end of buffer */
58         endptr = &answer[ret];
59
60         ptr = startptr + HFIXEDSZ;      /* advance past header */
61
62         while(1) {
63                 memset(expanded_buf, 0, sizeof(expanded_buf));
64                 ret = dn_expand(startptr,
65                                 endptr,
66                                 ptr,
67                                 expanded_buf,
68                                 sizeof(expanded_buf)
69                                 );
70                 if (ret < 0) break;
71                 ptr += ret;
72
73                 GETSHORT(type, ptr);
74                 ptr += INT16SZ + INT32SZ;
75                 GETSHORT(n, ptr);
76                 ptr += n;
77
78                 lprintf(9, "ret=%d, type=%d\n", ret, type);
79         }
80
81
82
83
84
85
86
87
88
89
90
91
92
93         /* FIX not done yet */
94         return(0);
95 }