Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1 | /* |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 2 | * stolen from net-tools-1.59 and stripped down for busybox by |
| 3 | * Erik Andersen <andersee@debian.org> |
| 4 | * |
| 5 | * Heavily modified by Manuel Novoa III Mar 12, 2001 |
| 6 | * |
| 7 | * Pruned unused code using KEEP_UNUSED define. |
| 8 | * Added print_bytes_scaled function to reduce code size. |
| 9 | * Added some (potentially) missing defines. |
| 10 | * Improved display support for -a and for a named interface. |
| 11 | * |
| 12 | * ----------------------------------------------------------- |
| 13 | * |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 14 | * ifconfig This file contains an implementation of the command |
| 15 | * that either displays or sets the characteristics of |
| 16 | * one or more of the system's networking interfaces. |
| 17 | * |
Eric Andersen | 7365c58 | 2002-09-17 06:36:56 +0000 | [diff] [blame] | 18 | * Version: $Id: interface.c,v 1.10 2002/09/17 06:36:02 andersen Exp $ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 19 | * |
| 20 | * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org> |
| 21 | * and others. Copyright 1993 MicroWalt Corporation |
| 22 | * |
| 23 | * This program is free software; you can redistribute it |
| 24 | * and/or modify it under the terms of the GNU General |
| 25 | * Public License as published by the Free Software |
| 26 | * Foundation; either version 2 of the License, or (at |
| 27 | * your option) any later version. |
| 28 | * |
| 29 | * Patched to support 'add' and 'del' keywords for INET(4) addresses |
| 30 | * by Mrs. Brisby <mrs.brisby@nimh.org> |
| 31 | * |
| 32 | * {1.34} - 19980630 - Arnaldo Carvalho de Melo <acme@conectiva.com.br> |
| 33 | * - gettext instead of catgets for i18n |
| 34 | * 10/1998 - Andi Kleen. Use interface list primitives. |
| 35 | * 20001008 - Bernd Eckenfels, Patch from RH for setting mtu |
| 36 | * (default AF was wrong) |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 37 | */ |
| 38 | |
| 39 | /* #define KEEP_UNUSED */ |
| 40 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 41 | /* |
| 42 | * |
| 43 | * Protocol Families. |
| 44 | * |
| 45 | */ |
| 46 | #define HAVE_AFINET 1 |
| 47 | #undef HAVE_AFINET6 |
| 48 | #undef HAVE_AFIPX |
| 49 | #undef HAVE_AFATALK |
| 50 | #undef HAVE_AFNETROM |
| 51 | #undef HAVE_AFX25 |
| 52 | #undef HAVE_AFECONET |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 53 | #undef HAVE_AFASH |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 54 | |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 55 | #if CONFIG_FEATURE_IPV6 |
| 56 | #define HAVE_AFINET6 1 |
| 57 | #endif |
| 58 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 59 | /* |
| 60 | * |
| 61 | * Device Hardware types. |
| 62 | * |
| 63 | */ |
| 64 | #define HAVE_HWETHER 1 |
| 65 | #define HAVE_HWPPP 1 |
| 66 | #undef HAVE_HWSLIP |
| 67 | |
| 68 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 69 | #include "inet_common.h" |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 70 | #include <stdio.h> |
| 71 | #include <errno.h> |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 72 | #include <stdlib.h> |
| 73 | #include <string.h> |
| 74 | #include <unistd.h> |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 75 | #include <fcntl.h> |
| 76 | #include <ctype.h> |
| 77 | #include <sys/ioctl.h> |
| 78 | #include <net/if.h> |
| 79 | #include <net/if_arp.h> |
Glenn L McGrath | 6b8c550 | 2001-05-05 03:19:12 +0000 | [diff] [blame] | 80 | #include "libbb.h" |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 81 | |
| 82 | #define _(x) x |
| 83 | #define _PATH_PROCNET_DEV "/proc/net/dev" |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 84 | #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6" |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 85 | #define new(p) ((p) = xcalloc(1,sizeof(*(p)))) |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 86 | #define KRELEASE(maj,min,patch) ((maj) * 65536 + (min)*256 + (patch)) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 87 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 88 | static int procnetdev_vsn = 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 89 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 90 | /* Ugh. But libc5 doesn't provide POSIX types. */ |
| 91 | #include <asm/types.h> |
| 92 | |
| 93 | |
| 94 | #ifdef HAVE_HWSLIP |
| 95 | #include <linux/if_slip.h> |
| 96 | #endif |
| 97 | |
| 98 | #if HAVE_AFINET6 |
| 99 | |
| 100 | #ifndef _LINUX_IN6_H |
| 101 | /* |
| 102 | * This is in linux/include/net/ipv6.h. |
| 103 | */ |
| 104 | |
| 105 | struct in6_ifreq { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 106 | struct in6_addr ifr6_addr; |
| 107 | __u32 ifr6_prefixlen; |
| 108 | unsigned int ifr6_ifindex; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | #endif |
| 112 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 113 | #endif /* HAVE_AFINET6 */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 114 | |
| 115 | #if HAVE_AFIPX |
Eric Andersen | b6b519b | 2001-04-09 23:52:18 +0000 | [diff] [blame] | 116 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 117 | #include <netipx/ipx.h> |
| 118 | #else |
| 119 | #include "ipx.h" |
| 120 | #endif |
| 121 | #endif |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 122 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 123 | /* Defines for glibc2.0 users. */ |
| 124 | #ifndef SIOCSIFTXQLEN |
| 125 | #define SIOCSIFTXQLEN 0x8943 |
| 126 | #define SIOCGIFTXQLEN 0x8942 |
| 127 | #endif |
| 128 | |
| 129 | /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */ |
| 130 | #ifndef ifr_qlen |
| 131 | #define ifr_qlen ifr_ifru.ifru_mtu |
| 132 | #endif |
| 133 | |
| 134 | #ifndef HAVE_TXQUEUELEN |
| 135 | #define HAVE_TXQUEUELEN 1 |
| 136 | #endif |
| 137 | |
| 138 | #ifndef IFF_DYNAMIC |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 139 | #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 140 | #endif |
| 141 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 142 | /* This structure defines protocol families and their handlers. */ |
| 143 | struct aftype { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 144 | const char *name; |
| 145 | const char *title; |
| 146 | int af; |
| 147 | int alen; |
| 148 | char *(*print) (unsigned char *); |
| 149 | char *(*sprint) (struct sockaddr *, int numeric); |
| 150 | int (*input) (int type, char *bufp, struct sockaddr *); |
| 151 | void (*herror) (char *text); |
| 152 | int (*rprint) (int options); |
| 153 | int (*rinput) (int typ, int ext, char **argv); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 154 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 155 | /* may modify src */ |
| 156 | int (*getmask) (char *src, struct sockaddr * mask, char *name); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 157 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 158 | int fd; |
| 159 | char *flag_file; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 162 | #ifdef KEEP_UNUSED |
| 163 | |
| 164 | static int flag_unx; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 165 | |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 166 | #ifdef HAVE_AFIPX |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 167 | static int flag_ipx; |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 168 | #endif |
| 169 | #ifdef HAVE_AFX25 |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 170 | static int flag_ax25; |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 171 | #endif |
| 172 | #ifdef HAVE_AFATALK |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 173 | static int flag_ddp; |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 174 | #endif |
| 175 | #ifdef HAVE_AFNETROM |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 176 | static int flag_netrom; |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 177 | #endif |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 178 | static int flag_inet; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 179 | |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 180 | #ifdef HAVE_AFINET6 |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 181 | static int flag_inet6; |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 182 | #endif |
| 183 | #ifdef HAVE_AFECONET |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 184 | static int flag_econet; |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 185 | #endif |
| 186 | #ifdef HAVE_AFX25 |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 187 | static int flag_x25 = 0; |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 188 | #endif |
| 189 | #ifdef HAVE_AFASH |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 190 | static int flag_ash; |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 191 | #endif |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 192 | |
| 193 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 194 | static struct aftrans_t { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 195 | char *alias; |
| 196 | char *name; |
| 197 | int *flag; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 198 | } aftrans[] = { |
| 199 | |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 200 | #ifdef HAVE_AFX25 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 201 | { |
| 202 | "ax25", "ax25", &flag_ax25}, |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 203 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 204 | { |
| 205 | "ip", "inet", &flag_inet}, |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 206 | #ifdef HAVE_AFINET6 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 207 | { |
| 208 | "ip6", "inet6", &flag_inet6}, |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 209 | #endif |
| 210 | #ifdef HAVE_AFIPX |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 211 | { |
| 212 | "ipx", "ipx", &flag_ipx}, |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 213 | #endif |
| 214 | #ifdef HAVE_AFATALK |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 215 | { |
| 216 | "appletalk", "ddp", &flag_ddp}, |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 217 | #endif |
| 218 | #ifdef HAVE_AFNETROM |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 219 | { |
| 220 | "netrom", "netrom", &flag_netrom}, |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 221 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 222 | { |
| 223 | "inet", "inet", &flag_inet}, |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 224 | #ifdef HAVE_AFINET6 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 225 | { |
| 226 | "inet6", "inet6", &flag_inet6}, |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 227 | #endif |
| 228 | #ifdef HAVE_AFATALK |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 229 | { |
| 230 | "ddp", "ddp", &flag_ddp}, |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 231 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 232 | { |
| 233 | "unix", "unix", &flag_unx}, { |
| 234 | "tcpip", "inet", &flag_inet}, |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 235 | #ifdef HAVE_AFECONET |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 236 | { |
| 237 | "econet", "ec", &flag_econet}, |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 238 | #endif |
| 239 | #ifdef HAVE_AFX25 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 240 | { |
| 241 | "x25", "x25", &flag_x25}, |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 242 | #endif |
| 243 | #ifdef HAVE_AFASH |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 244 | { |
| 245 | "ash", "ash", &flag_ash}, |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 246 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 247 | { |
| 248 | 0, 0, 0} |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 251 | static char afname[256] = ""; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 252 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 253 | |
| 254 | #if HAVE_AFUNIX |
| 255 | |
| 256 | /* Display a UNIX domain address. */ |
| 257 | static char *UNIX_print(unsigned char *ptr) |
| 258 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 259 | return (ptr); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
| 263 | /* Display a UNIX domain address. */ |
| 264 | static char *UNIX_sprint(struct sockaddr *sap, int numeric) |
| 265 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 266 | static char buf[64]; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 267 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 268 | if (sap->sa_family == 0xFFFF || sap->sa_family == 0) |
| 269 | return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf)); |
| 270 | return (UNIX_print(sap->sa_data)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 274 | static struct aftype unix_aftype = { |
| 275 | "unix", "UNIX Domain", AF_UNIX, 0, |
| 276 | UNIX_print, UNIX_sprint, NULL, NULL, |
| 277 | NULL, NULL, NULL, |
| 278 | -1, |
| 279 | "/proc/net/unix" |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 280 | }; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 281 | #endif /* HAVE_AFUNIX */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 282 | |
| 283 | #if HAVE_AFINET |
| 284 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 285 | #ifdef KEEP_UNUSED |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 286 | static void INET_reserror(char *text) |
| 287 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 288 | herror(text); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 291 | /* Display an Internet socket address. */ |
| 292 | static char *INET_print(unsigned char *ptr) |
| 293 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 294 | return (inet_ntoa((*(struct in_addr *) ptr))); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 295 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 296 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 297 | |
| 298 | /* Display an Internet socket address. */ |
| 299 | static char *INET_sprint(struct sockaddr *sap, int numeric) |
| 300 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 301 | static char buff[128]; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 302 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 303 | if (sap->sa_family == 0xFFFF || sap->sa_family == 0) |
| 304 | return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 305 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 306 | if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap, |
| 307 | numeric, 0xffffff00) != 0) |
| 308 | return (NULL); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 309 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 310 | return (buff); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 313 | #ifdef KEEP_UNUSED |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 314 | static char *INET_sprintmask(struct sockaddr *sap, int numeric, |
| 315 | unsigned int netmask) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 316 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 317 | static char buff[128]; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 318 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 319 | if (sap->sa_family == 0xFFFF || sap->sa_family == 0) |
| 320 | return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff)); |
| 321 | if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap, |
| 322 | numeric, netmask) != 0) |
| 323 | return (NULL); |
| 324 | return (buff); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 327 | static int INET_getsock(char *bufp, struct sockaddr *sap) |
| 328 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 329 | char *sp = bufp, *bp; |
| 330 | unsigned int i; |
| 331 | unsigned val; |
| 332 | struct sockaddr_in *sin; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 333 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 334 | sin = (struct sockaddr_in *) sap; |
| 335 | sin->sin_family = AF_INET; |
| 336 | sin->sin_port = 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 337 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 338 | val = 0; |
| 339 | bp = (char *) &val; |
| 340 | for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) { |
| 341 | *sp = toupper(*sp); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 342 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 343 | if ((*sp >= 'A') && (*sp <= 'F')) |
| 344 | bp[i] |= (int) (*sp - 'A') + 10; |
| 345 | else if ((*sp >= '0') && (*sp <= '9')) |
| 346 | bp[i] |= (int) (*sp - '0'); |
| 347 | else |
| 348 | return (-1); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 349 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 350 | bp[i] <<= 4; |
| 351 | sp++; |
| 352 | *sp = toupper(*sp); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 353 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 354 | if ((*sp >= 'A') && (*sp <= 'F')) |
| 355 | bp[i] |= (int) (*sp - 'A') + 10; |
| 356 | else if ((*sp >= '0') && (*sp <= '9')) |
| 357 | bp[i] |= (int) (*sp - '0'); |
| 358 | else |
| 359 | return (-1); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 360 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 361 | sp++; |
| 362 | } |
| 363 | sin->sin_addr.s_addr = htonl(val); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 364 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 365 | return (sp - bufp); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | static int INET_input(int type, char *bufp, struct sockaddr *sap) |
| 369 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 370 | switch (type) { |
| 371 | case 1: |
| 372 | return (INET_getsock(bufp, sap)); |
| 373 | case 256: |
| 374 | return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1)); |
| 375 | default: |
| 376 | return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0)); |
| 377 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static int INET_getnetmask(char *adr, struct sockaddr *m, char *name) |
| 381 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 382 | struct sockaddr_in *mask = (struct sockaddr_in *) m; |
| 383 | char *slash, *end; |
| 384 | int prefix; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 385 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 386 | if ((slash = strchr(adr, '/')) == NULL) |
| 387 | return 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 388 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 389 | *slash++ = '\0'; |
| 390 | prefix = strtoul(slash, &end, 0); |
| 391 | if (*end != '\0') |
| 392 | return -1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 393 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 394 | if (name) { |
| 395 | sprintf(name, "/%d", prefix); |
| 396 | } |
| 397 | mask->sin_family = AF_INET; |
| 398 | mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix)); |
| 399 | return 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 400 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 401 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 402 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 403 | static struct aftype inet_aftype = { |
| 404 | "inet", "DARPA Internet", AF_INET, sizeof(unsigned long), |
| 405 | NULL /* UNUSED INET_print */ , INET_sprint, |
| 406 | NULL /* UNUSED INET_input */ , NULL /* UNUSED INET_reserror */ , |
| 407 | NULL /*INET_rprint */ , NULL /*INET_rinput */ , |
| 408 | NULL /* UNUSED INET_getnetmask */ , |
| 409 | -1, |
| 410 | NULL |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 411 | }; |
| 412 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 413 | #endif /* HAVE_AFINET */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 414 | |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 415 | #if HAVE_AFINET6 |
| 416 | |
| 417 | #ifdef KEEP_UNUSED |
| 418 | static void INET6_reserror(char *text) |
| 419 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 420 | herror(text); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /* Display an Internet socket address. */ |
| 424 | static char *INET6_print(unsigned char *ptr) |
| 425 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 426 | static char name[80]; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 427 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 428 | inet_ntop(AF_INET6, (struct in6_addr *) ptr, name, 80); |
| 429 | return name; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 430 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 431 | #endif /* KEEP_UNUSED */ |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 432 | |
| 433 | /* Display an Internet socket address. */ |
| 434 | /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */ |
| 435 | static char *INET6_sprint(struct sockaddr *sap, int numeric) |
| 436 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 437 | static char buff[128]; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 438 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 439 | if (sap->sa_family == 0xFFFF || sap->sa_family == 0) |
| 440 | return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff)); |
| 441 | if (INET6_rresolve |
| 442 | (buff, sizeof(buff), (struct sockaddr_in6 *) sap, numeric) != 0) |
| 443 | return safe_strncpy(buff, _("[UNKNOWN]"), sizeof(buff)); |
| 444 | return (buff); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | #ifdef KEEP_UNUSED |
| 448 | static int INET6_getsock(char *bufp, struct sockaddr *sap) |
| 449 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 450 | struct sockaddr_in6 *sin6; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 451 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 452 | sin6 = (struct sockaddr_in6 *) sap; |
| 453 | sin6->sin6_family = AF_INET6; |
| 454 | sin6->sin6_port = 0; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 455 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 456 | if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0) |
| 457 | return (-1); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 458 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 459 | return 16; /* ?;) */ |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static int INET6_input(int type, char *bufp, struct sockaddr *sap) |
| 463 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 464 | switch (type) { |
| 465 | case 1: |
| 466 | return (INET6_getsock(bufp, sap)); |
| 467 | default: |
| 468 | return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap)); |
| 469 | } |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 470 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 471 | #endif /* KEEP_UNUSED */ |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 472 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 473 | static struct aftype inet6_aftype = { |
| 474 | "inet6", "IPv6", AF_INET6, sizeof(struct in6_addr), |
| 475 | NULL /* UNUSED INET6_print */ , INET6_sprint, |
| 476 | NULL /* UNUSED INET6_input */ , NULL /* UNUSED INET6_reserror */ , |
| 477 | NULL /*INET6_rprint */ , NULL /*INET6_rinput */ , |
| 478 | NULL /* UNUSED INET6_getnetmask */ , |
| 479 | -1, |
| 480 | NULL |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 481 | }; |
| 482 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 483 | #endif /* HAVE_AFINET6 */ |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 484 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 485 | /* Display an UNSPEC address. */ |
| 486 | static char *UNSPEC_print(unsigned char *ptr) |
| 487 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 488 | static char buff[sizeof(struct sockaddr) * 3 + 1]; |
| 489 | char *pos; |
| 490 | unsigned int i; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 491 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 492 | pos = buff; |
| 493 | for (i = 0; i < sizeof(struct sockaddr); i++) { |
| 494 | /* careful -- not every libc's sprintf returns # bytes written */ |
| 495 | sprintf(pos, "%02X-", (*ptr++ & 0377)); |
| 496 | pos += 3; |
| 497 | } |
| 498 | /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */ |
| 499 | *--pos = '\0'; |
| 500 | return (buff); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* Display an UNSPEC socket address. */ |
| 504 | static char *UNSPEC_sprint(struct sockaddr *sap, int numeric) |
| 505 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 506 | static char buf[64]; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 507 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 508 | if (sap->sa_family == 0xFFFF || sap->sa_family == 0) |
| 509 | return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf)); |
| 510 | return (UNSPEC_print(sap->sa_data)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 513 | static struct aftype unspec_aftype = { |
| 514 | "unspec", "UNSPEC", AF_UNSPEC, 0, |
| 515 | UNSPEC_print, UNSPEC_sprint, NULL, NULL, |
| 516 | NULL, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 517 | }; |
| 518 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 519 | static struct aftype *aftypes[] = { |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 520 | #if HAVE_AFUNIX |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 521 | &unix_aftype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 522 | #endif |
| 523 | #if HAVE_AFINET |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 524 | &inet_aftype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 525 | #endif |
| 526 | #if HAVE_AFINET6 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 527 | &inet6_aftype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 528 | #endif |
| 529 | #if HAVE_AFAX25 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 530 | &ax25_aftype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 531 | #endif |
| 532 | #if HAVE_AFNETROM |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 533 | &netrom_aftype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 534 | #endif |
| 535 | #if HAVE_AFROSE |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 536 | &rose_aftype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 537 | #endif |
| 538 | #if HAVE_AFIPX |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 539 | &ipx_aftype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 540 | #endif |
| 541 | #if HAVE_AFATALK |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 542 | &ddp_aftype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 543 | #endif |
| 544 | #if HAVE_AFECONET |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 545 | &ec_aftype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 546 | #endif |
| 547 | #if HAVE_AFASH |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 548 | &ash_aftype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 549 | #endif |
| 550 | #if HAVE_AFX25 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 551 | &x25_aftype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 552 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 553 | &unspec_aftype, |
| 554 | NULL |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 555 | }; |
| 556 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 557 | #ifdef KEEP_UNUSED |
| 558 | static short sVafinit = 0; |
| 559 | |
| 560 | static void afinit() |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 561 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 562 | unspec_aftype.title = _("UNSPEC"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 563 | #if HAVE_AFUNIX |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 564 | unix_aftype.title = _("UNIX Domain"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 565 | #endif |
| 566 | #if HAVE_AFINET |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 567 | inet_aftype.title = _("DARPA Internet"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 568 | #endif |
| 569 | #if HAVE_AFINET6 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 570 | inet6_aftype.title = _("IPv6"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 571 | #endif |
| 572 | #if HAVE_AFAX25 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 573 | ax25_aftype.title = _("AMPR AX.25"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 574 | #endif |
| 575 | #if HAVE_AFNETROM |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 576 | netrom_aftype.title = _("AMPR NET/ROM"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 577 | #endif |
| 578 | #if HAVE_AFIPX |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 579 | ipx_aftype.title = _("Novell IPX"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 580 | #endif |
| 581 | #if HAVE_AFATALK |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 582 | ddp_aftype.title = _("Appletalk DDP"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 583 | #endif |
| 584 | #if HAVE_AFECONET |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 585 | ec_aftype.title = _("Econet"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 586 | #endif |
| 587 | #if HAVE_AFX25 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 588 | x25_aftype.title = _("CCITT X.25"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 589 | #endif |
| 590 | #if HAVE_AFROSE |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 591 | rose_aftype.title = _("AMPR ROSE"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 592 | #endif |
| 593 | #if HAVE_AFASH |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 594 | ash_aftype.title = _("Ash"); |
| 595 | #endif |
| 596 | sVafinit = 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 599 | static int aftrans_opt(const char *arg) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 600 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 601 | struct aftrans_t *paft; |
| 602 | char *tmp1, *tmp2; |
| 603 | char buf[256]; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 604 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 605 | safe_strncpy(buf, arg, sizeof(buf)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 606 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 607 | tmp1 = buf; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 608 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 609 | while (tmp1) { |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 610 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 611 | tmp2 = strchr(tmp1, ','); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 612 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 613 | if (tmp2) |
| 614 | *(tmp2++) = '\0'; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 615 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 616 | paft = aftrans; |
| 617 | for (paft = aftrans; paft->alias; paft++) { |
| 618 | if (strcmp(tmp1, paft->alias)) |
| 619 | continue; |
| 620 | if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) { |
| 621 | fprintf(stderr, _("Too much address family arguments.\n")); |
| 622 | return (0); |
| 623 | } |
| 624 | if (paft->flag) |
| 625 | (*paft->flag)++; |
| 626 | if (afname[0]) |
| 627 | strcat(afname, ","); |
| 628 | strcat(afname, paft->name); |
| 629 | break; |
| 630 | } |
| 631 | if (!paft->alias) { |
| 632 | fprintf(stderr, _("Unknown address family `%s'.\n"), tmp1); |
| 633 | return (1); |
| 634 | } |
| 635 | tmp1 = tmp2; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 636 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 637 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 638 | return (0); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | /* set the default AF list from the program name or a constant value */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 642 | static void aftrans_def(char *tool, char *argv0, char *dflt) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 643 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 644 | char *tmp; |
| 645 | char *buf; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 646 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 647 | strcpy(afname, dflt); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 648 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 649 | if (!(tmp = strrchr(argv0, '/'))) |
| 650 | tmp = argv0; /* no slash?! */ |
| 651 | else |
| 652 | tmp++; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 653 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 654 | if (!(buf = strdup(tmp))) |
| 655 | return; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 656 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 657 | if (strlen(tool) >= strlen(tmp)) { |
| 658 | free(buf); |
| 659 | return; |
| 660 | } |
| 661 | tmp = buf + (strlen(tmp) - strlen(tool)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 662 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 663 | if (strcmp(tmp, tool) != 0) { |
| 664 | free(buf); |
| 665 | return; |
| 666 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 667 | *tmp = '\0'; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 668 | if ((tmp = strchr(buf, '_'))) |
| 669 | *tmp = '\0'; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 670 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 671 | afname[0] = '\0'; |
| 672 | if (aftrans_opt(buf)) |
| 673 | strcpy(afname, buf); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 674 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 675 | free(buf); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 678 | /* Check our protocol family table for this family. */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 679 | static struct aftype *get_aftype(const char *name) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 680 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 681 | struct aftype **afp; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 682 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 683 | #ifdef KEEP_UNUSED |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 684 | if (!sVafinit) |
| 685 | afinit(); |
| 686 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 687 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 688 | afp = aftypes; |
| 689 | while (*afp != NULL) { |
| 690 | if (!strcmp((*afp)->name, name)) |
| 691 | return (*afp); |
| 692 | afp++; |
| 693 | } |
| 694 | if (strchr(name, ',')) |
| 695 | fprintf(stderr, |
| 696 | _("Please don't supply more than one address family.\n")); |
| 697 | return (NULL); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 698 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 699 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 700 | |
| 701 | /* Check our protocol family table for this family. */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 702 | static struct aftype *get_afntype(int af) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 703 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 704 | struct aftype **afp; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 705 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 706 | #ifdef KEEP_UNUSED |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 707 | if (!sVafinit) |
| 708 | afinit(); |
| 709 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 710 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 711 | afp = aftypes; |
| 712 | while (*afp != NULL) { |
| 713 | if ((*afp)->af == af) |
| 714 | return (*afp); |
| 715 | afp++; |
| 716 | } |
| 717 | return (NULL); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /* Check our protocol family table for this family and return its socket */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 721 | static int get_socket_for_af(int af) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 722 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 723 | struct aftype **afp; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 724 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 725 | #ifdef KEEP_UNUSED |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 726 | if (!sVafinit) |
| 727 | afinit(); |
| 728 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 729 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 730 | afp = aftypes; |
| 731 | while (*afp != NULL) { |
| 732 | if ((*afp)->af == af) |
| 733 | return (*afp)->fd; |
| 734 | afp++; |
| 735 | } |
| 736 | return -1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 739 | #ifdef KEEP_UNUSED |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 740 | /* type: 0=all, 1=getroute */ |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 741 | static void print_aflist(int type) |
| 742 | { |
| 743 | int count = 0; |
| 744 | char *txt; |
| 745 | struct aftype **afp; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 746 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 747 | #ifdef KEEP_UNUSED |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 748 | if (!sVafinit) |
| 749 | afinit(); |
| 750 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 751 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 752 | afp = aftypes; |
| 753 | while (*afp != NULL) { |
| 754 | if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) { |
| 755 | afp++; |
| 756 | continue; |
| 757 | } |
| 758 | if ((count % 3) == 0) |
| 759 | fprintf(stderr, count ? "\n " : " "); |
| 760 | txt = (*afp)->name; |
| 761 | if (!txt) |
| 762 | txt = ".."; |
| 763 | fprintf(stderr, "%s (%s) ", txt, _((*afp)->title)); |
| 764 | count++; |
| 765 | afp++; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 766 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 767 | fprintf(stderr, "\n"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 768 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 769 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 770 | |
| 771 | struct user_net_device_stats { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 772 | unsigned long long rx_packets; /* total packets received */ |
| 773 | unsigned long long tx_packets; /* total packets transmitted */ |
| 774 | unsigned long long rx_bytes; /* total bytes received */ |
| 775 | unsigned long long tx_bytes; /* total bytes transmitted */ |
| 776 | unsigned long rx_errors; /* bad packets received */ |
| 777 | unsigned long tx_errors; /* packet transmit problems */ |
| 778 | unsigned long rx_dropped; /* no space in linux buffers */ |
| 779 | unsigned long tx_dropped; /* no space available in linux */ |
| 780 | unsigned long rx_multicast; /* multicast packets received */ |
| 781 | unsigned long rx_compressed; |
| 782 | unsigned long tx_compressed; |
| 783 | unsigned long collisions; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 784 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 785 | /* detailed rx_errors: */ |
| 786 | unsigned long rx_length_errors; |
| 787 | unsigned long rx_over_errors; /* receiver ring buff overflow */ |
| 788 | unsigned long rx_crc_errors; /* recved pkt with crc error */ |
| 789 | unsigned long rx_frame_errors; /* recv'd frame alignment error */ |
| 790 | unsigned long rx_fifo_errors; /* recv'r fifo overrun */ |
| 791 | unsigned long rx_missed_errors; /* receiver missed packet */ |
| 792 | /* detailed tx_errors */ |
| 793 | unsigned long tx_aborted_errors; |
| 794 | unsigned long tx_carrier_errors; |
| 795 | unsigned long tx_fifo_errors; |
| 796 | unsigned long tx_heartbeat_errors; |
| 797 | unsigned long tx_window_errors; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 798 | }; |
| 799 | |
| 800 | struct interface { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 801 | struct interface *next, *prev; |
| 802 | char name[IFNAMSIZ]; /* interface name */ |
| 803 | short type; /* if type */ |
| 804 | short flags; /* various flags */ |
| 805 | int metric; /* routing metric */ |
| 806 | int mtu; /* MTU value */ |
| 807 | int tx_queue_len; /* transmit queue length */ |
| 808 | struct ifmap map; /* hardware setup */ |
| 809 | struct sockaddr addr; /* IP address */ |
| 810 | struct sockaddr dstaddr; /* P-P IP address */ |
| 811 | struct sockaddr broadaddr; /* IP broadcast address */ |
| 812 | struct sockaddr netmask; /* IP network mask */ |
| 813 | struct sockaddr ipxaddr_bb; /* IPX network address */ |
| 814 | struct sockaddr ipxaddr_sn; /* IPX network address */ |
| 815 | struct sockaddr ipxaddr_e3; /* IPX network address */ |
| 816 | struct sockaddr ipxaddr_e2; /* IPX network address */ |
| 817 | struct sockaddr ddpaddr; /* Appletalk DDP address */ |
| 818 | struct sockaddr ecaddr; /* Econet address */ |
| 819 | int has_ip; |
| 820 | int has_ipx_bb; |
| 821 | int has_ipx_sn; |
| 822 | int has_ipx_e3; |
| 823 | int has_ipx_e2; |
| 824 | int has_ax25; |
| 825 | int has_ddp; |
| 826 | int has_econet; |
| 827 | char hwaddr[32]; /* HW address */ |
| 828 | int statistics_valid; |
| 829 | struct user_net_device_stats stats; /* statistics */ |
| 830 | int keepalive; /* keepalive value for SLIP */ |
| 831 | int outfill; /* outfill value for SLIP */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 832 | }; |
| 833 | |
| 834 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 835 | int interface_opt_a = 0; /* show all interfaces */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 836 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 837 | #ifdef KEEP_UNUSED |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 838 | static int opt_i = 0; /* show the statistics */ |
| 839 | static int opt_v = 0; /* debugging output flag */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 840 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 841 | static int addr_family = 0; /* currently selected AF */ |
| 842 | #endif /* KEEP_UNUSED */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 843 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 844 | static struct interface *int_list, *int_last; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 845 | static int skfd = -1; /* generic raw socket desc. */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 846 | |
| 847 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 848 | static int sockets_open(int family) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 849 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 850 | struct aftype **aft; |
| 851 | int sfd = -1; |
| 852 | static int force = -1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 853 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 854 | if (force < 0) { |
| 855 | force = 0; |
| 856 | if (get_kernel_revision() < KRELEASE(2, 1, 0)) |
| 857 | force = 1; |
| 858 | if (access("/proc/net", R_OK)) |
| 859 | force = 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 860 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 861 | for (aft = aftypes; *aft; aft++) { |
| 862 | struct aftype *af = *aft; |
| 863 | int type = SOCK_DGRAM; |
| 864 | |
| 865 | if (af->af == AF_UNSPEC) |
| 866 | continue; |
| 867 | if (family && family != af->af) |
| 868 | continue; |
| 869 | if (af->fd != -1) { |
| 870 | sfd = af->fd; |
| 871 | continue; |
| 872 | } |
| 873 | /* Check some /proc file first to not stress kmod */ |
| 874 | if (!family && !force && af->flag_file) { |
| 875 | if (access(af->flag_file, R_OK)) |
| 876 | continue; |
| 877 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 878 | #if HAVE_AFNETROM |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 879 | if (af->af == AF_NETROM) |
| 880 | type = SOCK_SEQPACKET; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 881 | #endif |
| 882 | #if HAVE_AFX25 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 883 | if (af->af == AF_X25) |
| 884 | type = SOCK_SEQPACKET; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 885 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 886 | af->fd = socket(af->af, type, 0); |
| 887 | if (af->fd >= 0) |
| 888 | sfd = af->fd; |
| 889 | } |
| 890 | if (sfd < 0) |
| 891 | fprintf(stderr, _("No usable address families found.\n")); |
| 892 | return sfd; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | /* like strcmp(), but knows about numbers */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 896 | static int nstrcmp(const char *astr, const char *b) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 897 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 898 | const char *a = astr; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 899 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 900 | while (*a == *b) { |
| 901 | if (*a == '\0') |
| 902 | return 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 903 | a++; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 904 | b++; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 905 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 906 | if (isdigit(*a)) { |
| 907 | if (!isdigit(*b)) |
| 908 | return -1; |
| 909 | while (a > astr) { |
| 910 | a--; |
| 911 | if (!isdigit(*a)) { |
| 912 | a++; |
| 913 | break; |
| 914 | } |
| 915 | if (!isdigit(*b)) |
| 916 | return -1; |
| 917 | b--; |
| 918 | } |
| 919 | return atoi(a) > atoi(b) ? 1 : -1; |
| 920 | } |
| 921 | return *a - *b; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | static struct interface *add_interface(char *name) |
| 925 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 926 | struct interface *ife, **nextp, *new; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 927 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 928 | for (ife = int_last; ife; ife = ife->prev) { |
| 929 | int n = nstrcmp(ife->name, name); |
| 930 | |
| 931 | if (n == 0) |
| 932 | return ife; |
| 933 | if (n < 0) |
| 934 | break; |
| 935 | } |
| 936 | new(new); |
| 937 | safe_strncpy(new->name, name, IFNAMSIZ); |
| 938 | nextp = ife ? &ife->next : &int_list; |
| 939 | new->prev = ife; |
| 940 | new->next = *nextp; |
| 941 | if (new->next) |
| 942 | new->next->prev = new; |
| 943 | else |
| 944 | int_last = new; |
| 945 | *nextp = new; |
| 946 | return new; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | |
| 950 | static int if_readconf(void) |
| 951 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 952 | int numreqs = 30; |
| 953 | struct ifconf ifc; |
| 954 | struct ifreq *ifr; |
| 955 | int n, err = -1; |
| 956 | int skfd2; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 957 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 958 | /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets |
| 959 | (as of 2.1.128) */ |
| 960 | skfd2 = get_socket_for_af(AF_INET); |
| 961 | if (skfd2 < 0) { |
| 962 | fprintf(stderr, _("warning: no inet socket available: %s\n"), |
| 963 | strerror(errno)); |
| 964 | /* Try to soldier on with whatever socket we can get hold of. */ |
| 965 | skfd2 = sockets_open(0); |
| 966 | if (skfd2 < 0) |
| 967 | return -1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 968 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 969 | |
| 970 | ifc.ifc_buf = NULL; |
| 971 | for (;;) { |
| 972 | ifc.ifc_len = sizeof(struct ifreq) * numreqs; |
| 973 | ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len); |
| 974 | |
| 975 | if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) { |
| 976 | perror("SIOCGIFCONF"); |
| 977 | goto out; |
| 978 | } |
| 979 | if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) { |
| 980 | /* assume it overflowed and try again */ |
| 981 | numreqs += 10; |
| 982 | continue; |
| 983 | } |
| 984 | break; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 985 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 986 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 987 | ifr = ifc.ifc_req; |
| 988 | for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) { |
| 989 | add_interface(ifr->ifr_name); |
| 990 | ifr++; |
| 991 | } |
| 992 | err = 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 993 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 994 | out: |
| 995 | free(ifc.ifc_buf); |
| 996 | return err; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | static char *get_name(char *name, char *p) |
| 1000 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1001 | while (isspace(*p)) |
| 1002 | p++; |
| 1003 | while (*p) { |
| 1004 | if (isspace(*p)) |
| 1005 | break; |
| 1006 | if (*p == ':') { /* could be an alias */ |
| 1007 | char *dot = p, *dotname = name; |
| 1008 | |
| 1009 | *name++ = *p++; |
| 1010 | while (isdigit(*p)) |
| 1011 | *name++ = *p++; |
| 1012 | if (*p != ':') { /* it wasn't, backup */ |
| 1013 | p = dot; |
| 1014 | name = dotname; |
| 1015 | } |
| 1016 | if (*p == '\0') |
| 1017 | return NULL; |
| 1018 | p++; |
| 1019 | break; |
| 1020 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1021 | *name++ = *p++; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1022 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1023 | *name++ = '\0'; |
| 1024 | return p; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | static int get_dev_fields(char *bp, struct interface *ife) |
| 1028 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1029 | switch (procnetdev_vsn) { |
| 1030 | case 3: |
| 1031 | sscanf(bp, |
| 1032 | "%Lu %Lu %lu %lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu %lu", |
| 1033 | &ife->stats.rx_bytes, |
| 1034 | &ife->stats.rx_packets, |
| 1035 | &ife->stats.rx_errors, |
| 1036 | &ife->stats.rx_dropped, |
| 1037 | &ife->stats.rx_fifo_errors, |
| 1038 | &ife->stats.rx_frame_errors, |
| 1039 | &ife->stats.rx_compressed, |
| 1040 | &ife->stats.rx_multicast, |
| 1041 | &ife->stats.tx_bytes, |
| 1042 | &ife->stats.tx_packets, |
| 1043 | &ife->stats.tx_errors, |
| 1044 | &ife->stats.tx_dropped, |
| 1045 | &ife->stats.tx_fifo_errors, |
| 1046 | &ife->stats.collisions, |
| 1047 | &ife->stats.tx_carrier_errors, &ife->stats.tx_compressed); |
| 1048 | break; |
| 1049 | case 2: |
| 1050 | sscanf(bp, "%Lu %Lu %lu %lu %lu %lu %Lu %Lu %lu %lu %lu %lu %lu", |
| 1051 | &ife->stats.rx_bytes, |
| 1052 | &ife->stats.rx_packets, |
| 1053 | &ife->stats.rx_errors, |
| 1054 | &ife->stats.rx_dropped, |
| 1055 | &ife->stats.rx_fifo_errors, |
| 1056 | &ife->stats.rx_frame_errors, |
| 1057 | &ife->stats.tx_bytes, |
| 1058 | &ife->stats.tx_packets, |
| 1059 | &ife->stats.tx_errors, |
| 1060 | &ife->stats.tx_dropped, |
| 1061 | &ife->stats.tx_fifo_errors, |
| 1062 | &ife->stats.collisions, &ife->stats.tx_carrier_errors); |
| 1063 | ife->stats.rx_multicast = 0; |
| 1064 | break; |
| 1065 | case 1: |
| 1066 | sscanf(bp, "%Lu %lu %lu %lu %lu %Lu %lu %lu %lu %lu %lu", |
| 1067 | &ife->stats.rx_packets, |
| 1068 | &ife->stats.rx_errors, |
| 1069 | &ife->stats.rx_dropped, |
| 1070 | &ife->stats.rx_fifo_errors, |
| 1071 | &ife->stats.rx_frame_errors, |
| 1072 | &ife->stats.tx_packets, |
| 1073 | &ife->stats.tx_errors, |
| 1074 | &ife->stats.tx_dropped, |
| 1075 | &ife->stats.tx_fifo_errors, |
| 1076 | &ife->stats.collisions, &ife->stats.tx_carrier_errors); |
| 1077 | ife->stats.rx_bytes = 0; |
| 1078 | ife->stats.tx_bytes = 0; |
| 1079 | ife->stats.rx_multicast = 0; |
| 1080 | break; |
| 1081 | } |
| 1082 | return 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1085 | static inline int procnetdev_version(char *buf) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1086 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1087 | if (strstr(buf, "compressed")) |
| 1088 | return 3; |
| 1089 | if (strstr(buf, "bytes")) |
| 1090 | return 2; |
| 1091 | return 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | static int if_readlist_proc(char *target) |
| 1095 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1096 | static int proc_read; |
| 1097 | FILE *fh; |
| 1098 | char buf[512]; |
| 1099 | struct interface *ife; |
| 1100 | int err; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1101 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1102 | if (proc_read) |
| 1103 | return 0; |
| 1104 | if (!target) |
| 1105 | proc_read = 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1106 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1107 | fh = fopen(_PATH_PROCNET_DEV, "r"); |
| 1108 | if (!fh) { |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1109 | fprintf(stderr, _("Warning: cannot open %s (%s). Limited output.\n"), |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1110 | _PATH_PROCNET_DEV, strerror(errno)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1111 | return if_readconf(); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1112 | } |
| 1113 | fgets(buf, sizeof buf, fh); /* eat line */ |
| 1114 | fgets(buf, sizeof buf, fh); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1115 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1116 | #if 0 /* pretty, but can't cope with missing fields */ |
| 1117 | fmt = proc_gen_fmt(_PATH_PROCNET_DEV, 1, fh, "face", "", /* parsed separately */ |
| 1118 | "bytes", "%lu", |
| 1119 | "packets", "%lu", |
| 1120 | "errs", "%lu", |
| 1121 | "drop", "%lu", |
| 1122 | "fifo", "%lu", |
| 1123 | "frame", "%lu", |
| 1124 | "compressed", "%lu", |
| 1125 | "multicast", "%lu", |
| 1126 | "bytes", "%lu", |
| 1127 | "packets", "%lu", |
| 1128 | "errs", "%lu", |
| 1129 | "drop", "%lu", |
| 1130 | "fifo", "%lu", |
| 1131 | "colls", "%lu", |
| 1132 | "carrier", "%lu", "compressed", "%lu", NULL); |
| 1133 | if (!fmt) |
| 1134 | return -1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1135 | #else |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1136 | procnetdev_vsn = procnetdev_version(buf); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1137 | #endif |
| 1138 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1139 | err = 0; |
| 1140 | while (fgets(buf, sizeof buf, fh)) { |
| 1141 | char *s, name[IFNAMSIZ]; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1142 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1143 | s = get_name(name, buf); |
| 1144 | ife = add_interface(name); |
| 1145 | get_dev_fields(s, ife); |
| 1146 | ife->statistics_valid = 1; |
| 1147 | if (target && !strcmp(target, name)) |
| 1148 | break; |
| 1149 | } |
| 1150 | if (ferror(fh)) { |
| 1151 | perror(_PATH_PROCNET_DEV); |
| 1152 | err = -1; |
| 1153 | proc_read = 0; |
| 1154 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1155 | #if 0 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1156 | free(fmt); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1157 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1158 | fclose(fh); |
| 1159 | return err; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1162 | static int if_readlist(void) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1163 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1164 | int err = if_readlist_proc(NULL); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1165 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1166 | if (!err) |
| 1167 | err = if_readconf(); |
| 1168 | return err; |
| 1169 | } |
| 1170 | |
| 1171 | static int for_all_interfaces(int (*doit) (struct interface *, void *), |
| 1172 | void *cookie) |
| 1173 | { |
| 1174 | struct interface *ife; |
| 1175 | |
| 1176 | if (!int_list && (if_readlist() < 0)) |
| 1177 | return -1; |
| 1178 | for (ife = int_list; ife; ife = ife->next) { |
| 1179 | int err = doit(ife, cookie); |
| 1180 | |
| 1181 | if (err) |
| 1182 | return err; |
| 1183 | } |
| 1184 | return 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | /* Support for fetching an IPX address */ |
| 1188 | |
| 1189 | #if HAVE_AFIPX |
| 1190 | static int ipx_getaddr(int sock, int ft, struct ifreq *ifr) |
| 1191 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1192 | ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft; |
| 1193 | return ioctl(sock, SIOCGIFADDR, ifr); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1194 | } |
| 1195 | #endif |
| 1196 | |
| 1197 | |
| 1198 | /* Fetch the interface configuration from the kernel. */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1199 | static int if_fetch(struct interface *ife) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1200 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1201 | struct ifreq ifr; |
| 1202 | int fd; |
| 1203 | char *ifname = ife->name; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1204 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1205 | strcpy(ifr.ifr_name, ifname); |
| 1206 | if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) |
| 1207 | return (-1); |
| 1208 | ife->flags = ifr.ifr_flags; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1209 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1210 | strcpy(ifr.ifr_name, ifname); |
| 1211 | if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0) |
| 1212 | memset(ife->hwaddr, 0, 32); |
| 1213 | else |
| 1214 | memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1215 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1216 | ife->type = ifr.ifr_hwaddr.sa_family; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1217 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1218 | strcpy(ifr.ifr_name, ifname); |
| 1219 | if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0) |
| 1220 | ife->metric = 0; |
| 1221 | else |
| 1222 | ife->metric = ifr.ifr_metric; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1223 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1224 | strcpy(ifr.ifr_name, ifname); |
| 1225 | if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0) |
| 1226 | ife->mtu = 0; |
| 1227 | else |
| 1228 | ife->mtu = ifr.ifr_mtu; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1229 | |
| 1230 | #ifdef HAVE_HWSLIP |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1231 | if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP || |
| 1232 | ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 || |
| 1233 | ife->type == ARPHRD_ADAPT) { |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1234 | #ifdef SIOCGOUTFILL |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1235 | strcpy(ifr.ifr_name, ifname); |
| 1236 | if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0) |
| 1237 | ife->outfill = 0; |
| 1238 | else |
| 1239 | ife->outfill = (unsigned int) ifr.ifr_data; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1240 | #endif |
| 1241 | #ifdef SIOCGKEEPALIVE |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1242 | strcpy(ifr.ifr_name, ifname); |
| 1243 | if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0) |
| 1244 | ife->keepalive = 0; |
| 1245 | else |
| 1246 | ife->keepalive = (unsigned int) ifr.ifr_data; |
| 1247 | #endif |
| 1248 | } |
| 1249 | #endif |
| 1250 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1251 | strcpy(ifr.ifr_name, ifname); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1252 | if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0) |
| 1253 | memset(&ife->map, 0, sizeof(struct ifmap)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1254 | else |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1255 | memcpy(&ife->map, &ifr.ifr_map, sizeof(struct ifmap)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1256 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1257 | strcpy(ifr.ifr_name, ifname); |
| 1258 | if (ioctl(skfd, SIOCGIFMAP, &ifr) < 0) |
| 1259 | memset(&ife->map, 0, sizeof(struct ifmap)); |
| 1260 | else |
| 1261 | ife->map = ifr.ifr_map; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1262 | |
| 1263 | #ifdef HAVE_TXQUEUELEN |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1264 | strcpy(ifr.ifr_name, ifname); |
| 1265 | if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0) |
| 1266 | ife->tx_queue_len = -1; /* unknown value */ |
| 1267 | else |
| 1268 | ife->tx_queue_len = ifr.ifr_qlen; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1269 | #else |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1270 | ife->tx_queue_len = -1; /* unknown value */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1271 | #endif |
| 1272 | |
| 1273 | #if HAVE_AFINET |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1274 | /* IPv4 address? */ |
| 1275 | fd = get_socket_for_af(AF_INET); |
| 1276 | if (fd >= 0) { |
| 1277 | strcpy(ifr.ifr_name, ifname); |
| 1278 | ifr.ifr_addr.sa_family = AF_INET; |
| 1279 | if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) { |
| 1280 | ife->has_ip = 1; |
| 1281 | ife->addr = ifr.ifr_addr; |
| 1282 | strcpy(ifr.ifr_name, ifname); |
| 1283 | if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0) |
| 1284 | memset(&ife->dstaddr, 0, sizeof(struct sockaddr)); |
| 1285 | else |
| 1286 | ife->dstaddr = ifr.ifr_dstaddr; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1287 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1288 | strcpy(ifr.ifr_name, ifname); |
| 1289 | if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0) |
| 1290 | memset(&ife->broadaddr, 0, sizeof(struct sockaddr)); |
| 1291 | else |
| 1292 | ife->broadaddr = ifr.ifr_broadaddr; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1293 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1294 | strcpy(ifr.ifr_name, ifname); |
| 1295 | if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0) |
| 1296 | memset(&ife->netmask, 0, sizeof(struct sockaddr)); |
| 1297 | else |
| 1298 | ife->netmask = ifr.ifr_netmask; |
| 1299 | } else |
| 1300 | memset(&ife->addr, 0, sizeof(struct sockaddr)); |
| 1301 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1302 | #endif |
| 1303 | |
| 1304 | #if HAVE_AFATALK |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1305 | /* DDP address maybe ? */ |
| 1306 | fd = get_socket_for_af(AF_APPLETALK); |
| 1307 | if (fd >= 0) { |
| 1308 | strcpy(ifr.ifr_name, ifname); |
| 1309 | if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) { |
| 1310 | ife->ddpaddr = ifr.ifr_addr; |
| 1311 | ife->has_ddp = 1; |
| 1312 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1313 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1314 | #endif |
| 1315 | |
| 1316 | #if HAVE_AFIPX |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1317 | /* Look for IPX addresses with all framing types */ |
| 1318 | fd = get_socket_for_af(AF_IPX); |
| 1319 | if (fd >= 0) { |
| 1320 | strcpy(ifr.ifr_name, ifname); |
| 1321 | if (!ipx_getaddr(fd, IPX_FRAME_ETHERII, &ifr)) { |
| 1322 | ife->has_ipx_bb = 1; |
| 1323 | ife->ipxaddr_bb = ifr.ifr_addr; |
| 1324 | } |
| 1325 | strcpy(ifr.ifr_name, ifname); |
| 1326 | if (!ipx_getaddr(fd, IPX_FRAME_SNAP, &ifr)) { |
| 1327 | ife->has_ipx_sn = 1; |
| 1328 | ife->ipxaddr_sn = ifr.ifr_addr; |
| 1329 | } |
| 1330 | strcpy(ifr.ifr_name, ifname); |
| 1331 | if (!ipx_getaddr(fd, IPX_FRAME_8023, &ifr)) { |
| 1332 | ife->has_ipx_e3 = 1; |
| 1333 | ife->ipxaddr_e3 = ifr.ifr_addr; |
| 1334 | } |
| 1335 | strcpy(ifr.ifr_name, ifname); |
| 1336 | if (!ipx_getaddr(fd, IPX_FRAME_8022, &ifr)) { |
| 1337 | ife->has_ipx_e2 = 1; |
| 1338 | ife->ipxaddr_e2 = ifr.ifr_addr; |
| 1339 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1340 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1341 | #endif |
| 1342 | |
| 1343 | #if HAVE_AFECONET |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1344 | /* Econet address maybe? */ |
| 1345 | fd = get_socket_for_af(AF_ECONET); |
| 1346 | if (fd >= 0) { |
| 1347 | strcpy(ifr.ifr_name, ifname); |
| 1348 | if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) { |
| 1349 | ife->ecaddr = ifr.ifr_addr; |
| 1350 | ife->has_econet = 1; |
| 1351 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1352 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1353 | #endif |
| 1354 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1355 | return 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1359 | static int do_if_fetch(struct interface *ife) |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1360 | { |
| 1361 | if (if_fetch(ife) < 0) { |
| 1362 | char *errmsg; |
| 1363 | |
| 1364 | if (errno == ENODEV) { |
| 1365 | /* Give better error message for this case. */ |
| 1366 | errmsg = _("Device not found"); |
| 1367 | } else { |
| 1368 | errmsg = strerror(errno); |
| 1369 | } |
| 1370 | fprintf(stderr, _("%s: error fetching interface information: %s\n"), |
| 1371 | ife->name, errmsg); |
| 1372 | return -1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1373 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1374 | return 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | /* This structure defines hardware protocols and their handlers. */ |
| 1378 | struct hwtype { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1379 | const char *name; |
| 1380 | const char *title; |
| 1381 | int type; |
| 1382 | int alen; |
| 1383 | char *(*print) (unsigned char *); |
| 1384 | int (*input) (char *, struct sockaddr *); |
| 1385 | int (*activate) (int fd); |
| 1386 | int suppress_null_addr; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1387 | }; |
| 1388 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1389 | static struct hwtype unspec_hwtype = { |
| 1390 | "unspec", "UNSPEC", -1, 0, |
| 1391 | UNSPEC_print, NULL, NULL |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1392 | }; |
| 1393 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1394 | static struct hwtype loop_hwtype = { |
| 1395 | "loop", "Local Loopback", ARPHRD_LOOPBACK, 0, |
| 1396 | NULL, NULL, NULL |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1397 | }; |
| 1398 | |
| 1399 | #if HAVE_HWETHER |
| 1400 | #include <net/if_arp.h> |
| 1401 | #include <linux/if_ether.h> |
| 1402 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1403 | /* Display an Ethernet address in readable format. */ |
| 1404 | static char *pr_ether(unsigned char *ptr) |
| 1405 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1406 | static char buff[64]; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1407 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1408 | snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X", |
| 1409 | (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377), |
| 1410 | (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377) |
| 1411 | ); |
| 1412 | return (buff); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1415 | #ifdef KEEP_UNUSED |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1416 | /* Input an Ethernet address and convert to binary. */ |
| 1417 | static int in_ether(char *bufp, struct sockaddr *sap) |
| 1418 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1419 | unsigned char *ptr; |
| 1420 | char c, *orig; |
| 1421 | int i; |
| 1422 | unsigned val; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1423 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1424 | sap->sa_family = ether_hwtype.type; |
| 1425 | ptr = sap->sa_data; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1426 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1427 | i = 0; |
| 1428 | orig = bufp; |
| 1429 | while ((*bufp != '\0') && (i < ETH_ALEN)) { |
| 1430 | val = 0; |
| 1431 | c = *bufp++; |
| 1432 | if (isdigit(c)) |
| 1433 | val = c - '0'; |
| 1434 | else if (c >= 'a' && c <= 'f') |
| 1435 | val = c - 'a' + 10; |
| 1436 | else if (c >= 'A' && c <= 'F') |
| 1437 | val = c - 'A' + 10; |
| 1438 | else { |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1439 | #ifdef DEBUG |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1440 | fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), |
| 1441 | orig); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1442 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1443 | errno = EINVAL; |
| 1444 | return (-1); |
| 1445 | } |
| 1446 | val <<= 4; |
| 1447 | c = *bufp; |
| 1448 | if (isdigit(c)) |
| 1449 | val |= c - '0'; |
| 1450 | else if (c >= 'a' && c <= 'f') |
| 1451 | val |= c - 'a' + 10; |
| 1452 | else if (c >= 'A' && c <= 'F') |
| 1453 | val |= c - 'A' + 10; |
| 1454 | else if (c == ':' || c == 0) |
| 1455 | val >>= 4; |
| 1456 | else { |
| 1457 | #ifdef DEBUG |
| 1458 | fprintf(stderr, _("in_ether(%s): invalid ether address!\n"), |
| 1459 | orig); |
| 1460 | #endif |
| 1461 | errno = EINVAL; |
| 1462 | return (-1); |
| 1463 | } |
| 1464 | if (c != 0) |
| 1465 | bufp++; |
| 1466 | *ptr++ = (unsigned char) (val & 0377); |
| 1467 | i++; |
| 1468 | |
| 1469 | /* We might get a semicolon here - not required. */ |
| 1470 | if (*bufp == ':') { |
| 1471 | if (i == ETH_ALEN) { |
| 1472 | #ifdef DEBUG |
| 1473 | fprintf(stderr, _("in_ether(%s): trailing : ignored!\n"), |
| 1474 | orig) |
| 1475 | #endif |
| 1476 | ; /* nothing */ |
| 1477 | } |
| 1478 | bufp++; |
| 1479 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1480 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1481 | |
| 1482 | /* That's it. Any trailing junk? */ |
| 1483 | if ((i == ETH_ALEN) && (*bufp != '\0')) { |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1484 | #ifdef DEBUG |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1485 | fprintf(stderr, _("in_ether(%s): trailing junk!\n"), orig); |
| 1486 | errno = EINVAL; |
| 1487 | return (-1); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1488 | #endif |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1489 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1490 | #ifdef DEBUG |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1491 | fprintf(stderr, "in_ether(%s): %s\n", orig, pr_ether(sap->sa_data)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1492 | #endif |
| 1493 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1494 | return (0); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1495 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1496 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1497 | |
| 1498 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1499 | static struct hwtype ether_hwtype = { |
| 1500 | "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN, |
| 1501 | pr_ether, NULL /* UNUSED in_ether */ , NULL |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1502 | }; |
| 1503 | |
| 1504 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1505 | #endif /* HAVE_HWETHER */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1506 | |
| 1507 | |
| 1508 | #if HAVE_HWPPP |
| 1509 | |
| 1510 | #include <net/if_arp.h> |
| 1511 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1512 | #ifdef KEEP_UNUSED |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1513 | /* Start the PPP encapsulation on the file descriptor. */ |
| 1514 | static int do_ppp(int fd) |
| 1515 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1516 | fprintf(stderr, _("You cannot start PPP with this program.\n")); |
| 1517 | return -1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1518 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1519 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1520 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1521 | static struct hwtype ppp_hwtype = { |
| 1522 | "ppp", "Point-Point Protocol", ARPHRD_PPP, 0, |
| 1523 | NULL, NULL, NULL /* UNUSED do_ppp */ , 0 |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1524 | }; |
| 1525 | |
| 1526 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1527 | #endif /* HAVE_PPP */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1528 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1529 | static struct hwtype *hwtypes[] = { |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1530 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1531 | &loop_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1532 | |
| 1533 | #if HAVE_HWSLIP |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1534 | &slip_hwtype, |
| 1535 | &cslip_hwtype, |
| 1536 | &slip6_hwtype, |
| 1537 | &cslip6_hwtype, |
| 1538 | &adaptive_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1539 | #endif |
| 1540 | #if HAVE_HWSTRIP |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1541 | &strip_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1542 | #endif |
| 1543 | #if HAVE_HWASH |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1544 | &ash_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1545 | #endif |
| 1546 | #if HAVE_HWETHER |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1547 | ðer_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1548 | #endif |
| 1549 | #if HAVE_HWTR |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1550 | &tr_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1551 | #ifdef ARPHRD_IEEE802_TR |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1552 | &tr_hwtype1, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1553 | #endif |
| 1554 | #endif |
| 1555 | #if HAVE_HWAX25 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1556 | &ax25_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1557 | #endif |
| 1558 | #if HAVE_HWNETROM |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1559 | &netrom_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1560 | #endif |
| 1561 | #if HAVE_HWROSE |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1562 | &rose_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1563 | #endif |
| 1564 | #if HAVE_HWTUNNEL |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1565 | &tunnel_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1566 | #endif |
| 1567 | #if HAVE_HWPPP |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1568 | &ppp_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1569 | #endif |
| 1570 | #if HAVE_HWHDLCLAPB |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1571 | &hdlc_hwtype, |
| 1572 | &lapb_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1573 | #endif |
| 1574 | #if HAVE_HWARC |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1575 | &arcnet_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1576 | #endif |
| 1577 | #if HAVE_HWFR |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1578 | &dlci_hwtype, |
| 1579 | &frad_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1580 | #endif |
| 1581 | #if HAVE_HWSIT |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1582 | &sit_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1583 | #endif |
| 1584 | #if HAVE_HWFDDI |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1585 | &fddi_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1586 | #endif |
| 1587 | #if HAVE_HWHIPPI |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1588 | &hippi_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1589 | #endif |
| 1590 | #if HAVE_HWIRDA |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1591 | &irda_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1592 | #endif |
| 1593 | #if HAVE_HWEC |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1594 | &ec_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1595 | #endif |
| 1596 | #if HAVE_HWX25 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1597 | &x25_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1598 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1599 | &unspec_hwtype, |
| 1600 | NULL |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1601 | }; |
| 1602 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1603 | #ifdef KEEP_UNUSED |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1604 | static short sVhwinit = 0; |
| 1605 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1606 | static void hwinit() |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1607 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1608 | loop_hwtype.title = _("Local Loopback"); |
| 1609 | unspec_hwtype.title = _("UNSPEC"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1610 | #if HAVE_HWSLIP |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1611 | slip_hwtype.title = _("Serial Line IP"); |
| 1612 | cslip_hwtype.title = _("VJ Serial Line IP"); |
| 1613 | slip6_hwtype.title = _("6-bit Serial Line IP"); |
| 1614 | cslip6_hwtype.title = _("VJ 6-bit Serial Line IP"); |
| 1615 | adaptive_hwtype.title = _("Adaptive Serial Line IP"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1616 | #endif |
| 1617 | #if HAVE_HWETHER |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1618 | ether_hwtype.title = _("Ethernet"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1619 | #endif |
| 1620 | #if HAVE_HWASH |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1621 | ash_hwtype.title = _("Ash"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1622 | #endif |
| 1623 | #if HAVE_HWFDDI |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1624 | fddi_hwtype.title = _("Fiber Distributed Data Interface"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1625 | #endif |
| 1626 | #if HAVE_HWHIPPI |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1627 | hippi_hwtype.title = _("HIPPI"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1628 | #endif |
| 1629 | #if HAVE_HWAX25 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1630 | ax25_hwtype.title = _("AMPR AX.25"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1631 | #endif |
| 1632 | #if HAVE_HWROSE |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1633 | rose_hwtype.title = _("AMPR ROSE"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1634 | #endif |
| 1635 | #if HAVE_HWNETROM |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1636 | netrom_hwtype.title = _("AMPR NET/ROM"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1637 | #endif |
| 1638 | #if HAVE_HWX25 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1639 | x25_hwtype.title = _("generic X.25"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1640 | #endif |
| 1641 | #if HAVE_HWTUNNEL |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1642 | tunnel_hwtype.title = _("IPIP Tunnel"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1643 | #endif |
| 1644 | #if HAVE_HWPPP |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1645 | ppp_hwtype.title = _("Point-to-Point Protocol"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1646 | #endif |
| 1647 | #if HAVE_HWHDLCLAPB |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1648 | hdlc_hwtype.title = _("(Cisco)-HDLC"); |
| 1649 | lapb_hwtype.title = _("LAPB"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1650 | #endif |
| 1651 | #if HAVE_HWARC |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1652 | arcnet_hwtype.title = _("ARCnet"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1653 | #endif |
| 1654 | #if HAVE_HWFR |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1655 | dlci_hwtype.title = _("Frame Relay DLCI"); |
| 1656 | frad_hwtype.title = _("Frame Relay Access Device"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1657 | #endif |
| 1658 | #if HAVE_HWSIT |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1659 | sit_hwtype.title = _("IPv6-in-IPv4"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1660 | #endif |
| 1661 | #if HAVE_HWIRDA |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1662 | irda_hwtype.title = _("IrLAP"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1663 | #endif |
| 1664 | #if HAVE_HWTR |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1665 | tr_hwtype.title = _("16/4 Mbps Token Ring"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1666 | #ifdef ARPHRD_IEEE802_TR |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1667 | tr_hwtype1.title = _("16/4 Mbps Token Ring (New)"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1668 | #endif |
| 1669 | #endif |
| 1670 | #if HAVE_HWEC |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1671 | ec_hwtype.title = _("Econet"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1672 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1673 | sVhwinit = 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1674 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1675 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1676 | |
| 1677 | #ifdef IFF_PORTSEL |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1678 | static const char *if_port_text[][4] = { |
| 1679 | /* Keep in step with <linux/netdevice.h> */ |
| 1680 | {"unknown", NULL, NULL, NULL}, |
| 1681 | {"10base2", "bnc", "coax", NULL}, |
| 1682 | {"10baseT", "utp", "tpe", NULL}, |
| 1683 | {"AUI", "thick", "db15", NULL}, |
| 1684 | {"100baseT", NULL, NULL, NULL}, |
| 1685 | {"100baseTX", NULL, NULL, NULL}, |
| 1686 | {"100baseFX", NULL, NULL, NULL}, |
| 1687 | {NULL, NULL, NULL, NULL}, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1688 | }; |
| 1689 | #endif |
| 1690 | |
| 1691 | /* Check our hardware type table for this type. */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1692 | static struct hwtype *get_hwntype(int type) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1693 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1694 | struct hwtype **hwp; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1695 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1696 | #ifdef KEEP_UNUSED |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1697 | if (!sVhwinit) |
| 1698 | hwinit(); |
| 1699 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1700 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1701 | hwp = hwtypes; |
| 1702 | while (*hwp != NULL) { |
| 1703 | if ((*hwp)->type == type) |
| 1704 | return (*hwp); |
| 1705 | hwp++; |
| 1706 | } |
| 1707 | return (NULL); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | /* return 1 if address is all zeros */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1711 | static int hw_null_address(struct hwtype *hw, void *ap) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1712 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1713 | unsigned int i; |
| 1714 | unsigned char *address = (unsigned char *) ap; |
| 1715 | |
| 1716 | for (i = 0; i < hw->alen; i++) |
| 1717 | if (address[i]) |
| 1718 | return 0; |
| 1719 | return 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1722 | static const char TRext[] = "\0\0k\0M"; |
| 1723 | |
| 1724 | static void print_bytes_scaled(unsigned long long ull, const char *end) |
| 1725 | { |
| 1726 | unsigned long long int_part; |
| 1727 | unsigned long frac_part; |
| 1728 | const char *ext; |
| 1729 | int i; |
| 1730 | |
| 1731 | frac_part = 0; |
| 1732 | ext = TRext; |
| 1733 | int_part = ull; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1734 | for (i = 0; i < 2; i++) { |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1735 | if (int_part >= 1024) { |
| 1736 | frac_part = ((int_part % 1024) * 10) / 1024; |
| 1737 | int_part /= 1024; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1738 | ext += 2; /* Kb, Mb */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1739 | } |
| 1740 | } |
| 1741 | |
Eric Andersen | 7365c58 | 2002-09-17 06:36:56 +0000 | [diff] [blame] | 1742 | printf("X bytes:%Lu (%Lu.%lu %siB)%s", ull, int_part, frac_part, ext, end); |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | static void ife_print(struct interface *ptr) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1746 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1747 | struct aftype *ap; |
| 1748 | struct hwtype *hw; |
| 1749 | int hf; |
| 1750 | int can_compress = 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1751 | |
| 1752 | #if HAVE_AFIPX |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1753 | static struct aftype *ipxtype = NULL; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1754 | #endif |
| 1755 | #if HAVE_AFECONET |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1756 | static struct aftype *ectype = NULL; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1757 | #endif |
| 1758 | #if HAVE_AFATALK |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1759 | static struct aftype *ddptype = NULL; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1760 | #endif |
| 1761 | #if HAVE_AFINET6 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1762 | FILE *f; |
| 1763 | char addr6[40], devname[20]; |
| 1764 | struct sockaddr_in6 sap; |
| 1765 | int plen, scope, dad_status, if_idx; |
| 1766 | char addr6p[8][5]; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1767 | #endif |
| 1768 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1769 | ap = get_afntype(ptr->addr.sa_family); |
| 1770 | if (ap == NULL) |
| 1771 | ap = get_afntype(0); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1772 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1773 | hf = ptr->type; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1774 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1775 | if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6) |
| 1776 | can_compress = 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1777 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1778 | hw = get_hwntype(hf); |
| 1779 | if (hw == NULL) |
| 1780 | hw = get_hwntype(-1); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1781 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1782 | printf(_("%-9.9s Link encap:%s "), ptr->name, _(hw->title)); |
| 1783 | /* For some hardware types (eg Ash, ATM) we don't print the |
| 1784 | hardware address if it's null. */ |
| 1785 | if (hw->print != NULL && (!(hw_null_address(hw, ptr->hwaddr) && |
| 1786 | hw->suppress_null_addr))) |
| 1787 | printf(_("HWaddr %s "), hw->print(ptr->hwaddr)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1788 | #ifdef IFF_PORTSEL |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1789 | if (ptr->flags & IFF_PORTSEL) { |
| 1790 | printf(_("Media:%s"), if_port_text[ptr->map.port][0]); |
| 1791 | if (ptr->flags & IFF_AUTOMEDIA) |
| 1792 | printf(_("(auto)")); |
| 1793 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1794 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1795 | printf("\n"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1796 | |
| 1797 | #if HAVE_AFINET |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1798 | if (ptr->has_ip) { |
| 1799 | printf(_(" %s addr:%s "), ap->name, |
| 1800 | ap->sprint(&ptr->addr, 1)); |
| 1801 | if (ptr->flags & IFF_POINTOPOINT) { |
| 1802 | printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1)); |
| 1803 | } |
| 1804 | if (ptr->flags & IFF_BROADCAST) { |
| 1805 | printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1)); |
| 1806 | } |
| 1807 | printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1808 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1809 | #endif |
| 1810 | |
| 1811 | #if HAVE_AFINET6 |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1812 | |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 1813 | #define IPV6_ADDR_ANY 0x0000U |
| 1814 | |
| 1815 | #define IPV6_ADDR_UNICAST 0x0001U |
| 1816 | #define IPV6_ADDR_MULTICAST 0x0002U |
| 1817 | #define IPV6_ADDR_ANYCAST 0x0004U |
| 1818 | |
| 1819 | #define IPV6_ADDR_LOOPBACK 0x0010U |
| 1820 | #define IPV6_ADDR_LINKLOCAL 0x0020U |
| 1821 | #define IPV6_ADDR_SITELOCAL 0x0040U |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1822 | |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 1823 | #define IPV6_ADDR_COMPATv4 0x0080U |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1824 | |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 1825 | #define IPV6_ADDR_SCOPE_MASK 0x00f0U |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1826 | |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 1827 | #define IPV6_ADDR_MAPPED 0x1000U |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1828 | #define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */ |
| 1829 | |
| 1830 | if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) { |
| 1831 | while (fscanf |
| 1832 | (f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n", |
| 1833 | addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4], |
| 1834 | addr6p[5], addr6p[6], addr6p[7], &if_idx, &plen, &scope, |
| 1835 | &dad_status, devname) != EOF) { |
| 1836 | if (!strcmp(devname, ptr->name)) { |
| 1837 | sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s", |
| 1838 | addr6p[0], addr6p[1], addr6p[2], addr6p[3], |
| 1839 | addr6p[4], addr6p[5], addr6p[6], addr6p[7]); |
| 1840 | inet_pton(AF_INET6, addr6, |
| 1841 | (struct sockaddr *) &sap.sin6_addr); |
| 1842 | sap.sin6_family = AF_INET6; |
| 1843 | printf(_(" inet6 addr: %s/%d"), |
| 1844 | inet6_aftype.sprint((struct sockaddr *) &sap, 1), |
| 1845 | plen); |
| 1846 | printf(_(" Scope:")); |
| 1847 | switch (scope & IPV6_ADDR_SCOPE_MASK) { |
| 1848 | case 0: |
| 1849 | printf(_("Global")); |
| 1850 | break; |
| 1851 | case IPV6_ADDR_LINKLOCAL: |
| 1852 | printf(_("Link")); |
| 1853 | break; |
| 1854 | case IPV6_ADDR_SITELOCAL: |
| 1855 | printf(_("Site")); |
| 1856 | break; |
| 1857 | case IPV6_ADDR_COMPATv4: |
| 1858 | printf(_("Compat")); |
| 1859 | break; |
| 1860 | case IPV6_ADDR_LOOPBACK: |
| 1861 | printf(_("Host")); |
| 1862 | break; |
| 1863 | default: |
| 1864 | printf(_("Unknown")); |
| 1865 | } |
| 1866 | printf("\n"); |
| 1867 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1868 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1869 | fclose(f); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1870 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1871 | #endif |
| 1872 | |
| 1873 | #if HAVE_AFIPX |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1874 | if (ipxtype == NULL) |
| 1875 | ipxtype = get_afntype(AF_IPX); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1876 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1877 | if (ipxtype != NULL) { |
| 1878 | if (ptr->has_ipx_bb) |
| 1879 | printf(_(" IPX/Ethernet II addr:%s\n"), |
| 1880 | ipxtype->sprint(&ptr->ipxaddr_bb, 1)); |
| 1881 | if (ptr->has_ipx_sn) |
| 1882 | printf(_(" IPX/Ethernet SNAP addr:%s\n"), |
| 1883 | ipxtype->sprint(&ptr->ipxaddr_sn, 1)); |
| 1884 | if (ptr->has_ipx_e2) |
| 1885 | printf(_(" IPX/Ethernet 802.2 addr:%s\n"), |
| 1886 | ipxtype->sprint(&ptr->ipxaddr_e2, 1)); |
| 1887 | if (ptr->has_ipx_e3) |
| 1888 | printf(_(" IPX/Ethernet 802.3 addr:%s\n"), |
| 1889 | ipxtype->sprint(&ptr->ipxaddr_e3, 1)); |
| 1890 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1891 | #endif |
| 1892 | |
| 1893 | #if HAVE_AFATALK |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1894 | if (ddptype == NULL) |
| 1895 | ddptype = get_afntype(AF_APPLETALK); |
| 1896 | if (ddptype != NULL) { |
| 1897 | if (ptr->has_ddp) |
| 1898 | printf(_(" EtherTalk Phase 2 addr:%s\n"), |
| 1899 | ddptype->sprint(&ptr->ddpaddr, 1)); |
| 1900 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1901 | #endif |
| 1902 | |
| 1903 | #if HAVE_AFECONET |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1904 | if (ectype == NULL) |
| 1905 | ectype = get_afntype(AF_ECONET); |
| 1906 | if (ectype != NULL) { |
| 1907 | if (ptr->has_econet) |
| 1908 | printf(_(" econet addr:%s\n"), |
| 1909 | ectype->sprint(&ptr->ecaddr, 1)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1910 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1911 | #endif |
| 1912 | |
| 1913 | printf(" "); |
| 1914 | /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */ |
| 1915 | if (ptr->flags == 0) |
| 1916 | printf(_("[NO FLAGS] ")); |
| 1917 | if (ptr->flags & IFF_UP) |
| 1918 | printf(_("UP ")); |
| 1919 | if (ptr->flags & IFF_BROADCAST) |
| 1920 | printf(_("BROADCAST ")); |
| 1921 | if (ptr->flags & IFF_DEBUG) |
| 1922 | printf(_("DEBUG ")); |
| 1923 | if (ptr->flags & IFF_LOOPBACK) |
| 1924 | printf(_("LOOPBACK ")); |
| 1925 | if (ptr->flags & IFF_POINTOPOINT) |
| 1926 | printf(_("POINTOPOINT ")); |
| 1927 | if (ptr->flags & IFF_NOTRAILERS) |
| 1928 | printf(_("NOTRAILERS ")); |
| 1929 | if (ptr->flags & IFF_RUNNING) |
| 1930 | printf(_("RUNNING ")); |
| 1931 | if (ptr->flags & IFF_NOARP) |
| 1932 | printf(_("NOARP ")); |
| 1933 | if (ptr->flags & IFF_PROMISC) |
| 1934 | printf(_("PROMISC ")); |
| 1935 | if (ptr->flags & IFF_ALLMULTI) |
| 1936 | printf(_("ALLMULTI ")); |
| 1937 | if (ptr->flags & IFF_SLAVE) |
| 1938 | printf(_("SLAVE ")); |
| 1939 | if (ptr->flags & IFF_MASTER) |
| 1940 | printf(_("MASTER ")); |
| 1941 | if (ptr->flags & IFF_MULTICAST) |
| 1942 | printf(_("MULTICAST ")); |
| 1943 | #ifdef HAVE_DYNAMIC |
| 1944 | if (ptr->flags & IFF_DYNAMIC) |
| 1945 | printf(_("DYNAMIC ")); |
| 1946 | #endif |
| 1947 | /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */ |
| 1948 | printf(_(" MTU:%d Metric:%d"), ptr->mtu, ptr->metric ? ptr->metric : 1); |
| 1949 | #ifdef SIOCSKEEPALIVE |
| 1950 | if (ptr->outfill || ptr->keepalive) |
| 1951 | printf(_(" Outfill:%d Keepalive:%d"), ptr->outfill, ptr->keepalive); |
| 1952 | #endif |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1953 | printf("\n"); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1954 | |
| 1955 | /* If needed, display the interface statistics. */ |
| 1956 | |
| 1957 | if (ptr->statistics_valid) { |
| 1958 | /* XXX: statistics are currently only printed for the primary address, |
| 1959 | * not for the aliases, although strictly speaking they're shared |
| 1960 | * by all addresses. |
| 1961 | */ |
| 1962 | printf(" "); |
| 1963 | |
| 1964 | printf(_ |
| 1965 | ("RX packets:%Lu errors:%lu dropped:%lu overruns:%lu frame:%lu\n"), |
| 1966 | ptr->stats.rx_packets, ptr->stats.rx_errors, |
| 1967 | ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors, |
| 1968 | ptr->stats.rx_frame_errors); |
| 1969 | if (can_compress) |
| 1970 | printf(_(" compressed:%lu\n"), |
| 1971 | ptr->stats.rx_compressed); |
| 1972 | printf(" "); |
| 1973 | printf(_ |
| 1974 | ("TX packets:%Lu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n"), |
| 1975 | ptr->stats.tx_packets, ptr->stats.tx_errors, |
| 1976 | ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors, |
| 1977 | ptr->stats.tx_carrier_errors); |
| 1978 | printf(_(" collisions:%lu "), ptr->stats.collisions); |
| 1979 | if (can_compress) |
| 1980 | printf(_("compressed:%lu "), ptr->stats.tx_compressed); |
| 1981 | if (ptr->tx_queue_len != -1) |
| 1982 | printf(_("txqueuelen:%d "), ptr->tx_queue_len); |
| 1983 | printf("\n R"); |
| 1984 | print_bytes_scaled(ptr->stats.rx_bytes, " T"); |
| 1985 | print_bytes_scaled(ptr->stats.tx_bytes, "\n"); |
| 1986 | |
| 1987 | } |
| 1988 | |
| 1989 | if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma || |
| 1990 | ptr->map.base_addr)) { |
| 1991 | printf(" "); |
| 1992 | if (ptr->map.irq) |
| 1993 | printf(_("Interrupt:%d "), ptr->map.irq); |
| 1994 | if (ptr->map.base_addr >= 0x100) /* Only print devices using it for |
| 1995 | I/O maps */ |
| 1996 | printf(_("Base address:0x%lx "), |
| 1997 | (unsigned long) ptr->map.base_addr); |
| 1998 | if (ptr->map.mem_start) { |
| 1999 | printf(_("Memory:%lx-%lx "), ptr->map.mem_start, |
| 2000 | ptr->map.mem_end); |
| 2001 | } |
| 2002 | if (ptr->map.dma) |
| 2003 | printf(_("DMA chan:%x "), ptr->map.dma); |
| 2004 | printf("\n"); |
| 2005 | } |
| 2006 | printf("\n"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 2010 | static int do_if_print(struct interface *ife, void *cookie) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 2011 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 2012 | int *opt_a = (int *) cookie; |
| 2013 | int res; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 2014 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 2015 | res = do_if_fetch(ife); |
| 2016 | if (res >= 0) { |
| 2017 | if ((ife->flags & IFF_UP) || *opt_a) |
| 2018 | ife_print(ife); |
| 2019 | } |
| 2020 | return res; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 2023 | static struct interface *lookup_interface(char *name) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 2024 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 2025 | struct interface *ife = NULL; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 2026 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 2027 | if (if_readlist_proc(name) < 0) |
| 2028 | return NULL; |
| 2029 | ife = add_interface(name); |
| 2030 | return ife; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | /* for ipv4 add/del modes */ |
| 2034 | static int if_print(char *ifname) |
| 2035 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 2036 | int res; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 2037 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 2038 | if (!ifname) { |
| 2039 | res = for_all_interfaces(do_if_print, &interface_opt_a); |
| 2040 | } else { |
| 2041 | struct interface *ife; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 2042 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 2043 | ife = lookup_interface(ifname); |
| 2044 | res = do_if_fetch(ife); |
| 2045 | if (res >= 0) |
| 2046 | ife_print(ife); |
| 2047 | } |
| 2048 | return res; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 2051 | int display_interfaces(char *ifname) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 2052 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 2053 | int status; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 2054 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 2055 | /* Create a channel to the NET kernel. */ |
| 2056 | if ((skfd = sockets_open(0)) < 0) { |
Manuel Novoa III | 78f5746 | 2001-03-10 02:00:54 +0000 | [diff] [blame] | 2057 | perror_msg_and_die("socket"); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 2058 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 2059 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 2060 | /* Do we have to show the current setup? */ |
| 2061 | status = if_print(ifname); |
| 2062 | close(skfd); |
| 2063 | exit(status < 0); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 2064 | } |