1 /* srv.c - DNS SRV code
2 * Copyright (C) 2003 Free Software Foundation, Inc.
4 * This file is part of GNUPG.
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 3 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include <sys/types.h>
25 #include <netinet/in.h>
26 #include <arpa/nameser.h>
37 /* Not every installation has gotten around to supporting SRVs
44 priosort(const void *a
,const void *b
)
46 const struct srventry
*sa
=a
,*sb
=b
;
47 if(sa
->priority
>sb
->priority
)
49 else if(sa
->priority
<sb
->priority
)
56 getsrv(const char *name
,struct srventry
**list
)
58 unsigned char answer
[PACKETSZ
];
60 unsigned char *pt
,*emsg
;
65 r
=res_query(name
,C_IN
,T_SRV
,answer
,PACKETSZ
);
66 if(r
<sizeof(HEADER
) || r
>PACKETSZ
)
69 if((((HEADER
*)answer
)->rcode
)==NOERROR
&&
70 (count
=ntohs(((HEADER
*)answer
)->ancount
)))
75 pt
=&answer
[sizeof(HEADER
)];
77 /* Skip over the query */
79 rc
=dn_skipname(pt
,emsg
);
85 while(count
-->0 && pt
<emsg
)
87 struct srventry
*srv
=NULL
;
90 *list
=xrealloc(*list
,(srvcount
+1)*sizeof(struct srventry
));
91 memset(&(*list
)[srvcount
],0,sizeof(struct srventry
));
92 srv
=&(*list
)[srvcount
];
95 rc
=dn_skipname(pt
,emsg
); /* the name we just queried for */
100 /* Truncated message? */
106 /* We asked for SRV and got something else !? */
112 /* We asked for IN and got something else !? */
119 srv
->priority
=*pt
++ << 8;
120 srv
->priority
|=*pt
++;
121 srv
->weight
=*pt
++ << 8;
123 srv
->port
=*pt
++ << 8;
126 /* Get the name. 2782 doesn't allow name compression, but
127 dn_expand still works to pull the name out of the
129 rc
=dn_expand(answer
,emsg
,pt
,srv
->target
,MAXDNAME
);
130 if(rc
==1 && srv
->target
[0]==0) /* "." */
135 /* Corrupt packet? */
140 printf("count=%d\n",srvcount
);
141 printf("priority=%d\n",srv
->priority
);
142 printf("weight=%d\n",srv
->weight
);
143 printf("port=%d\n",srv
->port
);
144 printf("target=%s\n",srv
->target
);
148 /* Now we have an array of all the srv records. */
150 /* Order by priority */
151 qsort(*list
,srvcount
,sizeof(struct srventry
),priosort
);
153 /* For each priority, move the zero-weighted items first. */
154 for(i
=0;i
<srvcount
;i
++)
158 for(j
=i
;j
<srvcount
&& (*list
)[i
].priority
==(*list
)[j
].priority
;j
++)
160 if((*list
)[j
].weight
==0)
165 struct srventry temp
;
167 memcpy(&temp
,&(*list
)[j
],sizeof(struct srventry
));
168 memcpy(&(*list
)[j
],&(*list
)[i
],sizeof(struct srventry
));
169 memcpy(&(*list
)[i
],&temp
,sizeof(struct srventry
));
177 /* Run the RFC-2782 weighting algorithm. We don't need very
178 high quality randomness for this, so regular libc srand/rand
180 srand(time(NULL
)*getpid());
182 for(i
=0;i
<srvcount
;i
++)
185 float prio_count
=0,chose
;
187 for(j
=i
;j
<srvcount
&& (*list
)[i
].priority
==(*list
)[j
].priority
;j
++)
189 prio_count
+=(*list
)[j
].weight
;
190 (*list
)[j
].run_count
=prio_count
;
193 chose
=prio_count
*rand()/RAND_MAX
;
195 for(j
=i
;j
<srvcount
&& (*list
)[i
].priority
==(*list
)[j
].priority
;j
++)
197 if(chose
<=(*list
)[j
].run_count
)
202 struct srventry temp
;
204 memcpy(&temp
,&(*list
)[j
],sizeof(struct srventry
));
205 memcpy(&(*list
)[j
],&(*list
)[i
],sizeof(struct srventry
));
206 memcpy(&(*list
)[i
],&temp
,sizeof(struct srventry
));
229 main(int argc
,char *argv
[])
231 struct srventry
*srv
;
234 rc
=getsrv("_hkp._tcp.wwwkeys.pgp.net",&srv
);
235 printf("Count=%d\n\n",rc
);
238 printf("priority=%hu\n",srv
[i
].priority
);
239 printf("weight=%hu\n",srv
[i
].weight
);
240 printf("port=%hu\n",srv
[i
].port
);
241 printf("target=%s\n",srv
[i
].target
);
253 compile-command: "cc -DTEST -I.. -I../include -Wall -g -o srv srv.c -lresolv libutil.a"