Skip to content

Instantly share code, notes, and snippets.

@Low-power
Created July 25, 2020 15:56
Show Gist options
  • Save Low-power/278b6b837173867e70ec03a728149262 to your computer and use it in GitHub Desktop.
Save Low-power/278b6b837173867e70ec03a728149262 to your computer and use it in GitHub Desktop.
Interface datalink layer configuration and statistics viewer for BSD
/* sysctl(3)-based ifconfig(8) implementation
Copyright 2015-2020 Rivoreo
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/socket.h>
#include <net/route.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <string.h>
#include <stdio.h>
#if defined __FreeBSD__ && !defined __FreeBSD_kernel__
#define __FreeBSD_kernel__ 1
#endif
#ifndef IFF_SMART
#define IFF_SMART 0x20
#endif
#ifndef IFF_DRV_RUNNING
#ifdef IFF_RUNNING
#define IFF_DRV_RUNNING IFF_RUNNING
#else
#define IFF_DRV_RUNNING 0x40
#endif
#endif
#ifndef IFF_DRV_OACTIVE
#ifdef IFF_OACTIVE
#define IFF_DRV_OACTIVE IFF_OACTIVE
#else
#define IFF_DRV_OACTIVE 0x400
#endif
#endif
#if defined __FreeBSD_kernel__ && __FreeBSD_version >= 1100070
#ifndef IFT_1822
#define IFT_1822 IFT_1822
#endif
#ifndef IFT_HDH1822
#define IFT_HDH1822 IFT_HDH1822
#endif
#ifndef IFT_X25DDN
#define IFT_X25DDN IFT_X25DDN
#endif
#ifndef IFT_X25
#define IFT_X25 IFT_X25
#endif
#ifndef IFT_ETHER
#define IFT_ETHER IFT_ETHER
#endif
#ifndef IFT_ISO88023
#define IFT_ISO88023 IFT_ISO88023
#endif
#ifndef IFT_ISO88024
#define IFT_ISO88024 IFT_ISO88024
#endif
#ifndef IFT_ISO88025
#define IFT_ISO88025 IFT_ISO88025
#endif
#ifndef IFT_ISO88026
#define IFT_ISO88026 IFT_ISO88026
#endif
#ifndef IFT_STARLAN
#define IFT_STARLAN IFT_STARLAN
#endif
#ifndef IFT_P10
#define IFT_P10 IFT_P10
#endif
#ifndef IFT_P80
#define IFT_P80 IFT_P80
#endif
#ifndef IFT_HY
#define IFT_HY IFT_HY
#endif
#ifndef IFT_FDDI
#define IFT_FDDI IFT_FDDI
#endif
#ifndef IFT_LAPB
#define IFT_LAPB IFT_LAPB
#endif
#ifndef IFT_SDLC
#define IFT_SDLC IFT_SDLC
#endif
#ifndef IFT_T1
#define IFT_T1 IFT_T1
#endif
#ifndef IFT_CEPT
#define IFT_CEPT IFT_CEPT
#endif
#ifndef IFT_ISDNBASIC
#define IFT_ISDNBASIC IFT_ISDNBASIC
#endif
#ifndef IFT_ISDNPRIMARY
#define IFT_ISDNPRIMARY IFT_ISDNPRIMARY
#endif
#ifndef IFT_PTPSERIAL
#define IFT_PTPSERIAL IFT_PTPSERIAL
#endif
#ifndef IFT_PPP
#define IFT_PPP IFT_PPP
#endif
#ifndef IFT_LOOP
#define IFT_LOOP IFT_LOOP
#endif
#ifndef IFT_EON
#define IFT_EON IFT_EON
#endif
#ifndef IFT_XETHER
#define IFT_XETHER IFT_XETHER
#endif
#ifndef IFT_NSIP
#define IFT_NSIP IFT_NSIP
#endif
#ifndef IFT_SLIP
#define IFT_SLIP IFT_SLIP
#endif
#ifndef IFT_ULTRA
#define IFT_ULTRA IFT_ULTRA
#endif
#ifndef IFT_DS3
#define IFT_DS3 IFT_DS3
#endif
#ifndef IFT_SIP
#define IFT_SIP IFT_SIP
#endif
#ifndef IFT_FRELAY
#define IFT_FRELAY IFT_FRELAY
#endif
#ifndef IFT_RS232
#define IFT_RS232 IFT_RS232
#endif
#ifndef IFT_PARA
#define IFT_PARA IFT_PARA
#endif
#ifndef IFT_ARCNET
#define IFT_ARCNET IFT_ARCNET
#endif
#ifndef IFT_ARCNETPLUS
#define IFT_ARCNETPLUS IFT_ARCNETPLUS
#endif
#ifndef IFT_ATM
#define IFT_ATM IFT_ATM
#endif
#ifndef IFT_MIOX25
#define IFT_MIOX25 IFT_MIOX25
#endif
#ifndef IFT_SONET
#define IFT_SONET IFT_SONET
#endif
#ifndef IFT_X25PLE
#define IFT_X25PLE IFT_X25PLE
#endif
#ifndef IFT_ISO88022LLC
#define IFT_ISO88022LLC IFT_ISO88022LLC
#endif
#ifndef IFT_LOCALTALK
#define IFT_LOCALTALK IFT_LOCALTALK
#endif
#ifndef IFT_SMDSDXI
#define IFT_SMDSDXI IFT_SMDSDXI
#endif
#ifndef IFT_FRELAYDCE
#define IFT_FRELAYDCE IFT_FRELAYDCE
#endif
#ifndef IFT_V35
#define IFT_V35 IFT_V35
#endif
#ifndef IFT_HSSI
#define IFT_HSSI IFT_HSSI
#endif
#ifndef IFT_HIPPI
#define IFT_HIPPI IFT_HIPPI
#endif
#ifndef IFT_MODEM
#define IFT_MODEM IFT_MODEM
#endif
#ifndef IFT_AAL5
#define IFT_AAL5 IFT_AAL5
#endif
#ifndef IFT_SONETPATH
#define IFT_SONETPATH IFT_SONETPATH
#endif
#ifndef IFT_SONETVT
#define IFT_SONETVT IFT_SONETVT
#endif
#ifndef IFT_SMDSICIP
#define IFT_SMDSICIP IFT_SMDSICIP
#endif
#ifndef IFT_PROPVIRTUAL
#define IFT_PROPVIRTUAL IFT_PROPVIRTUAL
#endif
#ifndef IFT_PROPMUX
#define IFT_PROPMUX IFT_PROPMUX
#endif
#ifndef IFT_IEEE80212
#define IFT_IEEE80212 IFT_IEEE80212
#endif
#ifndef IFT_FIBRECHANNEL
#define IFT_FIBRECHANNEL IFT_FIBRECHANNEL
#endif
#ifndef IFT_HIPPIINTERFACE
#define IFT_HIPPIINTERFACE IFT_HIPPIINTERFACE
#endif
#ifndef IFT_FRAMERELAYINTERCONNECT
#define IFT_FRAMERELAYINTERCONNECT IFT_FRAMERELAYINTERCONNECT
#endif
#ifndef IFT_AFLANE8023
#define IFT_AFLANE8023 IFT_AFLANE8023
#endif
#ifndef IFT_AFLANE8025
#define IFT_AFLANE8025 IFT_AFLANE8025
#endif
#ifndef IFT_CCTEMUL
#define IFT_CCTEMUL IFT_CCTEMUL
#endif
#ifndef IFT_FASTETHER
#define IFT_FASTETHER IFT_FASTETHER
#endif
#ifndef IFT_ISDN
#define IFT_ISDN IFT_ISDN
#endif
#ifndef IFT_V11
#define IFT_V11 IFT_V11
#endif
#ifndef IFT_V36
#define IFT_V36 IFT_V36
#endif
#ifndef IFT_G703AT64K
#define IFT_G703AT64K IFT_G703AT64K
#endif
#ifndef IFT_G703AT2MB
#define IFT_G703AT2MB IFT_G703AT2MB
#endif
#ifndef IFT_QLLC
#define IFT_QLLC IFT_QLLC
#endif
#ifndef IFT_FASTETHERFX
#define IFT_FASTETHERFX IFT_FASTETHERFX
#endif
#ifndef IFT_CHANNEL
#define IFT_CHANNEL IFT_CHANNEL
#endif
#ifndef IFT_IEEE80211
#define IFT_IEEE80211 IFT_IEEE80211
#endif
#ifndef IFT_IBM370PARCHAN
#define IFT_IBM370PARCHAN IFT_IBM370PARCHAN
#endif
#ifndef IFT_ESCON
#define IFT_ESCON IFT_ESCON
#endif
#ifndef IFT_DLSW
#define IFT_DLSW IFT_DLSW
#endif
#ifndef IFT_ISDNS
#define IFT_ISDNS IFT_ISDNS
#endif
#ifndef IFT_ISDNU
#define IFT_ISDNU IFT_ISDNU
#endif
#ifndef IFT_LAPD
#define IFT_LAPD IFT_LAPD
#endif
#ifndef IFT_IPSWITCH
#define IFT_IPSWITCH IFT_IPSWITCH
#endif
#ifndef IFT_RSRB
#define IFT_RSRB IFT_RSRB
#endif
#ifndef IFT_ATMLOGICAL
#define IFT_ATMLOGICAL IFT_ATMLOGICAL
#endif
#ifndef IFT_DS0
#define IFT_DS0 IFT_DS0
#endif
#ifndef IFT_DS0BUNDLE
#define IFT_DS0BUNDLE IFT_DS0BUNDLE
#endif
#ifndef IFT_BSC
#define IFT_BSC IFT_BSC
#endif
#ifndef IFT_ASYNC
#define IFT_ASYNC IFT_ASYNC
#endif
#ifndef IFT_CNR
#define IFT_CNR IFT_CNR
#endif
#ifndef IFT_ISO88025DTR
#define IFT_ISO88025DTR IFT_ISO88025DTR
#endif
#ifndef IFT_EPLRS
#define IFT_EPLRS IFT_EPLRS
#endif
#ifndef IFT_ARAP
#define IFT_ARAP IFT_ARAP
#endif
#ifndef IFT_PROPCNLS
#define IFT_PROPCNLS IFT_PROPCNLS
#endif
#ifndef IFT_HOSTPAD
#define IFT_HOSTPAD IFT_HOSTPAD
#endif
#ifndef IFT_TERMPAD
#define IFT_TERMPAD IFT_TERMPAD
#endif
#ifndef IFT_FRAMERELAYMPI
#define IFT_FRAMERELAYMPI IFT_FRAMERELAYMPI
#endif
#ifndef IFT_X213
#define IFT_X213 IFT_X213
#endif
#ifndef IFT_ADSL
#define IFT_ADSL IFT_ADSL
#endif
#ifndef IFT_RADSL
#define IFT_RADSL IFT_RADSL
#endif
#ifndef IFT_SDSL
#define IFT_SDSL IFT_SDSL
#endif
#ifndef IFT_VDSL
#define IFT_VDSL IFT_VDSL
#endif
#ifndef IFT_ISO88025CRFPINT
#define IFT_ISO88025CRFPINT IFT_ISO88025CRFPINT
#endif
#ifndef IFT_MYRINET
#define IFT_MYRINET IFT_MYRINET
#endif
#ifndef IFT_VOICEEM
#define IFT_VOICEEM IFT_VOICEEM
#endif
#ifndef IFT_VOICEFXO
#define IFT_VOICEFXO IFT_VOICEFXO
#endif
#ifndef IFT_VOICEFXS
#define IFT_VOICEFXS IFT_VOICEFXS
#endif
#ifndef IFT_VOICEENCAP
#define IFT_VOICEENCAP IFT_VOICEENCAP
#endif
#ifndef IFT_VOICEOVERIP
#define IFT_VOICEOVERIP IFT_VOICEOVERIP
#endif
#ifndef IFT_ATMDXI
#define IFT_ATMDXI IFT_ATMDXI
#endif
#ifndef IFT_ATMFUNI
#define IFT_ATMFUNI IFT_ATMFUNI
#endif
#ifndef IFT_ATMIMA
#define IFT_ATMIMA IFT_ATMIMA
#endif
#ifndef IFT_PPPMULTILINKBUNDLE
#define IFT_PPPMULTILINKBUNDLE IFT_PPPMULTILINKBUNDLE
#endif
#ifndef IFT_IPOVERCDLC
#define IFT_IPOVERCDLC IFT_IPOVERCDLC
#endif
#ifndef IFT_IPOVERCLAW
#define IFT_IPOVERCLAW IFT_IPOVERCLAW
#endif
#ifndef IFT_STACKTOSTACK
#define IFT_STACKTOSTACK IFT_STACKTOSTACK
#endif
#ifndef IFT_VIRTUALIPADDRESS
#define IFT_VIRTUALIPADDRESS IFT_VIRTUALIPADDRESS
#endif
#ifndef IFT_MPC
#define IFT_MPC IFT_MPC
#endif
#ifndef IFT_IPOVERATM
#define IFT_IPOVERATM IFT_IPOVERATM
#endif
#ifndef IFT_ISO88025FIBER
#define IFT_ISO88025FIBER IFT_ISO88025FIBER
#endif
#ifndef IFT_TDLC
#define IFT_TDLC IFT_TDLC
#endif
#ifndef IFT_GIGABITETHERNET
#define IFT_GIGABITETHERNET IFT_GIGABITETHERNET
#endif
#ifndef IFT_HDLC
#define IFT_HDLC IFT_HDLC
#endif
#ifndef IFT_LAPF
#define IFT_LAPF IFT_LAPF
#endif
#ifndef IFT_V37
#define IFT_V37 IFT_V37
#endif
#ifndef IFT_X25MLP
#define IFT_X25MLP IFT_X25MLP
#endif
#ifndef IFT_X25HUNTGROUP
#define IFT_X25HUNTGROUP IFT_X25HUNTGROUP
#endif
#ifndef IFT_TRANSPHDLC
#define IFT_TRANSPHDLC IFT_TRANSPHDLC
#endif
#ifndef IFT_INTERLEAVE
#define IFT_INTERLEAVE IFT_INTERLEAVE
#endif
#ifndef IFT_FAST
#define IFT_FAST IFT_FAST
#endif
#ifndef IFT_IP
#define IFT_IP IFT_IP
#endif
#ifndef IFT_DOCSCABLEMACLAYER
#define IFT_DOCSCABLEMACLAYER IFT_DOCSCABLEMACLAYER
#endif
#ifndef IFT_DOCSCABLEDOWNSTREAM
#define IFT_DOCSCABLEDOWNSTREAM IFT_DOCSCABLEDOWNSTREAM
#endif
#ifndef IFT_DOCSCABLEUPSTREAM
#define IFT_DOCSCABLEUPSTREAM IFT_DOCSCABLEUPSTREAM
#endif
#ifndef IFT_A12MPPSWITCH
#define IFT_A12MPPSWITCH IFT_A12MPPSWITCH
#endif
#ifndef IFT_TUNNEL
#define IFT_TUNNEL IFT_TUNNEL
#endif
#ifndef IFT_COFFEE
#define IFT_COFFEE IFT_COFFEE
#endif
#ifndef IFT_CES
#define IFT_CES IFT_CES
#endif
#ifndef IFT_ATMSUBINTERFACE
#define IFT_ATMSUBINTERFACE IFT_ATMSUBINTERFACE
#endif
#ifndef IFT_L2VLAN
#define IFT_L2VLAN IFT_L2VLAN
#endif
#ifndef IFT_L3IPVLAN
#define IFT_L3IPVLAN IFT_L3IPVLAN
#endif
#ifndef IFT_L3IPXVLAN
#define IFT_L3IPXVLAN IFT_L3IPXVLAN
#endif
#ifndef IFT_DIGITALPOWERLINE
#define IFT_DIGITALPOWERLINE IFT_DIGITALPOWERLINE
#endif
#ifndef IFT_MEDIAMAILOVERIP
#define IFT_MEDIAMAILOVERIP IFT_MEDIAMAILOVERIP
#endif
#ifndef IFT_DTM
#define IFT_DTM IFT_DTM
#endif
#ifndef IFT_DCN
#define IFT_DCN IFT_DCN
#endif
#ifndef IFT_IPFORWARD
#define IFT_IPFORWARD IFT_IPFORWARD
#endif
#ifndef IFT_MSDSL
#define IFT_MSDSL IFT_MSDSL
#endif
#ifndef IFT_IEEE1394
#define IFT_IEEE1394 IFT_IEEE1394
#endif
#ifndef IFT_IFGSN
#define IFT_IFGSN IFT_IFGSN
#endif
#ifndef IFT_DVBRCCMACLAYER
#define IFT_DVBRCCMACLAYER IFT_DVBRCCMACLAYER
#endif
#ifndef IFT_DVBRCCDOWNSTREAM
#define IFT_DVBRCCDOWNSTREAM IFT_DVBRCCDOWNSTREAM
#endif
#ifndef IFT_DVBRCCUPSTREAM
#define IFT_DVBRCCUPSTREAM IFT_DVBRCCUPSTREAM
#endif
#ifndef IFT_ATMVIRTUAL
#define IFT_ATMVIRTUAL IFT_ATMVIRTUAL
#endif
#ifndef IFT_MPLSTUNNEL
#define IFT_MPLSTUNNEL IFT_MPLSTUNNEL
#endif
#ifndef IFT_SRP
#define IFT_SRP IFT_SRP
#endif
#ifndef IFT_VOICEOVERATM
#define IFT_VOICEOVERATM IFT_VOICEOVERATM
#endif
#ifndef IFT_VOICEOVERFRAMERELAY
#define IFT_VOICEOVERFRAMERELAY IFT_VOICEOVERFRAMERELAY
#endif
#ifndef IFT_IDSL
#define IFT_IDSL IFT_IDSL
#endif
#ifndef IFT_COMPOSITELINK
#define IFT_COMPOSITELINK IFT_COMPOSITELINK
#endif
#ifndef IFT_SS7SIGLINK
#define IFT_SS7SIGLINK IFT_SS7SIGLINK
#endif
#ifndef IFT_PROPWIRELESSP2P
#define IFT_PROPWIRELESSP2P IFT_PROPWIRELESSP2P
#endif
#ifndef IFT_FRFORWARD
#define IFT_FRFORWARD IFT_FRFORWARD
#endif
#ifndef IFT_RFC1483
#define IFT_RFC1483 IFT_RFC1483
#endif
#ifndef IFT_USB
#define IFT_USB IFT_USB
#endif
#ifndef IFT_IEEE8023ADLAG
#define IFT_IEEE8023ADLAG IFT_IEEE8023ADLAG
#endif
#ifndef IFT_BGPPOLICYACCOUNTING
#define IFT_BGPPOLICYACCOUNTING IFT_BGPPOLICYACCOUNTING
#endif
#ifndef IFT_FRF16MFRBUNDLE
#define IFT_FRF16MFRBUNDLE IFT_FRF16MFRBUNDLE
#endif
#ifndef IFT_H323GATEKEEPER
#define IFT_H323GATEKEEPER IFT_H323GATEKEEPER
#endif
#ifndef IFT_H323PROXY
#define IFT_H323PROXY IFT_H323PROXY
#endif
#ifndef IFT_MPLS
#define IFT_MPLS IFT_MPLS
#endif
#ifndef IFT_MFSIGLINK
#define IFT_MFSIGLINK IFT_MFSIGLINK
#endif
#ifndef IFT_HDSL2
#define IFT_HDSL2 IFT_HDSL2
#endif
#ifndef IFT_SHDSL
#define IFT_SHDSL IFT_SHDSL
#endif
#ifndef IFT_DS1FDL
#define IFT_DS1FDL IFT_DS1FDL
#endif
#ifndef IFT_POS
#define IFT_POS IFT_POS
#endif
#ifndef IFT_DVBASILN
#define IFT_DVBASILN IFT_DVBASILN
#endif
#ifndef IFT_DVBASIOUT
#define IFT_DVBASIOUT IFT_DVBASIOUT
#endif
#ifndef IFT_PLC
#define IFT_PLC IFT_PLC
#endif
#ifndef IFT_NFAS
#define IFT_NFAS IFT_NFAS
#endif
#ifndef IFT_TR008
#define IFT_TR008 IFT_TR008
#endif
#ifndef IFT_GR303RDT
#define IFT_GR303RDT IFT_GR303RDT
#endif
#ifndef IFT_GR303IDT
#define IFT_GR303IDT IFT_GR303IDT
#endif
#ifndef IFT_ISUP
#define IFT_ISUP IFT_ISUP
#endif
#ifndef IFT_PROPDOCSWIRELESSMACLAYER
#define IFT_PROPDOCSWIRELESSMACLAYER IFT_PROPDOCSWIRELESSMACLAYER
#endif
#ifndef IFT_PROPDOCSWIRELESSDOWNSTREAM
#define IFT_PROPDOCSWIRELESSDOWNSTREAM IFT_PROPDOCSWIRELESSDOWNSTREAM
#endif
#ifndef IFT_PROPDOCSWIRELESSUPSTREAM
#define IFT_PROPDOCSWIRELESSUPSTREAM IFT_PROPDOCSWIRELESSUPSTREAM
#endif
#ifndef IFT_HIPERLAN2
#define IFT_HIPERLAN2 IFT_HIPERLAN2
#endif
#ifndef IFT_PROPBWAP2MP
#define IFT_PROPBWAP2MP IFT_PROPBWAP2MP
#endif
#ifndef IFT_SONETOVERHEADCHANNEL
#define IFT_SONETOVERHEADCHANNEL IFT_SONETOVERHEADCHANNEL
#endif
#ifndef IFT_DIGITALWRAPPEROVERHEADCHANNEL
#define IFT_DIGITALWRAPPEROVERHEADCHANNEL IFT_DIGITALWRAPPEROVERHEADCHANNEL
#endif
#ifndef IFT_AAL2
#define IFT_AAL2 IFT_AAL2
#endif
#ifndef IFT_RADIOMAC
#define IFT_RADIOMAC IFT_RADIOMAC
#endif
#ifndef IFT_ATMRADIO
#define IFT_ATMRADIO IFT_ATMRADIO
#endif
#ifndef IFT_IMT
#define IFT_IMT IFT_IMT
#endif
#ifndef IFT_MVL
#define IFT_MVL IFT_MVL
#endif
#ifndef IFT_REACHDSL
#define IFT_REACHDSL IFT_REACHDSL
#endif
#ifndef IFT_FRDLCIENDPT
#define IFT_FRDLCIENDPT IFT_FRDLCIENDPT
#endif
#ifndef IFT_ATMVCIENDPT
#define IFT_ATMVCIENDPT IFT_ATMVCIENDPT
#endif
#ifndef IFT_OPTICALCHANNEL
#define IFT_OPTICALCHANNEL IFT_OPTICALCHANNEL
#endif
#ifndef IFT_OPTICALTRANSPORT
#define IFT_OPTICALTRANSPORT IFT_OPTICALTRANSPORT
#endif
#ifndef IFT_INFINIBAND
#define IFT_INFINIBAND IFT_INFINIBAND
#endif
#ifndef IFT_BRIDGE
#define IFT_BRIDGE IFT_BRIDGE
#endif
#ifndef IFT_STF
#define IFT_STF IFT_STF
#endif
#ifndef IFT_GIF
#define IFT_GIF IFT_GIF
#endif
#ifndef IFT_PVC
#define IFT_PVC IFT_PVC
#endif
#ifndef IFT_FAITH
#define IFT_FAITH IFT_FAITH
#endif
#ifndef IFT_ENC
#define IFT_ENC IFT_ENC
#endif
#ifndef IFT_PFLOG
#define IFT_PFLOG IFT_PFLOG
#endif
#ifndef IFT_PFSYNC
#define IFT_PFSYNC IFT_PFSYNC
#endif
#ifndef IFT_IPXIP
#define IFT_IPXIP IFT_IPXIP
#endif
#endif
static void print_usage(const char *name) {
fprintf(stderr, "Usage: %s { -a | <interface> }\n", name);
}
static void print_interface_flags(unsigned int flags) {
static const struct {
unsigned int flag;
const char *name;
} flag_names[] = {
{ IFF_UP, "UP" },
{ IFF_BROADCAST, "BROADCAST" },
{ IFF_DEBUG, "DEBUG" },
{ IFF_LOOPBACK, "LOOPBACK" },
{ IFF_POINTOPOINT, "POINTOPOINT" },
{ IFF_SMART, "SMART" },
{ IFF_DRV_RUNNING, "RUNNING" },
{ IFF_NOARP, "NOARP" },
{ IFF_PROMISC, "PROMISC" },
{ IFF_ALLMULTI, "ALLMULTI" },
{ IFF_DRV_OACTIVE, "OACTIVE" },
{ IFF_SIMPLEX, "SIMPLEX" },
{ IFF_LINK0, "LINK0" },
{ IFF_LINK1, "LINK1" },
{ IFF_LINK2, "LINK2" },
{ IFF_ALTPHYS, "ALTPHYS" },
{ IFF_MULTICAST, "MULTICAST" },
#ifdef IFF_CANTCONFIG
{ IFF_CANTCONFIG, "CANTCONFIG" },
#endif
#ifdef IFF_PPROMISC
{ IFF_PPROMISC, "PPROMISC" },
#endif
#ifdef IFF_MONITOR
{ IFF_MONITOR, "MONITOR" },
#endif
#ifdef IFF_STATICARP
{ IFF_STATICARP, "STATICARP" },
#endif
};
int more = 0;
unsigned int i = 0;
do {
if(!(flags & flag_names[i].flag)) continue;
if(more) putchar(',');
fputs(flag_names[i].name, stdout);
more = 1;
} while(++i < sizeof flag_names / sizeof *flag_names);
}
static void print_datalink_name_and_address(const struct sockaddr_dl *sdl) {
putchar(' ');
switch(sdl->sdl_type) {
#ifdef IFT_1822
case IFT_1822:
fputs("1822", stdout);
break;
#endif
#ifdef IFT_HDH1822
case IFT_HDH1822:
fputs("hdh1822", stdout);
break;
#endif
#ifdef IFT_X25DDN
case IFT_X25DDN:
fputs("x25ddn", stdout);
break;
#endif
#ifdef IFT_X25
case IFT_X25:
fputs("x25", stdout);
break;
#endif
#ifdef IFT_ETHER
case IFT_ETHER:
fputs("ether", stdout);
break;
#endif
#ifdef IFT_ISO88023
case IFT_ISO88023:
fputs("iso88023", stdout);
break;
#endif
#ifdef IFT_ISO88024
case IFT_ISO88024:
fputs("iso88024", stdout);
break;
#endif
#ifdef IFT_ISO88025
case IFT_ISO88025:
fputs("iso88025", stdout);
break;
#endif
#ifdef IFT_ISO88026
case IFT_ISO88026:
fputs("iso88026", stdout);
break;
#endif
#ifdef IFT_STARLAN
case IFT_STARLAN:
fputs("starlan", stdout);
break;
#endif
#ifdef IFT_P10
case IFT_P10:
fputs("p10", stdout);
break;
#endif
#ifdef IFT_P80
case IFT_P80:
fputs("p80", stdout);
break;
#endif
#ifdef IFT_HY
case IFT_HY:
fputs("hy", stdout);
break;
#endif
#ifdef IFT_FDDI
case IFT_FDDI:
fputs("fddi", stdout);
break;
#endif
#ifdef IFT_LAPB
case IFT_LAPB:
fputs("lapb", stdout);
break;
#endif
#ifdef IFT_SDLC
case IFT_SDLC:
fputs("sdlc", stdout);
break;
#endif
#ifdef IFT_T1
case IFT_T1:
fputs("t1", stdout);
break;
#endif
#ifdef IFT_CEPT
case IFT_CEPT:
fputs("cept", stdout);
break;
#endif
#ifdef IFT_ISDNBASIC
case IFT_ISDNBASIC:
fputs("isdnbasic", stdout);
break;
#endif
#ifdef IFT_ISDNPRIMARY
case IFT_ISDNPRIMARY:
fputs("isdnprimary", stdout);
break;
#endif
#ifdef IFT_PTPSERIAL
case IFT_PTPSERIAL:
fputs("ptpserial", stdout);
break;
#endif
#ifdef IFT_PPP
case IFT_PPP:
fputs("ppp", stdout);
break;
#endif
#ifdef IFT_LOOP
case IFT_LOOP:
fputs("loop", stdout);
break;
#endif
#ifdef IFT_EON
case IFT_EON:
fputs("eon", stdout);
break;
#endif
#ifdef IFT_XETHER
case IFT_XETHER:
fputs("xether", stdout);
break;
#endif
#ifdef IFT_NSIP
case IFT_NSIP:
fputs("nsip", stdout);
break;
#endif
#ifdef IFT_SLIP
case IFT_SLIP:
fputs("slip", stdout);
break;
#endif
#ifdef IFT_ULTRA
case IFT_ULTRA:
fputs("ultra", stdout);
break;
#endif
#ifdef IFT_DS3
case IFT_DS3:
fputs("ds3", stdout);
break;
#endif
#ifdef IFT_SIP
case IFT_SIP:
fputs("sip", stdout);
break;
#endif
#ifdef IFT_FRELAY
case IFT_FRELAY:
fputs("frelay", stdout);
break;
#endif
#ifdef IFT_RS232
case IFT_RS232:
fputs("rs232", stdout);
break;
#endif
#ifdef IFT_PARA
case IFT_PARA:
fputs("para", stdout);
break;
#endif
#ifdef IFT_ARCNET
case IFT_ARCNET:
fputs("arcnet", stdout);
break;
#endif
#ifdef IFT_ARCNETPLUS
case IFT_ARCNETPLUS:
fputs("arcnetplus", stdout);
break;
#endif
#ifdef IFT_ATM
case IFT_ATM:
fputs("atm", stdout);
break;
#endif
#ifdef IFT_MIOX25
case IFT_MIOX25:
fputs("miox25", stdout);
break;
#endif
#ifdef IFT_SONET
case IFT_SONET:
fputs("sonet", stdout);
break;
#endif
#ifdef IFT_X25PLE
case IFT_X25PLE:
fputs("x25ple", stdout);
break;
#endif
#ifdef IFT_ISO88022LLC
case IFT_ISO88022LLC:
fputs("iso88022llc", stdout);
break;
#endif
#ifdef IFT_LOCALTALK
case IFT_LOCALTALK:
fputs("localtalk", stdout);
break;
#endif
#ifdef IFT_SMDSDXI
case IFT_SMDSDXI:
fputs("smdsdxi", stdout);
break;
#endif
#ifdef IFT_FRELAYDCE
case IFT_FRELAYDCE:
fputs("frelaydce", stdout);
break;
#endif
#ifdef IFT_V35
case IFT_V35:
fputs("v35", stdout);
break;
#endif
#ifdef IFT_HSSI
case IFT_HSSI:
fputs("hssi", stdout);
break;
#endif
#ifdef IFT_HIPPI
case IFT_HIPPI:
fputs("hippi", stdout);
break;
#endif
#ifdef IFT_MODEM
case IFT_MODEM:
fputs("modem", stdout);
break;
#endif
#ifdef IFT_AAL5
case IFT_AAL5:
fputs("aal5", stdout);
break;
#endif
#ifdef IFT_SONETPATH
case IFT_SONETPATH:
fputs("sonetpath", stdout);
break;
#endif
#ifdef IFT_SONETVT
case IFT_SONETVT:
fputs("sonetvt", stdout);
break;
#endif
#ifdef IFT_SMDSICIP
case IFT_SMDSICIP:
fputs("smdsicip", stdout);
break;
#endif
#ifdef IFT_PROPVIRTUAL
case IFT_PROPVIRTUAL:
fputs("propvirtual", stdout);
break;
#endif
#ifdef IFT_PROPMUX
case IFT_PROPMUX:
fputs("propmux", stdout);
break;
#endif
#ifdef IFT_IEEE80212
case IFT_IEEE80212:
fputs("ieee80212", stdout);
break;
#endif
#ifdef IFT_FIBRECHANNEL
case IFT_FIBRECHANNEL:
fputs("fibrechannel", stdout);
break;
#endif
#ifdef IFT_HIPPIINTERFACE
case IFT_HIPPIINTERFACE:
fputs("hippiinterface", stdout);
break;
#endif
#ifdef IFT_FRAMERELAYINTERCONNECT
case IFT_FRAMERELAYINTERCONNECT:
fputs("framerelayinterconnect", stdout);
break;
#endif
#ifdef IFT_AFLANE8023
case IFT_AFLANE8023:
fputs("aflane8023", stdout);
break;
#endif
#ifdef IFT_AFLANE8025
case IFT_AFLANE8025:
fputs("aflane8025", stdout);
break;
#endif
#ifdef IFT_CCTEMUL
case IFT_CCTEMUL:
fputs("cctemul", stdout);
break;
#endif
#ifdef IFT_FASTETHER
case IFT_FASTETHER:
fputs("fastether", stdout);
break;
#endif
#ifdef IFT_ISDN
case IFT_ISDN:
fputs("isdn", stdout);
break;
#endif
#ifdef IFT_V11
case IFT_V11:
fputs("v11", stdout);
break;
#endif
#ifdef IFT_V36
case IFT_V36:
fputs("v36", stdout);
break;
#endif
#ifdef IFT_G703AT64K
case IFT_G703AT64K:
fputs("g703at64k", stdout);
break;
#endif
#ifdef IFT_G703AT2MB
case IFT_G703AT2MB:
fputs("g703at2mb", stdout);
break;
#endif
#ifdef IFT_QLLC
case IFT_QLLC:
fputs("qllc", stdout);
break;
#endif
#ifdef IFT_FASTETHERFX
case IFT_FASTETHERFX:
fputs("fastetherfx", stdout);
break;
#endif
#ifdef IFT_CHANNEL
case IFT_CHANNEL:
fputs("channel", stdout);
break;
#endif
#ifdef IFT_IEEE80211
case IFT_IEEE80211:
fputs("ieee80211", stdout);
break;
#endif
#ifdef IFT_IBM370PARCHAN
case IFT_IBM370PARCHAN:
fputs("ibm370parchan", stdout);
break;
#endif
#ifdef IFT_ESCON
case IFT_ESCON:
fputs("escon", stdout);
break;
#endif
#ifdef IFT_DLSW
case IFT_DLSW:
fputs("dlsw", stdout);
break;
#endif
#ifdef IFT_ISDNS
case IFT_ISDNS:
fputs("isdns", stdout);
break;
#endif
#ifdef IFT_ISDNU
case IFT_ISDNU:
fputs("isdnu", stdout);
break;
#endif
#ifdef IFT_LAPD
case IFT_LAPD:
fputs("lapd", stdout);
break;
#endif
#ifdef IFT_IPSWITCH
case IFT_IPSWITCH:
fputs("ipswitch", stdout);
break;
#endif
#ifdef IFT_RSRB
case IFT_RSRB:
fputs("rsrb", stdout);
break;
#endif
#ifdef IFT_ATMLOGICAL
case IFT_ATMLOGICAL:
fputs("atmlogical", stdout);
break;
#endif
#ifdef IFT_DS0
case IFT_DS0:
fputs("ds0", stdout);
break;
#endif
#ifdef IFT_DS0BUNDLE
case IFT_DS0BUNDLE:
fputs("ds0bundle", stdout);
break;
#endif
#ifdef IFT_BSC
case IFT_BSC:
fputs("bsc", stdout);
break;
#endif
#ifdef IFT_ASYNC
case IFT_ASYNC:
fputs("async", stdout);
break;
#endif
#ifdef IFT_CNR
case IFT_CNR:
fputs("cnr", stdout);
break;
#endif
#ifdef IFT_ISO88025DTR
case IFT_ISO88025DTR:
fputs("iso88025dtr", stdout);
break;
#endif
#ifdef IFT_EPLRS
case IFT_EPLRS:
fputs("eplrs", stdout);
break;
#endif
#ifdef IFT_ARAP
case IFT_ARAP:
fputs("arap", stdout);
break;
#endif
#ifdef IFT_PROPCNLS
case IFT_PROPCNLS:
fputs("propcnls", stdout);
break;
#endif
#ifdef IFT_HOSTPAD
case IFT_HOSTPAD:
fputs("hostpad", stdout);
break;
#endif
#ifdef IFT_TERMPAD
case IFT_TERMPAD:
fputs("termpad", stdout);
break;
#endif
#ifdef IFT_FRAMERELAYMPI
case IFT_FRAMERELAYMPI:
fputs("framerelaympi", stdout);
break;
#endif
#ifdef IFT_X213
case IFT_X213:
fputs("x213", stdout);
break;
#endif
#ifdef IFT_ADSL
case IFT_ADSL:
fputs("adsl", stdout);
break;
#endif
#ifdef IFT_RADSL
case IFT_RADSL:
fputs("radsl", stdout);
break;
#endif
#ifdef IFT_SDSL
case IFT_SDSL:
fputs("sdsl", stdout);
break;
#endif
#ifdef IFT_VDSL
case IFT_VDSL:
fputs("vdsl", stdout);
break;
#endif
#ifdef IFT_ISO88025CRFPINT
case IFT_ISO88025CRFPINT:
fputs("iso88025crfpint", stdout);
break;
#endif
#ifdef IFT_MYRINET
case IFT_MYRINET:
fputs("myrinet", stdout);
break;
#endif
#ifdef IFT_VOICEEM
case IFT_VOICEEM:
fputs("voiceem", stdout);
break;
#endif
#ifdef IFT_VOICEFXO
case IFT_VOICEFXO:
fputs("voicefxo", stdout);
break;
#endif
#ifdef IFT_VOICEFXS
case IFT_VOICEFXS:
fputs("voicefxs", stdout);
break;
#endif
#ifdef IFT_VOICEENCAP
case IFT_VOICEENCAP:
fputs("voiceencap", stdout);
break;
#endif
#ifdef IFT_VOICEOVERIP
case IFT_VOICEOVERIP:
fputs("voiceoverip", stdout);
break;
#endif
#ifdef IFT_ATMDXI
case IFT_ATMDXI:
fputs("atmdxi", stdout);
break;
#endif
#ifdef IFT_ATMFUNI
case IFT_ATMFUNI:
fputs("atmfuni", stdout);
break;
#endif
#ifdef IFT_ATMIMA
case IFT_ATMIMA:
fputs("atmima", stdout);
break;
#endif
#ifdef IFT_PPPMULTILINKBUNDLE
case IFT_PPPMULTILINKBUNDLE:
fputs("pppmultilinkbundle", stdout);
break;
#endif
#ifdef IFT_IPOVERCDLC
case IFT_IPOVERCDLC:
fputs("ipovercdlc", stdout);
break;
#endif
#ifdef IFT_IPOVERCLAW
case IFT_IPOVERCLAW:
fputs("ipoverclaw", stdout);
break;
#endif
#ifdef IFT_STACKTOSTACK
case IFT_STACKTOSTACK:
fputs("stacktostack", stdout);
break;
#endif
#ifdef IFT_VIRTUALIPADDRESS
case IFT_VIRTUALIPADDRESS:
fputs("virtualipaddress", stdout);
break;
#endif
#ifdef IFT_MPC
case IFT_MPC:
fputs("mpc", stdout);
break;
#endif
#ifdef IFT_IPOVERATM
case IFT_IPOVERATM:
fputs("ipoveratm", stdout);
break;
#endif
#ifdef IFT_ISO88025FIBER
case IFT_ISO88025FIBER:
fputs("iso88025fiber", stdout);
break;
#endif
#ifdef IFT_TDLC
case IFT_TDLC:
fputs("tdlc", stdout);
break;
#endif
#ifdef IFT_GIGABITETHERNET
case IFT_GIGABITETHERNET:
fputs("gigabitethernet", stdout);
break;
#endif
#ifdef IFT_HDLC
case IFT_HDLC:
fputs("hdlc", stdout);
break;
#endif
#ifdef IFT_LAPF
case IFT_LAPF:
fputs("lapf", stdout);
break;
#endif
#ifdef IFT_V37
case IFT_V37:
fputs("v37", stdout);
break;
#endif
#ifdef IFT_X25MLP
case IFT_X25MLP:
fputs("x25mlp", stdout);
break;
#endif
#ifdef IFT_X25HUNTGROUP
case IFT_X25HUNTGROUP:
fputs("x25huntgroup", stdout);
break;
#endif
#ifdef IFT_TRANSPHDLC
case IFT_TRANSPHDLC:
fputs("transphdlc", stdout);
break;
#endif
#ifdef IFT_INTERLEAVE
case IFT_INTERLEAVE:
fputs("interleave", stdout);
break;
#endif
#ifdef IFT_FAST
case IFT_FAST:
fputs("fast", stdout);
break;
#endif
#ifdef IFT_IP
case IFT_IP:
fputs("ip", stdout);
break;
#endif
#ifdef IFT_DOCSCABLEMACLAYER
case IFT_DOCSCABLEMACLAYER:
fputs("docscablemaclayer", stdout);
break;
#endif
#ifdef IFT_DOCSCABLEDOWNSTREAM
case IFT_DOCSCABLEDOWNSTREAM:
fputs("docscabledownstream", stdout);
break;
#endif
#ifdef IFT_DOCSCABLEUPSTREAM
case IFT_DOCSCABLEUPSTREAM:
fputs("docscableupstream", stdout);
break;
#endif
#ifdef IFT_A12MPPSWITCH
case IFT_A12MPPSWITCH:
fputs("a12mppswitch", stdout);
break;
#endif
#ifdef IFT_TUNNEL
case IFT_TUNNEL:
fputs("tunnel", stdout);
break;
#endif
#ifdef IFT_COFFEE
case IFT_COFFEE:
fputs("coffee", stdout);
break;
#endif
#ifdef IFT_CES
case IFT_CES:
fputs("ces", stdout);
break;
#endif
#ifdef IFT_ATMSUBINTERFACE
case IFT_ATMSUBINTERFACE:
fputs("atmsubinterface", stdout);
break;
#endif
#ifdef IFT_L2VLAN
case IFT_L2VLAN:
fputs("l2vlan", stdout);
break;
#endif
#ifdef IFT_L3IPVLAN
case IFT_L3IPVLAN:
fputs("l3ipvlan", stdout);
break;
#endif
#ifdef IFT_L3IPXVLAN
case IFT_L3IPXVLAN:
fputs("l3ipxvlan", stdout);
break;
#endif
#ifdef IFT_DIGITALPOWERLINE
case IFT_DIGITALPOWERLINE:
fputs("digitalpowerline", stdout);
break;
#endif
#ifdef IFT_MEDIAMAILOVERIP
case IFT_MEDIAMAILOVERIP:
fputs("mediamailoverip", stdout);
break;
#endif
#ifdef IFT_DTM
case IFT_DTM:
fputs("dtm", stdout);
break;
#endif
#ifdef IFT_DCN
case IFT_DCN:
fputs("dcn", stdout);
break;
#endif
#ifdef IFT_IPFORWARD
case IFT_IPFORWARD:
fputs("ipforward", stdout);
break;
#endif
#ifdef IFT_MSDSL
case IFT_MSDSL:
fputs("msdsl", stdout);
break;
#endif
#ifdef IFT_IEEE1394
case IFT_IEEE1394:
fputs("ieee1394", stdout);
break;
#endif
#ifdef IFT_IFGSN
case IFT_IFGSN:
fputs("ifgsn", stdout);
break;
#endif
#ifdef IFT_DVBRCCMACLAYER
case IFT_DVBRCCMACLAYER:
fputs("dvbrccmaclayer", stdout);
break;
#endif
#ifdef IFT_DVBRCCDOWNSTREAM
case IFT_DVBRCCDOWNSTREAM:
fputs("dvbrccdownstream", stdout);
break;
#endif
#ifdef IFT_DVBRCCUPSTREAM
case IFT_DVBRCCUPSTREAM:
fputs("dvbrccupstream", stdout);
break;
#endif
#ifdef IFT_ATMVIRTUAL
case IFT_ATMVIRTUAL:
fputs("atmvirtual", stdout);
break;
#endif
#ifdef IFT_MPLSTUNNEL
case IFT_MPLSTUNNEL:
fputs("mplstunnel", stdout);
break;
#endif
#ifdef IFT_SRP
case IFT_SRP:
fputs("srp", stdout);
break;
#endif
#ifdef IFT_VOICEOVERATM
case IFT_VOICEOVERATM:
fputs("voiceoveratm", stdout);
break;
#endif
#ifdef IFT_VOICEOVERFRAMERELAY
case IFT_VOICEOVERFRAMERELAY:
fputs("voiceoverframerelay", stdout);
break;
#endif
#ifdef IFT_IDSL
case IFT_IDSL:
fputs("idsl", stdout);
break;
#endif
#ifdef IFT_COMPOSITELINK
case IFT_COMPOSITELINK:
fputs("compositelink", stdout);
break;
#endif
#ifdef IFT_SS7SIGLINK
case IFT_SS7SIGLINK:
fputs("ss7siglink", stdout);
break;
#endif
#ifdef IFT_PROPWIRELESSP2P
case IFT_PROPWIRELESSP2P:
fputs("propwirelessp2p", stdout);
break;
#endif
#ifdef IFT_FRFORWARD
case IFT_FRFORWARD:
fputs("frforward", stdout);
break;
#endif
#ifdef IFT_RFC1483
case IFT_RFC1483:
fputs("rfc1483", stdout);
break;
#endif
#ifdef IFT_USB
case IFT_USB:
fputs("usb", stdout);
break;
#endif
#ifdef IFT_IEEE8023ADLAG
case IFT_IEEE8023ADLAG:
fputs("ieee8023adlag", stdout);
break;
#endif
#ifdef IFT_BGPPOLICYACCOUNTING
case IFT_BGPPOLICYACCOUNTING:
fputs("bgppolicyaccounting", stdout);
break;
#endif
#ifdef IFT_FRF16MFRBUNDLE
case IFT_FRF16MFRBUNDLE:
fputs("frf16mfrbundle", stdout);
break;
#endif
#ifdef IFT_H323GATEKEEPER
case IFT_H323GATEKEEPER:
fputs("h323gatekeeper", stdout);
break;
#endif
#ifdef IFT_H323PROXY
case IFT_H323PROXY:
fputs("h323proxy", stdout);
break;
#endif
#ifdef IFT_MPLS
case IFT_MPLS:
fputs("mpls", stdout);
break;
#endif
#ifdef IFT_MFSIGLINK
case IFT_MFSIGLINK:
fputs("mfsiglink", stdout);
break;
#endif
#ifdef IFT_HDSL2
case IFT_HDSL2:
fputs("hdsl2", stdout);
break;
#endif
#ifdef IFT_SHDSL
case IFT_SHDSL:
fputs("shdsl", stdout);
break;
#endif
#ifdef IFT_DS1FDL
case IFT_DS1FDL:
fputs("ds1fdl", stdout);
break;
#endif
#ifdef IFT_POS
case IFT_POS:
fputs("pos", stdout);
break;
#endif
#ifdef IFT_DVBASILN
case IFT_DVBASILN:
fputs("dvbasiln", stdout);
break;
#endif
#ifdef IFT_DVBASIOUT
case IFT_DVBASIOUT:
fputs("dvbasiout", stdout);
break;
#endif
#ifdef IFT_PLC
case IFT_PLC:
fputs("plc", stdout);
break;
#endif
#ifdef IFT_NFAS
case IFT_NFAS:
fputs("nfas", stdout);
break;
#endif
#ifdef IFT_TR008
case IFT_TR008:
fputs("tr008", stdout);
break;
#endif
#ifdef IFT_GR303RDT
case IFT_GR303RDT:
fputs("gr303rdt", stdout);
break;
#endif
#ifdef IFT_GR303IDT
case IFT_GR303IDT:
fputs("gr303idt", stdout);
break;
#endif
#ifdef IFT_ISUP
case IFT_ISUP:
fputs("isup", stdout);
break;
#endif
#ifdef IFT_PROPDOCSWIRELESSMACLAYER
case IFT_PROPDOCSWIRELESSMACLAYER:
fputs("propdocswirelessmaclayer", stdout);
break;
#endif
#ifdef IFT_PROPDOCSWIRELESSDOWNSTREAM
case IFT_PROPDOCSWIRELESSDOWNSTREAM:
fputs("propdocswirelessdownstream", stdout);
break;
#endif
#ifdef IFT_PROPDOCSWIRELESSUPSTREAM
case IFT_PROPDOCSWIRELESSUPSTREAM:
fputs("propdocswirelessupstream", stdout);
break;
#endif
#ifdef IFT_HIPERLAN2
case IFT_HIPERLAN2:
fputs("hiperlan2", stdout);
break;
#endif
#ifdef IFT_PROPBWAP2MP
case IFT_PROPBWAP2MP:
fputs("propbwap2mp", stdout);
break;
#endif
#ifdef IFT_SONETOVERHEADCHANNEL
case IFT_SONETOVERHEADCHANNEL:
fputs("sonetoverheadchannel", stdout);
break;
#endif
#ifdef IFT_DIGITALWRAPPEROVERHEADCHANNEL
case IFT_DIGITALWRAPPEROVERHEADCHANNEL:
fputs("digitalwrapperoverheadchannel", stdout);
break;
#endif
#ifdef IFT_AAL2
case IFT_AAL2:
fputs("aal2", stdout);
break;
#endif
#ifdef IFT_RADIOMAC
case IFT_RADIOMAC:
fputs("radiomac", stdout);
break;
#endif
#ifdef IFT_ATMRADIO
case IFT_ATMRADIO:
fputs("atmradio", stdout);
break;
#endif
#ifdef IFT_IMT
case IFT_IMT:
fputs("imt", stdout);
break;
#endif
#ifdef IFT_MVL
case IFT_MVL:
fputs("mvl", stdout);
break;
#endif
#ifdef IFT_REACHDSL
case IFT_REACHDSL:
fputs("reachdsl", stdout);
break;
#endif
#ifdef IFT_FRDLCIENDPT
case IFT_FRDLCIENDPT:
fputs("frdlciendpt", stdout);
break;
#endif
#ifdef IFT_ATMVCIENDPT
case IFT_ATMVCIENDPT:
fputs("atmvciendpt", stdout);
break;
#endif
#ifdef IFT_OPTICALCHANNEL
case IFT_OPTICALCHANNEL:
fputs("opticalchannel", stdout);
break;
#endif
#ifdef IFT_OPTICALTRANSPORT
case IFT_OPTICALTRANSPORT:
fputs("opticaltransport", stdout);
break;
#endif
#ifdef IFT_INFINIBAND
case IFT_INFINIBAND:
fputs("infiniband", stdout);
break;
#endif
#ifdef IFT_BRIDGE
case IFT_BRIDGE:
fputs("bridge", stdout);
break;
#endif
#ifdef IFT_STF
case IFT_STF:
fputs("stf", stdout);
break;
#endif
default:
fputs("unknown", stdout);
break;
}
if(sdl->sdl_alen) {
putchar(' ');
const char *p = sdl->sdl_data + sdl->sdl_nlen;
unsigned int i = 0;
do {
if(i) putchar(':');
printf("%02hhx", p[i]);
} while(++i < sdl->sdl_alen);
}
putchar('\n');
}
static long int human_readable_binary(unsigned long int n, int *unit, unsigned int scale) {
if(!unit) return -1;
if(scale < 1) return -1;
switch(*unit) {
case 0:
if(n < 1024 * scale) return n;
n /= 1024;
*unit = 'K';
case 'K':
if(n < 1024 * scale) return n;
n /= 1024;
*unit = 'M';
case 'M':
if(n < 1024 * scale) return n;
n /= 1024;
*unit = 'G';
case 'G':
if(n < 1024 * scale) return n;
n /= 1024;
*unit = 'T';
case 'T':
if(n < 1024 * scale) return n;
n /= 1024;
*unit = 'P';
return n;
default:
return -1;
}
}
static void print_human_readable_binary_value(unsigned long int value) {
unsigned int scale = 10;
unsigned long int n = value * 10;
if(n < value) {
scale = 1;
n = value;
}
int unit_prefix = 0;
n = human_readable_binary(n, &unit_prefix, scale);
if(scale == 1) printf("%lu ", n);
else printf("%lu.%u ", n / 10, (unsigned int)(n % 10));
if(unit_prefix) printf("%ci", unit_prefix);
}
static void print_statistics(const struct if_data *data) {
printf(" RX packets %lu bytes %lu (",
(unsigned long int)data->ifi_ipackets, (unsigned long int)data->ifi_ibytes);
print_human_readable_binary_value(data->ifi_ibytes);
fputs("B)\n", stdout);
printf(" RX errors %lu dropped %lu\n",
(unsigned long int)data->ifi_ierrors, (unsigned long int)data->ifi_iqdrops);
printf(" TX packets %lu bytes %lu (",
(unsigned long int)data->ifi_opackets, (unsigned long int)data->ifi_obytes);
print_human_readable_binary_value(data->ifi_obytes);
fputs("B)\n", stdout);
printf(" TX errors %lu", (unsigned long int)data->ifi_oerrors);
#ifdef _IFI_OQDROPS
printf(" dropped %lu", (unsigned long int)data->ifi_oqdrops);
#endif
printf(" collisions %lu\n", (unsigned long int)data->ifi_collisions);
}
static int print_config(const char *interface) {
int mib[] = {CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
size_t len;
if(sysctl(mib, sizeof mib / sizeof(int), NULL, &len, NULL, 0) < 0) return -1;
char buffer[len];
if(sysctl(mib, sizeof mib / sizeof(int), buffer, &len, NULL, 0) < 0) return -1;
int i = 0;
struct if_msghdr *ifm;
while(i < len) {
ifm = (struct if_msghdr *)(buffer + i);
if(ifm->ifm_type == RTM_IFINFO) {
/*
printf("ifm_data.ifi_type = %hhu, ifm_data.ifi_physical = %hhu, ifm_data.ifi_addrlen = %hhu, ifm_data.ifi_link_state = %hhu, ifm_data.ifi_datalen = %hhu, ifm_data.ifi_mtu = %lu, ifm_data.ifi_metric = %lu, ifm_data.ifi_baudrate = %lu, ifm_data.ifi_ipackets = %lu, ifm_data.ifi_ierrors = %lu, ifm_data.ifi_opackets = %lu, ifm_data.ifi_oerrors = %lu, ifm_data.ifi_collisions = %lu, ifm_data.ifi_ibytes = %lu, ifm_data.ifi_obytes = %lu, ifm_data.ifi_imcasts = %lu, ifm_data.ifi_omcasts = %lu, ifm_data.ifi_iqdrops = %lu, ifm_data.ifi_noproto = %lu, ifm_data.ifi_hwassist = 0x%llx, ifm_data.ifi_epoch = %llu\n",
ifm->ifm_data.ifi_type,
ifm->ifm_data.ifi_physical,
ifm->ifm_data.ifi_addrlen,
ifm->ifm_data.ifi_link_state,
ifm->ifm_data.ifi_datalen,
ifm->ifm_data.ifi_mtu,
ifm->ifm_data.ifi_metric,
ifm->ifm_data.ifi_baudrate,
ifm->ifm_data.ifi_ipackets,
ifm->ifm_data.ifi_ierrors,
ifm->ifm_data.ifi_opackets,
ifm->ifm_data.ifi_oerrors,
ifm->ifm_data.ifi_collisions,
ifm->ifm_data.ifi_ibytes,
ifm->ifm_data.ifi_obytes,
ifm->ifm_data.ifi_imcasts,
ifm->ifm_data.ifi_omcasts,
ifm->ifm_data.ifi_iqdrops,
ifm->ifm_data.ifi_noproto,
(unsigned long long int)ifm->ifm_data.ifi_hwassist,
(unsigned long long int)ifm->ifm_data.ifi_epoch);
*/
if(ifm->ifm_msglen - sizeof(struct if_msghdr) >= sizeof(struct sockaddr_dl)) {
struct sockaddr_dl *sdl = (struct sockaddr_dl *)(ifm + 1);
if(sdl->sdl_family == AF_LINK) {
/*
printf("sdl_type = %hhu, sdl_nlen = %hhu, sdl_alen = %hhu, sdl_slen = %hhu, interface name: ",
sdl->sdl_type,
sdl->sdl_nlen,
sdl->sdl_alen,
sdl->sdl_slen);
*/
char name[sdl->sdl_nlen + 1];
memcpy(name, sdl->sdl_data, sdl->sdl_nlen);
name[sdl->sdl_nlen] = 0;
// puts(name);
if(!interface || strcmp(interface, name) == 0) {
printf("%s: flags=%x<", name, ifm->ifm_flags);
print_interface_flags(ifm->ifm_flags);
printf("> metric %lu mtu %lu index %hu\n",
(unsigned long int)ifm->ifm_data.ifi_metric,
(unsigned long int)ifm->ifm_data.ifi_mtu, ifm->ifm_index);
print_datalink_name_and_address(sdl);
print_statistics(&ifm->ifm_data);
if(interface) return 0;
}
}
}
}
i += ifm->ifm_msglen;
}
if(interface) {
fprintf(stderr, "ifconfig: interface %s doesn't exist\n", interface);
return -1;
}
return 0;
}
int main(int argc, char **argv) {
int print_all = 0;
char **v = argv + 1;
while(*v && **v == '-') {
const char *o = *v++ + 1;
if(o[0] == '-' && !o[1]) break;
switch(*o) {
case 'a':
print_all = 1;
break;
case 'h':
print_usage(argv[0]);
return 0;
default:
fprintf(stderr, "%s: Unknown option '-%c'\n", argv[0], *o);
return -1;
}
}
if(!print_all) {
if(!*v) {
print_usage(argv[0]);
return -1;
}
if(v[1]) {
fprintf(stderr, "%s: Changing configuration is not supported\n", argv[0]);
return 1;
}
}
return print_config(print_all ? NULL : *v) == 0 ? 0 : 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment