* dotlock.c, http.c, iobuf.c, simple-gettext.c, srv.c, srv.h, strgutil.c,
[gnupg.git] / util / srv.c
1 /* srv.c - DNS SRV code
2  * Copyright (C) 2003 Free Software Foundation, Inc.
3  *
4  * This file is part of GNUPG.
5  *
6  * GNUPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * GNUPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19  */
20
21 #include <config.h>
22 #ifdef USE_DNS_SRV
23 #ifdef _WIN32
24 #include <windows.h>
25 #else
26 #include <netinet/in.h>
27 #include <arpa/nameser.h>
28 #include <resolv.h>
29 #endif
30 #include <sys/types.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <time.h>
35 #include "memory.h"
36 #include "types.h"
37 #include "srv.h"
38
39 /* Not every installation has gotten around to supporting SRVs
40    yet... */
41 #ifndef T_SRV
42 #define T_SRV 33
43 #endif
44
45 static int priosort(const void *a,const void *b)
46 {
47   const struct srventry *sa=a,*sb=b;
48   if(sa->priority>sb->priority)
49     return 1;
50   else if(sa->priority<sb->priority)
51     return -1;
52   else
53     return 0;
54 }
55
56 int
57 getsrv(const char *name,struct srventry **list)
58 {
59   unsigned char answer[PACKETSZ];
60   int r,srvcount=0;
61   unsigned char *pt,*emsg;
62   u16 count,dlen;
63
64   *list=NULL;
65
66   r=res_query(name,C_IN,T_SRV,answer,PACKETSZ);
67   if(r<sizeof(HEADER))
68     return -1;
69
70   if((((HEADER *)answer)->rcode)==NOERROR &&
71      (count=ntohs(((HEADER *)answer)->ancount)))
72     {
73       int i,rc;
74
75       emsg=&answer[r];
76       pt=&answer[sizeof(HEADER)];
77
78       /* Skip over the query */
79
80       rc=dn_skipname(pt,emsg);
81       if(rc==-1)
82         goto fail;
83
84       pt+=rc+QFIXEDSZ;
85
86       while(count-->0 && pt<emsg)
87         {
88           struct srventry *srv=NULL;
89           u16 type,class;
90
91           *list=m_realloc(*list,(srvcount+1)*sizeof(struct srventry));
92           memset(&(*list)[srvcount],0,sizeof(struct srventry));
93           srv=&(*list)[srvcount];
94           srvcount++;
95
96           rc=dn_skipname(pt,emsg); /* the name we just queried for */
97           if(rc==-1)
98             goto fail;
99           pt+=rc;
100
101           /* Truncated message? */
102           if((emsg-pt)<16)
103             goto fail;
104
105           type=*pt++ << 8;
106           type|=*pt++;
107           /* We asked for SRV and got something else !? */
108           if(type!=T_SRV)
109             goto fail;
110
111           class=*pt++ << 8;
112           class|=*pt++;
113           /* We asked for IN and got something else !? */
114           if(class!=C_IN)
115             goto fail;
116
117           pt+=4; /* ttl */
118           dlen=*pt++ << 8;
119           dlen|=*pt++;
120           srv->priority=*pt++ << 8;
121           srv->priority|=*pt++;
122           srv->weight=*pt++ << 8;
123           srv->weight|=*pt++;
124           srv->port=*pt++ << 8;
125           srv->port|=*pt++;
126
127           /* Get the name.  2782 doesn't allow name compression, but
128              dn_expand still works to pull the name out of the
129              packet. */
130           rc=dn_expand(answer,emsg,pt,srv->target,MAXDNAME);
131           if(rc==1 && srv->target[0]==0) /* "." */
132             goto noanswer;
133           if(rc==-1)
134             goto fail;
135           pt+=rc;
136           /* Corrupt packet? */
137           if(dlen!=rc+6)
138             goto fail;
139
140 #if 0
141           printf("count=%d\n",srvcount);
142           printf("priority=%d\n",srv->priority);
143           printf("weight=%d\n",srv->weight);
144           printf("port=%d\n",srv->port);
145           printf("target=%s\n",srv->target);
146 #endif
147         }
148
149       /* Now we have an array of all the srv records. */
150
151       /* Order by priority */
152       qsort(*list,srvcount,sizeof(struct srventry),priosort);
153
154       /* For each priority, move the zero-weighted items first. */
155       for(i=0;i<srvcount;i++)
156         {
157           int j;
158
159           for(j=i;j<srvcount && (*list)[i].priority==(*list)[j].priority;j++)
160             {
161               if((*list)[j].weight==0)
162                 {
163                   /* Swap j with i */
164                   if(j!=i)
165                     {
166                       struct srventry temp;
167
168                       memcpy(&temp,&(*list)[j],sizeof(struct srventry));
169                       memcpy(&(*list)[j],&(*list)[i],sizeof(struct srventry));
170                       memcpy(&(*list)[i],&temp,sizeof(struct srventry));
171                     }
172
173                   break;
174                 }
175             }
176         }
177
178       /* Run the RFC-2782 weighting algorithm.  We don't need very
179          high quality randomness for this, so regular libc srand/rand
180          is sufficient. */
181       srand(time(NULL)*getpid());
182
183       for(i=0;i<srvcount;i++)
184         {
185           int j;
186           float prio_count=0,chose;
187
188           for(j=i;j<srvcount && (*list)[i].priority==(*list)[j].priority;j++)
189             {
190               prio_count+=(*list)[j].weight;
191               (*list)[j].run_count=prio_count;
192             }
193
194           chose=prio_count*rand()/RAND_MAX;
195
196           for(j=i;j<srvcount && (*list)[i].priority==(*list)[j].priority;j++)
197             {
198               if(chose<=(*list)[j].run_count)
199                 {
200                   /* Swap j with i */
201                   if(j!=i)
202                     {
203                       struct srventry temp;
204
205                       memcpy(&temp,&(*list)[j],sizeof(struct srventry));
206                       memcpy(&(*list)[j],&(*list)[i],sizeof(struct srventry));
207                       memcpy(&(*list)[i],&temp,sizeof(struct srventry));
208                     }
209                   break;
210                 }
211             }
212         }
213     }
214   
215   return srvcount;
216
217  noanswer:
218   m_free(*list);
219   *list=NULL;
220   return 0;
221
222  fail:
223   m_free(*list);
224   *list=NULL;
225   return -1;
226 }
227
228 #endif /* USE_DNS_SRV */
229
230 #ifdef TEST
231 int
232 main(int argc,char *argv[])
233 {
234   struct srventry *srv;
235   int rc,i;
236
237   rc=getsrv("_hkp._tcp.wwwkeys.pgp.net",&srv);
238   printf("Count=%d\n\n",rc);
239   for(i=0;i<rc;i++)
240     {
241       printf("priority=%d\n",srv[i].priority);
242       printf("weight=%d\n",srv[i].weight);
243       printf("port=%d\n",srv[i].port);
244       printf("target=%s\n",srv[i].target);
245       printf("\n");
246     }
247
248   m_free(srv);
249
250   return 0;
251 }
252 #endif /* TEST */