Reading routing entries

Hello,

I’m trying to get the address of the current gateway by software, and I am
stuck !
I am using a routing socket and the RTM_GET command, trying to get the
default route (route to 0.0.0.0, with a 0.0.0.0 netmask), but it always
returns me 0.0.0.0 in the gateway field (whereas the default route is set to
sth else in the routing table, I checked).
Can anyone help me on this ?? Here is my current code :

#include <sys/socket.h>
#include <sys/uio.h>
#include <unistd.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <libgen.h>
#include <arpa/inet.h>
#include <process.h>
#include <errno.h>

struct my_rt_t {
struct rt_msghdr rtm;
struct sockaddr_in dst;
struct sockaddr_in gw;
struct sockaddr_in msk;
};

int main() {
struct my_rt_t my_rt;
struct rt_msghdr *rtm;
struct sockaddr_in *dst, *msk, *gw;
int s;

if ((s=socket(AF_ROUTE, SOCK_RAW, 0)) == -1) {
printf(“error on socket”);
return;
}

memset(&my_rt, 0x0, sizeof(my_rt));
rtm = &(my_rt.rtm);
dst = &(my_rt.dst);
msk = &(my_rt.msk);
gw = &(my_rt.gw);

dst->sin_len = sizeof(*dst);
dst->sin_family = AF_INET;

msk->sin_len = sizeof(*msk);
msk->sin_family = AF_INET;

rtm->rtm_type = RTM_GET;
rtm->rtm_flags |= RTF_GATEWAY;
rtm->rtm_msglen = sizeof(my_rt);
rtm->rtm_version = RTM_VERSION;
rtm->rtm_seq = 1234;
rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
rtm->rtm_pid = getpid();
if (write(s, rtm, rtm->rtm_msglen) < 0) {
printf(“error on write : %s”, strerror(errno));
return;
}
printf(“gateway is : %s\n”, inet_ntoa(gw->sin_addr));
return 0;
}

Thanks in advance

Jeff

Jeff <fleuryj@thmulti.com> wrote:

Hello,

I’m trying to get the address of the current gateway by software, and I am
stuck !
I am using a routing socket and the RTM_GET command, trying to get the
default route (route to 0.0.0.0, with a 0.0.0.0 netmask), but it always
returns me 0.0.0.0 in the gateway field (whereas the default route is set to
sth else in the routing table, I checked).
Can anyone help me on this ?? Here is my current code :

#include <sys/socket.h
#include <sys/uio.h
#include <unistd.h
#include <net/route.h
#include <netinet/in.h
#include <netdb.h
#include <stdio.h
#include <libgen.h
#include <arpa/inet.h
#include <process.h
#include <errno.h

struct my_rt_t {
struct rt_msghdr rtm;
struct sockaddr_in dst;
struct sockaddr_in gw;
struct sockaddr_in msk;
};

int main() {
struct my_rt_t my_rt;
struct rt_msghdr *rtm;
struct sockaddr_in *dst, *msk, *gw;
int s;

if ((s=socket(AF_ROUTE, SOCK_RAW, 0)) == -1) {
printf(“error on socket”);
return;
}

memset(&my_rt, 0x0, sizeof(my_rt));
rtm = &(my_rt.rtm);
dst = &(my_rt.dst);
msk = &(my_rt.msk);
gw = &(my_rt.gw);

dst->sin_len = sizeof(*dst);
dst->sin_family = AF_INET;

msk->sin_len = sizeof(*msk);
msk->sin_family = AF_INET;

rtm->rtm_type = RTM_GET;
rtm->rtm_flags |= RTF_GATEWAY;
rtm->rtm_msglen = sizeof(my_rt);
rtm->rtm_version = RTM_VERSION;
rtm->rtm_seq = 1234;
rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
rtm->rtm_pid = getpid();
if (write(s, rtm, rtm->rtm_msglen) < 0) {
printf(“error on write : %s”, strerror(errno));
return;
}

/* Read back the response */
if(read(s, rtm, rtm->rtm_msglen) == -1) {
printf(“error on write : %s”, strerror(errno));
return 1;
}

printf(“gateway is : %s\n”, inet_ntoa(gw->sin_addr));
return 0;
}

Thanks in advance

Jeff