mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-22 17:40:26 +00:00
cleanup
This commit is contained in:
parent
bb3ae8b39b
commit
0470f6f1c1
@ -223,8 +223,6 @@ func (p *peer) sendPacket(packet []byte) {
|
|||||||
func (p *peer) sendLinkPacket(packet []byte) {
|
func (p *peer) sendLinkPacket(packet []byte) {
|
||||||
bs, nonce := boxSeal(&p.shared, packet, nil)
|
bs, nonce := boxSeal(&p.shared, packet, nil)
|
||||||
linkPacket := wire_linkProtoTrafficPacket{
|
linkPacket := wire_linkProtoTrafficPacket{
|
||||||
//toKey: p.box,
|
|
||||||
//fromKey: p.core.boxPub,
|
|
||||||
nonce: *nonce,
|
nonce: *nonce,
|
||||||
payload: bs,
|
payload: bs,
|
||||||
}
|
}
|
||||||
@ -237,12 +235,6 @@ func (p *peer) handleLinkTraffic(bs []byte) {
|
|||||||
if !packet.decode(bs) {
|
if !packet.decode(bs) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//if packet.toKey != p.core.boxPub {
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
//if packet.fromKey != p.box {
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
payload, isOK := boxOpen(&p.shared, packet.payload, &packet.nonce)
|
payload, isOK := boxOpen(&p.shared, packet.payload, &packet.nonce)
|
||||||
if !isOK {
|
if !isOK {
|
||||||
return
|
return
|
||||||
|
@ -295,9 +295,6 @@ func (r *router) handleDHTReq(bs []byte, fromKey *boxPubKey) {
|
|||||||
if !req.decode(bs) {
|
if !req.decode(bs) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//if req.key != *fromKey {
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
req.key = *fromKey
|
req.key = *fromKey
|
||||||
r.core.dht.handleReq(&req)
|
r.core.dht.handleReq(&req)
|
||||||
}
|
}
|
||||||
@ -307,9 +304,6 @@ func (r *router) handleDHTRes(bs []byte, fromKey *boxPubKey) {
|
|||||||
if !res.decode(bs) {
|
if !res.decode(bs) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//if res.key != *fromKey {
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
res.key = *fromKey
|
res.key = *fromKey
|
||||||
r.core.dht.handleRes(&res)
|
r.core.dht.handleRes(&res)
|
||||||
}
|
}
|
||||||
|
@ -74,8 +74,6 @@ func (iface *udpInterface) init(core *Core, addr string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
//iface.sock.SetReadBuffer(1048576)
|
|
||||||
//iface.sock.SetWriteBuffer(1048576)
|
|
||||||
iface.conns = make(map[connAddr]*connInfo)
|
iface.conns = make(map[connAddr]*connInfo)
|
||||||
go iface.reader()
|
go iface.reader()
|
||||||
}
|
}
|
||||||
@ -99,7 +97,6 @@ func (iface *udpInterface) startConn(info *connInfo) {
|
|||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
defer func() {
|
defer func() {
|
||||||
// Cleanup
|
// Cleanup
|
||||||
// FIXME this still leaks a peer struct
|
|
||||||
iface.mutex.Lock()
|
iface.mutex.Lock()
|
||||||
delete(iface.conns, info.addr)
|
delete(iface.conns, info.addr)
|
||||||
iface.mutex.Unlock()
|
iface.mutex.Unlock()
|
||||||
@ -178,51 +175,18 @@ func (iface *udpInterface) handleKeys(msg []byte, addr connAddr) {
|
|||||||
if ifce != nil && err == nil {
|
if ifce != nil && err == nil {
|
||||||
conn.chunkSize = uint16(ifce.MTU) - 60 - 8 - 3
|
conn.chunkSize = uint16(ifce.MTU) - 60 - 8 - 3
|
||||||
}
|
}
|
||||||
//conn.chunkSize = 65535 - 8 - 3
|
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
conn.in = func (msg []byte) { conn.peer.handlePacket(msg, conn.linkIn) }
|
|
||||||
conn.peer.out = func (msg []byte) {
|
|
||||||
start := time.Now()
|
|
||||||
iface.sock.WriteToUDP(msg, udpAddr)
|
|
||||||
timed := time.Since(start)
|
|
||||||
conn.peer.updateBandwidth(len(msg), timed)
|
|
||||||
util_putBytes(msg)
|
|
||||||
} // Old version, always one syscall per packet
|
|
||||||
//*/
|
|
||||||
/*
|
|
||||||
conn.peer.out = func (msg []byte) {
|
|
||||||
defer func() { recover() }()
|
|
||||||
select {
|
|
||||||
case conn.out<-msg:
|
|
||||||
default: util_putBytes(msg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
go func () {
|
|
||||||
for msg := range conn.out {
|
|
||||||
start := time.Now()
|
|
||||||
iface.sock.WriteToUDP(msg, udpAddr)
|
|
||||||
timed := time.Since(start)
|
|
||||||
conn.peer.updateBandwidth(len(msg), timed)
|
|
||||||
util_putBytes(msg)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
//*/
|
|
||||||
//*
|
|
||||||
var inChunks uint8
|
var inChunks uint8
|
||||||
var inBuf []byte
|
var inBuf []byte
|
||||||
conn.in = func(bs []byte) {
|
conn.in = func(bs []byte) {
|
||||||
//defer util_putBytes(bs)
|
//defer util_putBytes(bs)
|
||||||
chunks, chunk, count, payload := udp_decode(bs)
|
chunks, chunk, count, payload := udp_decode(bs)
|
||||||
//iface.core.log.Println("DEBUG:", addr, chunks, chunk, count, len(payload))
|
|
||||||
//iface.core.log.Println("DEBUG: payload:", payload)
|
|
||||||
if count != conn.countIn {
|
if count != conn.countIn {
|
||||||
inChunks = 0
|
inChunks = 0
|
||||||
inBuf = inBuf[:0]
|
inBuf = inBuf[:0]
|
||||||
conn.countIn = count
|
conn.countIn = count
|
||||||
}
|
}
|
||||||
if chunk <= chunks && chunk == inChunks+1 {
|
if chunk <= chunks && chunk == inChunks+1 {
|
||||||
//iface.core.log.Println("GOING:", addr, chunks, chunk, count, len(payload))
|
|
||||||
inChunks += 1
|
inChunks += 1
|
||||||
inBuf = append(inBuf, payload...)
|
inBuf = append(inBuf, payload...)
|
||||||
if chunks != chunk {
|
if chunks != chunk {
|
||||||
@ -230,7 +194,6 @@ func (iface *udpInterface) handleKeys(msg []byte, addr connAddr) {
|
|||||||
}
|
}
|
||||||
msg := append(util_getBytes(), inBuf...)
|
msg := append(util_getBytes(), inBuf...)
|
||||||
conn.peer.handlePacket(msg, conn.linkIn)
|
conn.peer.handlePacket(msg, conn.linkIn)
|
||||||
//iface.core.log.Println("DONE:", addr, chunks, chunk, count, len(payload))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
conn.peer.out = func(msg []byte) {
|
conn.peer.out = func(msg []byte) {
|
||||||
@ -251,7 +214,6 @@ func (iface *udpInterface) handleKeys(msg []byte, addr connAddr) {
|
|||||||
chunks, bs = append(chunks, bs[:conn.chunkSize]), bs[conn.chunkSize:]
|
chunks, bs = append(chunks, bs[:conn.chunkSize]), bs[conn.chunkSize:]
|
||||||
}
|
}
|
||||||
chunks = append(chunks, bs)
|
chunks = append(chunks, bs)
|
||||||
//iface.core.log.Println("DEBUG: out chunks:", len(chunks), len(msg))
|
|
||||||
if len(chunks) > 255 {
|
if len(chunks) > 255 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -266,7 +228,6 @@ func (iface *udpInterface) handleKeys(msg []byte, addr connAddr) {
|
|||||||
conn.countOut += 1
|
conn.countOut += 1
|
||||||
conn.peer.updateBandwidth(len(msg), timed)
|
conn.peer.updateBandwidth(len(msg), timed)
|
||||||
util_putBytes(msg)
|
util_putBytes(msg)
|
||||||
//iface.core.log.Println("DEBUG: sent:", len(msg))
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
//*/
|
//*/
|
||||||
@ -298,16 +259,12 @@ func (iface *udpInterface) handlePacket(msg []byte, addr connAddr) {
|
|||||||
func (iface *udpInterface) reader() {
|
func (iface *udpInterface) reader() {
|
||||||
bs := make([]byte, 65536) // This needs to be large enough for everything...
|
bs := make([]byte, 65536) // This needs to be large enough for everything...
|
||||||
for {
|
for {
|
||||||
//iface.core.log.Println("Starting read")
|
|
||||||
n, udpAddr, err := iface.sock.ReadFromUDP(bs)
|
n, udpAddr, err := iface.sock.ReadFromUDP(bs)
|
||||||
//iface.core.log.Println("Read", n, udpAddr.String(), err)
|
|
||||||
//iface.core.log.Println("DEBUG: read:", bs[0], bs[1], bs[2], n)
|
//iface.core.log.Println("DEBUG: read:", bs[0], bs[1], bs[2], n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
//iface.core.log.Println("DEBUG: recv len:", n)
|
|
||||||
//msg := append(util_getBytes(), bs[:n]...)
|
|
||||||
msg := bs[:n]
|
msg := bs[:n]
|
||||||
var addr connAddr
|
var addr connAddr
|
||||||
addr.fromUDPAddr(udpAddr)
|
addr.fromUDPAddr(udpAddr)
|
||||||
@ -330,9 +287,6 @@ func (iface *udpInterface) reader() {
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
//const udp_chunkSize = 508 // Apparently the maximum guaranteed safe IPv4 size
|
|
||||||
//const udp_chunkSize = 65535 - 3 - 8
|
|
||||||
|
|
||||||
func udp_decode(bs []byte) (chunks, chunk, count uint8, payload []byte) {
|
func udp_decode(bs []byte) (chunks, chunk, count uint8, payload []byte) {
|
||||||
if len(bs) >= 3 {
|
if len(bs) >= 3 {
|
||||||
chunks, chunk, count, payload = bs[0], bs[1], bs[2], bs[3:]
|
chunks, chunk, count, payload = bs[0], bs[1], bs[2], bs[3:]
|
||||||
|
@ -21,7 +21,6 @@ const (
|
|||||||
wire_DHTLookupResponse // inside protocol traffic header
|
wire_DHTLookupResponse // inside protocol traffic header
|
||||||
wire_SearchRequest // inside protocol traffic header
|
wire_SearchRequest // inside protocol traffic header
|
||||||
wire_SearchResponse // inside protocol traffic header
|
wire_SearchResponse // inside protocol traffic header
|
||||||
wire_Keys // udp key packet (boxPub, sigPub)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Encode uint64 using a variable length scheme
|
// Encode uint64 using a variable length scheme
|
||||||
@ -112,8 +111,6 @@ func wire_put_coords(coords []byte, bs []byte) []byte {
|
|||||||
func wire_decode_coords(packet []byte) ([]byte, int) {
|
func wire_decode_coords(packet []byte) ([]byte, int) {
|
||||||
coordLen, coordBegin := wire_decode_uint64(packet)
|
coordLen, coordBegin := wire_decode_uint64(packet)
|
||||||
coordEnd := coordBegin + int(coordLen)
|
coordEnd := coordBegin + int(coordLen)
|
||||||
//if coordBegin == 0 { panic("No coords found") } // Testing
|
|
||||||
//if coordEnd > len(packet) { panic("Packet too short") } // Testing
|
|
||||||
if coordBegin == 0 || coordEnd > len(packet) {
|
if coordBegin == 0 || coordEnd > len(packet) {
|
||||||
return nil, 0
|
return nil, 0
|
||||||
}
|
}
|
||||||
@ -138,7 +135,6 @@ func (m *msgAnnounce) encode() []byte {
|
|||||||
bs = append(bs, wire_encode_uint64(wire_intToUint(m.tstamp))...)
|
bs = append(bs, wire_encode_uint64(wire_intToUint(m.tstamp))...)
|
||||||
bs = append(bs, wire_encode_uint64(m.seq)...)
|
bs = append(bs, wire_encode_uint64(m.seq)...)
|
||||||
bs = append(bs, wire_encode_uint64(m.len)...)
|
bs = append(bs, wire_encode_uint64(m.len)...)
|
||||||
//bs = append(bs, wire_encode_uint64(m.Deg)...)
|
|
||||||
bs = append(bs, wire_encode_uint64(m.rseq)...)
|
bs = append(bs, wire_encode_uint64(m.rseq)...)
|
||||||
return bs
|
return bs
|
||||||
}
|
}
|
||||||
@ -159,7 +155,6 @@ func (m *msgAnnounce) decode(bs []byte) bool {
|
|||||||
return false
|
return false
|
||||||
case !wire_chop_uint64(&m.len, &bs):
|
case !wire_chop_uint64(&m.len, &bs):
|
||||||
return false
|
return false
|
||||||
//case !wire_chop_uint64(&m.Deg, &bs): return false
|
|
||||||
case !wire_chop_uint64(&m.rseq, &bs):
|
case !wire_chop_uint64(&m.rseq, &bs):
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -381,16 +376,12 @@ func (p *wire_protoTrafficPacket) decode(bs []byte) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type wire_linkProtoTrafficPacket struct {
|
type wire_linkProtoTrafficPacket struct {
|
||||||
//toKey boxPubKey
|
|
||||||
//fromKey boxPubKey
|
|
||||||
nonce boxNonce
|
nonce boxNonce
|
||||||
payload []byte
|
payload []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *wire_linkProtoTrafficPacket) encode() []byte {
|
func (p *wire_linkProtoTrafficPacket) encode() []byte {
|
||||||
bs := wire_encode_uint64(wire_LinkProtocolTraffic)
|
bs := wire_encode_uint64(wire_LinkProtocolTraffic)
|
||||||
//bs = append(bs, p.toKey[:]...)
|
|
||||||
//bs = append(bs, p.fromKey[:]...)
|
|
||||||
bs = append(bs, p.nonce[:]...)
|
bs = append(bs, p.nonce[:]...)
|
||||||
bs = append(bs, p.payload...)
|
bs = append(bs, p.payload...)
|
||||||
return bs
|
return bs
|
||||||
@ -403,10 +394,6 @@ func (p *wire_linkProtoTrafficPacket) decode(bs []byte) bool {
|
|||||||
return false
|
return false
|
||||||
case pType != wire_LinkProtocolTraffic:
|
case pType != wire_LinkProtocolTraffic:
|
||||||
return false
|
return false
|
||||||
//case !wire_chop_slice(p.toKey[:], &bs):
|
|
||||||
// return false
|
|
||||||
//case !wire_chop_slice(p.fromKey[:], &bs):
|
|
||||||
// return false
|
|
||||||
case !wire_chop_slice(p.nonce[:], &bs):
|
case !wire_chop_slice(p.nonce[:], &bs):
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -468,7 +455,6 @@ func (p *sessionPing) decode(bs []byte) bool {
|
|||||||
func (r *dhtReq) encode() []byte {
|
func (r *dhtReq) encode() []byte {
|
||||||
coords := wire_encode_coords(r.coords)
|
coords := wire_encode_coords(r.coords)
|
||||||
bs := wire_encode_uint64(wire_DHTLookupRequest)
|
bs := wire_encode_uint64(wire_DHTLookupRequest)
|
||||||
//bs = append(bs, r.key[:]...)
|
|
||||||
bs = append(bs, coords...)
|
bs = append(bs, coords...)
|
||||||
bs = append(bs, r.dest[:]...)
|
bs = append(bs, r.dest[:]...)
|
||||||
return bs
|
return bs
|
||||||
@ -481,8 +467,6 @@ func (r *dhtReq) decode(bs []byte) bool {
|
|||||||
return false
|
return false
|
||||||
case pType != wire_DHTLookupRequest:
|
case pType != wire_DHTLookupRequest:
|
||||||
return false
|
return false
|
||||||
//case !wire_chop_slice(r.key[:], &bs):
|
|
||||||
// return false
|
|
||||||
case !wire_chop_coords(&r.coords, &bs):
|
case !wire_chop_coords(&r.coords, &bs):
|
||||||
return false
|
return false
|
||||||
case !wire_chop_slice(r.dest[:], &bs):
|
case !wire_chop_slice(r.dest[:], &bs):
|
||||||
@ -495,7 +479,6 @@ func (r *dhtReq) decode(bs []byte) bool {
|
|||||||
func (r *dhtRes) encode() []byte {
|
func (r *dhtRes) encode() []byte {
|
||||||
coords := wire_encode_coords(r.coords)
|
coords := wire_encode_coords(r.coords)
|
||||||
bs := wire_encode_uint64(wire_DHTLookupResponse)
|
bs := wire_encode_uint64(wire_DHTLookupResponse)
|
||||||
//bs = append(bs, r.key[:]...)
|
|
||||||
bs = append(bs, coords...)
|
bs = append(bs, coords...)
|
||||||
bs = append(bs, r.dest[:]...)
|
bs = append(bs, r.dest[:]...)
|
||||||
for _, info := range r.infos {
|
for _, info := range r.infos {
|
||||||
@ -513,8 +496,6 @@ func (r *dhtRes) decode(bs []byte) bool {
|
|||||||
return false
|
return false
|
||||||
case pType != wire_DHTLookupResponse:
|
case pType != wire_DHTLookupResponse:
|
||||||
return false
|
return false
|
||||||
//case !wire_chop_slice(r.key[:], &bs):
|
|
||||||
// return false
|
|
||||||
case !wire_chop_coords(&r.coords, &bs):
|
case !wire_chop_coords(&r.coords, &bs):
|
||||||
return false
|
return false
|
||||||
case !wire_chop_slice(r.dest[:], &bs):
|
case !wire_chop_slice(r.dest[:], &bs):
|
||||||
|
@ -118,7 +118,7 @@ func generateConfig() *nodeConfig {
|
|||||||
cfg.Multicast = true
|
cfg.Multicast = true
|
||||||
cfg.LinkLocal = ""
|
cfg.LinkLocal = ""
|
||||||
cfg.IfName = "auto"
|
cfg.IfName = "auto"
|
||||||
cfg.IfMTU = 1280 //65535
|
cfg.IfMTU = 1280
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
cfg.IfTAPMode = true
|
cfg.IfTAPMode = true
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user