route.c revision 1.14 1 1.14 thorpej /* $NetBSD: route.c,v 1.14 1995/10/03 21:42:47 thorpej 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.1 cgd #ifndef lint
37 1.14 thorpej #if 0
38 1.14 thorpej static char sccsid[] = "from: @(#)route.c 8.3 (Berkeley) 3/9/94";
39 1.14 thorpej #else
40 1.14 thorpej static char *rcsid = "$NetBSD: route.c,v 1.14 1995/10/03 21:42:47 thorpej Exp $";
41 1.14 thorpej #endif
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #include <sys/param.h>
45 1.10 mycroft #include <sys/protosw.h>
46 1.1 cgd #include <sys/socket.h>
47 1.1 cgd #include <sys/mbuf.h>
48 1.1 cgd
49 1.1 cgd #include <net/if.h>
50 1.10 mycroft #include <net/if_dl.h>
51 1.10 mycroft #include <net/if_types.h>
52 1.11 jtc #define _KERNEL
53 1.1 cgd #include <net/route.h>
54 1.11 jtc #undef _KERNEL
55 1.1 cgd #include <netinet/in.h>
56 1.1 cgd
57 1.1 cgd #include <netns/ns.h>
58 1.1 cgd
59 1.10 mycroft #include <sys/sysctl.h>
60 1.4 paul
61 1.1 cgd #include <netdb.h>
62 1.1 cgd #include <stdio.h>
63 1.10 mycroft #include <stdlib.h>
64 1.1 cgd #include <string.h>
65 1.10 mycroft #include <unistd.h>
66 1.10 mycroft #include "netstat.h"
67 1.1 cgd
68 1.10 mycroft #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d)))
69 1.1 cgd
70 1.1 cgd /*
71 1.1 cgd * Definitions for showing gateway flags.
72 1.1 cgd */
73 1.1 cgd struct bits {
74 1.1 cgd short b_mask;
75 1.1 cgd char b_val;
76 1.1 cgd } bits[] = {
77 1.1 cgd { RTF_UP, 'U' },
78 1.1 cgd { RTF_GATEWAY, 'G' },
79 1.1 cgd { RTF_HOST, 'H' },
80 1.10 mycroft { RTF_REJECT, 'R' },
81 1.1 cgd { RTF_DYNAMIC, 'D' },
82 1.1 cgd { RTF_MODIFIED, 'M' },
83 1.10 mycroft { RTF_DONE, 'd' }, /* Completed -- for routing messages only */
84 1.10 mycroft { RTF_MASK, 'm' }, /* Mask Present -- for routing messages only */
85 1.1 cgd { RTF_CLONING, 'C' },
86 1.1 cgd { RTF_XRESOLVE, 'X' },
87 1.1 cgd { RTF_LLINFO, 'L' },
88 1.10 mycroft { RTF_STATIC, 'S' },
89 1.10 mycroft { RTF_PROTO1, '1' },
90 1.10 mycroft { RTF_PROTO2, '2' },
91 1.1 cgd { 0 }
92 1.1 cgd };
93 1.1 cgd
94 1.10 mycroft static union {
95 1.10 mycroft struct sockaddr u_sa;
96 1.10 mycroft u_short u_data[128];
97 1.10 mycroft } pt_u;
98 1.10 mycroft
99 1.10 mycroft int do_rtent = 0;
100 1.10 mycroft struct rtentry rtentry;
101 1.10 mycroft struct radix_node rnode;
102 1.10 mycroft struct radix_mask rmask;
103 1.10 mycroft
104 1.10 mycroft int NewTree = 0;
105 1.10 mycroft
106 1.10 mycroft static struct sockaddr *kgetsa __P((struct sockaddr *));
107 1.10 mycroft static void p_tree __P((struct radix_node *));
108 1.10 mycroft static void p_rtnode __P(());
109 1.10 mycroft static void ntreestuff __P(());
110 1.10 mycroft static void np_rtentry __P((struct rt_msghdr *));
111 1.10 mycroft static void p_sockaddr __P((struct sockaddr *, int, int));
112 1.10 mycroft static void p_flags __P((int, char *));
113 1.10 mycroft static void p_rtentry __P((struct rtentry *));
114 1.3 cgd
115 1.3 cgd /*
116 1.1 cgd * Print routing tables.
117 1.1 cgd */
118 1.10 mycroft void
119 1.10 mycroft routepr(rtree)
120 1.10 mycroft u_long rtree;
121 1.1 cgd {
122 1.10 mycroft struct radix_node_head *rnh, head;
123 1.10 mycroft int i;
124 1.1 cgd
125 1.1 cgd printf("Routing tables\n");
126 1.10 mycroft
127 1.10 mycroft if (Aflag == 0 && NewTree)
128 1.10 mycroft ntreestuff();
129 1.10 mycroft else {
130 1.10 mycroft if (rtree == 0) {
131 1.10 mycroft printf("rt_tables: symbol not in namelist\n");
132 1.10 mycroft return;
133 1.10 mycroft }
134 1.10 mycroft
135 1.10 mycroft kget(rtree, rt_tables);
136 1.10 mycroft for (i = 0; i <= AF_MAX; i++) {
137 1.10 mycroft if ((rnh = rt_tables[i]) == 0)
138 1.10 mycroft continue;
139 1.10 mycroft kget(rnh, head);
140 1.10 mycroft if (i == AF_UNSPEC) {
141 1.10 mycroft if (Aflag && af == 0) {
142 1.10 mycroft printf("Netmasks:\n");
143 1.10 mycroft p_tree(head.rnh_treetop);
144 1.10 mycroft }
145 1.10 mycroft } else if (af == AF_UNSPEC || af == i) {
146 1.10 mycroft pr_family(i);
147 1.10 mycroft do_rtent = 1;
148 1.10 mycroft pr_rthdr();
149 1.10 mycroft p_tree(head.rnh_treetop);
150 1.10 mycroft }
151 1.1 cgd }
152 1.1 cgd }
153 1.1 cgd }
154 1.1 cgd
155 1.10 mycroft /*
156 1.10 mycroft * Print address family header before a section of the routing table.
157 1.10 mycroft */
158 1.10 mycroft void
159 1.10 mycroft pr_family(af)
160 1.10 mycroft int af;
161 1.4 paul {
162 1.10 mycroft char *afname;
163 1.4 paul
164 1.10 mycroft switch (af) {
165 1.4 paul case AF_INET:
166 1.10 mycroft afname = "Internet";
167 1.10 mycroft break;
168 1.4 paul case AF_NS:
169 1.10 mycroft afname = "XNS";
170 1.10 mycroft break;
171 1.4 paul case AF_ISO:
172 1.10 mycroft afname = "ISO";
173 1.4 paul break;
174 1.10 mycroft case AF_CCITT:
175 1.10 mycroft afname = "X.25";
176 1.4 paul break;
177 1.4 paul default:
178 1.10 mycroft afname = NULL;
179 1.10 mycroft break;
180 1.4 paul }
181 1.10 mycroft if (afname)
182 1.10 mycroft printf("\n%s:\n", afname);
183 1.10 mycroft else
184 1.10 mycroft printf("\nProtocol Family %d:\n", af);
185 1.4 paul }
186 1.4 paul
187 1.10 mycroft /* column widths; each followed by one space */
188 1.10 mycroft #define WID_DST 16 /* width of destination column */
189 1.10 mycroft #define WID_GW 18 /* width of gateway column */
190 1.4 paul
191 1.10 mycroft /*
192 1.10 mycroft * Print header for routing table columns.
193 1.10 mycroft */
194 1.10 mycroft void
195 1.10 mycroft pr_rthdr()
196 1.1 cgd {
197 1.1 cgd
198 1.10 mycroft if (Aflag)
199 1.10 mycroft printf("%-8.8s ","Address");
200 1.13 thorpej printf("%-*.*s %-*.*s %-6.6s %6.6s%8.8s %6.6s %s\n",
201 1.10 mycroft WID_DST, WID_DST, "Destination",
202 1.10 mycroft WID_GW, WID_GW, "Gateway",
203 1.13 thorpej "Flags", "Refs", "Use", "Mtu", "Interface");
204 1.1 cgd }
205 1.1 cgd
206 1.10 mycroft static struct sockaddr *
207 1.1 cgd kgetsa(dst)
208 1.10 mycroft register struct sockaddr *dst;
209 1.1 cgd {
210 1.10 mycroft
211 1.1 cgd kget(dst, pt_u.u_sa);
212 1.10 mycroft if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa))
213 1.10 mycroft kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len);
214 1.1 cgd return (&pt_u.u_sa);
215 1.1 cgd }
216 1.1 cgd
217 1.10 mycroft static void
218 1.1 cgd p_tree(rn)
219 1.10 mycroft struct radix_node *rn;
220 1.1 cgd {
221 1.1 cgd
222 1.1 cgd again:
223 1.1 cgd kget(rn, rnode);
224 1.1 cgd if (rnode.rn_b < 0) {
225 1.1 cgd if (Aflag)
226 1.1 cgd printf("%-8.8x ", rn);
227 1.10 mycroft if (rnode.rn_flags & RNF_ROOT) {
228 1.10 mycroft if (Aflag)
229 1.10 mycroft printf("(root node)%s",
230 1.1 cgd rnode.rn_dupedkey ? " =>\n" : "\n");
231 1.10 mycroft } else if (do_rtent) {
232 1.1 cgd kget(rn, rtentry);
233 1.1 cgd p_rtentry(&rtentry);
234 1.1 cgd if (Aflag)
235 1.1 cgd p_rtnode();
236 1.1 cgd } else {
237 1.1 cgd p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key),
238 1.10 mycroft 0, 44);
239 1.1 cgd putchar('\n');
240 1.1 cgd }
241 1.1 cgd if (rn = rnode.rn_dupedkey)
242 1.1 cgd goto again;
243 1.1 cgd } else {
244 1.1 cgd if (Aflag && do_rtent) {
245 1.1 cgd printf("%-8.8x ", rn);
246 1.1 cgd p_rtnode();
247 1.1 cgd }
248 1.1 cgd rn = rnode.rn_r;
249 1.1 cgd p_tree(rnode.rn_l);
250 1.1 cgd p_tree(rn);
251 1.1 cgd }
252 1.1 cgd }
253 1.1 cgd
254 1.10 mycroft char nbuf[20];
255 1.10 mycroft
256 1.10 mycroft static void
257 1.1 cgd p_rtnode()
258 1.1 cgd {
259 1.10 mycroft struct radix_mask *rm = rnode.rn_mklist;
260 1.1 cgd
261 1.1 cgd if (rnode.rn_b < 0) {
262 1.1 cgd if (rnode.rn_mask) {
263 1.1 cgd printf("\t mask ");
264 1.1 cgd p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask),
265 1.1 cgd 0, -1);
266 1.1 cgd } else if (rm == 0)
267 1.1 cgd return;
268 1.1 cgd } else {
269 1.1 cgd sprintf(nbuf, "(%d)", rnode.rn_b);
270 1.1 cgd printf("%6.6s %8.8x : %8.8x", nbuf, rnode.rn_l, rnode.rn_r);
271 1.1 cgd }
272 1.1 cgd while (rm) {
273 1.1 cgd kget(rm, rmask);
274 1.1 cgd sprintf(nbuf, " %d refs, ", rmask.rm_refs);
275 1.1 cgd printf(" mk = %8.8x {(%d),%s",
276 1.1 cgd rm, -1 - rmask.rm_b, rmask.rm_refs ? nbuf : " ");
277 1.1 cgd p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask), 0, -1);
278 1.1 cgd putchar('}');
279 1.1 cgd if (rm = rmask.rm_mklist)
280 1.1 cgd printf(" ->");
281 1.1 cgd }
282 1.1 cgd putchar('\n');
283 1.1 cgd }
284 1.1 cgd
285 1.10 mycroft static void
286 1.1 cgd ntreestuff()
287 1.1 cgd {
288 1.10 mycroft size_t needed;
289 1.10 mycroft int mib[6];
290 1.1 cgd char *buf, *next, *lim;
291 1.1 cgd register struct rt_msghdr *rtm;
292 1.1 cgd
293 1.10 mycroft mib[0] = CTL_NET;
294 1.10 mycroft mib[1] = PF_ROUTE;
295 1.10 mycroft mib[2] = 0;
296 1.10 mycroft mib[3] = 0;
297 1.10 mycroft mib[4] = NET_RT_DUMP;
298 1.10 mycroft mib[5] = 0;
299 1.10 mycroft if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
300 1.10 mycroft { perror("route-sysctl-estimate"); exit(1);}
301 1.1 cgd if ((buf = malloc(needed)) == 0)
302 1.1 cgd { printf("out of space\n"); exit(1);}
303 1.10 mycroft if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
304 1.10 mycroft { perror("sysctl of routing table"); exit(1);}
305 1.1 cgd lim = buf + needed;
306 1.1 cgd for (next = buf; next < lim; next += rtm->rtm_msglen) {
307 1.1 cgd rtm = (struct rt_msghdr *)next;
308 1.1 cgd np_rtentry(rtm);
309 1.1 cgd }
310 1.1 cgd }
311 1.1 cgd
312 1.10 mycroft static void
313 1.1 cgd np_rtentry(rtm)
314 1.10 mycroft register struct rt_msghdr *rtm;
315 1.1 cgd {
316 1.1 cgd register struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
317 1.10 mycroft #ifdef notdef
318 1.10 mycroft static int masks_done, banner_printed;
319 1.10 mycroft #endif
320 1.10 mycroft static int old_af;
321 1.1 cgd int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST;
322 1.1 cgd
323 1.1 cgd #ifdef notdef
324 1.1 cgd /* for the moment, netmasks are skipped over */
325 1.1 cgd if (!banner_printed) {
326 1.1 cgd printf("Netmasks:\n");
327 1.1 cgd banner_printed = 1;
328 1.1 cgd }
329 1.1 cgd if (masks_done == 0) {
330 1.1 cgd if (rtm->rtm_addrs != RTA_DST ) {
331 1.1 cgd masks_done = 1;
332 1.1 cgd af = sa->sa_family;
333 1.1 cgd }
334 1.1 cgd } else
335 1.1 cgd #endif
336 1.1 cgd af = sa->sa_family;
337 1.1 cgd if (af != old_af) {
338 1.10 mycroft pr_family(af);
339 1.1 cgd old_af = af;
340 1.1 cgd }
341 1.1 cgd if (rtm->rtm_addrs == RTA_DST)
342 1.1 cgd p_sockaddr(sa, 0, 36);
343 1.1 cgd else {
344 1.1 cgd p_sockaddr(sa, rtm->rtm_flags, 16);
345 1.1 cgd if (sa->sa_len == 0)
346 1.1 cgd sa->sa_len = sizeof(long);
347 1.1 cgd sa = (struct sockaddr *)(sa->sa_len + (char *)sa);
348 1.1 cgd p_sockaddr(sa, 0, 18);
349 1.1 cgd }
350 1.1 cgd p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
351 1.1 cgd putchar('\n');
352 1.1 cgd }
353 1.1 cgd
354 1.10 mycroft static void
355 1.1 cgd p_sockaddr(sa, flags, width)
356 1.10 mycroft struct sockaddr *sa;
357 1.10 mycroft int flags, width;
358 1.1 cgd {
359 1.10 mycroft char workbuf[128], *cplim;
360 1.10 mycroft register char *cp = workbuf;
361 1.1 cgd
362 1.1 cgd switch(sa->sa_family) {
363 1.1 cgd case AF_INET:
364 1.1 cgd {
365 1.1 cgd register struct sockaddr_in *sin = (struct sockaddr_in *)sa;
366 1.1 cgd
367 1.1 cgd cp = (sin->sin_addr.s_addr == 0) ? "default" :
368 1.1 cgd ((flags & RTF_HOST) ?
369 1.10 mycroft routename(sin->sin_addr.s_addr) :
370 1.12 mycroft netname(sin->sin_addr.s_addr, INADDR_ANY));
371 1.10 mycroft break;
372 1.1 cgd }
373 1.1 cgd
374 1.1 cgd case AF_NS:
375 1.10 mycroft cp = ns_print(sa);
376 1.4 paul break;
377 1.4 paul
378 1.4 paul case AF_LINK:
379 1.10 mycroft {
380 1.10 mycroft register struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
381 1.10 mycroft
382 1.10 mycroft if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
383 1.10 mycroft sdl->sdl_slen == 0)
384 1.10 mycroft (void) sprintf(workbuf, "link#%d", sdl->sdl_index);
385 1.10 mycroft else switch (sdl->sdl_type) {
386 1.10 mycroft case IFT_ETHER:
387 1.10 mycroft {
388 1.10 mycroft register int i;
389 1.10 mycroft register u_char *lla = (u_char *)sdl->sdl_data +
390 1.10 mycroft sdl->sdl_nlen;
391 1.10 mycroft
392 1.10 mycroft cplim = "";
393 1.10 mycroft for (i = 0; i < sdl->sdl_alen; i++, lla++) {
394 1.10 mycroft cp += sprintf(cp, "%s%x", cplim, *lla);
395 1.10 mycroft cplim = ":";
396 1.10 mycroft }
397 1.10 mycroft cp = workbuf;
398 1.10 mycroft break;
399 1.10 mycroft }
400 1.10 mycroft default:
401 1.10 mycroft cp = link_ntoa(sdl);
402 1.10 mycroft break;
403 1.10 mycroft }
404 1.4 paul break;
405 1.10 mycroft }
406 1.1 cgd
407 1.1 cgd default:
408 1.1 cgd {
409 1.10 mycroft register u_char *s = (u_char *)sa->sa_data, *slim;
410 1.1 cgd
411 1.10 mycroft slim = sa->sa_len + (u_char *) sa;
412 1.1 cgd cplim = cp + sizeof(workbuf) - 6;
413 1.1 cgd cp += sprintf(cp, "(%d)", sa->sa_family);
414 1.7 cgd while (s < slim && cp < cplim) {
415 1.7 cgd cp += sprintf(cp, " %02x", *s++);
416 1.7 cgd if (s < slim)
417 1.10 mycroft cp += sprintf(cp, "%02x", *s++);
418 1.7 cgd }
419 1.1 cgd cp = workbuf;
420 1.1 cgd }
421 1.1 cgd }
422 1.1 cgd if (width < 0 )
423 1.1 cgd printf("%s ", cp);
424 1.1 cgd else {
425 1.1 cgd if (nflag)
426 1.1 cgd printf("%-*s ", width, cp);
427 1.1 cgd else
428 1.1 cgd printf("%-*.*s ", width, width, cp);
429 1.1 cgd }
430 1.1 cgd }
431 1.1 cgd
432 1.10 mycroft static void
433 1.1 cgd p_flags(f, format)
434 1.4 paul register int f;
435 1.4 paul char *format;
436 1.4 paul {
437 1.4 paul char name[33], *flags;
438 1.4 paul register struct bits *p = bits;
439 1.4 paul
440 1.4 paul for (flags = name; p->b_mask; p++)
441 1.4 paul if (p->b_mask & f)
442 1.4 paul *flags++ = p->b_val;
443 1.4 paul *flags = '\0';
444 1.4 paul printf(format, name);
445 1.4 paul }
446 1.4 paul
447 1.10 mycroft static void
448 1.4 paul p_rtentry(rt)
449 1.10 mycroft register struct rtentry *rt;
450 1.4 paul {
451 1.10 mycroft static struct ifnet ifnet, *lastif;
452 1.10 mycroft static char name[16];
453 1.4 paul
454 1.10 mycroft p_sockaddr(kgetsa(rt_key(rt)), rt->rt_flags, WID_DST);
455 1.10 mycroft p_sockaddr(kgetsa(rt->rt_gateway), RTF_HOST, WID_GW);
456 1.4 paul p_flags(rt->rt_flags, "%-6.6s ");
457 1.4 paul printf("%6d %8d ", rt->rt_refcnt, rt->rt_use);
458 1.13 thorpej if (rt->rt_rmx.rmx_mtu)
459 1.13 thorpej printf("%6d ", rt->rt_rmx.rmx_mtu);
460 1.13 thorpej else
461 1.13 thorpej printf("%6s ", "-");
462 1.10 mycroft if (rt->rt_ifp) {
463 1.10 mycroft if (rt->rt_ifp != lastif) {
464 1.10 mycroft kget(rt->rt_ifp, ifnet);
465 1.10 mycroft kread((u_long)ifnet.if_name, name, 16);
466 1.10 mycroft lastif = rt->rt_ifp;
467 1.10 mycroft }
468 1.10 mycroft printf(" %.15s%d%s", name, ifnet.if_unit,
469 1.10 mycroft rt->rt_nodes[0].rn_dupedkey ? " =>" : "");
470 1.1 cgd }
471 1.10 mycroft putchar('\n');
472 1.1 cgd }
473 1.1 cgd
474 1.1 cgd char *
475 1.1 cgd routename(in)
476 1.12 mycroft u_int32_t in;
477 1.1 cgd {
478 1.1 cgd register char *cp;
479 1.1 cgd static char line[MAXHOSTNAMELEN + 1];
480 1.1 cgd struct hostent *hp;
481 1.1 cgd static char domain[MAXHOSTNAMELEN + 1];
482 1.1 cgd static int first = 1;
483 1.1 cgd
484 1.1 cgd if (first) {
485 1.1 cgd first = 0;
486 1.1 cgd if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
487 1.1 cgd (cp = index(domain, '.')))
488 1.1 cgd (void) strcpy(domain, cp + 1);
489 1.1 cgd else
490 1.1 cgd domain[0] = 0;
491 1.1 cgd }
492 1.1 cgd cp = 0;
493 1.1 cgd if (!nflag) {
494 1.1 cgd hp = gethostbyaddr((char *)&in, sizeof (struct in_addr),
495 1.1 cgd AF_INET);
496 1.1 cgd if (hp) {
497 1.1 cgd if ((cp = index(hp->h_name, '.')) &&
498 1.1 cgd !strcmp(cp + 1, domain))
499 1.1 cgd *cp = 0;
500 1.1 cgd cp = hp->h_name;
501 1.1 cgd }
502 1.1 cgd }
503 1.1 cgd if (cp)
504 1.1 cgd strncpy(line, cp, sizeof(line) - 1);
505 1.1 cgd else {
506 1.1 cgd #define C(x) ((x) & 0xff)
507 1.10 mycroft in = ntohl(in);
508 1.10 mycroft sprintf(line, "%u.%u.%u.%u",
509 1.10 mycroft C(in >> 24), C(in >> 16), C(in >> 8), C(in));
510 1.1 cgd }
511 1.1 cgd return (line);
512 1.1 cgd }
513 1.1 cgd
514 1.1 cgd /*
515 1.1 cgd * Return the name of the network whose address is given.
516 1.1 cgd * The address is assumed to be that of a net or subnet, not a host.
517 1.1 cgd */
518 1.1 cgd char *
519 1.1 cgd netname(in, mask)
520 1.12 mycroft u_int32_t in, mask;
521 1.1 cgd {
522 1.1 cgd char *cp = 0;
523 1.1 cgd static char line[MAXHOSTNAMELEN + 1];
524 1.1 cgd struct netent *np = 0;
525 1.12 mycroft u_int32_t net;
526 1.1 cgd int subnetshift;
527 1.1 cgd
528 1.12 mycroft in = ntohl(in);
529 1.12 mycroft mask = ntohl(mask);
530 1.12 mycroft if (!nflag && in != INADDR_ANY) {
531 1.12 mycroft if (mask == INADDR_ANY) {
532 1.12 mycroft if (IN_CLASSA(in)) {
533 1.1 cgd mask = IN_CLASSA_NET;
534 1.1 cgd subnetshift = 8;
535 1.12 mycroft } else if (IN_CLASSB(in)) {
536 1.1 cgd mask = IN_CLASSB_NET;
537 1.1 cgd subnetshift = 8;
538 1.1 cgd } else {
539 1.1 cgd mask = IN_CLASSC_NET;
540 1.1 cgd subnetshift = 4;
541 1.1 cgd }
542 1.1 cgd /*
543 1.1 cgd * If there are more bits than the standard mask
544 1.1 cgd * would suggest, subnets must be in use.
545 1.1 cgd * Guess at the subnet mask, assuming reasonable
546 1.1 cgd * width subnet fields.
547 1.1 cgd */
548 1.12 mycroft while (in &~ mask)
549 1.1 cgd mask = (long)mask >> subnetshift;
550 1.1 cgd }
551 1.12 mycroft net = in & mask;
552 1.1 cgd while ((mask & 1) == 0)
553 1.1 cgd mask >>= 1, net >>= 1;
554 1.1 cgd np = getnetbyaddr(net, AF_INET);
555 1.1 cgd if (np)
556 1.1 cgd cp = np->n_name;
557 1.10 mycroft }
558 1.1 cgd if (cp)
559 1.1 cgd strncpy(line, cp, sizeof(line) - 1);
560 1.12 mycroft else if ((in & 0xffffff) == 0)
561 1.12 mycroft sprintf(line, "%u", C(in >> 24));
562 1.12 mycroft else if ((in & 0xffff) == 0)
563 1.12 mycroft sprintf(line, "%u.%u", C(in >> 24) , C(in >> 16));
564 1.12 mycroft else if ((in & 0xff) == 0)
565 1.12 mycroft sprintf(line, "%u.%u.%u", C(in >> 24), C(in >> 16), C(in >> 8));
566 1.1 cgd else
567 1.12 mycroft sprintf(line, "%u.%u.%u.%u", C(in >> 24),
568 1.12 mycroft C(in >> 16), C(in >> 8), C(in));
569 1.1 cgd return (line);
570 1.1 cgd }
571 1.1 cgd
572 1.1 cgd /*
573 1.1 cgd * Print routing statistics
574 1.1 cgd */
575 1.10 mycroft void
576 1.1 cgd rt_stats(off)
577 1.9 cgd u_long off;
578 1.1 cgd {
579 1.1 cgd struct rtstat rtstat;
580 1.1 cgd
581 1.1 cgd if (off == 0) {
582 1.1 cgd printf("rtstat: symbol not in namelist\n");
583 1.1 cgd return;
584 1.1 cgd }
585 1.10 mycroft kread(off, (char *)&rtstat, sizeof (rtstat));
586 1.1 cgd printf("routing:\n");
587 1.1 cgd printf("\t%u bad routing redirect%s\n",
588 1.1 cgd rtstat.rts_badredirect, plural(rtstat.rts_badredirect));
589 1.1 cgd printf("\t%u dynamically created route%s\n",
590 1.1 cgd rtstat.rts_dynamic, plural(rtstat.rts_dynamic));
591 1.1 cgd printf("\t%u new gateway%s due to redirects\n",
592 1.1 cgd rtstat.rts_newgateway, plural(rtstat.rts_newgateway));
593 1.1 cgd printf("\t%u destination%s found unreachable\n",
594 1.1 cgd rtstat.rts_unreach, plural(rtstat.rts_unreach));
595 1.1 cgd printf("\t%u use%s of a wildcard route\n",
596 1.1 cgd rtstat.rts_wildcard, plural(rtstat.rts_wildcard));
597 1.1 cgd }
598 1.1 cgd short ns_nullh[] = {0,0,0};
599 1.1 cgd short ns_bh[] = {-1,-1,-1};
600 1.1 cgd
601 1.1 cgd char *
602 1.10 mycroft ns_print(sa)
603 1.10 mycroft register struct sockaddr *sa;
604 1.1 cgd {
605 1.10 mycroft register struct sockaddr_ns *sns = (struct sockaddr_ns*)sa;
606 1.1 cgd struct ns_addr work;
607 1.1 cgd union { union ns_net net_e; u_long long_e; } net;
608 1.1 cgd u_short port;
609 1.1 cgd static char mybuf[50], cport[10], chost[25];
610 1.1 cgd char *host = "";
611 1.1 cgd register char *p; register u_char *q;
612 1.1 cgd
613 1.1 cgd work = sns->sns_addr;
614 1.1 cgd port = ntohs(work.x_port);
615 1.1 cgd work.x_port = 0;
616 1.1 cgd net.net_e = work.x_net;
617 1.1 cgd if (ns_nullhost(work) && net.long_e == 0) {
618 1.1 cgd if (port ) {
619 1.1 cgd sprintf(mybuf, "*.%xH", port);
620 1.1 cgd upHex(mybuf);
621 1.1 cgd } else
622 1.1 cgd sprintf(mybuf, "*.*");
623 1.1 cgd return (mybuf);
624 1.1 cgd }
625 1.1 cgd
626 1.1 cgd if (bcmp(ns_bh, work.x_host.c_host, 6) == 0) {
627 1.1 cgd host = "any";
628 1.1 cgd } else if (bcmp(ns_nullh, work.x_host.c_host, 6) == 0) {
629 1.1 cgd host = "*";
630 1.1 cgd } else {
631 1.1 cgd q = work.x_host.c_host;
632 1.1 cgd sprintf(chost, "%02x%02x%02x%02x%02x%02xH",
633 1.1 cgd q[0], q[1], q[2], q[3], q[4], q[5]);
634 1.10 mycroft for (p = chost; *p == '0' && p < chost + 12; p++)
635 1.10 mycroft continue;
636 1.1 cgd host = p;
637 1.1 cgd }
638 1.1 cgd if (port)
639 1.1 cgd sprintf(cport, ".%xH", htons(port));
640 1.1 cgd else
641 1.1 cgd *cport = 0;
642 1.1 cgd
643 1.1 cgd sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport);
644 1.1 cgd upHex(mybuf);
645 1.1 cgd return(mybuf);
646 1.1 cgd }
647 1.1 cgd
648 1.1 cgd char *
649 1.10 mycroft ns_phost(sa)
650 1.10 mycroft struct sockaddr *sa;
651 1.1 cgd {
652 1.10 mycroft register struct sockaddr_ns *sns = (struct sockaddr_ns *)sa;
653 1.1 cgd struct sockaddr_ns work;
654 1.1 cgd static union ns_net ns_zeronet;
655 1.1 cgd char *p;
656 1.10 mycroft
657 1.1 cgd work = *sns;
658 1.1 cgd work.sns_addr.x_port = 0;
659 1.1 cgd work.sns_addr.x_net = ns_zeronet;
660 1.1 cgd
661 1.10 mycroft p = ns_print((struct sockaddr *)&work);
662 1.1 cgd if (strncmp("0H.", p, 3) == 0) p += 3;
663 1.1 cgd return(p);
664 1.1 cgd }
665 1.10 mycroft
666 1.10 mycroft void
667 1.1 cgd upHex(p0)
668 1.10 mycroft char *p0;
669 1.1 cgd {
670 1.1 cgd register char *p = p0;
671 1.1 cgd for (; *p; p++) switch (*p) {
672 1.1 cgd
673 1.1 cgd case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
674 1.1 cgd *p += ('A' - 'a');
675 1.1 cgd }
676 1.1 cgd }
677