Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small fixes to parsing ports #228

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions multiaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func TestConstructFails(t *testing.T) {
"/ip4/127.0.0.1/p2p/tcp",
"/unix",
"/ip4/1.2.3.4/tcp/80/unix",
"/ip4/1.2.3.4/tcp/-1/unix",
0xTylerHolmes marked this conversation as resolved.
Show resolved Hide resolved
"/ip4/127.0.0.1/tcp/9090/http/p2p-webcrt-direct",
"/",
"",
Expand Down
35 changes: 16 additions & 19 deletions transcoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,18 @@ func ip4BtS(b []byte) (string, error) {
var TranscoderPort = NewTranscoderFromFunctions(portStB, portBtS, nil)

func portStB(s string) ([]byte, error) {
i, err := strconv.Atoi(s)
i, err := strconv.ParseUint(s, 10, 16)
if err != nil {
return nil, fmt.Errorf("failed to parse port addr: %s", err)
}
if i >= 65536 {
return nil, fmt.Errorf("failed to parse port addr: %s", "greater than 65536")
}
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(i))
return b, nil
}

func portBtS(b []byte) (string, error) {
i := binary.BigEndian.Uint16(b)
return strconv.Itoa(int(i)), nil
return strconv.FormatUint(uint64(i), 10), nil
}

var TranscoderOnion = NewTranscoderFromFunctions(onionStB, onionBtS, nil)
Expand All @@ -167,15 +164,12 @@ func onionStB(s string) ([]byte, error) {
}

// onion port number
i, err := strconv.Atoi(addr[1])
i, err := strconv.ParseUint(addr[1], 10, 16)
if err != nil {
return nil, fmt.Errorf("failed to parse onion addr: %s", err)
}
if i >= 65536 {
return nil, fmt.Errorf("failed to parse onion addr: %s", "port greater than 65536")
}
if i < 1 {
return nil, fmt.Errorf("failed to parse onion addr: %s", "port less than 1")
if i == 0 {
return nil, fmt.Errorf("failed to parse onion addr: %s", "non-zero port")
}

onionPortBytes := make([]byte, 2)
Expand All @@ -189,7 +183,10 @@ func onionStB(s string) ([]byte, error) {
func onionBtS(b []byte) (string, error) {
addr := strings.ToLower(base32.StdEncoding.EncodeToString(b[0:10]))
port := binary.BigEndian.Uint16(b[10:12])
return addr + ":" + strconv.Itoa(int(port)), nil
if port == 0 {
return "", fmt.Errorf("failed to parse onion addr: %s", "non-zero port")
}
return addr + ":" + strconv.FormatUint(uint64(port), 10), nil
}

var TranscoderOnion3 = NewTranscoderFromFunctions(onion3StB, onion3BtS, nil)
Expand All @@ -210,15 +207,12 @@ func onion3StB(s string) ([]byte, error) {
}

// onion port number
i, err := strconv.Atoi(addr[1])
i, err := strconv.ParseUint(addr[1], 10, 16)
if err != nil {
return nil, fmt.Errorf("failed to parse onion addr: %s", err)
}
if i >= 65536 {
return nil, fmt.Errorf("failed to parse onion addr: %s", "port greater than 65536")
}
if i < 1 {
return nil, fmt.Errorf("failed to parse onion addr: %s", "port less than 1")
if i == 0 {
return nil, fmt.Errorf("failed to parse onion addr: %s", "non-zero port")
}

onionPortBytes := make([]byte, 2)
Expand All @@ -232,7 +226,10 @@ func onion3StB(s string) ([]byte, error) {
func onion3BtS(b []byte) (string, error) {
addr := strings.ToLower(base32.StdEncoding.EncodeToString(b[0:35]))
port := binary.BigEndian.Uint16(b[35:37])
str := addr + ":" + strconv.Itoa(int(port))
if port < 1 {
return "", fmt.Errorf("failed to parse onion addr: %s", "port less than 1")
}
str := addr + ":" + strconv.FormatUint(uint64(port), 10)
return str, nil
}

Expand Down