| 1 |
/* ans.c - Interface for text2atm and atm2text to ANS */ |
| 2 |
|
| 3 |
/* Written 1996-2000 by Werner Almesberger, EPFL-LRC/ICA */ |
| 4 |
|
| 5 |
|
| 6 |
/* |
| 7 |
* This stuff is a temporary hack to avoid using gethostbyname_nsap and such |
| 8 |
* without doing the "full upgrade" to getaddrinfo/getnameinfo. This also |
| 9 |
* serves as an exercise for me to get all the details right before I propose |
| 10 |
* a patch that would eventually end up in libc (and that should therefore be |
| 11 |
* as stable as possible). |
| 12 |
*/ |
| 13 |
|
| 14 |
#if HAVE_CONFIG_H |
| 15 |
#include <config.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
#include <stdlib.h> |
| 19 |
#include <stdio.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include <netinet/in.h> |
| 23 |
#include <arpa/nameser.h> |
| 24 |
#include <netdb.h> |
| 25 |
#include <resolv.h> |
| 26 |
|
| 27 |
#include "atm.h" |
| 28 |
#include "atmres.h" |
| 29 |
|
| 30 |
|
| 31 |
#define MAX_ANSWER 2048 |
| 32 |
#define MAX_NAME 1024 |
| 33 |
|
| 34 |
#define GET16(pos) (((pos)[0] << 8) | (pos)[1]) |
| 35 |
|
| 36 |
|
| 37 |
static int ans(const char *text,int wanted,void *result,int res_len) |
| 38 |
{ |
| 39 |
unsigned char answer[MAX_ANSWER]; |
| 40 |
unsigned char name[MAX_NAME]; |
| 41 |
unsigned char *pos,*data,*found; |
| 42 |
int answer_len,name_len,data_len,found_len; |
| 43 |
int questions,answers; |
| 44 |
|
| 45 |
found_len = 0; /* gcc wants it */ |
| 46 |
if ((answer_len = res_search(text,C_IN,wanted,answer,MAX_ANSWER)) < 0) |
| 47 |
return TRY_OTHER; |
| 48 |
/* |
| 49 |
* Response header: id, flags, #queries, #answers, #authority, |
| 50 |
* #additional (all 16 bits) |
| 51 |
*/ |
| 52 |
pos = answer+12; |
| 53 |
if (answer[3] & 15) return TRY_OTHER; /* rcode != 0 */ |
| 54 |
questions = GET16(answer+4); |
| 55 |
if (questions != 1) return TRY_OTHER; /* trouble ... */ |
| 56 |
answers = GET16(answer+6); |
| 57 |
if (answers < 1) return TRY_OTHER; |
| 58 |
/* |
| 59 |
* Query: name, type (16), class (16) |
| 60 |
*/ |
| 61 |
if ((name_len = dn_expand(answer,answer+answer_len,pos,name,MAX_NAME)) < 0) |
| 62 |
return TRY_OTHER; |
| 63 |
pos += name_len; |
| 64 |
if (GET16(pos) != wanted || GET16(pos+2) != C_IN) return TRY_OTHER; |
| 65 |
pos += 4; |
| 66 |
/* |
| 67 |
* Iterate over answers until we find something we like, giving priority |
| 68 |
* to ATMA_AESA (until signaling is fixed to work with E.164 too) |
| 69 |
*/ |
| 70 |
found = NULL; |
| 71 |
while (answers--) { |
| 72 |
/* |
| 73 |
* RR: name, type (16), class (16), TTL (32), resource_len (16), |
| 74 |
* resource_data ... |
| 75 |
*/ |
| 76 |
if ((name_len = dn_expand(answer,answer+answer_len,pos,name,MAX_NAME)) |
| 77 |
< 0) return TRY_OTHER; |
| 78 |
pos += name_len; |
| 79 |
data_len = GET16(pos+8); |
| 80 |
data = pos+10; |
| 81 |
pos = data+data_len; |
| 82 |
if (GET16(data-10) != wanted || GET16(data-8) != C_IN || !--data_len) |
| 83 |
continue; |
| 84 |
switch (wanted) { |
| 85 |
case T_NSAP: |
| 86 |
data_len++; |
| 87 |
if (data_len != ATM_ESA_LEN) continue; |
| 88 |
memcpy(((struct sockaddr_atmsvc *) result)-> |
| 89 |
sas_addr.prv,data,ATM_ESA_LEN); |
| 90 |
return 0; |
| 91 |
case T_ATMA: |
| 92 |
switch (*data++) { |
| 93 |
case ATMA_AESA: |
| 94 |
if (data_len != ATM_ESA_LEN) continue; |
| 95 |
memcpy(((struct sockaddr_atmsvc *) result)-> |
| 96 |
sas_addr.prv,data,ATM_ESA_LEN); |
| 97 |
return 0; |
| 98 |
case ATMA_E164: |
| 99 |
if (data_len > ATM_E164_LEN) continue; |
| 100 |
if (!found) { |
| 101 |
found = data; |
| 102 |
found_len = data_len; |
| 103 |
} |
| 104 |
break; |
| 105 |
default: |
| 106 |
continue; |
| 107 |
} |
| 108 |
case T_PTR: |
| 109 |
if (dn_expand(answer,answer+answer_len,data,result, |
| 110 |
res_len) < 0) return FATAL; |
| 111 |
return 0; |
| 112 |
default: |
| 113 |
continue; |
| 114 |
} |
| 115 |
} |
| 116 |
if (!found) return TRY_OTHER; |
| 117 |
memcpy(((struct sockaddr_atmsvc *) result)->sas_addr.pub,found, |
| 118 |
found_len); |
| 119 |
((struct sockaddr_atmsvc *) result)->sas_addr.pub[found_len] = 0; |
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
int ans_byname(const char *text,struct sockaddr_atmsvc *addr,int length, |
| 125 |
int flags) |
| 126 |
{ |
| 127 |
if (!(flags & T2A_SVC) || length != sizeof(*addr)) return TRY_OTHER; |
| 128 |
memset(addr,0,sizeof(*addr)); |
| 129 |
addr->sas_family = AF_ATMSVC; |
| 130 |
if (!ans(text,T_ATMA,addr,length)) return 0; |
| 131 |
return ans(text,T_NSAP,addr,length); |
| 132 |
} |