How to resolve an internet address? - iphone

I have a problem: How can I convert (resolve) an ip address (like www.google.com) into ip address (array of byte)? I tried different code but every time the code crash if the address don't exist. There is a way to check also this?
+ (void) resolveIPAddress: (NSString*) dnsAddress {
struct hostent hostentry;
const char str = [ dnsAddress UTF8String ];
hostentry = gethostbyname(str);
char ipbuf[4];
char *ipbuf_ptr = &ipbuf[0];
ipbuf_ptr = inet_ntoa(*((struct in_addr *)hostentry->h_addr_list[0]));
printf("%s",ipbuf_ptr);
}

The problem is that your method tries using the results of gethostbyname without checking h_errno. When h_errno is non-zero, the results in hostentry are invalid. Dereferencing them in inet_ntoa causes the crash.
+ (void) resolveIPAddress: (NSString*) dnsAddress {
struct hostent hostentry;
const char str = [ dnsAddress UTF8String ];
hostentry = gethostbyname(str);
if (h_errno) {
NSLog(#"Error resolving host: %d", h_errno);
return;
}
char ipbuf[4];
char *ipbuf_ptr = &ipbuf[0];
ipbuf_ptr = inet_ntoa(*((struct in_addr *)hostentry->h_addr_list[0]));
printf("%s",ipbuf_ptr);
}

Related

Error with sendto() function: Invalid Argument Error

I am working on writing a ping CLI program for linux and I have been getting errno 22: invalid argument in the sendto() function. I don't understand why, all the arguments seem to be correct.
Here is where I call the function:
// send echo request
bytesSent = sendto(socketFD, // socket file descriptor
(char*)&packet, PacketSize, // packet and size
0, // flags
(sockaddr*)DestinationAddr, (socklen_t)sizeof(DestinationAddr)); // destination address and size
'packet' looks like this:
(I call initializePacket() in the function where I call sendto())
struct PacketData {
icmphdr header;
char message[PacketSize - sizeof(header)]; // want total size to be 64 bytes
};
PacketData initializePacket(int &transmitted) {
PacketData packet = {};
packet.header.type = ICMP_ECHO; // set ICMP type to Echo
packet.header.un.echo.id = getpid() & 0xFFFF; // set id (ICMP field is 16 bits)
packet.header.checksum = 0; // fixed checksum because data is unchanging
packet.header.un.echo.sequence = transmitted++;
// fill up message
memset(&packet.message, '0', sizeof(packet.message));
packet.message[PacketSize - sizeof(packet.header) - 1] = '\0';
return packet;
}
'DestinationAddr' is this:
// variables needed to store IP Address
addrinfo* result;
sockaddr_in* DestinationAddr;
char ipString[INET_ADDRSTRLEN];
// get IP Address and store in result (passed by reference)
if (getIPAddress(argv[1], result) != 0) {
std::cout << "Invalid IP Address. Terminating ...\n";
exit(EXIT_FAILURE);
}
else {
DestinationAddr = (sockaddr_in*)result->ai_addr; // get struct from resulting linked list
void* address;
address = &DestinationAddr->sin_addr; // store IP Address
inet_ntop(result->ai_family, address, ipString, sizeof(ipString)); // convert binary IP to string
std::cout << "IP: " << ipString << std::endl;
}
And the getIPAddress() function is:
int getIPAddress(char* hostName, addrinfo* &result) {
addrinfo tempStruct = {0};
tempStruct.ai_family = AF_INET; // want IPv4
tempStruct.ai_socktype = SOCK_DGRAM; // set socket type to datagram
tempStruct.ai_flags = AI_PASSIVE; // fill in IP automatically
// get and validate IP address
return (getaddrinfo(hostName, &PortNo, &tempStruct, &result));
}
PortNo is defined as: const char PortNo = '0';
According to documentation icmp:
A user protocol may receive ICMP packets for all local sockets by opening a raw socket with the protocol IPPROTO_ICMP.
So, try creating your socket like that:
socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)
And, if you encounter EPERM error, then run your program as root.

LKM to lookup tcp_sock from packet

My goal is to write a LKM (Linux Kernel Module) which intercepts all TCP packets, lookups tcp_sock structure and based on some conditions, logs some information from tcp_sock structure (ex: tcpsock->snd_una).
This is how I am trying to achieve this: I am intercepting using netfilter on ubunutu (code below) but is there a way to access tcp_sock structure (look for NOTE in the code)?
I am not particular about netfilter. Please suggest if there is any other way of writing LKM to achieve this. It has to be a LKM.
unsigned int ip_packets_hook_func(unsigned int hooknum,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *))
{
struct iphdr *ipp = (struct iphdr *)skb_network_header(skb);
struct tcphdr *tcp_hdr;
struct tcp_sock *tcpsock;
if (!skb) {
return NF_ACCEPT;
}
if (ipp->protocol == IPPROTO_TCP ) { // Incomming packet is TCP
tcp_hdr = (struct tcphdr *) ((__u32 *)ipp + ipp->ihl);
tcpsock = (struct tcp_sock *) skb->sk;
if(ntohs(tcp_hdr->dest) != INTERESTED_PORT) {
return NF_ACCEPT;
}
printk("TCP ports: source: %d, dest: %d \n", ntohs(tcp_hdr->source),
ntohs(tcp_hdr->dest));
if(tcp_hdr->syn || tcp_hdr->fin || tcp_hdr->rst) {
printk("Flag: %s %s %s\n", tcp_hdr->syn? "SYN" : "",
tcp_hdr->fin? "FIN" : "",
tcp_hdr->rst? "RST" : "");
return NF_ACCEPT;
}
// **NOTE**
// skb->sk is NULL at this point.
// Get tcp_sock for this connection.
} else {
printk("Its not TCP\n");
}
return NF_ACCEPT;
}
I was able to achieve it by looking up inet hashtables (code snippet below). I am the server in this example. Make sure you lookup after 3 way handshake is complete.
const struct iphdr *iph;
const struct tcphdr *th;
struct sock *sk = NULL;
struct tcp_sock *tp;
.
.
.
.
sk = __inet_lookup_established(dev_net(skb->dev), &tcp_hashinfo,
iph->saddr, th->source,
iph->daddr, ntohs(th->dest),
skb->skb_iif);
// Sanity checks here.
tp = tcp_sk(sk);

parsing NSData object for information

I have a NSData object coming back from my server, it varies in its content but sticks to a particular structure.
I would like to know (hopfully with some example code) how to work though this object to get the data I need out of it.
the structure of the data objects inside the objects are like this
leading value (UInt16) - (tells me what section of the response it is)
Size of string (UInt32) or number - (UInt32)
String (not null terminated) i.e. followed by the next leading value.
I have been reading through the Binary Data Programming Guide however that's only really showing me how to put my data into new NSData objects and accessing and compairing the bytes.
The thing I am stuck on is how do I say grab the info dynamically. Check the NSdata objects first leading value figure out if its string or int then get the string or int and move onto the next leading value..
any suggestions or example code would be really helpfull.. just stuck in abit of a mind block as I have never attempted anything like this in objective C.
Some of this depends on how your server is written to encode the data into what it is sending you. Assuming it is encoding the numeric values using standard network byte ordering (big-endian) you will want it converted to the correct byte-ordering for iOS (I believe that is always little-endian).
I would approach it something like this:
uint16_t typeWithNetworkOrdering, typeWithLocalOrdering;
uint32_t sizeWithNetworkOrdering, sizeWithLocalOrdering;
char *cstring = NULL;
uint32_t numberWithNetworkOrdering, numberWithLocalOrdering;
const void *bytes = [myData bytes];
NSUInteger length = [myData length];
while (length > 0) {
memcpy(&typeWithNetworkOrdering, bytes, sizeof(uint16_t));
bytes += sizeof(uint16_t);
length -= sizeof(uint16_t);
memcpy(&sizeWithNetworkOrdering, bytes, sizeof(uint32_t));
bytes += sizeof(uint32_t);
length -= sizeof(uint32_t);
typeWithLocalOrdering = CFSwapInt16BigToHost(typeWithNetworkOrdering);
sizeWithLocalOrdering = CFSwapInt32BigToHost(sizeWithNetworkOrdering);
if (typeWithLocalOrdering == STRING_TYPE) { // STRING_TYPE is whatever type value corresponds to a string
cstring = (char *) malloc(sizeWithLocalOrdering + 1);
strncpy(cstring, bytes, sizeWithLocalOrdering);
cstring[sizeWithLocalOrdering] = '\0';
NSString *resultString = [NSString stringWithCString:cstring encoding:NSUTF8StringEncoding];
NSLog(#"String = %#", resultString);
free(cstring);
bytes += sizeWithLocalOrdering;
length -= sizeWithLocalOrdering;
// Do whatever you need to with the string
}
else if (typeWithLocalOrdering == NUMBER_TYPE) { // NUMBER_TYPE is whatever type value corresponds to a number
memcpy(&numberWithNetworkOrdering, bytes, sizeof(uint32_t));
numberWithLocalOrdering = CFSwapInt32BigToHost(numberWithNetworkOrdering);
NSLog(#"Number = %u", numberWithLocalOrdering);
bytes += sizeof(uint32_t);
length -= sizeof(uint32_t);
// Do whatever you need to with the number
}
}
Define your own internal structs and cast the pointer to it:
NSData* data;
struct headerType
{
uint16_t type;
uint32_t length;
};
const struct headerType* header=(const struct headerType*)[data bytes]; // get the header of the response
if (header->type==1)
{
const char* text=((const char*)header)+6; // skip the header (16bits+32bits=6 bytes offset)
}
EDIT:
If you need to read them in a loop:
NSData* data;
const uint8_t* cursor=(const uint8_t*)[data bytes];
while (true)
{
uint16_t type=*((uint16_t*)cursor);
cursor+=2;
if (cursor==1)
{
// string
uint32_t length=*((uint32_t*)cursor);
cursor+=4;
const char* str=(const char*)cursor;
cursor+=length;
}
else if (cursor==2)
{
// another type
}
else
break;
}

How to create UUID type 1 in objective C (iOS)

I created UUID (don't know which type) with the following code:
// Create universally unique identifier (object)
CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);
// Get the string representation of CFUUID object.
NSString *uuidStr = (__bridge NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);
CFRelease(uuidObject);
But my API that is send data to says that is not type 1 that it needs http://en.wikipedia.org/wiki/Universally_unique_identifier#Version_1_.28MAC_address.29.
How to create this type1 UUID in objC (iphone)?
I have problems making this in obj C, is it an option to use C code to generate this?
I've been searching for the same thing. Here it is:
uuid_generate_time
https://developer.apple.com/library/ios/documentation/System/Conceptual/ManPages_iPhoneOS/man3/uuid_generate_time.3.html
Also there's Apple source code for this function:
http://www.opensource.apple.com/source/xnu/xnu-792.13.8/libkern/uuid/uuid.c
NSString* uuidString = nil;
// Get UUID type 1
uuid_t dateUUID;
uuid_generate_time(dateUUID);
// Convert it to string
uuid_string_t unparsedUUID;
uuid_unparse_lower(dateUUID, unparsedUUID);
uuidString = [[NSString alloc] initWithUTF8String:unparsedUUID];
Get the MAC address first: (from developertips)
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
...
- (NSString *)getMacAddress
{
int mgmtInfoBase[6];
char *msgBuffer = NULL;
size_t length;
unsigned char macAddress[6];
struct if_msghdr *interfaceMsgStruct;
struct sockaddr_dl *socketStruct;
NSString *errorFlag = NULL;
// Setup the management Information Base (mib)
mgmtInfoBase[0] = CTL_NET; // Request network subsystem
mgmtInfoBase[1] = AF_ROUTE; // Routing table info
mgmtInfoBase[2] = 0;
mgmtInfoBase[3] = AF_LINK; // Request link layer information
mgmtInfoBase[4] = NET_RT_IFLIST; // Request all configured interfaces
// With all configured interfaces requested, get handle index
if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0)
errorFlag = #"if_nametoindex failure";
else
{
// Get the size of the data available (store in len)
if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)
errorFlag = #"sysctl mgmtInfoBase failure";
else
{
// Alloc memory based on above call
if ((msgBuffer = malloc(length)) == NULL)
errorFlag = #"buffer allocation failure";
else
{
// Get system information, store in buffer
if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
errorFlag = #"sysctl msgBuffer failure";
}
}
}
// Befor going any further...
if (errorFlag != NULL)
{
NSLog(#"Error: %#", errorFlag);
return errorFlag;
}
// Map msgbuffer to interface message structure
interfaceMsgStruct = (struct if_msghdr *) msgBuffer;
// Map to link-level socket structure
socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);
// Copy link layer address data in socket structure to an array
memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);
// Read from char array into a string object, into traditional Mac address format
NSString *macAddressString = [NSString stringWithFormat:#"%02X:%02X:%02X:%02X:%02X:%02X",
macAddress[0], macAddress[1], macAddress[2],
macAddress[3], macAddress[4], macAddress[5]];
NSLog(#"Mac Address: %#", macAddressString);
// Release the buffer memory
free(msgBuffer);
return macAddressString;
}
Then generate the UUIDv1 with the rfc4122 spec, if the spec is too long to read, you may port the code from other language, here's one that I found: https://github.com/fredriklindberg/class.uuid.php/blob/master/class.uuid.php
Using following function you can create dynamic UUID.
-(NSString*)getDynamicUUID
{
CFUUIDRef uuidObj = CFUUIDCreate(nil);//create a new UUID
NSString *uuidString = (NSString*)CFUUIDCreateString(nil, uuidObj);
CFRelease(uuidObj);
return uuidString;
}
Hope this helps..

iPhone WiFi Subnet Mask and Router Address

I have code that allows me to determine the MAC address and the IP address of the WiFi connection on the iPhone, but I can't figure out how to get the Subnet Mask and Router address for the connection. Can anyone point me in the right direction here?
You can get that information by calling getifaddrs. (I use this function in an app of mine to figure out the iPhone's IP address.)
struct ifaddrs *ifa = NULL, *ifList;
getifaddrs(&ifList); // should check for errors
for (ifa = ifList; ifa != NULL; ifa = ifa->ifa_next) {
ifa->ifa_addr // interface address
ifa->ifa_netmask // subnet mask
ifa->ifa_dstaddr // broadcast address, NOT router address
}
freeifaddrs(ifList); // clean up after yourself
This gets you the subnet mask; for the router address, see this question.
This is all old-school UNIX networking stuff, you'll have to pick out which of the interfaces is the WiFi connection (other stuff like a loopback interface will be in there too). Then you might have to use functions like inet_ntoa() depending on what format you want to read the IP addresses. It's not bad, just tedious and ugly. Have fun!
NSString *address = #"error";
NSString *netmask = #"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)
{
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL)
{
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:#"en0"])
{
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
netmask = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
NSLog(#"address %#", address);
NSLog(#"netmask %#", netmask);