]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/cypherpunks.ru/govpn/egd.go
Fix copyright years
[govpn.git] / src / cypherpunks.ru / govpn / egd.go
index f54c1168d11e0ce4287090e0079678414443316a..ac23477628ab1930c9b913c3b1faf2368db3a735 100644 (file)
@@ -22,25 +22,35 @@ import (
        "crypto/rand"
        "io"
        "net"
-)
 
-var (
-       Rand = 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))
+       }
+       defer CloseLog(conn, logger, logrus.Fields{"func": logFuncPrefix + "EGDRand.Read"})
+       n, err := conn.Write([]byte{0x02, byte(len(b))})
+       if err != nil {
+               return 0, errors.Wrapf(err, "conn.Write unix:%q", string(egdPath))
+       }
+       if n, err = io.ReadFull(conn, b); err != nil {
+               return 0, errors.Wrapf(err, wrapIoReadFull, string(egdPath))
        }
-       defer conn.Close()
-       conn.Write([]byte{0x02, byte(len(b))})
-       return io.ReadFull(conn, b)
+       return n, nil
 }
 
+// EGDInit sets random source to a EGD socket
 func EGDInit(path string) {
        Rand = EGDRand(path)
 }