19

我想计算广播地址:

IP:     192.168.3.1
Subnet: 255.255.255.0
=       192.168.3.255

在 C.

我知道方法(在反向 IP 和子网之间进行奇特的按位 OR),但我的问题是我来自 MacOSX Cocoa 编程的绿色领域。

我查看了 ipcal 的来源,但无法将其集成到我的代码库中。互联网上的某个地方一定有一个简单的十行代码,我就是找不到。有人可以指出如何在 C 中执行此操作的简短代码示例吗?

4

5 回答 5

40

只需计算:

broadcast = ip | ( ~ subnet )

(广播 = ip-addr 或反向子网掩码)

广播地址有一个1位,而子网掩码有一个0位。

于 2009-04-22T14:55:32.030 回答
6

我知道 OP 至少对位级算术有一个模糊的理解,但是在将字符串转换为数字及其逆时迷失了方向。这是一个使用 froh42 计算的工作示例(无论如何测试最少)。

jcomeau@aspire:~/rentacoder/jcomeau/freifunk$ cat inet.c; make inet; ./inet 192.168.3.1 255.255.255.0
#include <arpa/inet.h>
#include <stdio.h>
int main(int argc, char **argv) {
    char *host_ip = argc > 1 ? argv[1] : "127.0.0.1";
    char *netmask = argc > 2 ? argv[2] : "255.255.255.255";
    struct in_addr host, mask, broadcast;
    char broadcast_address[INET_ADDRSTRLEN];
    if (inet_pton(AF_INET, host_ip, &host) == 1 &&
        inet_pton(AF_INET, netmask, &mask) == 1)
        broadcast.s_addr = host.s_addr | ~mask.s_addr;
    else {
        fprintf(stderr, "Failed converting strings to numbers\n");
        return 1;
    }
    if (inet_ntop(AF_INET, &broadcast, broadcast_address, INET_ADDRSTRLEN) != NULL)
        printf("Broadcast address of %s with netmask %s is %s\n",
            host_ip, netmask, broadcast_address);
    else {
        fprintf(stderr, "Failed converting number to string\n");
        return 1;
    }
    return 0;
}
cc     inet.c   -o inet
Broadcast address of 192.168.3.1 with netmask 255.255.255.0 is 192.168.3.255
于 2013-07-17T04:35:52.833 回答
3

可以吗?

unsigned broadcast(unsigned ip,unsigned subnet){
    unsigned int bits = subnet ^ 0xffffffff; 
    unsigned int bcast = ip | bits;

    return bcast;
}

编辑:我认为 ip 和子网都没有“。”

于 2009-04-22T14:59:26.137 回答
1

以下是如何在 C# 中执行此操作。例如使用 ip 10.28.40.149 和网络掩码 255.255.252.0 返回 10.28.43.255 这是正确的广播地址。感谢这里的一些代码

private static string GetBroadcastAddress(string ipAddress, string subnetMask) {
        //determines a broadcast address from an ip and subnet
        var ip = IPAddress.Parse(ipAddress);
        var mask = IPAddress.Parse(subnetMask);

        byte[] ipAdressBytes = ip.GetAddressBytes();
        byte[] subnetMaskBytes = mask.GetAddressBytes();

        if (ipAdressBytes.Length != subnetMaskBytes.Length)
            throw new ArgumentException("Lengths of IP address and subnet mask do not match.");

        byte[] broadcastAddress = new byte[ipAdressBytes.Length];
        for (int i = 0; i < broadcastAddress.Length; i++) {
            broadcastAddress[i] = (byte)(ipAdressBytes[i] | (subnetMaskBytes[i] ^ 255));
        }
        return new IPAddress(broadcastAddress).ToString();
    }
于 2012-06-14T17:39:47.047 回答
-1

好的,将来谁会寻找此代码。我今天有时需要这个,这是完整的代码,它可以工作:)只需复制并粘贴它,然后导入所需的 dll。

private IPAddress CalculateBroadCastAddress(IPAddress currentIP, IPAddress ipNetMask)
    {
        string[] strCurrentIP = currentIP.ToString().Split('.');
        string[] strIPNetMask = ipNetMask.ToString().Split('.');

        ArrayList arBroadCast = new ArrayList();

        for (int i = 0; i < 4; i++)
        {
            int nrBCOct = int.Parse(strCurrentIP[i]) | (int.Parse(strIPNetMask[i]) ^ 255);
            arBroadCast.Add(nrBCOct.ToString());
        }
        return IPAddress.Parse(arBroadCast[0] + "." + arBroadCast[1] +
               "." + arBroadCast[2] + "." + arBroadCast[3]);
    }


    private IPAddress getIP()
    {
        IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
            {
                return ip;

            }
        }
        return null;
    }

    private IPAddress getSubnetMask()
    {
        NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
        IPAddress ip = getIP();
        foreach (NetworkInterface interf in Interfaces)
        {

            UnicastIPAddressInformationCollection UnicastIPInfoCol = interf.GetIPProperties().UnicastAddresses;

            foreach (UnicastIPAddressInformation UnicatIPInfo in UnicastIPInfoCol)
            {
                if (UnicatIPInfo.Address.Equals(ip))
                    return UnicatIPInfo.IPv4Mask;
            }

        }

        return null;

    }

然后就像这样称呼它:

IPAddress  broadcastip = CalculateBroadCastAddress(getIP(), getSubnetMask());

快乐编码:)

于 2013-11-27T09:45:24.440 回答