将UDP数据包广播到255.255.255.255 [英] Broadcasting UDP packet to 255.255.255.255

查看:1799
本文介绍了将UDP数据包广播到255.255.255.255的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

初次张贴者,希望这是一个简单的例子:

first time poster, hopefully this is an easy one:

我需要将广播数据包发送到一个硬件,该硬件在开机时位于与我的机器不同的子网上,以便告诉它将其IP地址重置为我的网络上的IP地址.但是,除非使用DHCP,否则似乎无法在自己的子网中广播,最终我将无法做到这一点.网络上没有路由器,只是我的机器和我要与之交谈的机器之间的简单切换,以及网络上的其他两台Linux机器.

I need to send a broadcast packet to a piece of hardware which, when it powers up, is on a different subnet than my machine, in order to tell it to reset its IP address to one that's on my network. But, I can't seem to broadcast off my own subnet unless I'm using DHCP, which ultimately I won't be able to do. There is no router on the network, just a simple switch between my machine and the box I'm trying to talk to, plus a couple other Linux machines on the network.

因此,基本上,此示例代码在测试环境(在启用了DHCP的较大网络上)的Fedora 19上运行,直到尝试静态设置IP地址为止:

So basically this example code WORKS on Fedora 19 in a test environment (on a larger network where I have DHCP enabled), until I try to statically set my IP address:

int main(int argc, char *argv[])
{
    int sock;
    if( (sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        perror("socket : ");
        return -1;
    }

    int broadcast = 1;
    if( setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) != 0 )
    {
        perror("setsockopt : ");
        close(sock);
        return -1;
    }

    char *ip = "255.255.255.255";
    char * msg = "Hello World!";

    struct sockaddr_in si;
    si.sin_family = AF_INET;
    si.sin_port   = htons( 4444 );
    inet_aton( ip, &si.sin_addr.s_addr );

    /* send data */
    size_t nBytes = sendto(sock, msg, strlen(msg), 0, 
                    (struct sockaddr*) &si, sizeof(si));

    printf("Sent msg: %s, %d bytes with socket %d to %s\n", msg, nBytes, sock, ip);

    return 0;
}

如果我使用DHCP,则输出为:

If I'm using DHCP, the output is:

Sent msg: Hello World!, 12 bytes with socket 3 to 255.255.255.255

我可以看到数据包在Wireshark中发出.

And I can see the packet go out in Wireshark.

然后,如果我将IP静态设置为192.168.100.1,则会得到:

Then if I set my IP statically to say, 192.168.100.1, I get:

Sent msg: Hello World!, -1 bytes with socket 3 to 255.255.255.255

我在Wireshark中看不到该数据包.而且我可以确认ifconfig中显示的TX数据包数量没有增加.我尝试禁用防火墙:

And I don't see the packet in Wireshark. And I can confirm that the number of TX packets shown in ifconfig doesn't increment. I tried disabling the firewall:

sudo iptables -F

但是什么也没做.有人看到我想念的东西吗?我可以广播到192.168.100.255,但这不会进入我需要交谈的框,例如默认为192.168.200.10,即255.255.255.0.我可以通过更改网络上所有其他设备的网络参数来使其工作,但这并不是一个选择.

but that didn't do anything. Anybody see something that I'm missing? I can broadcast to 192.168.100.255, but that won't make it to the box I need to talk to, which for example might be at 192.168.200.10, 255.255.255.0 by default. I could make it work by changing the network parameters of everything else on the network, but that's not really an option.

关于某些评论和答案,请注意,我没有使用路由器,并且可以复制行为,而只需在计算机和另一台计算机之间建立一条电线即可.所以,真正的问题是,Linux为什么不发送数据包?我不清楚,但是我怀疑Linux本身会丢弃跨子网的广播数据包,除非它可以将该决定委托给网络上的另一个机构.无论如何,鉴于我的网络很小,我只需要解决它.

In regards to some of the comments and answers, note that I'm not using a router and can replicate the behaviour with nothing but a wire between my computer and the other box. So, really the question is, why isn't Linux sending the packets? I don't know obviously, but I'm suspecting that Linux itself drops cross-subnet broadcast packets unless it can delegate that decision to another authority on the network. In any event, given that my network is so small I'll just have to work around it.

推荐答案

我只是遇到了这个问题.一切正常,直到删除网关,然后在尝试从套接字发送消息时出现网络无法访问的错误.

I just ran into this issue. Everything worked fine until I removed the gateway and then I get a network unreachable error when trying to send from the socket.

由于我正在使用广播消息来配置盒子的网络设置,因此我无法选择配置网关地址以使其正常工作.但是,由于我的盒子只有一个网络接口,所以我找到了以下解决方案:

Since I am using the broadcast messages to configure the network settings of my box it is not an alternative for me to configure the gateway address to get it working. However since my box only has a single network interface I found the following solution:

// Bind to the proper interface to be able to send without default gateway char netif[] = "eth0"; setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, netif, sizeof(netif));

// Bind to the proper interface to be able to send without default gateway char netif[] = "eth0"; setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, netif, sizeof(netif));

使用此方法,无论任何默认网关如何,我都可以将其发送到套接字上的广播255.255.255.255地址.只要您知道要使用的接口,该解决方案就可以与多个网络接口一起使用.

With this I am able to send to the broadcast 255.255.255.255 address on the socket regardless of any default gateway. This solution should work with multiple network interfaces as long as you know what interface you want to use.

这篇关于将UDP数据包广播到255.255.255.255的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆