📄 br-json.c.svn-base
字号:
/*
* br-json. Brigde list in JSON format.
*
* Based on Bridge Utils, see <http://www.linuxfoundation.org/en/Net:Bridge>
*
* Copyright (C) 2000 Lennert Buytenhek
* 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 as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 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/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_bridge.h>
#define MAX_BRIDGES 32
#define MAX_PORTS 32
int print_ifnames(const char *);
int get_stp(const char *);
int br_socket_fd;
int main(void) {
int i, num, first = 1;
char brname[IFNAMSIZ];
int brindices[MAX_BRIDGES];
unsigned long brargs[3] = { BRCTL_GET_BRIDGES, (unsigned long) brindices, MAX_BRIDGES };
if ((br_socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "Cannot init: %s", strerror(errno));
return errno;
}
num = ioctl(br_socket_fd, SIOCGIFBR, brargs);
if (num < 0) {
fprintf(stderr, "Get bridge indices failed: %s", strerror(errno));
return -errno;
}
printf("{");
for (i = 0; i < num; i++) {
if (!if_indextoname(brindices[i], brname)) {
fprintf(stderr, "Get find name for brindex %d failed", brindices[i]);
return -errno;
}
printf("%s\"%s\": {\"stp\": %d, \"ifnames\": [", (first ? "" : ", "), brname, get_stp(brname) );
print_ifnames(brname);
printf("]}");
first = 0;
}
printf("}");
return 0;
}
int print_ifnames(const char *brname) {
int i, err, first = 1;
struct ifreq ifr;
char ifname[IFNAMSIZ];
int ifindices[MAX_PORTS];
unsigned long args[4] = { BRCTL_GET_PORT_LIST, (unsigned long) ifindices, MAX_PORTS, 0 };
memset(ifindices, 0, sizeof(ifindices));
strncpy(ifr.ifr_name, brname, IFNAMSIZ);
ifr.ifr_data = (char *) &args;
err = ioctl(br_socket_fd, SIOCDEVPRIVATE, &ifr);
if (err < 0) {
fprintf(stderr, "List ports for bridge %s failed: %s", brname, strerror(errno));
return -errno;
}
for (i = 0; i < MAX_PORTS; i++) {
if (!ifindices[i] || !if_indextoname(ifindices[i], ifname)) {
continue;
}
printf("%s\"%s\"", (first ? "" : ", "), ifname);
first = 0;
}
return 0;
}
int get_stp(const char *bridge) {
struct ifreq ifr;
struct __bridge_info i;
unsigned long args[4] = { BRCTL_GET_BRIDGE_INFO, (unsigned long) &i, 0, 0 };
strncpy(ifr.ifr_name, bridge, IFNAMSIZ);
ifr.ifr_data = (char *) &args;
if (ioctl(br_socket_fd, SIOCDEVPRIVATE, &ifr) < 0) {
fprintf(stderr, "Can't get stp info for %s", bridge);
return 0;
}
return i.stp_enabled;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -