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

157 lines
4.3 KiB
Go
Raw Normal View History

package common
2016-02-15 00:34:29 -08:00
/*
I2P RouterAddress
2016-06-16 23:17:21 -07:00
https://geti2p.net/spec/common-structures#routeraddress
2016-02-15 00:34:29 -08:00
Accurate for version 0.9.24
+----+----+----+----+----+----+----+----+
|cost| expiration
+----+----+----+----+----+----+----+----+
| transport_style |
+----+----+----+----+-//-+----+----+----+
| |
+ +
| options |
~ ~
~ ~
| |
+----+----+----+----+----+----+----+----+
cost :: Integer
length -> 1 byte
case 0 -> free
case 255 -> expensive
expiration :: Date (must be all zeros, see notes below)
length -> 8 bytes
case null -> never expires
transport_style :: String
length -> 1-256 bytes
options :: Mapping
*/
2016-02-07 02:54:02 -08:00
import (
log "github.com/sirupsen/logrus"
2016-02-15 00:34:29 -08:00
)
// Minimum number of bytes in a valid RouterAddress
2016-02-15 00:34:29 -08:00
const (
ROUTER_ADDRESS_MIN_SIZE = 9
2016-02-07 02:54:02 -08:00
)
type RouterAddress struct {
cost *Integer
expiration *Date
transport_style *I2PString
options *Mapping
}
//[]byte
2016-02-07 02:54:02 -08:00
//
2016-02-15 00:34:29 -08:00
// Return the cost integer for this RouterAddress and any errors encountered
// parsing the RouterAddress.
2016-02-07 02:54:02 -08:00
//
func (router_address RouterAddress) Cost() int {
return router_address.cost.Int()
}
2016-02-07 02:54:02 -08:00
//
2016-02-15 00:34:29 -08:00
// Return the Date this RouterAddress expires and any errors encountered
// parsing the RouterAddress.
2016-02-07 02:54:02 -08:00
//
func (router_address RouterAddress) Expiration() Date {
return *router_address.expiration
}
2016-02-07 02:54:02 -08:00
//
2016-02-15 00:34:29 -08:00
// Return the Transport type for this RouterAddress and any errors encountered
// parsing the RouterAddress.
2016-02-07 02:54:02 -08:00
//
func (router_address RouterAddress) TransportStyle() I2PString {
return *router_address.transport_style
}
2016-02-07 02:54:02 -08:00
//
2016-02-15 00:34:29 -08:00
// Return the Mapping containing the options for this RouterAddress and any
// errors encountered parsing the RouterAddress.
2016-02-07 02:54:02 -08:00
//
func (router_address RouterAddress) Options() Mapping {
return *router_address.options
}
2016-02-07 02:54:02 -08:00
//
2016-02-15 00:34:29 -08:00
// Check if the RouterAddress is empty or if it is too small to contain valid data.
2016-02-07 02:54:02 -08:00
//
2016-02-12 00:21:27 -08:00
func (router_address RouterAddress) checkValid() (err error, exit bool) {
/*addr_len := len(router_address)
2016-02-07 02:54:02 -08:00
exit = false
2016-02-12 00:21:27 -08:00
if addr_len == 0 {
2016-02-15 00:34:29 -08:00
log.WithFields(log.Fields{
2016-06-16 23:04:03 -07:00
"at": "(RouterAddress) checkValid",
2016-02-15 00:34:29 -08:00
"reason": "no data",
}).Error("invalid router address")
2016-02-07 02:54:02 -08:00
err = errors.New("error parsing RouterAddress: no data")
exit = true
} else if addr_len < ROUTER_ADDRESS_MIN_SIZE {
2016-02-15 00:34:29 -08:00
log.WithFields(log.Fields{
2016-06-16 23:04:03 -07:00
"at": "(RouterAddress) checkValid",
"reason": "data too small (len < ROUTER_ADDRESS_MIN_SIZE)",
2016-02-15 00:34:29 -08:00
}).Warn("router address format warning")
2016-02-07 02:54:02 -08:00
err = errors.New("warning parsing RouterAddress: data too small")
}*/
2016-02-07 02:54:02 -08:00
return
}
2016-02-07 02:54:02 -08:00
//
2016-02-15 00:34:29 -08:00
// Given a slice of bytes, read a RouterAddress, returning the remaining bytes and any
// errors encountered parsing the RouterAddress.
2016-02-07 02:54:02 -08:00
//
2016-02-07 02:54:02 -08:00
func ReadRouterAddress(data []byte) (router_address RouterAddress, remainder []byte, err error) {
cost, remainder, err := NewInteger([]byte{data[0]})
router_address.cost = cost
2016-02-07 02:54:02 -08:00
if err != nil {
log.WithFields(log.Fields{
"at": "(RouterAddress) ReadNewRouterAddress",
"reason": "error parsing cost",
}).Warn("error parsing RouterAddress")
2016-02-07 02:54:02 -08:00
}
expiration, remainder, err := NewDate(remainder)
router_address.expiration = expiration
2016-02-12 00:21:27 -08:00
if err != nil {
log.WithFields(log.Fields{
"at": "(RouterAddress) ReadNewRouterAddress",
"reason": "error parsing expiration",
}).Error("error parsing RouterAddress")
2016-02-12 00:21:27 -08:00
}
transport_style, remainder, err := NewI2PString(remainder)
router_address.transport_style = transport_style
if err != nil {
log.WithFields(log.Fields{
"at": "(RouterAddress) ReadNewRouterAddress",
"reason": "error parsing transport_style",
}).Error("error parsing RouterAddress")
2016-02-12 00:21:27 -08:00
}
options, remainder, err := NewMapping(remainder)
router_address.options = options
if err != nil {
log.WithFields(log.Fields{
"at": "(RouterAddress) ReadNewRouterAddress",
"reason": "error parsing options",
}).Error("error parsing RouterAddress")
}
return
}
2017-04-16 21:02:47 -07:00
func NewRouterAddress(data []byte) (router_address *RouterAddress, remainder []byte, err error) {
objrouteraddress, remainder, err := ReadRouterAddress(data)
router_address = &objrouteraddress
2016-02-07 02:54:02 -08:00
return
}