-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSSDPDiscovery.m
More file actions
204 lines (182 loc) · 6.5 KB
/
SSDPDiscovery.m
File metadata and controls
204 lines (182 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// SSDPDiscovery.m
// SSDP Browser
//
// Created by Thomas Tempelmann on 20.04.24.
// Copyright © 2024 Thomas Tempelmann. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "SSDPDiscovery.h"
#import "GCDAsyncUdpSocket.h"
static NSExceptionName SSDPDiscoveryException = @"SSDPDiscoveryException"; // used only here, not thrown to callers
@interface SSDPDiscovery() <GCDAsyncUdpSocketDelegate>
@property NSMutableArray<GCDAsyncUdpSocket*> *sockets;
@property NSError *lastError;
@property BOOL stopped;
- (BOOL) isDiscovering;
@end
@implementation SSDPDiscovery
- (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
fromAddress:(NSData *)address
withFilterContext:(nullable id)filterContext
{
NSString *host = [GCDAsyncUdpSocket hostFromAddress:address];
NSString *msg = [NSString.alloc initWithData:data encoding:NSUTF8StringEncoding];
if (msg.length > 0) {
SSDPService *service = [SSDPService.alloc initHost:host response:msg];
[self.delegate ssdpDiscovery:self didDiscoverService:service];
}
}
-(void)udpSocket:(GCDAsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error {
NSLog(@"%s %@", __func__, error);
}
- (void)udpSocketDidClose:(GCDAsyncUdpSocket *)sock withError:(NSError * _Nullable)error
{
[self.sockets removeObject:sock];
if (error) {
if (error.code == 65) {
// no need to report "not reachable"
} else {
NSLog(@"Socket error: %@", error);
self.lastError = error;
}
}
[self checkFinish];
}
- (void) checkFinish {
if (!self.isDiscovering) {
if (self.lastError && [self.delegate respondsToSelector:@selector(ssdpDiscovery:didFinishWithError:)]) {
[self.delegate ssdpDiscovery:self didFinishWithError:self.lastError];
} else if ([self.delegate respondsToSelector:@selector(ssdpDiscoveryDidFinish:)]) {
[self.delegate ssdpDiscoveryDidFinish:self];
}
self.delegate = nil;
}
}
- (instancetype) init {
if (self = [super init]) {
self.sockets = NSMutableArray.new;
}
return self;
}
- (void) dealloc {
[self stop];
}
- (BOOL) isDiscovering {
return self.sockets.count > 0;
}
- (void) discoverServiceForDuration:(NSTimeInterval)duration searchTarget:(NSString*)searchTarget port:(UInt16)port onInterfaces:(NSArray<NSString*>*)onInterfaces specificAddress:(NSString*)specificAddress
{
id tmpDelegate = self.delegate;
self.delegate = nil;
[self forceStop];
self.lastError = nil;
self.stopped = NO;
self.delegate = tmpDelegate;
if ([self.delegate respondsToSelector:@selector(ssdpDiscoveryDidStart:)]) {
[self.delegate ssdpDiscoveryDidStart:self];
}
dispatch_queue_t queue = dispatch_get_main_queue();
if (specificAddress) {
// if specificAddress is given, we only query that one address, without using a multicast
GCDAsyncUdpSocket *socket = [GCDAsyncUdpSocket.alloc initWithDelegate:self delegateQueue:queue];
socket.userTag = specificAddress;
NSError *error = nil;
[socket bindToPort:0 error:&error];
if (error) {
self.lastError = error;
} else {
[socket beginReceiving:&error];
if (error) {
self.lastError = error;
} else {
NSString *message = @"M-SEARCH * HTTP/1.1\r\nMAN: \"ssdp:discover\"\r\nHOST: %@:%d\r\nST: %@\r\nMX: %d\r\n\r\n";
message = [NSString stringWithFormat:message, specificAddress, (int)port, searchTarget, (int)duration];
[socket sendData:[message dataUsingEncoding:NSUTF8StringEncoding] toHost:specificAddress port:port withTimeout:-1 tag:0];
[self.sockets addObject:socket];
}
}
} else {
for (__strong NSString *interface in onInterfaces) {
if (interface.length == 0) {
interface = nil;
}
// Determine the multicast address based on the interface's address type (ipv4 vs ipv6)
BOOL useIP4;
NSString *multicastAddr;
NSData *interface4 = nil;
NSData *interface6 = nil;
[GCDAsyncUdpSocket convertInterfaceDescription:interface port:port intoAddress4:&interface4 address6:&interface6];
if (interface4) {
multicastAddr = @"239.255.255.250";
useIP4 = YES;
} else if (interface6) {
multicastAddr = @"ff02::c"; // use "ff02::c" for "link-local" or "ff05::c" for "site-local"
useIP4 = NO;
} else {
continue;
}
// Set up the UDP socket
NSError *error = nil;
GCDAsyncUdpSocket *socket = [GCDAsyncUdpSocket.alloc initWithDelegate:self delegateQueue:queue];
socket.userTag = interface;
@try {
[socket enableReusePort:YES error:&error];
if (error) {
[NSException raise:SSDPDiscoveryException format:@"enableReusePort failed; %@", error];
}
[socket setIPv4Enabled:useIP4];
[socket setIPv6Enabled:!useIP4];
NSString *interfaceForBind = [interface componentsSeparatedByString:@"%"].lastObject;
if (interfaceForBind.length == 0) {
interfaceForBind = nil;
}
[socket bindToPort:0 interface:interfaceForBind error:&error];
if (error) {
[NSException raise:SSDPDiscoveryException format:@"bindToPort failed; %@", error];
}
if (useIP4) {
// Apparently needed when using a VPN that routes all traffic thru the tunnel
[socket sendIPv4MulticastOnInterface:interfaceForBind error:nil];
} else {
[socket sendIPv6MulticastOnInterface:interfaceForBind error:nil];
}
[socket beginReceiving:&error];
if (error) {
[NSException raise:SSDPDiscoveryException format:@"bindToPort failed; %@", error];
}
NSString *message = @"M-SEARCH * HTTP/1.1\r\nMAN: \"ssdp:discover\"\r\nHOST: %@:%d\r\nST: %@\r\nMX: %d\r\n\r\n";
message = [NSString stringWithFormat:message, multicastAddr, (int)port, searchTarget, (int)duration];
[socket sendData:[message dataUsingEncoding:NSUTF8StringEncoding] toHost:multicastAddr port:port withTimeout:-1 tag:0];
[self.sockets addObject:socket];
} @catch (NSException *exception) {
[socket close];
NSLog (@"Socket error: %@ on interface %@", error, interface.length > 0 ? interface : @"localhost");
self.lastError = error;
}
}
}
if (self.isDiscovering) { // Might we run into a race condition here?
dispatch_after (dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), queue, ^{
[self stop];
});
} else {
if ([self.delegate respondsToSelector:@selector(ssdpDiscoveryDidFinish:)]) {
[self.delegate ssdpDiscoveryDidFinish:self];
}
}
}
- (void) forceStop {
self.stopped = YES;
while (self.isDiscovering) {
GCDAsyncUdpSocket *socket = self.sockets.lastObject;
[self.sockets removeLastObject];
[socket close];
}
}
- (void) stop {
if (self.isDiscovering) {
[self forceStop];
}
}
@end