Files
go-i2p/lib/common/router_info.go

123 lines
3.1 KiB
Go
Raw Normal View History

package common
2016-02-07 02:54:02 -08:00
import (
"errors"
)
type RouterInfo []byte
2016-02-07 02:54:02 -08:00
//
// Read a RouterIdentity from the RouterInfo, returning the
// RouterIdentity and any parsing errors.
//
func (router_info RouterInfo) RouterIdentity() (router_identity RouterIdentity, err error) {
router_identity, _, err = ReadRouterIdentity(router_info)
return
}
2016-02-07 02:54:02 -08:00
//
// Return the Date the RouterInfo was published and any errors
// encountered parsing the RouterInfo.
//
func (router_info RouterInfo) Published() (date Date, err error) {
_, remainder, _ := ReadRouterIdentity(router_info)
2016-02-07 02:54:02 -08:00
if len(remainder) < 8 {
err = errors.New("error parsing date: not enough data")
2016-02-07 02:54:02 -08:00
return
}
copy(remainder[:8], date[:])
return
}
2016-02-07 02:54:02 -08:00
//
// Return the Integer representing the number of RouterAddresses
// are contained in this RouterInfo.
//
func (router_info RouterInfo) RouterAddressCount() (count int, err error) {
_, remainder, _ := ReadRouterIdentity(router_info)
2016-02-07 02:54:02 -08:00
if len(remainder) < 9 {
err = errors.New("error parsing router addresses: not enough data")
2016-02-07 02:54:02 -08:00
return
}
count = Integer([]byte{remainder[8]})
return
}
2016-02-07 02:54:02 -08:00
//
// Read the RouterAddresses inside this RouterInfo and return
// them in a slice.
//
func (router_info RouterInfo) RouterAddresses() (router_addresses []RouterAddress, err error) {
_, remainder, _ := ReadRouterIdentity(router_info)
if len(remainder) < 9 {
err = errors.New("error parsing router addresses: not enough data")
2016-02-07 02:54:02 -08:00
return
}
remaining := router_info[9:]
2016-02-07 02:54:02 -08:00
var router_address RouterAddress
addr_count, cerr := router_info.RouterAddressCount()
if cerr != nil {
err = cerr
return
}
for i := 0; i < addr_count; i++ {
router_address, remaining, err = ReadRouterAddress(remaining)
2016-02-01 08:06:24 -05:00
if err == nil {
2016-02-07 02:54:02 -08:00
router_addresses = append(router_addresses, router_address)
2016-02-01 08:06:24 -05:00
}
}
2016-02-07 02:54:02 -08:00
return
}
2016-02-07 02:54:02 -08:00
//
// Return the PeerSize value, currently unused and always zero.
//
func (router_info RouterInfo) PeerSize() int {
2016-02-07 02:54:02 -08:00
// Peer size is unused:
// https://geti2p.net/en/docs/spec/common-structures#struct_RouterAddress
return 0
}
2016-02-07 02:54:02 -08:00
//
// Return the Options Mapping inside this RouterInfo.
2016-02-07 02:54:02 -08:00
//
func (router_info RouterInfo) Options() Mapping {
head := router_info.optionsLocation()
size := head + router_info.optionsSize()
return Mapping(router_info[head:size])
}
2016-02-07 02:54:02 -08:00
//
// Return the 40 bytes that follow the Mapping in the RouterInfo.
2016-02-07 02:54:02 -08:00
//
2016-02-14 01:24:31 -08:00
func (router_info RouterInfo) Signature() Signature {
head := router_info.optionsLocation()
size := head + router_info.optionsSize()
2016-02-14 01:24:31 -08:00
return Signature(router_info[head+size : head+size+40])
}
2016-02-07 02:54:02 -08:00
//
// Used to determine where in the RouterInfo the Mapping
// data begins for parsing.
2016-02-07 02:54:02 -08:00
//
func (router_info RouterInfo) optionsLocation() int {
offset := 9
var router_address RouterAddress
remaining := router_info[9:]
2016-02-07 02:54:02 -08:00
addr_count, _ := router_info.RouterAddressCount()
for i := 0; i < addr_count; i++ {
router_address, remaining, _ = ReadRouterAddress(remaining)
offset += len(router_address)
}
return offset
}
2016-02-07 02:54:02 -08:00
//
// Used to determine the size of the options in the RouterInfo
// for parsing.
2016-02-07 02:54:02 -08:00
//
func (router_info RouterInfo) optionsSize() int {
head := router_info.optionsLocation()
return Integer(router_info[head:head+1]) + 1
}