IP validation

Change-Id: I9852efb8acf590242da762de2f9fcde445ec9a66
This commit is contained in:
siqi 2013-07-04 15:44:12 +02:00
parent c242867260
commit ae7496a4cc
4 changed files with 40 additions and 7 deletions

View File

@ -21,3 +21,9 @@ typedef enum protocol {NETWORK} Protocol_t;
ofName:(NSString*) name;
@end
@interface NSString (IPValidation)
- (BOOL)isValidIPAddress;
@end

View File

@ -7,13 +7,12 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#import "Server.h"
#import <arpa/inet.h>
@interface Server()
@end
@implementation Server
@ -54,3 +53,23 @@
}
@end
@implementation NSString (IPValidation)
- (BOOL)isValidIPAddress
{
const char *utf8 = [self UTF8String];
int success;
struct in_addr dst;
success = inet_pton(AF_INET, utf8, &dst);
if (success != 1) {
struct in6_addr dst6;
success = inet_pton(AF_INET6, utf8, &dst6);
}
return success;
}
@end

View File

@ -22,12 +22,20 @@
- (IBAction)save:(id)sender {
NSString *serverName = [self.nameCell.textField text];
NSString *serverAddr = [self.addrCell.textField text];
if (!serverName) {
serverName = @"Computer";
if ([serverAddr isValidIPAddress]) {
if (!serverName)
serverName = @"Computer";
NSLog(@"New server name:%@ ip:%@", serverName, serverAddr);
[self.comManager addServersWithName:serverName AtAddress:serverAddr];
[self.navigationController popViewControllerAnimated:YES];
} else {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Invalid IP Address"
message:@"A valid IP address should be like this: \"192.168.1.1\""
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
NSLog(@"New server name:%@ ip:%@", serverName, serverAddr);
[self.comManager addServersWithName:serverName AtAddress:serverAddr];
[self.navigationController popViewControllerAnimated:YES];
}
- (BOOL)isModal