-
Notifications
You must be signed in to change notification settings - Fork 52
/
DnssdParticipant.cs
242 lines (206 loc) · 8.61 KB
/
DnssdParticipant.cs
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// ---------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ---------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Networking;
using Windows.Networking.Sockets;
namespace NetworkHelper
{
public class DnssdParticipant : SessionParticipant
{
/// <summary>
/// The message to send in TestConnectionAsync
/// </summary>
private const string TEST_MESSAGE = "connection_test";
/// <summary>
/// The network protocol that will be accepting connections for responses.
/// </summary>
private const string NETWORK_PROTOCOL = "_tcp";
/// <summary>
/// The domain of the DNS-SD registration.
/// </summary>
private const string DOMAIN = "local";
/// <summary>
/// The service type of the DNS-SD registration.
/// </summary>
private const string SERVICE_TYPE = "_p2phelper";
/// <summary>
/// The protocol ID that identifies DNS-SD.
/// </summary>
private const string PROTOCOL_GUID = "{4526e8c1-8aac-4153-9b16-55e86ada0e54}";
/// <summary>
/// The host name property.
/// </summary>
private const string HOSTNAME_PROPERTY = "System.Devices.Dnssd.HostName";
/// <summary>
/// The service name property.
/// </summary>
private const string SERVICENAME_PROPERTY = "System.Devices.Dnssd.ServiceName";
/// <summary>
/// The instance name property.
/// </summary>
private const string INSTANCENAME_PROPERTY = "System.Devices.Dnssd.InstanceName";
/// <summary>
/// The IP address property.
/// </summary>
private const string IPADDRESS_PROPERTY = "System.Devices.IpAddress";
/// <summary>
/// The port number property.
/// </summary>
private const string PORTNUMBER_PROPERTY = "System.Devices.Dnssd.PortNumber";
/// <summary>
/// All of the properties that will be returned when a DNS-SD instance has been found.
/// </summary>
private string[] _propertyKeys = new String[] {
HOSTNAME_PROPERTY,
SERVICENAME_PROPERTY,
INSTANCENAME_PROPERTY,
IPADDRESS_PROPERTY,
PORTNUMBER_PROPERTY
};
/// <summary>
/// The AQS filter string that is used to filter services to DNS-SD only.
/// </summary>
private string _aqsQueryString = $"System.Devices.AepService.ProtocolId:={PROTOCOL_GUID} AND " +
$"System.Devices.Dnssd.Domain:=\"{DOMAIN}\" AND System.Devices.Dnssd.ServiceName:=\"{SERVICE_TYPE}.{NETWORK_PROTOCOL}\"";
/// <summary>
/// The device watcher that will be watching for DNS-SD connections.
/// </summary>
private DeviceWatcher _watcher;
/// <summary>
/// The message that will be sent when connecting to a manager.
/// </summary>
public string ListenerMessage { get; set; }
/// <summary>
/// Establishes a connection to a DNS-SD instance by connecting to it's TCP socket and sending a message.
/// </summary>
public override async Task ConnectToManagerAsync(Guid manager)
{
var subscription = base.Managers[manager] as DnssdManagerInformation;
using (var socket = new StreamSocket())
{
await socket.ConnectAsync(subscription.Host, subscription.Port);
using (var writer = new StreamWriter(socket.OutputStream.AsStreamForWrite()))
{
await writer.WriteLineAsync(ListenerMessage.ToString());
await writer.FlushAsync();
}
}
}
/// <summary>
/// Creates a TcpCommunicationChannel object for communication with the DNS-SD instance.
/// </summary>
public override ICommunicationChannel CreateCommunicationChannel(Guid manager, int flags = 0)
{
var managerCast = Managers[manager] as DnssdManagerInformation;
return new TcpCommunicationChannel { RemoteHostname = managerCast == null ? null : managerCast.Host };
}
/// <summary>
/// Uses a device watcher to monitor for available DSN-SD instances.
/// </summary>
public override async Task<bool> StartListeningAsync()
{
bool status = false;
if (_watcher == null)
{
_watcher = DeviceInformation.CreateWatcher(
_aqsQueryString,
_propertyKeys,
DeviceInformationKind.AssociationEndpointService);
_watcher.Added += async (s, a) => await OnFoundDnssdServiceAsync(a.Properties);
_watcher.Updated += async (s, a) => await OnFoundDnssdServiceAsync(a.Properties);
_watcher.Start();
status = true;
}
await Task.CompletedTask;
return status;
}
/// <summary>
/// Stops watching for available DNS-SD instances.
/// </summary>
public override bool StopListening()
{
if (_watcher != null)
{
_watcher.Stop();
_watcher = null;
return true;
}
return false;
}
/// <summary>
/// Adds the manager to the list of Managers, when a DNS-SD instance is found.
/// </summary>
private async Task OnFoundDnssdServiceAsync(IReadOnlyDictionary<string, object> properties)
{
var host = new HostName((properties[IPADDRESS_PROPERTY] as String[])[0]);
var port = properties[PORTNUMBER_PROPERTY].ToString();
bool isValidConnection = await TestConnectionAsync(host, port);
if (isValidConnection)
{
base.AddManager(
new DnssdManagerInformation { Host = host, Port = port },
properties[INSTANCENAME_PROPERTY].ToString()
);
}
}
/// <summary>
/// Attempts to connect and send a test message to a TCP host for verification the host exists.
/// </summary>
private async Task<bool> TestConnectionAsync(HostName host, string port)
{
using (var socket = new StreamSocket())
{
try
{
await socket.ConnectAsync(host, port);
using (var writer = new StreamWriter(socket.OutputStream.AsStreamForWrite()))
{
await writer.WriteLineAsync(TEST_MESSAGE);
await writer.FlushAsync();
}
}
catch (Exception)
{
return false;
}
}
return true;
}
}
public class DnssdManagerInformation
{
public HostName Host { get; set; }
public string Port { get; set; }
public override bool Equals(object obj)
{
var objCast = obj as DnssdManagerInformation;
return objCast != null ? Host.IsEqual(objCast.Host) && Port == objCast.Port : false;
}
public override int GetHashCode() => (Host + Port).GetHashCode();
}
}