2 * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
)
43 # include <sys/types.h>
44 # include <sys/socket.h>
46 # ifdef OPENSSL_SYS_VXWORKS
47 # include <streams/un.h>
53 short sun_family
; /* AF_UNIX */
54 char sun_path
[108]; /* path name (gag) */
56 # endif /* NO_SYS_UN_H */
60 int RAND_query_egd_bytes(const char *path
, unsigned char *buf
, int bytes
)
63 struct sockaddr_un addr
;
64 int mybuffer
, ret
= -1, i
, numbytes
, fd
;
65 unsigned char tempbuf
[255];
67 if (bytes
> (int)sizeof(tempbuf
))
71 memset(&addr
, 0, sizeof(addr
));
72 addr
.sun_family
= AF_UNIX
;
73 if (strlen(path
) >= sizeof(addr
.sun_path
))
75 strcpy(addr
.sun_path
, path
);
76 i
= offsetof(struct sockaddr_un
, sun_path
) + strlen(path
);
77 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
78 if (fd
== -1 || (fp
= fdopen(fd
, "r+")) == NULL
)
84 if (connect(fd
, (struct sockaddr
*)&addr
, i
) == 0)
103 /* No error, try again */
111 /* Make request, see how many bytes we can get back. */
114 if (fwrite(tempbuf
, sizeof(char), 2, fp
) != 2 || fflush(fp
) == EOF
)
116 if (fread(tempbuf
, sizeof(char), 1, fp
) != 1 || tempbuf
[0] == 0)
118 numbytes
= tempbuf
[0];
120 /* Which buffer are we using? */
121 mybuffer
= buf
== NULL
;
126 i
= fread(buf
, sizeof(char), numbytes
, fp
);
131 RAND_add(tempbuf
, i
, i
);
139 int RAND_egd_bytes(const char *path
, int bytes
)
143 num
= RAND_query_egd_bytes(path
, NULL
, bytes
);
146 if (RAND_status() != 1)
151 int RAND_egd(const char *path
)
153 return RAND_egd_bytes(path
, 255);