Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
IpHostPort.cs
1using System;
3using System.Security.Cryptography.X509Certificates;
4using System.Text;
5using System.Threading.Tasks;
6using Waher.Content;
13using Waher.Security;
17
18namespace Waher.Things.Ip
19{
23 public class IpHostPort : IpHost
24 {
25 private int port = 0;
26 private bool tls = false;
27
33 public override Task<bool> AcceptsChildAsync(INode Child)
34 {
35 return Task.FromResult(false);
36 }
37
43 public override Task<string> GetTypeNameAsync(Language Language)
44 {
45 return Language.GetStringAsync(typeof(IpHostPort), 23, "Port on IP Host");
46 }
47
51 [Page(1, "IP")]
52 [Header(4, "Port Number:", 60)]
53 [ToolTip(5, "Port number to use when communicating with device.")]
54 [DefaultValue(0)]
55 [Range(0, 65535)]
56 [Required]
57 public int Port
58 {
59 get => this.port;
60 set => this.port = value;
61 }
62
66 [Page(1, "IP")]
67 [Header(11, "Encrypted (TLS)", 70)]
68 [ToolTip(12, "Check if Transport Layer Encryption (TLS) should be used.")]
69 public bool Tls
70 {
71 get => this.tls;
72 set => this.tls = value;
73 }
74
83 public async Task<BinaryTcpClient> ConnectTcp(bool DecoupledEvents, params ISniffer[] Sniffers)
84 {
85 X509Certificate Certificate = Types.TryGetModuleParameter<X509Certificate>("X509");
86 BinaryTcpClient Client = new BinaryTcpClient(DecoupledEvents, Sniffers);
87 await Client.ConnectAsync(this.Host, this.port, this.tls);
88
89 if (this.tls)
90 {
91 await Client.UpgradeToTlsAsClient(Certificate, Crypto.SecureTls);
92 Client.Continue();
93 }
94
95 return Client;
96 }
97
107 public async Task<TextTcpClient> ConnectTcp(Encoding Encoding, bool DecoupledEvents, params ISniffer[] Sniffers)
108 {
109 X509Certificate Certificate = Types.TryGetModuleParameter<X509Certificate>("X509");
110 TextTcpClient Client = new TextTcpClient(Encoding, DecoupledEvents, Sniffers);
111 await Client.ConnectAsync(this.Host, this.port, this.tls);
112
113 if (this.tls)
114 {
115 await Client.UpgradeToTlsAsClient(Certificate, Crypto.SecureTls);
116 Client.Continue();
117 }
118
119 return Client;
120 }
121
128 public async override Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
129 {
130 LinkedList<Parameter> Result = await base.GetDisplayableParametersAsync(Language, Caller) as LinkedList<Parameter>;
131
132 Result.AddLast(new Int32Parameter("Port", await Language.GetStringAsync(typeof(IpHost), 10, "Port"), this.port));
133
134 return Result;
135 }
136
141 public async override Task StartReadout(ISensorReadout Request)
142 {
143 try
144 {
145 DateTime Now = DateTime.UtcNow;
146 string Module = typeof(IpHost).Namespace;
147
148 using BinaryTcpClient Client = await this.ConnectTcp(false);
149
150 List<Field> Fields = new List<Field>()
151 {
152 new QuantityField(this, Now, "Connect", (DateTime.UtcNow-Now).TotalMilliseconds,
153 0, "ms", FieldType.Momentary, FieldQoS.AutomaticReadout, Module, 13)
154 };
155
156 if (Request.IsIncluded(FieldType.Identity) && this.Tls)
157 {
158 AddCertificateFields(Client.RemoteCertificate,
159 Client.RemoteCertificateValid, this, Now, Fields);
160 }
161
162 await Request.ReportFields(false, Fields);
163 }
164 catch (Exception ex)
165 {
166 await Request.ReportErrors(false, new ThingError(this, ex.Message));
167 }
168
169 await base.StartReadout(Request);
170 }
171
180 public static void AddCertificateFields(X509Certificate Certificate, bool IsValid,
181 ThingReference Node, DateTime Timestamp, List<Field> Fields)
182 {
183 if (!(Certificate is null))
184 {
185 string Module = typeof(IpHost).Namespace;
186 string s;
187
188 Fields.Add(new BooleanField(Node, Timestamp, "Certificate Valid", IsValid, FieldType.Identity | FieldType.Status, FieldQoS.AutomaticReadout, Module, 14));
189 Fields.Add(new StringField(Node, Timestamp, "Subject", Certificate.Subject, FieldType.Identity, FieldQoS.AutomaticReadout, Module, 15));
190 Fields.Add(new StringField(Node, Timestamp, "Issuer", Certificate.Issuer, FieldType.Identity, FieldQoS.AutomaticReadout, Module, 16));
191 Fields.Add(new StringField(Node, Timestamp, "S/N", Certificate.GetSerialNumberString(), FieldType.Identity, FieldQoS.AutomaticReadout, Module, 17));
192 Fields.Add(new StringField(Node, Timestamp, "Digest", Certificate.GetCertHashString(), FieldType.Identity, FieldQoS.AutomaticReadout, Module, 20));
193 Fields.Add(new StringField(Node, Timestamp, "Algorithm", Certificate.GetKeyAlgorithm(), FieldType.Identity, FieldQoS.AutomaticReadout, Module, 21));
194 Fields.Add(new StringField(Node, Timestamp, "Public Key", Certificate.GetPublicKeyString(), FieldType.Identity, FieldQoS.AutomaticReadout, Module, 22));
195
196 if (CommonTypes.TryParseRfc822(s = Certificate.GetEffectiveDateString(), out DateTimeOffset TP))
197 Fields.Add(new DateTimeField(Node, Timestamp, "Effective", TP.UtcDateTime, FieldType.Identity, FieldQoS.AutomaticReadout, Module, 18));
198 else
199 Fields.Add(new StringField(Node, Timestamp, "Effective", s, FieldType.Identity, FieldQoS.AutomaticReadout, Module, 18));
200
201 if (CommonTypes.TryParseRfc822(s = Certificate.GetExpirationDateString(), out TP))
202 Fields.Add(new DateTimeField(Node, Timestamp, "Expires", TP.UtcDateTime, FieldType.Identity, FieldQoS.AutomaticReadout, Module, 19));
203 else
204 Fields.Add(new StringField(Node, Timestamp, "Expires", s, FieldType.Identity, FieldQoS.AutomaticReadout, Module, 19));
205 }
206 }
207
208 }
209}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:15
static bool TryParseRfc822(string s, out DateTimeOffset Value)
Parses a date and time value encoded according to RFC 822, §5.
Definition: CommonTypes.cs:172
Implements a binary TCP Client, by encapsulating a TcpClient. It also makes the use of TcpClient safe...
Implements a text-based TCP Client, by using the thread-safe full-duplex BinaryTcpClient.
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:607
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
Current date and time.
Definition: Now.cs:12
Helper methods for encrypting and decrypting streams of data.
Definition: Crypto.cs:14
const SslProtocols SecureTls
TLS 1.2 & 1.3
Definition: Crypto.cs:18
Node representing an IP Host machine.
Definition: IpHost.cs:25
string Host
Host name or IP address.
Definition: IpHost.cs:73
Node representing a port on an IP Host machine.
Definition: IpHostPort.cs:24
async Task< BinaryTcpClient > ConnectTcp(bool DecoupledEvents, params ISniffer[] Sniffers)
Connect to the remote host and port using a binary protocol over TCP.
Definition: IpHostPort.cs:83
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
Definition: IpHostPort.cs:43
static void AddCertificateFields(X509Certificate Certificate, bool IsValid, ThingReference Node, DateTime Timestamp, List< Field > Fields)
Adds information from a certificate as fields to a collection of fields.
Definition: IpHostPort.cs:180
async override Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
Definition: IpHostPort.cs:128
override Task< bool > AcceptsChildAsync(INode Child)
If the node accepts a presumptive child, i.e. can receive as a child (if that child accepts the node ...
Definition: IpHostPort.cs:33
bool Tls
If connection is encrypted using TLS or not.
Definition: IpHostPort.cs:70
async Task< TextTcpClient > ConnectTcp(Encoding Encoding, bool DecoupledEvents, params ISniffer[] Sniffers)
Connect to the remote host and port using a text-based protocol over TCP.
Definition: IpHostPort.cs:107
int Port
Port number.
Definition: IpHostPort.cs:58
async override Task StartReadout(ISensorReadout Request)
Starts the readout of the sensor.
Definition: IpHostPort.cs:141
Tokens available in request.
Definition: RequestOrigin.cs:9
Represents a boolean value that can be either true or false.
Definition: BooleanField.cs:11
Represents a date and optional time value.
Represents a physical quantity value.
Represents a string value.
Definition: StringField.cs:10
Contains information about an error on a thing
Definition: ThingError.cs:10
Contains a reference to a thing
Interface for sniffers. Sniffers can be added to ICommunicationLayer classes to eavesdrop on communic...
Definition: ISniffer.cs:10
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
Interface for classes managing sensor data readouts.
bool IsIncluded(string FieldName)
Checks if a field with the given parameters is included in the readout.
Task ReportErrors(bool Done, params ThingError[] Errors)
Report error states to the client.
Task ReportFields(bool Done, params Field[] Fields)
Report read fields to the client.
FieldQoS
Field Quality of Service flags
Definition: FieldQoS.cs:10
FieldType
Field Type flags
Definition: FieldType.cs:10