gpt4 book ai didi

c++ - 同一端口但不同接口(interface)上的多个广播接收

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:29:19 24 4
gpt4 key购买 nike

如果在 eth0 上接收到广播,如果在 wlan0 上接收到另一个广播,我正在尝试构建一个小型个人 DHCP 服务器来服务于特定范围,但我不能在同一地址上绑定(bind)多个接口(interface):端口组合(255.255.255.255:67)我听说过 SO_REUSABLE 但我不知道如何实现它,当然这是否是实现它的好方法

实际上这是我的代码:

#include <stdio.h>   
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <errno.h>
#include <fstream>
#include <iostream>
#include <vector>

#define BUFLEN 1024
#define PORT 67
using namespace std;

char *ipAddrFromInterface(char *apInterfaceName) //this function is not from me
{
return "255.255.255.255";

/*char *if_name = (char *) apInterfaceName;
struct ifreq ifr;
size_t if_name_len = strlen(if_name);

if(if_name_len < sizeof(ifr.ifr_name))
{
memcpy(ifr.ifr_name, if_name, if_name_len);
ifr.ifr_name[if_name_len] = 0;
}
else
printf("interface name is too long\n");

int fd = socket(AF_INET, SOCK_DGRAM, 0);

if(fd == -1)
printf("A => %s\n", strerror(errno));

if(ioctl(fd, SIOCGIFADDR, &ifr) == -1)
{
int temp_errno = errno;
close(fd);
printf("B => %s\n", strerror(temp_errno));
}

if(ioctl(fd, SIOCGIFADDR, &ifr) == -1)
{
int temp_errno = errno;
close(fd);
printf("C => %s\n", strerror(temp_errno));
}

close(fd);

struct sockaddr_in* ipaddr = (struct sockaddr_in*) &ifr.ifr_addr;
return inet_ntoa(ipaddr->sin_addr);*/
}

struct socketData
{
int sock;
sockaddr_in socket;
char *interfaceName;
};

void print(int i)
{
printf("%d\n", i);
fflush(stdout);
}

void server_receive_thread(vector<char*> aInterfaceList)
{
int socketIndex = 0;
struct sockaddr_in localSock;
int socketDescriptor; int socketLength;
vector<socketData> aSockets;

for( ; socketIndex < aInterfaceList.size(); socketIndex++)
{
socketData socketD;
char *apInterfaceName = aInterfaceList.at(socketIndex);

if((socketDescriptor = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
printf("can't listen on interface %s... sleeping\n", apInterfaceName);
}
else
{
memset(&localSock, 0, sizeof(localSock));
localSock.sin_family = AF_INET;
localSock.sin_port = htons(PORT);

inet_aton(ipAddrFromInterface(apInterfaceName), &localSock.sin_addr);
setsockopt(socketDescriptor, SOL_SOCKET, SO_BINDTODEVICE, apInterfaceName, sizeof(apInterfaceName));

if(bind(socketDescriptor, (struct sockaddr *) &localSock, sizeof(localSock)) == -1)
{
printf("can't bind interface %s to listen on port %d... sleeping\n", apInterfaceName, PORT);
}
else
{
printf("bound to interface %s on port %d\n", apInterfaceName, PORT);
socketD.sock = socketDescriptor;
socketD.socket = localSock;
socketD.interfaceName = apInterfaceName;
aSockets.push_back(socketD);
}
}
}

fd_set master;
int fdMax = -1;
while(1)
{
FD_ZERO(&master);

for(int iSock = 0; iSock < aSockets.size(); iSock++)
{
socketData d = aSockets.at(iSock);
FD_SET(d.sock, &master);

if(d.sock > fdMax)
fdMax = d.sock;
}

printf("fdmax is : ");
print(fdMax);

if(select(fdMax + 1, &master, NULL, NULL, NULL) == -1)
print(2);

print(200);

for(int iSock = 0; iSock < aSockets.size(); iSock++)
{
socketData d = aSockets.at(iSock);

if(FD_ISSET(d.sock, &master))
print(3);
}
print(1);
}
}

int main()
{
std::vector<char*> interfaceList;
interfaceList.push_back("wlan0");
interfaceList.push_back("eth0");
server_receive_thread(interfaceList);
return 0;
}

最佳答案

您不需要每个接口(interface)一个套接字。只需将一个套接字绑定(bind)到 0.0.0.0 和所需的端口。然后它将通过所有接口(interface)接收。您当然不能也不需要绑定(bind)到 255.255.255.255。

或者,将其绑定(bind)到连接到您要服务的范围的单个 IP 地址。

关于c++ - 同一端口但不同接口(interface)上的多个广播接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30000501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com