route.c revision 1.54 1 1.54 enami /* $NetBSD: route.c,v 1.54 2001/03/08 03:47:04 enami Exp $ */
2 1.14 thorpej
3 1.1 cgd /*
4 1.10 mycroft * Copyright (c) 1983, 1988, 1993
5 1.10 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.23 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.14 thorpej #if 0
39 1.14 thorpej static char sccsid[] = "from: @(#)route.c 8.3 (Berkeley) 3/9/94";
40 1.14 thorpej #else
41 1.54 enami __RCSID("$NetBSD: route.c,v 1.54 2001/03/08 03:47:04 enami Exp $");
42 1.14 thorpej #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/param.h>
46 1.10 mycroft #include <sys/protosw.h>
47 1.1 cgd #include <sys/socket.h>
48 1.1 cgd #include <sys/mbuf.h>
49 1.30 mrg #include <sys/un.h>
50 1.1 cgd
51 1.1 cgd #include <net/if.h>
52 1.10 mycroft #include <net/if_dl.h>
53 1.10 mycroft #include <net/if_types.h>
54 1.11 jtc #define _KERNEL
55 1.1 cgd #include <net/route.h>
56 1.11 jtc #undef _KERNEL
57 1.1 cgd #include <netinet/in.h>
58 1.21 christos #include <netatalk/at.h>
59 1.30 mrg #include <netiso/iso.h>
60 1.1 cgd
61 1.1 cgd #include <netns/ns.h>
62 1.1 cgd
63 1.10 mycroft #include <sys/sysctl.h>
64 1.4 paul
65 1.38 itojun #include <arpa/inet.h>
66 1.38 itojun
67 1.29 mrg #include <err.h>
68 1.1 cgd #include <netdb.h>
69 1.1 cgd #include <stdio.h>
70 1.10 mycroft #include <stdlib.h>
71 1.1 cgd #include <string.h>
72 1.10 mycroft #include <unistd.h>
73 1.29 mrg
74 1.10 mycroft #include "netstat.h"
75 1.1 cgd
76 1.10 mycroft #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
77 1.1 cgd
78 1.39 itojun /* alignment constraint for routing socket */
79 1.39 itojun #define ROUNDUP(a) \
80 1.39 itojun ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
81 1.39 itojun #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
82 1.39 itojun
83 1.1 cgd /*
84 1.1 cgd * Definitions for showing gateway flags.
85 1.1 cgd */
86 1.1 cgd struct bits {
87 1.1 cgd short b_mask;
88 1.1 cgd char b_val;
89 1.1 cgd } bits[] = {
90 1.1 cgd { RTF_UP, 'U' },
91 1.1 cgd { RTF_GATEWAY, 'G' },
92 1.1 cgd { RTF_HOST, 'H' },
93 1.10 mycroft { RTF_REJECT, 'R' },
94 1.1 cgd { RTF_DYNAMIC, 'D' },
95 1.1 cgd { RTF_MODIFIED, 'M' },
96 1.10 mycroft { RTF_DONE, 'd' }, /* Completed -- for routing messages only */
97 1.10 mycroft { RTF_MASK, 'm' }, /* Mask Present -- for routing messages only */
98 1.1 cgd { RTF_CLONING, 'C' },
99 1.1 cgd { RTF_XRESOLVE, 'X' },
100 1.1 cgd { RTF_LLINFO, 'L' },
101 1.10 mycroft { RTF_STATIC, 'S' },
102 1.52 itojun { RTF_BLACKHOLE,'B' },
103 1.52 itojun { RTF_CLONED, 'c' },
104 1.10 mycroft { RTF_PROTO1, '1' },
105 1.10 mycroft { RTF_PROTO2, '2' },
106 1.1 cgd { 0 }
107 1.1 cgd };
108 1.1 cgd
109 1.30 mrg /*
110 1.30 mrg * XXX we put all of the sockaddr types in here to force the alignment
111 1.30 mrg * to be correct.
112 1.30 mrg */
113 1.37 chopps static union sockaddr_union {
114 1.10 mycroft struct sockaddr u_sa;
115 1.30 mrg struct sockaddr_in u_in;
116 1.30 mrg struct sockaddr_un u_un;
117 1.30 mrg struct sockaddr_iso u_iso;
118 1.30 mrg struct sockaddr_at u_at;
119 1.30 mrg struct sockaddr_dl u_dl;
120 1.30 mrg struct sockaddr_ns u_ns;
121 1.10 mycroft u_short u_data[128];
122 1.38 itojun int u_dummy; /* force word-alignment */
123 1.10 mycroft } pt_u;
124 1.10 mycroft
125 1.10 mycroft int do_rtent = 0;
126 1.10 mycroft struct rtentry rtentry;
127 1.10 mycroft struct radix_node rnode;
128 1.10 mycroft struct radix_mask rmask;
129 1.10 mycroft
130 1.10 mycroft int NewTree = 0;
131 1.10 mycroft
132 1.10 mycroft static struct sockaddr *kgetsa __P((struct sockaddr *));
133 1.10 mycroft static void p_tree __P((struct radix_node *));
134 1.21 christos static void p_rtnode __P((void));
135 1.21 christos static void ntreestuff __P((void));
136 1.10 mycroft static void np_rtentry __P((struct rt_msghdr *));
137 1.16 mellon static void p_sockaddr __P((const struct sockaddr *,
138 1.16 mellon const struct sockaddr *, int, int));
139 1.50 is static void p_flags __P((int));
140 1.10 mycroft static void p_rtentry __P((struct rtentry *));
141 1.21 christos static void ntreestuff __P((void));
142 1.21 christos static u_long forgemask __P((u_long));
143 1.29 mrg static void domask __P((char *, size_t, u_long, u_long));
144 1.3 cgd
145 1.3 cgd /*
146 1.1 cgd * Print routing tables.
147 1.1 cgd */
148 1.10 mycroft void
149 1.10 mycroft routepr(rtree)
150 1.10 mycroft u_long rtree;
151 1.1 cgd {
152 1.10 mycroft struct radix_node_head *rnh, head;
153 1.10 mycroft int i;
154 1.1 cgd
155 1.1 cgd printf("Routing tables\n");
156 1.10 mycroft
157 1.10 mycroft if (Aflag == 0 && NewTree)
158 1.10 mycroft ntreestuff();
159 1.10 mycroft else {
160 1.10 mycroft if (rtree == 0) {
161 1.10 mycroft printf("rt_tables: symbol not in namelist\n");
162 1.10 mycroft return;
163 1.10 mycroft }
164 1.10 mycroft
165 1.10 mycroft kget(rtree, rt_tables);
166 1.10 mycroft for (i = 0; i <= AF_MAX; i++) {
167 1.10 mycroft if ((rnh = rt_tables[i]) == 0)
168 1.10 mycroft continue;
169 1.10 mycroft kget(rnh, head);
170 1.10 mycroft if (i == AF_UNSPEC) {
171 1.10 mycroft if (Aflag && af == 0) {
172 1.10 mycroft printf("Netmasks:\n");
173 1.10 mycroft p_tree(head.rnh_treetop);
174 1.10 mycroft }
175 1.10 mycroft } else if (af == AF_UNSPEC || af == i) {
176 1.10 mycroft pr_family(i);
177 1.10 mycroft do_rtent = 1;
178 1.43 soren pr_rthdr(i);
179 1.10 mycroft p_tree(head.rnh_treetop);
180 1.10 mycroft }
181 1.1 cgd }
182 1.1 cgd }
183 1.1 cgd }
184 1.1 cgd
185 1.10 mycroft /*
186 1.10 mycroft * Print address family header before a section of the routing table.
187 1.10 mycroft */
188 1.10 mycroft void
189 1.10 mycroft pr_family(af)
190 1.10 mycroft int af;
191 1.4 paul {
192 1.10 mycroft char *afname;
193 1.4 paul
194 1.10 mycroft switch (af) {
195 1.4 paul case AF_INET:
196 1.10 mycroft afname = "Internet";
197 1.10 mycroft break;
198 1.38 itojun #ifdef INET6
199 1.38 itojun case AF_INET6:
200 1.38 itojun afname = "Internet6";
201 1.38 itojun break;
202 1.38 itojun #endif
203 1.4 paul case AF_NS:
204 1.10 mycroft afname = "XNS";
205 1.10 mycroft break;
206 1.4 paul case AF_ISO:
207 1.10 mycroft afname = "ISO";
208 1.4 paul break;
209 1.21 christos case AF_APPLETALK:
210 1.21 christos afname = "AppleTalk";
211 1.21 christos break;
212 1.10 mycroft case AF_CCITT:
213 1.10 mycroft afname = "X.25";
214 1.4 paul break;
215 1.4 paul default:
216 1.10 mycroft afname = NULL;
217 1.10 mycroft break;
218 1.4 paul }
219 1.10 mycroft if (afname)
220 1.10 mycroft printf("\n%s:\n", afname);
221 1.10 mycroft else
222 1.10 mycroft printf("\nProtocol Family %d:\n", af);
223 1.4 paul }
224 1.4 paul
225 1.10 mycroft /* column widths; each followed by one space */
226 1.38 itojun #ifndef INET6
227 1.41 itojun #define WID_DST(af) 18 /* width of destination column */
228 1.41 itojun #define WID_GW(af) 18 /* width of gateway column */
229 1.38 itojun #else
230 1.41 itojun /* width of destination/gateway column */
231 1.49 itojun #ifdef KAME_SCOPEID
232 1.49 itojun /* strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4 */
233 1.49 itojun #define WID_DST(af) ((af) == AF_INET6 ? (nflag ? 34 : 18) : 18)
234 1.49 itojun #define WID_GW(af) ((af) == AF_INET6 ? (nflag ? 30 : 18) : 18)
235 1.49 itojun #else
236 1.42 itojun /* strlen("fe80::aaaa:bbbb:cccc:dddd") == 25, strlen("/128") == 4 */
237 1.42 itojun #define WID_DST(af) ((af) == AF_INET6 ? (nflag ? 29 : 18) : 18)
238 1.42 itojun #define WID_GW(af) ((af) == AF_INET6 ? (nflag ? 25 : 18) : 18)
239 1.49 itojun #endif
240 1.38 itojun #endif /* INET6 */
241 1.4 paul
242 1.10 mycroft /*
243 1.10 mycroft * Print header for routing table columns.
244 1.10 mycroft */
245 1.10 mycroft void
246 1.41 itojun pr_rthdr(af)
247 1.41 itojun int af;
248 1.1 cgd {
249 1.44 itojun
250 1.10 mycroft if (Aflag)
251 1.10 mycroft printf("%-8.8s ","Address");
252 1.13 thorpej printf("%-*.*s %-*.*s %-6.6s %6.6s%8.8s %6.6s %s\n",
253 1.41 itojun WID_DST(af), WID_DST(af), "Destination",
254 1.41 itojun WID_GW(af), WID_GW(af), "Gateway",
255 1.13 thorpej "Flags", "Refs", "Use", "Mtu", "Interface");
256 1.1 cgd }
257 1.1 cgd
258 1.10 mycroft static struct sockaddr *
259 1.1 cgd kgetsa(dst)
260 1.23 lukem struct sockaddr *dst;
261 1.1 cgd {
262 1.10 mycroft
263 1.1 cgd kget(dst, pt_u.u_sa);
264 1.10 mycroft if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
265 1.10 mycroft kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
266 1.1 cgd return (&pt_u.u_sa);
267 1.1 cgd }
268 1.1 cgd
269 1.10 mycroft static void
270 1.1 cgd p_tree(rn)
271 1.10 mycroft struct radix_node *rn;
272 1.1 cgd {
273 1.1 cgd
274 1.1 cgd again:
275 1.1 cgd kget(rn, rnode);
276 1.1 cgd if (rnode.rn_b < 0) {
277 1.1 cgd if (Aflag)
278 1.21 christos printf("%-8.8lx ", (u_long) rn);
279 1.10 mycroft if (rnode.rn_flags & RNF_ROOT) {
280 1.10 mycroft if (Aflag)
281 1.10 mycroft printf("(root node)%s",
282 1.1 cgd rnode.rn_dupedkey ? " =>\n" : "\n");
283 1.10 mycroft } else if (do_rtent) {
284 1.1 cgd kget(rn, rtentry);
285 1.1 cgd p_rtentry(&rtentry);
286 1.1 cgd if (Aflag)
287 1.1 cgd p_rtnode();
288 1.1 cgd } else {
289 1.1 cgd p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
290 1.16 mellon NULL, 0, 44);
291 1.1 cgd putchar('\n');
292 1.1 cgd }
293 1.21 christos if ((rn = rnode.rn_dupedkey) != NULL)
294 1.1 cgd goto again;
295 1.1 cgd } else {
296 1.1 cgd if (Aflag && do_rtent) {
297 1.21 christos printf("%-8.8lx ", (u_long) rn);
298 1.1 cgd p_rtnode();
299 1.1 cgd }
300 1.1 cgd rn = rnode.rn_r;
301 1.1 cgd p_tree(rnode.rn_l);
302 1.1 cgd p_tree(rn);
303 1.1 cgd }
304 1.1 cgd }
305 1.1 cgd
306 1.10 mycroft static void
307 1.1 cgd p_rtnode()
308 1.1 cgd {
309 1.10 mycroft struct radix_mask *rm = rnode.rn_mklist;
310 1.29 mrg char nbuf[20];
311 1.1 cgd
312 1.1 cgd if (rnode.rn_b < 0) {
313 1.1 cgd if (rnode.rn_mask) {
314 1.1 cgd printf("\t mask ");
315 1.1 cgd p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
316 1.16 mellon NULL, 0, -1);
317 1.1 cgd } else if (rm == 0)
318 1.1 cgd return;
319 1.1 cgd } else {
320 1.29 mrg (void)snprintf(nbuf, sizeof nbuf, "(%d)", rnode.rn_b);
321 1.21 christos printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long) rnode.rn_l,
322 1.21 christos (u_long) rnode.rn_r);
323 1.1 cgd }
324 1.1 cgd while (rm) {
325 1.1 cgd kget(rm, rmask);
326 1.29 mrg (void)snprintf(nbuf, sizeof nbuf, " %d refs, ", rmask.rm_refs);
327 1.21 christos printf(" mk = %8.8lx {(%d),%s", (u_long) rm,
328 1.21 christos -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " ");
329 1.16 mellon if (rmask.rm_flags & RNF_NORMAL) {
330 1.16 mellon struct radix_node rnode_aux;
331 1.16 mellon printf(" <normal>, ");
332 1.16 mellon kget(rmask.rm_leaf, rnode_aux);
333 1.16 mellon p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask),
334 1.16 mellon NULL, 0, -1);
335 1.16 mellon } else
336 1.29 mrg p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask),
337 1.29 mrg NULL, 0, -1);
338 1.1 cgd putchar('}');
339 1.21 christos if ((rm = rmask.rm_mklist) != NULL)
340 1.1 cgd printf(" ->");
341 1.1 cgd }
342 1.1 cgd putchar('\n');
343 1.1 cgd }
344 1.1 cgd
345 1.10 mycroft static void
346 1.1 cgd ntreestuff()
347 1.1 cgd {
348 1.10 mycroft size_t needed;
349 1.10 mycroft int mib[6];
350 1.1 cgd char *buf, *next, *lim;
351 1.23 lukem struct rt_msghdr *rtm;
352 1.1 cgd
353 1.29 mrg mib[0] = CTL_NET;
354 1.29 mrg mib[1] = PF_ROUTE;
355 1.29 mrg mib[2] = 0;
356 1.29 mrg mib[3] = 0;
357 1.29 mrg mib[4] = NET_RT_DUMP;
358 1.29 mrg mib[5] = 0;
359 1.29 mrg if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
360 1.29 mrg err(1, "route sysctl estimate");
361 1.1 cgd if ((buf = malloc(needed)) == 0)
362 1.29 mrg errx(1, "out of space");
363 1.29 mrg if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
364 1.29 mrg err(1, "sysctl of routing table");
365 1.1 cgd lim = buf + needed;
366 1.1 cgd for (next = buf; next < lim; next += rtm->rtm_msglen) {
367 1.1 cgd rtm = (struct rt_msghdr *)next;
368 1.1 cgd np_rtentry(rtm);
369 1.1 cgd }
370 1.1 cgd }
371 1.1 cgd
372 1.10 mycroft static void
373 1.1 cgd np_rtentry(rtm)
374 1.23 lukem struct rt_msghdr *rtm;
375 1.1 cgd {
376 1.23 lukem struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
377 1.10 mycroft #ifdef notdef
378 1.10 mycroft static int masks_done, banner_printed;
379 1.10 mycroft #endif
380 1.10 mycroft static int old_af;
381 1.1 cgd int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
382 1.1 cgd
383 1.40 is if (Lflag && (rtm->rtm_flags & RTF_LLINFO))
384 1.40 is return;
385 1.1 cgd #ifdef notdef
386 1.1 cgd /* for the moment, netmasks are skipped over */
387 1.1 cgd if (!banner_printed) {
388 1.1 cgd printf("Netmasks:\n");
389 1.1 cgd banner_printed = 1;
390 1.1 cgd }
391 1.1 cgd if (masks_done == 0) {
392 1.1 cgd if (rtm->rtm_addrs != RTA_DST ) {
393 1.1 cgd masks_done = 1;
394 1.1 cgd af = sa->sa_family;
395 1.1 cgd }
396 1.1 cgd } else
397 1.1 cgd #endif
398 1.1 cgd af = sa->sa_family;
399 1.1 cgd if (af != old_af) {
400 1.10 mycroft pr_family(af);
401 1.1 cgd old_af = af;
402 1.1 cgd }
403 1.1 cgd if (rtm->rtm_addrs == RTA_DST)
404 1.16 mellon p_sockaddr(sa, NULL, 0, 36);
405 1.1 cgd else {
406 1.16 mellon p_sockaddr(sa, NULL, rtm->rtm_flags, 16);
407 1.44 itojun #if 0
408 1.44 itojun if (sa->sa_len == 0)
409 1.44 itojun sa->sa_len = sizeof(long);
410 1.44 itojun #endif
411 1.39 itojun sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
412 1.16 mellon p_sockaddr(sa, NULL, 0, 18);
413 1.1 cgd }
414 1.50 is p_flags(rtm->rtm_flags & interesting);
415 1.1 cgd putchar('\n');
416 1.1 cgd }
417 1.1 cgd
418 1.10 mycroft static void
419 1.16 mellon p_sockaddr(sa, mask, flags, width)
420 1.16 mellon const struct sockaddr *sa, *mask;
421 1.10 mycroft int flags, width;
422 1.1 cgd {
423 1.10 mycroft char workbuf[128], *cplim;
424 1.23 lukem char *cp = workbuf;
425 1.1 cgd
426 1.1 cgd switch(sa->sa_family) {
427 1.1 cgd case AF_INET:
428 1.1 cgd {
429 1.23 lukem struct sockaddr_in *sin = (struct sockaddr_in *)sa;
430 1.1 cgd
431 1.45 sommerfe if ((sin->sin_addr.s_addr == INADDR_ANY) &&
432 1.45 sommerfe (mask != NULL) &&
433 1.45 sommerfe (((struct sockaddr_in *)mask)->sin_addr.s_addr == 0))
434 1.16 mellon cp = "default";
435 1.16 mellon else if (flags & RTF_HOST)
436 1.16 mellon cp = routename(sin->sin_addr.s_addr);
437 1.16 mellon else if (mask)
438 1.16 mellon cp = netname(sin->sin_addr.s_addr,
439 1.19 mycroft ((struct sockaddr_in *)mask)->sin_addr.s_addr);
440 1.16 mellon else
441 1.16 mellon cp = netname(sin->sin_addr.s_addr, INADDR_ANY);
442 1.10 mycroft break;
443 1.1 cgd }
444 1.1 cgd
445 1.38 itojun #ifdef INET6
446 1.38 itojun case AF_INET6:
447 1.38 itojun {
448 1.44 itojun struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
449 1.44 itojun #ifdef KAME_SCOPEID
450 1.44 itojun struct in6_addr *in6 = &sa6->sin6_addr;
451 1.44 itojun
452 1.44 itojun /*
453 1.44 itojun * XXX: This is a special workaround for KAME kernels.
454 1.44 itojun * sin6_scope_id field of SA should be set in the future.
455 1.44 itojun */
456 1.44 itojun if (IN6_IS_ADDR_LINKLOCAL(in6) ||
457 1.44 itojun IN6_IS_ADDR_MC_LINKLOCAL(in6)) {
458 1.44 itojun /* XXX: override is ok? */
459 1.44 itojun sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)&in6->s6_addr[2]);
460 1.44 itojun *(u_short *)&in6->s6_addr[2] = 0;
461 1.44 itojun }
462 1.44 itojun #endif
463 1.38 itojun
464 1.38 itojun if (flags & RTF_HOST)
465 1.44 itojun cp = routename6(sa6);
466 1.38 itojun else if (mask) {
467 1.44 itojun cp = netname6(sa6,
468 1.44 itojun &((struct sockaddr_in6 *)mask)->sin6_addr);
469 1.38 itojun } else
470 1.44 itojun cp = netname6(sa6, NULL);
471 1.38 itojun break;
472 1.38 itojun }
473 1.38 itojun #endif
474 1.38 itojun
475 1.33 mrg #ifndef SMALL
476 1.21 christos case AF_APPLETALK:
477 1.21 christos case 0:
478 1.21 christos {
479 1.21 christos if (!(flags & RTF_HOST) && mask)
480 1.21 christos cp = atalk_print2(sa,mask,11);
481 1.21 christos else
482 1.21 christos cp = atalk_print(sa,11);
483 1.21 christos break;
484 1.21 christos }
485 1.1 cgd case AF_NS:
486 1.18 mellon cp = ns_print((struct sockaddr *)sa);
487 1.4 paul break;
488 1.33 mrg #endif
489 1.4 paul
490 1.4 paul case AF_LINK:
491 1.10 mycroft {
492 1.23 lukem struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
493 1.10 mycroft
494 1.10 mycroft if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
495 1.10 mycroft sdl->sdl_slen == 0)
496 1.29 mrg (void)snprintf(workbuf, sizeof workbuf, "link#%d",
497 1.29 mrg sdl->sdl_index);
498 1.10 mycroft else switch (sdl->sdl_type) {
499 1.20 thorpej case IFT_FDDI:
500 1.10 mycroft case IFT_ETHER:
501 1.51 matt case IFT_IEEE1394:
502 1.10 mycroft {
503 1.51 matt int i, alen = sdl->sdl_alen;
504 1.23 lukem u_char *lla = (u_char *)sdl->sdl_data +
505 1.10 mycroft sdl->sdl_nlen;
506 1.10 mycroft
507 1.51 matt if (sdl->sdl_type == IFT_IEEE1394 && alen > 8)
508 1.51 matt alen = 8;
509 1.51 matt
510 1.10 mycroft cplim = "";
511 1.51 matt for (i = 0; i < alen; i++, lla++) {
512 1.29 mrg /* XXX */
513 1.20 thorpej cp += sprintf(cp, "%s%02x", cplim, *lla);
514 1.10 mycroft cplim = ":";
515 1.10 mycroft }
516 1.10 mycroft cp = workbuf;
517 1.10 mycroft break;
518 1.10 mycroft }
519 1.10 mycroft default:
520 1.10 mycroft cp = link_ntoa(sdl);
521 1.10 mycroft break;
522 1.10 mycroft }
523 1.4 paul break;
524 1.10 mycroft }
525 1.1 cgd
526 1.1 cgd default:
527 1.1 cgd {
528 1.23 lukem u_char *s = (u_char *)sa->sa_data, *slim;
529 1.1 cgd
530 1.10 mycroft slim = sa->sa_len + (u_char *) sa;
531 1.1 cgd cplim = cp + sizeof(workbuf) - 6;
532 1.1 cgd cp += sprintf(cp, "(%d)", sa->sa_family);
533 1.7 cgd while (s < slim && cp < cplim) {
534 1.7 cgd cp += sprintf(cp, " %02x", *s++);
535 1.7 cgd if (s < slim)
536 1.10 mycroft cp += sprintf(cp, "%02x", *s++);
537 1.7 cgd }
538 1.1 cgd cp = workbuf;
539 1.1 cgd }
540 1.1 cgd }
541 1.1 cgd if (width < 0 )
542 1.1 cgd printf("%s ", cp);
543 1.1 cgd else {
544 1.1 cgd if (nflag)
545 1.1 cgd printf("%-*s ", width, cp);
546 1.1 cgd else
547 1.1 cgd printf("%-*.*s ", width, width, cp);
548 1.1 cgd }
549 1.1 cgd }
550 1.1 cgd
551 1.10 mycroft static void
552 1.50 is p_flags(f)
553 1.23 lukem int f;
554 1.4 paul {
555 1.4 paul char name[33], *flags;
556 1.23 lukem struct bits *p = bits;
557 1.4 paul
558 1.29 mrg for (flags = name; p->b_mask && flags - name < sizeof(name); p++)
559 1.4 paul if (p->b_mask & f)
560 1.4 paul *flags++ = p->b_val;
561 1.4 paul *flags = '\0';
562 1.50 is printf("%-6.6s ", name);
563 1.4 paul }
564 1.4 paul
565 1.37 chopps static struct sockaddr *sockcopy __P((struct sockaddr *,
566 1.37 chopps union sockaddr_union *));
567 1.35 chopps
568 1.35 chopps /*
569 1.35 chopps * copy a sockaddr into an allocated region, allocate at least sockaddr
570 1.35 chopps * bytes and zero unused
571 1.35 chopps */
572 1.35 chopps static struct sockaddr *
573 1.37 chopps sockcopy(sp, dp)
574 1.35 chopps struct sockaddr *sp;
575 1.37 chopps union sockaddr_union *dp;
576 1.35 chopps {
577 1.37 chopps int len;
578 1.35 chopps
579 1.37 chopps if (sp == 0 || sp->sa_len == 0)
580 1.37 chopps (void)memset(dp, 0, sizeof (*sp));
581 1.37 chopps else {
582 1.37 chopps len = (sp->sa_len >= sizeof (*sp)) ? sp->sa_len : sizeof (*sp);
583 1.37 chopps (void)memcpy(dp, sp, len);
584 1.37 chopps }
585 1.37 chopps return ((struct sockaddr *)dp);
586 1.35 chopps }
587 1.35 chopps
588 1.10 mycroft static void
589 1.4 paul p_rtentry(rt)
590 1.23 lukem struct rtentry *rt;
591 1.4 paul {
592 1.10 mycroft static struct ifnet ifnet, *lastif;
593 1.37 chopps union sockaddr_union addr_un, mask_un;
594 1.35 chopps struct sockaddr *addr, *mask;
595 1.41 itojun int af;
596 1.40 is
597 1.40 is if (Lflag && (rt->rt_flags & RTF_LLINFO))
598 1.40 is return;
599 1.4 paul
600 1.38 itojun memset(&addr_un, 0, sizeof(addr_un));
601 1.38 itojun memset(&mask_un, 0, sizeof(mask_un));
602 1.37 chopps addr = sockcopy(kgetsa(rt_key(rt)), &addr_un);
603 1.41 itojun af = addr->sa_family;
604 1.35 chopps if (rt_mask(rt))
605 1.37 chopps mask = sockcopy(kgetsa(rt_mask(rt)), &mask_un);
606 1.16 mellon else
607 1.38 itojun mask = sockcopy(NULL, &mask_un);
608 1.41 itojun p_sockaddr(addr, mask, rt->rt_flags, WID_DST(af));
609 1.41 itojun p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, WID_GW(af));
610 1.50 is p_flags(rt->rt_flags);
611 1.27 kml printf("%6d %8lu ", rt->rt_refcnt, rt->rt_use);
612 1.13 thorpej if (rt->rt_rmx.rmx_mtu)
613 1.27 kml printf("%6lu", rt->rt_rmx.rmx_mtu);
614 1.13 thorpej else
615 1.26 kml printf("%6s", "-");
616 1.26 kml putchar((rt->rt_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
617 1.10 mycroft if (rt->rt_ifp) {
618 1.10 mycroft if (rt->rt_ifp != lastif) {
619 1.10 mycroft kget(rt->rt_ifp, ifnet);
620 1.10 mycroft lastif = rt->rt_ifp;
621 1.10 mycroft }
622 1.15 thorpej printf(" %.16s%s", ifnet.if_xname,
623 1.10 mycroft rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
624 1.1 cgd }
625 1.10 mycroft putchar('\n');
626 1.34 kml if (vflag) {
627 1.34 kml printf("\texpire %10lu%c recvpipe %10ld%c "
628 1.34 kml "sendpipe %10ld%c\n",
629 1.34 kml rt->rt_rmx.rmx_expire,
630 1.34 kml (rt->rt_rmx.rmx_locks & RTV_EXPIRE) ? 'L' : ' ',
631 1.34 kml rt->rt_rmx.rmx_recvpipe,
632 1.34 kml (rt->rt_rmx.rmx_locks & RTV_RPIPE) ? 'L' : ' ',
633 1.34 kml rt->rt_rmx.rmx_sendpipe,
634 1.34 kml (rt->rt_rmx.rmx_locks & RTV_SPIPE) ? 'L' : ' ');
635 1.34 kml printf("\tssthresh %10lu%c rtt %10ld%c "
636 1.34 kml "rttvar %10ld%c\n",
637 1.34 kml rt->rt_rmx.rmx_ssthresh,
638 1.34 kml (rt->rt_rmx.rmx_locks & RTV_SSTHRESH) ? 'L' : ' ',
639 1.34 kml rt->rt_rmx.rmx_rtt,
640 1.34 kml (rt->rt_rmx.rmx_locks & RTV_RTT) ? 'L' : ' ',
641 1.34 kml rt->rt_rmx.rmx_rttvar,
642 1.34 kml (rt->rt_rmx.rmx_locks & RTV_RTTVAR) ? 'L' : ' ');
643 1.54 enami printf("\thopcount %10lu%c\n",
644 1.54 enami rt->rt_rmx.rmx_hopcount,
645 1.54 enami (rt->rt_rmx.rmx_locks & RTV_HOPCOUNT) ? 'L' : ' ');
646 1.54 enami }
647 1.1 cgd }
648 1.1 cgd
649 1.1 cgd char *
650 1.1 cgd routename(in)
651 1.12 mycroft u_int32_t in;
652 1.1 cgd {
653 1.23 lukem char *cp;
654 1.1 cgd static char line[MAXHOSTNAMELEN + 1];
655 1.1 cgd struct hostent *hp;
656 1.1 cgd static char domain[MAXHOSTNAMELEN + 1];
657 1.1 cgd static int first = 1;
658 1.1 cgd
659 1.1 cgd if (first) {
660 1.1 cgd first = 0;
661 1.28 mrg if (gethostname(domain, MAXHOSTNAMELEN) == 0) {
662 1.28 mrg domain[sizeof(domain) - 1] = '\0';
663 1.28 mrg if ((cp = strchr(domain, '.')))
664 1.28 mrg (void)strcpy(domain, cp + 1);
665 1.28 mrg else
666 1.28 mrg domain[0] = 0;
667 1.28 mrg } else
668 1.1 cgd domain[0] = 0;
669 1.1 cgd }
670 1.1 cgd cp = 0;
671 1.1 cgd if (!nflag) {
672 1.1 cgd hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
673 1.1 cgd AF_INET);
674 1.1 cgd if (hp) {
675 1.23 lukem if ((cp = strchr(hp->h_name, '.')) &&
676 1.1 cgd !strcmp(cp + 1, domain))
677 1.1 cgd *cp = 0;
678 1.1 cgd cp = hp->h_name;
679 1.1 cgd }
680 1.1 cgd }
681 1.29 mrg if (cp) {
682 1.1 cgd strncpy(line, cp, sizeof(line) - 1);
683 1.29 mrg line[sizeof(line) - 1] = '\0';
684 1.29 mrg } else {
685 1.1 cgd #define C(x) ((x) & 0xff)
686 1.10 mycroft in = ntohl(in);
687 1.29 mrg snprintf(line, sizeof line, "%u.%u.%u.%u",
688 1.10 mycroft C(in >> 24), C(in >> 16), C(in >> 8), C(in));
689 1.1 cgd }
690 1.1 cgd return (line);
691 1.1 cgd }
692 1.1 cgd
693 1.16 mellon static u_long
694 1.16 mellon forgemask(a)
695 1.16 mellon u_long a;
696 1.16 mellon {
697 1.16 mellon u_long m;
698 1.16 mellon
699 1.16 mellon if (IN_CLASSA(a))
700 1.16 mellon m = IN_CLASSA_NET;
701 1.16 mellon else if (IN_CLASSB(a))
702 1.16 mellon m = IN_CLASSB_NET;
703 1.16 mellon else
704 1.16 mellon m = IN_CLASSC_NET;
705 1.16 mellon return (m);
706 1.16 mellon }
707 1.16 mellon
708 1.16 mellon static void
709 1.29 mrg domask(dst, dlen, addr, mask)
710 1.16 mellon char *dst;
711 1.29 mrg size_t dlen;
712 1.16 mellon u_long addr, mask;
713 1.16 mellon {
714 1.23 lukem int b, i;
715 1.16 mellon
716 1.16 mellon if (!mask || (forgemask(addr) == mask)) {
717 1.16 mellon *dst = '\0';
718 1.16 mellon return;
719 1.16 mellon }
720 1.16 mellon i = 0;
721 1.16 mellon for (b = 0; b < 32; b++)
722 1.16 mellon if (mask & (1 << b)) {
723 1.23 lukem int bb;
724 1.16 mellon
725 1.16 mellon i = b;
726 1.16 mellon for (bb = b+1; bb < 32; bb++)
727 1.16 mellon if (!(mask & (1 << bb))) {
728 1.16 mellon i = -1; /* noncontig */
729 1.16 mellon break;
730 1.16 mellon }
731 1.16 mellon break;
732 1.16 mellon }
733 1.16 mellon if (i == -1)
734 1.29 mrg (void)snprintf(dst, dlen, "&0x%lx", mask);
735 1.16 mellon else
736 1.29 mrg (void)snprintf(dst, dlen, "/%d", 32-i);
737 1.16 mellon }
738 1.16 mellon
739 1.1 cgd /*
740 1.1 cgd * Return the name of the network whose address is given.
741 1.1 cgd * The address is assumed to be that of a net or subnet, not a host.
742 1.1 cgd */
743 1.1 cgd char *
744 1.1 cgd netname(in, mask)
745 1.12 mycroft u_int32_t in, mask;
746 1.1 cgd {
747 1.1 cgd char *cp = 0;
748 1.16 mellon static char line[MAXHOSTNAMELEN + 4];
749 1.1 cgd struct netent *np = 0;
750 1.16 mellon u_int32_t net, omask;
751 1.23 lukem u_int32_t i;
752 1.1 cgd int subnetshift;
753 1.1 cgd
754 1.16 mellon i = ntohl(in);
755 1.19 mycroft omask = mask = ntohl(mask);
756 1.16 mellon if (!nflag && i != INADDR_ANY) {
757 1.12 mycroft if (mask == INADDR_ANY) {
758 1.16 mellon switch (mask = forgemask(i)) {
759 1.16 mellon case IN_CLASSA_NET:
760 1.1 cgd subnetshift = 8;
761 1.16 mellon break;
762 1.16 mellon case IN_CLASSB_NET:
763 1.1 cgd subnetshift = 8;
764 1.16 mellon break;
765 1.16 mellon case IN_CLASSC_NET:
766 1.1 cgd subnetshift = 4;
767 1.16 mellon break;
768 1.16 mellon default:
769 1.16 mellon abort();
770 1.1 cgd }
771 1.1 cgd /*
772 1.1 cgd * If there are more bits than the standard mask
773 1.1 cgd * would suggest, subnets must be in use.
774 1.1 cgd * Guess at the subnet mask, assuming reasonable
775 1.1 cgd * width subnet fields.
776 1.1 cgd */
777 1.16 mellon while (i &~ mask)
778 1.1 cgd mask = (long)mask >> subnetshift;
779 1.1 cgd }
780 1.16 mellon net = i & mask;
781 1.32 pk /*
782 1.32 pk * Note: shift the hosts bits out in octet units, since
783 1.32 pk * not all versions of getnetbyaddr() do this for us (e.g.
784 1.32 pk * the current `etc/networks' parser).
785 1.32 pk */
786 1.32 pk while ((mask & 0xff) == 0)
787 1.32 pk mask >>= 8, net >>= 8;
788 1.1 cgd np = getnetbyaddr(net, AF_INET);
789 1.1 cgd if (np)
790 1.1 cgd cp = np->n_name;
791 1.10 mycroft }
792 1.1 cgd if (cp)
793 1.1 cgd strncpy(line, cp, sizeof(line) - 1);
794 1.16 mellon else if ((i & 0xffffff) == 0)
795 1.29 mrg (void)snprintf(line, sizeof line, "%u", C(i >> 24));
796 1.16 mellon else if ((i & 0xffff) == 0)
797 1.29 mrg (void)snprintf(line, sizeof line, "%u.%u", C(i >> 24)
798 1.29 mrg , C(i >> 16));
799 1.16 mellon else if ((i & 0xff) == 0)
800 1.29 mrg (void)snprintf(line, sizeof line, "%u.%u.%u", C(i >> 24),
801 1.29 mrg C(i >> 16), C(i >> 8));
802 1.1 cgd else
803 1.29 mrg (void)snprintf(line, sizeof line, "%u.%u.%u.%u", C(i >> 24),
804 1.16 mellon C(i >> 16), C(i >> 8), C(i));
805 1.29 mrg domask(line + strlen(line), sizeof(line) - strlen(line), i, omask);
806 1.1 cgd return (line);
807 1.1 cgd }
808 1.38 itojun
809 1.38 itojun #ifdef INET6
810 1.38 itojun char *
811 1.44 itojun netname6(sa6, mask)
812 1.44 itojun struct sockaddr_in6 *sa6;
813 1.38 itojun struct in6_addr *mask;
814 1.38 itojun {
815 1.44 itojun static char line[NI_MAXHOST];
816 1.47 itojun u_char *p, *q;
817 1.38 itojun u_char *lim;
818 1.38 itojun int masklen, final = 0, illegal = 0;
819 1.44 itojun #ifdef KAME_SCOPEID
820 1.44 itojun int flag = NI_WITHSCOPEID;
821 1.44 itojun #else
822 1.44 itojun int flag = 0;
823 1.44 itojun #endif
824 1.44 itojun int error;
825 1.47 itojun struct sockaddr_in6 sin6;
826 1.38 itojun
827 1.47 itojun sin6 = *sa6;
828 1.44 itojun if (mask) {
829 1.44 itojun masklen = 0;
830 1.49 itojun lim = (u_char *)(mask + 1);
831 1.47 itojun for (p = (u_char *)mask, q = (u_char *)&sin6.sin6_addr;
832 1.47 itojun p < lim;
833 1.47 itojun p++, q++) {
834 1.44 itojun if (final && *p) {
835 1.44 itojun illegal++;
836 1.47 itojun *q = 0;
837 1.44 itojun continue;
838 1.44 itojun }
839 1.38 itojun
840 1.44 itojun switch (*p & 0xff) {
841 1.44 itojun case 0xff:
842 1.44 itojun masklen += 8;
843 1.44 itojun break;
844 1.44 itojun case 0xfe:
845 1.44 itojun masklen += 7;
846 1.44 itojun final++;
847 1.44 itojun break;
848 1.44 itojun case 0xfc:
849 1.44 itojun masklen += 6;
850 1.44 itojun final++;
851 1.44 itojun break;
852 1.44 itojun case 0xf8:
853 1.44 itojun masklen += 5;
854 1.44 itojun final++;
855 1.44 itojun break;
856 1.44 itojun case 0xf0:
857 1.44 itojun masklen += 4;
858 1.44 itojun final++;
859 1.44 itojun break;
860 1.44 itojun case 0xe0:
861 1.44 itojun masklen += 3;
862 1.44 itojun final++;
863 1.44 itojun break;
864 1.44 itojun case 0xc0:
865 1.44 itojun masklen += 2;
866 1.44 itojun final++;
867 1.44 itojun break;
868 1.44 itojun case 0x80:
869 1.44 itojun masklen += 1;
870 1.44 itojun final++;
871 1.44 itojun break;
872 1.44 itojun case 0x00:
873 1.44 itojun final++;
874 1.44 itojun break;
875 1.44 itojun default:
876 1.44 itojun final++;
877 1.44 itojun illegal++;
878 1.44 itojun break;
879 1.44 itojun }
880 1.47 itojun
881 1.47 itojun if (!illegal)
882 1.47 itojun *q &= *p;
883 1.47 itojun else
884 1.47 itojun *q = 0;
885 1.38 itojun }
886 1.49 itojun } else
887 1.44 itojun masklen = 128;
888 1.38 itojun
889 1.48 itojun if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr))
890 1.38 itojun return("default");
891 1.38 itojun
892 1.44 itojun if (nflag)
893 1.44 itojun flag |= NI_NUMERICHOST;
894 1.47 itojun error = getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
895 1.44 itojun line, sizeof(line), NULL, 0, flag);
896 1.44 itojun if (error)
897 1.44 itojun strcpy(line, "invalid");
898 1.44 itojun
899 1.44 itojun if (nflag)
900 1.44 itojun sprintf(&line[strlen(line)], "/%d", masklen);
901 1.44 itojun
902 1.38 itojun return line;
903 1.38 itojun }
904 1.38 itojun
905 1.38 itojun char *
906 1.44 itojun routename6(sa6)
907 1.44 itojun struct sockaddr_in6 *sa6;
908 1.38 itojun {
909 1.44 itojun static char line[NI_MAXHOST];
910 1.44 itojun #ifdef KAME_SCOPEID
911 1.44 itojun int flag = NI_WITHSCOPEID;
912 1.44 itojun #else
913 1.44 itojun int flag = 0;
914 1.44 itojun #endif
915 1.44 itojun /* use local variable for safety */
916 1.44 itojun struct sockaddr_in6 sa6_local;
917 1.44 itojun int error;
918 1.44 itojun
919 1.44 itojun memset(&sa6_local, 0, sizeof(sa6_local));
920 1.44 itojun sa6_local.sin6_family = AF_INET6;
921 1.44 itojun sa6_local.sin6_len = sizeof(struct sockaddr_in6);
922 1.44 itojun sa6_local.sin6_addr = sa6->sin6_addr;
923 1.44 itojun sa6_local.sin6_scope_id = sa6->sin6_scope_id;
924 1.44 itojun
925 1.44 itojun if (nflag)
926 1.44 itojun flag |= NI_NUMERICHOST;
927 1.44 itojun
928 1.44 itojun error = getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len,
929 1.44 itojun line, sizeof(line), NULL, 0, flag);
930 1.44 itojun if (error)
931 1.44 itojun strcpy(line, "invalid");
932 1.44 itojun
933 1.38 itojun return line;
934 1.38 itojun }
935 1.38 itojun #endif /*INET6*/
936 1.1 cgd
937 1.1 cgd /*
938 1.1 cgd * Print routing statistics
939 1.1 cgd */
940 1.10 mycroft void
941 1.1 cgd rt_stats(off)
942 1.9 cgd u_long off;
943 1.1 cgd {
944 1.1 cgd struct rtstat rtstat;
945 1.1 cgd
946 1.1 cgd if (off == 0) {
947 1.1 cgd printf("rtstat: symbol not in namelist\n");
948 1.1 cgd return;
949 1.1 cgd }
950 1.10 mycroft kread(off, (char *)&rtstat, sizeof (rtstat));
951 1.1 cgd printf("routing:\n");
952 1.53 itojun printf("\t%llu bad routing redirect%s\n",
953 1.53 itojun (unsigned long long)rtstat.rts_badredirect,
954 1.53 itojun plural(rtstat.rts_badredirect));
955 1.53 itojun printf("\t%llu dynamically created route%s\n",
956 1.53 itojun (unsigned long long)rtstat.rts_dynamic,
957 1.53 itojun plural(rtstat.rts_dynamic));
958 1.53 itojun printf("\t%llu new gateway%s due to redirects\n",
959 1.53 itojun (unsigned long long)rtstat.rts_newgateway,
960 1.53 itojun plural(rtstat.rts_newgateway));
961 1.53 itojun printf("\t%llu destination%s found unreachable\n",
962 1.53 itojun (unsigned long long)rtstat.rts_unreach,
963 1.53 itojun plural(rtstat.rts_unreach));
964 1.53 itojun printf("\t%llu use%s of a wildcard route\n",
965 1.53 itojun (unsigned long long)rtstat.rts_wildcard,
966 1.53 itojun plural(rtstat.rts_wildcard));
967 1.1 cgd }
968 1.1 cgd short ns_nullh[] = {0,0,0};
969 1.1 cgd short ns_bh[] = {-1,-1,-1};
970 1.1 cgd
971 1.1 cgd char *
972 1.10 mycroft ns_print(sa)
973 1.17 mellon struct sockaddr *sa;
974 1.1 cgd {
975 1.23 lukem struct sockaddr_ns *sns = (struct sockaddr_ns*)sa;
976 1.1 cgd struct ns_addr work;
977 1.29 mrg union {
978 1.29 mrg union ns_net net_e;
979 1.29 mrg u_long long_e;
980 1.29 mrg } net;
981 1.1 cgd u_short port;
982 1.1 cgd static char mybuf[50], cport[10], chost[25];
983 1.1 cgd char *host = "";
984 1.23 lukem char *p;
985 1.23 lukem u_char *q;
986 1.1 cgd
987 1.1 cgd work = sns->sns_addr;
988 1.1 cgd port = ntohs(work.x_port);
989 1.1 cgd work.x_port = 0;
990 1.1 cgd net.net_e = work.x_net;
991 1.1 cgd if (ns_nullhost(work) && net.long_e == 0) {
992 1.1 cgd if (port ) {
993 1.29 mrg (void)snprintf(mybuf, sizeof mybuf, "*.%xH", port);
994 1.1 cgd upHex(mybuf);
995 1.1 cgd } else
996 1.29 mrg (void)snprintf(mybuf, sizeof mybuf, "*.*");
997 1.1 cgd return (mybuf);
998 1.1 cgd }
999 1.1 cgd
1000 1.23 lukem if (memcmp(ns_bh, work.x_host.c_host, 6) == 0) {
1001 1.1 cgd host = "any";
1002 1.23 lukem } else if (memcmp(ns_nullh, work.x_host.c_host, 6) == 0) {
1003 1.1 cgd host = "*";
1004 1.1 cgd } else {
1005 1.1 cgd q = work.x_host.c_host;
1006 1.29 mrg (void)snprintf(chost, sizeof chost, "%02x%02x%02x%02x%02x%02xH",
1007 1.1 cgd q[0], q[1], q[2], q[3], q[4], q[5]);
1008 1.10 mycroft for (p = chost; *p == '0' && p < chost + 12; p++)
1009 1.10 mycroft continue;
1010 1.1 cgd host = p;
1011 1.1 cgd }
1012 1.1 cgd if (port)
1013 1.29 mrg (void)snprintf(cport, sizeof cport, ".%xH", htons(port));
1014 1.1 cgd else
1015 1.1 cgd *cport = 0;
1016 1.1 cgd
1017 1.29 mrg (void)snprintf(mybuf, sizeof mybuf, "%xH.%s%s", (int)ntohl(net.long_e),
1018 1.29 mrg host, cport);
1019 1.1 cgd upHex(mybuf);
1020 1.29 mrg return (mybuf);
1021 1.1 cgd }
1022 1.1 cgd
1023 1.1 cgd char *
1024 1.10 mycroft ns_phost(sa)
1025 1.10 mycroft struct sockaddr *sa;
1026 1.1 cgd {
1027 1.23 lukem struct sockaddr_ns *sns = (struct sockaddr_ns *)sa;
1028 1.1 cgd struct sockaddr_ns work;
1029 1.1 cgd static union ns_net ns_zeronet;
1030 1.1 cgd char *p;
1031 1.10 mycroft
1032 1.1 cgd work = *sns;
1033 1.1 cgd work.sns_addr.x_port = 0;
1034 1.1 cgd work.sns_addr.x_net = ns_zeronet;
1035 1.1 cgd
1036 1.10 mycroft p = ns_print((struct sockaddr *)&work);
1037 1.29 mrg if (strncmp("0H.", p, 3) == 0)
1038 1.29 mrg p += 3;
1039 1.29 mrg return (p);
1040 1.1 cgd }
1041 1.10 mycroft
1042 1.10 mycroft void
1043 1.1 cgd upHex(p0)
1044 1.10 mycroft char *p0;
1045 1.1 cgd {
1046 1.23 lukem char *p = p0;
1047 1.1 cgd
1048 1.29 mrg for (; *p; p++)
1049 1.29 mrg switch (*p) {
1050 1.29 mrg case 'a':
1051 1.29 mrg case 'b':
1052 1.29 mrg case 'c':
1053 1.29 mrg case 'd':
1054 1.29 mrg case 'e':
1055 1.29 mrg case 'f':
1056 1.29 mrg *p += ('A' - 'a');
1057 1.29 mrg }
1058 1.1 cgd }
1059