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
|
#if !defined(lint) && !defined(SABER)
static char rcsid[] = "$Id: addr.c,v 8.4 1996/05/23 08:21:28 vixie Exp $";
#endif /* not lint */
/* Copyright (c) 1996 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#include <sys/param.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "../conf/portability.h"
extern char *optarg;
extern int optind;
extern int opterr;
extern int optopt;
static const char *prog = "addr";
#define BIGGEST_ADDRESS IN6ADDRSZ
static void
usage() {
fprintf(stderr,
"usage: %s [-4] [-6] [-n hexstring] [-p address]\n",
prog);
exit(1);
}
/* Warning: this scribbles on `dst' even if it's going to return `0'. */
static int
hexstring(src, dst, len)
const char *src;
u_char *dst;
int len;
{
static const char xdigits[] = "0123456789abcdef";
u_char *ptr = dst, *end = dst + len;
u_int val;
int ch, digits;
val = 0;
digits = 0;
bzero((void*)dst, len);
while ((ch = *src++) != '\0') {
if (ch == '0' && (*src == 'x' || *src == 'X')) {
src++;
continue;
}
if (isascii(ch) && (isspace(ch) || ispunct(ch))) {
if (digits > 0) {
if (ptr == end)
return (0);
*ptr++ = (u_char) (val & 0xff);
val = 0;
digits = 0;
}
digits = 0;
continue;
}
if (!isascii(ch) || !isxdigit(ch))
return (0);
if (isupper(ch))
ch = tolower(ch);
/* Clock it in using little endian arithmetic. */
val <<= 4;
val |= (strchr(xdigits, ch) - xdigits);
if (++digits == 2) {
if (ptr == end)
return (0);
*ptr++ = (u_char) (val & 0xff);
digits = 0;
val = 0;
}
}
if (digits > 0) {
if (ptr == end)
return (0);
*ptr++ = (u_char) (val & 0xff);
}
return ((ptr - dst) == len);
}
static void
display(input, af, addr, len)
const char *input;
int af;
const u_char *addr;
int len;
{
static int before = 0;
char p[sizeof "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255"];
int i;
if (before)
putchar('\n');
else
before++;
printf("Input: \"%s\"\n", input);
printf("Network: [af%d len%d]", af, len);
for (i = 0; i < len; i++)
printf(" %02x", addr[i]);
putchar('\n');
printf("Presentation: \"%s\"\n", inet_ntop(af, addr, p, sizeof p));
}
int
main(argc, argv)
int argc;
char *argv[];
{
u_char addr[BIGGEST_ADDRESS];
int optchr, af, len;
prog = argv[0];
af = AF_INET;
len = INADDRSZ;
while ((optchr = getopt(argc, argv, "46n:p:")) != -1) {
switch (optchr) {
case '4':
af = AF_INET;
len = INADDRSZ;
break;
case '6':
af = AF_INET6;
len = IN6ADDRSZ;
break;
case 'n':
if (!hexstring(optarg, addr, len)) {
fprintf(stderr, "bad hex string: \"%s\"\n",
optarg);
usage();
/* NOTREACHED */
}
display(optarg, af, addr, len);
break;
case 'p':
if (inet_pton(af, optarg, addr) <= 0) {
fprintf(stderr, "bad address: \"%s\"\n",
optarg);
usage();
/* NOTREACHED */
}
display(optarg, af, addr, len);
break;
default:
usage();
/* NOTREACHED */
}
}
exit(0);
/* NOTREACHED */
}
|