Skip to content

Commit

Permalink
netip.Addr{,Port}
Browse files Browse the repository at this point in the history
  • Loading branch information
nikandfor committed Jul 6, 2024
1 parent bd22994 commit 0a578eb
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
28 changes: 28 additions & 0 deletions tlwire/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tlwire

import (
"math"
"net/netip"
"time"
)

Expand Down Expand Up @@ -115,6 +116,33 @@ func (d *Decoder) Duration(p []byte, st int) (dr time.Duration, i int) {
return time.Duration(sub), i
}

func (d *Decoder) Addr(p []byte, st int) (a netip.Addr, ap netip.AddrPort, i int, err error) {
if p[st] != Semantic|NetAddr {
panic("not an address")
}

tag, sub, i := d.Tag(p, st+1)
if tag == Special && sub == Nil {
return
}
if tag != String {
panic("unsupported address encoding")
}

ab := p[i : i+int(sub)]
i += int(sub)

err = a.UnmarshalText(ab)
if err != nil {
err = ap.UnmarshalText(ab)
}
if err != nil {
return a, ap, st, err
}

return
}

func (d LowDecoder) Skip(b []byte, st int) (i int) {
_, _, i = d.SkipTag(b, st)
return
Expand Down
44 changes: 44 additions & 0 deletions tlwire/encdec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package tlwire

import (
"net/netip"
"testing"

"github.com/stretchr/testify/assert"
)

func TestAddr(tb *testing.T) {
var e Encoder
var d Decoder
var b []byte

for _, a := range []string{"1.1.1.1", "ff22::ff11"} {
x := netip.MustParseAddr(a)

b = e.AppendAddr(b[:0], x)

y, z, j, err := d.Addr(b, 0)
assert.NoError(tb, err)
assert.Equal(tb, len(b), j)
assert.Equal(tb, x, y)
assert.False(tb, z.IsValid())
}

for _, a := range []string{"1.1.1.1:8080", "[ff22::ff11]:1234"} {
x := netip.MustParseAddrPort(a)

b = e.AppendAddrPort(b[:0], x)

y, z, j, err := d.Addr(b, 0)
assert.NoError(tb, err)
assert.Equal(tb, len(b), j)
assert.Equal(tb, x, z)
assert.False(tb, y.IsValid())
}

b[0] = Semantic | NetAddr
b = e.AppendString(b[:1], "qweqwe")

_, _, _, err := d.Addr(b, 0)
assert.Error(tb, err)
}
31 changes: 31 additions & 0 deletions tlwire/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tlwire

import (
"math"
"net/netip"
"time"

"github.com/nikandfor/hacked/hfmt"
Expand Down Expand Up @@ -131,6 +132,36 @@ func (e *Encoder) AppendDuration(b []byte, d time.Duration) []byte {
return e.AppendInt64(b, d.Nanoseconds())
}

func (e *Encoder) AppendAddr(b []byte, a netip.Addr) []byte {
b = append(b, Semantic|NetAddr)

if !a.IsValid() {
return append(b, Special|Nil)
}

b = e.AppendTag(b, String, 0)
st := len(b)
b = a.AppendTo(b)
b = e.InsertLen(b, st, len(b)-st)

return b
}

func (e *Encoder) AppendAddrPort(b []byte, a netip.AddrPort) []byte {
b = append(b, Semantic|NetAddr)

if !a.IsValid() {
return append(b, Special|Nil)
}

b = e.AppendTag(b, String, 0)
st := len(b)
b = a.AppendTo(b)
b = e.InsertLen(b, st, len(b)-st)

return b
}

func (e *Encoder) AppendFormat(b []byte, fmt string, args ...interface{}) []byte {
b = append(b, String)
st := len(b)
Expand Down
14 changes: 14 additions & 0 deletions tlwire/encoder_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tlwire

import (
"fmt"
"net/netip"
"net/url"
"reflect"
"time"
Expand Down Expand Up @@ -87,6 +88,19 @@ func init() {
return e.AppendDuration(b, *x.(*time.Duration))
})

SetEncoder(netip.Addr{}, func(e *Encoder, b []byte, x interface{}) []byte {
return e.AppendAddr(b, x.(netip.Addr))
})
SetEncoder((*netip.Addr)(nil), func(e *Encoder, b []byte, x interface{}) []byte {
return e.AppendAddr(b, *x.(*netip.Addr))
})
SetEncoder(netip.AddrPort{}, func(e *Encoder, b []byte, x interface{}) []byte {
return e.AppendAddrPort(b, x.(netip.AddrPort))
})
SetEncoder((*netip.AddrPort)(nil), func(e *Encoder, b []byte, x interface{}) []byte {
return e.AppendAddrPort(b, *x.(*netip.AddrPort))
})

SetEncoder((*url.URL)(nil), func(e *Encoder, b []byte, x interface{}) []byte {
u := x.(*url.URL)
if u == nil {
Expand Down
2 changes: 1 addition & 1 deletion tlwire/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const (
Big

Caller
_
NetAddr
Hex
_
Embedding
Expand Down

0 comments on commit 0a578eb

Please sign in to comment.