CFSocketSetAddress fails if not host computer IP? - objective-c

I am making a simple WOL application.
So far I can successfully create a socket, however when setting the address using CFSocketSetAddress I can only use the network ip of my computer (WiFi ip = 192.168.0.5) or local ip (127.0.0.1).
For WOL I would like to send the data to the broadcast address (255.255.255.255). If this is entered I am returned with the error 'address could not be set'.
Am I miss-understanding the use of CFSocketSetAddress, and the address is supposed to be the hosts IP, or the destination IP? In either case, what do I need to do, so that my destination ip is the broadcast address?
Below is some of my code:
/*************************************************************************/
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT); //port
inet_aton(IP, &addr.sin_addr);//IP is the host network ip: 192.168.0.5
NSData *address = [NSData dataWithBytes: &addr length: sizeof(addr)];
if (CFSocketSetAddress(WOLsocket, (CFDataRef)address) != kCFSocketSuccess){
NSLog(#"Address could not be set!");
}

I solved my problem by using a different method (Got native socket), and then instead of using CFSocketSetAddress, I passed the address in the second argument of CFSocketSendData.
I don't have a link/reference for the changes, as it was some code stored on my HDD from days of heavy googling.
Would like to say thanks to David Gelhar Who pointed me in the right direction from my previous question.
for anyone else who might need this this is my code;
//Gets native & sets options
/*************************************************************************/
int desc = -1;
desc = CFSocketGetNative(WOLsocket);
int yes = 1;
if (setsockopt (desc, SOL_SOCKET, SO_BROADCAST, (char *)&yes, sizeof (yes)) < 0) {
NSLog(#"Set Socket options failed");
return EXIT_FAILURE;
}
//sets address socket - doesn't bind this is done in CFSocketSendData
/*************************************************************************/
unsigned long bcast = 0xffffffff;
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT); //port
addr.sin_addr.s_addr = bcast;
NSData *address = [NSData dataWithBytes: &addr length: sizeof(addr)];
//Sends Data
/*************************************************************************/
char ethadd []= "helloworld";
CFDataRef Data = CFDataCreate(NULL, (const UInt8*)ethadd, sizeof(ethadd));
if (CFSocketSendData(WOLsocket, address, Data, 0) < 0){
NSLog(#"Data could not be sent!");
return EXIT_FAILURE;
}
else NSLog(#"Data Sent");
}

For my app, the problem is that it was sandboxed, but it wasn't given network permissions.
If you're using App Sandbox, you need to check one of these:

Related

Calling CFRelease for a CFHostRef will sometimes crash

This is a secondary question that arose out of a post I made earlier today. I have the method below, which works fine for what I need, but sometimes crashes when I call CFRelease on the hostRef variable. I think it may have to do with the resource being used elsewhere when I'm trying to release it, but as far as I can tell, I'm synchronously resolving the host and I'm not accessing it from another thread.
I tried calling CFHostCancelInfoResolution before CFRelease, but that didn't change the frequency of the crashes. I thought I would post this here to see if there are some assumptions or misconceptions I have that aren't true.
+ (NSArray *) addressesForHostname: (NSString *)hostname {
CFMutableArrayRef ipAddresses = nil;
DLog(#"Getting addresses for host name %#", hostname);
CFHostRef hostRef = CFHostCreateWithName(kCFAllocatorDefault, (__bridge CFStringRef)(hostname));
CFStreamError error;
BOOL didResolve = CFHostStartInfoResolution(hostRef, kCFHostAddresses, &error); // synchronously get the host.
if (didResolve) {
CFArrayRef responseObjects = CFHostGetAddressing(hostRef, NULL);
long numberOfResponses = CFArrayGetCount(responseObjects);
ipAddresses = CFArrayCreateMutable(kCFAllocatorDefault, numberOfResponses, &kCFTypeArrayCallBacks);
for ( int i = 0 ; i < numberOfResponses; ++i ) {
char * ipAddress = NULL;
CFDataRef responseObject = CFArrayGetValueAtIndex(responseObjects, i);
struct sockaddr * currentAddress = (struct sockaddr *) CFDataGetBytePtr(responseObject); // Unwrap the CFData wrapper aound the sockaddr struct
switch (currentAddress->sa_family) {
case AF_INET: { // Internetworking AKA IPV4
DLog(#"Extracting IPV4 address");
struct sockaddr_in * socketAddress = (struct sockaddr_in *) currentAddress;
ipAddress = malloc(sizeof(INET_ADDRSTRLEN));
inet_ntop(AF_INET,
&(socketAddress->sin_addr),
ipAddress,
INET_ADDRSTRLEN);
CFStringRef ipAddressString = CFStringCreateWithCString(kCFAllocatorDefault, ipAddress, kCFStringEncodingASCII);
CFArrayInsertValueAtIndex(ipAddresses, i, ipAddressString);
break;
}
case AF_INET6: { // IPV6
DLog(#"Extracting IPV6 address");
struct sockaddr_in6 * socketAddress = (struct sockaddr_in6 *) currentAddress;
ipAddress = malloc(sizeof(INET6_ADDRSTRLEN));
inet_ntop(AF_INET6,
&(socketAddress->sin6_addr),
ipAddress,
INET6_ADDRSTRLEN);
CFStringRef ipAddressString = CFStringCreateWithCString(kCFAllocatorDefault, ipAddress, kCFStringEncodingASCII);
CFArrayInsertValueAtIndex(ipAddresses, i, ipAddressString);
break;
}
default:
DLog(#"Unsupported addressing protocol encountered. Gracefully ignoring and continuing.");
break;
}
if(ipAddress != NULL) {
free(ipAddress);
}
}
CFRelease(responseObjects);
}
CFRelease(hostRef);
return (__bridge_transfer NSArray *) ipAddresses;
}
removing CFRelease(responseObjects); shall fix it
You must follow the Create Rule for Core Foundation objects. If you received the object by calling a function with the words Create or Copy in their names (or if you call CFRetain explicitly), then you must release (CFRelease) the object when you're done with it. If you did not receive the object this way, then you must not release the object.
There are several mistakes in your code. First, the one you're finding, which is around responseObjects. You fetch this object using:
CFDataRef responseObject = CFArrayGetValueAtIndex(responseObjects, i);
The function does not have Create or Copy in its name. You must not call CFRelease on it.
However, you also call this:
ipAddresses = CFArrayCreateMutable(kCFAllocatorDefault, numberOfResponses, &kCFTypeArrayCallBacks);
and in a couple of places:
CFStringRef ipAddressString = CFStringCreateWithCString(kCFAllocatorDefault, ipAddress, kCFStringEncodingASCII);
You must call CFRelease on these objects before they go out of scope or you will leak them.
I think CFStreamError error; may cause this issue.
Try to declare error to null?
// synchronously get the host.
BOOL didResolve = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL);

how to resolve hostname from ip address in iOS Objective-C

I'm looking for a way to resolve the hostname of a device in my LAN from its ip address on this LAN.
I wrote a program in C which works perfectly on Linux using gethostbyaddr() function.
When I tried that on OS X or iOS it doesn't work.
It seems that there is a problem with gethostbyaddr() in OS X and iOS.
Anyway, if you have another idea to get hostname of remote machine from it's IP in iOS, it'll make my day.
This is the code I used:
First test:
192.168.0.101 is the ip address of the machine that we are querying for hostname.
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
struct hostent *he;
struct in_addr ipv4addr;
inet_pton(AF_INET, "192.168.0.101", &ipv4addr);
he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
printf("Host name: %s\n", he->h_name);
This code works well on linux, but it doesn't on OS X nor iOS.
Second test:
+ (NSArray *)hostnamesForAddress:(NSString *)address {
// Get the host reference for the given address.
struct addrinfo hints;
struct addrinfo *result = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
int errorStatus = getaddrinfo([address cStringUsingEncoding:NSASCIIStringEncoding], NULL, &hints, &result);
if (errorStatus != 0) return nil;
CFDataRef addressRef = CFDataCreate(NULL, (UInt8 *)result->ai_addr, result->ai_addrlen);
if (addressRef == nil) return nil;
freeaddrinfo(result);
CFHostRef hostRef = CFHostCreateWithAddress(kCFAllocatorDefault, addressRef);
if (hostRef == nil) return nil;
CFRelease(addressRef);
BOOL isSuccess = CFHostStartInfoResolution(hostRef, kCFHostNames, NULL);
if (!isSuccess) return nil;
// Get the hostnames for the host reference.
CFArrayRef hostnamesRef = CFHostGetNames(hostRef, NULL);
NSMutableArray *hostnames = [NSMutableArray array];
for (int currentIndex = 0; currentIndex < [(NSArray *)hostnamesRef count]; currentIndex++) {
[hostnames addObject:[(NSArray *)hostnamesRef objectAtIndex:currentIndex]];
}
return hostnames;
}
This code stucks at CFHostStartInfoResolution returning nil at this point.
Thx in advance.
It's actually a one-liner.
[[NSHost hostWithAddress:#"173.194.34.24"] name]

Obtaining a server IP address from hostname

When performing an NSURLRequest to a hostname, is it possible to obtain the IP address of the server that the response came from?
The NSURL method:
- (NSString *)host;
simply returns the hostname, and I see no way of obtaining the IP address from any of the other NSURL methods.
Perhaps there is a way of performing a host lookup before inititing the NSURLRequest?
You can use the system call gethostbyname() to resolve a hostname then use the returning structure to get the ip address. Have a look at inet_ntop() for this last part.
EXAMPLE CODE
struct hostent *hostentry;
hostentry = gethostbyname("google.com");
char * ipbuf;
ipbuf = inet_ntoa(*((struct in_addr *)hostentry->h_addr_list[0]));
printf("%s",ipbuf);
I was asking a question regarding
"how to get IP from hostname in unix\linux?"
but found this question in a different context which is not for Unix I guess, let me correct if I am wrong
since this question already been asked so I am fearing to avoid asking the same question marked as duplicated by stack overflow team.
Quest: how to get IP from hostname in unix\linux?
Ans: the two commands over there
ping host_name
Ex:
ping -s google.co.in
PING google.co.in: 56 data bytes
64 bytes from dfw06s48-in-f3.1e100.net (216.58.194.99): icmp_seq=0. time=2.477 ms
64 bytes from dfw06s48-in-f3.1e100.net (216.58.194.99): icmp_seq=1. time=1.415 ms
64 bytes from dfw06s48-in-f3.1e100.net (216.58.194.99): icmp_seq=2. time=1.712 ms
nslookup host_name
Ex:
nslookup google.co.in
Server: 155.179.59.249
Address: 155.179.59.249#53
Non-authoritative answer:
Name: google.co.in
Address: 216.58.194.99
#import <arpa/inet.h>
- (BOOL)resolveHost:(NSString *)hostname {
Boolean result;
CFHostRef hostRef;
CFArrayRef addresses;
NSString *ipAddress = nil;
hostRef = CFHostCreateWithName(kCFAllocatorDefault, (__bridge
CFStringRef)hostname);
CFStreamError *error = NULL;
if (hostRef) {
result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, error);
if (result) {
addresses = CFHostGetAddressing(hostRef, &result);
}
}
if (result) {
CFIndex index = 0;
CFDataRef ref = (CFDataRef) CFArrayGetValueAtIndex(addresses, index);
int port=0;
struct sockaddr *addressGeneric;
NSData *myData = (__bridge NSData *)ref;
addressGeneric = (struct sockaddr *)[myData bytes];
switch (addressGeneric->sa_family) {
case AF_INET: {
struct sockaddr_in *ip4;
char dest[INET_ADDRSTRLEN];
ip4 = (struct sockaddr_in *)[myData bytes];
port = ntohs(ip4->sin_port);
ipAddress = [NSString stringWithFormat:#"%s", inet_ntop(AF_INET, &ip4->sin_addr, dest, sizeof dest)];
}
break;
case AF_INET6: {
struct sockaddr_in6 *ip6;
char dest[INET6_ADDRSTRLEN];
ip6 = (struct sockaddr_in6 *)[myData bytes];
port = ntohs(ip6->sin6_port);
ipAddress = [NSString stringWithFormat:#"%s", inet_ntop(AF_INET6, &ip6->sin6_addr, dest, sizeof dest)];
}
break;
default:
ipAddress = nil;
break;
}
}
NSLog(#"%#", ipAddress);
if (ipAddress) {
return YES;
} else {
return NO;
}
}
[self resolveHost:#"google.com"]

iOS - How do I create a read and write stream to a network socket?

I am new to iOS development. I am attempting to create a read and write stream. I am using the CFNetworking programming guide's examples to try and get something working.
I am trying to schedule the read stream on the run loop to work around the issue of the streams blocking. Right away I have run into issues. How can I create a CFHost object using CFHhostCreateWithAddress? Here is what I have so far:
NSString *address = #"irc.ubuntu.net";
CFDataRef addressDataRef = (CFDataRef)[address dataUsingEncoding:NSASCIIStringEncoding];
CFHostRef host = CFHostCreateWithAddress(kCFAllocatorDefault, addressDataRef);
//Create Read and Write Stream
CFStreamCreatePairWithSocketToCFHost(kCFAllocatorDefault, host, 8008, &readStream, &writeStream);
The second line bombs. Can someone please tell me how to create a CFHostRef?
Thanks a lot!
The documentation states that the second argument to CFHostCreateWithAddress() must be "A CFDataRef object containing a sockaddr structure for the address of the host. This value must not be NULL."
You're passing a CFDataRef representing "irc.ubuntu.net", which is by no means a sockaddr struct.
Use CFHostCreateWithName:
CFHostRef CFHostCreateWithName (
CFAllocatorRef allocator,
CFStringRef hostname
);
As you probably know, you can cast an NSString * to CFStringRef, or create a constant CFStringRef with the macro CFSTR().
+ (NSData *)dataForIPAddress:(NSString *)address {
struct addrinfo hints;
struct addrinfo *result = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_NUMERICHOST;
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = 0;
int errorStatus = getaddrinfo([address cStringUsingEncoding:NSASCIIStringEncoding], NULL, &hints, &result);
if (errorStatus != 0) return nil;
CFDataRef addressRef = CFDataCreate(NULL, (UInt8 *)result->ai_addr, result->ai_addrlen);
if (addressRef == nil) return nil;
freeaddrinfo(result);
return [(NSData *)addressRef autorelease];
}

How to use CFNetwork to get byte array from sockets?

I'm working in a project for the iPad, it is a small program and I need it to communicate with another software that runs on windows and act like a server; so the application that I'm creating for the iPad will be the client.
I'm using CFNetwork to do sockets communication, this is the way I'm establishing the connection:
char ip[] = "192.168.0.244";
NSString *ipAddress = [[NSString alloc] initWithCString:ip];
/* Build our socket context; this ties an instance of self to the socket */
CFSocketContext CTX = { 0, self, NULL, NULL, NULL };
/* Create the server socket as a TCP IPv4 socket and set a callback */
/* for calls to the socket's lower-level connect() function */
TCPClient = CFSocketCreate(NULL, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketDataCallBack, (CFSocketCallBack)ConnectCallBack, &CTX);
if (TCPClient == NULL)
return;
/* Set the port and address we want to listen on */
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = inet_addr([ipAddress UTF8String]);
CFDataRef connectAddr = CFDataCreate(NULL, (unsigned char *)&addr, sizeof(addr));
CFSocketConnectToAddress(TCPClient, connectAddr, -1);
CFRunLoopSourceRef sourceRef = CFSocketCreateRunLoopSource(kCFAllocatorDefault, TCPClient, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), sourceRef, kCFRunLoopCommonModes);
CFRelease(sourceRef);
CFRunLoopRun();
And this is the way I sent the data, which basically is a byte array
/* The native socket, used for various operations */
// TCPClient is a CFSocketRef member variable
CFSocketNativeHandle sock = CFSocketGetNative(TCPClient);
Byte byteData[3];
byteData[0] = 0;
byteData[1] = 4;
byteData[2] = 0;
send(sock, byteData, strlen(byteData)+1, 0);
Finally, as you may have noticed, when I create the server socket, I registered a callback for the kCFSocketDataCallBack type, this is the code.
void ConnectCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
{
// SocketsViewController is the class that contains all the methods
SocketsViewController *obj = (SocketsViewController*)info;
UInt8 *unsignedData = (UInt8 *) CFDataGetBytePtr(data);
char *value = (char*)unsignedData;
NSString *text = [[NSString alloc]initWithCString:value length:strlen(value)];
[obj writeToTextView:text];
[text release];
}
Actually, this callback is being invoked in my code, the problem is that I don't know how can I get the data that the windows client sent me, I'm expecting to receive an array of bytes, but I don't know how can I get those bytes from the data param.
If anyone can help me to find a way to do this, or maybe me point me to another way to get the data from the server in my client application I would really appreciate it.
Thanks.
In your callback, the data parameter is indeed a CFDataRef value for the kCFSocketDataCallBack callback type.
CFDataRef dataRef = (CFDataRef) data;
Byte *array = new Byte[CFDataGetLength(dataRef)]; // Or use a fixed length
CFDataGetBytes(dataRef, CFRangeMake(0, CFDataGetLength(theData)), array);
The array will then contains the array of byte.