Skip to content

Conversation

lla-dane
Copy link
Contributor

@lla-dane lla-dane commented Sep 22, 2025

Tracks: multiformats/multiaddr#181
Adds the ipcidr protocol in py-multiaddr in reference with go-implementation

var TranscoderIPCIDR = NewTranscoderFromFunctions(ipcidrStB, ipcidrBtS, ipcidrValidate)

func ipcidrBtS(b []byte) (string, error) {
	if err := ipcidrValidate(b); err != nil {
		return "", err
	}
	return strconv.Itoa(int(b[0])), nil
}

func ipcidrStB(s string) ([]byte, error) {
	ipMask, err := strconv.ParseUint(s, 10, 8)
	if err != nil {
		return nil, err
	}
	return []byte{byte(uint8(ipMask))}, nil
}

func ipcidrValidate(b []byte) error {
	if len(b) != 1 {
		return fmt.Errorf("invalid length (should be == 1)")
	}
	return nil
}

@seetadev @acul71

- Added the codec utility functions in codecs/ipcidr.py
- Added the general codec internal tests in test_protocols.py
@lla-dane
Copy link
Contributor Author

Now will write the ipcidr + multiaddr tests as written in go implementation.

@seetadev
Copy link
Contributor

@lla-dane : Great, thanks Abhinav.

Lets try and getting key protocols in the upcoming multiaddr release. We plan for another release 4 weeks from now.

- Included the ipcidr addresses in the test-cases
- Now all the test cases are running for ipcidr addrs also
"/ip4/192.0.2.0/ipcidr/24",
"/ip6/::1",
"/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21",
"/ip6/2001:db8::/ipcidr/32",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seetadev @acul71: I included the ipcidr type addresses in existing test cases, so now all the tests are running the same ipcidr address, as go-multiaddr.

@lla-dane
Copy link
Contributor Author

@seetadev @acul71: I added the tests, I think this PR should be ready for merge.

@lla-dane lla-dane changed the title WIP: Add ipcidr protocol in reference with go-multiaddr Add ipcidr protocol in reference with go-multiaddr Oct 1, 2025
acul71 added 3 commits October 2, 2025 15:58
- Added /ipcidr/24 (missing IP address) to valid tests
- Added /ip4/1.2.3.0/ipcidr/128 (invalid IPv4 CIDR) to valid tests
- Added /ip6/::/ipcidr/255 (invalid IPv6 CIDR) to valid tests
- These cases are valid in multiaddr format (correct behavior)
- Added news fragment for ipcidr feature documentation
- All tests now pass (265 passed, 1 skipped)
- Removed newsfragments/95-feature.ipcidr.md
- Keep only the test case additions from Go implementation
- Removed /ipcidr/24, /ip4/1.2.3.0/ipcidr/128, /ip6/::/ipcidr/255 test cases
- These cases are valid multiaddr format but don't serve the same purpose as Go tests
- Go tests validate MultiaddrToIPNet failures, Python doesn't have equivalent function
- All tests still pass (262 passed, 1 skipped)
@acul71
Copy link
Contributor

acul71 commented Oct 2, 2025

Thanks, @lla-dane , just add a newfragment doc and I'll merge this PR

PR #95 Review: Add ipcidr protocol support to py-multiaddr

Summary

APPROVED WITH CHANGES - Implementation complete and working correctly.

Implementation Status

  • Protocol Registration: ✅ Complete (code 43, name "ipcidr", size 8 bits)
  • Codec Implementation: ✅ Complete (string↔bytes conversion, validation)
  • Test Coverage: ✅ Complete (8 ipcidr-specific tests passing)

Test Results

======================== 262 passed, 1 skipped in 0.29s =======================

Required Changes

Add news fragment file: newsfragments/95.feature..md

Final Status

✅ APPROVED WITH CHANGES - Add documentation file before merging.

@lla-dane
Copy link
Contributor Author

lla-dane commented Oct 2, 2025

@acul71: Added the newsfragment file

@seetadev
Copy link
Contributor

seetadev commented Oct 2, 2025

@lla-dane : Great, thank you so much Abhinav. Appreciate your efforts.

Me and @acul71 will spend sometime learning the py-multiaddr release process sooon.

We wish to test this addition in some of the py-libp2p examples or modules (locally) before we do the final review + merge.

@acul71
Copy link
Contributor

acul71 commented Oct 4, 2025

We wish to test this addition in some of the py-libp2p examples or modules (locally) before we do the final review + merge.

@seetadev I think we should merge this now rather than waiting for tests. Since the ipcidr protocol will be needed soon in py-libp2p, it makes more sense to get it merged first so we can use it immediately for testing and development.

This approach would allow us to:

  • Start integrating ipcidr support into py-libp2p right away
  • Identify any issues during actual usage rather than in artificial test cases
  • Move faster on the py-libp2p integration work

The protocol addition looks solid based on the Go implementation reference, and having it available will make the py-libp2p testing much more effective. We can always iterate on any issues that come up during integration.

What do you think about merging this first and then doing the testing in the context of py-libp2p?

Use Cases for ipcidr Protocol in py-libp2p

1. Eclipse Attack Prevention

Allow trusted peers to connect even when under attack by specifying trusted IP ranges.

2. Network Access Control

Restrict connections to specific IP ranges for security purposes.

3. Resource Management

Set connection limits per network prefix to prevent resource exhaustion.

4. Peer Authentication

Combine IP ranges with specific peer IDs for enhanced security.

Benefits of Early Integration

  • Immediate Availability: Protocol ready for py-libp2p development
  • Real-world Testing: Test in actual libp2p scenarios
  • Faster Development: Parallel work on integration and testing
  • Issue Discovery: Identify problems during actual usage
  • Iterative Improvement: Refine based on real usage patterns

Related Discussion

For more details on ipcidr protocol usage across libp2p implementations, see: ipcidr Protocol Usage in libp2p Discussion #976

@seetadev
Copy link
Contributor

seetadev commented Oct 4, 2025

@acul71 : +1. We will merge it and try the addition in multiple PRs at this juncture. Appreciate your feedback and pointers.

@lla-dane : Thank you Abhinav. Will discuss with you the remaining protocols and their additions this week. We wish to reach parity with go-libp2p in this month (if possible).

@seetadev seetadev merged commit 2fd14c7 into multiformats:master Oct 4, 2025
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants