aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet/util/address.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/gnunet/util/address.go')
-rw-r--r--src/gnunet/util/address.go56
1 files changed, 22 insertions, 34 deletions
diff --git a/src/gnunet/util/address.go b/src/gnunet/util/address.go
index 4cd07da..35e0030 100644
--- a/src/gnunet/util/address.go
+++ b/src/gnunet/util/address.go
@@ -27,10 +27,10 @@ import (
27 27
28// Address specifies how a peer is reachable on the network. 28// Address specifies how a peer is reachable on the network.
29type Address struct { 29type Address struct {
30 Netw string `` // network protocol 30 Netw string // network protocol
31 Options uint32 `order:"big"` // address options 31 Options uint32 // address options
32 Expires AbsoluteTime `` // expiration date for address 32 Expires AbsoluteTime // expiration date for address
33 Address []byte `size:"*"` // address data (protocol-dependent) 33 Address []byte // address data (protocol-dependent)
34} 34}
35 35
36// NewAddress returns a new Address for the given transport and specs 36// NewAddress returns a new Address for the given transport and specs
@@ -43,6 +43,8 @@ func NewAddress(transport string, addr string) *Address {
43 } 43 }
44} 44}
45 45
46// NewAddressWrap returns new address from net.Addr with no options
47// or expiry date.
46func NewAddressWrap(addr net.Addr) *Address { 48func NewAddressWrap(addr net.Addr) *Address {
47 return &Address{ 49 return &Address{
48 Netw: addr.Network(), 50 Netw: addr.Network(),
@@ -53,7 +55,7 @@ func NewAddressWrap(addr net.Addr) *Address {
53} 55}
54 56
55// ParseAddress translates a GNUnet address string like 57// ParseAddress translates a GNUnet address string like
56// "r5n+ip+udp://1.2.3.4:6789" or "gnunet+tcp://12.3.4.5/". 58// "ip+udp://1.2.3.4:6789" or "gnunet+tcp://12.3.4.5/".
57// It can also handle standard strings like "udp:127.0.0.1:6735". 59// It can also handle standard strings like "udp:127.0.0.1:6735".
58func ParseAddress(s string) (addr *Address, err error) { 60func ParseAddress(s string) (addr *Address, err error) {
59 p := strings.SplitN(s, ":", 2) 61 p := strings.SplitN(s, ":", 2)
@@ -72,11 +74,6 @@ func (a *Address) Equals(b *Address) bool {
72 bytes.Equal(a.Address, b.Address) 74 bytes.Equal(a.Address, b.Address)
73} 75}
74 76
75// StringAll returns a human-readable representation of an address.
76func (a *Address) StringAll() string {
77 return a.Netw + "://" + string(a.Address)
78}
79
80// implement net.Addr interface methods: 77// implement net.Addr interface methods:
81 78
82// String returns a human-readable representation of an address. 79// String returns a human-readable representation of an address.
@@ -91,7 +88,7 @@ func (a *Address) Network() string {
91 88
92//---------------------------------------------------------------------- 89//----------------------------------------------------------------------
93 90
94// URI returns a string representaion of an address. 91// URI returns a string representation of an address.
95func (a *Address) URI() string { 92func (a *Address) URI() string {
96 return URI(a.Netw, a.Address) 93 return URI(a.Netw, a.Address)
97} 94}
@@ -101,24 +98,6 @@ func URI(network string, addr []byte) string {
101 98
102//---------------------------------------------------------------------- 99//----------------------------------------------------------------------
103 100
104// IPAddress (can be IPv4 or IPv6 or a DNS name)
105type IPAddress struct {
106 Host []byte `size:"*-2"`
107 Port uint16 `order:"big"`
108}
109
110// NewIPAddress creates a new instance for a given host and port.
111func NewIPAddress(host []byte, port uint16) *IPAddress {
112 ip := &IPAddress{
113 Host: make([]byte, len(host)),
114 Port: port,
115 }
116 copy(ip.Host, host)
117 return ip
118}
119
120//----------------------------------------------------------------------
121
122// PeerAddrList is a list of addresses per peer ID. 101// PeerAddrList is a list of addresses per peer ID.
123type PeerAddrList struct { 102type PeerAddrList struct {
124 list *Map[string, []*Address] 103 list *Map[string, []*Address]
@@ -133,12 +112,13 @@ func NewPeerAddrList() *PeerAddrList {
133 112
134// Add address for peer. The returned mode is 0=not added, 1=new peer, 113// Add address for peer. The returned mode is 0=not added, 1=new peer,
135// 2=new address 114// 2=new address
136func (a *PeerAddrList) Add(id string, addr *Address) (mode int) { 115func (a *PeerAddrList) Add(peer *PeerID, addr *Address) (mode int) {
137 // check for expired address. 116 // check for expired address.
138 mode = 0 117 mode = 0
139 if !addr.Expires.Expired() { 118 if !addr.Expires.Expired() {
140 // run add operation 119 // run add operation
141 a.list.Process(func() error { 120 _ = a.list.Process(func() error {
121 id := peer.String()
142 list, ok := a.list.Get(id) 122 list, ok := a.list.Get(id)
143 if !ok { 123 if !ok {
144 list = make([]*Address, 0) 124 list = make([]*Address, 0)
@@ -160,7 +140,8 @@ func (a *PeerAddrList) Add(id string, addr *Address) (mode int) {
160} 140}
161 141
162// Get address for peer 142// Get address for peer
163func (a *PeerAddrList) Get(id string, transport string) (res []*Address) { 143func (a *PeerAddrList) Get(peer *PeerID, transport string) (res []*Address) {
144 id := peer.String()
164 list, ok := a.list.Get(id) 145 list, ok := a.list.Get(id)
165 if ok { 146 if ok {
166 for _, addr := range list { 147 for _, addr := range list {
@@ -181,6 +162,13 @@ func (a *PeerAddrList) Get(id string, transport string) (res []*Address) {
181} 162}
182 163
183// Delete a list entry by key. 164// Delete a list entry by key.
184func (a *PeerAddrList) Delete(id string) { 165func (a *PeerAddrList) Delete(peer *PeerID) {
185 a.list.Delete(id) 166 a.list.Delete(peer.String())
167}
168
169// Contains checks if a peer is contained in the list. Does not check
170// for expired entries.
171func (a *PeerAddrList) Contains(peer *PeerID) (ok bool) {
172 _, ok = a.list.Get(peer.String())
173 return
186} 174}