-
Notifications
You must be signed in to change notification settings - Fork 461
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
55 additions
and
10 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
39 changes: 39 additions & 0 deletions
39
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,39 @@ | ||
// 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; | ||
|
||
private const int MaxPacketSize = 10 * 1024; | ||
|
||
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($"Received discovery message 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