Error Setting Socket Option So_bindtodevice
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of
So_bindtodevice Example
this site About Us Learn more about Stack Overflow the company Business Learn so_bindtodevice linux more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question
So_bindtodevice Vs Bind
x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Problems so_bindtodevice python with SO_BINDTODEVICE Linux socket option up vote 12 down vote favorite 6 I have a PC with two network cards. One (eth0) is for LAN/internet and the other for UDP communication with one microcontroller device. The microcontroller has an IP (192.168.7.2) and a MAC address. The second pc network adapter (eth1) has 192.168.7.1. The microcontroller has a very simple IP stack, so the so_bindtodevice windows easiest way for the mc to send UDP packets is to broadcast them. On the PC side I'd like to receive the broadcasts - but only from eth1. So I try to bind the UDP socket to the eth1 device. The problems (source code below): setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, device, sizeof(device)) requires root privileges, why? (setting other options works as user) getsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (void *)buffer, &opt_length) gives "Protocol not available". I would like to read back the device I set via setsockopt command. Where can I find good info? I checked some Linux-programming, network books, but for example the SO_BINDTODEVICE option I've only found on the internet. My lengthy (dirty) test program shows the problems. Setting and getting back the SO_RCVTIMEO and SO_BROADCAST options works as expected. Running the code as user exits with: could not set SO_BINDTODEVICE (Operation not permitted)" Running with sudo gives: SO_BINDTODEVICE set ./mc-test: could not get SO_BINDTODEVICE (Protocol not available) So, setting the option seems to work but reading it back is not possible? /* SO_BINDTODEVICE test */ #include you constrain a packet to go out one specific interface? If you answered "bind() the socket to an address," you should read on. Why might one need to strictly control where packets can be routed? The best use case I know is when ethernet is used as a control plane inside a product. Packets intended to go to another card within so_bindtodevice not working the chassis must not, under any circumstances, leave the chassis. You don't want bugs or misconfiguration to result in leaking control traffic. The bind() system call is frequently misunderstood. It is used to bind to a particular IP address. Only packets destined to that IP address will be received, and any transmitted packets will carry that IP address as their source. bind() does http://stackoverflow.com/questions/1207746/problems-with-so-bindtodevice-linux-socket-option not control anything about the routing of transmitted packets. So for example, if you bound to the IP address of eth0 but you send a packet to a destination where the kernel's best route goes out eth1, it will happily send the packet out eth1 with the source IP address of eth0. This is perfectly valid for TCP/IP, where packets can traverse unrelated networks on their way to the destination. In Linux, to control the physical topology of http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html communication you use the SO_BINDTODEVICE socket option. #include *optlen); int setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen); Description Sockets are fairly configurable beasts. In fact, they are so configurable, I'm not even going to cover it all here. It's probably system-dependent anyway. But I will talk about the basics. Obviously, these functions get and set certain http://beej.us/guide/bgnet/output/html/multipage/setsockoptman.html options on a socket. On a Linux box, all the socket information is in the man page http://serverfault.com/questions/598093/heartbeat-doesnt-work-on-virtual-interface for socket in section 7. (Type: "man 7 socket" to get all these goodies.) As for parameters, s is the socket you're talking about, level should be set to SOL_SOCKET. Then you set the optname to the name you're interested in. Again, see your man page for all the options, but here are some of the most fun ones: SO_BINDTODEVICE Bind this socket to a error setting symbolic device name like eth0 instead of using bind() to bind it to an IP address. Type the command ifconfig under Unix to see the device names. SO_REUSEADDR Allows other sockets to bind() to this port, unless there is an active listening socket bound to the port already. This enables you to get around those "Address already in use" error messages when you try to restart your server after a crash. SO_BROADCAST Allows UDP datagram (SOCK_DGRAM) sockets to send and receive error setting socket packets sent to and from the broadcast address. Does nothing—NOTHING!!—to TCP stream sockets! Hahaha! As for the parameter optval, it's usually a pointer to an int indicating the value in question. For booleans, zero is false, and non-zero is true. And that's an absolute fact, unless it's different on your system. If there is no parameter to be passed, optval can be NULL. The final parameter, optlen, should be set to the length of optval, probably sizeof(int), but varies depending on the option. Note that in the case of getsockopt(), this is a pointer to a socklen_t, and it specifies the maximum size object that will be stored in optval (to prevent buffer overflows). And getsockopt() will modify the value of optlen to reflect the number of bytes actually set. Warning: on some systems (notably Sun and Windows), the option can be a char instead of an int, and is set to, for example, a character value of '1' instead of an int value of 1. Again, check your own man pages for more info with "man setsockopt" and "man 7 socket"! Return Value Returns zero on success, or -1 on error (and errno will be set accordingly.) Example int optval; int optlen; char *optval2; // set SO_REUSEADDR on a socket to true (1): optval = 1; setsockopt(s1, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval); // bind a socket to a device name (might not work on all systems): optval2 = "eth1"; // 4 by Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Server Fault Questions Tags Users Badges Unanswered Ask Question _ Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top heartbeat doesn't work on virtual interface up vote 1 down vote favorite I try to use vertual interface as broadcast for heartbeat(bcast in ha.cf). When I try it I see this message in syslog: May 23 12:17:26 Ubuntu2 heartbeat: [1107]: ERROR: glib: Error setting socket option SO_BINDTODEVICE: No such device May 23 12:17:26 Ubuntu2 heartbeat: [1107]: ERROR: make_io_childpair: cannot open bcast eth1:0 But when I try use physic interface it work. My ifconfig: eth1 Link encap:Ethernet HWaddr 08:00:27:6c:46:05 inet addr:192.168.1.162 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe6c:4605/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1092 errors:0 dropped:0 overruns:0 frame:0 TX packets:1061 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:69886 (69.8 KB) TX bytes:67058 (67.0 KB) eth1:0 Link encap:Ethernet HWaddr 08:00:27:6c:46:05 inet addr:192.168.1.62 Bcast:192.168.1.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:1323 errors:0 dropped:0 overruns:0 frame:0 TX packets:1323 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:120776 (120.7 KB) TX bytes:120776 (120.7 KB) Servers are pingable from each other from all interfaces. How I should setuSo_bindtodevice Operation Not Permitted
So_bindtodevice Root