SO_BINDTODEVICE Linux 套接字选项的问题 [英] Problems with SO_BINDTODEVICE Linux socket option
问题描述
我有一台带有两个网卡的 PC.一个 (eth0
) 用于 LAN/互联网,另一个用于与一个微控制器设备的 UDP 通信.微控制器具有 IP (192.168.7.2) 和 MAC 地址.第二个 pc 网络适配器 (eth1
) 具有 192.168.7.1.
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.
微控制器有一个非常简单的 IP 堆栈,因此 mc 发送 UDP 数据包的最简单方法是广播它们.
The microcontroller has a very simple IP stack, so the easiest way for the mc to send UDP packets is to broadcast them.
在 PC 端,我想接收广播 - 但只能来自 eth1
.所以我尝试将 UDP 套接字绑定到 eth1
设备.
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.
问题(源代码如下):
setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, device, sizeof(device))
需要root权限,为什么?(以用户身份设置其他选项)
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)
给出协议不可用".我想回读我通过 setsockopt
命令设置的设备.
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.
在哪里可以找到好的信息?我检查了一些 Linux 编程、网络书籍,但例如我只在互联网上找到的 SO_BINDTODEVICE
选项.
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.
我冗长(脏)的测试程序显示了问题.设置和取回 SO_RCVTIMEO
和 SO_BROADCAST
选项按预期工作.
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)"
使用 sudo 运行给出:
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 <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <errno.h>
#define MC_IP "192.168.7.2"
#define MC_PORT (54321)
#define MY_PORT (54321)
#define MY_DEVICE "eth1"
#define BUFFERSIZE (1000)
/* global variables */
int sock;
struct sockaddr_in MC_addr;
struct sockaddr_in my_addr;
char buffer[BUFFERSIZE];
int main(int argc, char *argv[])
{
unsigned int echolen, clientlen;
int rc, n;
char opt_buffer[1000];
struct protoent *udp_protoent;
struct timeval receive_timeout;
int optval;
socklen_t opt_length;
/* Create the UDP socket */
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
{
printf ("%s: failed to create UDP socket (%s)
",
argv[0], strerror(errno));
exit (EXIT_FAILURE);
}
printf ("UDP socket created
");
/* set the recvfrom timeout value */
receive_timeout.tv_sec = 5;
receive_timeout.tv_usec = 0;
rc=setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &receive_timeout,
sizeof(receive_timeout));
if (rc != 0)
{
printf ("%s: could not set SO_RCVTIMEO (%s)
",
argv[0], strerror(errno));
exit (EXIT_FAILURE);
}
printf ("set timeout to
time [s]: %d
time [ms]: %d
", receive_timeout.tv_sec, receive_timeout.tv_usec);
/* verify the recvfrom timeout value */
rc=getsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &receive_timeout, &opt_length);
if (rc != 0)
{
printf ("%s: could not get socket options (%s)
",
argv[0], strerror(errno));
exit (EXIT_FAILURE);
}
printf ("timeout value
time [s]: %d
time [ms]: %d
", receive_timeout.tv_sec, receive_timeout.tv_usec);
/* allow broadcast messages for the socket */
int true = 1;
rc=setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &true, sizeof(true));
if (rc != 0)
{
printf ("%s: could not set SO_BROADCAST (%s)
",
argv[0], strerror(errno));
exit (EXIT_FAILURE);
}
printf ("set SO_BROADCAST
");
/* verify SO_BROADCAST setting */
rc=getsockopt(sock, SOL_SOCKET, SO_BROADCAST, &optval, &opt_length);
if (optval != 0)
{
printf("SO_BROADCAST is enabled
");
}
/* bind the socket to one network device */
const char device[] = MY_DEVICE;
rc=setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, device, sizeof(device));
if (rc != 0)
{
printf ("%s: could not set SO_BINDTODEVICE (%s)
",
argv[0], strerror(errno));
exit (EXIT_FAILURE);
}
printf ("SO_BINDTODEVICE set
");
/* verify SO_BINDTODEVICE setting */
rc = getsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (void *)buffer, &opt_length);
if (rc != 0)
{
printf ("%s: could not get SO_BINDTODEVICE (%s)
",
argv[0], strerror(errno));
exit (EXIT_FAILURE);
}
if (rc == 0)
{
printf("SO_BINDTODEVICE is: %s
", buffer);
}
/* Construct the server sockaddr_in structure */
memset(&MC_addr, 0, sizeof(MC_addr)); /* Clear struct */
MC_addr.sin_family = AF_INET; /* Internet/IP */
MC_addr.sin_addr.s_addr = inet_addr(MC_IP); /* IP address */
MC_addr.sin_port = htons(MC_PORT); /* server port */
/* bind my own Port */
my_addr.sin_family = AF_INET;
my_addr.sin_addr.s_addr = INADDR_ANY; /* INADDR_ANY all local addresses */
my_addr.sin_port = htons(MY_PORT);
rc = bind (sock, (struct sockaddr *) &my_addr, sizeof(my_addr));
if (rc < 0)
{
printf ("%s: could not bind port (%s)
",
argv[0], strerror(errno));
exit (EXIT_FAILURE);
}
printf ("port bound
");
/* identify mc */
buffer[0] = (char)1;
buffer[1] = (char)0;
send_data (buffer, 2);
printf ("sent command: %d
", (char)buffer[0]);
rc=receive_data(buffer);
printf ("%d bytes received
", rc);
buffer[rc] = (char)0; /* string end symbol */
printf ("%d - %s
", (int)(char)buffer[0], &buffer[1]);
close(sock);
printf ("socket closed
");
exit(0);
}
/* send data to the MC *****************************************************/
/* buffer points to the bytes to send */
/* buf_length is the number of bytes to send */
/* returns allways 0 */
int send_data( char *buffer, int buf_length )
{
int rc;
rc = sendto (sock, buffer, buf_length, 0,
(struct sockaddr *) &MC_addr,
sizeof(MC_addr));
if (rc < 0)
{
printf ("could not send data
");
close (sock);
exit (EXIT_FAILURE);
}
return(0);
}
/* receive data from the MC *****************************************************/
/* buffer points to the memory for the received data */
/* max BUFFERSIZE bytes can be received */
/* returns number of bytes received */
int receive_data(char *buffer)
{
int rc, MC_addr_length;
MC_addr_length = sizeof(MC_addr);
rc = recvfrom (sock, buffer, BUFFERSIZE, 0,
(struct sockaddr *) &MC_addr,
&MC_addr_length);
if (rc < 0)
{
printf ("could not receive data
");
close (sock);
exit (EXIT_FAILURE);
}
return(rc);
}
推荐答案
在看到关于 SO_BINDTODEVICE 实际使用方式的相互矛盾的答案后,我一直在研究这个问题.一些来源声称正确的用法是传入一个 struct ifreq
指针,它具有通过 ioctl 获取的设备名称和索引.例如:
I have been looking into this for a while after seeing conflicting answers to how SO_BINDTODEVICE is actually used. Some sources claim that the correct usage is to pass in a struct ifreq
pointer, which has the device name and index obtained via an ioctl. For example:
struct ifreq ifr;
memset(&ifr, 0, sizeof(struct ifreq));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "eth0");
ioctl(fd, SIOCGIFINDEX, &ifr);
setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, (void*)&ifr, sizeof(struct ifreq));
Beej 的网络教程所说的将设备名称作为一个字符指针.例如:
Where as Beej's networking tutorial says to pass the device name as a char pointer. For example:
char *devname = "eth0";
setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, devname, strlen(devname));
这两种方法我都试过了,它们都做了需要的,但我想注意的是,第一种方法获得的设备索引是多余的.如果您查看 net/core/中的内核代码sock.c,sock_bindtodevice
只是复制设备名称字符串,调用dev_get_by_name_rcu
获取设备并绑定到它.
I have tried both of these methods and they both do what is required, but I wanted to note that the device index obtained in the first method is superfluous. If you look at the kernel code in net/core/sock.c, sock_bindtodevice
just copies the device name string, calls dev_get_by_name_rcu
to get the device and binds to it.
第一种方法起作用的原因是设备名称是 ifreq
结构中的第一个元素,参见 http://linux.die.net/man/7/netdevice.
The reason that the first approach works is that the device name is the first element in the ifreq
structure, see http://linux.die.net/man/7/netdevice.
这篇关于SO_BINDTODEVICE Linux 套接字选项的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!