1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
package multiaddr
import (
"cmp"
"encoding/json"
"errors"
"fmt"
"log"
"strings"
"golang.org/x/exp/slices"
)
var errNilPtr = errors.New("nil ptr")
// Multiaddr is the data structure representing a Multiaddr
type Multiaddr []Component
func (m Multiaddr) copy() Multiaddr {
if m == nil {
return nil
}
out := make(Multiaddr, len(m))
copy(out, m)
return out
}
// NewMultiaddr parses and validates an input string, returning a *Multiaddr
func NewMultiaddr(s string) (a Multiaddr, err error) {
defer func() {
if e := recover(); e != nil {
log.Printf("Panic in NewMultiaddr on input %q: %s", s, e)
err = fmt.Errorf("%v", e)
}
}()
b, err := stringToBytes(s)
if err != nil {
return nil, err
}
return NewMultiaddrBytes(b)
}
// NewMultiaddrBytes initializes a Multiaddr from a byte representation.
// It validates it as an input string.
func NewMultiaddrBytes(b []byte) (a Multiaddr, err error) {
defer func() {
if e := recover(); e != nil {
log.Printf("Panic in NewMultiaddrBytes on input %q: %s", b, e)
err = fmt.Errorf("%v", e)
}
}()
bytesRead, m, err := readMultiaddr(b)
if err != nil {
return nil, err
}
if bytesRead != len(b) {
return nil, fmt.Errorf("unexpected extra data. %v bytes leftover", len(b)-bytesRead)
}
if len(m) == 0 {
return nil, nil
}
return m, nil
}
// Equal tests whether two multiaddrs are equal
func (m Multiaddr) Equal(m2 Multiaddr) bool {
if len(m) != len(m2) {
return false
}
for i, c := range m {
if !c.Equal(&m2[i]) {
return false
}
}
return true
}
func (m Multiaddr) Compare(o Multiaddr) int {
for i := 0; i < len(m) && i < len(o); i++ {
if cmp := m[i].Compare(&o[i]); cmp != 0 {
return cmp
}
}
return cmp.Compare(len(m), len(o))
}
// Bytes returns the []byte representation of this Multiaddr
func (m Multiaddr) Bytes() []byte {
size := 0
for _, c := range m {
size += len(c.bytes)
}
// This method is inlined in the caller. Using a fixed sized array
// avoids an allocation.
var out []byte
if size < 128 {
var o [128]byte
out = o[:0]
} else {
out = make([]byte, 0, size)
}
for _, c := range m {
out = append(out, c.bytes...)
}
return out
}
// String returns the string representation of a Multiaddr
func (m Multiaddr) String() string {
var buf strings.Builder
for _, c := range m {
c.writeTo(&buf)
}
return buf.String()
}
func (m Multiaddr) MarshalBinary() ([]byte, error) {
return m.Bytes(), nil
}
func (m *Multiaddr) UnmarshalBinary(data []byte) error {
if m == nil {
return errNilPtr
}
new, err := NewMultiaddrBytes(data)
if err != nil {
return err
}
*m = new
return nil
}
func (m Multiaddr) MarshalText() ([]byte, error) {
return []byte(m.String()), nil
}
func (m *Multiaddr) UnmarshalText(data []byte) error {
if m == nil {
return errNilPtr
}
new, err := NewMultiaddr(string(data))
if err != nil {
return err
}
*m = new
return nil
}
func (m Multiaddr) MarshalJSON() ([]byte, error) {
return json.Marshal(m.String())
}
func (m *Multiaddr) UnmarshalJSON(data []byte) error {
if m == nil {
return errNilPtr
}
var v string
if err := json.Unmarshal(data, &v); err != nil {
return err
}
new, err := NewMultiaddr(v)
*m = new
return err
}
// Protocols returns the list of protocols this Multiaddr has.
// will panic in case we access bytes incorrectly.
func (m Multiaddr) Protocols() []Protocol {
out := make([]Protocol, 0, len(m))
for _, c := range m {
out = append(out, c.Protocol())
}
return out
}
type Multiaddrer interface {
// Multiaddr returns the Multiaddr representation
Multiaddr() Multiaddr
}
func (m Multiaddr) Multiaddr() Multiaddr {
return m
}
// AppendComponent is the same as using `append(m, *c)`, but with a safety check
// for a nil Component.
func (m Multiaddr) AppendComponent(cs ...*Component) Multiaddr {
for _, c := range cs {
if c == nil {
continue
}
m = append(m, *c)
}
return m
}
// Encapsulate wraps a given Multiaddr, returning the resulting joined Multiaddr
func (m Multiaddr) Encapsulate(other Multiaddrer) Multiaddr {
return Join(m, other)
}
// Decapsulate unwraps Multiaddr up until the given Multiaddr is found.
func (m Multiaddr) Decapsulate(rightPartsAny Multiaddrer) Multiaddr {
if rightPartsAny == nil {
return m
}
rightParts := rightPartsAny.Multiaddr()
leftParts := m
lastIndex := -1
for i := range leftParts {
foundMatch := false
for j, rightC := range rightParts {
if len(leftParts) <= i+j {
foundMatch = false
break
}
foundMatch = rightC.Equal(&leftParts[i+j])
if !foundMatch {
break
}
}
if foundMatch {
lastIndex = i
}
}
if lastIndex == 0 {
return nil
}
if lastIndex < 0 {
return m
}
return leftParts[:lastIndex]
}
var ErrProtocolNotFound = fmt.Errorf("protocol not found in multiaddr")
func (m Multiaddr) ValueForProtocol(code int) (value string, err error) {
for _, c := range m {
if c.Protocol().Code == code {
return c.Value(), nil
}
}
return "", ErrProtocolNotFound
}
// FilterAddrs is a filter that removes certain addresses, according to the given filters.
// If all filters return true, the address is kept.
func FilterAddrs(a []Multiaddr, filters ...func(Multiaddr) bool) []Multiaddr {
b := make([]Multiaddr, 0, len(a))
addrloop:
for _, addr := range a {
for _, filter := range filters {
if !filter(addr) {
continue addrloop
}
}
b = append(b, addr)
}
return b
}
// Contains reports whether addr is contained in addrs.
func Contains(addrs []Multiaddr, addr Multiaddr) bool {
for _, a := range addrs {
if addr.Equal(a) {
return true
}
}
return false
}
// Unique deduplicates addresses in place, leave only unique addresses.
// It doesn't allocate.
func Unique(addrs []Multiaddr) []Multiaddr {
if len(addrs) == 0 {
return addrs
}
// Use the new slices package here, as the sort function doesn't allocate (sort.Slice does).
slices.SortFunc(addrs, func(a, b Multiaddr) int { return a.Compare(b) })
idx := 1
for i := 1; i < len(addrs); i++ {
if !addrs[i-1].Equal(addrs[i]) {
addrs[idx] = addrs[i]
idx++
}
}
for i := idx; i < len(addrs); i++ {
addrs[i] = nil
}
return addrs[:idx]
}
|