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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 #include <sys/types.h>
27 #include <netinet/in.h>
28 #include <arpa/nameser.h>
39 /* Not every installation has gotten around to supporting SRVs
46 priosort(const void *a
,const void *b
)
48 const struct srventry
*sa
=a
,*sb
=b
;
49 if(sa
->priority
>sb
->priority
)
51 else if(sa
->priority
<sb
->priority
)
58 getsrv(const char *name
,struct srventry
**list
)
60 unsigned char answer
[PACKETSZ
];
62 unsigned char *pt
,*emsg
;
67 r
=res_query(name
,C_IN
,T_SRV
,answer
,PACKETSZ
);
68 if(r
<sizeof(HEADER
) || r
>PACKETSZ
)
71 if((((HEADER
*)answer
)->rcode
)==NOERROR
&&
72 (count
=ntohs(((HEADER
*)answer
)->ancount
)))
77 pt
=&answer
[sizeof(HEADER
)];
79 /* Skip over the query */
81 rc
=dn_skipname(pt
,emsg
);
87 while(count
-->0 && pt
<emsg
)
89 struct srventry
*srv
=NULL
;
92 *list
=xrealloc(*list
,(srvcount
+1)*sizeof(struct srventry
));
93 memset(&(*list
)[srvcount
],0,sizeof(struct srventry
));
94 srv
=&(*list
)[srvcount
];
97 rc
=dn_skipname(pt
,emsg
); /* the name we just queried for */
102 /* Truncated message? */
108 /* We asked for SRV and got something else !? */
114 /* We asked for IN and got something else !? */
121 srv
->priority
=*pt
++ << 8;
122 srv
->priority
|=*pt
++;
123 srv
->weight
=*pt
++ << 8;
125 srv
->port
=*pt
++ << 8;
128 /* Get the name. 2782 doesn't allow name compression, but
129 dn_expand still works to pull the name out of the
131 rc
=dn_expand(answer
,emsg
,pt
,srv
->target
,MAXDNAME
);
132 if(rc
==1 && srv
->target
[0]==0) /* "." */
137 /* Corrupt packet? */
142 printf("count=%d\n",srvcount
);
143 printf("priority=%d\n",srv
->priority
);
144 printf("weight=%d\n",srv
->weight
);
145 printf("port=%d\n",srv
->port
);
146 printf("target=%s\n",srv
->target
);
150 /* Now we have an array of all the srv records. */
152 /* Order by priority */
153 qsort(*list
,srvcount
,sizeof(struct srventry
),priosort
);
155 /* For each priority, move the zero-weighted items first. */
156 for(i
=0;i
<srvcount
;i
++)
160 for(j
=i
;j
<srvcount
&& (*list
)[i
].priority
==(*list
)[j
].priority
;j
++)
162 if((*list
)[j
].weight
==0)
167 struct srventry temp
;
169 memcpy(&temp
,&(*list
)[j
],sizeof(struct srventry
));
170 memcpy(&(*list
)[j
],&(*list
)[i
],sizeof(struct srventry
));
171 memcpy(&(*list
)[i
],&temp
,sizeof(struct srventry
));
179 /* Run the RFC-2782 weighting algorithm. We don't need very
180 high quality randomness for this, so regular libc srand/rand
182 srand(time(NULL
)*getpid());
184 for(i
=0;i
<srvcount
;i
++)
187 float prio_count
=0,chose
;
189 for(j
=i
;j
<srvcount
&& (*list
)[i
].priority
==(*list
)[j
].priority
;j
++)
191 prio_count
+=(*list
)[j
].weight
;
192 (*list
)[j
].run_count
=prio_count
;
195 chose
=prio_count
*rand()/RAND_MAX
;
197 for(j
=i
;j
<srvcount
&& (*list
)[i
].priority
==(*list
)[j
].priority
;j
++)
199 if(chose
<=(*list
)[j
].run_count
)
204 struct srventry temp
;
206 memcpy(&temp
,&(*list
)[j
],sizeof(struct srventry
));
207 memcpy(&(*list
)[j
],&(*list
)[i
],sizeof(struct srventry
));
208 memcpy(&(*list
)[i
],&temp
,sizeof(struct srventry
));
231 main(int argc
,char *argv
[])
233 struct srventry
*srv
;
236 rc
=getsrv("_hkp._tcp.wwwkeys.pgp.net",&srv
);
237 printf("Count=%d\n\n",rc
);
240 printf("priority=%d\n",srv
[i
].priority
);
241 printf("weight=%d\n",srv
[i
].weight
);
242 printf("port=%d\n",srv
[i
].port
);
243 printf("target=%s\n",srv
[i
].target
);
255 compile-command: "cc -DTEST -I.. -I../include -Wall -g -o srv srv.c -lresolv libutil.a"