]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/govpn/identify.go
Replace many panic() with Fatalln less verbose and scary printing
[govpn.git] / src / govpn / identify.go
index 4363c58d39fd9bd001b8aaae73cab6c8e2b93e81..663afe73599bf14b8fc40acf7e2b738c9ae6d7e6 100644 (file)
@@ -21,6 +21,7 @@ package govpn
 import (
        "crypto/subtle"
        "encoding/hex"
+       "errors"
        "io/ioutil"
        "log"
        "os"
@@ -112,8 +113,8 @@ func (cc cipherCache) refresh() {
        }
        available := make(map[PeerId]bool)
        for _, peerId := range peerIds {
-               id := IDDecode(peerId)
-               if id == nil {
+               id, err := IDDecode(peerId)
+               if err != nil {
                        continue
                }
                available[*id] = true
@@ -220,17 +221,16 @@ func (id *PeerId) Conf() *PeerConf {
 
 // Decode identification string.
 // It must be 32 hexadecimal characters long.
-// If it is not the valid one, then return nil.
-func IDDecode(raw string) *PeerId {
+func IDDecode(raw string) (*PeerId, error) {
        if len(raw) != IDSize*2 {
-               return nil
+               return nil, errors.New("ID must be 32 characters long")
        }
        idDecoded, err := hex.DecodeString(raw)
        if err != nil {
-               return nil
+               return nil, errors.New("ID must contain hexadecimal characters only")
        }
        idP := new([IDSize]byte)
        copy(idP[:], idDecoded)
        id := PeerId(*idP)
-       return &id
+       return &id, nil
 }