mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-22 21:10:29 +00:00
Export fields of sessionPing, dhtReq, dhtRes
This commit is contained in:
parent
49af65296d
commit
bbdcee1015
@ -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()
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user