Skip to content

Commit

Permalink
Disable network capture and hide behind constant
Browse files Browse the repository at this point in the history
  • Loading branch information
marcschier committed Jan 2, 2024
1 parent 9b7f97c commit cf5d93e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
16 changes: 3 additions & 13 deletions docs/opc-publisher/commandline.md
Original file line number Diff line number Diff line change
Expand Up @@ -859,21 +859,11 @@ Diagnostic options
--cap, --capturedevice, --CaptureDevice=VALUE
The capture device to use to capture network
traffic.
Available devices on this system:
`Local Area Connection* 10`
`Local Area Connection* 9`
`Local Area Connection* 8`
`vEthernet (Default Switch)`
`Bluetooth Network Connection`
`Local Area Connection* 2`
`Local Area Connection* 1`
`Ethernet`
`Wi-Fi`
`\Device\NPF_Loopback`
Default: `null` (disabled).
Network capture is not supported on this system.
--cpf, --capturefile, --CaptureFileName=VALUE
The file name to capture traffic to.
A device must be selected using `--cd`.
A device must be selected using `--cd` if
capture capability is supported on this system.
Default: `opcua.pcap`.
```

Expand Down
14 changes: 12 additions & 2 deletions src/Azure.IIoT.OpcUa.Publisher.Module/src/Runtime/CommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,10 @@ public CommandLine(string[] args, CommandLineLogger? logger = null)
"Explicitly enable or disable exporting prometheus metrics directly on the standard path.\nDefault: `disabled` if Otlp collector is configured, otherwise `enabled`.\n",
(bool? b) => this[Configuration.Otlp.EnableMetricsKey] = b?.ToString() ?? "True" },
{ $"cap|capturedevice=|{OpcUaClientConfig.CaptureDeviceKey}=",
$"The capture device to use to capture network traffic.\nAvailable devices on this system:\n `{string.Join("`\n `", OpcUaClientCapture.AvailableDevices)}`\nDefault: `null` (disabled).\n",
$"The capture device to use to capture network traffic.\n{SupportsCapture(OpcUaClientCapture.AvailableDevices)}\n",
(string s) => this[OpcUaClientConfig.CaptureDeviceKey] = s },
{ $"cpf|capturefile=|{OpcUaClientConfig.CaptureFileNameKey}=",
$"The file name to capture traffic to.\nA device must be selected using `--cd`.\nDefault: `{OpcUaClientConfig.CaptureFileNameDefault}`.\n",
$"The file name to capture traffic to.\nA device must be selected using `--cd` if capture capability is supported on this system.\nDefault: `{OpcUaClientConfig.CaptureFileNameDefault}`.\n",
(string s) => this[OpcUaClientConfig.CaptureFileNameKey] = s },

// testing purposes
Expand Down Expand Up @@ -617,6 +617,16 @@ void SetStoreType(string s, string storeTypeKey, string optionName)
throw new OptionException("Bad store type", optionName);
}
}

private static string SupportsCapture(IReadOnlyList<string> devices)
{
if (devices.Count == 0)
{
return "Network capture is not supported on this system.";
}
return $"Available devices on your system:\n `{string.Join("`\n `", devices)}`\nDefault: `null` (disabled).";
}

private readonly CommandLineLogger _logger;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition=" '$(SHARPPCAP)' != '' ">
<DefineConstants>SHARPPCAP</DefineConstants>
</PropertyGroup>
<ItemGroup Condition=" '$(SHARPPCAP)' != '' ">
<PackageReference Include="SharpPcap" Version="6.2.5" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
<PackageReference Include="Furly.Azure.IoT.Edge" Version="1.0.18" />
<PackageReference Include="Irony" Version="1.2.0" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Client.ComplexTypes" Version="1.4.372.106" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Client" Version="1.4.372.106" />
<PackageReference Include="SharpPcap" Version="6.2.5" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Azure.IIoT.OpcUa\src\Azure.IIoT.OpcUa.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ namespace Azure.IIoT.OpcUa.Publisher.Stack.Services
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
#if SHARPPCAP
using SharpPcap;
using SharpPcap.LibPcap;
#endif
using System.Collections.Generic;
using System.Linq;
using System.IO;
Expand All @@ -27,13 +29,15 @@ public static IReadOnlyList<string> AvailableDevices
{
get
{
#if SHARPPCAP
try
{
return LibPcapLiveDeviceList.Instance
.Select(d => d.Interface.FriendlyName ?? d.Name)
.ToArray();
}
catch
#endif
{
return Array.Empty<string>();
}
Expand Down Expand Up @@ -62,6 +66,7 @@ public void Start()
return;
}

#if SHARPPCAP
_logger.LogInformation("Using SharpPcap {Version}", Pcap.SharpPcapVersion);
var device = FindDeviceByName(deviceName);
if (device == null)
Expand All @@ -73,15 +78,21 @@ public void Start()

_device?.Dispose();
_device = new CaptureDevice(this, device, _options.Value.CaptureFileName);
#else
_logger.LogWarning("SharpPcap is not included in this build.");
#endif
}

/// <inheritdoc/>
public void Dispose()
{
#if SHARPPCAP
_device?.Dispose();
_device = null;
#endif
}

#if SHARPPCAP
/// <summary>
/// Find device by name
/// </summary>
Expand Down Expand Up @@ -173,6 +184,7 @@ public void Dispose()
}

private CaptureDevice? _device;
#endif
private readonly IOptions<OpcUaClientOptions> _options;
private readonly ILogger<OpcUaClientCapture> _logger;
}
Expand Down

0 comments on commit cf5d93e

Please sign in to comment.