Skip to content

Commit 4ac76bc

Browse files
authored
Merge pull request #41 from jimm98y/features/ipv6
F Added IPV6 support
2 parents 3114a45 + b291b7b commit 4ac76bc

File tree

8 files changed

+269
-116
lines changed

8 files changed

+269
-116
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
44
<Nullable>disable</Nullable>
55
<Title>$(ProjectName)</Title>
6-
<Version>0.5.1</Version>
6+
<Version>0.5.2</Version>
77
<Authors>Lukas Volf</Authors>
88
<Copyright>MIT</Copyright>
99
<PackageProjectUrl>https://github.com/jimm98y/SharpOnvif</PackageProjectUrl>

src/OnvifClient/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ static async Task MainAsync(string[] args)
3131
Console.WriteLine($"Found device: Manufacturer = {onvifDevice.Manufacturer}, Model = {onvifDevice.Hardware}");
3232
}
3333

34-
var device = devices.FirstOrDefault(x => x.Addresses != null && x.Addresses.FirstOrDefault(x => x.Contains("127.0.0.1")) != null);
34+
var device = devices.FirstOrDefault(x => x.Addresses != null && x.Addresses.FirstOrDefault(x => x.Contains("127.0.0.1") || x.Contains("[::1]")) != null);
3535

3636
if (device == null)
3737
{
3838
Console.WriteLine("Please run OnvifService on the localhost as Administrator, or use a different camera URL and credentials.");
3939
}
4040
else
4141
{
42-
using (var client = new SimpleOnvifClient(device.Addresses.First(x => x.Contains("127.0.0.1")), "admin", "password", true))
42+
using (var client = new SimpleOnvifClient(device.Addresses.First(x => x.Contains("127.0.0.1") || x.Contains("[::1]")), "admin", "password", true))
4343
{
4444
var services = await client.GetServicesAsync(true);
4545
var cameraDateTime = await client.GetSystemDateAndTimeUtcAsync();

src/OnvifService/Onvif/DeviceImpl.cs

Lines changed: 123 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ public class DeviceImpl : DeviceBase
1818
private readonly IConfiguration _configuration;
1919

2020
public string PrimaryNICName { get; private set; }
21+
2122
public string PrimaryIPv4Address { get; private set; }
2223
public string PrimaryIPv4DNS { get; private set; }
24+
public string PrimaryIPv4Gateway { get; private set; }
25+
26+
public string PrimaryIPv6Address { get; private set; }
27+
public string PrimaryIPv6DNS { get; private set; }
28+
public string PrimaryIPv6Gateway { get; private set; }
29+
2330
public string PrimaryMACAddress { get; private set; }
2431
public string PrimaryNTPAddress { get; private set; }
25-
public string PrimaryIPv4Gateway { get; private set; }
2632

2733
public DeviceImpl(IServer server, ILogger<DeviceImpl> logger, IConfiguration configuration)
2834
{
@@ -32,16 +38,25 @@ public DeviceImpl(IServer server, ILogger<DeviceImpl> logger, IConfiguration con
3238

3339
PrimaryNICName = _configuration.GetValue("DeviceImpl:PrimaryNICName", "");
3440
if (string.IsNullOrEmpty(PrimaryNICName)) PrimaryNICName = GetPrimaryNICName();
41+
3542
PrimaryIPv4Address = _configuration.GetValue("DeviceImpl:PrimaryIPv4Address", "");
3643
if (string.IsNullOrEmpty(PrimaryIPv4Address)) PrimaryIPv4Address = GetPrimaryIPv4Address();
3744
PrimaryIPv4DNS = _configuration.GetValue("DeviceImpl:PrimaryIPv4DNS", "");
3845
if (string.IsNullOrEmpty(PrimaryIPv4DNS)) PrimaryIPv4DNS = GetPrimaryIPv4DNS();
46+
PrimaryIPv4Gateway = _configuration.GetValue("DeviceImpl:PrimaryIPv4Gateway", "");
47+
if (string.IsNullOrEmpty(PrimaryIPv4Gateway)) PrimaryIPv4Gateway = GetPrimaryIPv4Gateway();
48+
49+
PrimaryIPv6Address = _configuration.GetValue("DeviceImpl:PrimaryIPv6Address", "");
50+
if (string.IsNullOrEmpty(PrimaryIPv6Address)) PrimaryIPv6Address = GetPrimaryIPv6Address();
51+
PrimaryIPv6DNS = _configuration.GetValue("DeviceImpl:PrimaryIPv6DNS", "");
52+
if (string.IsNullOrEmpty(PrimaryIPv6DNS)) PrimaryIPv6DNS = GetPrimaryIPv6DNS();
53+
PrimaryIPv6Gateway = _configuration.GetValue("DeviceImpl:PrimaryIPv6Gateway", "");
54+
if (string.IsNullOrEmpty(PrimaryIPv6Gateway)) PrimaryIPv6Gateway = GetPrimaryIPv6Gateway();
55+
3956
PrimaryMACAddress = _configuration.GetValue("DeviceImpl:PrimaryMACAddress", "");
4057
if (string.IsNullOrEmpty(PrimaryMACAddress)) PrimaryMACAddress = GetPrimaryMACAddress();
4158
PrimaryNTPAddress = _configuration.GetValue("DeviceImpl:PrimaryNTPAddress", "");
4259
if (string.IsNullOrEmpty(PrimaryNTPAddress)) PrimaryNTPAddress = GetPrimaryNTPAddress();
43-
PrimaryIPv4Gateway = _configuration.GetValue("DeviceImpl:PrimaryIPv4Gateway", "");
44-
if (string.IsNullOrEmpty(PrimaryIPv4Gateway)) PrimaryIPv4Gateway = GetPrimaryIPv4Gateway();
4560
}
4661

4762
public override GetCapabilitiesResponse GetCapabilities(GetCapabilitiesRequest request)
@@ -167,6 +182,20 @@ public override GetNetworkInterfacesResponse GetNetworkInterfaces(GetNetworkInte
167182
},
168183
},
169184
Enabled = true,
185+
},
186+
IPv6 = new IPv6NetworkInterface()
187+
{
188+
Config = new IPv6Configuration()
189+
{
190+
Manual = new PrefixedIPv6Address[]
191+
{
192+
new PrefixedIPv6Address()
193+
{
194+
Address = PrimaryIPv6Address
195+
}
196+
},
197+
},
198+
Enabled = true,
170199
}
171200
},
172201
}
@@ -218,7 +247,8 @@ public override NetworkGateway GetNetworkDefaultGateway()
218247
{
219248
return new NetworkGateway()
220249
{
221-
IPv4Address = new string[] { PrimaryIPv4Gateway }
250+
IPv4Address = new string[] { PrimaryIPv4Gateway },
251+
IPv6Address = new string[] { PrimaryIPv6Gateway }
222252
};
223253
}
224254

@@ -460,7 +490,7 @@ private static System.Net.NetworkInformation.NetworkInterface GetPrimaryNetworkI
460490

461491
public static string GetPrimaryIPv4Address()
462492
{
463-
string ret = "0.0.0.0";
493+
string ret = "127.0.0.1";
464494
var nic = GetPrimaryNetworkInterface();
465495
if (nic != null)
466496
{
@@ -486,7 +516,7 @@ public static string GetPrimaryIPv4Address()
486516

487517
public static string GetPrimaryIPv4DNS()
488518
{
489-
string ret = "0.0.0.0";
519+
string ret = "127.0.0.1";
490520
var nic = GetPrimaryNetworkInterface();
491521
if (nic != null)
492522
{
@@ -507,28 +537,84 @@ public static string GetPrimaryIPv4DNS()
507537
return ret;
508538
}
509539

510-
public static string GetPrimaryNTPAddress(string ntp = "time.windows.com")
540+
public static string GetPrimaryIPv4Gateway()
511541
{
512-
string ret = "0.0.0.0";
513-
var dnsHostEntry = System.Net.Dns.GetHostEntry(ntp);
514-
if (dnsHostEntry != null)
542+
string ret = "127.0.0.1";
543+
var nic = GetPrimaryNetworkInterface();
544+
if (nic != null)
515545
{
516-
var addressList = dnsHostEntry.AddressList;
517-
if(addressList != null)
546+
var nicProperties = nic.GetIPProperties();
547+
if (nicProperties != null)
518548
{
519-
var ntpAddress = addressList.FirstOrDefault(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
520-
if(ntpAddress != null)
549+
var gatewayAddresses = nicProperties.GatewayAddresses;
550+
if (gatewayAddresses != null)
521551
{
522-
ret = ntpAddress.ToString();
552+
var gatewayAddress = gatewayAddresses.FirstOrDefault(x => x.Address != null && x.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
553+
if (gatewayAddress != null)
554+
{
555+
if (gatewayAddress.Address != null)
556+
{
557+
ret = gatewayAddress.Address.ToString();
558+
}
559+
}
523560
}
524561
}
525562
}
526563
return ret;
527564
}
528565

529-
public static string GetPrimaryIPv4Gateway()
566+
public static string GetPrimaryIPv6Address()
530567
{
531-
string ret = "0.0.0.0";
568+
string ret = "::1";
569+
var nic = GetPrimaryNetworkInterface();
570+
if (nic != null)
571+
{
572+
var nicProperties = nic.GetIPProperties();
573+
if (nicProperties != null)
574+
{
575+
var unicastAddresses = nicProperties.UnicastAddresses;
576+
if (unicastAddresses != null)
577+
{
578+
var unicastAddress = unicastAddresses.FirstOrDefault(x => x.Address != null && x.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6);
579+
if (unicastAddress != null)
580+
{
581+
if (unicastAddress.Address != null)
582+
{
583+
ret = unicastAddress.Address.ToString();
584+
}
585+
}
586+
}
587+
}
588+
}
589+
return ret;
590+
}
591+
592+
public static string GetPrimaryIPv6DNS()
593+
{
594+
string ret = "::1";
595+
var nic = GetPrimaryNetworkInterface();
596+
if (nic != null)
597+
{
598+
var nicProperties = nic.GetIPProperties();
599+
if (nicProperties != null)
600+
{
601+
var dnsAddresses = nicProperties.DnsAddresses;
602+
if (dnsAddresses != null)
603+
{
604+
var dnsAddress = dnsAddresses.FirstOrDefault(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6);
605+
if (dnsAddress != null)
606+
{
607+
ret = dnsAddress.ToString();
608+
}
609+
}
610+
}
611+
}
612+
return ret;
613+
}
614+
615+
public static string GetPrimaryIPv6Gateway()
616+
{
617+
string ret = "::1";
532618
var nic = GetPrimaryNetworkInterface();
533619
if (nic != null)
534620
{
@@ -538,7 +624,7 @@ public static string GetPrimaryIPv4Gateway()
538624
var gatewayAddresses = nicProperties.GatewayAddresses;
539625
if (gatewayAddresses != null)
540626
{
541-
var gatewayAddress = gatewayAddresses.FirstOrDefault(x => x.Address != null && x.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
627+
var gatewayAddress = gatewayAddresses.FirstOrDefault(x => x.Address != null && x.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6);
542628
if (gatewayAddress != null)
543629
{
544630
if (gatewayAddress.Address != null)
@@ -552,6 +638,25 @@ public static string GetPrimaryIPv4Gateway()
552638
return ret;
553639
}
554640

641+
public static string GetPrimaryNTPAddress(string ntp = "time.windows.com")
642+
{
643+
string ret = "127.0.0.1";
644+
var dnsHostEntry = System.Net.Dns.GetHostEntry(ntp);
645+
if (dnsHostEntry != null)
646+
{
647+
var addressList = dnsHostEntry.AddressList;
648+
if(addressList != null)
649+
{
650+
var ntpAddress = addressList.FirstOrDefault(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
651+
if(ntpAddress != null)
652+
{
653+
ret = ntpAddress.ToString();
654+
}
655+
}
656+
}
657+
return ret;
658+
}
659+
555660
public static string GetPrimaryMACAddress()
556661
{
557662
string ret = "00:00:00:00:00:00";

src/OnvifService/Onvif/MediaImpl.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using SharpOnvifCommon.PTZ;
77
using SharpOnvifServer;
88
using SharpOnvifServer.Media;
9-
using System;
109
using System.Collections.Generic;
1110
using System.Linq;
1211

@@ -76,14 +75,12 @@ public override Profile GetProfile(string ProfileToken)
7675

7776
public override MediaUri GetSnapshotUri(string ProfileToken)
7877
{
79-
Uri endpointUri = OperationContext.Current.IncomingMessageProperties.Via;
80-
8178
if (Profiles == null || !Profiles.ContainsKey(ProfileToken))
8279
OnvifErrors.ReturnSenderInvalidArg();
8380

8481
return new MediaUri()
8582
{
86-
Uri = string.IsNullOrEmpty(Profiles[ProfileToken].VideoSnapshotUri) ? OnvifHelpers.ChangeUriPath(endpointUri, "/preview").ToString() : Profiles[ProfileToken].VideoSnapshotUri
83+
Uri = string.IsNullOrEmpty(Profiles[ProfileToken].VideoSnapshotUri) ? OnvifHelpers.ChangeUriPath(OperationContext.Current.IncomingMessageProperties.Via, "/preview").ToString() : Profiles[ProfileToken].VideoSnapshotUri
8784
};
8885
}
8986

src/OnvifService/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public class OnvifImpl : SharpOnvifServer.DeviceMgmt.Device, SharpOnvifServer.Me
6565
serviceBuilder.AddServiceEndpoint<OnvifService.Onvif.OnvifImpl, SharpOnvifServer.Events.NotificationProducer>(onvifBinding, URI_DEVICE_SERVICE);
6666
serviceBuilder.AddServiceEndpoint<OnvifService.Onvif.OnvifImpl, SharpOnvifServer.Events.EventPortType>(onvifBinding, URI_DEVICE_SERVICE);
6767
serviceBuilder.AddServiceEndpoint<OnvifService.Onvif.OnvifImpl, SharpOnvifServer.Events.PullPoint>(onvifBinding, URI_DEVICE_SERVICE);
68+
69+
Note: this would only work as long as there are no collisions, meaning 2 interfaces must not implement the same method
6870
*/
6971

7072
serviceBuilder.AddService<OnvifService.Onvif.DeviceImpl>();

src/OnvifService/appsettings.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@
6464
"PrimaryNICName": null,
6565
"PrimaryIPv4Address": null,
6666
"PrimaryIPv4DNS": null,
67+
"PrimaryIPv4Gateway": null,
68+
"PrimaryIPv6Address": null,
69+
"PrimaryIPv6DNS": null,
70+
"PrimaryIPv6Gateway": null,
6771
"PrimaryMACAddress": null,
68-
"PrimaryNTPAddress": null,
69-
"PrimaryIPv4Gateway": null
72+
"PrimaryNTPAddress": null
7073
},
7174
"OnvifDiscovery": {
7275
"NetworkInterfaces": [],

0 commit comments

Comments
 (0)