-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(x/meg): Support capturing components (#269)
* Use Matchable interface * Add Bytes to Matchable interface * feat(x/meg): Support capturing bytes * Export CaptureWithF Can be used by more specific capturers (e.g capture net.AddrIP) * Support Any match, RawValue, and multiple Concatenations * Add CaptureAddrPort
- Loading branch information
Showing
6 changed files
with
282 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package multiaddr | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
"net/netip" | ||
|
||
"github.com/multiformats/go-multiaddr/x/meg" | ||
) | ||
|
||
func CaptureAddrPort(network *string, ipPort *netip.AddrPort) (capturePattern meg.Pattern) { | ||
var ipOnly netip.Addr | ||
capturePort := func(s meg.Matchable) error { | ||
switch s.Code() { | ||
case P_UDP: | ||
*network = "udp" | ||
case P_TCP: | ||
*network = "tcp" | ||
default: | ||
return fmt.Errorf("invalid network: %s", s.Value()) | ||
} | ||
|
||
port := binary.BigEndian.Uint16(s.RawValue()) | ||
*ipPort = netip.AddrPortFrom(ipOnly, port) | ||
return nil | ||
} | ||
|
||
pattern := meg.Cat( | ||
meg.Or( | ||
meg.CaptureWithF(P_IP4, func(s meg.Matchable) error { | ||
var ok bool | ||
ipOnly, ok = netip.AddrFromSlice(s.RawValue()) | ||
if !ok { | ||
return fmt.Errorf("invalid ip4 address: %s", s.Value()) | ||
} | ||
return nil | ||
}), | ||
meg.CaptureWithF(P_IP6, func(s meg.Matchable) error { | ||
var ok bool | ||
ipOnly, ok = netip.AddrFromSlice(s.RawValue()) | ||
if !ok { | ||
return fmt.Errorf("invalid ip6 address: %s", s.Value()) | ||
} | ||
return nil | ||
}), | ||
), | ||
meg.Or( | ||
meg.CaptureWithF(P_UDP, capturePort), | ||
meg.CaptureWithF(P_TCP, capturePort), | ||
), | ||
) | ||
|
||
return pattern | ||
} |
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
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
Oops, something went wrong.