5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-26 08:21:36 +00:00

Export fields of sessionPing, dhtReq, dhtRes

This commit is contained in:
Neil Alexander 2018-06-02 22:19:42 +01:00
parent 49af65296d
commit bbdcee1015
4 changed files with 81 additions and 81 deletions

View File

@ -51,16 +51,16 @@ type bucket struct {
} }
type dhtReq struct { type dhtReq struct {
key boxPubKey // Key of whoever asked Key boxPubKey // Key of whoever asked
coords []byte // Coords of whoever asked Coords []byte // Coords of whoever asked
dest NodeID // NodeID they're asking about Dest NodeID // NodeID they're asking about
} }
type dhtRes struct { type dhtRes struct {
key boxPubKey // key to respond to Key boxPubKey // key to respond to
coords []byte // coords to respond to Coords []byte // coords to respond to
dest NodeID Dest NodeID
infos []*dhtInfo // response Infos []*dhtInfo // response
} }
type dht_rumor struct { type dht_rumor struct {
@ -90,33 +90,33 @@ func (t *dht) handleReq(req *dhtReq) {
loc := t.core.switchTable.getLocator() loc := t.core.switchTable.getLocator()
coords := loc.getCoords() coords := loc.getCoords()
res := dhtRes{ res := dhtRes{
key: t.core.boxPub, Key: t.core.boxPub,
coords: coords, Coords: coords,
dest: req.dest, Dest: req.Dest,
infos: t.lookup(&req.dest, false), Infos: t.lookup(&req.Dest, false),
} }
t.sendRes(&res, req) t.sendRes(&res, req)
// Also (possibly) add them to our DHT // Also (possibly) add them to our DHT
info := dhtInfo{ info := dhtInfo{
key: req.key, key: req.Key,
coords: req.coords, coords: req.Coords,
} }
t.insertIfNew(&info, false) // This seems DoSable (we just trust their 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... //if req.dest != t.nodeID { t.ping(&info, info.getNodeID()) } // Or spam...
} }
func (t *dht) handleRes(res *dhtRes) { func (t *dht) handleRes(res *dhtRes) {
reqs, isIn := t.reqs[res.key] reqs, isIn := t.reqs[res.Key]
if !isIn { if !isIn {
return return
} }
_, isIn = reqs[res.dest] _, isIn = reqs[res.Dest]
if !isIn { if !isIn {
return return
} }
rinfo := dhtInfo{ rinfo := dhtInfo{
key: res.key, key: res.Key,
coords: res.coords, coords: res.Coords,
send: time.Now(), // Technically wrong but should be OK... send: time.Now(), // Technically wrong but should be OK...
recv: time.Now(), recv: time.Now(),
} }
@ -138,15 +138,15 @@ func (t *dht) handleRes(res *dhtRes) {
} }
// Insert into table // Insert into table
t.insert(&rinfo, false) t.insert(&rinfo, false)
if res.dest == *rinfo.getNodeID() { if res.Dest == *rinfo.getNodeID() {
return return
} // No infinite recursions } // No infinite recursions
if len(res.infos) > dht_lookup_size { if len(res.Infos) > dht_lookup_size {
// Ignore any "extra" lookup results // Ignore any "extra" lookup results
res.infos = res.infos[:dht_lookup_size] res.Infos = res.Infos[:dht_lookup_size]
} }
for _, info := range res.infos { for _, info := range res.Infos {
if dht_firstCloserThanThird(info.getNodeID(), &res.dest, rinfo.getNodeID()) { if dht_firstCloserThanThird(info.getNodeID(), &res.Dest, rinfo.getNodeID()) {
t.addToMill(info, info.getNodeID()) t.addToMill(info, info.getNodeID())
} }
} }
@ -335,18 +335,18 @@ func (t *dht) sendReq(req *dhtReq, dest *dhtInfo) {
panic("This should never happen") panic("This should never happen")
} }
} }
reqsToDest[req.dest] = time.Now() reqsToDest[req.Dest] = time.Now()
} }
func (t *dht) sendRes(res *dhtRes, req *dhtReq) { func (t *dht) sendRes(res *dhtRes, req *dhtReq) {
// Send a reply for a dhtReq // Send a reply for a dhtReq
bs := res.encode() 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) payload, nonce := boxSeal(shared, bs, nil)
p := wire_protoTrafficPacket{ p := wire_protoTrafficPacket{
TTL: ^uint64(0), TTL: ^uint64(0),
Coords: req.coords, Coords: req.Coords,
ToKey: req.key, ToKey: req.Key,
FromKey: t.core.boxPub, FromKey: t.core.boxPub,
Nonce: *nonce, Nonce: *nonce,
Payload: payload, Payload: payload,
@ -403,9 +403,9 @@ func (t *dht) ping(info *dhtInfo, target *NodeID) {
loc := t.core.switchTable.getLocator() loc := t.core.switchTable.getLocator()
coords := loc.getCoords() coords := loc.getCoords()
req := dhtReq{ req := dhtReq{
key: t.core.boxPub, Key: t.core.boxPub,
coords: coords, Coords: coords,
dest: *target, Dest: *target,
} }
info.pings++ info.pings++
info.send = time.Now() info.send = time.Now()

View File

@ -324,7 +324,7 @@ func (r *router) handlePing(bs []byte, fromKey *boxPubKey) {
if !ping.decode(bs) { if !ping.decode(bs) {
return return
} }
ping.sendPermPub = *fromKey ping.SendPermPub = *fromKey
r.core.sessions.handlePing(&ping) r.core.sessions.handlePing(&ping)
} }
@ -337,7 +337,7 @@ func (r *router) handleDHTReq(bs []byte, fromKey *boxPubKey) {
if !req.decode(bs) { if !req.decode(bs) {
return return
} }
req.key = *fromKey req.Key = *fromKey
r.core.dht.handleReq(&req) r.core.dht.handleReq(&req)
} }
@ -346,7 +346,7 @@ func (r *router) handleDHTRes(bs []byte, fromKey *boxPubKey) {
if !res.decode(bs) { if !res.decode(bs) {
return return
} }
res.key = *fromKey res.Key = *fromKey
r.core.dht.handleRes(&res) r.core.dht.handleRes(&res)
} }

View File

@ -38,40 +38,40 @@ type sessionInfo struct {
} }
type sessionPing struct { type sessionPing struct {
sendPermPub boxPubKey // Sender's permanent key SendPermPub boxPubKey // Sender's permanent key
handle handle // Random number to ID session Handle handle // Random number to ID session
sendSesPub boxPubKey // Session key to use SendSesPub boxPubKey // Session key to use
coords []byte Coords []byte
tstamp int64 // unix time, but the only real requirement is that it increases Tstamp int64 // unix time, but the only real requirement is that it increases
isPong bool IsPong bool
mtu uint16 MTU uint16
} }
// Returns true if the session was updated, false otherwise // Returns true if the session was updated, false otherwise
func (s *sessionInfo) update(p *sessionPing) bool { func (s *sessionInfo) update(p *sessionPing) bool {
if !(p.tstamp > s.tstamp) { if !(p.Tstamp > s.tstamp) {
// To protect against replay attacks // To protect against replay attacks
return false return false
} }
if p.sendPermPub != s.theirPermPub { if p.SendPermPub != s.theirPermPub {
// Should only happen if two sessions got the same handle // 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 // That shouldn't be allowed anyway, but if it happens then let one time out
return false return false
} }
if p.sendSesPub != s.theirSesPub { if p.SendSesPub != s.theirSesPub {
s.theirSesPub = p.sendSesPub s.theirSesPub = p.SendSesPub
s.theirHandle = p.handle s.theirHandle = p.Handle
s.sharedSesKey = *getSharedKey(&s.mySesPriv, &s.theirSesPub) s.sharedSesKey = *getSharedKey(&s.mySesPriv, &s.theirSesPub)
s.theirNonce = boxNonce{} s.theirNonce = boxNonce{}
s.nonceMask = 0 s.nonceMask = 0
} }
if p.mtu >= 1280 || p.mtu == 0 { if p.MTU >= 1280 || p.MTU == 0 {
s.theirMTU = p.mtu s.theirMTU = p.MTU
} }
s.coords = append([]byte{}, p.coords...) s.coords = append([]byte{}, p.Coords...)
now := time.Now() now := time.Now()
s.time = now s.time = now
s.tstamp = p.tstamp s.tstamp = p.Tstamp
s.init = true s.init = true
return true return true
} }
@ -215,12 +215,12 @@ func (ss *sessions) getPing(sinfo *sessionInfo) sessionPing {
loc := ss.core.switchTable.getLocator() loc := ss.core.switchTable.getLocator()
coords := loc.getCoords() coords := loc.getCoords()
ref := sessionPing{ ref := sessionPing{
sendPermPub: ss.core.boxPub, SendPermPub: ss.core.boxPub,
handle: sinfo.myHandle, Handle: sinfo.myHandle,
sendSesPub: sinfo.mySesPub, SendSesPub: sinfo.mySesPub,
tstamp: time.Now().Unix(), Tstamp: time.Now().Unix(),
coords: coords, Coords: coords,
mtu: sinfo.myMTU, MTU: sinfo.myMTU,
} }
sinfo.myNonce.update() sinfo.myNonce.update()
return ref return ref
@ -250,7 +250,7 @@ func (ss *sessions) ping(sinfo *sessionInfo) {
func (ss *sessions) sendPingPong(sinfo *sessionInfo, isPong bool) { func (ss *sessions) sendPingPong(sinfo *sessionInfo, isPong bool) {
ping := ss.getPing(sinfo) ping := ss.getPing(sinfo)
ping.isPong = isPong ping.IsPong = isPong
bs := ping.encode() bs := ping.encode()
shared := ss.getSharedKey(&ss.core.boxPriv, &sinfo.theirPermPub) shared := ss.getSharedKey(&ss.core.boxPriv, &sinfo.theirPermPub)
payload, nonce := boxSeal(shared, bs, nil) payload, nonce := boxSeal(shared, bs, nil)
@ -271,13 +271,13 @@ func (ss *sessions) sendPingPong(sinfo *sessionInfo, isPong bool) {
func (ss *sessions) handlePing(ping *sessionPing) { func (ss *sessions) handlePing(ping *sessionPing) {
// Get the corresponding session (or create a new session) // 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.timedout() {
if isIn { if isIn {
sinfo.close() sinfo.close()
} }
ss.createSession(&ping.sendPermPub) ss.createSession(&ping.SendPermPub)
sinfo, isIn = ss.getByTheirPerm(&ping.sendPermPub) sinfo, isIn = ss.getByTheirPerm(&ping.SendPermPub)
if !isIn { if !isIn {
panic("This should not happen") 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")*/ if !sinfo.update(ping) { /*panic("Should not happen in testing")*/
return return
} }
if !ping.isPong { if !ping.IsPong {
ss.sendPingPong(sinfo, true) ss.sendPingPong(sinfo, true)
} }
if sinfo.packet != nil { if sinfo.packet != nil {

View File

@ -405,19 +405,19 @@ func (p *wire_linkProtoTrafficPacket) decode(bs []byte) bool {
func (p *sessionPing) encode() []byte { func (p *sessionPing) encode() []byte {
var pTypeVal uint64 var pTypeVal uint64
if p.isPong { if p.IsPong {
pTypeVal = wire_SessionPong pTypeVal = wire_SessionPong
} else { } else {
pTypeVal = wire_SessionPing pTypeVal = wire_SessionPing
} }
bs := wire_encode_uint64(pTypeVal) bs := wire_encode_uint64(pTypeVal)
//p.sendPermPub used in top level (crypto), so skipped here //p.sendPermPub used in top level (crypto), so skipped here
bs = append(bs, p.handle[:]...) bs = append(bs, p.Handle[:]...)
bs = append(bs, p.sendSesPub[:]...) bs = append(bs, p.SendSesPub[:]...)
bs = append(bs, wire_encode_uint64(wire_intToUint(p.tstamp))...) bs = append(bs, wire_encode_uint64(wire_intToUint(p.Tstamp))...)
coords := wire_encode_coords(p.coords) coords := wire_encode_coords(p.Coords)
bs = append(bs, 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 return bs
} }
@ -431,32 +431,32 @@ func (p *sessionPing) decode(bs []byte) bool {
case pType != wire_SessionPing && pType != wire_SessionPong: case pType != wire_SessionPing && pType != wire_SessionPong:
return false return false
//p.sendPermPub used in top level (crypto), so skipped here //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 return false
case !wire_chop_slice(p.sendSesPub[:], &bs): case !wire_chop_slice(p.SendSesPub[:], &bs):
return false return false
case !wire_chop_uint64(&tstamp, &bs): case !wire_chop_uint64(&tstamp, &bs):
return false return false
case !wire_chop_coords(&p.coords, &bs): case !wire_chop_coords(&p.Coords, &bs):
return false return false
case !wire_chop_uint64(&mtu, &bs): case !wire_chop_uint64(&mtu, &bs):
mtu = 1280 mtu = 1280
} }
p.tstamp = wire_intFromUint(tstamp) p.Tstamp = wire_intFromUint(tstamp)
if pType == wire_SessionPong { if pType == wire_SessionPong {
p.isPong = true p.IsPong = true
} }
p.mtu = uint16(mtu) p.MTU = uint16(mtu)
return true return true
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
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, coords...) bs = append(bs, coords...)
bs = append(bs, r.dest[:]...) bs = append(bs, r.Dest[:]...)
return bs return bs
} }
@ -467,9 +467,9 @@ 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_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):
return false return false
default: default:
return true return true
@ -477,11 +477,11 @@ 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, 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 {
coords = wire_encode_coords(info.coords) coords = wire_encode_coords(info.coords)
bs = append(bs, info.key[:]...) bs = append(bs, info.key[:]...)
bs = append(bs, coords...) bs = append(bs, coords...)
@ -496,9 +496,9 @@ 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_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):
return false return false
} }
for len(bs) > 0 { for len(bs) > 0 {
@ -509,7 +509,7 @@ func (r *dhtRes) decode(bs []byte) bool {
case !wire_chop_coords(&info.coords, &bs): case !wire_chop_coords(&info.coords, &bs):
return false return false
} }
r.infos = append(r.infos, &info) r.Infos = append(r.Infos, &info)
} }
return true return true
} }