Pages

Showing posts with label address. Show all posts
Showing posts with label address. Show all posts

September 11, 2013

Check if a string is a valid MAC address

How to test if a string is a valid MAC address?
You need:
- http://objective-c-functions.blogspot.com/2013/08/check-if-number-is-valid-hex.html


/** Check if a MAC address is valid.

 MAC address must be in the 12:34:56:78:9A:BC format.

 @param mac String representing the MAC address to check.
 @return Returns true if given MAC address is valid, false otherwise.
 @see macAddressWithColonsFromString:
 @see getMacAddressesFromFileAtPath:

 */
+ (BOOL)isValidMacAddress:(NSString *)mac {

    // Must be 17 chars.
    NSInteger len = [mac length];
    if (len != 17) {
        return NO;
    }

    // Let's valide all characters.
    NSString *tempStr;
    if (len == 17) {

        // Loop by couples plus separator (eg: "C6:").
        for (NSInteger i=0; i<=len; i+=3) {

            // Position 0: hex char.
            tempStr = [mac substringWithRange:NSMakeRange(i+0, 1)];
            if (![LMFunctions isHexadecimalNumber:tempStr]) { return NO; }

            // Position 1: hex char.
            tempStr = [mac substringWithRange:NSMakeRange(i+1, 1)];
            if (![LMFunctions isHexadecimalNumber:tempStr]) { return NO; }

            // Position 2: separator.
            if (i+2 < len) // Warning: last separator does not exist.
            {
                tempStr = [mac substringWithRange:NSMakeRange(i+2, 1)];
                if (![tempStr isEqualToString:@":"]) { return NO; }
            }
        }
    }

    return YES;
}



Download the ready-for-use source file (.m) of Check if a string is a valid MAC address

September 10, 2013

Check if a string is a valid IP address (IPv4 and IPv6)

This function checks if an IP address (either IPv4 or IPv6) is valid.


/** Check if an IP address is valid.

 Looks for both IPv4 and IPv6.
 Based on: http://stackoverflow.com/questions/1679152/how-to-validate-an-ip-address-with-regular-expression-in-objective-c/10971521#10971521

 @param ip IP address as string.
 @return Returns true if given IP address is valid, false otherwise.

 */
+ (BOOL)isValidIpAddress:(NSString *)ip {
    const char *utf8 = [ip UTF8String];

    // Check valid IPv4.
    struct in_addr dst;
    int success = inet_pton(AF_INET, utf8, &(dst.s_addr));
    if (success != 1) {
        // Check valid IPv6.
        struct in6_addr dst6;
        success = inet_pton(AF_INET6, utf8, &dst6);
    }
    return (success == 1);
}



Download the ready-for-use source file (.m) of Check if a string is a valid IP address (IPv4 and IPv6)

September 6, 2013

Get a list of items from a file (eg: MAC address)

I often save a set of items in a txt file so then I can "load" them directly from that file.
For example, this is a function which purpose is read MAC addresses from a given file. Of course you can adapt it to your needs.


/** Returns all the MAC addresses written in a file.

 This function treats every line as a MAC address.

 @param filePath The path of the file to search through.
 @return An array containing all the MAC addresses found in the file.
 @see macAddressWithColonsFromString:
 @see isValidMacAddress:

 */
+ (NSArray *)getMacAddressesFromFileAtPath:(NSString *)filePath {

    if (filePath) {
        NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
        NSArray *arr = [[NSArray alloc] initWithArray:[str componentsSeparatedByString:@"\n"]];
        return arr;
    } else {
        return nil;
    }
}



Download the ready-for-use source file (.m) of Get a list of items from a file (eg: MAC address)

June 17, 2013

Get my own IP address on iPhone (or iPad, iPod)

What's my IP?
It works on both iOS (iPhone, iPad, iPod) and Mac OS Xcode projects.


/** Returns the local own IP address.

 @return Returns the IP address as string.

 */
+ (NSString *)myIpAddress {
    NSString *address = @"127.0.0.1"; // Default value that means you're probably not connected to a network.
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = getifaddrs(&interfaces); // Retrieve the current interfaces - returns 0 on success.
    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)];
                }
            }
            temp_addr = temp_addr->ifa_next;
        }
    }
    // Free memory.
    freeifaddrs(interfaces);
    return address;
}



Download the ready-for-use source file (.m) of Get my own IP address on iPhone (or iPad, iPod)