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
|
/* text2atm.c - Converts textual representation of ATM address to binary
encoding */
/* Written 1995-2000 by Werner Almesberger, EPFL-LRC/ICA */
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>
#include "atm.h"
#include "atmsap.h"
#include "atmres.h"
static int try_pvc(const char *text,struct sockaddr_atmpvc *addr,int flags)
{
int part[3];
int i;
part[0] = part[1] = part[2] = 0;
i = 0;
while (1) {
if (!*text) return FATAL; /* empty or ends with a dot */
if (i == 3) return TRY_OTHER; /* too long */
if (isdigit(*text)) {
if (*text == '0' && isdigit(text[1])) return TRY_OTHER;
/* no leading zeroes */
do {
if (part[i] > INT_MAX/10) return TRY_OTHER;/* number too big */
part[i] = part[i]*10+*text++-'0';
}
while (isdigit(*text));
i++;
if (!*text) break;
if (*text++ != '.') return TRY_OTHER; /* non-PVC character */
continue;
}
if (*text == '*') {
if (!(flags & T2A_WILDCARD)) return FATAL; /* not allowed */
part[i++] = ATM_ITF_ANY; /* all *_ANY have the same value */
}
else {
if (*text != '?') return TRY_OTHER; /* invalid character */
if (!(flags & T2A_UNSPEC)) return FATAL; /* not allowed */
part[i++] = ATM_VPI_UNSPEC; /* all *_UNSPEC have the same
value */
}
if (!*++text) break;
if (*text++ != '.') return FATAL; /* dot required */
}
if (i < 2) return TRY_OTHER; /* no dots */
if (i == 2) {
part[2] = part[1];
part[1] = part[0];
part[0] = 0; /* default interface */
}
if (part[0] > SHRT_MAX || part[2] > ATM_MAX_VCI)
return TRY_OTHER; /* too big */
if (part[1] > (flags & T2A_NNI ? ATM_MAX_VPI_NNI : ATM_MAX_VPI))
return TRY_OTHER; /* too big */
if (part[0] == ATM_VPI_UNSPEC) return FATAL; /* bad */
addr->sap_family = AF_ATMPVC;
addr->sap_addr.itf = part[0];
addr->sap_addr.vpi = part[1];
addr->sap_addr.vci = part[2];
return 0;
}
static int do_try_nsap(const char *text,struct sockaddr_atmsvc *addr,int flags)
{
const char *walk;
int count,pos,dot;
int offset,len;
char value;
count = dot = 0;
for (walk = text; *walk; walk++)
if (isdigit(*walk)) {
if (count++ == 15) break;
dot = 1;
}
else if (*text != '.') break;
else if (!dot) return FATAL; /* two dots in a row */
else dot = 0;
if (*walk != ':') {
pos = 0;
offset = 0;
}
else {
if (!dot || *text == '0') return FATAL;
addr->sas_addr.prv[0] = ATM_AFI_E164;
addr->sas_addr.prv[1] = 0;
memset(addr->sas_addr.prv+1,0,8);
for (pos = 18-count-1; *text; text++) {
if (*text == '.') continue;
if (*text == ':') break;
else {
if (pos & 1) addr->sas_addr.prv[pos >> 1] |= *text-'0';
else addr->sas_addr.prv[pos >> 1] = (*text-'0') << 4;
pos++;
}
}
addr->sas_addr.prv[8] |= 0xf;
text++;
pos++;
offset = 72;
}
for (dot = 0; *text; text++)
if (isxdigit(*text)) {
if (pos == ATM_ESA_LEN*2) return TRY_OTHER; /* too long */
value = isdigit(*text) ? *text-'0' : (islower(*text) ?
toupper(*text) : *text)-'A'+10;
if (pos & 1) addr->sas_addr.prv[pos >> 1] |= value;
else addr->sas_addr.prv[pos >> 1] = value << 4;
pos++;
dot = 1;
}
else
if (*text == '/' && (flags & T2A_WILDCARD)) break;
else if (*text != '.') return TRY_OTHER;
else {
if (!dot) return FATAL; /* two dots in a row */
dot = 0;
}
if (!dot) return FATAL;
if (pos > 1 && !*addr->sas_addr.prv)
return TRY_OTHER; /* no leading zeroes */
if (!*text)
return pos != ATM_ESA_LEN*2 ? TRY_OTHER : ATM_ESA_LEN*2;
/* handle bad length */
len = 0;
while (*++text) {
if (!isdigit(*text)) return -1; /* non-digit in length */
if (len >= pos*4) return -1; /* too long */
len = len*10+*text-'0';
}
if (len > 7 && addr->sas_addr.prv[0] != ATM_AFI_E164) offset = 72;
if (len < offset) return FATAL;
return len > pos*4 ? TRY_OTHER : len;
}
static int try_nsap(const char *text,struct sockaddr_atmsvc *addr,int flags)
{
int result;
result = do_try_nsap(text,addr,flags);
if (result < 0) return result;
addr->sas_family = AF_ATMSVC;
*addr->sas_addr.pub = 0;
return result;
}
static int try_e164(const char *text,struct sockaddr_atmsvc *addr,int flags)
{
int i,dot,result;
if (*text == ':' || *text == '+') text++;
for (i = dot = 0; *text; text++)
if (isdigit(*text)) {
if (i == ATM_E164_LEN) return TRY_OTHER; /* too long */
addr->sas_addr.pub[i++] = *text;
dot = 1;
}
else if (*text != '.') break;
else {
if (!dot) return TRY_OTHER; /* two dots in a row */
dot = 0;
}
if (!dot) return TRY_OTHER;
addr->sas_addr.pub[i] = 0;
*addr->sas_addr.prv = 0;
result = 0;
if (*text) {
if (*text++ != '+') return TRY_OTHER;
else {
result = do_try_nsap(text,addr,flags);
if (result < 0) return FATAL;
}
}
addr->sas_family = AF_ATMSVC;
return result;
}
static int search(FILE *file,const char *text,struct sockaddr *addr,int length,
int flags)
{
char line[MAX_ATM_NAME_LEN+1];
const char *here;
int result;
while (fgets(line,MAX_ATM_NAME_LEN,file)) {
if (!strtok(line,"\t\n ")) continue;
while ((here = strtok(NULL,"\t\n ")))
if (!strcasecmp(here,text)) {
here = strtok(line,"\t\n ");
result = text2atm(here,addr,length,flags);
if (result >= 0) return result;
}
}
return TRY_OTHER;
}
static int try_name(const char *text,struct sockaddr *addr,int length,
int flags)
{
FILE *file;
int result;
if (!(file = fopen(HOSTS_ATM,"r"))) return TRY_OTHER;
result = search(file,text,addr,length,flags);
(void) fclose(file);
return result;
}
int text2atm(const char *text,struct sockaddr *addr,int length,int flags)
{
int result;
if (!*text) return -1;
if (!(flags & (T2A_PVC | T2A_SVC))) flags |= T2A_PVC | T2A_SVC;
if (length < sizeof(struct sockaddr_atmpvc)) return -1;
if (flags & T2A_PVC) {
result = try_pvc(text,(struct sockaddr_atmpvc *) addr,flags);
if (result != TRY_OTHER) return result;
}
if ((flags & T2A_SVC) && length >= sizeof(struct sockaddr_atmsvc)) {
result = try_nsap(text,(struct sockaddr_atmsvc *) addr,flags);
if (result != TRY_OTHER) return result;
result = try_e164(text,(struct sockaddr_atmsvc *) addr,flags);
if (result != TRY_OTHER) return result;
}
if (!(flags & T2A_NAME)) return -1;
result = try_name(text,addr,length,flags & ~T2A_NAME);
if (result == TRY_OTHER && !(flags & T2A_LOCAL))
result = ans_byname(text,(struct sockaddr_atmsvc *) addr,length,flags);
if (result != TRY_OTHER) return result;
return -1;
}
|