Permalink
Browse files

120 columns in AKSIPUserAgent

  • Loading branch information...
eofster committed Nov 24, 2012
1 parent 4d36936 commit b584f29ca4615d34c22d5beb78d43272b781320b
View
@@ -36,14 +36,11 @@
}
-// Returns password for the first Keychain item with a specified service name
-// and account name.
-+ (NSString *)passwordForServiceName:(NSString *)serviceName
- accountName:(NSString *)accountName;
+// Returns password for the first Keychain item with a specified service name and account name.
++ (NSString *)passwordForServiceName:(NSString *)serviceName accountName:(NSString *)accountName;
-// Adds an item to the Keychain with a specified service name, account name,
-// and password. If the same item already exists, its password will be replaced
-// with the new one.
+// Adds an item to the Keychain with a specified service name, account name, and password. If the same item already
+// exists, its password will be replaced with the new one.
+ (BOOL)addItemWithServiceName:(NSString *)serviceName
accountName:(NSString *)accountName
password:(NSString *)password;
View
@@ -33,88 +33,84 @@
@implementation AKKeychain
-+ (NSString *)passwordForServiceName:(NSString *)serviceName
- accountName:(NSString *)accountName {
-
- void *passwordData = nil;
- UInt32 passwordLength;
- OSStatus findStatus;
-
- findStatus = SecKeychainFindGenericPassword(
- NULL, // Default keychain.
- [serviceName length],
- [serviceName UTF8String],
- [accountName length],
- [accountName UTF8String],
- &passwordLength,
- &passwordData,
- NULL); // Keychain item reference.
-
- if (findStatus != noErr)
- return nil;
-
- NSString *password = [[[NSString alloc] initWithBytes:passwordData
- length:passwordLength
- encoding:NSUTF8StringEncoding]
- autorelease];
-
- SecKeychainItemFreeContent(NULL, passwordData);
-
- return password;
++ (NSString *)passwordForServiceName:(NSString *)serviceName accountName:(NSString *)accountName {
+ void *passwordData = nil;
+ UInt32 passwordLength;
+ OSStatus findStatus;
+
+ findStatus = SecKeychainFindGenericPassword(NULL, // Default keychain.
+ [serviceName length],
+ [serviceName UTF8String],
+ [accountName length],
+ [accountName UTF8String],
+ &passwordLength,
+ &passwordData,
+ NULL); // Keychain item reference.
+
+ if (findStatus != noErr) {
+ return nil;
+ }
+
+ NSString *password = [[[NSString alloc] initWithBytes:passwordData
+ length:passwordLength
+ encoding:NSUTF8StringEncoding]
+ autorelease];
+
+ SecKeychainItemFreeContent(NULL, passwordData);
+
+ return password;
}
+ (BOOL)addItemWithServiceName:(NSString *)serviceName
accountName:(NSString *)accountName
password:(NSString *)password {
-
- SecKeychainItemRef keychainItemRef = nil;
- OSStatus addStatus, findStatus, modifyStatus;
- BOOL success = NO;
-
- // Add item to keychain.
- addStatus = SecKeychainAddGenericPassword(
- NULL, // NULL for default keychain.
- [serviceName length],
- [serviceName UTF8String],
- [accountName length],
- [accountName UTF8String],
- [password length],
- [password UTF8String],
- NULL); // Don't need keychain item reference.
-
- if (addStatus == noErr) {
- success = YES;
- } else if (addStatus == errSecDuplicateItem) {
- // Get the pointer to the duplicate item.
- findStatus = SecKeychainFindGenericPassword(
- NULL, // Default keychain.
- [serviceName length],
- [serviceName UTF8String],
- [accountName length],
- [accountName UTF8String],
- NULL,
- NULL,
- &keychainItemRef); // Pointer to the duplicate item.
- if (findStatus == noErr) {
- // Modify password in the duplicate item.
- modifyStatus = SecKeychainItemModifyAttributesAndData(
- keychainItemRef,
- NULL, // No changes in attributes.
- [password length],
- [password UTF8String]);
-
- if (modifyStatus == noErr) {
+ SecKeychainItemRef keychainItemRef = nil;
+ OSStatus addStatus, findStatus, modifyStatus;
+ BOOL success = NO;
+
+ // Add item to keychain.
+ addStatus = SecKeychainAddGenericPassword(NULL, // NULL for default keychain.
+ [serviceName length],
+ [serviceName UTF8String],
+ [accountName length],
+ [accountName UTF8String],
+ [password length],
+ [password UTF8String],
+ NULL); // Don't need keychain item reference.
+
+ if (addStatus == noErr) {
success = YES;
- }
+
+ } else if (addStatus == errSecDuplicateItem) {
+ // Get the pointer to the duplicate item.
+ findStatus = SecKeychainFindGenericPassword(NULL, // Default keychain.
+ [serviceName length],
+ [serviceName UTF8String],
+ [accountName length],
+ [accountName UTF8String],
+ NULL,
+ NULL,
+ &keychainItemRef); // Pointer to the duplicate item.
+
+ if (findStatus == noErr) {
+ // Modify password in the duplicate item.
+ modifyStatus = SecKeychainItemModifyAttributesAndData(keychainItemRef,
+ NULL, // No changes in attributes.
+ [password length],
+ [password UTF8String]);
+
+ if (modifyStatus == noErr) {
+ success = YES;
+ }
+ }
}
- }
-
- if (keychainItemRef != nil) {
- CFRelease(keychainItemRef);
- }
-
- return success;
+
+ if (keychainItemRef != nil) {
+ CFRelease(keychainItemRef);
+ }
+
+ return success;
}
@end
@@ -42,10 +42,10 @@ extern NSString * const AKNetworkReachabilityDidBecomeUnreachableNotification;
// Wrapper for SCNetworkReachability.
@interface AKNetworkReachability : NSObject {
- @private
- SCNetworkReachabilityRef reachability_; // Strong.
- SCNetworkReachabilityContext context_;
- NSString *host_;
+ @private
+ SCNetworkReachabilityRef reachability_; // Strong.
+ SCNetworkReachabilityContext context_;
+ NSString *host_;
}
// Host name or address of the target host.
@@ -36,15 +36,11 @@
#import "AKNSString+Scanning.h"
-NSString * const AKNetworkReachabilityDidBecomeReachableNotification
- = @"AKNetworkReachabilityDidBecomeReachable";
-NSString * const AKNetworkReachabilityDidBecomeUnreachableNotification
- = @"AKNetworkReachabilityDidBecomeUnreachable";
+NSString * const AKNetworkReachabilityDidBecomeReachableNotification = @"AKNetworkReachabilityDidBecomeReachable";
+NSString * const AKNetworkReachabilityDidBecomeUnreachableNotification = @"AKNetworkReachabilityDidBecomeUnreachable";
// SCNetworkReachability callback.
-static void AKReachabilityChanged(SCNetworkReachabilityRef target,
- SCNetworkConnectionFlags flags,
- void *info);
+static void AKReachabilityChanged(SCNetworkReachabilityRef target, SCNetworkConnectionFlags flags, void *info);
@interface AKNetworkReachability ()
@@ -60,98 +56,84 @@ @implementation AKNetworkReachability
@dynamic reachable;
- (BOOL)isReachable {
- SCNetworkConnectionFlags flags;
- Boolean flagsValid = SCNetworkReachabilityGetFlags(reachability_, &flags);
-
- return (flagsValid && (flags & kSCNetworkFlagsReachable)) ? YES : NO;
+ SCNetworkConnectionFlags flags;
+ Boolean flagsValid = SCNetworkReachabilityGetFlags(reachability_, &flags);
+
+ return (flagsValid && (flags & kSCNetworkFlagsReachable)) ? YES : NO;
}
+ (AKNetworkReachability *)networkReachabilityWithHost:(NSString *)nameOrAddress {
- return [[[self alloc] initWithHost:nameOrAddress] autorelease];
+ return [[[self alloc] initWithHost:nameOrAddress] autorelease];
}
- (id)initWithHost:(NSString *)nameOrAddress {
- self = [super init];
- if (self == nil) {
- return nil;
- }
-
- if ([nameOrAddress length] == 0) {
- [self release];
- return nil;
- }
-
- if ([nameOrAddress ak_isIPAddress]) {
- struct sockaddr_in sin;
- bzero(&sin, sizeof(sin));
- sin.sin_len = sizeof(sin);
- sin.sin_family = AF_INET;
- inet_aton([nameOrAddress UTF8String], &sin.sin_addr);
- reachability_
- = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault,
- (struct sockaddr *)&sin);
- } else {
- reachability_
- = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault,
- [nameOrAddress UTF8String]);
- }
-
- context_.info = self;
- Boolean callbackSet = SCNetworkReachabilitySetCallback(reachability_,
- &AKReachabilityChanged,
- &context_);
- if (!callbackSet) {
- [self release];
- return nil;
- }
-
- Boolean scheduled
- = SCNetworkReachabilityScheduleWithRunLoop(reachability_,
- CFRunLoopGetMain(),
- kCFRunLoopDefaultMode);
- if (!scheduled) {
- [self release];
- return nil;
- }
-
- [self setHost:nameOrAddress];
-
- return self;
+ self = [super init];
+ if (self == nil) {
+ return nil;
+ }
+
+ if ([nameOrAddress length] == 0) {
+ [self release];
+ return nil;
+ }
+
+ if ([nameOrAddress ak_isIPAddress]) {
+ struct sockaddr_in sin;
+ bzero(&sin, sizeof(sin));
+ sin.sin_len = sizeof(sin);
+ sin.sin_family = AF_INET;
+ inet_aton([nameOrAddress UTF8String], &sin.sin_addr);
+ reachability_ = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (struct sockaddr *)&sin);
+ } else {
+ reachability_ = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, [nameOrAddress UTF8String]);
+ }
+
+ context_.info = self;
+ Boolean callbackSet = SCNetworkReachabilitySetCallback(reachability_, &AKReachabilityChanged, &context_);
+ if (!callbackSet) {
+ [self release];
+ return nil;
+ }
+
+ Boolean scheduled = SCNetworkReachabilityScheduleWithRunLoop(reachability_,
+ CFRunLoopGetMain(),
+ kCFRunLoopDefaultMode);
+ if (!scheduled) {
+ [self release];
+ return nil;
+ }
+
+ [self setHost:nameOrAddress];
+
+ return self;
}
- (void)dealloc {
- SCNetworkReachabilityUnscheduleFromRunLoop(reachability_,
- CFRunLoopGetMain(),
- kCFRunLoopDefaultMode);
- if (reachability_ != NULL) {
- CFRelease(reachability_);
- }
-
- [host_ release];
-
- [super dealloc];
+ SCNetworkReachabilityUnscheduleFromRunLoop(reachability_, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
+ if (reachability_ != NULL) {
+ CFRelease(reachability_);
+ }
+
+ [host_ release];
+
+ [super dealloc];
}
- (NSString *)description {
- return [NSString stringWithFormat:@"%@ reachability", [self host]];
+ return [NSString stringWithFormat:@"%@ reachability", [self host]];
}
@end
-static void AKReachabilityChanged(SCNetworkReachabilityRef target,
- SCNetworkConnectionFlags flags,
- void *info) {
-
- AKNetworkReachability *networkReachability = (AKNetworkReachability *)info;
-
- if (flags & kSCNetworkFlagsReachable) {
- [[NSNotificationCenter defaultCenter]
- postNotificationName:AKNetworkReachabilityDidBecomeReachableNotification
- object:networkReachability];
- } else {
- [[NSNotificationCenter defaultCenter]
- postNotificationName:AKNetworkReachabilityDidBecomeUnreachableNotification
- object:networkReachability];
- }
+static void AKReachabilityChanged(SCNetworkReachabilityRef target, SCNetworkConnectionFlags flags, void *info) {
+ AKNetworkReachability *networkReachability = (AKNetworkReachability *)info;
+
+ if (flags & kSCNetworkFlagsReachable) {
+ [[NSNotificationCenter defaultCenter] postNotificationName:AKNetworkReachabilityDidBecomeReachableNotification
+ object:networkReachability];
+ } else {
+ [[NSNotificationCenter defaultCenter] postNotificationName:AKNetworkReachabilityDidBecomeUnreachableNotification
+ object:networkReachability];
+ }
}
Oops, something went wrong.

0 comments on commit b584f29

Please sign in to comment.