xref: /openbsd-src/regress/lib/libc/netdb/netdb.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: netdb.c,v 1.1 2004/10/25 15:10:36 otto Exp $	*/
2 
3 /*
4  * Public domain, 2004, Otto Moerbeek <otto@drijf.net>
5  */
6 
7 #include <err.h>
8 #include <netdb.h>
9 #include <stdarg.h>
10 
11 int ret = 0;
12 
13 void
14 checkp(int n, struct protoent *p, int proto, const char *name, ...)
15 {
16 	va_list va;
17 	char *a;
18 	int i;
19 
20 	if (p == NULL) {
21 		warnx("%d proto struct is NULL", n);
22 		ret = 1;
23 		return;
24 	}
25 	if (p->p_proto != proto) {
26 		warnx("%d proto num mismatch %d %d", n, p->p_proto, proto);
27 		ret = 1;
28 	}
29 	if (strcmp(p->p_name, name) != 0) {
30 		warnx("%d proto name mismatch %s %s", n, p->p_name, name);
31 		ret = 1;
32 	}
33 	va_start(va, name);
34 	a = va_arg(va, char *);
35 	i = 0;
36 	while (a != NULL) {
37 		if (strcmp(p->p_aliases[i], a) != 0) {
38 			warnx("%d proto alias mismatch %s %s", n,
39 			    p->p_aliases[i], a);
40 			ret = 1;
41 		}
42 		i++;
43 		a = va_arg(va, char *);
44 	}
45 	if (p->p_aliases[i] != NULL) {
46 			warnx("%d proto alias list does not end with NULL", n);
47 			ret = 1;
48 	}
49 	va_end(va);
50 }
51 
52 
53 void
54 checks(int n, struct servent *s, int port, const char *proto,
55     const char *name, ...)
56 {
57 	va_list va;
58 	char *a;
59 	int i;
60 
61 	if (s == NULL) {
62 		warnx("%d serv struct is NULL", n);
63 		ret = 1;
64 		return;
65 	}
66 	if (s->s_port != ntohs(port)) {
67 		warnx("%d port num mismatch %d %d", n, s->s_port, port);
68 		ret = 1;
69 	}
70 	if (strcmp(s->s_name, name) != 0) {
71 		warnx("%d serv name mismatch %s %s", n, s->s_name, name);
72 		ret = 1;
73 	}
74 	if (strcmp(s->s_proto, proto) != 0) {
75 		warnx("%d serv proto mismatch %s %s", n, s->s_proto, proto);
76 		ret = 1;
77 	}
78 	va_start(va, name);
79 	a = va_arg(va, char *);
80 	i = 0;
81 	while (a != NULL) {
82 		if (strcmp(s->s_aliases[i], a) != 0) {
83 			warnx("%d serv alias mismatch %s %s", n,
84 			    s->s_aliases[i], a);
85 			ret = 1;
86 		}
87 		i++;
88 		a = va_arg(va, char *);
89 	}
90 	if (s->s_aliases[i] != NULL) {
91 			warnx("%d serv alias list does not end with NULL", n);
92 			ret = 1;
93 	}
94 	va_end(va);
95 }
96 
97 int
98 main()
99 {
100 	struct protoent *p;
101 	struct protoent protoent;
102 	struct protoent_data protoent_data;
103 	struct servent *s;
104 	struct servent servent;
105 	struct servent_data servent_data;
106 	int r;
107 
108 	p = getprotobynumber(35);
109 	checkp(1, p, 35, "idpr", "IDPR", (char *)NULL);
110 
111 	p = getprotobyname("igp");
112 	checkp(2, p, 9, "igp", "IGP", (char *) NULL);
113 
114 	p = getprotobyname("RDP");
115 	checkp(3, p, 27, "rdp", "RDP", (char *) NULL);
116 
117 	p = getprotobyname("vrrp");
118 	checkp(4, p, 112, "carp", "CARP", "vrrp", (char *) NULL);
119 
120 	p = getprotobyname("nonexistent");
121 	if (p != NULL)
122 		err(1, "nonexistent proto found");
123 
124 	memset(&protoent_data, 0, sizeof(protoent_data));
125 	r = getprotobynumber_r(35, &protoent, &protoent_data);
126 	if (r != 0)
127 		err(1, "proto not found");
128 
129 	checkp(5, &protoent, 35, "idpr", "IDPR", (char *)NULL);
130 
131 	r = getprotobyname_r("vrrp", &protoent, &protoent_data);
132 	if (r != 0)
133 		err(1, "proto not found");
134 	checkp(6, &protoent, 112, "carp", "CARP", "vrrp", (char *) NULL);
135 
136 	r = getprotobyname_r("nonexistent", &protoent, &protoent_data);
137 	if (r != -1)
138 		err(1, "nonexistent proto found");
139 
140 	r = getprotobyname_r("", &protoent, &protoent_data);
141 	if (r != -1)
142 		err(1, "nonexistent proto found");
143 
144 
145 	r = getprotobynumber_r(256, &protoent, &protoent_data);
146 	if (r != -1)
147 		err(1, "nonexistent proto found");
148 
149 	s = getservbyname("zip", NULL);
150 	checks(7, s, 6, "ddp", "zip", (char *)NULL);
151 
152 	s = getservbyname("nicname", "tcp");
153 	checks(8, s, 43, "tcp", "whois", "nicname", (char *)NULL);
154 
155 	s = getservbyport(htons(42), "tcp");
156 	checks(9, s, 42, "tcp", "nameserver", "name", (char *)NULL);
157 
158 	memset(&servent_data, 0, sizeof(servent_data));
159 	r = getservbyname_r("zip", "ddp", &servent, &servent_data);
160 	if (r != 0)
161 		err(1, "servent not found");
162 	checks(10, &servent, 6, "ddp", "zip", (char *)NULL);
163 
164 	r = getservbyport_r(htons(520), NULL, &servent, &servent_data);
165 	if (r != 0)
166 		err(1, "servent not found");
167 	checks(11, &servent, 520, "udp", "route", "router", "routed", (char *)NULL);
168 
169 	r = getservbyname_r("nonexistent", NULL, &servent, &servent_data);
170 	if (r != -1)
171 		err(1, "nonexiststent servent found");
172 
173 	r = getservbyport_r(htons(50000), NULL, &servent, &servent_data);
174 	if (r != -1)
175 		err(1, "nonexistent servent found");
176 
177 	r = getservbyport_r(htons(520), "tcp", &servent, &servent_data);
178 	if (r != -1)
179 		err(1, "nonexistent servent found");
180 
181 	return ret;
182 }
183