5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-12-23 06:25:40 +00:00

optimize wire_put_uint64; use protokey for flowlabel fallback.

This commit is contained in:
cathugger 2018-07-30 01:58:52 +00:00
parent fec7100898
commit 36dcab9300
No known key found for this signature in database
GPG Key ID: 9BADDA2DAF6F01A8
2 changed files with 20 additions and 15 deletions

View File

@ -440,12 +440,21 @@ func (sinfo *sessionInfo) doSend(bs []byte) {
coords = append(coords, sinfo.coords...) // Start with the real coords
coords = append(coords, 0) // Then target the local switchport
coords = wire_put_uint64(uint64(flowlabel), coords) // Then variable-length encoded flowlabel
} else if len(bs) >= 48 /* min UDP len, others are bigger */ &&
(bs[6] == 0x06 || bs[6] == 0x11 || bs[6] == 0x84) /* TCP UDP SCTP */ {
// if flowlabel was unspecified (0), try to use known protocols' ports
// protokey: proto | sport | dport
pkey := uint64(bs[6])<<32 /* proto */ |
uint64(bs[40])<<24 | uint64(bs[41])<<16 /* sport */ |
uint64(bs[42])<<8 | uint64(bs[43]) /* dport */
coords = append(coords, sinfo.coords...) // Start with the real coords
coords = append(coords, 0) // Then target the local switchport
coords = wire_put_uint64(pkey, coords) // Then variable-length encoded protokey
} else {
// 0 value means that flowlabels aren't being generated by OS.
// flowlabel was unspecified (0) and protocol unrecognised.
// To save bytes, we're not including it, therefore we won't need self-port override either.
// So just use sinfo.coords directly to avoid golang GC allocations.
// Recent enough Linux kernel supports flowlabels out of the box so this will be rare.
// XXX Attempt to look into TCP/UDP/SCTP/DCCP headers' sport/dport fields there?
// Recent enough Linux and BSDs support flowlabels (auto_flowlabel) out of the box so this will be rare.
coords = sinfo.coords
}
payload, nonce := boxSeal(&sinfo.sharedSesKey, bs, &sinfo.myNonce)

View File

@ -25,19 +25,15 @@ func wire_encode_uint64(elem uint64) []byte {
// Encode uint64 using a variable length scheme.
// Similar to binary.Uvarint, but big-endian.
func wire_put_uint64(elem uint64, out []byte) []byte {
bs := make([]byte, 0, 10)
bs = append(bs, byte(elem&0x7f))
for e := elem >> 7; e > 0; e >>= 7 {
bs = append(bs, byte(e|0x80))
func wire_put_uint64(e uint64, out []byte) []byte {
var b [10]byte
i := len(b) - 1
b[i] = byte(e & 0x7f)
for e >>= 7; e != 0; e >>= 7 {
i--
b[i] = byte(e | 0x80)
}
// Now reverse bytes, because we set them in the wrong order
// TODO just put them in the right place the first time...
last := len(bs) - 1
for idx := 0; idx < len(bs)/2; idx++ {
bs[idx], bs[last-idx] = bs[last-idx], bs[idx]
}
return append(out, bs...)
return append(out, b[i:]...)
}
// Returns the length of a wire encoded uint64 of this value.