我想计算广播地址:
IP: 192.168.3.1
Subnet: 255.255.255.0
= 192.168.3.255
在C中。
我知道方法(在反转的IP和子网之间进行花式按位OR运算),但我的问题是我来自MacOSX Cocoa程序开发的绿色领域。
我查看了ipcal的源代码,但无法将其集成到我的代码库中。 必须有一个简单的十行代码在互联网上,我只是找不到它。 有人能指向一个简短的C语言代码示例吗?
只需要进行计算:
broadcast = ip | ( ~ subnet )
广播地址 = IP地址或反转的子网掩码。
在子网掩码为0的位置,广播地址对应的二进制位为1。
我理解原帖作者至少对于位运算有一定的了解,但是在将字符串转换为数字以及反向操作上感到困惑。这里提供一个可用的(虽然测试较少)示例,使用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
可能吗?
unsigned broadcast(unsigned ip,unsigned subnet){
unsigned int bits = subnet ^ 0xffffffff;
unsigned int bcast = ip | bits;
return bcast;
}
编辑:我考虑到IP地址和子网掩码都没有“.”
以下是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();
}
好的,未来需要查找此代码的人。我今天花了一些时间,因为我需要它,这里是完整的代码,它可以正常工作 :) 只需复制并粘贴它,然后导入所需的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());
开心编程 :)