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