Saturday, February 25, 2017

How to setup multiple routing entries for socket?

Leave a Comment

I am very new to static routing, our client requested to implement static routing for sockets. When I googled I came across with rtentry to set routing information. When I opened this structure I saw the fields for static routing

struct sockaddr rt_dst;     /* Target address.  */ struct sockaddr rt_gateway;     /* Gateway addr (RTF_GATEWAY).  */ struct sockaddr rt_genmask;     /* Target network mask (IP).  */ 

But how can I setup multiple entry here? creating multiple rtentry and calling ioctl(FileDes, SIOCADDRT, &rtentry) will fix my problem?

int32_t FileDes = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); for(auto RtEntry : RtEntriesList) {    ioctl(FileDes, SIOCADDRT, RtEntry)` } 

If I configure, how can I test this? It will be helpful if you can provide a link to know more about these things.

1 Answers

Answers 1

Finally I got my answers.

int32_t FileDes = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);  struct rtentry Route1; struct rtentry Route2; struct rtentry Route3;  // configure Route1 // configure Route2 // configure Route3  RtEntriesList.push_back(&Route1); RtEntriesList.push_back(&Route2); RtEntriesList.push_back(&Route3);  for(auto RtEntry : RtEntriesList) {    ioctl(FileDes, SIOCADDRT, RtEntry); } 

will work, we can create multple routing entries and add to the socket FD. and this will update the system wide routing table.

its similar to route add .. command

for testing i set the gateway as my PC ip address and started wireshark there. after setting routing configurations the given range of IP is routed to my PC. Thanks @osgx for the information that its actually setting the system wide routing table.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment