229 lines
5.7 KiB
C
229 lines
5.7 KiB
C
/*
|
|
Copyright (c) 2012, Matthias Schiffer <mschiffer@universe-factory.net>
|
|
All rights reserved.
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright notice,
|
|
this list of conditions and the following disclaimer.
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
this list of conditions and the following disclaimer in the documentation
|
|
and/or other materials provided with the distribution.
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <libgen.h>
|
|
#include <stdarg.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include <net/ethernet.h>
|
|
#include <net/if.h>
|
|
#include <net/if_arp.h>
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
|
|
|
|
#define ANNOUNCE_INTERVAL 20
|
|
#define ANNOUNCE_TIMEOUT 60
|
|
|
|
#define SYSFS_PATH_MAX 256
|
|
#define SYSFS_CLASS_NET "/sys/class/net"
|
|
|
|
#define FFD_MAGIC 0xffd
|
|
#define FFVISD_VERSION 0
|
|
|
|
|
|
//static char *mesh = "bat0";
|
|
|
|
|
|
typedef struct _ffvisd_eth_address_t {
|
|
uint8_t address[ETH_ALEN];
|
|
} ffvisd_eth_address_t;
|
|
|
|
typedef struct _ffvisd_ipv6_host_address_t {
|
|
uint8_t address[8];
|
|
} ffvisd_ipv6_host_address_t;
|
|
|
|
typedef struct _ffvisd_server_announce_t {
|
|
ffvisd_eth_address_t eth_address;
|
|
uint16_t reserved;
|
|
ffvisd_ipv6_host_address_t address;
|
|
uint32_t age;
|
|
} ffvisd_server_announce_t;
|
|
|
|
typedef struct _ffvisd_announce_t {
|
|
uint16_t magic;
|
|
uint16_t version;
|
|
uint32_t n_servers;
|
|
uint32_t reserved;
|
|
ffvisd_server_announce_t servers[];
|
|
} ffvisd_announce_t;
|
|
|
|
typedef enum _ffvisd_iftype_t {
|
|
IF_UNKNOWN = 0,
|
|
IF_WIRED,
|
|
IF_WIRELESS,
|
|
IF_VIRTUAL,
|
|
IF_BRIDGE,
|
|
IF_MESH,
|
|
IF_MAX
|
|
} ffvisd_iftype_t;
|
|
|
|
typedef struct _ffvisd_iface_t {
|
|
unsigned ifindex;
|
|
ffvisd_iftype_t iftype;
|
|
char *ifname;
|
|
char *bridge_iface;
|
|
char *mesh_iface;
|
|
} ffvisd_iface_t;
|
|
|
|
static const char *ffvisd_iftype_names[IF_MAX] = {
|
|
[IF_UNKNOWN] = "unknown",
|
|
[IF_WIRED] = "wired",
|
|
[IF_WIRELESS] = "wireless",
|
|
[IF_VIRTUAL] = "virtual",
|
|
[IF_BRIDGE] = "bridge",
|
|
[IF_MESH] = "mesh",
|
|
};
|
|
|
|
typedef void (*iface_cb)(const ffvisd_iface_t *iface, void *arg);
|
|
|
|
|
|
void ffvisd_iface_free(ffvisd_iface_t *iface);
|
|
|
|
|
|
bool file_readv(const char *file, const char *format, va_list ap) {
|
|
FILE *f = fopen(file, "r");
|
|
if (!f)
|
|
return false;
|
|
|
|
int ret = vfscanf(f, format, ap);
|
|
|
|
fclose(f);
|
|
|
|
return (ret > 0);
|
|
}
|
|
|
|
bool iface_file_read(const char *ifname, const char *file, const char *format, ...) {
|
|
char filename[SYSFS_PATH_MAX];
|
|
|
|
snprintf(filename, SYSFS_PATH_MAX, SYSFS_CLASS_NET"/%s/%s", ifname, file);
|
|
|
|
va_list ap;
|
|
va_start(ap, format);
|
|
bool ret = file_readv(filename, format, ap);
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
}
|
|
|
|
bool iface_file_exists(const char *ifname, const char *file) {
|
|
char filename[SYSFS_PATH_MAX];
|
|
|
|
snprintf(filename, SYSFS_PATH_MAX, SYSFS_CLASS_NET"/%s/%s", ifname, file);
|
|
|
|
struct stat st;
|
|
return (stat(filename, &st) == 0);
|
|
}
|
|
|
|
char* get_bridge(const char *ifname) {
|
|
char filename[SYSFS_PATH_MAX], filename2[SYSFS_PATH_MAX] = {0};
|
|
|
|
snprintf(filename, SYSFS_PATH_MAX, SYSFS_CLASS_NET"/%s/brport/bridge", ifname);
|
|
if (readlink(filename, filename2, sizeof(filename2)) < 0)
|
|
return NULL;
|
|
|
|
return strdup(basename(filename2));
|
|
}
|
|
|
|
ffvisd_iface_t* ffvisd_iface_new(const char *ifname, unsigned ifindex) {
|
|
ffvisd_iface_t *iface = calloc(1, sizeof(ffvisd_iface_t));
|
|
|
|
iface->ifname = strdup(ifname);
|
|
iface->ifindex = ifindex;
|
|
|
|
if (!iface_file_read(ifname, "batman_adv/mesh_iface", "%as", &iface->mesh_iface) || !strcmp(iface->mesh_iface, "none")) {
|
|
free(iface->mesh_iface);
|
|
iface->mesh_iface = NULL;
|
|
}
|
|
|
|
iface->bridge_iface = get_bridge(ifname);
|
|
|
|
int type;
|
|
if (iface_file_read(ifname, "type", "%i", &type) && type == ARPHRD_ETHER) {
|
|
if(iface_file_exists(ifname, "mesh"))
|
|
iface->iftype = IF_MESH;
|
|
else if(iface_file_exists(ifname, "bridge"))
|
|
iface->iftype = IF_BRIDGE;
|
|
else if(iface_file_exists(ifname, "tun_flags"))
|
|
iface->iftype = IF_VIRTUAL;
|
|
else if(iface_file_exists(ifname, "wireless"))
|
|
iface->iftype = IF_WIRELESS;
|
|
else
|
|
iface->iftype = IF_WIRED;
|
|
}
|
|
|
|
return iface;
|
|
}
|
|
|
|
void ffvisd_iface_free(ffvisd_iface_t *iface) {
|
|
if (iface) {
|
|
free(iface->ifname);
|
|
free(iface->bridge_iface);
|
|
free(iface->mesh_iface);
|
|
free(iface);
|
|
}
|
|
}
|
|
|
|
void ffvisd_iface_foreach(iface_cb cb, void *arg) {
|
|
struct if_nameindex *ifaces = if_nameindex();
|
|
|
|
if (!ifaces) {
|
|
fprintf(stderr, "error: if_nameindex: %m");
|
|
return;
|
|
}
|
|
|
|
int i;
|
|
for (i = 0; ifaces[i].if_name; i++) {
|
|
ffvisd_iface_t *iface = ffvisd_iface_new(ifaces[i].if_name, ifaces[i].if_index);
|
|
|
|
if (iface)
|
|
cb(iface, arg);
|
|
|
|
ffvisd_iface_free(iface);
|
|
}
|
|
|
|
if_freenameindex(ifaces);
|
|
}
|
|
|
|
|
|
void pr_iface_info(const ffvisd_iface_t *iface, void *arg) {
|
|
printf("Interface %s: ifindex=%i bridge_iface=%s mesh_iface=%s iftype=%s\n", iface->ifname, iface->ifindex, iface->bridge_iface, iface->mesh_iface, ffvisd_iftype_names[iface->iftype]);
|
|
}
|
|
|
|
|
|
int main() {
|
|
ffvisd_iface_foreach(pr_iface_info, NULL);
|
|
|
|
return 0;
|
|
}
|