📄 if-json.c.svn-base
字号:
/*
* if-json. Interface list in JSON format.
* Based on busybox, see <http://www.busybox.net/>.
*
* Copyright (C) 2008 OpenRB.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _PATH_PROCNET_DEV "/proc/net/dev"
#if INT_MAX == LONG_MAX
static const char *const ss_fmt[] = {
"%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u",
"%llu%llu%u%u%u%u%n%n%llu%llu%u%u%u%u%u",
"%llu%llu%u%u%u%u%u%u%llu%llu%u%u%u%u%u%u"
};
#else
static const char *const ss_fmt[] = {
"%n%llu%lu%lu%lu%lu%n%n%n%llu%lu%lu%lu%lu%lu",
"%llu%llu%lu%lu%lu%lu%n%n%llu%llu%lu%lu%lu%lu%lu",
"%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu"
};
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <string.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <net/if_arp.h>
#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined(_NEWLIB_VERSION)
#include <net/ethernet.h>
#else
#include <linux/if_ether.h>
#endif
struct user_net_device_stats {
unsigned long long rx_packets;
unsigned long long tx_packets;
unsigned long long rx_bytes;
unsigned long long tx_bytes;
unsigned long rx_errors;
unsigned long tx_errors;
unsigned long rx_dropped;
unsigned long tx_dropped;
unsigned long rx_multicast;
unsigned long rx_compressed;
unsigned long tx_compressed;
unsigned long collisions;
unsigned long rx_length_errors;
unsigned long rx_over_errors;
unsigned long rx_crc_errors;
unsigned long rx_frame_errors;
unsigned long rx_fifo_errors;
unsigned long rx_missed_errors;
unsigned long tx_aborted_errors;
unsigned long tx_carrier_errors;
unsigned long tx_fifo_errors;
unsigned long tx_heartbeat_errors;
unsigned long tx_window_errors;
};
struct interface {
struct interface *next, *prev;
char name[IFNAMSIZ];
short type;
short flags;
int metric;
int mtu;
int tx_queue_len;
struct ifmap map;
struct sockaddr addr;
struct sockaddr dstaddr;
struct sockaddr broadaddr;
struct sockaddr netmask;
int has_ip;
char hwaddr[32];
int statistics_valid;
struct user_net_device_stats stats;
int keepalive;
int outfill;
};
struct interface *int_list, *int_last;
/* prototypes */
int print_short(struct interface *, int, int);
int print_full(struct interface *, int, int);
int if_fetch(struct interface *);
int do_if_fetch(struct interface *);
struct interface *add_interface(char *);
void get_dev_fields(char *, struct interface *, int);
char *get_name(char *name, char *p);
void *xzalloc(size_t);
void *xmalloc(size_t);
void *xrealloc(void *, size_t);
char *safe_strncpy(char *, const char *, size_t);
inline int procnetdev_version(char *);
const struct hwtype *get_hwntype(int);
const struct aftype *get_afntype(int);
char *print_ether(unsigned char *);
const char *sprint_inet(struct sockaddr *, int);
const char *sprint_unspec(struct sockaddr *, int);
char *print_unspec(unsigned char *);
char *inet_addr(struct sockaddr_in *);
char * xstrdup(const char *);
struct hwtype {
const char *name;
const char *title;
int type;
char *(*print) (unsigned char *);
};
const struct hwtype loop_hwtype = {
.name = "loop",
.title = "Loopback",
.type = ARPHRD_LOOPBACK
};
const struct hwtype ether_hwtype = {
.name = "ether",
.title = "Ethernet",
.type = ARPHRD_ETHER,
.print = print_ether
};
const struct hwtype ppp_hwtype = {
.name = "ppp",
.title = "Point-to-Point",
.type = ARPHRD_PPP
};
const struct hwtype unspec_hwtype = {
.name = "unspec",
.title = "Unspec",
.type = -1,
};
const struct hwtype *const hwtypes[] = {
&loop_hwtype,
ðer_hwtype,
&ppp_hwtype,
&unspec_hwtype,
NULL
};
struct aftype {
const char *name;
int af;
const char *(*sprint) (struct sockaddr *, int numeric);
};
const struct aftype inet_aftype = {
.name = "inet",
.af = AF_INET,
.sprint = sprint_inet
};
const struct aftype unspec_aftype = {
.name = "unspec",
.af = AF_UNSPEC,
.sprint = sprint_unspec
};
const struct aftype *const aftypes[] = {
&inet_aftype,
&unspec_aftype,
NULL
};
int main(int argc, char **argv) {
struct interface *ife;
int i, res, err, first = 1, full = 1, eth_only = 0;
/* print all or status only ('s' switch), 'Ethernet' only using 'e' switch */
if (argc > 1 && argv[1][0] == '-') {
for (i = 1; i < 3; i++) {
if (!argv[1][ i ]) break;
else if (argv[1][ i ] == 's') full = 0;
else if (argv[1][ i ] == 'e') eth_only = 1;
}
}
/* fetch interface list */
if (!int_list && (if_readlist() < 0)) {
printf("{}");
return -1;
}
printf("{");
/* for all interfaces */
for (ife = int_list; ife; ife = ife->next) {
res = do_if_fetch(ife);
if (res >= 0) {
err = full ? print_full(ife, first, eth_only) : print_short(ife, first, eth_only);
if (!err) first = 0;
}
}
printf("}");
}
int print_short(struct interface * ptr, int first, int eth_only) {
if (eth_only && ptr->type != ARPHRD_ETHER) {
return 1;
}
printf("%s\"%s\": %d", (first ? "" : ", "), ptr->name, (ptr->flags & IFF_UP) ? 1 : 0 );
return 0;
}
int print_full(struct interface *ptr, int first, int eth_only) {
const struct aftype *ap;
const struct hwtype *hw;
ap = get_afntype(ptr->addr.sa_family);
if (ap == NULL) ap = get_afntype(0);
if (eth_only && ptr->type != ARPHRD_ETHER) {
return 1;
}
hw = get_hwntype(ptr->type);
if (hw == NULL) hw = get_hwntype(-1);
printf("%s\"%s\": {\"up\": %d, \"type\": \"%s\", \"mtu\": %d",
(first ? "" : ", "), ptr->name, (ptr->flags & IFF_UP) ? 1 : 0, hw->title, ptr->mtu);
if (hw->print != NULL) {
printf(", \"hw\": \"%s\"", hw->print((unsigned char *)ptr->hwaddr));
}
if (ptr->has_ip) {
printf(", \"%saddr\": \"%s\"", ap->name, ap->sprint(&ptr->addr, 1));
if (ptr->flags & IFF_POINTOPOINT) {
printf(", \"ptp\": \"%s\"", ap->sprint(&ptr->dstaddr, 1));
}
if (ptr->flags & IFF_BROADCAST) {
printf(", \"bcast\": \"%s\"", ap->sprint(&ptr->broadaddr, 1));
}
printf(", \"mask\": \"%s\"", ap->sprint(&ptr->netmask, 1));
}
if (ptr->statistics_valid) {
printf(", \"rxpack\": %llu, \"rxerr\": %lu, \"rxbytes\": %llu",
ptr->stats.rx_packets, ptr->stats.rx_errors, ptr->stats.rx_bytes);
printf(", \"txpack\": %llu, \"txerr\": %lu, \"txbytes\": %llu",
ptr->stats.tx_packets, ptr->stats.tx_errors, ptr->stats.tx_bytes);
}
printf("}");
return 0;
}
int do_if_fetch(struct interface *ife) {
if (if_fetch(ife) < 0) {
const char *errmsg;
fprintf(stderr, "%s: error fetching interface information: %s", ife->name,
(errno == ENODEV ? "Device not found" : strerror(errno)));
return -1;
}
return 0;
}
int if_fetch(struct interface *ife) {
struct ifreq ifr;
char *ifname = ife->name;
int skfd;
skfd = socket(AF_INET, SOCK_DGRAM, 0);
if (skfd < 0) {
fprintf(stderr, "error: no inet socket available");
return -1;
}
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
close(skfd);
return -1;
}
ife->flags = ifr.ifr_flags;
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
memset(ife->hwaddr, 0, 32);
if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0) {
memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
}
ife->type = ifr.ifr_hwaddr.sa_family;
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
ife->metric = 0;
if (ioctl(skfd, SIOCGIFMETRIC, &ifr) >= 0) ife->metric = ifr.ifr_metric;
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
ife->mtu = 0;
if (ioctl(skfd, SIOCGIFMTU, &ifr) >= 0) ife->mtu = ifr.ifr_mtu;
memset(&ife->map, 0, sizeof(struct ifmap));
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
ifr.ifr_addr.sa_family = AF_INET;
memset(&ife->addr, 0, sizeof(struct sockaddr));
if (ioctl(skfd, SIOCGIFADDR, &ifr) == 0) {
ife->has_ip = 1;
ife->addr = ifr.ifr_addr;
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
if (ioctl(skfd, SIOCGIFDSTADDR, &ifr) >= 0)
ife->dstaddr = ifr.ifr_dstaddr;
strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -