github.com/annchain/OG@v0.0.9/p2p/enr/entries.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package enr
    18  
    19  //go:generate msgp
    20  
    21  import (
    22  	"fmt"
    23  	"github.com/annchain/OG/types/msg"
    24  	"github.com/tinylib/msgp/msgp"
    25  	"net"
    26  )
    27  
    28  // Entry is implemented by known node record entry types.
    29  //
    30  // To define a new entry that is to be included in a node record,
    31  // create a Go type that satisfies this interface. The type should
    32  // also implement rlp.Decoder if additional checks are needed on the value.
    33  type Entry interface {
    34  	ENRKey() string
    35  	msg.MsgpMember
    36  }
    37  
    38  type generic struct {
    39  	key string
    40  	//value interface{}
    41  	value msg.MsgpMember
    42  }
    43  
    44  func (g generic) ENRKey() string { return g.key }
    45  
    46  func (g generic) MarshalMsg(b []byte) ([]byte, error) {
    47  	return g.value.MarshalMsg(b)
    48  }
    49  
    50  func (g *generic) UnmarshalMsg(b []byte) ([]byte, error) {
    51  	return g.value.UnmarshalMsg(b)
    52  }
    53  
    54  func (g *generic) Msgsize() int {
    55  	return g.value.Msgsize()
    56  }
    57  
    58  func (g *generic) DecodeMsg(en *msgp.Reader) (err error) {
    59  	return g.value.DecodeMsg(en)
    60  }
    61  
    62  func (g generic) EncodeMsg(en *msgp.Writer) (err error) {
    63  	return g.value.EncodeMsg(en)
    64  }
    65  
    66  // WithEntry wraps any value with a key name. It can be used to set and load arbitrary values
    67  // in a record. The value v must be supported by rlp. To use WithEntry with Load, the value
    68  // must be a pointer.
    69  func WithEntry(k string, v msg.MsgpMember) Entry {
    70  	return &generic{key: k, value: v}
    71  }
    72  
    73  // TCP is the "tcp" key, which holds the TCP port of the node.
    74  type TCP uint16
    75  
    76  func (v TCP) ENRKey() string { return "tcp" }
    77  
    78  // UDP is the "udp" key, which holds the UDP port of the node.
    79  type UDP uint16
    80  
    81  func (v UDP) ENRKey() string { return "udp" }
    82  
    83  // ID is the "id" key, which holds the name of the identity scheme.
    84  type ID string
    85  
    86  const IDv4 = ID("v4") // the default identity scheme
    87  
    88  func (v ID) ENRKey() string { return "id" }
    89  
    90  // IP is the "ip" key, which holds the IP address of the node.
    91  type IP net.IP
    92  
    93  func (v IP) ENRKey() string { return "ip" }
    94  
    95  // EncodeRLP implements rlp.Encoder.
    96  func (v IP) MarshalMsg(b []byte) ([]byte, error) {
    97  	if ip4 := net.IP(v).To4(); ip4 != nil {
    98  		bs := msg.Bytes(ip4)
    99  		return bs.MarshalMsg(b)
   100  	}
   101  	bs := msg.Bytes(v)
   102  	return bs.MarshalMsg(b)
   103  }
   104  
   105  func (v IP) Msgsize() int {
   106  	bs := msg.Bytes(v)
   107  	return bs.Msgsize()
   108  }
   109  
   110  // DecodeRLP implements rlp.Decoder.
   111  func (v *IP) UnmarshalMsg(b []byte) ([]byte, error) {
   112  	var bs msg.Bytes
   113  	d, err := bs.UnmarshalMsg(b)
   114  	if err != nil {
   115  		return d, err
   116  	}
   117  	if len(bs) != 4 && len(bs) != 16 {
   118  		return d, fmt.Errorf("invalid IP address, want 4 or 16 bytes: %v", *v)
   119  	}
   120  	*v = IP(bs)
   121  	return d, nil
   122  }
   123  
   124  func (v *IP) DecodeMsg(en *msgp.Reader) error {
   125  	var bs msg.Bytes
   126  	err := bs.DecodeMsg(en)
   127  	if err != nil {
   128  		return err
   129  	}
   130  	if len(bs) != 4 && len(bs) != 16 {
   131  		return fmt.Errorf("invalid IP address, want 4 or 16 bytes: %v", *v)
   132  	}
   133  	*v = IP(bs)
   134  	return nil
   135  }
   136  
   137  func (v IP) EncodeMsg(en *msgp.Writer) (err error) {
   138  	if ip4 := net.IP(v).To4(); ip4 != nil {
   139  		bs := msg.Bytes(ip4)
   140  		return bs.EncodeMsg(en)
   141  	}
   142  	bs := msg.Bytes(v)
   143  	return bs.EncodeMsg(en)
   144  }