Skip to content

Added ALPN support #597

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 4 additions & 0 deletions MQTTClient/MQTTClient/MQTTCFSocketTransport.h
Original file line number Diff line number Diff line change
@@ -42,6 +42,10 @@
/// A Boolean value indicating whether the transport should send data over the cell network. Defaults to `YES`.
@property (nonatomic) BOOL allowsCellularAccess;

/** ALPN to use, default to not use ALPN
*/
@property (strong, nonatomic) NSArray *alpn;

/** Require for VoIP background service
* defaults to NO
*/
21 changes: 20 additions & 1 deletion MQTTClient/MQTTClient/MQTTCFSocketTransport.m
Original file line number Diff line number Diff line change
@@ -27,13 +27,15 @@ @implementation MQTTCFSocketTransport
@synthesize streamSSLLevel;
@synthesize host;
@synthesize port;
@synthesize alpn;

- (instancetype)init {
self = [super init];
self.host = @"localhost";
self.port = 1883;
self.tls = false;
self.allowsCellularAccess = YES;
self.alpn = nil;
self.voip = false;
self.certificates = nil;
self.queue = dispatch_get_main_queue();
@@ -95,8 +97,25 @@ - (void)open {
code:errSSLInternal
userInfo:@{NSLocalizedDescriptionKey : @"Fail to init ssl output stream!"}];
}

if (@available(iOS 11, *)) {
if (self.alpn != nil && [self.alpn count] > 0) {
OSStatus err;
SSLContextRef sslContext = CFReadStreamCopyProperty(readStream, kCFStreamPropertySSLContext);
if ((err = SSLSetALPNProtocols(sslContext, (__bridge CFArrayRef) self.alpn))) {
connectError = [NSError errorWithDomain:@"MQTT" code:err userInfo:@{
NSLocalizedDescriptionKey : @"ALPN error"}];
}

sslContext = CFWriteStreamCopyProperty(writeStream, kCFStreamPropertySSLContext);
if ((err = SSLSetALPNProtocols(sslContext, (__bridge CFArrayRef) self.alpn))) {
connectError = [NSError errorWithDomain:@"MQTT" code:err userInfo:@{
NSLocalizedDescriptionKey : @"ALPN error"}];
}
}
}
}

if (!self.allowsCellularAccess) {
CFReadStreamSetProperty(readStream, kCFStreamPropertyNoCellular, kCFBooleanTrue);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertyNoCellular, kCFBooleanTrue);
17 changes: 17 additions & 0 deletions MQTTClient/MQTTClient/MQTTSSLSecurityPolicyTransport.m
Original file line number Diff line number Diff line change
@@ -65,6 +65,23 @@ - (void)open {
code:errSSLInternal
userInfo:@{NSLocalizedDescriptionKey : @"Fail to init ssl output stream!"}];
}

if (@available(iOS 11, *)) {
if (self.alpn != nil && [self.alpn count] > 0) {
OSStatus err;
SSLContextRef sslContext = CFReadStreamCopyProperty(readStream, kCFStreamPropertySSLContext);
if ((err = SSLSetALPNProtocols(sslContext, (__bridge CFArrayRef) self.alpn))) {
connectError = [NSError errorWithDomain:@"MQTT" code:err userInfo:@{
NSLocalizedDescriptionKey : @"ALPN error"}];
}

sslContext = CFWriteStreamCopyProperty(writeStream, kCFStreamPropertySSLContext);
if ((err = SSLSetALPNProtocols(sslContext, (__bridge CFArrayRef) self.alpn))) {
connectError = [NSError errorWithDomain:@"MQTT" code:err userInfo:@{
NSLocalizedDescriptionKey : @"ALPN error"}];
}
}
}
}

if (!connectError) {
4 changes: 4 additions & 0 deletions MQTTClient/MQTTClient/MQTTSessionManager.h
Original file line number Diff line number Diff line change
@@ -90,6 +90,10 @@ typedef NS_ENUM(int, MQTTSessionManagerState) {
*/
@property (readonly) UInt32 port;

/** alpn to negotiate in TLS sessions
*/
@property (strong, nonatomic) NSArray *alpn;

/** the delegate receiving incoming messages
*/
@property (weak, nonatomic) id<MQTTSessionManagerDelegate> delegate;
1 change: 1 addition & 0 deletions MQTTClient/MQTTClient/MQTTSessionManager.m
Original file line number Diff line number Diff line change
@@ -354,6 +354,7 @@ - (void)connectToInternal:(MQTTConnectHandler)connectHandler {
transport.voip = self.session.voip;
transport.queue = self.queue;
transport.streamSSLLevel = self.streamSSLLevel;
transport.alpn = self.alpn;
self.session.transport = transport;
[self.session connectWithConnectHandler:connectHandler];
}