Home | History | Annotate | Line # | Download | only in route
show.c revision 1.51
      1  1.51  christos /*	$NetBSD: show.c,v 1.51 2020/08/29 19:28:32 christos Exp $	*/
      2   1.1       gwr 
      3   1.1       gwr /*
      4   1.1       gwr  * Copyright (c) 1983, 1988, 1993
      5   1.1       gwr  *	The Regents of the University of California.  All rights reserved.
      6   1.1       gwr  *
      7   1.1       gwr  * Redistribution and use in source and binary forms, with or without
      8   1.1       gwr  * modification, are permitted provided that the following conditions
      9   1.1       gwr  * are met:
     10   1.1       gwr  * 1. Redistributions of source code must retain the above copyright
     11   1.1       gwr  *    notice, this list of conditions and the following disclaimer.
     12   1.1       gwr  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       gwr  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       gwr  *    documentation and/or other materials provided with the distribution.
     15  1.21       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       gwr  *    may be used to endorse or promote products derived from this software
     17   1.1       gwr  *    without specific prior written permission.
     18   1.1       gwr  *
     19   1.1       gwr  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       gwr  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       gwr  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       gwr  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       gwr  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       gwr  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       gwr  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       gwr  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       gwr  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       gwr  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       gwr  * SUCH DAMAGE.
     30   1.1       gwr  */
     31   1.1       gwr 
     32   1.4     lukem #include <sys/cdefs.h>
     33   1.1       gwr #ifndef lint
     34   1.1       gwr #if 0
     35   1.1       gwr static char sccsid[] = "from: @(#)route.c	8.3 (Berkeley) 3/9/94";
     36   1.1       gwr #else
     37  1.51  christos __RCSID("$NetBSD: show.c,v 1.51 2020/08/29 19:28:32 christos Exp $");
     38   1.1       gwr #endif
     39   1.1       gwr #endif /* not lint */
     40   1.1       gwr 
     41   1.1       gwr #include <sys/param.h>
     42   1.1       gwr #include <sys/protosw.h>
     43   1.1       gwr #include <sys/socket.h>
     44   1.1       gwr 
     45  1.41    kefren #include <arpa/inet.h>
     46   1.1       gwr #include <net/if.h>
     47   1.1       gwr #include <net/if_dl.h>
     48   1.1       gwr #include <net/if_types.h>
     49   1.1       gwr #include <net/route.h>
     50   1.1       gwr #include <netinet/in.h>
     51  1.41    kefren #include <netmpls/mpls.h>
     52   1.1       gwr 
     53   1.1       gwr #include <sys/sysctl.h>
     54   1.1       gwr 
     55   1.1       gwr #include <netdb.h>
     56  1.36    dyoung #include <stdbool.h>
     57   1.1       gwr #include <stdio.h>
     58   1.1       gwr #include <stdlib.h>
     59   1.1       gwr #include <string.h>
     60   1.1       gwr #include <unistd.h>
     61   1.5  christos #include <err.h>
     62   1.1       gwr 
     63  1.26  ginsbach #include "keywords.h"
     64  1.46  christos #include "rtutil.h"
     65   1.2  christos #include "extern.h"
     66  1.42     pooka #include "prog_ops.h"
     67   1.1       gwr 
     68  1.33    dyoung void
     69  1.38    dyoung parse_show_opts(int argc, char * const *argv, int *afp, int *flagsp,
     70  1.36    dyoung     const char **afnamep, bool nolink)
     71  1.33    dyoung {
     72  1.33    dyoung 	const char *afname = "unspec";
     73  1.33    dyoung 	int af, flags;
     74  1.33    dyoung 
     75  1.33    dyoung 	flags = 0;
     76  1.33    dyoung 	af = AF_UNSPEC;
     77  1.33    dyoung 	for (; argc >= 2; argc--) {
     78  1.33    dyoung 		if (*argv[argc - 1] != '-')
     79  1.33    dyoung 			goto bad;
     80  1.33    dyoung 		switch (keyword(argv[argc - 1] + 1)) {
     81  1.33    dyoung 		case K_HOST:
     82  1.33    dyoung 			flags |= RTF_HOST;
     83  1.33    dyoung 			break;
     84  1.33    dyoung 		case K_INET:
     85  1.33    dyoung 			af = AF_INET;
     86  1.33    dyoung 			afname = argv[argc - 1] + 1;
     87  1.33    dyoung 			break;
     88  1.33    dyoung #ifdef INET6
     89  1.33    dyoung 		case K_INET6:
     90  1.33    dyoung 			af = AF_INET6;
     91  1.33    dyoung 			afname = argv[argc - 1] + 1;
     92  1.33    dyoung 			break;
     93  1.33    dyoung #endif
     94  1.33    dyoung #ifndef SMALL
     95  1.33    dyoung 		case K_ATALK:
     96  1.33    dyoung 			af = AF_APPLETALK;
     97  1.33    dyoung 			afname = argv[argc - 1] + 1;
     98  1.33    dyoung 			break;
     99  1.41    kefren 		case K_MPLS:
    100  1.41    kefren 			af = AF_MPLS;
    101  1.41    kefren 			afname = argv[argc - 1] + 1;
    102  1.41    kefren 			break;
    103  1.33    dyoung #endif /* SMALL */
    104  1.33    dyoung 		case K_LINK:
    105  1.33    dyoung 			if (nolink)
    106  1.33    dyoung 				goto bad;
    107  1.33    dyoung 			af = AF_LINK;
    108  1.33    dyoung 			afname = argv[argc - 1] + 1;
    109  1.33    dyoung 			break;
    110  1.33    dyoung 		default:
    111  1.33    dyoung 			goto bad;
    112  1.33    dyoung 		}
    113  1.33    dyoung 	}
    114  1.33    dyoung 	switch (argc) {
    115  1.33    dyoung 	case 1:
    116  1.33    dyoung 	case 0:
    117  1.33    dyoung 		break;
    118  1.33    dyoung 	default:
    119  1.33    dyoung 	bad:
    120  1.33    dyoung 		usage(argv[argc - 1]);
    121  1.33    dyoung 	}
    122  1.33    dyoung 	if (afnamep != NULL)
    123  1.33    dyoung 		*afnamep = afname;
    124  1.33    dyoung 	*afp = af;
    125  1.33    dyoung 	*flagsp = flags;
    126  1.33    dyoung }
    127  1.33    dyoung 
    128   1.1       gwr /*
    129   1.1       gwr  * Print routing tables.
    130   1.1       gwr  */
    131   1.1       gwr void
    132  1.46  christos show(int argc, char *const *argv, int flags)
    133   1.1       gwr {
    134  1.46  christos 	int af, rflags;
    135  1.51  christos 	static int interesting = RTF_ANNOUNCE | RTF_BLACKHOLE | RTF_BROADCAST |
    136  1.51  christos 	    RTF_CONNECTED | RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST | RTF_LLDATA |
    137  1.51  christos 	    RTF_LOCAL | RTF_MODIFIED | RTF_PROTO1 | RTF_PROTO2 | RTF_REJECT |
    138  1.51  christos 	    RTF_STATIC | RTF_UP;
    139   1.1       gwr 
    140  1.46  christos 	parse_show_opts(argc, argv, &af, &rflags, NULL, true);
    141  1.46  christos 	p_rttables(af, flags, rflags, interesting);
    142   1.1       gwr }
    143