/* srv.c - DNS SRV code
* Copyright (C) 2003, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
*
* This file is part of GNUPG.
*
* GNUPG is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GNUPG is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see .
*/
#include
#include
#ifdef _WIN32
#include
#else
#include
#include
#include
#include
#endif
#include
#include
#include
#include
#include "srv.h"
/* Not every installation has gotten around to supporting SRVs
yet... */
#ifndef T_SRV
# define T_SRV 33
# ifdef __VMS
# include "cert_vms.h"
# endif /* def __VMS */
#endif
static int
priosort(const void *a,const void *b)
{
const struct srventry *sa=a,*sb=b;
if(sa->priority>sb->priority)
return 1;
else if(sa->prioritypriority)
return -1;
else
return 0;
}
int
getsrv(const char *name,struct srventry **list)
{
unsigned char answer[2048];
int r,srvcount=0;
unsigned char *pt,*emsg;
u16 count,dlen;
HEADER *header=(HEADER *)answer;
*list=NULL;
r=res_query(name,C_IN,T_SRV,answer,2048);
if(r2048)
return -1;
if(header->rcode==NOERROR && (count=ntohs(header->ancount)))
{
int i,rc;
emsg=&answer[r];
pt=&answer[sizeof(HEADER)];
/* Skip over the query */
rc=dn_skipname(pt,emsg);
if(rc==-1)
goto fail;
pt+=rc+QFIXEDSZ;
while(count-->0 && ptpriority=*pt++ << 8;
srv->priority|=*pt++;
srv->weight=*pt++ << 8;
srv->weight|=*pt++;
srv->port=*pt++ << 8;
srv->port|=*pt++;
/* Get the name. 2782 doesn't allow name compression, but
dn_expand still works to pull the name out of the
packet. */
rc=dn_expand(answer,emsg,pt,srv->target,MAXDNAME);
if(rc==1 && srv->target[0]==0) /* "." */
goto noanswer;
if(rc==-1)
goto fail;
pt+=rc;
/* Corrupt packet? */
if(dlen!=rc+6)
goto fail;
#if 0
printf("count=%d\n",srvcount);
printf("priority=%d\n",srv->priority);
printf("weight=%d\n",srv->weight);
printf("port=%d\n",srv->port);
printf("target=%s\n",srv->target);
#endif
}
/* Now we have an array of all the srv records. */
/* Order by priority */
qsort(*list,srvcount,sizeof(struct srventry),priosort);
/* For each priority, move the zero-weighted items first. */
for(i=0;i