Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonard van Driel committed Sep 9, 2012
1 parent 4781303 commit 7446faf
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 42 deletions.
4 changes: 2 additions & 2 deletions Pusher/NWAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification
- (void)applicationWillTerminate:(NSNotification *)notification
{
NWLog(@"Application will terminate");
[pusher disconnect]; pusher = nil;
[pusher disconnect]; pusher = nil;
}

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)application
{
return YES;
return YES;
}


Expand Down
2 changes: 1 addition & 1 deletion Pusher/NWPushFeedback.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ - (BOOL)connectWithIdentity:(SecIdentityRef)identity sandbox:(BOOL)sandbox

- (void)disconnect
{
[connection disconnect]; connection = nil;
[connection disconnect]; connection = nil;
}


Expand Down
36 changes: 18 additions & 18 deletions Pusher/NWPusher.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ - (BOOL)connectWithIdentityRef:(SecIdentityRef)identity sandbox:(BOOL)sandbox

- (void)disconnect
{
[connection disconnect]; connection = nil;
[connection disconnect]; connection = nil;
}


Expand Down Expand Up @@ -117,13 +117,13 @@ - (BOOL)pushPayloadData:(NSData *)payload tokenData:(NSData *)token enhance:(BOO
NWLogWarn(@"Payload cannot be more than %i bytes (UTF8)", (int)NWPayloadMaxSize);
return NO;
}

char buffer[sizeof(uint8_t) + sizeof(uint32_t) * 2 + sizeof(uint16_t) + NWDeviceTokenSize + sizeof(uint16_t) + NWPayloadMaxSize];
char *p = buffer;

uint8_t command = enhance ? 1 : 0;
memcpy(p, &command, sizeof(uint8_t));
p += sizeof(uint8_t);
char buffer[sizeof(uint8_t) + sizeof(uint32_t) * 2 + sizeof(uint16_t) + NWDeviceTokenSize + sizeof(uint16_t) + NWPayloadMaxSize];
char *p = buffer;

uint8_t command = enhance ? 1 : 0;
memcpy(p, &command, sizeof(uint8_t));
p += sizeof(uint8_t);

if (enhance) {
uint32_t ID = htonl(identifier);
Expand All @@ -135,20 +135,20 @@ - (BOOL)pushPayloadData:(NSData *)payload tokenData:(NSData *)token enhance:(BOO
p += sizeof(uint32_t);
}

uint16_t tokenLength = htons(token.length);
memcpy(p, &tokenLength, sizeof(uint16_t));
p += sizeof(uint16_t);
uint16_t tokenLength = htons(token.length);
memcpy(p, &tokenLength, sizeof(uint16_t));
p += sizeof(uint16_t);

memcpy(p, token.bytes, token.length);
p += token.length;

memcpy(p, token.bytes, token.length);
p += token.length;
uint16_t payloadLength = htons(payload.length);
memcpy(p, &payloadLength, sizeof(uint16_t));
p += sizeof(uint16_t);

uint16_t payloadLength = htons(payload.length);
memcpy(p, &payloadLength, sizeof(uint16_t));
p += sizeof(uint16_t);
memcpy(p, payload.bytes, payload.length);
p += payload.length;

memcpy(p, payload.bytes, payload.length);
p += payload.length;

NSData *data = [NSData dataWithBytes:buffer length:p - buffer];
BOOL result = [connection write:data length:NULL];

Expand Down
40 changes: 20 additions & 20 deletions Pusher/NWSSLConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@implementation NWSSLConnection {
otSocket connection;
SSLContextRef context;
SSLContextRef context;
}

@synthesize host, port, identity;
Expand All @@ -37,48 +37,48 @@ - (void)dealloc

- (BOOL)connect
{
PeerSpec spec;
OSStatus status = MakeServerConnection(host.UTF8String, (int)port, &connection, &spec);
PeerSpec spec;
OSStatus status = MakeServerConnection(host.UTF8String, (int)port, &connection, &spec);
if (status != noErr) {
NWLogWarn(@"Unable to create connection to server (%i)", status);
return NO;
}

status = SSLNewContext(false, &context);
status = SSLNewContext(false, &context);
if (status != noErr) {
NWLogWarn(@"Unable create SSL context (%i)", status);
return NO;
}
status = SSLSetIOFuncs(context, SocketRead, SocketWrite);
status = SSLSetIOFuncs(context, SocketRead, SocketWrite);
if (status != noErr) {
NWLogWarn(@"Unable to set socket callbacks (%i)", status);
return NO;
}
status = SSLSetConnection(context, connection);
status = SSLSetConnection(context, connection);
if (status != noErr) {
NWLogWarn(@"Unable to set SSL connection (%i)", status);
return NO;
}
status = SSLSetPeerDomainName(context, host.UTF8String, strlen(host.UTF8String));
status = SSLSetPeerDomainName(context, host.UTF8String, strlen(host.UTF8String));
if (status != noErr) {
NWLogWarn(@"Unable to set peer domain (%i)", status);
return NO;
}

CFArrayRef certificates = CFArrayCreate(NULL, (const void **)&identity, 1, NULL);
status = SSLSetCertificate(context, certificates);
CFArrayRef certificates = CFArrayCreate(NULL, (const void **)&identity, 1, NULL);
status = SSLSetCertificate(context, certificates);
CFRelease(certificates);
if (status != noErr) {
NWLogWarn(@"Unable to assign certificate (%i)", status);
return NO;
}
do {
status = SSLHandshake(context);
} while(status == errSSLWouldBlock);
do {
status = SSLHandshake(context);
} while(status == errSSLWouldBlock);
if (status != noErr) {
switch (status) {
case ioErr: NWLogWarn(@"Unable to perform SSL handshake, no connection"); break;
Expand Down Expand Up @@ -120,7 +120,7 @@ - (BOOL)write:(NSData *)data length:(NSUInteger *)length
{
size_t processed = 0;
const void *bytes = data.bytes;
OSStatus status = errSSLWouldBlock;
OSStatus status = errSSLWouldBlock;
for (NSUInteger i = 0; i < 4 && status == errSSLWouldBlock; i++) {
status = SSLWrite(context, bytes, data.length, &processed);
}
Expand All @@ -140,9 +140,9 @@ - (BOOL)write:(NSData *)data length:(NSUInteger *)length

- (void)disconnect
{
SSLClose(context);
close((int)connection); connection = NULL;
SSLDisposeContext(context); context = NULL;
SSLClose(context);
close((int)connection); connection = NULL;
SSLDisposeContext(context); context = NULL;
}

@end
2 changes: 1 addition & 1 deletion Pusher/NWSecTools.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ + (BOOL)identityWithCertificateData:(NSData *)certificate identity:(SecIdentityR

+ (BOOL)identityWithCertificateRef:(SecCertificateRef)certificate identity:(SecIdentityRef *)identity
{
OSStatus status = SecIdentityCreateWithCertificate(NULL, certificate, identity);
OSStatus status = SecIdentityCreateWithCertificate(NULL, certificate, identity);
if (status != noErr) {
switch (status) {
case errSecItemNotFound: NWLogWarn(@"Unable to create identitiy, private key missing"); break;
Expand Down

0 comments on commit 7446faf

Please sign in to comment.