Skip to content
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
2 changes: 1 addition & 1 deletion Classes/SSDPService.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ - (id)initWithHeaders:(NSDictionary *)headers {
self = [super init];
if (self) {
_location = [NSURL URLWithString:[headers objectForKey:@"location"]];
_serviceType = [headers objectForKey:@"st"];
_serviceType = [headers objectForKey:@"st"] ? [headers objectForKey:@"st"] : [headers objectForKey:@"nt"];
_uniqueServiceName = [headers objectForKey:@"usn"];
_server = [headers objectForKey:@"server"];
}
Expand Down
17 changes: 8 additions & 9 deletions Classes/SSDPServiceBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
@class SSDPService;

@protocol SSDPServiceBrowserDelegate
- (void) ssdpBrowser:(SSDPServiceBrowser *)browser didNotStartBrowsingForServices:(NSError *)error;
- (void) ssdpBrowser:(SSDPServiceBrowser *)browser didFindService:(SSDPService *)service;
- (void) ssdpBrowser:(SSDPServiceBrowser *)browser didRemoveService:(SSDPService *)service;
- (void)ssdpBrowser:(SSDPServiceBrowser *)browser didNotStartBrowsingForServices:(NSError *)error;
- (void)ssdpBrowser:(SSDPServiceBrowser *)browser didFindService:(SSDPService *)service;
- (void)ssdpBrowser:(SSDPServiceBrowser *)browser didRemoveService:(SSDPService *)service;
@end


Expand All @@ -39,13 +39,12 @@
@property(readonly, nonatomic) NSString *networkInterface;
@property(assign, nonatomic) id<SSDPServiceBrowserDelegate> delegate;

- (id) initWithServiceType:(NSString *)serviceType onInterface:(NSString *)networkInterface;
- (id) initWithServiceType:(NSString *)serviceType;
- (id)initWithServiceType:(NSString *)serviceType onInterface:(NSString *)networkInterface;
- (id)initWithServiceType:(NSString *)serviceType;

- (void) startBrowsingForServices;
- (void) stopBrowsingForServices;
- (void)startBrowsingForServices;
- (void)stopBrowsingForServices;


+ (NSDictionary *) availableNetworkInterfaces;
+ (NSDictionary *)availableNetworkInterfaces;

@end
44 changes: 35 additions & 9 deletions Classes/SSDPServiceBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ @interface SSDPServiceBrowser () {

@implementation SSDPServiceBrowser

- (id) initWithServiceType:(NSString *)serviceType onInterface:(NSString *)networkInterface {
- (id)initWithServiceType:(NSString *)serviceType onInterface:(NSString *)networkInterface {
self = [super init];
if (self) {
_serviceType = [serviceType copy];
Expand Down Expand Up @@ -112,13 +112,23 @@ - (void)startBrowsingForServices {

NSDictionary *interfaces = [SSDPServiceBrowser availableNetworkInterfaces];
NSData *sourceAddress = _networkInterface? [interfaces objectForKey:_networkInterface] : nil;
if( !sourceAddress ) sourceAddress = [[interfaces allValues] firstObject];

if(![_socket bindToAddress:sourceAddress error:&err]) {
[self _notifyDelegateWithError:err];
return;

if(sourceAddress) {
// Bind to address to receive unicast datagrams only
if(![_socket bindToAddress:sourceAddress error:&err]) {
[self _notifyDelegateWithError:err];
return;
}
}
else {
// Bind to port to receive unicast and multicast datagrams
if(![_socket bindToPort:SSDPMulticastUDPPort error:&err]) {
[self _notifyDelegateWithError:err];
return;
}
}

// Join multicast group in order to receive multicast datagrams
if(![_socket joinMulticastGroup:SSDPMulticastGroupAddress error:&err]) {
[self _notifyDelegateWithError:err];
return;
Expand Down Expand Up @@ -157,10 +167,18 @@ - (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
NSString *msg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if( msg ) {
NSDictionary *headers = [self _parseHeadersFromMessage:msg];
if( [headers objectForKey:SSDPResponseStatusKey] ) {
SSDPService *service = [[SSDPService alloc] initWithHeaders:headers];
SSDPService *service = [[SSDPService alloc] initWithHeaders:headers];
if( [[headers objectForKey:SSDPResponseStatusKey] isEqualToString:@"200"] ) {
[self _notifyDelegateWithFoundService:service];
}
else if ([[headers objectForKey:SSDPRequestMethodKey] isEqualToString:@"NOTIFY"]) {
if ([[headers objectForKey:@"nts"] isEqualToString:@"ssdp:alive"]) {
[self _notifyDelegateWithFoundService:service];
}
else if ([[headers objectForKey:@"nts"] isEqualToString:@"ssdp:byebye"]) {
[self _notifyDelegateWithRemovedService:service];
}
}
}
else {
NSString *host = nil;
Expand Down Expand Up @@ -231,8 +249,16 @@ - (void)_notifyDelegateWithFoundService:(SSDPService *)service
});
}

- (void)_notifyDelegateWithRemovedService:(SSDPService *)service
{
dispatch_async(dispatch_get_main_queue(), ^{
@autoreleasepool {
[_delegate ssdpBrowser:self didRemoveService:service];
}
});
}

+ (NSDictionary *) availableNetworkInterfaces {
+ (NSDictionary *)availableNetworkInterfaces {
NSMutableDictionary *addresses = [NSMutableDictionary dictionary];
struct ifaddrs *interfaces = NULL;
struct ifaddrs *ifa = NULL;
Expand Down