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
|
/* $OpenBSD: netdb.c,v 1.1 2004/10/25 15:10:36 otto Exp $ */
/*
* Public domain, 2004, Otto Moerbeek <otto@drijf.net>
*/
#include <err.h>
#include <netdb.h>
#include <stdarg.h>
int ret = 0;
void
checkp(int n, struct protoent *p, int proto, const char *name, ...)
{
va_list va;
char *a;
int i;
if (p == NULL) {
warnx("%d proto struct is NULL", n);
ret = 1;
return;
}
if (p->p_proto != proto) {
warnx("%d proto num mismatch %d %d", n, p->p_proto, proto);
ret = 1;
}
if (strcmp(p->p_name, name) != 0) {
warnx("%d proto name mismatch %s %s", n, p->p_name, name);
ret = 1;
}
va_start(va, name);
a = va_arg(va, char *);
i = 0;
while (a != NULL) {
if (strcmp(p->p_aliases[i], a) != 0) {
warnx("%d proto alias mismatch %s %s", n,
p->p_aliases[i], a);
ret = 1;
}
i++;
a = va_arg(va, char *);
}
if (p->p_aliases[i] != NULL) {
warnx("%d proto alias list does not end with NULL", n);
ret = 1;
}
va_end(va);
}
void
checks(int n, struct servent *s, int port, const char *proto,
const char *name, ...)
{
va_list va;
char *a;
int i;
if (s == NULL) {
warnx("%d serv struct is NULL", n);
ret = 1;
return;
}
if (s->s_port != ntohs(port)) {
warnx("%d port num mismatch %d %d", n, s->s_port, port);
ret = 1;
}
if (strcmp(s->s_name, name) != 0) {
warnx("%d serv name mismatch %s %s", n, s->s_name, name);
ret = 1;
}
if (strcmp(s->s_proto, proto) != 0) {
warnx("%d serv proto mismatch %s %s", n, s->s_proto, proto);
ret = 1;
}
va_start(va, name);
a = va_arg(va, char *);
i = 0;
while (a != NULL) {
if (strcmp(s->s_aliases[i], a) != 0) {
warnx("%d serv alias mismatch %s %s", n,
s->s_aliases[i], a);
ret = 1;
}
i++;
a = va_arg(va, char *);
}
if (s->s_aliases[i] != NULL) {
warnx("%d serv alias list does not end with NULL", n);
ret = 1;
}
va_end(va);
}
int
main()
{
struct protoent *p;
struct protoent protoent;
struct protoent_data protoent_data;
struct servent *s;
struct servent servent;
struct servent_data servent_data;
int r;
p = getprotobynumber(35);
checkp(1, p, 35, "idpr", "IDPR", (char *)NULL);
p = getprotobyname("igp");
checkp(2, p, 9, "igp", "IGP", (char *) NULL);
p = getprotobyname("RDP");
checkp(3, p, 27, "rdp", "RDP", (char *) NULL);
p = getprotobyname("vrrp");
checkp(4, p, 112, "carp", "CARP", "vrrp", (char *) NULL);
p = getprotobyname("nonexistent");
if (p != NULL)
err(1, "nonexistent proto found");
memset(&protoent_data, 0, sizeof(protoent_data));
r = getprotobynumber_r(35, &protoent, &protoent_data);
if (r != 0)
err(1, "proto not found");
checkp(5, &protoent, 35, "idpr", "IDPR", (char *)NULL);
r = getprotobyname_r("vrrp", &protoent, &protoent_data);
if (r != 0)
err(1, "proto not found");
checkp(6, &protoent, 112, "carp", "CARP", "vrrp", (char *) NULL);
r = getprotobyname_r("nonexistent", &protoent, &protoent_data);
if (r != -1)
err(1, "nonexistent proto found");
r = getprotobyname_r("", &protoent, &protoent_data);
if (r != -1)
err(1, "nonexistent proto found");
r = getprotobynumber_r(256, &protoent, &protoent_data);
if (r != -1)
err(1, "nonexistent proto found");
s = getservbyname("zip", NULL);
checks(7, s, 6, "ddp", "zip", (char *)NULL);
s = getservbyname("nicname", "tcp");
checks(8, s, 43, "tcp", "whois", "nicname", (char *)NULL);
s = getservbyport(htons(42), "tcp");
checks(9, s, 42, "tcp", "nameserver", "name", (char *)NULL);
memset(&servent_data, 0, sizeof(servent_data));
r = getservbyname_r("zip", "ddp", &servent, &servent_data);
if (r != 0)
err(1, "servent not found");
checks(10, &servent, 6, "ddp", "zip", (char *)NULL);
r = getservbyport_r(htons(520), NULL, &servent, &servent_data);
if (r != 0)
err(1, "servent not found");
checks(11, &servent, 520, "udp", "route", "router", "routed", (char *)NULL);
r = getservbyname_r("nonexistent", NULL, &servent, &servent_data);
if (r != -1)
err(1, "nonexiststent servent found");
r = getservbyport_r(htons(50000), NULL, &servent, &servent_data);
if (r != -1)
err(1, "nonexistent servent found");
r = getservbyport_r(htons(520), "tcp", &servent, &servent_data);
if (r != -1)
err(1, "nonexistent servent found");
return ret;
}
|