Linux 套接字编程 套接字选项SO_BINDTODEVICE 绑定接口 示例

man socket(7)里对该选项的描述:

       SO_BINDTODEVICE
              Bind this socket to a particular device like “eth0”, as speci‐
              fied in the passed interface name.  If the name is an empty
              string or the option length is zero, the socket device binding
              is removed.  The passed option is a variable-length null-ter‐
              minated interface name string with the maximum size of IFNAM‐
              SIZ.  If a socket is bound to an interface, only packets
              received from that particular interface are processed by the
              socket.  Note that this works only for some socket types, par‐
              ticularly AF_INET sockets.  It is not supported for packet
              sockets (use normal bind(2) there).

              Before Linux 3.8, this socket option could be set, but could
              not retrieved with getsockopt(2).  Since Linux 3.8, it is
              readable.  The optlen argument should contain the buffer size
              available to receive the device name and is recommended to be
              IFNAMSIZ bytes.  The real device name length is reported back
              in the optlen argument.

 将套接字绑定到指定接口,例如eth0等。如果绑定了接口,这个套接字只能处理由该接口收到的数据。
注意,并不是所有套接字类型都有这个选项。AF_INET套接字支持,但是packet 套接字不支持(不过,可以使用bind函数绑定地址)

如果有多个接口,例如eth0, eth1, ethx......,就可以在创建套接字的时候绑定相应的接口发送数据,例如我的电脑里有两个接口 :

在创建套接字的时候就可以绑定相应的接口发送数据,demo:

/*
 *  Desrciption : 套接字选项SO_BINDTODEVICE使用,需要root权限执行
 *  Author : mason
 *  Date : 201809
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <errno.h>


int main()
{
    int sock;
    struct sockaddr_in addr, raddr;
    char buffer[] = "hello world";

    /* 指定接口 */
    struct ifreq nif;
    char *inface = "eth0";
    strcpy(nif.ifr_name, inface);

    /* 创建套接字 */
    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == sock)
    {
        printf("create socket fail \r\n");
        return;
    }
    else
    {
        printf("create socket success \r\n");
    }

    
    if (inet_aton("192.168.80.129", &addr.sin_addr) != 1)
    {
        printf("addr convert fail \r\n");
        close(sock);
        return;
    }
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8000);

    if (inet_aton("192.168.80.1", &raddr.sin_addr) != 1)
    {
        printf("addr convert fail \r\n");
        close(sock);
        return;
    }
    raddr.sin_family = AF_INET;
    raddr.sin_port = htons(8000);

    #if 0  //绑定地址
    if (bind(sock, (struct sockadd *)&addr, sizeof(addr)) != 0)
    {
        printf("bind fail \r\n");
        close(sock);
        return ;
    }
    else
    {
        printf("bind success \r\n");
    }
    #endif

    /* 绑定接口 */
    if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (char *)&nif, sizeof(nif)) < 0)
    {
        close(sock);
        printf("bind interface fail, errno: %d \r\n", errno);
		return ;		
    }
    else
    {
        printf("bind interface success \r\n");
    }

    /* 发送 */
    sendto(sock, buffer, sizeof(buffer), 0, ((struct sockadd *)&raddr),sizeof(raddr));
    
    close(sock);
    return;
}

分别绑定eth0, eth1发送数据,抓包如下图,可以看到有不同的源地址发送。

程序执行的时候需要使用sudo权限,不然会提示绑定接口失败。

感觉类似的功能完全可以由bind接口实现,即绑定指定IP地址。

 

  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
在使用原始套接字时,可以通过设置套接字选项来指定要使用的网络接口。具体来说,可以使用setsockopt函数设置SO_BINDTODEVICE选项,该选项允许将套接字绑定到指定的网络接口上。示例代码如下: ``` #include <sys/socket.h> #include <netinet/in.h> #include <netinet/ip.h> #include <netinet/tcp.h> #include <unistd.h> #include <string.h> #include <stdio.h> int main(int argc, char* argv[]) { // 创建原始套接字 int sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP); if (sock < 0) { perror("socket"); return -1; } // 设置套接字选项绑定到指定的网络接口上 const char* ifname = "eth0"; // 指定要绑定的网络接口名 if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname)) < 0) { perror("setsockopt"); close(sock); return -1; } // 发送TCP数据包 struct sockaddr_in addr = {0}; addr.sin_family = AF_INET; addr.sin_port = htons(80); addr.sin_addr.s_addr = inet_addr("192.168.1.1"); char buf[1024] = "GET / HTTP/1.1\r\nHost: 192.168.1.1\r\n\r\n"; if (sendto(sock, buf, strlen(buf), 0, (struct sockaddr*)&addr, sizeof(addr)) < 0) { perror("sendto"); close(sock); return -1; } close(sock); return 0; } ``` 在上面的示例代码中,首先使用socket函数创建原始套接字,然后使用setsockopt函数设置SO_BINDTODEVICE选项,将套接字绑定到指定的网络接口上。最后,使用sendto函数发送TCP数据包。需要注意的是,绑定到指定的网络接口上需要具有管理员权限。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值