]> Cypherpunks.ru repositories - govpn.git/blobdiff - src/cypherpunks.ru/govpn/egd.go
Refactor govpn common package
[govpn.git] / src / cypherpunks.ru / govpn / egd.go
index f06411f468eeee0f064656314f9b4dae0a2d6991..647c2c9623177dcdc3d9866d07e35f0c9735bf64 100644 (file)
@@ -1,6 +1,6 @@
 /*
 GoVPN -- simple secure free software virtual private network daemon
-Copyright (C) 2014-2017 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
@@ -22,26 +22,35 @@ import (
        "crypto/rand"
        "io"
        "net"
+
+       "github.com/Sirupsen/logrus"
+       "github.com/pkg/errors"
 )
 
 // Rand is a source of entropy
 var Rand = rand.Reader
 
-// EGDRand is a EGD source of entropy
+// 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 set random source to a EGD socket
+// EGDInit sets random source to a EGD socket
 func EGDInit(path string) {
        Rand = EGDRand(path)
 }