-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
NettyDiscoveryBaseHandler
and packet size validation
- Loading branch information
Showing
5 changed files
with
58 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/Nethermind/Nethermind.Network.Discovery/NettyDiscoveryBaseHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// SPDX-FileCopyrightText: 2024 Demerzel Solutions Limited | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
using DotNetty.Transport.Channels; | ||
using DotNetty.Transport.Channels.Sockets; | ||
using Nethermind.Logging; | ||
|
||
namespace Nethermind.Network.Discovery; | ||
|
||
public abstract class NettyDiscoveryBaseHandler : SimpleChannelInboundHandler<DatagramPacket> | ||
{ | ||
private readonly ILogger _logger; | ||
|
||
// https://github.com/ethereum/devp2p/blob/master/discv4.md#wire-protocol | ||
// https://github.com/ethereum/devp2p/blob/master/discv5/discv5-wire.md#udp-communication | ||
protected const int MaxPacketSize = 1280; | ||
|
||
protected NettyDiscoveryBaseHandler(ILogManager? logManager) | ||
{ | ||
_logger = logManager?.GetClassLogger<NettyDiscoveryBaseHandler>() ?? throw new ArgumentNullException(nameof(logManager)); | ||
} | ||
|
||
public override void ChannelRead(IChannelHandlerContext ctx, object msg) | ||
{ | ||
if (msg is DatagramPacket packet && AcceptInboundMessage(packet) && !ValidatePacket(packet)) | ||
return; | ||
|
||
base.ChannelRead(ctx, msg); | ||
} | ||
|
||
protected bool ValidatePacket(DatagramPacket packet) | ||
{ | ||
if (packet.Content.ReadableBytes is 0 or > MaxPacketSize) | ||
{ | ||
if (_logger.IsWarn) _logger.Warn($"Skipping discovery packet of invalid size: {packet.Content.ReadableBytes}"); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters