How do I go about calculating the Broadcast Address in Objective C
I would like to have the Broadcast Address resolve to the same result as shown in the following Su
You don't need to provide IP address & subnet-mask to calculate "Broadcast" address of any network interface. Actually you don't need to manually calculate it.
There is ifaddrs Structure
that describes a network host. It provides complete details of any network interface. By using getifaddrs()
method, you can retrieve Broadcast address of the network without providing IP & subnet mask as attribute. Function itself retrieves the details. I'd prefer this because this will make code more generic.
I am using this in my code.
#import <ifaddrs.h> // for interface addresses needed to find local IP
static NSString * broadcastAddress()
//Gets Local IP of the device over Wifi
//Calculates & returns broadcast Address for the network
{
NSString * broadcastAddr= @"Error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0)
{
temp_addr = interfaces;
while(temp_addr != NULL)
{
// check if interface is en0 which is the wifi connection on the iPhone
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
{
broadcastAddr = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_dstaddr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
freeifaddrs(interfaces);
return broadcastAddr;
}
To know more about ifaddrs structure
See this
The following code can be used to calculate the broadcast address, all you need is the IP Address and the Subnet Mask in a NSString type to perform the calculation.
////////////////////////////////////////////////////////
// Calculates the local broadcast packet from Local IP Address and Subnet Mask.
// Uses the following calculation:-
// broadcast = ip | ( ~ subnet ) i.e. Broadcast = ip-addr OR the inverted subnet-mask
////////////////////////////////////////////////////////
// Local IP Address and Subnet Mask
NSString *localIPAddress = @"192.168.1.10";
NSString *netmaskAddress = @"255.255.192.0";
// Separate out the subnet mask values and assign them to variables as integers
NSScanner* scannerNetmask = [NSScanner scannerWithString:netmaskAddress];
[scannerNetmask setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"."]];
int aNetmask, bNetmask, cNetmask, dNetmask;
[scannerNetmask scanInt:&aNetmask];
[scannerNetmask scanInt:&bNetmask];
[scannerNetmask scanInt:&cNetmask];
[scannerNetmask scanInt:&dNetmask];
// Separate out the ip address values and assign them to variables as integers
NSScanner* scannerLocalIP = [NSScanner scannerWithString:localIPAddress];
[scannerLocalIP setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"."]];
int aLocalIP, bLocalIP, cLocalIP, dLocalIP;
[scannerLocalIP scanInt:&aLocalIP];
[scannerLocalIP scanInt:&bLocalIP];
[scannerLocalIP scanInt:&cLocalIP];
[scannerLocalIP scanInt:&dLocalIP];
// Calculate the bitwise inversion of the Netmask
// Do this by first casting value as a unsigned char (1 byte, no complement)
unsigned char biNetmask, ciNetmask, diNetmask;
biNetmask = ~(unsigned char)bNetmask;
ciNetmask = ~(unsigned char)cNetmask;
diNetmask = ~(unsigned char)dNetmask;
// Calculating the separated values for the broadcast address
unsigned char bBroadcast, cBroadcast, dBroadcast;
bBroadcast = (unsigned char)bLocalIP | biNetmask;
cBroadcast = (unsigned char)cLocalIP | ciNetmask;
dBroadcast = (unsigned char)dLocalIP | diNetmask;
// Create the Broadcast Address from separate values and display in the console
NSString broadcastAddress = [NSString stringWithFormat:@"%d.%d.%d.%d", aLocalIP, bBroadcast, cBroadcast, dBroadcast];
NSLog(@"Broadcast Address: %@", broadcastAddress);
Alternative solution:
#include <net/ethernet.h>
#include <arpa/inet.h>
NSString *localIPAddress = @"192.168.1.10";
NSString *netmaskAddress = @"255.255.192.0";
// Strings to in_addr:
struct in_addr localAddr;
struct in_addr netmaskAddr;
inet_aton([localIPAddress UTF8String], &localAddr);
inet_aton([netmaskAddress UTF8String], &netmaskAddr);
// The broadcast address calculation:
localAddr.s_addr |= ~(netmaskAddr.s_addr);
// in_addr to string:
NSString *broadCastAddress = [NSString stringWithUTF8String:inet_ntoa(localAddr)];