Skip to content

EndpointDefinitionBuilder - Only IPv4 #271

@tinohager

Description

@tinohager

When I connect to the SMTP server for testing purposes, the connection is always slow. It takes 2 seconds to establish the connection.
This is because localhost is first resolved to ::1 and a connection is only established when it falls back to 127.0.0.1.
I'd like to change this so that we can also support IPv6.

src\SmtpServer\EndpointDefinitionBuilder.cs

Open IPv4

/// <summary>
/// Adds an endpoint with the given port.
/// </summary>
/// <param name="port">The port for the endpoint to listen on.</param>
/// <returns>The endpoint builder to continue building on.</returns>
public EndpointDefinitionBuilder Port(int port)
{
    _setters.Add(definition => definition.Endpoint = new IPEndPoint(IPAddress.Any, port));

    return this;
}

Open IPv6

  /// <summary>
  /// Adds an endpoint with the given port.
  /// </summary>
  /// <param name="port">The port for the endpoint to listen on.</param>
  /// <returns>The endpoint builder to continue building on.</returns>
  public EndpointDefinitionBuilder Port(int port)
  {
      _setters.Add(definition => definition.Endpoint = new IPEndPoint(IPAddress.IPv6Any, port));

      return this;
  }

src\SmtpServer\Net\EndpointListenerFactory.cs

With DualMode we have IPv4 and IPv6 support.

/// <inheritdoc />
public virtual IEndpointListener CreateListener(IEndpointDefinition endpointDefinition)
{
    var tcpListener = new TcpListener(endpointDefinition.Endpoint);
    tcpListener.Server.DualMode = true;
    tcpListener.Start();

    var endpointEventArgs = new EndpointEventArgs(endpointDefinition, tcpListener.LocalEndpoint);
    OnEndpointStarted(endpointEventArgs);

    return new EndpointListener(tcpListener, () => OnEndpointStopped(endpointEventArgs));
}

Required changes

src\SmtpServer\EndpointDefinitionBuilder.cs

_setters.Add(definition => definition.Endpoint = new IPEndPoint(IPAddress.IPv6Any, port));

src\SmtpServer\Net\EndpointListenerFactory.cs

tcpListener.Server.DualMode = true;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions