From 49af65296dfa4542a89de59a8f83df5bf376f2f2 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sat, 2 Jun 2018 21:21:05 +0100 Subject: [PATCH 1/2] Export fields of wire structs --- src/yggdrasil/admin.go | 6 +- src/yggdrasil/core.go | 2 +- src/yggdrasil/dht.go | 24 +++--- src/yggdrasil/peer.go | 108 ++++++++++++------------ src/yggdrasil/release.go | 2 +- src/yggdrasil/router.go | 16 ++-- src/yggdrasil/search.go | 24 +++--- src/yggdrasil/session.go | 32 +++---- src/yggdrasil/wire.go | 174 +++++++++++++++++++-------------------- 9 files changed, 194 insertions(+), 194 deletions(-) diff --git a/src/yggdrasil/admin.go b/src/yggdrasil/admin.go index cbfe315..fe9c05e 100644 --- a/src/yggdrasil/admin.go +++ b/src/yggdrasil/admin.go @@ -583,9 +583,9 @@ func (a *admin) getResponse_dot() []byte { infos[info.key] = info } } - addInfo(dht, "fillcolor=\"#ffffff\" style=filled fontname=\"sans serif\"", "Known in DHT") // white - addInfo(sessions, "fillcolor=\"#acf3fd\" style=filled fontname=\"sans serif\"", "Open session") // blue - addInfo(peers, "fillcolor=\"#ffffb5\" style=filled fontname=\"sans serif\"", "Connected peer") // yellow + addInfo(dht, "fillcolor=\"#ffffff\" style=filled fontname=\"sans serif\"", "Known in DHT") // white + addInfo(sessions, "fillcolor=\"#acf3fd\" style=filled fontname=\"sans serif\"", "Open session") // blue + addInfo(peers, "fillcolor=\"#ffffb5\" style=filled fontname=\"sans serif\"", "Connected peer") // yellow addInfo(append([]admin_nodeInfo(nil), *self), "fillcolor=\"#a5ff8a\" style=filled fontname=\"sans serif\"", "This node") // green // Get coords as a slice of strings, FIXME? this looks very fragile coordSlice := func(coords string) []string { diff --git a/src/yggdrasil/core.go b/src/yggdrasil/core.go index 9938266..ff9c360 100644 --- a/src/yggdrasil/core.go +++ b/src/yggdrasil/core.go @@ -172,7 +172,7 @@ func (c *Core) GetAddress() *net.IP { func (c *Core) GetSubnet() *net.IPNet { subnet := address_subnetForNodeID(c.GetNodeID())[:] subnet = append(subnet, 0, 0, 0, 0, 0, 0, 0, 0) - return &net.IPNet{ IP: subnet, Mask: net.CIDRMask(64, 128) } + return &net.IPNet{IP: subnet, Mask: net.CIDRMask(64, 128)} } // Sets the output logger of the Yggdrasil node after startup. This may be diff --git a/src/yggdrasil/dht.go b/src/yggdrasil/dht.go index 1f03bee..b31b0a2 100644 --- a/src/yggdrasil/dht.go +++ b/src/yggdrasil/dht.go @@ -318,12 +318,12 @@ func (t *dht) sendReq(req *dhtReq, dest *dhtInfo) { shared := t.core.sessions.getSharedKey(&t.core.boxPriv, &dest.key) payload, nonce := boxSeal(shared, bs, nil) p := wire_protoTrafficPacket{ - ttl: ^uint64(0), - coords: dest.coords, - toKey: dest.key, - fromKey: t.core.boxPub, - nonce: *nonce, - payload: payload, + TTL: ^uint64(0), + Coords: dest.coords, + ToKey: dest.key, + FromKey: t.core.boxPub, + Nonce: *nonce, + Payload: payload, } packet := p.encode() t.core.router.out(packet) @@ -344,12 +344,12 @@ func (t *dht) sendRes(res *dhtRes, req *dhtReq) { shared := t.core.sessions.getSharedKey(&t.core.boxPriv, &req.key) payload, nonce := boxSeal(shared, bs, nil) p := wire_protoTrafficPacket{ - ttl: ^uint64(0), - coords: req.coords, - toKey: req.key, - fromKey: t.core.boxPub, - nonce: *nonce, - payload: payload, + TTL: ^uint64(0), + Coords: req.coords, + ToKey: req.key, + FromKey: t.core.boxPub, + Nonce: *nonce, + Payload: payload, } packet := p.encode() t.core.router.out(packet) diff --git a/src/yggdrasil/peer.go b/src/yggdrasil/peer.go index 4ce1a78..f6d1a4f 100644 --- a/src/yggdrasil/peer.go +++ b/src/yggdrasil/peer.go @@ -207,16 +207,16 @@ func (p *peer) linkLoop(in <-chan []byte) { switch { case p.msgAnc == nil: update = true - case lastRSeq != p.msgAnc.seq: + case lastRSeq != p.msgAnc.Seq: update = true - case p.msgAnc.rseq != p.myMsg.seq: + case p.msgAnc.Rseq != p.myMsg.seq: update = true case counter%4 == 0: update = true } if update { if p.msgAnc != nil { - lastRSeq = p.msgAnc.seq + lastRSeq = p.msgAnc.Seq } p.sendSwitchAnnounce() } @@ -291,8 +291,8 @@ func (p *peer) sendPacket(packet []byte) { func (p *peer) sendLinkPacket(packet []byte) { bs, nonce := boxSeal(&p.shared, packet, nil) linkPacket := wire_linkProtoTrafficPacket{ - nonce: *nonce, - payload: bs, + Nonce: *nonce, + Payload: bs, } packet = linkPacket.encode() p.sendPacket(packet) @@ -303,7 +303,7 @@ func (p *peer) handleLinkTraffic(bs []byte) { if !packet.decode(bs) { return } - payload, isOK := boxOpen(&p.shared, packet.payload, &packet.nonce) + payload, isOK := boxOpen(&p.shared, packet.Payload, &packet.Nonce) if !isOK { return } @@ -331,9 +331,9 @@ func (p *peer) handleSwitchAnnounce(packet []byte) { } //if p.msgAnc != nil && anc.Seq != p.msgAnc.Seq { p.msgHops = nil } if p.msgAnc == nil || - anc.root != p.msgAnc.root || - anc.tstamp != p.msgAnc.tstamp || - anc.seq != p.msgAnc.seq { + anc.Root != p.msgAnc.Root || + anc.Tstamp != p.msgAnc.Tstamp || + anc.Seq != p.msgAnc.Seq { p.msgHops = nil } p.msgAnc = &anc @@ -344,10 +344,10 @@ func (p *peer) handleSwitchAnnounce(packet []byte) { func (p *peer) requestHop(hop uint64) { //p.core.log.Println("DEBUG requestHop") req := msgHopReq{} - req.root = p.msgAnc.root - req.tstamp = p.msgAnc.tstamp - req.seq = p.msgAnc.seq - req.hop = hop + req.Root = p.msgAnc.Root + req.Tstamp = p.msgAnc.Tstamp + req.Seq = p.msgAnc.Seq + req.Hop = hop packet := req.encode() p.sendLinkPacket(packet) } @@ -364,28 +364,28 @@ func (p *peer) handleSwitchHopRequest(packet []byte) { if !req.decode(packet) { return } - if req.root != p.myMsg.locator.root { + if req.Root != p.myMsg.locator.root { return } - if req.tstamp != p.myMsg.locator.tstamp { + if req.Tstamp != p.myMsg.locator.tstamp { return } - if req.seq != p.myMsg.seq { + if req.Seq != p.myMsg.seq { return } - if uint64(len(p.myMsg.locator.coords)) <= req.hop { + if uint64(len(p.myMsg.locator.coords)) <= req.Hop { return } res := msgHop{} - res.root = p.myMsg.locator.root - res.tstamp = p.myMsg.locator.tstamp - res.seq = p.myMsg.seq - res.hop = req.hop - res.port = p.myMsg.locator.coords[res.hop] - sinfo := p.getSig(res.hop) + res.Root = p.myMsg.locator.root + res.Tstamp = p.myMsg.locator.tstamp + res.Seq = p.myMsg.seq + res.Hop = req.Hop + res.Port = p.myMsg.locator.coords[res.Hop] + sinfo := p.getSig(res.Hop) //p.core.log.Println("DEBUG sig:", sinfo) - res.next = sinfo.next - res.sig = sinfo.sig + res.Next = sinfo.next + res.Sig = sinfo.sig packet = res.encode() p.sendLinkPacket(packet) } @@ -402,31 +402,31 @@ func (p *peer) handleSwitchHop(packet []byte) { if !res.decode(packet) { return } - if res.root != p.msgAnc.root { + if res.Root != p.msgAnc.Root { return } - if res.tstamp != p.msgAnc.tstamp { + if res.Tstamp != p.msgAnc.Tstamp { return } - if res.seq != p.msgAnc.seq { + if res.Seq != p.msgAnc.Seq { return } - if res.hop != uint64(len(p.msgHops)) { + if res.Hop != uint64(len(p.msgHops)) { return } // always process in order loc := switchLocator{coords: make([]switchPort, 0, len(p.msgHops)+1)} - loc.root = res.root - loc.tstamp = res.tstamp + loc.root = res.Root + loc.tstamp = res.Tstamp for _, hop := range p.msgHops { - loc.coords = append(loc.coords, hop.port) + loc.coords = append(loc.coords, hop.Port) } - loc.coords = append(loc.coords, res.port) - thisHopKey := &res.root - if res.hop != 0 { - thisHopKey = &p.msgHops[res.hop-1].next + loc.coords = append(loc.coords, res.Port) + thisHopKey := &res.Root + if res.Hop != 0 { + thisHopKey = &p.msgHops[res.Hop-1].Next } - bs := getBytesForSig(&res.next, &loc) - if p.core.sigs.check(thisHopKey, &res.sig, bs) { + bs := getBytesForSig(&res.Next, &loc) + if p.core.sigs.check(thisHopKey, &res.Sig, bs) { p.msgHops = append(p.msgHops, &res) p.processSwitchMessage() } else { @@ -442,12 +442,12 @@ func (p *peer) processSwitchMessage() { if p.msgAnc == nil { return } - if uint64(len(p.msgHops)) < p.msgAnc.len { + if uint64(len(p.msgHops)) < p.msgAnc.Len { p.requestHop(uint64(len(p.msgHops))) return } p.throttle++ - if p.msgAnc.len != uint64(len(p.msgHops)) { + if p.msgAnc.Len != uint64(len(p.msgHops)) { return } msg := switchMessage{} @@ -455,26 +455,26 @@ func (p *peer) processSwitchMessage() { sigs := make([]sigInfo, 0, len(p.msgHops)) for idx, hop := range p.msgHops { // Consistency checks, should be redundant (already checked these...) - if hop.root != p.msgAnc.root { + if hop.Root != p.msgAnc.Root { return } - if hop.tstamp != p.msgAnc.tstamp { + if hop.Tstamp != p.msgAnc.Tstamp { return } - if hop.seq != p.msgAnc.seq { + if hop.Seq != p.msgAnc.Seq { return } - if hop.hop != uint64(idx) { + if hop.Hop != uint64(idx) { return } - coords = append(coords, hop.port) - sigs = append(sigs, sigInfo{next: hop.next, sig: hop.sig}) + coords = append(coords, hop.Port) + sigs = append(sigs, sigInfo{next: hop.Next, sig: hop.Sig}) } msg.from = p.sig - msg.locator.root = p.msgAnc.root - msg.locator.tstamp = p.msgAnc.tstamp + msg.locator.root = p.msgAnc.Root + msg.locator.tstamp = p.msgAnc.Tstamp msg.locator.coords = coords - msg.seq = p.msgAnc.seq + msg.seq = p.msgAnc.Seq //msg.RSeq = p.msgAnc.RSeq //msg.Degree = p.msgAnc.Deg p.core.switchTable.handleMessage(&msg, p.port, sigs) @@ -493,13 +493,13 @@ func (p *peer) processSwitchMessage() { func (p *peer) sendSwitchAnnounce() { anc := msgAnnounce{} - anc.root = p.myMsg.locator.root - anc.tstamp = p.myMsg.locator.tstamp - anc.seq = p.myMsg.seq - anc.len = uint64(len(p.myMsg.locator.coords)) + anc.Root = p.myMsg.locator.root + anc.Tstamp = p.myMsg.locator.tstamp + anc.Seq = p.myMsg.seq + anc.Len = uint64(len(p.myMsg.locator.coords)) //anc.Deg = p.myMsg.Degree if p.msgAnc != nil { - anc.rseq = p.msgAnc.seq + anc.Rseq = p.msgAnc.Seq } packet := anc.encode() p.sendLinkPacket(packet) diff --git a/src/yggdrasil/release.go b/src/yggdrasil/release.go index fdbe8d5..1b5e9ca 100644 --- a/src/yggdrasil/release.go +++ b/src/yggdrasil/release.go @@ -8,5 +8,5 @@ import "log" // Starts the function profiler. This is only supported when built with // '-tags build'. func StartProfiler(_ *log.Logger) error { - return errors.New("Release builds do not support -pprof, build using '-tags debug'") + return errors.New("Release builds do not support -pprof, build using '-tags debug'") } diff --git a/src/yggdrasil/router.go b/src/yggdrasil/router.go index 545a15b..08677ef 100644 --- a/src/yggdrasil/router.go +++ b/src/yggdrasil/router.go @@ -266,7 +266,7 @@ func (r *router) handleTraffic(packet []byte) { if !p.decode(packet) { return } - sinfo, isIn := r.core.sessions.getSessionForHandle(&p.handle) + sinfo, isIn := r.core.sessions.getSessionForHandle(&p.Handle) if !isIn { return } @@ -283,13 +283,13 @@ func (r *router) handleProto(packet []byte) { // Now try to open the payload var sharedKey *boxSharedKey //var theirPermPub *boxPubKey - if p.toKey == r.core.boxPub { + if p.ToKey == r.core.boxPub { // Try to open using our permanent key - sharedKey = r.core.sessions.getSharedKey(&r.core.boxPriv, &p.fromKey) + sharedKey = r.core.sessions.getSharedKey(&r.core.boxPriv, &p.FromKey) } else { return } - bs, isOK := boxOpen(sharedKey, p.payload, &p.nonce) + bs, isOK := boxOpen(sharedKey, p.Payload, &p.Nonce) if !isOK { return } @@ -303,13 +303,13 @@ func (r *router) handleProto(packet []byte) { //fmt.Println("RECV bytes:", bs) switch bsType { case wire_SessionPing: - r.handlePing(bs, &p.fromKey) + r.handlePing(bs, &p.FromKey) case wire_SessionPong: - r.handlePong(bs, &p.fromKey) + r.handlePong(bs, &p.FromKey) case wire_DHTLookupRequest: - r.handleDHTReq(bs, &p.fromKey) + r.handleDHTReq(bs, &p.FromKey) case wire_DHTLookupResponse: - r.handleDHTRes(bs, &p.fromKey) + r.handleDHTRes(bs, &p.FromKey) case wire_SearchRequest: r.handleSearchReq(bs) case wire_SearchResponse: diff --git a/src/yggdrasil/search.go b/src/yggdrasil/search.go index d440661..6d7216d 100644 --- a/src/yggdrasil/search.go +++ b/src/yggdrasil/search.go @@ -108,12 +108,12 @@ func (s *searches) forwardSearch(req *searchReq, next *dhtInfo) { shared := s.core.sessions.getSharedKey(&s.core.boxPriv, &next.key) payload, nonce := boxSeal(shared, bs, nil) p := wire_protoTrafficPacket{ - ttl: ^uint64(0), - coords: next.coords, - toKey: next.key, - fromKey: s.core.boxPub, - nonce: *nonce, - payload: payload, + TTL: ^uint64(0), + Coords: next.coords, + ToKey: next.key, + FromKey: s.core.boxPub, + Nonce: *nonce, + Payload: payload, } packet := p.encode() s.core.router.out(packet) @@ -132,12 +132,12 @@ func (s *searches) sendSearchRes(req *searchReq) { shared := s.core.sessions.getSharedKey(&s.core.boxPriv, &req.key) payload, nonce := boxSeal(shared, bs, nil) p := wire_protoTrafficPacket{ - ttl: ^uint64(0), - coords: req.coords, - toKey: req.key, - fromKey: s.core.boxPub, - nonce: *nonce, - payload: payload, + TTL: ^uint64(0), + Coords: req.coords, + ToKey: req.key, + FromKey: s.core.boxPub, + Nonce: *nonce, + Payload: payload, } packet := p.encode() s.core.router.out(packet) diff --git a/src/yggdrasil/session.go b/src/yggdrasil/session.go index 090c973..c88786e 100644 --- a/src/yggdrasil/session.go +++ b/src/yggdrasil/session.go @@ -255,12 +255,12 @@ func (ss *sessions) sendPingPong(sinfo *sessionInfo, isPong bool) { shared := ss.getSharedKey(&ss.core.boxPriv, &sinfo.theirPermPub) payload, nonce := boxSeal(shared, bs, nil) p := wire_protoTrafficPacket{ - ttl: ^uint64(0), - coords: sinfo.coords, - toKey: sinfo.theirPermPub, - fromKey: ss.core.boxPub, - nonce: *nonce, - payload: payload, + TTL: ^uint64(0), + Coords: sinfo.coords, + ToKey: sinfo.theirPermPub, + FromKey: ss.core.boxPub, + Nonce: *nonce, + Payload: payload, } packet := p.encode() ss.core.router.out(packet) @@ -383,11 +383,11 @@ func (sinfo *sessionInfo) doSend(bs []byte) { payload, nonce := boxSeal(&sinfo.sharedSesKey, bs, &sinfo.myNonce) defer util_putBytes(payload) p := wire_trafficPacket{ - ttl: ^uint64(0), - coords: sinfo.coords, - handle: sinfo.theirHandle, - nonce: *nonce, - payload: payload, + TTL: ^uint64(0), + Coords: sinfo.coords, + Handle: sinfo.theirHandle, + Nonce: *nonce, + Payload: payload, } packet := p.encode() sinfo.bytesSent += uint64(len(bs)) @@ -395,12 +395,12 @@ func (sinfo *sessionInfo) doSend(bs []byte) { } func (sinfo *sessionInfo) doRecv(p *wire_trafficPacket) { - defer util_putBytes(p.payload) - payloadSize := uint16(len(p.payload)) - if !sinfo.nonceIsOK(&p.nonce) { + defer util_putBytes(p.Payload) + payloadSize := uint16(len(p.Payload)) + if !sinfo.nonceIsOK(&p.Nonce) { return } - bs, isOK := boxOpen(&sinfo.sharedSesKey, p.payload, &p.nonce) + bs, isOK := boxOpen(&sinfo.sharedSesKey, p.Payload, &p.Nonce) if !isOK { // We're going to guess that the session MTU is too large // Set myMTU to the largest value we think we can receive @@ -433,7 +433,7 @@ func (sinfo *sessionInfo) doRecv(p *wire_trafficPacket) { } } go func() { sinfo.core.router.admin <- fixSessionMTU }() - sinfo.updateNonce(&p.nonce) + sinfo.updateNonce(&p.Nonce) sinfo.time = time.Now() sinfo.bytesRecvd += uint64(len(bs)) sinfo.core.router.recvPacket(bs, &sinfo.theirAddr, &sinfo.theirSubnet) diff --git a/src/yggdrasil/wire.go b/src/yggdrasil/wire.go index 167a97d..9f2abfa 100644 --- a/src/yggdrasil/wire.go +++ b/src/yggdrasil/wire.go @@ -121,21 +121,21 @@ func wire_decode_coords(packet []byte) ([]byte, int) { // Announces that we can send parts of a Message with a particular seq type msgAnnounce struct { - root sigPubKey - tstamp int64 - seq uint64 - len uint64 + Root sigPubKey + Tstamp int64 + Seq uint64 + Len uint64 //Deg uint64 - rseq uint64 + Rseq uint64 } func (m *msgAnnounce) encode() []byte { bs := wire_encode_uint64(wire_SwitchAnnounce) - bs = append(bs, m.root[:]...) - 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.len)...) - bs = append(bs, wire_encode_uint64(m.rseq)...) + bs = append(bs, m.Root[:]...) + 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.Len)...) + bs = append(bs, wire_encode_uint64(m.Rseq)...) return bs } @@ -147,34 +147,34 @@ func (m *msgAnnounce) decode(bs []byte) bool { return false case pType != wire_SwitchAnnounce: return false - case !wire_chop_slice(m.root[:], &bs): + case !wire_chop_slice(m.Root[:], &bs): return false case !wire_chop_uint64(&tstamp, &bs): return false - case !wire_chop_uint64(&m.seq, &bs): + case !wire_chop_uint64(&m.Seq, &bs): return false - case !wire_chop_uint64(&m.len, &bs): + case !wire_chop_uint64(&m.Len, &bs): return false - case !wire_chop_uint64(&m.rseq, &bs): + case !wire_chop_uint64(&m.Rseq, &bs): return false } - m.tstamp = wire_intFromUint(tstamp) + m.Tstamp = wire_intFromUint(tstamp) return true } type msgHopReq struct { - root sigPubKey - tstamp int64 - seq uint64 - hop uint64 + Root sigPubKey + Tstamp int64 + Seq uint64 + Hop uint64 } func (m *msgHopReq) encode() []byte { bs := wire_encode_uint64(wire_SwitchHopRequest) - bs = append(bs, m.root[:]...) - 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.hop)...) + bs = append(bs, m.Root[:]...) + 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.Hop)...) return bs } @@ -186,38 +186,38 @@ func (m *msgHopReq) decode(bs []byte) bool { return false case pType != wire_SwitchHopRequest: return false - case !wire_chop_slice(m.root[:], &bs): + case !wire_chop_slice(m.Root[:], &bs): return false case !wire_chop_uint64(&tstamp, &bs): return false - case !wire_chop_uint64(&m.seq, &bs): + case !wire_chop_uint64(&m.Seq, &bs): return false - case !wire_chop_uint64(&m.hop, &bs): + case !wire_chop_uint64(&m.Hop, &bs): return false } - m.tstamp = wire_intFromUint(tstamp) + m.Tstamp = wire_intFromUint(tstamp) return true } type msgHop struct { - root sigPubKey - tstamp int64 - seq uint64 - hop uint64 - port switchPort - next sigPubKey - sig sigBytes + Root sigPubKey + Tstamp int64 + Seq uint64 + Hop uint64 + Port switchPort + Next sigPubKey + Sig sigBytes } func (m *msgHop) encode() []byte { bs := wire_encode_uint64(wire_SwitchHop) - bs = append(bs, m.root[:]...) - 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.hop)...) - bs = append(bs, wire_encode_uint64(uint64(m.port))...) - bs = append(bs, m.next[:]...) - bs = append(bs, m.sig[:]...) + bs = append(bs, m.Root[:]...) + 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.Hop)...) + bs = append(bs, wire_encode_uint64(uint64(m.Port))...) + bs = append(bs, m.Next[:]...) + bs = append(bs, m.Sig[:]...) return bs } @@ -229,22 +229,22 @@ func (m *msgHop) decode(bs []byte) bool { return false case pType != wire_SwitchHop: return false - case !wire_chop_slice(m.root[:], &bs): + case !wire_chop_slice(m.Root[:], &bs): return false case !wire_chop_uint64(&tstamp, &bs): return false - case !wire_chop_uint64(&m.seq, &bs): + case !wire_chop_uint64(&m.Seq, &bs): return false - case !wire_chop_uint64(&m.hop, &bs): + case !wire_chop_uint64(&m.Hop, &bs): return false - case !wire_chop_uint64((*uint64)(&m.port), &bs): + case !wire_chop_uint64((*uint64)(&m.Port), &bs): return false - case !wire_chop_slice(m.next[:], &bs): + case !wire_chop_slice(m.Next[:], &bs): return false - case !wire_chop_slice(m.sig[:], &bs): + case !wire_chop_slice(m.Sig[:], &bs): return false } - m.tstamp = wire_intFromUint(tstamp) + m.Tstamp = wire_intFromUint(tstamp) return true } @@ -292,22 +292,22 @@ func wire_chop_uint64(toUInt64 *uint64, fromSlice *[]byte) bool { // Wire traffic packets type wire_trafficPacket struct { - ttl uint64 - coords []byte - handle handle - nonce boxNonce - payload []byte + TTL uint64 + Coords []byte + Handle handle + Nonce boxNonce + Payload []byte } // This is basically MarshalBinary, but decode doesn't allow that... func (p *wire_trafficPacket) encode() []byte { bs := util_getBytes() bs = wire_put_uint64(wire_Traffic, bs) - bs = wire_put_uint64(p.ttl, bs) - bs = wire_put_coords(p.coords, bs) - bs = append(bs, p.handle[:]...) - bs = append(bs, p.nonce[:]...) - bs = append(bs, p.payload...) + bs = wire_put_uint64(p.TTL, bs) + bs = wire_put_coords(p.Coords, bs) + bs = append(bs, p.Handle[:]...) + bs = append(bs, p.Nonce[:]...) + bs = append(bs, p.Payload...) return bs } @@ -319,37 +319,37 @@ func (p *wire_trafficPacket) decode(bs []byte) bool { return false case pType != wire_Traffic: return false - case !wire_chop_uint64(&p.ttl, &bs): + case !wire_chop_uint64(&p.TTL, &bs): return false - case !wire_chop_coords(&p.coords, &bs): + case !wire_chop_coords(&p.Coords, &bs): return false - case !wire_chop_slice(p.handle[:], &bs): + case !wire_chop_slice(p.Handle[:], &bs): return false - case !wire_chop_slice(p.nonce[:], &bs): + case !wire_chop_slice(p.Nonce[:], &bs): return false } - p.payload = append(util_getBytes(), bs...) + p.Payload = append(util_getBytes(), bs...) return true } type wire_protoTrafficPacket struct { - ttl uint64 - coords []byte - toKey boxPubKey - fromKey boxPubKey - nonce boxNonce - payload []byte + TTL uint64 + Coords []byte + ToKey boxPubKey + FromKey boxPubKey + Nonce boxNonce + Payload []byte } func (p *wire_protoTrafficPacket) encode() []byte { - coords := wire_encode_coords(p.coords) + coords := wire_encode_coords(p.Coords) bs := wire_encode_uint64(wire_ProtocolTraffic) - bs = append(bs, wire_encode_uint64(p.ttl)...) + bs = append(bs, wire_encode_uint64(p.TTL)...) bs = append(bs, coords...) - bs = append(bs, p.toKey[:]...) - bs = append(bs, p.fromKey[:]...) - bs = append(bs, p.nonce[:]...) - bs = append(bs, p.payload...) + bs = append(bs, p.ToKey[:]...) + bs = append(bs, p.FromKey[:]...) + bs = append(bs, p.Nonce[:]...) + bs = append(bs, p.Payload...) return bs } @@ -360,30 +360,30 @@ func (p *wire_protoTrafficPacket) decode(bs []byte) bool { return false case pType != wire_ProtocolTraffic: return false - case !wire_chop_uint64(&p.ttl, &bs): + case !wire_chop_uint64(&p.TTL, &bs): return false - case !wire_chop_coords(&p.coords, &bs): + case !wire_chop_coords(&p.Coords, &bs): return false - case !wire_chop_slice(p.toKey[:], &bs): + case !wire_chop_slice(p.ToKey[:], &bs): return false - case !wire_chop_slice(p.fromKey[:], &bs): + 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 } - p.payload = bs + p.Payload = bs return true } type wire_linkProtoTrafficPacket struct { - nonce boxNonce - payload []byte + Nonce boxNonce + Payload []byte } func (p *wire_linkProtoTrafficPacket) encode() []byte { bs := wire_encode_uint64(wire_LinkProtocolTraffic) - bs = append(bs, p.nonce[:]...) - bs = append(bs, p.payload...) + bs = append(bs, p.Nonce[:]...) + bs = append(bs, p.Payload...) return bs } @@ -394,10 +394,10 @@ func (p *wire_linkProtoTrafficPacket) decode(bs []byte) bool { return false case pType != wire_LinkProtocolTraffic: return false - case !wire_chop_slice(p.nonce[:], &bs): + case !wire_chop_slice(p.Nonce[:], &bs): return false } - p.payload = bs + p.Payload = bs return true } From bbdcee101560e83c661f070aaf92ed503bb092f6 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sat, 2 Jun 2018 22:19:42 +0100 Subject: [PATCH 2/2] Export fields of sessionPing, dhtReq, dhtRes --- src/yggdrasil/dht.go | 58 ++++++++++++++++++++-------------------- src/yggdrasil/router.go | 6 ++--- src/yggdrasil/session.go | 54 ++++++++++++++++++------------------- src/yggdrasil/wire.go | 44 +++++++++++++++--------------- 4 files changed, 81 insertions(+), 81 deletions(-) diff --git a/src/yggdrasil/dht.go b/src/yggdrasil/dht.go index b31b0a2..5e61d41 100644 --- a/src/yggdrasil/dht.go +++ b/src/yggdrasil/dht.go @@ -51,16 +51,16 @@ type bucket struct { } type dhtReq struct { - key boxPubKey // Key of whoever asked - coords []byte // Coords of whoever asked - dest NodeID // NodeID they're asking about + Key boxPubKey // Key of whoever asked + Coords []byte // Coords of whoever asked + Dest NodeID // NodeID they're asking about } type dhtRes struct { - key boxPubKey // key to respond to - coords []byte // coords to respond to - dest NodeID - infos []*dhtInfo // response + Key boxPubKey // key to respond to + Coords []byte // coords to respond to + Dest NodeID + Infos []*dhtInfo // response } type dht_rumor struct { @@ -90,33 +90,33 @@ func (t *dht) handleReq(req *dhtReq) { loc := t.core.switchTable.getLocator() coords := loc.getCoords() res := dhtRes{ - key: t.core.boxPub, - coords: coords, - dest: req.dest, - infos: t.lookup(&req.dest, false), + Key: t.core.boxPub, + Coords: coords, + Dest: req.Dest, + Infos: t.lookup(&req.Dest, false), } t.sendRes(&res, req) // Also (possibly) add them to our DHT info := dhtInfo{ - key: req.key, - coords: req.coords, + key: req.Key, + coords: req.Coords, } t.insertIfNew(&info, false) // This seems DoSable (we just trust their coords...) //if req.dest != t.nodeID { t.ping(&info, info.getNodeID()) } // Or spam... } func (t *dht) handleRes(res *dhtRes) { - reqs, isIn := t.reqs[res.key] + reqs, isIn := t.reqs[res.Key] if !isIn { return } - _, isIn = reqs[res.dest] + _, isIn = reqs[res.Dest] if !isIn { return } rinfo := dhtInfo{ - key: res.key, - coords: res.coords, + key: res.Key, + coords: res.Coords, send: time.Now(), // Technically wrong but should be OK... recv: time.Now(), } @@ -138,15 +138,15 @@ func (t *dht) handleRes(res *dhtRes) { } // Insert into table t.insert(&rinfo, false) - if res.dest == *rinfo.getNodeID() { + if res.Dest == *rinfo.getNodeID() { return } // No infinite recursions - if len(res.infos) > dht_lookup_size { + if len(res.Infos) > dht_lookup_size { // Ignore any "extra" lookup results - res.infos = res.infos[:dht_lookup_size] + res.Infos = res.Infos[:dht_lookup_size] } - for _, info := range res.infos { - if dht_firstCloserThanThird(info.getNodeID(), &res.dest, rinfo.getNodeID()) { + for _, info := range res.Infos { + if dht_firstCloserThanThird(info.getNodeID(), &res.Dest, rinfo.getNodeID()) { t.addToMill(info, info.getNodeID()) } } @@ -335,18 +335,18 @@ func (t *dht) sendReq(req *dhtReq, dest *dhtInfo) { panic("This should never happen") } } - reqsToDest[req.dest] = time.Now() + reqsToDest[req.Dest] = time.Now() } func (t *dht) sendRes(res *dhtRes, req *dhtReq) { // Send a reply for a dhtReq bs := res.encode() - shared := t.core.sessions.getSharedKey(&t.core.boxPriv, &req.key) + shared := t.core.sessions.getSharedKey(&t.core.boxPriv, &req.Key) payload, nonce := boxSeal(shared, bs, nil) p := wire_protoTrafficPacket{ TTL: ^uint64(0), - Coords: req.coords, - ToKey: req.key, + Coords: req.Coords, + ToKey: req.Key, FromKey: t.core.boxPub, Nonce: *nonce, Payload: payload, @@ -403,9 +403,9 @@ func (t *dht) ping(info *dhtInfo, target *NodeID) { loc := t.core.switchTable.getLocator() coords := loc.getCoords() req := dhtReq{ - key: t.core.boxPub, - coords: coords, - dest: *target, + Key: t.core.boxPub, + Coords: coords, + Dest: *target, } info.pings++ info.send = time.Now() diff --git a/src/yggdrasil/router.go b/src/yggdrasil/router.go index 08677ef..75d85d1 100644 --- a/src/yggdrasil/router.go +++ b/src/yggdrasil/router.go @@ -324,7 +324,7 @@ func (r *router) handlePing(bs []byte, fromKey *boxPubKey) { if !ping.decode(bs) { return } - ping.sendPermPub = *fromKey + ping.SendPermPub = *fromKey r.core.sessions.handlePing(&ping) } @@ -337,7 +337,7 @@ func (r *router) handleDHTReq(bs []byte, fromKey *boxPubKey) { if !req.decode(bs) { return } - req.key = *fromKey + req.Key = *fromKey r.core.dht.handleReq(&req) } @@ -346,7 +346,7 @@ func (r *router) handleDHTRes(bs []byte, fromKey *boxPubKey) { if !res.decode(bs) { return } - res.key = *fromKey + res.Key = *fromKey r.core.dht.handleRes(&res) } diff --git a/src/yggdrasil/session.go b/src/yggdrasil/session.go index c88786e..8bbbc33 100644 --- a/src/yggdrasil/session.go +++ b/src/yggdrasil/session.go @@ -38,40 +38,40 @@ type sessionInfo struct { } type sessionPing struct { - sendPermPub boxPubKey // Sender's permanent key - handle handle // Random number to ID session - sendSesPub boxPubKey // Session key to use - coords []byte - tstamp int64 // unix time, but the only real requirement is that it increases - isPong bool - mtu uint16 + SendPermPub boxPubKey // Sender's permanent key + Handle handle // Random number to ID session + SendSesPub boxPubKey // Session key to use + Coords []byte + Tstamp int64 // unix time, but the only real requirement is that it increases + IsPong bool + MTU uint16 } // Returns true if the session was updated, false otherwise func (s *sessionInfo) update(p *sessionPing) bool { - if !(p.tstamp > s.tstamp) { + if !(p.Tstamp > s.tstamp) { // To protect against replay attacks return false } - if p.sendPermPub != s.theirPermPub { + if p.SendPermPub != s.theirPermPub { // Should only happen if two sessions got the same handle // That shouldn't be allowed anyway, but if it happens then let one time out return false } - if p.sendSesPub != s.theirSesPub { - s.theirSesPub = p.sendSesPub - s.theirHandle = p.handle + if p.SendSesPub != s.theirSesPub { + s.theirSesPub = p.SendSesPub + s.theirHandle = p.Handle s.sharedSesKey = *getSharedKey(&s.mySesPriv, &s.theirSesPub) s.theirNonce = boxNonce{} s.nonceMask = 0 } - if p.mtu >= 1280 || p.mtu == 0 { - s.theirMTU = p.mtu + if p.MTU >= 1280 || p.MTU == 0 { + s.theirMTU = p.MTU } - s.coords = append([]byte{}, p.coords...) + s.coords = append([]byte{}, p.Coords...) now := time.Now() s.time = now - s.tstamp = p.tstamp + s.tstamp = p.Tstamp s.init = true return true } @@ -215,12 +215,12 @@ func (ss *sessions) getPing(sinfo *sessionInfo) sessionPing { loc := ss.core.switchTable.getLocator() coords := loc.getCoords() ref := sessionPing{ - sendPermPub: ss.core.boxPub, - handle: sinfo.myHandle, - sendSesPub: sinfo.mySesPub, - tstamp: time.Now().Unix(), - coords: coords, - mtu: sinfo.myMTU, + SendPermPub: ss.core.boxPub, + Handle: sinfo.myHandle, + SendSesPub: sinfo.mySesPub, + Tstamp: time.Now().Unix(), + Coords: coords, + MTU: sinfo.myMTU, } sinfo.myNonce.update() return ref @@ -250,7 +250,7 @@ func (ss *sessions) ping(sinfo *sessionInfo) { func (ss *sessions) sendPingPong(sinfo *sessionInfo, isPong bool) { ping := ss.getPing(sinfo) - ping.isPong = isPong + ping.IsPong = isPong bs := ping.encode() shared := ss.getSharedKey(&ss.core.boxPriv, &sinfo.theirPermPub) payload, nonce := boxSeal(shared, bs, nil) @@ -271,13 +271,13 @@ func (ss *sessions) sendPingPong(sinfo *sessionInfo, isPong bool) { func (ss *sessions) handlePing(ping *sessionPing) { // Get the corresponding session (or create a new session) - sinfo, isIn := ss.getByTheirPerm(&ping.sendPermPub) + sinfo, isIn := ss.getByTheirPerm(&ping.SendPermPub) if !isIn || sinfo.timedout() { if isIn { sinfo.close() } - ss.createSession(&ping.sendPermPub) - sinfo, isIn = ss.getByTheirPerm(&ping.sendPermPub) + ss.createSession(&ping.SendPermPub) + sinfo, isIn = ss.getByTheirPerm(&ping.SendPermPub) if !isIn { panic("This should not happen") } @@ -286,7 +286,7 @@ func (ss *sessions) handlePing(ping *sessionPing) { if !sinfo.update(ping) { /*panic("Should not happen in testing")*/ return } - if !ping.isPong { + if !ping.IsPong { ss.sendPingPong(sinfo, true) } if sinfo.packet != nil { diff --git a/src/yggdrasil/wire.go b/src/yggdrasil/wire.go index 9f2abfa..1f6f422 100644 --- a/src/yggdrasil/wire.go +++ b/src/yggdrasil/wire.go @@ -405,19 +405,19 @@ func (p *wire_linkProtoTrafficPacket) decode(bs []byte) bool { func (p *sessionPing) encode() []byte { var pTypeVal uint64 - if p.isPong { + if p.IsPong { pTypeVal = wire_SessionPong } else { pTypeVal = wire_SessionPing } bs := wire_encode_uint64(pTypeVal) //p.sendPermPub used in top level (crypto), so skipped here - bs = append(bs, p.handle[:]...) - bs = append(bs, p.sendSesPub[:]...) - bs = append(bs, wire_encode_uint64(wire_intToUint(p.tstamp))...) - coords := wire_encode_coords(p.coords) + bs = append(bs, p.Handle[:]...) + bs = append(bs, p.SendSesPub[:]...) + bs = append(bs, wire_encode_uint64(wire_intToUint(p.Tstamp))...) + coords := wire_encode_coords(p.Coords) bs = append(bs, coords...) - bs = append(bs, wire_encode_uint64(uint64(p.mtu))...) + bs = append(bs, wire_encode_uint64(uint64(p.MTU))...) return bs } @@ -431,32 +431,32 @@ func (p *sessionPing) decode(bs []byte) bool { case pType != wire_SessionPing && pType != wire_SessionPong: return false //p.sendPermPub used in top level (crypto), so skipped here - case !wire_chop_slice(p.handle[:], &bs): +case !wire_chop_slice(p.Handle[:], &bs): return false - case !wire_chop_slice(p.sendSesPub[:], &bs): + case !wire_chop_slice(p.SendSesPub[:], &bs): return false case !wire_chop_uint64(&tstamp, &bs): return false - case !wire_chop_coords(&p.coords, &bs): + case !wire_chop_coords(&p.Coords, &bs): return false case !wire_chop_uint64(&mtu, &bs): mtu = 1280 } - p.tstamp = wire_intFromUint(tstamp) + p.Tstamp = wire_intFromUint(tstamp) if pType == wire_SessionPong { - p.isPong = true + p.IsPong = true } - p.mtu = uint16(mtu) + p.MTU = uint16(mtu) return true } //////////////////////////////////////////////////////////////////////////////// func (r *dhtReq) encode() []byte { - coords := wire_encode_coords(r.coords) + coords := wire_encode_coords(r.Coords) bs := wire_encode_uint64(wire_DHTLookupRequest) bs = append(bs, coords...) - bs = append(bs, r.dest[:]...) + bs = append(bs, r.Dest[:]...) return bs } @@ -467,9 +467,9 @@ func (r *dhtReq) decode(bs []byte) bool { return false case pType != wire_DHTLookupRequest: return false - case !wire_chop_coords(&r.coords, &bs): + case !wire_chop_coords(&r.Coords, &bs): return false - case !wire_chop_slice(r.dest[:], &bs): + case !wire_chop_slice(r.Dest[:], &bs): return false default: return true @@ -477,11 +477,11 @@ func (r *dhtReq) decode(bs []byte) bool { } func (r *dhtRes) encode() []byte { - coords := wire_encode_coords(r.coords) + coords := wire_encode_coords(r.Coords) bs := wire_encode_uint64(wire_DHTLookupResponse) bs = append(bs, coords...) - bs = append(bs, r.dest[:]...) - for _, info := range r.infos { + bs = append(bs, r.Dest[:]...) + for _, info := range r.Infos { coords = wire_encode_coords(info.coords) bs = append(bs, info.key[:]...) bs = append(bs, coords...) @@ -496,9 +496,9 @@ func (r *dhtRes) decode(bs []byte) bool { return false case pType != wire_DHTLookupResponse: return false - case !wire_chop_coords(&r.coords, &bs): + case !wire_chop_coords(&r.Coords, &bs): return false - case !wire_chop_slice(r.dest[:], &bs): + case !wire_chop_slice(r.Dest[:], &bs): return false } for len(bs) > 0 { @@ -509,7 +509,7 @@ func (r *dhtRes) decode(bs []byte) bool { case !wire_chop_coords(&info.coords, &bs): return false } - r.infos = append(r.infos, &info) + r.Infos = append(r.Infos, &info) } return true }