I want to analyze the returned kernel routes the call function sysctlwith arguments: { CTL_NET, PF_ROUTE, 0, AF_INET, NET_RT_DUMP2, 0 }.
After that, I get several routing messages, in all of them I found a problem: struct sockaddr*the index RTAX_NETMASKdoes not contain a valid one struct sockaddr.
The code for extracting addresses from struct rt_msghdr2returned sysctlis the following:
#define ROUNDUP(a) \
((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint32_t) - 1))) : sizeof(uint32_t))
struct rt_msghdr2* routeMsg = (struct rt_msgdr2*)buffer;
struct sockaddr* sockAddrArray[RTAX_MAX];
memset(sockAddrArray, 0, sizeof(sockAddrArray));
struct sockaddr* currentSockAddr;
currentSockAddr = (struct sockaddr*)(routeMsg + 1);
for (int i = 0; i < RTAX_MAX; i++) {
if (routeMsg->rtm_addrs & (1 << i)) {
sockAddrArray[i] = currentSockAddr;
currentSockAddr = (struct sockaddr *)(ROUNDUP(currentSockAddr->sa_len) + (char *)currentSockAddr);
}
else {
sockAddrArray[i] = NULL;
}
}
Even if it routeMsgclaims to have a netmask when I check the contents, I find that it has something like this:
sockAddrArray[RTAX_NETMASK]->sa_len = 0
sockAddrArray[RTAX_NETMASK]->sa_type = \xff
sockAddrArray[RTAX_NETMASK]->sa_data = { \xff, \xff, \0, ...}
The strange thing is that in the corresponding route on the host (based on the network and gateway addresses) there is a netmask 255.255.255.0that matches the direct contents of the netmask too well sockaddr.
, , RTAX_NETMASK , .
- , ?