mirror of
https://github.com/logos-messaging/go-waku-rendezvous.git
synced 2026-01-07 15:43:09 +00:00
implement binary packing details
This commit is contained in:
parent
c703d37aa2
commit
ae10cc6b9f
116
db.go
116
db.go
@ -1,10 +1,13 @@
|
|||||||
package rendezvous
|
package rendezvous
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"crypto/sha256"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -242,7 +245,7 @@ func (db *DB) Discover(ns string, cookie []byte, limit int) ([]RegistrationRecor
|
|||||||
)
|
)
|
||||||
|
|
||||||
if cookie != nil {
|
if cookie != nil {
|
||||||
counter, err = cookieToCounter(cookie)
|
counter, err = unpackCookie(cookie)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error unpacking cookie: %s", err.Error())
|
log.Errorf("error unpacking cookie: %s", err.Error())
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
@ -293,15 +296,15 @@ func (db *DB) Discover(ns string, cookie []byte, limit int) ([]RegistrationRecor
|
|||||||
log.Errorf("error decoding peer id: %s", err.Error())
|
log.Errorf("error decoding peer id: %s", err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
reg.Id = p
|
|
||||||
|
|
||||||
addrs, err = unpackAddrs(raddrs)
|
addrs, err := unpackAddrs(raddrs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error unpacking address: %s", err.Error())
|
log.Errorf("error unpacking address: %s", err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
reg.Addrs = addrs
|
|
||||||
|
|
||||||
|
reg.Id = p
|
||||||
|
reg.Addrs = addrs
|
||||||
reg.Ttl = int(expire - now)
|
reg.Ttl = int(expire - now)
|
||||||
|
|
||||||
if ns == "" {
|
if ns == "" {
|
||||||
@ -317,15 +320,14 @@ func (db *DB) Discover(ns string, cookie []byte, limit int) ([]RegistrationRecor
|
|||||||
}
|
}
|
||||||
|
|
||||||
if counter > 0 {
|
if counter > 0 {
|
||||||
cookie = counterToCookie(counter, ns, db.nonce)
|
cookie = packCookie(counter, ns, db.nonce)
|
||||||
}
|
}
|
||||||
|
|
||||||
return regs, cookie, nil
|
return regs, cookie, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) ValidCookie(ns string, cookie []byte) bool {
|
func (db *DB) ValidCookie(ns string, cookie []byte) bool {
|
||||||
// XXX
|
return validCookie(cookie, ns, db.nonce)
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) background(ctx context.Context) {
|
func (db *DB) background(ctx context.Context) {
|
||||||
@ -345,21 +347,97 @@ func (db *DB) background(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func packAddrs(addrs [][]byte) []byte {
|
func packAddrs(addrs [][]byte) []byte {
|
||||||
// XXX
|
packlen := 0
|
||||||
return nil
|
for _, addr := range addrs {
|
||||||
|
packlen = packlen + 2 + len(addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
packed := make([]byte, packlen)
|
||||||
|
buf := packed
|
||||||
|
for _, addr := range addrs {
|
||||||
|
binary.BigEndian.PutUint16(buf, uint16(len(addr)))
|
||||||
|
buf = buf[2:]
|
||||||
|
copy(buf, addr)
|
||||||
|
buf = buf[len(addr):]
|
||||||
|
}
|
||||||
|
|
||||||
|
return packed
|
||||||
}
|
}
|
||||||
|
|
||||||
func unpackAddrs(maddrs []byte) ([][]byte, error) {
|
func unpackAddrs(packed []byte) ([][]byte, error) {
|
||||||
// XXX
|
var addrs [][]byte
|
||||||
return nil, errors.New("IMPLEMENTME: unpackAddrs")
|
|
||||||
|
buf := packed
|
||||||
|
for len(buf) > 1 {
|
||||||
|
l := binary.BigEndian.Uint16(buf)
|
||||||
|
buf = buf[2:]
|
||||||
|
if len(buf) < int(l) {
|
||||||
|
return nil, fmt.Errorf("bad packed address: not enough bytes %v %v", packed, buf)
|
||||||
|
}
|
||||||
|
addr := make([]byte, l)
|
||||||
|
copy(addr, buf[:l])
|
||||||
|
buf = buf[l:]
|
||||||
|
addrs = append(addrs, addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(buf) > 0 {
|
||||||
|
return nil, fmt.Errorf("bad packed address: unprocessed bytes: %v %v", packed, buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
return addrs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func cookieToCounter(cookie []byte) (int64, error) {
|
// cookie: counter:SHA256(nonce + ns + counter)
|
||||||
// XXX
|
func packCookie(counter int64, ns string, nonce []byte) []byte {
|
||||||
return 0, errors.New("IMPLEMENTME: cookieToCounter")
|
cbits := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(cbits, uint64(counter))
|
||||||
|
|
||||||
|
hash := sha256.New()
|
||||||
|
_, err := hash.Write(nonce)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
_, err = hash.Write([]byte(ns))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
_, err = hash.Write(cbits)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash.Sum(cbits)
|
||||||
}
|
}
|
||||||
|
|
||||||
func counterToCookie(counter int64, ns string, nonce []byte) []byte {
|
func unpackCookie(cookie []byte) (int64, error) {
|
||||||
// XXX
|
if len(cookie) < 8 {
|
||||||
return nil
|
return 0, fmt.Errorf("bad packed cookie: not enough bytes: %v", cookie)
|
||||||
|
}
|
||||||
|
|
||||||
|
counter := binary.BigEndian.Uint64(cookie[:8])
|
||||||
|
return int64(counter), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func validCookie(cookie []byte, ns string, nonce []byte) bool {
|
||||||
|
if len(cookie) != 40 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
cbits := cookie[:8]
|
||||||
|
hash := sha256.New()
|
||||||
|
_, err := hash.Write(nonce)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
_, err = hash.Write([]byte(ns))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
_, err = hash.Write(cbits)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
hbits := hash.Sum(nil)
|
||||||
|
|
||||||
|
return bytes.Equal(cookie[8:], hbits)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user