相关疑难解决方法(0)

如何将原始套接字绑定到特定接口

我的应用程序在CentOS 5.5上运行.我正在使用原始套接字发送数据:

sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if (sd < 0) {
  // Error
}
const int opt_on = 1;
rc = setsockopt(m_SocketDescriptor, IPPROTO_IP, IP_HDRINCL, &opt_on, sizeof(opt_on));
if (rc < 0) {
  close(sd);
  // Error
}
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = my_ip_address;

if (sendto(m_SocketDescriptor, DataBuffer, (size_t)TotalSize, 0, (struct sockaddr *)&sin, sizeof(struct sockaddr)) < 0)  {
  close(sd);
  // Error
}
Run Code Online (Sandbox Code Playgroud)

如何将此套接字绑定到特定的网络接口(例如eth1)?

c sockets linux network-programming raw-sockets

30
推荐指数
2
解决办法
7万
查看次数

使用setsockopt()系统的设备绑定的原始套接字在Fedora core 6(2.6.18-1.2798.fc6)中不起作用

请任何人都可以帮助解决这个问题.请

在下面的示例代码中,我们将原始sock绑定到eth0.但是在运行程序时,原始sock的recvfrom正在同一台机器(xx_86)上从eth0和eth1接收数据包.我不清楚为什么,可以在这个问题上帮助一个人.我希望setsockopt不能正常运行OS:Fedora core 6(2.6.18-1.2798.fc6)

Sampe代码:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <linux/if_ether.h>
#include <net/if.h>
#include <linux/filter.h>
#include <sys/ioctl.h> 
#include <string.h>
#include <arpa/inet.h>

int main(int argc, char **argv) {
int sock, i;
unsigned char buffer[2048];
unsigned char tbuff[2048];
unsigned char *iphead, *ethhead,*phead;
struct ifreq ethreq;

// NOTE: use TCPDUMP to build the filter array.
// set filter to sniff only port 443
// $ sudo tcpdump -dd port 443

// raw for recvfrom eth0 …
Run Code Online (Sandbox Code Playgroud)

c raw-sockets

3
推荐指数
2
解决办法
1万
查看次数

标签 统计

c ×2

raw-sockets ×2

linux ×1

network-programming ×1

sockets ×1