|
| 1 | +using System.Net; |
| 2 | +using System.Net.Sockets; |
| 3 | + |
| 4 | +namespace sigtran.net.Layers.SCTP; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Options for a native SCTP listener. |
| 8 | +/// </summary> |
| 9 | +public sealed class NativeSctpListenerOptions |
| 10 | +{ |
| 11 | + /// <summary>Creates native SCTP listener options.</summary> |
| 12 | + /// <param name="localEndpoint">The local endpoint to bind.</param> |
| 13 | + /// <param name="backlog">The listen backlog.</param> |
| 14 | + /// <param name="outboundStreams">The outbound stream count to report for accepted associations.</param> |
| 15 | + /// <param name="inboundStreams">The inbound stream count to report for accepted associations.</param> |
| 16 | + /// <param name="defaultPayloadProtocolIdentifier">The default Payload Protocol Identifier.</param> |
| 17 | + public NativeSctpListenerOptions( |
| 18 | + SctpEndpoint localEndpoint, |
| 19 | + int backlog = 100, |
| 20 | + ushort outboundStreams = 1, |
| 21 | + ushort inboundStreams = 1, |
| 22 | + uint defaultPayloadProtocolIdentifier = SctpPayloadProtocolIdentifiers.M3ua) |
| 23 | + { |
| 24 | + if (backlog <= 0) |
| 25 | + { |
| 26 | + throw new ArgumentOutOfRangeException(nameof(backlog), "Listen backlog must be positive."); |
| 27 | + } |
| 28 | + |
| 29 | + if (outboundStreams == 0) |
| 30 | + { |
| 31 | + throw new ArgumentOutOfRangeException(nameof(outboundStreams), "Outbound stream count must be positive."); |
| 32 | + } |
| 33 | + |
| 34 | + if (inboundStreams == 0) |
| 35 | + { |
| 36 | + throw new ArgumentOutOfRangeException(nameof(inboundStreams), "Inbound stream count must be positive."); |
| 37 | + } |
| 38 | + |
| 39 | + LocalEndpoint = localEndpoint ?? throw new ArgumentNullException(nameof(localEndpoint)); |
| 40 | + Backlog = backlog; |
| 41 | + OutboundStreams = outboundStreams; |
| 42 | + InboundStreams = inboundStreams; |
| 43 | + DefaultPayloadProtocolIdentifier = defaultPayloadProtocolIdentifier; |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary>The local endpoint to bind.</summary> |
| 47 | + public SctpEndpoint LocalEndpoint { get; } |
| 48 | + |
| 49 | + /// <summary>The listen backlog.</summary> |
| 50 | + public int Backlog { get; } |
| 51 | + |
| 52 | + /// <summary>The outbound stream count to report for accepted associations.</summary> |
| 53 | + public ushort OutboundStreams { get; } |
| 54 | + |
| 55 | + /// <summary>The inbound stream count to report for accepted associations.</summary> |
| 56 | + public ushort InboundStreams { get; } |
| 57 | + |
| 58 | + /// <summary>The default Payload Protocol Identifier.</summary> |
| 59 | + public uint DefaultPayloadProtocolIdentifier { get; } |
| 60 | +} |
| 61 | + |
| 62 | +/// <summary> |
| 63 | +/// Native SCTP server-side listener. |
| 64 | +/// </summary> |
| 65 | +public sealed class NativeSctpListener : IDisposable |
| 66 | +{ |
| 67 | + private readonly INativeSctpSocketFactory _socketFactory; |
| 68 | + private readonly NativeSctpEndpointResolver _resolver = new(); |
| 69 | + private Socket? _listenSocket; |
| 70 | + |
| 71 | + /// <summary>Creates a native SCTP listener.</summary> |
| 72 | + /// <param name="socketFactory">The socket factory.</param> |
| 73 | + public NativeSctpListener(INativeSctpSocketFactory? socketFactory = null) |
| 74 | + { |
| 75 | + _socketFactory = socketFactory ?? new NativeSctpSocketFactory(); |
| 76 | + } |
| 77 | + |
| 78 | + /// <summary>Starts listening on the configured local endpoint.</summary> |
| 79 | + /// <param name="options">The listener options.</param> |
| 80 | + /// <param name="ct">A cancellation token.</param> |
| 81 | + public async Task StartAsync(NativeSctpListenerOptions options, CancellationToken ct = default) |
| 82 | + { |
| 83 | + ArgumentNullException.ThrowIfNull(options); |
| 84 | + if (_listenSocket is not null) |
| 85 | + { |
| 86 | + throw new InvalidOperationException("Native SCTP listener has already started."); |
| 87 | + } |
| 88 | + |
| 89 | + IPEndPoint local = await _resolver.ResolveAsync(options.LocalEndpoint, ct).ConfigureAwait(false); |
| 90 | + Socket socket = _socketFactory.CreateSocket(); |
| 91 | + try |
| 92 | + { |
| 93 | + socket.Bind(local); |
| 94 | + socket.Listen(options.Backlog); |
| 95 | + _listenSocket = socket; |
| 96 | + } |
| 97 | + catch |
| 98 | + { |
| 99 | + socket.Dispose(); |
| 100 | + throw; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary>Accepts one native SCTP association.</summary> |
| 105 | + /// <param name="options">The listener options used to describe accepted association defaults.</param> |
| 106 | + /// <param name="ct">A cancellation token.</param> |
| 107 | + /// <returns>The accepted native SCTP socket adapter.</returns> |
| 108 | + public async Task<NativeSctpSocketAdapter> AcceptAsync(NativeSctpListenerOptions options, CancellationToken ct = default) |
| 109 | + { |
| 110 | + ArgumentNullException.ThrowIfNull(options); |
| 111 | + Socket listenSocket = _listenSocket ?? throw new InvalidOperationException("Native SCTP listener has not started."); |
| 112 | + Socket accepted = await listenSocket.AcceptAsync(ct).ConfigureAwait(false); |
| 113 | + |
| 114 | + SctpEndpoint remote = ToSctpEndpoint(accepted.RemoteEndPoint, "remote"); |
| 115 | + SctpConnectionOptions connectionOptions = new( |
| 116 | + remote, |
| 117 | + options.LocalEndpoint, |
| 118 | + options.OutboundStreams, |
| 119 | + options.InboundStreams, |
| 120 | + options.DefaultPayloadProtocolIdentifier); |
| 121 | + |
| 122 | + return new NativeSctpSocketAdapter(accepted, connectionOptions, SctpAssociationState.Established); |
| 123 | + } |
| 124 | + |
| 125 | + /// <inheritdoc /> |
| 126 | + public void Dispose() |
| 127 | + { |
| 128 | + _listenSocket?.Dispose(); |
| 129 | + _listenSocket = null; |
| 130 | + } |
| 131 | + |
| 132 | + private static SctpEndpoint ToSctpEndpoint(EndPoint? endpoint, string label) |
| 133 | + { |
| 134 | + if (endpoint is IPEndPoint ip) |
| 135 | + { |
| 136 | + return new SctpEndpoint(ip.Address.ToString(), ip.Port); |
| 137 | + } |
| 138 | + |
| 139 | + throw new InvalidOperationException($"Accepted SCTP socket did not expose a valid {label} endpoint."); |
| 140 | + } |
| 141 | +} |
0 commit comments