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

lightningnetwork / lnd / 11170835610

03 Oct 2024 10:41PM UTC coverage: 49.188% (-9.6%) from 58.738%
11170835610

push

github

web-flow
Merge pull request #9154 from ziggie1984/master

multi: bump btcd version.

3 of 6 new or added lines in 6 files covered. (50.0%)

26110 existing lines in 428 files now uncovered.

97359 of 197934 relevant lines covered (49.19%)

1.04 hits per line

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

66.22
/channeldb/addr.go
1
package channeldb
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 {
2✔
34
        var (
2✔
35
                addrType byte
2✔
36
                ip       []byte
2✔
37
        )
2✔
38

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

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

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

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

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

65
        return nil
2✔
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 {
2✔
71
        var suffixIndex int
2✔
72
        hostLen := len(addr.OnionService)
2✔
73
        switch hostLen {
2✔
74
        case tor.V2Len:
2✔
75
                if _, err := w.Write([]byte{byte(v2OnionAddr)}); err != nil {
2✔
76
                        return err
×
77
                }
×
78
                suffixIndex = tor.V2Len - tor.OnionSuffixLen
2✔
79
        case tor.V3Len:
2✔
80
                if _, err := w.Write([]byte{byte(v3OnionAddr)}); err != nil {
2✔
81
                        return err
×
82
                }
×
83
                suffixIndex = tor.V3Len - tor.OnionSuffixLen
2✔
UNCOV
84
        default:
×
UNCOV
85
                return errors.New("unknown onion service length")
×
86
        }
87

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

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

100
        // Sanity check the decoded length.
101
        switch {
2✔
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 {
2✔
112
                return err
×
113
        }
×
114

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

121
        return nil
2✔
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) {
2✔
128
        var addrType [1]byte
2✔
129
        if _, err := r.Read(addrType[:]); err != nil {
2✔
130
                return nil, err
×
131
        }
×
132

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

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

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

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

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

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

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

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

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

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

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

207
        return address, nil
2✔
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 {
2✔
213
        switch addr := address.(type) {
2✔
214
        case *net.TCPAddr:
2✔
215
                return encodeTCPAddr(w, addr)
2✔
216
        case *tor.OnionAddr:
2✔
217
                return encodeOnionAddr(w, addr)
2✔
UNCOV
218
        default:
×
UNCOV
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