2 * Copyright 2000-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/opensslconf.h>
12 NON_EMPTY_TRANSLATION_UNIT
15 # include <openssl/crypto.h>
16 # include <openssl/e_os2.h>
17 # include <openssl/rand.h>
23 # if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_VOS) || defined(OPENSSL_SYS_UEFI)
24 int RAND_query_egd_bytes(const char *path
, unsigned char *buf
, int bytes
)
29 int RAND_egd(const char *path
)
34 int RAND_egd_bytes(const char *path
, int bytes
)
41 # include <openssl/opensslconf.h>
42 # include OPENSSL_UNISTD
44 # include <sys/types.h>
45 # include <sys/socket.h>
47 # ifdef OPENSSL_SYS_VXWORKS
48 # include <streams/un.h>
54 short sun_family
; /* AF_UNIX */
55 char sun_path
[108]; /* path name (gag) */
57 # endif /* NO_SYS_UN_H */
61 int RAND_query_egd_bytes(const char *path
, unsigned char *buf
, int bytes
)
64 struct sockaddr_un addr
;
65 int mybuffer
, ret
= -1, i
, numbytes
, fd
;
66 unsigned char tempbuf
[255];
68 if (bytes
> (int)sizeof(tempbuf
))
72 memset(&addr
, 0, sizeof(addr
));
73 addr
.sun_family
= AF_UNIX
;
74 if (strlen(path
) >= sizeof(addr
.sun_path
))
76 strcpy(addr
.sun_path
, path
);
77 i
= offsetof(struct sockaddr_un
, sun_path
) + strlen(path
);
78 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
79 if (fd
== -1 || (fp
= fdopen(fd
, "r+")) == NULL
)
85 if (connect(fd
, (struct sockaddr
*)&addr
, i
) == 0)
104 /* No error, try again */
112 /* Make request, see how many bytes we can get back. */
115 if (fwrite(tempbuf
, sizeof(char), 2, fp
) != 2 || fflush(fp
) == EOF
)
117 if (fread(tempbuf
, sizeof(char), 1, fp
) != 1 || tempbuf
[0] == 0)
119 numbytes
= tempbuf
[0];
121 /* Which buffer are we using? */
122 mybuffer
= buf
== NULL
;
127 i
= fread(buf
, sizeof(char), numbytes
, fp
);
132 RAND_add(tempbuf
, i
, i
);
140 int RAND_egd_bytes(const char *path
, int bytes
)
144 num
= RAND_query_egd_bytes(path
, NULL
, bytes
);
147 if (RAND_status() != 1)
152 int RAND_egd(const char *path
)
154 return RAND_egd_bytes(path
, 255);