|
| 1 | +using System.Net.Sockets; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | + |
| 4 | +namespace sigtran.net.Layers.SCTP; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Identifies native SCTP platform capability status. |
| 8 | +/// </summary> |
| 9 | +public enum NativeSctpPlatformStatus |
| 10 | +{ |
| 11 | + /// <summary>The operating system is not a supported native SCTP target.</summary> |
| 12 | + UnsupportedOperatingSystem, |
| 13 | + |
| 14 | + /// <summary>The operating system is supported, but socket creation failed.</summary> |
| 15 | + SocketCreationUnavailable, |
| 16 | + |
| 17 | + /// <summary>The operating system supports creating an SCTP socket.</summary> |
| 18 | + SocketCreationSupported |
| 19 | +} |
| 20 | + |
| 21 | +/// <summary> |
| 22 | +/// Describes native SCTP platform capability. |
| 23 | +/// </summary> |
| 24 | +public sealed class NativeSctpPlatformCapability |
| 25 | +{ |
| 26 | + /// <summary>Creates a native SCTP platform capability result.</summary> |
| 27 | + /// <param name="status">The capability status.</param> |
| 28 | + /// <param name="reason">The diagnostic reason.</param> |
| 29 | + public NativeSctpPlatformCapability(NativeSctpPlatformStatus status, string reason) |
| 30 | + { |
| 31 | + Status = status; |
| 32 | + Reason = string.IsNullOrWhiteSpace(reason) ? throw new ArgumentException("Native SCTP capability reason is required.", nameof(reason)) : reason; |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary>The capability status.</summary> |
| 36 | + public NativeSctpPlatformStatus Status { get; } |
| 37 | + |
| 38 | + /// <summary>The diagnostic reason.</summary> |
| 39 | + public string Reason { get; } |
| 40 | + |
| 41 | + /// <summary>Whether native SCTP socket creation is supported.</summary> |
| 42 | + public bool CanCreateSocket => Status == NativeSctpPlatformStatus.SocketCreationSupported; |
| 43 | + |
| 44 | + /// <summary>Formats a compact capability summary.</summary> |
| 45 | + /// <returns>The capability summary.</returns> |
| 46 | + public string Describe() |
| 47 | + { |
| 48 | + return $"nativeSctp status={Status} canCreateSocket={CanCreateSocket} reason={Reason}"; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// <summary> |
| 53 | +/// Provides native SCTP platform constants and capability probing. |
| 54 | +/// </summary> |
| 55 | +public static class NativeSctpPlatform |
| 56 | +{ |
| 57 | + /// <summary>The Linux IPPROTO_SCTP protocol number.</summary> |
| 58 | + public const int IpProtocolSctp = 132; |
| 59 | + |
| 60 | + /// <summary>The socket type used by SCTP one-to-one style associations.</summary> |
| 61 | + public const SocketType SctpSocketType = SocketType.Seqpacket; |
| 62 | + |
| 63 | + /// <summary>Gets the protocol type value used when creating native SCTP sockets.</summary> |
| 64 | + public static ProtocolType SctpProtocolType => (ProtocolType)IpProtocolSctp; |
| 65 | + |
| 66 | + /// <summary>Returns whether the current operating system is the supported native SCTP target.</summary> |
| 67 | + /// <returns>True on Linux; otherwise false.</returns> |
| 68 | + public static bool IsSupportedOperatingSystem() |
| 69 | + { |
| 70 | + return OperatingSystem.IsLinux(); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary>Probes whether a native SCTP socket can be created.</summary> |
| 74 | + /// <returns>The native SCTP platform capability.</returns> |
| 75 | + public static NativeSctpPlatformCapability Probe() |
| 76 | + { |
| 77 | + if (!IsSupportedOperatingSystem()) |
| 78 | + { |
| 79 | + return new(NativeSctpPlatformStatus.UnsupportedOperatingSystem, RuntimeInformation.OSDescription); |
| 80 | + } |
| 81 | + |
| 82 | + try |
| 83 | + { |
| 84 | + using Socket socket = new(AddressFamily.InterNetwork, SctpSocketType, SctpProtocolType); |
| 85 | + return new(NativeSctpPlatformStatus.SocketCreationSupported, "SCTP socket creation succeeded."); |
| 86 | + } |
| 87 | + catch (SocketException ex) |
| 88 | + { |
| 89 | + return new(NativeSctpPlatformStatus.SocketCreationUnavailable, ex.SocketErrorCode.ToString()); |
| 90 | + } |
| 91 | + catch (PlatformNotSupportedException ex) |
| 92 | + { |
| 93 | + return new(NativeSctpPlatformStatus.SocketCreationUnavailable, ex.Message); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments