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

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

问题描述

第一次发帖,希望这是一个简单的:

first time poster, hopefully this is an easy one:

我需要向一个硬件发送一个广播数据包,当它启动时,它与我的机器位于不同的子网中,以便告诉它将其 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.

所以基本上这个示例代码在测试环境中的 Fedora 19 上工作(在我启用了 DHCP 的较大网络上),直到我尝试静态设置我的 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
", 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:

//绑定到正确的接口,以便能够在没有默认网关的情况下发送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天全站免登陆