Skip to content

Commit

Permalink
Improve address detection
Browse files Browse the repository at this point in the history
  • Loading branch information
flcl42 committed Oct 17, 2023
1 parent 4efe0fc commit b710320
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
25 changes: 15 additions & 10 deletions src/libp2p/Libp2p.Protocols.IpTcp/IpTcpProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ public async Task ListenAsync(IChannel channel, IChannelFactory? channelFactory,
{
_logger?.LogInformation("ListenAsync({contextId})", context.Id);

Socket srv = new(SocketType.Stream, ProtocolType.Tcp);
Multiaddr addr = context.LocalPeer.Address;
MultiaddrEnum ipProtocol = addr.Has(MultiaddrEnum.Ip4) ? MultiaddrEnum.Ip4 : MultiaddrEnum.Ip6;
IPAddress ipAddress = IPAddress.Parse(addr.At(ipProtocol)!);
int tcpPort = int.Parse(addr.At(MultiaddrEnum.Tcp)!);

Socket srv = new(SocketType.Stream, ProtocolType.Tcp);
srv.Bind(new IPEndPoint(ipAddress, tcpPort));
srv.Listen(tcpPort);

_logger?.LogDebug("Ready to handle connections");
context.ListenerReady();


IPEndPoint localIpEndpoint = (IPEndPoint)srv.LocalEndPoint!;
channel.OnClose(() =>
{
Expand All @@ -45,12 +43,19 @@ public async Task ListenAsync(IChannel channel, IChannelFactory? channelFactory,
});

context.LocalEndpoint = Multiaddr.From(
ipProtocol, localIpEndpoint.Address.ToString(),
ipProtocol, ipProtocol == MultiaddrEnum.Ip4 ?
localIpEndpoint.Address.MapToIPv4().ToString() :
localIpEndpoint.Address.MapToIPv6().ToString(),
MultiaddrEnum.Tcp, localIpEndpoint.Port);

context.LocalPeer.Address = context.LocalPeer.Address
.Replace(ipProtocol, localIpEndpoint.Address.ToString())
.Replace(MultiaddrEnum.Tcp, localIpEndpoint.Port.ToString());
if (tcpPort == 0)
{
context.LocalPeer.Address = context.LocalPeer.Address
.Replace(MultiaddrEnum.Tcp, localIpEndpoint.Port.ToString());
}

_logger?.LogDebug("Ready to handle connections");
context.ListenerReady();

await Task.Run(async () =>
{
Expand Down Expand Up @@ -116,7 +121,7 @@ public async Task DialAsync(IChannel channel, IChannelFactory channelFactory, IP
TaskCompletionSource<bool?> waitForStop = new(TaskCreationOptions.RunContinuationsAsynchronously);
Socket client = new(SocketType.Stream, ProtocolType.Tcp);
Multiaddr addr = context.RemotePeer.Address;
Core.Enums.Multiaddr ipProtocol = addr.Has(MultiaddrEnum.Ip4) ? MultiaddrEnum.Ip4 : MultiaddrEnum.Ip6;
MultiaddrEnum ipProtocol = addr.Has(MultiaddrEnum.Ip4) ? MultiaddrEnum.Ip4 : MultiaddrEnum.Ip6;
IPAddress ipAddress = IPAddress.Parse(addr.At(ipProtocol)!);
int tcpPort = int.Parse(addr.At(MultiaddrEnum.Tcp)!);
try
Expand Down
18 changes: 10 additions & 8 deletions src/libp2p/Libp2p.Protocols.Quic/QuicProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ public async Task ListenAsync(IChannel channel, IChannelFactory? channelFactory,
ipProtocol, listener.LocalEndPoint.Address.ToString(),
MultiaddrEnum.Udp, listener.LocalEndPoint.Port);

context.LocalPeer.Address = context.LocalPeer.Address
.Replace(ipProtocol, listener.LocalEndPoint.Address.ToString())
.Replace(MultiaddrEnum.Udp, listener.LocalEndPoint.Port.ToString());
if (udpPort == 0)
{
context.LocalPeer.Address = context.LocalPeer.Address
.Replace(MultiaddrEnum.Udp, listener.LocalEndPoint.Port.ToString());
}

channel.OnClose(async () =>
{
Expand Down Expand Up @@ -171,11 +173,11 @@ private async Task ProcessStreams(QuicConnection connection, IPeerContext contex
connection.LocalEndPoint.Port);

context.LocalPeer.Address = context.LocalPeer.Address.Replace(
context.LocalEndpoint.Has(MultiaddrEnum.Ip4) ? MultiaddrEnum.Ip4 : MultiaddrEnum.Ip6, newIpProtocol,
connection.LocalEndPoint.Address.ToString())
.Replace(
MultiaddrEnum.Udp,
connection.LocalEndPoint.Port.ToString());
context.LocalEndpoint.Has(MultiaddrEnum.Ip4) ?
MultiaddrEnum.Ip4 :
MultiaddrEnum.Ip6,
newIpProtocol,
connection.LocalEndPoint.Address.ToString());

IPEndPoint remoteIpEndpoint = connection.RemoteEndPoint!;
newIpProtocol = remoteIpEndpoint.AddressFamily == AddressFamily.InterNetwork
Expand Down
4 changes: 2 additions & 2 deletions src/samples/chat/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
{
Multiaddr remoteAddr = args[1];

string addrTemplate = remoteAddr.Has(Nethermind.Libp2p.Core.Enums.Multiaddr.Quic) ?
string addrTemplate = remoteAddr.Has(Nethermind.Libp2p.Core.Enums.Multiaddr.QuicV1) ?
"/ip4/0.0.0.0/udp/0/quic-v1" :
"/ip4/0.0.0.0/tcp/0";

Expand All @@ -43,7 +43,7 @@
Identity optionalFixedIdentity = new(Enumerable.Repeat((byte)42, 32).ToArray());
ILocalPeer peer = peerFactory.Create(optionalFixedIdentity);

string addrTemplate = args.Contains("quic") ?
string addrTemplate = args.Contains("-quic") ?
"/ip4/0.0.0.0/udp/{0}/quic-v1/p2p/{1}" :
"/ip4/0.0.0.0/tcp/{0}/p2p/{1}";

Expand Down

0 comments on commit b710320

Please sign in to comment.