jackhab
jackhab

Reputation: 17708

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

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.

Upvotes: 9

Views: 16835

Answers (3)

Hasturkun
Hasturkun

Reputation: 36422

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.

Upvotes: 7

Stephane
Stephane

Reputation: 1

You need to use socket option IP_MULTICAST_IF.

See here: Multicast-HOWTO-6.html

Upvotes: 0

Oswald
Oswald

Reputation: 31685

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.

Upvotes: 10

Related Questions