X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Fcypherpunks.ru%2Fgovpn%2Fegd.go;fp=src%2Fcypherpunks.ru%2Fgovpn%2Fegd.go;h=04ec87b9678cf2dfa10c576c84bfc66bbe4ef493;hb=cecb63f12f4a9f523276a0c19c7feb7437c7f53a;hp=0000000000000000000000000000000000000000;hpb=5123d4cd2b5cfbbba1112710ce29d3d85a3b3ef9;p=govpn.git diff --git a/src/cypherpunks.ru/govpn/egd.go b/src/cypherpunks.ru/govpn/egd.go new file mode 100644 index 0000000..04ec87b --- /dev/null +++ b/src/cypherpunks.ru/govpn/egd.go @@ -0,0 +1,56 @@ +/* +GoVPN -- simple secure free software virtual private network daemon +Copyright (C) 2014-2016 Sergey Matveev + +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 +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package govpn + +import ( + "crypto/rand" + "errors" + "io" + "net" +) + +var ( + Rand io.Reader = rand.Reader +) + +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 + } + conn.Write([]byte{0x02, byte(len(b))}) + read, err := conn.Read(b) + if err != nil { + conn.Close() + return read, err + } + if read != len(b) { + conn.Close() + return read, errors.New("Got less bytes than expected from EGD") + } + conn.Close() + return read, nil +} + +func EGDInit(path string) { + Rand = EGDRand(path) +}