- ipv6 received= debugging
[sip-router] / resolve.h
1 /*
2  * $Id$
3  *
4  * resolver related functions
5  */
6
7
8 #ifndef resolve_h
9 #define resolve_h
10
11 #include <netdb.h>
12
13 #include "ip_addr.h"
14
15
16 /* gethostbyname wrappers
17  * use this, someday htey will use a local cache */
18
19
20
21 static inline struct hostent* resolvehost(const char* name)
22 {
23         struct hostent* he;
24         
25 #ifdef DNS_IP_HACK
26 #endif
27
28         he=gethostbyname(name); /*ipv4*/
29
30 #ifdef USE_IPV6
31         if(he==0){
32                 /*try ipv6*/
33                 he=gethostbyname2(name, AF_INET6);
34         }
35 #endif
36         return he;
37 }
38
39
40
41
42 #define HEX2I(c) \
43         (       (((c)>='0') && ((c)<='9'))? (c)-'0' :  \
44                 (((c)>='A') && ((c)<='F'))? ((c)-'A')+10 : \
45                 (((c)>='a') && ((c)<='f'))? ((c)-'a')+10 : -1 )
46
47
48 #define rev_resolvehost(ip) gethostbyaddr((ip)->u.addr, (ip)->len, (ip)->af);
49
50
51
52 #if 0
53 /* returns an ip_addr struct.; on error retunrs 0 and sets err (if !=0)
54  * the ip_addr struct is static, so subsequent calls will destroy its 
55  * content */
56 static /*inline*/ struct ip_addr* str2ip6(unsigned char* str, unsigned int len,
57                 int* err)
58 {
59         int i, idx1, rest;
60         int no_colons;
61         int double_colon;
62         int hex;
63         static struct ip_addr ip;
64         unsigned short* addr_start;
65         unsigned short addr_end[8];
66         unsigned short* addr;
67         unsigned char* limit;
68         unsigned char* init;
69         
70         /* init */
71         init=str;
72         i=idx1=rest=0;
73         double_colon=0;
74         no_colons=0;
75         ip.af=AF_INET6;
76         ip.len=16;
77         addr_start=ip.u.addr16;
78         addr=addr_start;
79         limit=str+len;
80         memset(addr_start, 0 , 8*sizeof(unsigned short));
81         memset(addr_end, 0 , 8*sizeof(unsigned short));
82         
83         for (; str<limit; str++){
84                 if (*str==':'){
85                         no_colons++;
86                         if (no_colons>7) goto error_too_many_colons;
87                         if (double_colon){
88                                 idx1=i;
89                                 i=0;
90                                 if (addr==addr_end) goto error_colons;
91                                 addr=addr_end;
92                         }else{
93                                 double_colon=1;
94                                 i++;
95                         }
96                 }else if ((hex=HEX2I(*str))>=0){
97                                 addr[i]=addr[i]*16+hex;
98                                 double_colon=0;
99                 }else{
100                         /* error, unknown char */
101                         goto error_char;
102                 }
103         }
104         
105         rest=8-i-idx1;
106         memcpy(addr_start+idx1+rest, addr_end, i*sizeof(unsigned short));
107         if (err) *err=0;
108         return &ip;
109
110 error_too_many_colons:
111         DBG("str2ip6: ERROR: too many colons in [%.*s]\n", (int) len, init);
112         if (err) *err=1;
113         return 0;
114
115 error_colons:
116         DBG("str2ip6: ERROR: too many double colons in [%.*s]\n", (int) len, init);
117         if (err) *err=1;
118         return 0;
119
120 error_char:
121         DBG("str2ip6: WARNING: unexpected char %c in  [%.*s]\n", *str, (int) len,
122                         init);
123         if (err) *err=1;
124         return 0;
125 }
126 #endif
127
128
129
130 #endif