]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/cypherpunks.ru/govpn/egd.go
Fix copyright years
[govpn.git] / src / cypherpunks.ru / govpn / egd.go
index 04ec87b9678cf2dfa10c576c84bfc66bbe4ef493..ac23477628ab1930c9b913c3b1faf2368db3a735 100644 (file)
@@ -1,6 +1,6 @@
 /*
 GoVPN -- simple secure free software virtual private network daemon
-Copyright (C) 2014-2016 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2014-2017 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
@@ -20,37 +20,37 @@ package govpn
 
 import (
        "crypto/rand"
-       "errors"
        "io"
        "net"
-)
 
-var (
-       Rand io.Reader = rand.Reader
+       "github.com/Sirupsen/logrus"
+       "github.com/pkg/errors"
 )
 
+// Rand is a source of entropy
+var Rand = rand.Reader
+
+// EGDRand is a EGD (Entropy Gathering Daemon) source of entropy
 type EGDRand string
 
 // Read n bytes from EGD, blocking mode.
 func (egdPath EGDRand) Read(b []byte) (int, error) {
        conn, err := net.Dial("unix", string(egdPath))
        if err != nil {
-               return 0, err
+               return 0, errors.Wrapf(err, "net.Dial unix:%q", string(egdPath))
        }
-       conn.Write([]byte{0x02, byte(len(b))})
-       read, err := conn.Read(b)
+       defer CloseLog(conn, logger, logrus.Fields{"func": logFuncPrefix + "EGDRand.Read"})
+       n, err := conn.Write([]byte{0x02, byte(len(b))})
        if err != nil {
-               conn.Close()
-               return read, err
+               return 0, errors.Wrapf(err, "conn.Write unix:%q", string(egdPath))
        }
-       if read != len(b) {
-               conn.Close()
-               return read, errors.New("Got less bytes than expected from EGD")
+       if n, err = io.ReadFull(conn, b); err != nil {
+               return 0, errors.Wrapf(err, wrapIoReadFull, string(egdPath))
        }
-       conn.Close()
-       return read, nil
+       return n, nil
 }
 
+// EGDInit sets random source to a EGD socket
 func EGDInit(path string) {
        Rand = EGDRand(path)
 }