1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
/*
* $Id: resolve.h,v 1.4 2006/02/14 21:43:44 bogdan_iancu Exp $
*
* resolver related functions
*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of openser, a free SIP server.
*
* openser 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 2 of the License, or
* (at your option) any later version
*
* openser 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History:
* --------
* 2003-04-12 support for resolving ipv6 address references added (andrei)
* 2004-07-28 darwin needs nameser_compat.h (andrei)
*/
#ifndef __resolve_h
#define __resolve_h
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/nameser.h>
#ifdef __OS_darwin
#include <arpa/nameser_compat.h>
#endif
#include "ip_addr.h"
#define MAX_QUERY_SIZE 8192
#define ANS_SIZE 8192
#define DNS_HDR_SIZE 12
#define MAX_DNS_NAME 256
#define MAX_DNS_STRING 255
/* query union*/
union dns_query{
HEADER hdr;
unsigned char buff[MAX_QUERY_SIZE];
};
/* rdata struct*/
struct rdata {
unsigned short type;
unsigned short class;
unsigned int ttl;
void* rdata;
struct rdata* next;
};
/* srv rec. struct*/
struct srv_rdata {
unsigned short priority;
unsigned short weight;
unsigned short port;
unsigned int name_len;
char name[MAX_DNS_NAME];
};
/* naptr rec. struct*/
struct naptr_rdata {
unsigned short order;
unsigned short pref;
unsigned int flags_len;
char flags[MAX_DNS_STRING];
unsigned int services_len;
char services[MAX_DNS_STRING];
unsigned int regexp_len;
char regexp[MAX_DNS_STRING];
unsigned int repl_len; /* not currently used */
char repl[MAX_DNS_NAME];
};
/* A rec. struct */
struct a_rdata {
unsigned char ip[4];
};
struct aaaa_rdata {
unsigned char ip6[16];
};
/* cname rec. struct*/
struct cname_rdata {
char name[MAX_DNS_NAME];
};
struct rdata* get_record(char* name, int type);
void free_rdata_list(struct rdata* head);
extern int dns_try_ipv6;
#define rev_resolvehost(ip)\
gethostbyaddr((char*)(ip)->u.addr, (ip)->len, (ip)->af);
#define HEX2I(c) \
( (((c)>='0') && ((c)<='9'))? (c)-'0' : \
(((c)>='A') && ((c)<='F'))? ((c)-'A')+10 : \
(((c)>='a') && ((c)<='f'))? ((c)-'a')+10 : -1 )
#define get_naptr(_rdata) \
( ((struct naptr_rdata*)(_rdata)->rdata) )
/* converts a str to an ipv4 address, returns the address or 0 on error
Warning: the result is a pointer to a statically allocated structure */
static inline struct ip_addr* str2ip(str* st)
{
int i;
unsigned char *limit;
static struct ip_addr ip;
unsigned char* s;
s=(unsigned char*)st->s;
/*init*/
ip.u.addr32[0]=0;
i=0;
limit=(unsigned char*)(st->s + st->len);
for(;s<limit ;s++){
if (*s=='.'){
i++;
if (i>3) goto error_dots;
}else if ( (*s <= '9' ) && (*s >= '0') ){
ip.u.addr[i]=ip.u.addr[i]*10+*s-'0';
}else{
//error unknown char
goto error_char;
}
}
if (i<3) goto error_dots;
ip.af=AF_INET;
ip.len=4;
return &ip;
error_dots:
DBG("str2ip: ERROR: too %s dots in [%.*s]\n", (i>3)?"many":"few",
st->len, st->s);
return 0;
error_char:
/*
DBG("str2ip: WARNING: unexpected char %c in [%.*s]\n", *s, st->len, st->s);
*/
return 0;
}
/* returns an ip_addr struct.; on error returns 0
* the ip_addr struct is static, so subsequent calls will destroy its content*/
static inline struct ip_addr* str2ip6(str* st)
{
int i, idx1, rest;
int no_colons;
int double_colon;
int hex;
static struct ip_addr ip;
unsigned short* addr_start;
unsigned short addr_end[8];
unsigned short* addr;
unsigned char* limit;
unsigned char* s;
/* init */
if ((st->len) && (st->s[0]=='[')){
/* skip over [ ] */
if (st->s[st->len-1]!=']') goto error_char;
s=(unsigned char*)(st->s+1);
limit=(unsigned char*)(st->s+st->len-1);
}else{
s=(unsigned char*)st->s;
limit=(unsigned char*)(st->s+st->len);
}
i=idx1=rest=0;
double_colon=0;
no_colons=0;
ip.af=AF_INET6;
ip.len=16;
addr_start=ip.u.addr16;
addr=addr_start;
memset(addr_start, 0 , 8*sizeof(unsigned short));
memset(addr_end, 0 , 8*sizeof(unsigned short));
for (; s<limit; s++){
if (*s==':'){
no_colons++;
if (no_colons>7) goto error_too_many_colons;
if (double_colon){
idx1=i;
i=0;
if (addr==addr_end) goto error_colons;
addr=addr_end;
}else{
double_colon=1;
addr[i]=htons(addr[i]);
i++;
}
}else if ((hex=HEX2I(*s))>=0){
addr[i]=addr[i]*16+hex;
double_colon=0;
}else{
/* error, unknown char */
goto error_char;
}
}
if (!double_colon){ /* not ending in ':' */
addr[i]=htons(addr[i]);
i++;
}
/* if address contained '::' fix it */
if (addr==addr_end){
rest=8-i-idx1;
memcpy(addr_start+idx1+rest, addr_end, i*sizeof(unsigned short));
}else{
/* no double colons inside */
if (no_colons<7) goto error_too_few_colons;
}
/*
DBG("str2ip6: idx1=%d, rest=%d, no_colons=%d, hex=%x\n",
idx1, rest, no_colons, hex);
DBG("str2ip6: address %x:%x:%x:%x:%x:%x:%x:%x\n",
addr_start[0], addr_start[1], addr_start[2],
addr_start[3], addr_start[4], addr_start[5],
addr_start[6], addr_start[7] );
*/
return &ip;
error_too_many_colons:
DBG("str2ip6: ERROR: too many colons in [%.*s]\n", st->len, st->s);
return 0;
error_too_few_colons:
DBG("str2ip6: ERROR: too few colons in [%.*s]\n", st->len, st->s);
return 0;
error_colons:
DBG("str2ip6: ERROR: too many double colons in [%.*s]\n", st->len, st->s);
return 0;
error_char:
/*
DBG("str2ip6: WARNING: unexpected char %c in [%.*s]\n", *s, st->len,
st->s);*/
return 0;
}
struct hostent* sip_resolvehost(str* name, unsigned short* port,
int *proto, int is_sips);
/* gethostbyname wrappers
* use this, someday they will use a local cache */
static inline struct hostent* resolvehost(char* name, int no_ip_test)
{
static struct hostent* he=0;
#ifdef HAVE_GETIPNODEBYNAME
int err;
static struct hostent* he2=0;
#endif
struct ip_addr* ip;
str s;
if (!no_ip_test) {
s.s = (char*)name;
s.len = strlen(name);
/* check if it's an ip address */
if ( ((ip=str2ip(&s))!=0)
#ifdef USE_IPV6
|| ((ip=str2ip6(&s))!=0)
#endif
){
/* we are lucky, this is an ip address */
return ip_addr2he(&s, ip);
}
}
/* ipv4 */
he=gethostbyname(name);
#ifdef USE_IPV6
if(he==0 && dns_try_ipv6){
/*try ipv6*/
#ifdef HAVE_GETHOSTBYNAME2
he=gethostbyname2(name, AF_INET6);
#elif defined HAVE_GETIPNODEBYNAME
/* on solaris 8 getipnodebyname has a memory leak,
* after some time calls to it will fail with err=3
* solution: patch your solaris 8 installation */
if (he2) freehostent(he2);
he=he2=getipnodebyname(name, AF_INET6, 0, &err);
#else
#error neither gethostbyname2 or getipnodebyname present
#endif
}
#endif
return he;
}
int resolv_init();
#endif
|