How to configure multiple routing entries for a socket?

I am very new to static routing, our client asked me to implement static routing for sockets. When I googled, I ran into rtentry to set up routing information. When I opened this structure, I saw 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 set up multiple entries here? creating multiple rtentry and calling ioctl(FileDes, SIOCADDRT, &rtentry) fix my problem?

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

If I tune, how can I check this? It will be useful if you can provide a link to learn more about these things.

+7
c ++ linux sockets routing
source share
1 answer

Finally, I got the 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 multiposition routing entries and add FD to the socket. and this will update the routing table of the entire system.

its similar to the route add .. command

for testing, I installed the gateway as my PC ip-address and started laying wires there. after configuring the routing configuration, the specified IP range is sent to my computer. Thanks to @osgx for the information that it actually sets up the routing table on the system.

+3
source share

All Articles