Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send UDP packet from a specific interface on Linux?

Tags:

c

linux

sockets

udp

How do I send UDP packet from a specific interface on Linux using C? Should I use bind? Is it possible to send UDP from the interface not having IP address?

Thanks.

like image 828
jackhab Avatar asked Jan 03 '11 13:01

jackhab


2 Answers

Use bind. You cannot send UDP packets via an interface that does not have an IP address, because UDP uses the Internet Protocol and the Internet Protocol requires an IP address.

like image 89
Oswald Avatar answered Nov 12 '22 09:11

Oswald


You can bind a socket to a specific interface by using the SO_BINDTODEVICE socket option, however this requires root privileges.

Alternately, you can set the IP_PKTINFO option, and use sendmsg for sending, setting the in_pktinfo's ipi_ifindex to the index of your interface.

like image 6
Hasturkun Avatar answered Nov 12 '22 08:11

Hasturkun