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;
2using System.Collections.Generic;
3using System.Security.Cryptography.X509Certificates;
4using System.Text;
5using System.Threading.Tasks;
6using Waher.Content;
14
15namespace Waher.Things.Ip
16{
20 public class IpHostPort : IpHost
21 {
22 private int port = 0;
23 private bool tls = false;
24
30 public override Task<bool> AcceptsChildAsync(INode Child)
31 {
32 return Task.FromResult(false);
33 }
34
40 public override Task<string> GetTypeNameAsync(Language Language)
41 {
42 return Language.GetStringAsync(typeof(IpHostPort), 23, "Port on IP Host");
43 }
44
48 [Page(1, "IP")]
49 [Header(4, "Port Number:", 60)]
50 [ToolTip(5, "Port number to use when communicating with device.")]
51 [DefaultValue(0)]
52 [Range(0, 65535)]
53 [Required]
54 public int Port
55 {
56 get => this.port;
57 set => this.port = value;
58 }
59
63 [Page(1, "IP")]
64 [Header(11, "Encrypted (TLS)", 70)]
65 [ToolTip(12, "Check if Transport Layer Encryption (TLS) should be used.")]
66 public bool Tls
67 {
68 get => this.tls;
69 set => this.tls = value;
70 }
71
80 public async Task<BinaryTcpClient> ConnectTcp(bool DecoupledEvents, params ISniffer[] Sniffers)
81 {
82 BinaryTcpClient Client = new BinaryTcpClient(DecoupledEvents, Sniffers);
83 await Client.ConnectAsync(this.Host, this.port);
84
85 if (this.tls)
86 await Client.UpgradeToTlsAsClient(System.Security.Authentication.SslProtocols.Tls12);
87
88 return Client;
89 }
90
100 public async Task<TextTcpClient> ConnectTcp(Encoding Encoding, bool DecoupledEvents, params ISniffer[] Sniffers)
101 {
102 TextTcpClient Client = new TextTcpClient(Encoding, DecoupledEvents, Sniffers);
103 await Client.ConnectAsync(this.Host, this.port);
104
105 if (this.tls)
106 await Client.UpgradeToTlsAsClient(System.Security.Authentication.SslProtocols.Tls12);
107
108 return Client;
109 }
110
117 public async override Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
118 {
119 LinkedList<Parameter> Result = await base.GetDisplayableParametersAsync(Language, Caller) as LinkedList<Parameter>;
120
121 Result.AddLast(new Int32Parameter("Port", await Language.GetStringAsync(typeof(IpHost), 10, "Port"), this.port));
122
123 return Result;
124 }
125
130 public async override Task StartReadout(ISensorReadout Request)
131 {
132 try
133 {
134 DateTime Now = DateTime.Now;
135 string Module = typeof(IpHost).Namespace;
136
137 using (BinaryTcpClient Client = await this.ConnectTcp(false))
138 {
139 List<Field> Fields = new List<Field>()
140 {
141 new QuantityField(this, Now, "Connect", (DateTime.Now-Now).TotalMilliseconds, 0, "ms", FieldType.Momentary, FieldQoS.AutomaticReadout, Module, 13)
142 };
143
144 if (Request.IsIncluded(FieldType.Identity) && this.Tls)
145 {
146 X509Certificate Cert = Client.RemoteCertificate;
147 string s;
148
149 if (!(Cert is null))
150 {
151 Fields.Add(new BooleanField(this, Now, "Certificate Valid", Client.RemoteCertificateValid, FieldType.Identity | FieldType.Status, FieldQoS.AutomaticReadout, Module, 14));
152 Fields.Add(new StringField(this, Now, "Subject", Cert.Subject, FieldType.Identity, FieldQoS.AutomaticReadout, Module, 15));
153 Fields.Add(new StringField(this, Now, "Issuer", Cert.Issuer, FieldType.Identity, FieldQoS.AutomaticReadout, Module, 16));
154 Fields.Add(new StringField(this, Now, "S/N", Cert.GetSerialNumberString(), FieldType.Identity, FieldQoS.AutomaticReadout, Module, 17));
155 Fields.Add(new StringField(this, Now, "Digest", Cert.GetCertHashString(), FieldType.Identity, FieldQoS.AutomaticReadout, Module, 20));
156 Fields.Add(new StringField(this, Now, "Algorithm", Cert.GetKeyAlgorithm(), FieldType.Identity, FieldQoS.AutomaticReadout, Module, 21));
157 Fields.Add(new StringField(this, Now, "Public Key", Cert.GetPublicKeyString(), FieldType.Identity, FieldQoS.AutomaticReadout, Module, 22));
158
159 if (CommonTypes.TryParseRfc822(s = Cert.GetEffectiveDateString(), out DateTimeOffset TP))
160 Fields.Add(new DateTimeField(this, Now, "Effective", TP.UtcDateTime, FieldType.Identity, FieldQoS.AutomaticReadout, Module, 18));
161 else
162 Fields.Add(new StringField(this, Now, "Effective", s, FieldType.Identity, FieldQoS.AutomaticReadout, Module, 18));
163
164 if (CommonTypes.TryParseRfc822(s = Cert.GetExpirationDateString(), out TP))
165 Fields.Add(new DateTimeField(this, Now, "Expires", TP.UtcDateTime, FieldType.Identity, FieldQoS.AutomaticReadout, Module, 19));
166 else
167 Fields.Add(new StringField(this, Now, "Expires", Cert.GetExpirationDateString(), FieldType.Identity, FieldQoS.AutomaticReadout, Module, 19));
168 }
169 }
170
171 await Request.ReportFields(false, Fields);
172 }
173 }
174 catch (Exception ex)
175 {
176 await Request.ReportErrors(false, new ThingError(this, ex.Message));
177 }
178
179 await base.StartReadout(Request);
180 }
181
182 }
183}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static bool TryParseRfc822(string s, out DateTimeOffset Value)
Parses a date and time value encoded according to RFC 822, §5.
Definition: CommonTypes.cs:170
Implements a binary TCP Client, by encapsulating a TcpClient. It also makes the use of TcpClient safe...
Task UpgradeToTlsAsClient(SslProtocols Protocols)
Upgrades a client connection to TLS.
bool RemoteCertificateValid
If the remote certificate is valid.
X509Certificate RemoteCertificate
Certificate used by the remote endpoint.
Task< bool > ConnectAsync(string Host, int Port)
Connects to a host using TCP.
Implements a text-based TCP Client, by using the thread-safe full-duplex BinaryTcpClient.
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
Node representing an IP Host machine.
Definition: IpHost.cs:21
string Host
Host name or IP address.
Definition: IpHost.cs:63
Node representing a port on an IP Host machine.
Definition: IpHostPort.cs:21
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:80
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
Definition: IpHostPort.cs:40
async override Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
Definition: IpHostPort.cs:117
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:30
bool Tls
If connection is encrypted using TLS or not.
Definition: IpHostPort.cs:67
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:100
int Port
Port number.
Definition: IpHostPort.cs:55
async override Task StartReadout(ISensorReadout Request)
Starts the readout of the sensor.
Definition: IpHostPort.cs:130
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
Interface for sniffers. Sniffers can be added to ICommunicationLayer classes to eavesdrop on communic...
Definition: ISniffer.cs:11
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