• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 12199391122

06 Dec 2024 01:10PM UTC coverage: 49.807% (-9.1%) from 58.933%
12199391122

push

github

web-flow
Merge pull request #9337 from Guayaba221/patch-1

chore: fix typo in ruby.md

100137 of 201051 relevant lines covered (49.81%)

2.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.22
/graph/db/addr.go
1
package graphdb
2

3
import (
4
        "encoding/binary"
5
        "errors"
6
        "fmt"
7
        "io"
8
        "net"
9

10
        "github.com/lightningnetwork/lnd/tor"
11
)
12

13
// addressType specifies the network protocol and version that should be used
14
// when connecting to a node at a particular address.
15
type addressType uint8
16

17
const (
18
        // tcp4Addr denotes an IPv4 TCP address.
19
        tcp4Addr addressType = 0
20

21
        // tcp6Addr denotes an IPv6 TCP address.
22
        tcp6Addr addressType = 1
23

24
        // v2OnionAddr denotes a version 2 Tor onion service address.
25
        v2OnionAddr addressType = 2
26

27
        // v3OnionAddr denotes a version 3 Tor (prop224) onion service address.
28
        v3OnionAddr addressType = 3
29
)
30

31
// encodeTCPAddr serializes a TCP address into its compact raw bytes
32
// representation.
33
func encodeTCPAddr(w io.Writer, addr *net.TCPAddr) error {
4✔
34
        var (
4✔
35
                addrType byte
4✔
36
                ip       []byte
4✔
37
        )
4✔
38

4✔
39
        if addr.IP.To4() != nil {
8✔
40
                addrType = byte(tcp4Addr)
4✔
41
                ip = addr.IP.To4()
4✔
42
        } else {
8✔
43
                addrType = byte(tcp6Addr)
4✔
44
                ip = addr.IP.To16()
4✔
45
        }
4✔
46

47
        if ip == nil {
4✔
48
                return fmt.Errorf("unable to encode IP %v", addr.IP)
×
49
        }
×
50

51
        if _, err := w.Write([]byte{addrType}); err != nil {
4✔
52
                return err
×
53
        }
×
54

55
        if _, err := w.Write(ip); err != nil {
4✔
56
                return err
×
57
        }
×
58

59
        var port [2]byte
4✔
60
        byteOrder.PutUint16(port[:], uint16(addr.Port))
4✔
61
        if _, err := w.Write(port[:]); err != nil {
4✔
62
                return err
×
63
        }
×
64

65
        return nil
4✔
66
}
67

68
// encodeOnionAddr serializes an onion address into its compact raw bytes
69
// representation.
70
func encodeOnionAddr(w io.Writer, addr *tor.OnionAddr) error {
4✔
71
        var suffixIndex int
4✔
72
        hostLen := len(addr.OnionService)
4✔
73
        switch hostLen {
4✔
74
        case tor.V2Len:
4✔
75
                if _, err := w.Write([]byte{byte(v2OnionAddr)}); err != nil {
4✔
76
                        return err
×
77
                }
×
78
                suffixIndex = tor.V2Len - tor.OnionSuffixLen
4✔
79
        case tor.V3Len:
4✔
80
                if _, err := w.Write([]byte{byte(v3OnionAddr)}); err != nil {
4✔
81
                        return err
×
82
                }
×
83
                suffixIndex = tor.V3Len - tor.OnionSuffixLen
4✔
84
        default:
×
85
                return errors.New("unknown onion service length")
×
86
        }
87

88
        suffix := addr.OnionService[suffixIndex:]
4✔
89
        if suffix != tor.OnionSuffix {
4✔
90
                return fmt.Errorf("invalid suffix \"%v\"", suffix)
×
91
        }
×
92

93
        host, err := tor.Base32Encoding.DecodeString(
4✔
94
                addr.OnionService[:suffixIndex],
4✔
95
        )
4✔
96
        if err != nil {
4✔
97
                return err
×
98
        }
×
99

100
        // Sanity check the decoded length.
101
        switch {
4✔
102
        case hostLen == tor.V2Len && len(host) != tor.V2DecodedLen:
×
103
                return fmt.Errorf("onion service %v decoded to invalid host %x",
×
104
                        addr.OnionService, host)
×
105

106
        case hostLen == tor.V3Len && len(host) != tor.V3DecodedLen:
×
107
                return fmt.Errorf("onion service %v decoded to invalid host %x",
×
108
                        addr.OnionService, host)
×
109
        }
110

111
        if _, err := w.Write(host); err != nil {
4✔
112
                return err
×
113
        }
×
114

115
        var port [2]byte
4✔
116
        byteOrder.PutUint16(port[:], uint16(addr.Port))
4✔
117
        if _, err := w.Write(port[:]); err != nil {
4✔
118
                return err
×
119
        }
×
120

121
        return nil
4✔
122
}
123

124
// DeserializeAddr reads the serialized raw representation of an address and
125
// deserializes it into the actual address. This allows us to avoid address
126
// resolution within the channeldb package.
127
func DeserializeAddr(r io.Reader) (net.Addr, error) {
4✔
128
        var addrType [1]byte
4✔
129
        if _, err := r.Read(addrType[:]); err != nil {
4✔
130
                return nil, err
×
131
        }
×
132

133
        var address net.Addr
4✔
134
        switch addressType(addrType[0]) {
4✔
135
        case tcp4Addr:
4✔
136
                var ip [4]byte
4✔
137
                if _, err := r.Read(ip[:]); err != nil {
4✔
138
                        return nil, err
×
139
                }
×
140

141
                var port [2]byte
4✔
142
                if _, err := r.Read(port[:]); err != nil {
4✔
143
                        return nil, err
×
144
                }
×
145

146
                address = &net.TCPAddr{
4✔
147
                        IP:   net.IP(ip[:]),
4✔
148
                        Port: int(binary.BigEndian.Uint16(port[:])),
4✔
149
                }
4✔
150
        case tcp6Addr:
4✔
151
                var ip [16]byte
4✔
152
                if _, err := r.Read(ip[:]); err != nil {
4✔
153
                        return nil, err
×
154
                }
×
155

156
                var port [2]byte
4✔
157
                if _, err := r.Read(port[:]); err != nil {
4✔
158
                        return nil, err
×
159
                }
×
160

161
                address = &net.TCPAddr{
4✔
162
                        IP:   net.IP(ip[:]),
4✔
163
                        Port: int(binary.BigEndian.Uint16(port[:])),
4✔
164
                }
4✔
165
        case v2OnionAddr:
4✔
166
                var h [tor.V2DecodedLen]byte
4✔
167
                if _, err := r.Read(h[:]); err != nil {
4✔
168
                        return nil, err
×
169
                }
×
170

171
                var p [2]byte
4✔
172
                if _, err := r.Read(p[:]); err != nil {
4✔
173
                        return nil, err
×
174
                }
×
175

176
                onionService := tor.Base32Encoding.EncodeToString(h[:])
4✔
177
                onionService += tor.OnionSuffix
4✔
178
                port := int(binary.BigEndian.Uint16(p[:]))
4✔
179

4✔
180
                address = &tor.OnionAddr{
4✔
181
                        OnionService: onionService,
4✔
182
                        Port:         port,
4✔
183
                }
4✔
184
        case v3OnionAddr:
4✔
185
                var h [tor.V3DecodedLen]byte
4✔
186
                if _, err := r.Read(h[:]); err != nil {
4✔
187
                        return nil, err
×
188
                }
×
189

190
                var p [2]byte
4✔
191
                if _, err := r.Read(p[:]); err != nil {
4✔
192
                        return nil, err
×
193
                }
×
194

195
                onionService := tor.Base32Encoding.EncodeToString(h[:])
4✔
196
                onionService += tor.OnionSuffix
4✔
197
                port := int(binary.BigEndian.Uint16(p[:]))
4✔
198

4✔
199
                address = &tor.OnionAddr{
4✔
200
                        OnionService: onionService,
4✔
201
                        Port:         port,
4✔
202
                }
4✔
203
        default:
×
204
                return nil, ErrUnknownAddressType
×
205
        }
206

207
        return address, nil
4✔
208
}
209

210
// SerializeAddr serializes an address into its raw bytes representation so that
211
// it can be deserialized without requiring address resolution.
212
func SerializeAddr(w io.Writer, address net.Addr) error {
4✔
213
        switch addr := address.(type) {
4✔
214
        case *net.TCPAddr:
4✔
215
                return encodeTCPAddr(w, addr)
4✔
216
        case *tor.OnionAddr:
4✔
217
                return encodeOnionAddr(w, addr)
4✔
218
        default:
×
219
                return ErrUnknownAddressType
×
220
        }
221
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc