Getting the routing table on MACOSX (software)

The question is, how can I get the routing table on MACOSX? I do not mean netstat -nr . I mean, how to do this programmatically, using the C . First of all, I downloaded netstat source codes from opensource.apple.com. I found the void function mroutepr (void) in mroute.c .
This function looks like a function that receives a routing table, but I'm not sure.

There is an array declaration: struct vif viftable [CONFIG_MAXVIFS];

But when I tried to compile mroutepr, I found that struct vif was not declared in /usr/include/netinet/ip_mroute.h I added all the necessary additions. I checked it seven times :))

Then I check the source code of the xnu kernel. I found this structute in the xnu kernel in this file: xnu / bsd / netinet / ip_mroute.h. There was a complete definition of struct vif.

This structure seems to be available only in kernel mode.

I am puzzled. How can I declare struct vif only for kernel code? How does netstat work ?


All of the above is incorrect :))) The solution is in the route.c file.
The ntreestuff (void) function is the entry point for getting the routing table. Then, in the np_rtentry (rtm) function, we print the table on the console.

static void ntreestuff(void)
{
    size_t needed;
    int mib[6];
    char *buf, *next, *lim;
    struct rt_msghdr2 *rtm;

    mib[0] = CTL_NET;
    mib[1] = PF_ROUTE;
    mib[2] = 0;
    mib[3] = 0;
    mib[4] = NET_RT_DUMP2;
    mib[5] = 0;

    if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) 
    {
        err(1, "sysctl: net.route.0.0.dump estimate");
    }

    if ((buf = malloc(needed)) == 0) 
    {
        err(2, "malloc(%lu)", (unsigned long)needed);
    }

    if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 
    {
        err(1, "sysctl: net.route.0.0.dump");
    }

    lim  = buf + needed;
    for (next = buf; next < lim; next += rtm->rtm_msglen) 
    {
        rtm = (struct rt_msghdr2 *)next;
        np_rtentry(rtm);
    }
}
+1
1

, . , ( , sysctl), , root-, . , RTM_GET routing, . 6 , ..

#include <stdio.h>
#include <stdlib.h>
#include <err.h>

#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/route.h>

/* Darwin doesn't define this for some very odd reason */
#ifndef SA_SIZE
# define SA_SIZE(sa)                        \
    (  (!(sa) || ((struct sockaddr *)(sa))->sa_len == 0) ?  \
           sizeof(long)     :               \
           1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(long) - 1) ) )
#endif


static void
ntreestuff(void)
{
    size_t needed;
    int mib[6];
    char *buf, *next, *lim;
    struct rt_msghdr *rtm;
    struct sockaddr *sa;
    struct sockaddr_in *sockin;
    char line[MAXHOSTNAMELEN];

    mib[0] = CTL_NET;
    mib[1] = PF_ROUTE;
    mib[2] = 0;
    mib[3] = 0;
    mib[4] = NET_RT_DUMP;
    mib[5] = 0;
    if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
        err(1, "sysctl: net.route.0.0.dump estimate");
    }

    if ((buf = (char *)malloc(needed)) == NULL) {
        errx(2, "malloc(%lu)", (unsigned long)needed);
    }
    if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
        err(1, "sysctl: net.route.0.0.dump");
    }
    lim  = buf + needed;
    for (next = buf; next < lim; next += rtm->rtm_msglen) {
        rtm = (struct rt_msghdr *)next;
        sa = (struct sockaddr *)(rtm + 1);
        sa = (struct sockaddr *)(SA_SIZE(sa) + (char *)sa);
        sockin = (struct sockaddr_in *)sa;
        inet_ntop(AF_INET, &sockin->sin_addr.s_addr, line, sizeof(line) - 1);
        printf("defaultrouter=%s\n", line);
        break;
    }

    free(buf);
}

int
main(int argc __unused, char *argv[] __unused)
{
    ntreestuff();
    return (0);
}
+1

All Articles