add csharp bindings with pinvoke and nuget packaging support#31
Merged
MuriloChianfa merged 2 commits intomainfrom Jan 31, 2026
Merged
add csharp bindings with pinvoke and nuget packaging support#31MuriloChianfa merged 2 commits intomainfrom
MuriloChianfa merged 2 commits intomainfrom
Conversation
Resolved conflicts in: - .github/workflows/ci.yml: Added C#, Lua, Perl, PHP, and Python bindings tests - CMakeLists.txt: Combined C#, Lua, Perl, PHP, and Python wrapper options - docker/README.md: Documented all language binding containers - scripts/docker-build.sh: Added support for all binding images This merge brings in the Lua (#29), Perl (#28), Python (#27), and PHP (#26) bindings alongside the existing C# bindings work.
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
MuriloChianfa
added a commit
that referenced
this pull request
Jan 31, 2026
Resolved conflicts in: - .github/workflows/ci.yml: Added Java, C#, Lua, Perl, PHP, and Python bindings tests - CMakeLists.txt: Combined Java, C#, Lua, Perl, PHP, and Python wrapper options - docker/README.md: Documented all language binding containers - scripts/docker-build.sh: Added support for all binding images This merge brings in the C# (#31), Lua (#29), Perl (#28), Python (#27), and PHP (#26) bindings alongside the existing Java bindings work.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR adds comprehensive C# bindings for the liblpm C library using P/Invoke, providing high-performance longest prefix match (LPM) routing table operations for .NET applications. The bindings deliver near-native C performance with modern .NET idioms, zero-copy operations via
Span<T>, type safety, and automatic resource management throughIDisposableandSafeHandlepatterns.Type of Change
Related Issues
Closes #
Motivation and Context
C# and .NET are widely used in enterprise applications, microservices, cloud-native applications, gaming servers, and high-performance services where efficient IP routing lookups are essential. While .NET has extensive networking capabilities, high-performance LPM implementations with modern APIs are rare. This implementation provides:
Span<T>andReadOnlySpan<T>support for batch operationsLpmTrieIPv4andLpmTrieIPv6classesIDisposablepattern withSafeHandlefor automatic cleanupThis enables .NET developers to leverage liblpm's high-performance routing capabilities in ASP.NET Core applications, Azure Functions, gaming servers (Unity, Godot), microservices, and enterprise network tools.
Changes Made
Core Implementation
C# API (
LibLpm/): 8 classes, ~2,800 linesLpmTrie.cs: Abstract base class with common functionalityLpmTrieIPv4.cs(329 lines): IPv4-specific operations with optimized uint APILpmTrieIPv6.cs: IPv6-specific operations with 16-byte handlingNativeMethods.cs(457 lines): Complete P/Invoke declarations for all native functionsSafeLpmHandle.cs:SafeHandleimplementation for automatic memory cleanupLpmAlgorithm.cs: Type-safe enum for algorithm selectionLpmException: Base exception (abstract)LpmCreationException: Table creation failuresLpmInvalidPrefixException: Invalid address or prefix formatLpmOperationException: Add/delete operation failuresNativeLibraryLoader.cs: Cross-platform native library discovery and loadingP/Invoke Bridge Features:
API Features:
CreateDefault(),CreateDir24(),CreateStride8(),CreateWide16()LookupRaw()for zero-overhead hot pathsIDisposable,SafeHandle, automatic cleanupBuild System
Project Files (
.csproj): 100 linesSolution Structure (
LibLpm.sln):LibLpm: Main library projectLibLpm.Tests: xUnit test projectLibLpm.Examples: Example applicationsCMakeLists.txt: CMake integration for building native library
Dockerfile: Docker build support for easy testing
Testing & Quality
Test Suite (
LibLpm.Tests/): 4 test classes, comprehensive coverageIPv4Tests.cs: IPv4 operations, edge cases, longest prefix matchIPv6Tests.cs: IPv6 operations, various address formatsBatchTests.cs: Batch operation correctness and performanceResourceTests.cs: Disposal, SafeHandle, memory managementExamples (
LibLpm.Examples/): 2 comprehensive examplesBasicExample.cs(211 lines): IPv4/IPv6 operations, all API stylesBatchExample.cs: High-performance batch processing with SpanDocumentation
README.md (468 lines):
XML Documentation: Comprehensive inline documentation
Testing
Test Environment
Tests Performed
dotnet test)dotnet run --project LibLpm.Examples)Test Output
Performance Impact
Performance Characteristics
P/Invoke Overhead: P/Invoke adds approximately 10-25ns per call. To minimize overhead:
Use Span for Zero-Copy:
Use uint API for IPv4 (fastest):
Batch Operations with Span:
Avoid String Parsing in Hot Paths:
Benchmark Results
Key Takeaway:
LookupRaw(uint)for absolute maximum performanceSpan<T>for zero-allocation operationsDocumentation
XML Documentation Highlights:
Code Quality
C# Code Quality:
[MethodImpl(MethodImplOptions.AggressiveInlining)])P/Invoke Quality:
Breaking Changes
This is a new feature addition with no impact on existing C library or other language bindings.
Additional Notes
Key Features
Span<T>andReadOnlySpan<T>for stack allocationsPackage Structure
Files Changed
NuGet Package
Ready for NuGet.org publication:
Includes:
runtimes/linux-x64/native/liblpm.soruntimes/linux-arm64/native/liblpm.soruntimes/win-x64/native/lpm.dllruntimes/osx-x64/native/liblpm.dylibruntimes/osx-arm64/native/liblpm.dylibPlatform Support
.NET Versions:
Use Cases
Thread Safety
Lookup,LookupBatch): Thread-safe with proper lockingAdd,Delete): NOT thread-safeExample with
ReaderWriterLockSlim(provided in README):Checklist
Reviewer Notes
Areas for Review
P/Invoke Safety: Memory management and marshalling
API Design: Modern .NET best practices
Performance Optimizations: Minimize overhead
Build System: NuGet packaging
Documentation: XML docs completeness
Testing: Coverage and patterns
Testing Instructions
Development Workflow
Performance Testing
Known Limitations
Future Enhancements (Out of Scope)
NuGet Publication
Once approved, the package can be published to NuGet.org:
After release, users can install:
Or in project file: