Project

General

Profile

Bug #3363 ยป sysctl_blocks.c

guy, 12/21/2023 11:11 PM

 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_var.h>
#include <sys/param.h>
#include <net/route.h>
#include <sys/sysctl.h>
#include <net/if_dl.h>

#include <netinet/in.h>
#include <netinet/in_var.h>

#include <ifaddrs.h>

#if !defined(AF_LINK)
#define SA_LEN(sa) sizeof(struct sockaddr)
#endif

#if !defined(SA_LEN)
#define SA_LEN(sa) (sa)->sa_len
#endif

#define SA_RLEN(sa) RT_ROUNDUP((sa)->sa_len)

#ifndef ALIGNBYTES
/*
* On systems with a routing socket, ALIGNBYTES should match the value
* that the kernel uses when building the messages.
*/
#define ALIGNBYTES XXX
#endif
#ifndef ALIGN
#define ALIGN(p) (((u_long)(p) + ALIGNBYTES) &~ ALIGNBYTES)
#endif

#define MAX_SYSCTL_TRY 5

int
my_getifaddrs(struct ifaddrs **pif)
{
int icnt = 1;
int dcnt = 0;
int ncnt = 0;
int ntry = 0;
int mib[6];
size_t needed;
char *buf;
char *next;
struct ifaddrs *cif = NULL;
char *p, *p0;
struct rt_msghdr *rtm;
struct if_msghdr *ifm;
struct ifa_msghdr *ifam;
struct sockaddr_dl *dl;
struct sockaddr *sa;
struct ifaddrs *ifa, *ift;
u_short idx = 0;
int i;
size_t len, alen;
char *data;
char *names;

mib[0] = CTL_NET;
mib[1] = PF_ROUTE;
mib[2] = 0; /* protocol */
mib[3] = 0; /* wildcard address family */
mib[4] = NET_RT_IFLIST;
mib[5] = 0; /* no flags */
do {
/*
* We'll try to get addresses several times in case that
* the number of addresses is unexpectedly increased during
* the two sysctl calls. This should rarely happen, but we'll
* try to do our best for applications that assume success of
* this library (which should usually be the case).
* Portability note: since FreeBSD does not add margin of
* memory at the first sysctl, the possibility of failure on
* the second sysctl call is a bit higher.
*/

fprintf(stderr, "sysctl to get buffer size\n");
if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
return (-1);
fprintf(stderr, "allocate buffer, size %zu\n", needed);
if ((buf = malloc(needed)) == NULL)
return (-1);
fprintf(stderr, "sysctl to get if list\n");
if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
free(buf);
return (-1);
}
fprintf(stderr, "Fetch failed, try again\n");
free(buf);
buf = NULL;
}
} while (buf == NULL);

fprintf(stderr, "That worked, process the list\n");
for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)(void *)next;
if (rtm->rtm_version != RTM_VERSION)
continue;
switch (rtm->rtm_type) {
case RTM_IFINFO:
ifm = (struct if_msghdr *)(void *)rtm;
if (ifm->ifm_addrs & RTA_IFP) {
idx = ifm->ifm_index;
++icnt;
dl = (struct sockaddr_dl *)(void *)(ifm + 1);
dcnt += SA_RLEN((struct sockaddr *)(void*)dl) +
ALIGNBYTES;
dcnt += sizeof(ifm->ifm_data);
ncnt += dl->sdl_nlen + 1;
} else
idx = 0;
break;

case RTM_NEWADDR:
ifam = (struct ifa_msghdr *)(void *)rtm;
if (idx && ifam->ifam_index != idx)
abort(); /* this cannot happen */

#define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD)
if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
break;
p = (char *)(void *)(ifam + 1);
++icnt;
/* Scan to look for length of address */
alen = 0;
for (p0 = p, i = 0; i < RTAX_MAX; i++) {
if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
== 0)
continue;
sa = (struct sockaddr *)(void *)p;
len = SA_RLEN(sa);
if (i == RTAX_IFA) {
alen = len;
break;
}
p += len;
}
for (p = p0, i = 0; i < RTAX_MAX; i++) {
if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
== 0)
continue;
sa = (struct sockaddr *)(void *)p;
len = SA_RLEN(sa);
if (i == RTAX_NETMASK && SA_LEN(sa) == 0)
dcnt += alen;
else
dcnt += len;
p += len;
}
break;
}
}

if (icnt + dcnt + ncnt == 1) {
*pif = NULL;
free(buf);
return (0);
}
fprintf(stderr, "Allocate buffer for returned interface list\n");
data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
if (data == NULL) {
free(buf);
return(-1);
}

ifa = (struct ifaddrs *)(void *)data;
data += sizeof(struct ifaddrs) * icnt;
names = data + dcnt;

memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
ift = ifa;

idx = 0;
for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
rtm = (struct rt_msghdr *)(void *)next;
if (rtm->rtm_version != RTM_VERSION)
continue;
switch (rtm->rtm_type) {
case RTM_IFINFO:
ifm = (struct if_msghdr *)(void *)rtm;
if (ifm->ifm_addrs & RTA_IFP) {
idx = ifm->ifm_index;
dl = (struct sockaddr_dl *)(void *)(ifm + 1);

cif = ift;
ift->ifa_name = names;
ift->ifa_flags = (int)ifm->ifm_flags;
memcpy(names, dl->sdl_data,
(size_t)dl->sdl_nlen);
names[dl->sdl_nlen] = 0;
names += dl->sdl_nlen + 1;

ift->ifa_addr = (struct sockaddr *)(void *)data;
memcpy(data, dl,
(size_t)SA_LEN((struct sockaddr *)
(void *)dl));
data += SA_RLEN((struct sockaddr *)(void *)dl);

/* ifm_data needs to be aligned */
ift->ifa_data = data = (void *)ALIGN(data);
memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data));
data += sizeof(ifm->ifm_data);
ift = (ift->ifa_next = ift + 1);
} else
idx = 0;
break;

case RTM_NEWADDR:
ifam = (struct ifa_msghdr *)(void *)rtm;
if (idx && ifam->ifam_index != idx)
abort(); /* this cannot happen */

if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
break;
ift->ifa_name = cif->ifa_name;
ift->ifa_flags = cif->ifa_flags;
ift->ifa_data = NULL;
ift->ifa_addrflags = ifam->ifam_addrflags;
p = (char *)(void *)(ifam + 1);
/* Scan to look for length of address */
alen = 0;
for (p0 = p, i = 0; i < RTAX_MAX; i++) {
if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
== 0)
continue;
sa = (struct sockaddr *)(void *)p;
len = SA_RLEN(sa);
if (i == RTAX_IFA) {
alen = len;
break;
}
p += len;
}
for (p = p0, i = 0; i < RTAX_MAX; i++) {
if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
== 0)
continue;
sa = (struct sockaddr *)(void *)p;
len = SA_RLEN(sa);
switch (i) {
case RTAX_IFA:
ift->ifa_addr =
(struct sockaddr *)(void *)data;
memcpy(data, p, len);
data += len;
break;

case RTAX_NETMASK:
ift->ifa_netmask =
(struct sockaddr *)(void *)data;
if (SA_LEN(sa) == 0) {
memset(data, 0, alen);
data += alen;
break;
}
memcpy(data, p, len);
data += len;
break;

case RTAX_BRD:
ift->ifa_broadaddr =
(struct sockaddr *)(void *)data;
memcpy(data, p, len);
data += len;
break;
}
p += len;
}

ift = (ift->ifa_next = ift + 1);
break;
}
}

fprintf(stderr, "OK, we're done\n");
free(buf);
if (--ift >= ifa) {
ift->ifa_next = NULL;
*pif = ifa;
} else {
*pif = NULL;
free(ifa);
}
return (0);
}

void
my_freeifaddrs(struct ifaddrs *ifp)
{

free(ifp);
}

#include <ifaddrs.h>

#ifndef RT_ADVANCE
#define RT_ADVANCE(x, n) (x += (n)->sa_len)
#endif

static void
do_addrs(const struct ifa_msghdr *ifam)
{
const char *cp = (const char *)(ifam + 1);
const struct sockaddr *sa, *ifa = NULL, *brd = NULL;
unsigned i;
char *ifname = NULL;
char namebuf[IFNAMSIZ];

if (ifam->ifam_addrs == 0)
return;
for (i = 1; i; i <<= 1) {
if ((i & ifam->ifam_addrs) == 0)
continue;
sa = (const struct sockaddr *)cp;
if (i == RTA_IFP) {
const struct sockaddr_dl *li;

li = (const struct sockaddr_dl *)sa;
if ((ifname = if_indextoname(li->sdl_index, namebuf)) == NULL) {
fprintf(stderr, "ignoring change on unknown interface #%d\n",
li->sdl_index);
return;
}
} else if (i == RTA_IFA)
ifa = sa;
else if (i == RTA_BRD)
brd = sa;
RT_ADVANCE(cp, sa);
}
if (ifa != NULL && ifname != NULL) {
printf("%s address change: %s\n", ifname,
(ifam->ifam_type == RTM_DELADDR) ? "down" : "up");
if (ifa != NULL) {
printf(" <insert address here>");
if (brd != NULL)
printf(" <insert broadcast/P2P test here>");
}
printf("\n");
}
}

static void
do_announce(const struct if_announcemsghdr *ifan)
{
switch (ifan->ifan_what) {
case IFAN_ARRIVAL:
printf("%s arrived (index %d)\n", ifan->ifan_name,
ifan->ifan_index);
break;
case IFAN_DEPARTURE:
printf("%s departed (index %d)\n", ifan->ifan_name,
ifan->ifan_index);
break;
default:
(void) printf("%s (index %d) unknown announce: what=%d\n",
ifan->ifan_name, ifan->ifan_index, ifan->ifan_what);
break;
}
}

static void
do_carrier(const struct if_msghdr *ifm)
{
char *ifname;
char namebuf[IFNAMSIZ];

if ((ifname = if_indextoname(ifm->ifm_index, namebuf)) == NULL)
return;

/*
* Treat it as an event worth handling if:
* - the carrier status changed, or
* - this is the first time we've been called, and
* inhibit_initial is not set
*/
switch (ifm->ifm_data.ifi_link_state) {

case LINK_STATE_UNKNOWN:
printf("%s: interface/carrier state unknown\n", ifname);
break;

case LINK_STATE_UP:
printf("%s: interface/carrier up\n", ifname);
break;

case LINK_STATE_DOWN:
printf("%s: interface/carrier down\n", ifname);
break;

default:
printf("%s: unknown link status value %lu\n", ifname,
ifm->ifm_data.ifi_link_state);
break;
}
}

static void
do_getifaddrs(void)
{
struct ifaddrs *ifaddrs;

fprintf(stderr, "Calling my_getifaddrs:\n");
if (my_getifaddrs(&ifaddrs) < 0)
fprintf(stderr, "my_getifaddrs: %s\n", strerror(errno));
else
my_freeifaddrs(ifaddrs);
}

/* XXX - should make sure len is big enough for the message type? */
static void
dispatch(const void *msg)
{
const struct rt_msghdr *hd = msg;

if (hd->rtm_version != RTM_VERSION) {
fprintf(stderr, "Wrong version - %u\n", hd->rtm_version);
return;
}

switch (hd->rtm_type) {

case RTM_NEWADDR:
fprintf(stderr, "RTM_NEWADDR\n");
do_getifaddrs();
do_addrs(msg);
break;

case RTM_DELADDR:
fprintf(stderr, "RTM_DELADDR\n");
do_getifaddrs();
do_addrs(msg);
break;

case RTM_IFANNOUNCE:
fprintf(stderr, "RTM_IFANNOUNCE\n");
do_getifaddrs();
do_announce(msg);
break;

case RTM_IFINFO:
fprintf(stderr, "RTM_IFINFO\n");
do_getifaddrs();
do_carrier(msg);
break;

default:
/* Should be impossible as we filter messages. */
fprintf(stderr, "unknown message ignored (%u)\n", hd->rtm_type);
break;
}
}

int
main(void)
{
int s, n;
struct msghdr msg;
struct iovec iov[1];
char buf[2048];
#ifdef RO_MSGFILTER
unsigned char msgfilter[] = {
RTM_IFINFO, RTM_IFANNOUNCE,
RTM_NEWADDR, RTM_DELADDR,
};
#endif

s = socket(PF_ROUTE, SOCK_RAW, 0);
if (s < 0) {
fprintf(stderr, "error opening routing socket: %s\n",
strerror(errno));
exit(2);
}
#ifdef RO_MSGFILTER
if (setsockopt(s, PF_ROUTE, RO_MSGFILTER,
&msgfilter, sizeof(msgfilter)) < 0)
fprintf(stderr, "RO_MSGFILTER: %s\n", strerror(errno));
#endif
#ifdef SO_RERROR
n = 1;
if (setsockopt(s, SOL_SOCKET, SO_RERROR, &n, sizeof(n)) < 0)
fprintf(stderr, "SO_RERROR: %s\n", strerror(errno));
#endif

iov[0].iov_base = buf;
iov[0].iov_len = sizeof(buf);
memset(&msg, 0, sizeof(msg));
msg.msg_iov = iov;
msg.msg_iovlen = 1;

for (;;) {
ssize_t nread;

fprintf(stderr, "Reading:\n");
nread = recvmsg(s, &msg, 0);
if (nread == -1) {
if (errno == ENOBUFS) {
fprintf(stderr,
"routing socket overflow detected\n");
/* XXX We don't track addresses, so they
* won't be reported. */
continue;
}
fprintf(stderr, "recvmsg: %s\n", strerror(errno));
} else {
if (nread != 0)
dispatch(iov[0].iov_base);
}
}

close(s);

return 0;
}

    (1-1/1)