constants

This commit is contained in:
Hayden Parker
2016-12-29 15:46:25 +00:00
parent c501cb0c65
commit 22356f8a3a
2 changed files with 30 additions and 18 deletions

View File

@ -135,6 +135,16 @@ const (
FOLLOW_ON_FRAGMENT FOLLOW_ON_FRAGMENT
) )
const (
FLAG_SIZE = 1
TUNNEL_ID_SIZE = 4
HASH_SIZE = 32
DELAY_SIZE = 1
MESSAGE_ID_SIZE = 4
EXTENDED_OPTIONS_MIN_SIZE = 2
SIZE_FIELD_SIZE = 2
)
type DelayFactor byte type DelayFactor byte
type DeliveryInstructions []byte type DeliveryInstructions []byte
@ -298,9 +308,9 @@ func (delivery_instructions DeliveryInstructions) HasHash() (bool, error) {
return false, err return false, err
} }
if di_type == DT_TUNNEL || di_type == DT_ROUTER { if di_type == DT_TUNNEL || di_type == DT_ROUTER {
min_size := 33 min_size := FLAG_SIZE + HASH_SIZE
if di_type == DT_TUNNEL { if di_type == DT_TUNNEL {
min_size += 4 min_size += TUNNEL_ID_SIZE
} }
if len(delivery_instructions) < min_size { if len(delivery_instructions) < min_size {
return false, errors.New("Delivery Instructions indicates hash present but has too little data") return false, errors.New("Delivery Instructions indicates hash present but has too little data")
@ -319,8 +329,8 @@ func (delivery_instructions DeliveryInstructions) TunnelID() (tunnel_id uint32,
return return
} }
if has_tunnel_id { if has_tunnel_id {
if len(delivery_instructions) >= 5 { if len(delivery_instructions) >= FLAG_SIZE+TUNNEL_ID_SIZE {
tunnel_id = binary.BigEndian.Uint32(delivery_instructions[1:5]) tunnel_id = binary.BigEndian.Uint32(delivery_instructions[FLAG_SIZE:TUNNEL_ID_SIZE])
} else { } else {
err = errors.New("DeliveryInstructions are invalid, too little data for Tunnel ID") err = errors.New("DeliveryInstructions are invalid, too little data for Tunnel ID")
} }
@ -338,12 +348,11 @@ func (delivery_instructions DeliveryInstructions) Hash() (hash common.Hash, err
if err != nil { if err != nil {
return return
} }
hash_start := 1 hash_start := FLAG_SIZE
hash_end := 33 hash_end := FLAG_SIZE + HASH_SIZE
if delivery_type == DT_TUNNEL { if delivery_type == DT_TUNNEL {
// add 4 bytes for DT_TUNNEL's TunnelID hash_start := hash_start + TUNNEL_ID_SIZE
hash_start := hash_start + 4 hash_end := hash_end + TUNNEL_ID_SIZE
hash_end := hash_end + 4
if len(delivery_instructions) >= hash_end { if len(delivery_instructions) >= hash_end {
copy(hash[:], delivery_instructions[hash_start:hash_end]) copy(hash[:], delivery_instructions[hash_start:hash_end])
} else { } else {
@ -374,15 +383,15 @@ func (delivery_instructions DeliveryInstructions) Delay() (delay_factor DelayFac
return return
} }
if di_type == DT_TUNNEL { if di_type == DT_TUNNEL {
if len(delivery_instructions) >= 37 { if len(delivery_instructions) >= FLAG_SIZE+TUNNEL_ID_SIZE+HASH_SIZE {
delay_factor = DelayFactor(delivery_instructions[37]) delay_factor = DelayFactor(delivery_instructions[FLAG_SIZE+TUNNEL_ID_SIZE+HASH_SIZE])
} else { } else {
err = errors.New("DeliveryInstructions is invalid, does not contain enough data for DelayFactor") err = errors.New("DeliveryInstructions is invalid, does not contain enough data for DelayFactor")
return return
} }
} else if di_type == DT_ROUTER { } else if di_type == DT_ROUTER {
if len(delivery_instructions) >= 36 { if len(delivery_instructions) >= FLAG_SIZE+HASH_SIZE {
delay_factor = DelayFactor(delivery_instructions[36]) delay_factor = DelayFactor(delivery_instructions[FLAG_SIZE+HASH_SIZE])
} else { } else {
err = errors.New("DeliveryInstructions is invalid, does not contain enough data for DelayFactor") err = errors.New("DeliveryInstructions is invalid, does not contain enough data for DelayFactor")
return return

View File

@ -30,17 +30,20 @@ func validFirstFragmentDeliveryInstructions() []byte {
FirstFragment: true, FirstFragment: true,
Type: 0x02, Type: 0x02,
Delay: false, Delay: false,
}.FlagByte() }
data = append(data, flag) data = append(data, flag.FlagByte())
tunnel_id := []byte{0x00, 0x00, 0x00, 0x01} tunnel_id := []byte{0x00, 0x00, 0x00, 0x01}
data = append(data, tunnel_id...) data = append(data, tunnel_id...)
hash := make([]byte, 32) hash := make([]byte, HASH_SIZE)
data = append(data, hash...) data = append(data, hash...)
// Add 0 delay if flag.Delay {
data = append(data, 0) data = append(data, 1)
} else {
data = append(data, 0)
}
message_id := []byte{0x00, 0x00, 0x00, 0x02} message_id := []byte{0x00, 0x00, 0x00, 0x02}
data = append(data, message_id...) data = append(data, message_id...)