3

I have 2 Ethernet interfaces configured in my Linux Machine. Lets say Interface A and Interface B.

I am writing a tcp client socket program and need to send the packets on the defined interface.

For example.

./client -intf interface A/B

if it the input is interface A, then the client has to send packets only via interface A and vice versa.

Could anyone provides some hint on how to implement this?.

Thanks in advance!!!

Saravana
  • 31
  • 1
  • 6

2 Answers2

6

You can use bind() before you call connect() to bind your client socket to a particular IP address. You can use getifaddrs() to list the interfaces and their associated IP addresses.

caf
  • 233,326
  • 40
  • 323
  • 462
  • Hello thanks for the input. Could you provide little more information. – Saravana Nov 21 '12 at 07:40
  • 1
    @Saravana: Call `getifaddrs()` to find the mapping from interface names to IP addresses. Create a `sockaddr_in` / `sockaddr_in6` structure with the IP address of the interface you want to use and a port of 0, and pass that to `bind()` on your socket before you call `connect()`. – caf Nov 21 '12 at 08:14
  • thank you so much caf. So in bind we are binding to the ip address of the Ip interface. but where shall we provide the server Ip actually it needs to connect?. – Saravana Nov 21 '12 at 20:23
  • @Saravana: In `connect()`, as normal. – caf Nov 21 '12 at 20:50
0

Had to do something similar before, have you tried this? https://austinmarton.wordpress.com/2011/09/14/sending-raw-ethernet-packets-from-a-specific-interface-in-c-on-linux/

bjrnt
  • 2,552
  • 3
  • 27
  • 38
  • I have seen this. I need little more information. If i dont provide the interface name, my application has to use the default routing. How can implement this i dont give interface name.? – Saravana Nov 21 '12 at 07:41