1
/* rndegd.c - interface to the EGD
2
* Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4
* This file is part of GnuPG.
6
* GnuPG is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* GnuPG is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
33
#include <sys/types.h>
34
#include <sys/socket.h>
39
#include "algorithms.h"
45
#define offsetof(type, member) ((size_t) &((type *)0)->member)
48
static int egd_socket = -1;
51
do_write( int fd, void *buf, size_t nbytes )
53
size_t nleft = nbytes;
57
nwritten = write( fd, buf, nleft);
64
buf = (char*)buf + nwritten;
70
do_read( int fd, void *buf, size_t nbytes )
77
n = read(fd, (char*)buf + nread, nbytes );
78
} while( n == -1 && errno == EINTR );
80
return nread? nread:-1;
82
/* EGD probably died. */
92
/* Connect to the EGD and return the file descriptor. Return -1 on
93
error. With NOFAIL set to true, silently fail and return the
94
error, otherwise print an error message and die. */
96
rndegd_connect_socket (int nofail)
99
const char *bname = NULL;
101
struct sockaddr_un addr;
104
if (egd_socket != -1)
110
#ifdef EGD_SOCKET_NAME
111
bname = EGD_SOCKET_NAME;
113
if ( !bname || !*bname )
116
if ( *bname == '=' && bname[1] )
117
name = make_filename( g10_opt_homedir, bname+1 , NULL );
119
name = make_filename( bname , NULL );
121
if ( strlen(name)+1 >= sizeof addr.sun_path )
122
g10_log_fatal ("EGD socketname is too long\n");
124
memset( &addr, 0, sizeof addr );
125
addr.sun_family = AF_UNIX;
126
strcpy( addr.sun_path, name );
127
addr_len = (offsetof( struct sockaddr_un, sun_path )
128
+ strlen( addr.sun_path ));
130
fd = socket(AF_UNIX, SOCK_STREAM, 0);
131
if (fd == -1 && !nofail)
132
g10_log_fatal("can't create unix domain socket: %s\n",
134
else if (connect (fd, (struct sockaddr*)&addr, addr_len) == -1)
137
g10_log_fatal("can't connect to `%s': %s\n",
138
name, strerror(errno) );
150
* Note: we always use the highest level.
151
* TO boost the performance we may want to add some
152
* additional code for level 1
154
* Using a level of 0 should never block and better add nothing
155
* to the pool. So this is just a dummy for EGD.
158
rndegd_gather_random( void (*add)(const void*, size_t, int), int requester,
159
size_t length, int level )
173
if (fd == -1 || do_restart)
174
fd = rndegd_connect_socket (0);
178
nbytes = length < 255? length : 255;
179
/* first time we do it with a non blocking request */
180
buffer[0] = 1; /* non blocking */
182
if( do_write( fd, buffer, 2 ) == -1 )
183
g10_log_fatal("can't write to the EGD: %s\n", strerror(errno) );
184
n = do_read( fd, buffer, 1 );
186
g10_log_error("read error on EGD: %s\n", strerror(errno));
192
n = do_read( fd, buffer, n );
194
g10_log_error("read error on EGD: %s\n", strerror(errno));
198
(*add)( buffer, n, requester );
204
_("Please wait, entropy is being gathered. Do some work if it would\n"
205
"keep you from getting bored, because it will improve the quality\n"
206
"of the entropy.\n") );
209
nbytes = length < 255? length : 255;
211
buffer[0] = 2; /* blocking */
213
if( do_write( fd, buffer, 2 ) == -1 )
214
g10_log_fatal("can't write to the EGD: %s\n", strerror(errno) );
215
n = do_read( fd, buffer, nbytes );
217
g10_log_error("read error on EGD: %s\n", strerror(errno));
221
(*add)( buffer, n, requester );
224
memset(buffer, 0, sizeof(buffer) );
226
return 0; /* success */
229
#endif /*USE_RNDEGD*/