socket绑定指定网卡发包
socket綁定指定網(wǎng)卡發(fā)包
-
- SO_BINDTODEVICE
網(wǎng)絡(luò)編程中有時明明用eth0的地址來bind一個udp套接口, 可是發(fā)出去的包卻是從eht1走的, 在網(wǎng)上找到這么一段話解釋該問題:
在多 IP/網(wǎng)卡主機(jī)上,UDP 包/協(xié)議會自動根據(jù)路由最優(yōu)來選擇從哪個網(wǎng)卡發(fā)數(shù)據(jù)包出去,即使你在此之前把該 SOCKET 綁定到了另一個網(wǎng)卡上。這樣一來,如果你執(zhí)行了綁定,則在 UDP 包中所代表的源 IP 字段可能不是你的數(shù)據(jù)包真正發(fā)出的地址。
?
比如:你有兩個網(wǎng)卡分別為:A—192.168.1.100; B-192.168.2.100; mask-255.255.255.0
此時你如果將一 UDP 套接字 S 綁定到了 A 上,但是要發(fā)的目的地址為 192.168.2.110,這時包實(shí)際是從網(wǎng)卡 B 上發(fā)出去的(根據(jù)路由最優(yōu)原則),但在包頭的結(jié)構(gòu)里面,由于 BIND 的緣故,可能指向的源地址為 A。這樣源 IP 地址就產(chǎn)生了與實(shí)際不相符的錯誤。
要解決這種問題, 可以把套接字綁定到一個指定的網(wǎng)絡(luò)設(shè)備, “eth0”, "ppp0"等.
示例1
int sock; struct ifreq ifr; sock = socket(AF_INET, SOCK_DGRAM, 0); memset(&ifr, 0x00, sizeof(ifr)); strncpy(ifr.ifr_name, "eth0", strlen("eth0")); setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (char *)&ifr, sizeof(ifr));示例2
int sock; struct sockaddr_ll sl; struct ifreq ifr; sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_IPV6)); memset(&sl, 0x00, sizeof(sl)); memset(&ifr, 0x00, sizeof(ifr)); sl.sll_family = AF_PACKET; sl.sll_protocol = htons(ETH_P_IPV6); strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name)); ioctl(fd, SIOCGIFINDEX, &ifr); sl.sll_ifindex = ifr.ifr_ifindex; bind(fd, (struct sockaddr *)&sl, sizeof(sl));示例3
int sock; struct sockaddr addr; sock = socket(PF_PACKET, SOCK_PACKET, ETH_P_IP); memset(&addr, 0x00, sizeof(addr)); addr.sa_family = PF_PACKET; strncpy(addr.sa_data, "eth0", sizeof(addr.sa_data)); bind(sock, &addr, sizeof(addr));SO_BINDTODEVICE
Bind this socket to a particular device like “eth0”, as specified 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-terminated interface name string with the maximum size of IFNAMSIZ. If a socket is bound to an interface, only packets received from that particular interface are processed by the socket. Note that this only works for some socket types, particularly AF_INET sockets. It is not supported for packet sockets (use normal bind(8) there).
總結(jié)
以上是生活随笔為你收集整理的socket绑定指定网卡发包的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多网卡指定网卡发送数据
- 下一篇: socket开发:一台服务器同一端口同时