Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I made some improvements that will make it much easier to use with less confusion #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
build/
PushMeBaby.xcodeproj/xcuserdata/
PushMeBaby.xcodeproj/xcuserdata/
PushMeBaby.xcodeproj/project.xcworkspace/
PushMeBaby.xcodeproj/TemplateIcon.icns
2 changes: 2 additions & 0 deletions Classes/ApplicationDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
SecKeychainRef keychain;
SecCertificateRef certificate;
SecIdentityRef identity;
NSWindow* window;
}
@property (nonatomic, retain) IBOutlet NSWindow* window;
#pragma mark IBAction
- (IBAction)push:(id)sender;
@end
83 changes: 62 additions & 21 deletions Classes/ApplicationDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,22 @@
//
// Created by Stefan Hafeneger on 07.04.09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
// Modified by jlott on 01.01.2012
//

#import "ApplicationDelegate.h"

// apple gateway must be production if using a production certificate, else if using a developer certificate, then use sandbox
#define kApplePushGateway "gateway.push.apple.com" //"gateway.sandbox.push.apple.com"

// Device token should be 32 bytes
// The push text box requires there to be spaces in the token between every 8 characters. There is code to check and fix this at runtime.
#define kDeviceToken @"1111111122222222aaaaaaaabbbbbbbbccccccccddddddddeeeeeeeeffffffff"

// name the cert to whatever you want. Make sure the cert is bundled with app (check the Copy Bundle Resources build phase)
// cert should be of extension ".cer" and should NOT contain the private key, just the cert by itself.
#define kPushCertificate @"Push_Notification_Certificate_Production.cer"

@interface ApplicationDelegate ()
#pragma mark Properties
@property(nonatomic, retain) NSString *deviceToken, *payload, *certificate;
Expand All @@ -17,15 +29,17 @@ - (void)disconnect;
@end

@implementation ApplicationDelegate
@synthesize window;

#pragma mark Allocation

- (id)init {
self = [super init];
if(self != nil) {
self.deviceToken = @"";
self.payload = @"{\"aps\":{\"alert\":\"This is some fancy message.\",\"badge\":1}}";
self.certificate = [[NSBundle mainBundle] pathForResource:@"apns" ofType:@"cer"];
self.deviceToken = kDeviceToken;
self.payload = @"{\"aps\":{\"alert\":\"This is some fancy message.\",\"badge\":1,\"sound\" : \"bingbong.aiff\"}}";
NSString* certificateName = kPushCertificate;
self.certificate = [[NSBundle mainBundle] pathForResource:[certificateName stringByDeletingPathExtension] ofType:[certificateName pathExtension]];
}
return self;
}
Expand Down Expand Up @@ -76,41 +90,41 @@ - (void)connect {

// Establish connection to server.
PeerSpec peer;
result = MakeServerConnection("gateway.sandbox.push.apple.com", 2195, &socket, &peer);// NSLog(@"MakeServerConnection(): %d", result);
result = MakeServerConnection(kApplePushGateway, 2195, &socket, &peer); NSLog(@"MakeServerConnection(): [%s]- %@", (char *)GetMacOSStatusErrorString(result),[NSString stringWithUTF8String:(char *) GetMacOSStatusCommentString(result)] );

// Create new SSL context.
result = SSLNewContext(false, &context);// NSLog(@"SSLNewContext(): %d", result);
result = SSLNewContext(false, &context); NSLog(@"SSLNewContext(): [%s]- %@", (char *)GetMacOSStatusErrorString(result),[NSString stringWithUTF8String:(char *) GetMacOSStatusCommentString(result)] );

// Set callback functions for SSL context.
result = SSLSetIOFuncs(context, SocketRead, SocketWrite);// NSLog(@"SSLSetIOFuncs(): %d", result);
result = SSLSetIOFuncs(context, SocketRead, SocketWrite); NSLog(@"SSLSetIOFuncs(): [%s]- %@", (char *)GetMacOSStatusErrorString(result),[NSString stringWithUTF8String:(char *) GetMacOSStatusCommentString(result)] );

// Set SSL context connection.
result = SSLSetConnection(context, socket);// NSLog(@"SSLSetConnection(): %d", result);
result = SSLSetConnection(context, socket); NSLog(@"SSLSetConnection(): [%s]- %@", (char *)GetMacOSStatusErrorString(result),[NSString stringWithUTF8String:(char *) GetMacOSStatusCommentString(result)] );

// Set server domain name.
result = SSLSetPeerDomainName(context, "gateway.sandbox.push.apple.com", 30);// NSLog(@"SSLSetPeerDomainName(): %d", result);
result = SSLSetPeerDomainName(context, kApplePushGateway, [[NSString stringWithUTF8String:kApplePushGateway] length]); NSLog(@"SSLSetPeerDomainName(): [%s]- %@", (char *)GetMacOSStatusErrorString(result),[NSString stringWithUTF8String:(char *) GetMacOSStatusCommentString(result)] );

// Open keychain.
result = SecKeychainCopyDefault(&keychain);// NSLog(@"SecKeychainOpen(): %d", result);
result = SecKeychainCopyDefault(&keychain); NSLog(@"SecKeychainOpen(): [%s]- %@", (char *)GetMacOSStatusErrorString(result),[NSString stringWithUTF8String:(char *) GetMacOSStatusCommentString(result)] );

// Create certificate.
NSData *certificateData = [NSData dataWithContentsOfFile:self.certificate];
CSSM_DATA data;
data.Data = (uint8 *)[certificateData bytes];
data.Length = [certificateData length];
result = SecCertificateCreateFromData(&data, CSSM_CERT_X_509v3, CSSM_CERT_ENCODING_BER, &certificate);// NSLog(@"SecCertificateCreateFromData(): %d", result);
result = SecCertificateCreateFromData(&data, CSSM_CERT_X_509v3, CSSM_CERT_ENCODING_BER, &certificate); NSLog(@"SecCertificateCreateFromData(): [%s]- %@", (char *)GetMacOSStatusErrorString(result),[NSString stringWithUTF8String:(char *) GetMacOSStatusCommentString(result)] );

// Create identity.
result = SecIdentityCreateWithCertificate(keychain, certificate, &identity);// NSLog(@"SecIdentityCreateWithCertificate(): %d", result);
result = SecIdentityCreateWithCertificate(keychain, certificate, &identity); NSLog(@"SecIdentityCreateWithCertificate(): %d [%s]- %@", result, (char *)GetMacOSStatusErrorString(result),[NSString stringWithUTF8String:(char *) GetMacOSStatusCommentString(result)] );

// Set client certificate.
CFArrayRef certificates = CFArrayCreate(NULL, (const void **)&identity, 1, NULL);
result = SSLSetCertificate(context, certificates);// NSLog(@"SSLSetCertificate(): %d", result);
result = SSLSetCertificate(context, certificates); NSLog(@"SSLSetCertificate(): [%s]- %@", (char *)GetMacOSStatusErrorString(result),[NSString stringWithUTF8String:(char *) GetMacOSStatusCommentString(result)] );
CFRelease(certificates);

// Perform SSL handshake.
do {
result = SSLHandshake(context);// NSLog(@"SSLHandshake(): %d", result);
result = SSLHandshake(context); NSLog(@"SSLHandshake(): %d", result);NSLog(@"SSLHandshake(): [%s]- %@", (char *)GetMacOSStatusErrorString(result),[NSString stringWithUTF8String:(char *) GetMacOSStatusCommentString(result)] );
} while(result == errSSLWouldBlock);

}
Expand All @@ -125,7 +139,7 @@ - (void)disconnect {
OSStatus result;

// Close SSL session.
result = SSLClose(context);// NSLog(@"SSLClose(): %d", result);
result = SSLClose(context);// NSLog(@"SSLClose(): [%s]- %@", (char *)GetMacOSStatusErrorString(result),[NSString stringWithUTF8String:(char *) GetMacOSStatusCommentString(result)] );

// Release identity.
CFRelease(identity);
Expand All @@ -140,34 +154,59 @@ - (void)disconnect {
close((int)socket);

// Delete SSL context.
result = SSLDisposeContext(context);// NSLog(@"SSLDisposeContext(): %d", result);
result = SSLDisposeContext(context);// NSLog(@"SSLDisposeContext(): [%s]- %@", (char *)GetMacOSStatusErrorString(result),[NSString stringWithUTF8String:(char *) GetMacOSStatusCommentString(result)] );

}

#pragma mark IBAction

- (IBAction)push:(id)sender {

if(self.certificate == nil) {
NSLog(@"you need the APNS Certificate for the app to work");
exit(1);
if(! self.certificate) {
NSString* message = @"you need the APNS Certificate for the app to work";
NSLog(@"%@", message);
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setMessageText:message];
[alert beginSheetModalForWindow:window
modalDelegate:self
didEndSelector:nil
contextInfo:nil];
return;
}

// Validate input.
if(self.deviceToken == nil || self.payload == nil) {
return;
}
else if(![self.deviceToken rangeOfString:@" "].length)
{
//put in spaces in device token
NSMutableString* tempString = [NSMutableString stringWithString:self.deviceToken];
int offset = 0;
for(int i = 0; i < tempString.length; i++)
{
if(i%8 == 0 && i != 0 && i+offset < tempString.length-1)
{
//NSLog(@"i = %d + offset[%d] = %d", i, offset, i+offset);
[tempString insertString:@" " atIndex:i+offset];
offset++;
}
}
NSLog(@" device token string after adding spaces = '%@'", tempString);
self.deviceToken = tempString;
}

// Convert string into device token data.
NSMutableData *deviceToken = [NSMutableData data];
unsigned value;
NSScanner *scanner = [NSScanner scannerWithString:self.deviceToken];
while(![scanner isAtEnd]) {
[scanner scanHexInt:&value];
//NSLog(@"scanned value %x", value);
value = htonl(value);
[deviceToken appendBytes:&value length:sizeof(value)];
}

NSLog(@"device token data %@, length = %ld", deviceToken, deviceToken.length);
// Create C input variables.
char *deviceTokenBinary = (char *)[deviceToken bytes];
char *payloadBinary = (char *)[self.payload UTF8String];
Expand All @@ -192,10 +231,12 @@ - (IBAction)push:(id)sender {
memcpy(pointer, payloadBinary, payloadLength);
pointer += payloadLength;

NSLog(@"pointer - message- %ld", (pointer -message));
// Send message over SSL.
size_t processed = 0;
OSStatus result = SSLWrite(context, &message, (pointer - message), &processed);// NSLog(@"SSLWrite(): %d %d", result, processed);

NSLog(@"SSLWrite(): [%s]- %@", (char *)GetMacOSStatusErrorString(result),[NSString stringWithUTF8String:(char *) GetMacOSStatusCommentString(result)] );
NSLog(@"SSLWrite(): %d %ld", result, processed);
}

@end
18 changes: 9 additions & 9 deletions Classes/ioSock.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ OSStatus AcceptClientConnection(
{
struct sockaddr_in addr;
int sock;
int len;
socklen_t len;

len = sizeof(struct sockaddr_in);
sock = accept((int)listenSock, (struct sockaddr *) &addr, &len);
Expand Down Expand Up @@ -325,12 +325,12 @@ OSStatus SocketRead(
* RETURNED */
size_t *dataLength) /* IN/OUT */
{
UInt32 bytesToGo = *dataLength;
UInt32 initLen = bytesToGo;
size_t bytesToGo = *dataLength;
size_t initLen = bytesToGo;
UInt8 *currData = (UInt8 *)data;
int sock = (int)connection;
OSStatus rtn = noErr;
UInt32 bytesRead;
size_t bytesRead;
int rrtn;

*dataLength = 0;
Expand Down Expand Up @@ -403,17 +403,17 @@ OSStatus SocketWrite(
const void *data,
size_t *dataLength) /* IN/OUT */
{
UInt32 bytesSent = 0;
size_t bytesSent = 0;
int sock = (int)connection;
int length;
UInt32 dataLen = *dataLength;
size_t dataLen = *dataLength;
const UInt8 *dataPtr = (UInt8 *)data;
OSStatus ortn;

if(oneAtATime && (*dataLength > 1)) {
UInt32 i;
UInt32 outLen;
UInt32 thisMove;
size_t i;
size_t outLen;
size_t thisMove;

outLen = 0;
for(i=0; i<dataLen; i++) {
Expand Down
Loading