]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/govpn/identify.go
Lock-values are better passed as a pointer
[govpn.git] / src / govpn / identify.go
index a0b7d22f65de59d0840de73f8b4efbb8be6626ca..ed55e0dce6148b540878bf502b821b88d848cd9c 100644 (file)
@@ -1,6 +1,6 @@
 /*
 GoVPN -- simple secure free software virtual private network daemon
-Copyright (C) 2014-2015 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2014-2016 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -21,7 +21,6 @@ package govpn
 import (
        "crypto/subtle"
        "encoding/hex"
-       "errors"
        "log"
        "sync"
 
@@ -38,20 +37,8 @@ func (id PeerId) String() string {
        return hex.EncodeToString(id[:])
 }
 
-// Decode identification string.
-// It must be 32 hexadecimal characters long.
-func IDDecode(raw string) (*PeerId, error) {
-       if len(raw) != IDSize*2 {
-               return nil, errors.New("ID must be 32 characters long")
-       }
-       idDecoded, err := hex.DecodeString(raw)
-       if err != nil {
-               return nil, errors.New("ID must contain hexadecimal characters only")
-       }
-       idP := new([IDSize]byte)
-       copy(idP[:], idDecoded)
-       id := PeerId(*idP)
-       return &id, nil
+func (id PeerId) MarshalJSON() ([]byte, error) {
+       return []byte(`"` + id.String() + `"`), nil
 }
 
 type CipherCache struct {
@@ -59,14 +46,14 @@ type CipherCache struct {
        l sync.RWMutex
 }
 
-func NewCipherCache(peerIds []PeerId) CipherCache {
+func NewCipherCache(peerIds []PeerId) *CipherCache {
        cc := CipherCache{c: make(map[PeerId]*xtea.Cipher, len(peerIds))}
        cc.Update(peerIds)
-       return cc
+       return &cc
 }
 
 // Remove disappeared keys, add missing ones with initialized ciphers.
-func (cc CipherCache) Update(peerIds []PeerId) {
+func (cc *CipherCache) Update(peerIds []PeerId) {
        available := make(map[PeerId]struct{})
        for _, peerId := range peerIds {
                available[peerId] = struct{}{}
@@ -94,7 +81,7 @@ func (cc CipherCache) Update(peerIds []PeerId) {
 // Try to find peer's identity (that equals to an encryption key)
 // by taking first blocksize sized bytes from data at the beginning
 // as plaintext and last bytes as cyphertext.
-func (cc CipherCache) Find(data []byte) *PeerId {
+func (cc *CipherCache) Find(data []byte) *PeerId {
        if len(data) < xtea.BlockSize*2 {
                return nil
        }