2

I have an application in which i bound a socket to an outgoing interface from among a few using SO_BINDTODEVICE.

Now the problem is when i receive a response, i need to know which interface it came from. The ip address alone from which the response came is not enough, because same ip is bound to multiple interfaces.

I have used the code below provided in stackoverflow questions. But it fails giving segmentation faults in the second iteration of for loop (the first iteration went into continue part of loop).

Can some one help?

C++ DGRAM socket get the RECEIVER address

How to tell which interface the socket received the message from?

Community
  • 1
  • 1
fkl
  • 5,412
  • 4
  • 28
  • 68
  • What is the OS? Are you trying this on a TCP or UDP socket? – EdwardH Feb 20 '12 at 21:00
  • Linux (POSIX API). It's a udp socket. Though now the problem is resolved. It appears that i have to set the IP_PKTINFO option right after socket creation. If i do it latter just before the receive call, it returns interface index 0 always. During this the socket was used to send data out too, then was added to a select descriptor set for receiving. – fkl Feb 21 '12 at 21:45
  • possible duplicate of [How to tell which interface the socket received the message from?](http://stackoverflow.com/questions/603577/how-to-tell-which-interface-the-socket-received-the-message-from) – Jason C Oct 26 '13 at 18:21

1 Answers1

0

The problem is resolved. The IP_PKTINFO option should be set right after socket creation before any other use of the socket and it works fine.

In my case, i was using the socket after creation to send data out and latter added to a select descriptor set for reading. Then finally just before calling receive i was setting IP_PKTINFO and it returned interface index 0 always.

fkl
  • 5,412
  • 4
  • 28
  • 68