Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup multiple routing entries for socket?

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.

like image 827
Gilson PJ Avatar asked Feb 15 '17 07:02

Gilson PJ


People also ask

Which command is used to add entries to a routing table?

The route command allows you to make manual entries into the network routing tables. The route command distinguishes between routes to hosts and routes to networks by interpreting the network address of the Destination variable, which can be specified either by symbolic name or numeric address.

How do I add a static route in AIX?

Adding Static Routes in AIXStep 1: Go to the SMITTY menu for routes. Step 2: Select Type of route 'net' or 'host' (if default route then leave set to 'net'). Step 3: Enter the destination address. Step 6: Enter the network interface for this route.

What is use route?

In computing, route is a command used to view and manipulate the IP routing table in Unix-like and Microsoft Windows operating systems and also in IBM OS/2 and ReactOS. Manual manipulation of the routing table is characteristic of static routing.


1 Answers

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.

like image 51
Gilson PJ Avatar answered Oct 20 '22 20:10

Gilson PJ