Calculate broadcast address from ip and subnet mask

后端 未结 5 582
一向
一向 2020-12-09 04:17

I want to calculate the broadcast address for:

IP:     192.168.3.1
Subnet: 255.255.255.0
=       192.168.3.255

in C.

I know the way

相关标签:
5条回答
  • 2020-12-09 04:58

    Could it be?

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

    Edit: I considered that both ip and subnet are without "."

    0 讨论(0)
  • 2020-12-09 04:58

    Here is how to do it in C#. for example using ip 10.28.40.149 with netmask 255.255.252.0 returns 10.28.43.255 which is the correct broadcast address. thanks to some code from here

    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();
        }
    
    0 讨论(0)
  • 2020-12-09 05:14

    Just calculate:

    broadcast = ip | ( ~ subnet )
    

    (Broadcast = ip-addr or the inverted subnet-mask)

    The broadcast address has a 1 bit where the subnet mask has a 0 bit.

    0 讨论(0)
  • 2020-12-09 05:16

    ok whom will look for this code in the future. I have spend sometimes today as I needed this, here is the full code and it works :) simply copy and paste it and then import the required dlls.

    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;
    
        }
    

    Then just call it like :

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

    Happy coding :)

    0 讨论(0)
  • 2020-12-09 05:18

    I understand that the OP had at least a vague understanding of the bit-level arithmetic but was lost on converting the strings to numbers and its inverse. here's a working (with minimal testing anyway) example, using froh42's calculation.

    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
    
    0 讨论(0)
提交回复
热议问题