IP validation
Change-Id: I9852efb8acf590242da762de2f9fcde445ec9a66
This commit is contained in:
parent
c242867260
commit
ae7496a4cc
Binary file not shown.
@ -21,3 +21,9 @@ typedef enum protocol {NETWORK} Protocol_t;
|
||||
ofName:(NSString*) name;
|
||||
|
||||
@end
|
||||
|
||||
@interface NSString (IPValidation)
|
||||
|
||||
- (BOOL)isValidIPAddress;
|
||||
|
||||
@end
|
@ -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
|
||||
|
@ -22,12 +22,20 @@
|
||||
- (IBAction)save:(id)sender {
|
||||
NSString *serverName = [self.nameCell.textField text];
|
||||
NSString *serverAddr = [self.addrCell.textField text];
|
||||
if (!serverName) {
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)isModal
|
||||
|
Loading…
x
Reference in New Issue
Block a user