New Makefile structure; added compatibility with Windows + Visual C + nmake.
This commit is contained in:
parent
2f454aad57
commit
3f00688b9d
29 changed files with 2240 additions and 1069 deletions
|
|
@ -28,8 +28,16 @@
|
|||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
/*
|
||||
* Network stuff on Windows requires some specific code.
|
||||
*/
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#pragma comment(lib, "Ws2_32.lib")
|
||||
#endif
|
||||
|
||||
#include "brssl.h"
|
||||
#include "bearssl.h"
|
||||
|
||||
static void
|
||||
usage(void)
|
||||
|
|
@ -53,6 +61,18 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
#ifdef _WIN32
|
||||
{
|
||||
WSADATA wd;
|
||||
int r;
|
||||
|
||||
r = WSAStartup(MAKEWORD(2, 2), &wd);
|
||||
if (r != 0) {
|
||||
fprintf(stderr, "WARNING: network initialisation"
|
||||
" failed (WSAStartup() returned %d)\n", r);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
cmd = argv[1];
|
||||
if (eqstr(cmd, "client")) {
|
||||
if (do_client(argc - 2, argv + 2) < 0) {
|
||||
|
|
|
|||
|
|
@ -475,13 +475,17 @@ const char *find_error_name(int err, const char **comment);
|
|||
* Run a SSL engine, with a socket connected to the peer, and using
|
||||
* stdin/stdout to exchange application data.
|
||||
*
|
||||
* To help with Win32 compatibility, the socket descriptor is provided
|
||||
* as an "unsigned long" value.
|
||||
*
|
||||
* Returned value:
|
||||
* 0 SSL connection closed successfully
|
||||
* x > 0 SSL error "x"
|
||||
* -1 early socket close
|
||||
* -2 stdout was closed, or something failed badly
|
||||
*/
|
||||
int run_ssl_engine(br_ssl_engine_context *eng, int fd, unsigned flags);
|
||||
int run_ssl_engine(br_ssl_engine_context *eng,
|
||||
unsigned long fd, unsigned flags);
|
||||
|
||||
#define RUN_ENGINE_VERBOSE 0x0001 /* enable verbose messages */
|
||||
#define RUN_ENGINE_TRACE 0x0002 /* hex dump of records */
|
||||
|
|
|
|||
|
|
@ -29,6 +29,10 @@
|
|||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
|
@ -37,14 +41,17 @@
|
|||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#define SOCKET int
|
||||
#define INVALID_SOCKET (-1)
|
||||
#endif
|
||||
|
||||
#include "brssl.h"
|
||||
#include "bearssl.h"
|
||||
|
||||
static int
|
||||
host_connect(const char *host, const char *port, int verbose)
|
||||
{
|
||||
struct addrinfo hints, *si, *p;
|
||||
int fd;
|
||||
SOCKET fd;
|
||||
int err;
|
||||
|
||||
memset(&hints, 0, sizeof hints);
|
||||
|
|
@ -54,9 +61,9 @@ host_connect(const char *host, const char *port, int verbose)
|
|||
if (err != 0) {
|
||||
fprintf(stderr, "ERROR: getaddrinfo(): %s\n",
|
||||
gai_strerror(err));
|
||||
return -1;
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
fd = -1;
|
||||
fd = INVALID_SOCKET;
|
||||
for (p = si; p != NULL; p = p->ai_next) {
|
||||
if (verbose) {
|
||||
struct sockaddr *sa;
|
||||
|
|
@ -84,17 +91,21 @@ host_connect(const char *host, const char *port, int verbose)
|
|||
fprintf(stderr, "connecting to: %s\n", tmp);
|
||||
}
|
||||
fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
|
||||
if (fd < 0) {
|
||||
if (fd == INVALID_SOCKET) {
|
||||
if (verbose) {
|
||||
perror("socket()");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (connect(fd, p->ai_addr, p->ai_addrlen) < 0) {
|
||||
if (connect(fd, p->ai_addr, p->ai_addrlen) == INVALID_SOCKET) {
|
||||
if (verbose) {
|
||||
perror("connect()");
|
||||
}
|
||||
#ifdef _WIN32
|
||||
closesocket(fd);
|
||||
#else
|
||||
close(fd);
|
||||
#endif
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
|
|
@ -102,7 +113,7 @@ host_connect(const char *host, const char *port, int verbose)
|
|||
if (p == NULL) {
|
||||
freeaddrinfo(si);
|
||||
fprintf(stderr, "ERROR: failed to connect\n");
|
||||
return -1;
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
freeaddrinfo(si);
|
||||
if (verbose) {
|
||||
|
|
@ -111,9 +122,18 @@ host_connect(const char *host, const char *port, int verbose)
|
|||
|
||||
/*
|
||||
* We make the socket non-blocking, since we are going to use
|
||||
* poll() to organise I/O.
|
||||
* poll() or select() to organise I/O.
|
||||
*/
|
||||
#ifdef _WIN32
|
||||
{
|
||||
u_long arg;
|
||||
|
||||
arg = 1;
|
||||
ioctlsocket(fd, FIONBIO, &arg);
|
||||
}
|
||||
#else
|
||||
fcntl(fd, F_SETFL, O_NONBLOCK);
|
||||
#endif
|
||||
return fd;
|
||||
}
|
||||
|
||||
|
|
@ -494,7 +514,7 @@ do_client(int argc, char *argv[])
|
|||
const char *sni;
|
||||
anchor_list anchors = VEC_INIT;
|
||||
unsigned vmin, vmax;
|
||||
VECTOR(const char *) alpn_names = VEC_INIT;
|
||||
VECTOR(char *) alpn_names = VEC_INIT;
|
||||
cipher_suite *suites;
|
||||
size_t num_suites;
|
||||
uint16_t *suite_ids;
|
||||
|
|
@ -514,7 +534,7 @@ do_client(int argc, char *argv[])
|
|||
size_t minhello_len;
|
||||
int fallback;
|
||||
uint32_t flags;
|
||||
int fd;
|
||||
SOCKET fd;
|
||||
|
||||
retcode = 0;
|
||||
verbose = 1;
|
||||
|
|
@ -539,7 +559,7 @@ do_client(int argc, char *argv[])
|
|||
minhello_len = (size_t)-1;
|
||||
fallback = 0;
|
||||
flags = 0;
|
||||
fd = -1;
|
||||
fd = INVALID_SOCKET;
|
||||
for (i = 0; i < argc; i ++) {
|
||||
const char *arg;
|
||||
|
||||
|
|
@ -1031,7 +1051,8 @@ do_client(int argc, char *argv[])
|
|||
br_ssl_engine_set_all_flags(&cc.eng, flags);
|
||||
if (VEC_LEN(alpn_names) != 0) {
|
||||
br_ssl_engine_set_protocol_names(&cc.eng,
|
||||
&VEC_ELT(alpn_names, 0), VEC_LEN(alpn_names));
|
||||
(const char **)&VEC_ELT(alpn_names, 0),
|
||||
VEC_LEN(alpn_names));
|
||||
}
|
||||
|
||||
if (chain != NULL) {
|
||||
|
|
@ -1055,15 +1076,17 @@ do_client(int argc, char *argv[])
|
|||
br_ssl_client_reset(&cc, sni, 0);
|
||||
|
||||
/*
|
||||
* We need to avoid SIGPIPE.
|
||||
* On Unix systems, we need to avoid SIGPIPE.
|
||||
*/
|
||||
#ifndef _WIN32
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Connect to the peer.
|
||||
*/
|
||||
fd = host_connect(host, port, verbose);
|
||||
if (fd < 0) {
|
||||
if (fd == INVALID_SOCKET) {
|
||||
goto client_exit_error;
|
||||
}
|
||||
|
||||
|
|
@ -1092,8 +1115,12 @@ client_exit:
|
|||
free_certificates(chain, chain_len);
|
||||
free_private_key(sk);
|
||||
xfree(iobuf);
|
||||
if (fd >= 0) {
|
||||
if (fd != INVALID_SOCKET) {
|
||||
#ifdef _WIN32
|
||||
closesocket(fd);
|
||||
#else
|
||||
close(fd);
|
||||
#endif
|
||||
}
|
||||
return retcode;
|
||||
|
||||
|
|
|
|||
100
tools/server.c
100
tools/server.c
|
|
@ -29,6 +29,10 @@
|
|||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
|
@ -37,14 +41,18 @@
|
|||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "brssl.h"
|
||||
#include "bearssl.h"
|
||||
#define SOCKET int
|
||||
#define INVALID_SOCKET (-1)
|
||||
#define SOCKADDR_STORAGE struct sockaddr_storage
|
||||
#endif
|
||||
|
||||
static int
|
||||
#include "brssl.h"
|
||||
|
||||
static SOCKET
|
||||
host_bind(const char *host, const char *port, int verbose)
|
||||
{
|
||||
struct addrinfo hints, *si, *p;
|
||||
int fd;
|
||||
SOCKET fd;
|
||||
int err;
|
||||
|
||||
memset(&hints, 0, sizeof hints);
|
||||
|
|
@ -54,9 +62,9 @@ host_bind(const char *host, const char *port, int verbose)
|
|||
if (err != 0) {
|
||||
fprintf(stderr, "ERROR: getaddrinfo(): %s\n",
|
||||
gai_strerror(err));
|
||||
return -1;
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
fd = -1;
|
||||
fd = INVALID_SOCKET;
|
||||
for (p = si; p != NULL; p = p->ai_next) {
|
||||
struct sockaddr *sa;
|
||||
struct sockaddr_in sa4;
|
||||
|
|
@ -102,21 +110,27 @@ host_bind(const char *host, const char *port, int verbose)
|
|||
fprintf(stderr, "binding to: %s\n", tmp);
|
||||
}
|
||||
fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
|
||||
if (fd < 0) {
|
||||
if (fd == INVALID_SOCKET) {
|
||||
if (verbose) {
|
||||
perror("socket()");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
opt = 1;
|
||||
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt);
|
||||
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
|
||||
(void *)&opt, sizeof opt);
|
||||
opt = 0;
|
||||
setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof opt);
|
||||
setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY,
|
||||
(void *)&opt, sizeof opt);
|
||||
if (bind(fd, sa, sa_len) < 0) {
|
||||
if (verbose) {
|
||||
perror("bind()");
|
||||
}
|
||||
#ifdef _WIN32
|
||||
closesocket(fd);
|
||||
#else
|
||||
close(fd);
|
||||
#endif
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
|
|
@ -124,15 +138,19 @@ host_bind(const char *host, const char *port, int verbose)
|
|||
if (p == NULL) {
|
||||
freeaddrinfo(si);
|
||||
fprintf(stderr, "ERROR: failed to bind\n");
|
||||
return -1;
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
freeaddrinfo(si);
|
||||
if (listen(fd, 5) < 0) {
|
||||
if (verbose) {
|
||||
perror("listen()");
|
||||
}
|
||||
#ifdef _WIN32
|
||||
closesocket(fd);
|
||||
#else
|
||||
close(fd);
|
||||
return -1;
|
||||
#endif
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
if (verbose) {
|
||||
fprintf(stderr, "bound.\n");
|
||||
|
|
@ -140,27 +158,27 @@ host_bind(const char *host, const char *port, int verbose)
|
|||
return fd;
|
||||
}
|
||||
|
||||
static int
|
||||
accept_client(int server_fd, int verbose)
|
||||
static SOCKET
|
||||
accept_client(SOCKET server_fd, int verbose)
|
||||
{
|
||||
int fd;
|
||||
struct sockaddr sa;
|
||||
SOCKADDR_STORAGE sa;
|
||||
socklen_t sa_len;
|
||||
|
||||
sa_len = sizeof sa;
|
||||
fd = accept(server_fd, &sa, &sa_len);
|
||||
if (fd < 0) {
|
||||
fd = accept(server_fd, (struct sockaddr *)&sa, &sa_len);
|
||||
if (fd == INVALID_SOCKET) {
|
||||
if (verbose) {
|
||||
perror("accept()");
|
||||
}
|
||||
return -1;
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
if (verbose) {
|
||||
char tmp[INET6_ADDRSTRLEN + 50];
|
||||
const char *name;
|
||||
|
||||
name = NULL;
|
||||
switch (sa.sa_family) {
|
||||
switch (((struct sockaddr *)&sa)->sa_family) {
|
||||
case AF_INET:
|
||||
name = inet_ntop(AF_INET,
|
||||
&((struct sockaddr_in *)&sa)->sin_addr,
|
||||
|
|
@ -173,8 +191,8 @@ accept_client(int server_fd, int verbose)
|
|||
break;
|
||||
}
|
||||
if (name == NULL) {
|
||||
sprintf(tmp, "<unknown: %lu>",
|
||||
(unsigned long)sa.sa_family);
|
||||
sprintf(tmp, "<unknown: %lu>", (unsigned long)
|
||||
((struct sockaddr *)&sa)->sa_family);
|
||||
name = tmp;
|
||||
}
|
||||
fprintf(stderr, "accepting connection from: %s\n", name);
|
||||
|
|
@ -182,9 +200,18 @@ accept_client(int server_fd, int verbose)
|
|||
|
||||
/*
|
||||
* We make the socket non-blocking, since we are going to use
|
||||
* poll() to organise I/O.
|
||||
* poll() or select() to organise I/O.
|
||||
*/
|
||||
#ifdef _WIN32
|
||||
{
|
||||
u_long arg;
|
||||
|
||||
arg = 1;
|
||||
ioctlsocket(fd, FIONBIO, &arg);
|
||||
}
|
||||
#else
|
||||
fcntl(fd, F_SETFL, O_NONBLOCK);
|
||||
#endif
|
||||
return fd;
|
||||
}
|
||||
|
||||
|
|
@ -606,7 +633,7 @@ do_server(int argc, char *argv[])
|
|||
int cert_signer_algo;
|
||||
private_key *sk;
|
||||
anchor_list anchors = VEC_INIT;
|
||||
VECTOR(const char *) alpn_names = VEC_INIT;
|
||||
VECTOR(char *) alpn_names = VEC_INIT;
|
||||
br_x509_minimal_context xc;
|
||||
const br_hash_class *dnhash;
|
||||
size_t u;
|
||||
|
|
@ -616,7 +643,7 @@ do_server(int argc, char *argv[])
|
|||
unsigned char *iobuf, *cache;
|
||||
size_t iobuf_len, cache_len;
|
||||
uint32_t flags;
|
||||
int server_fd, fd;
|
||||
SOCKET server_fd, fd;
|
||||
|
||||
retcode = 0;
|
||||
verbose = 1;
|
||||
|
|
@ -639,8 +666,8 @@ do_server(int argc, char *argv[])
|
|||
cache = NULL;
|
||||
cache_len = (size_t)-1;
|
||||
flags = 0;
|
||||
server_fd = -1;
|
||||
fd = -1;
|
||||
server_fd = INVALID_SOCKET;
|
||||
fd = INVALID_SOCKET;
|
||||
for (i = 0; i < argc; i ++) {
|
||||
const char *arg;
|
||||
|
||||
|
|
@ -1089,7 +1116,8 @@ do_server(int argc, char *argv[])
|
|||
|
||||
if (VEC_LEN(alpn_names) != 0) {
|
||||
br_ssl_engine_set_protocol_names(&cc.eng,
|
||||
&VEC_ELT(alpn_names, 0), VEC_LEN(alpn_names));
|
||||
(const char **)&VEC_ELT(alpn_names, 0),
|
||||
VEC_LEN(alpn_names));
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -1139,15 +1167,17 @@ do_server(int argc, char *argv[])
|
|||
br_ssl_engine_set_buffer(&cc.eng, iobuf, iobuf_len, bidi);
|
||||
|
||||
/*
|
||||
* We need to ignore SIGPIPE.
|
||||
* On Unix systems, we need to ignore SIGPIPE.
|
||||
*/
|
||||
#ifndef _WIN32
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Open the server socket.
|
||||
*/
|
||||
server_fd = host_bind(bind_name, port, verbose);
|
||||
if (server_fd < 0) {
|
||||
if (server_fd == INVALID_SOCKET) {
|
||||
goto server_exit_error;
|
||||
}
|
||||
|
||||
|
|
@ -1162,15 +1192,19 @@ do_server(int argc, char *argv[])
|
|||
int x;
|
||||
|
||||
fd = accept_client(server_fd, verbose);
|
||||
if (fd < 0) {
|
||||
if (fd == INVALID_SOCKET) {
|
||||
goto server_exit_error;
|
||||
}
|
||||
br_ssl_server_reset(&cc);
|
||||
x = run_ssl_engine(&cc.eng, fd,
|
||||
(verbose ? RUN_ENGINE_VERBOSE : 0)
|
||||
| (trace ? RUN_ENGINE_TRACE : 0));
|
||||
#ifdef _WIN32
|
||||
closesocket(fd);
|
||||
#else
|
||||
close(fd);
|
||||
fd = -1;
|
||||
#endif
|
||||
fd = INVALID_SOCKET;
|
||||
if (x < -1) {
|
||||
goto server_exit_error;
|
||||
}
|
||||
|
|
@ -1188,8 +1222,12 @@ server_exit:
|
|||
VEC_CLEAREXT(alpn_names, &free_alpn);
|
||||
xfree(iobuf);
|
||||
xfree(cache);
|
||||
if (fd >= 0) {
|
||||
if (fd != INVALID_SOCKET) {
|
||||
#ifdef _WIN32
|
||||
closesocket(fd);
|
||||
#else
|
||||
close(fd);
|
||||
#endif
|
||||
}
|
||||
return retcode;
|
||||
|
||||
|
|
|
|||
423
tools/sslio.c
423
tools/sslio.c
|
|
@ -28,6 +28,10 @@
|
|||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
|
|
@ -37,8 +41,11 @@
|
|||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
|
||||
#define SOCKET int
|
||||
#define INVALID_SOCKET (-1)
|
||||
#endif
|
||||
|
||||
#include "brssl.h"
|
||||
#include "bearssl.h"
|
||||
|
||||
static void
|
||||
dump_blob(const char *name, const void *data, size_t len)
|
||||
|
|
@ -134,25 +141,173 @@ run_command(br_ssl_engine_context *cc, unsigned char *buf, size_t len)
|
|||
}
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
typedef struct {
|
||||
unsigned char buf[1024];
|
||||
size_t ptr, len;
|
||||
} in_buffer;
|
||||
|
||||
static int
|
||||
in_return_bytes(in_buffer *bb, unsigned char *buf, size_t len)
|
||||
{
|
||||
if (bb->ptr < bb->len) {
|
||||
size_t clen;
|
||||
|
||||
if (buf == NULL) {
|
||||
return 1;
|
||||
}
|
||||
clen = bb->len - bb->ptr;
|
||||
if (clen > len) {
|
||||
clen = len;
|
||||
}
|
||||
memcpy(buf, bb->buf + bb->ptr, clen);
|
||||
bb->ptr += clen;
|
||||
if (bb->ptr == bb->len) {
|
||||
bb->ptr = bb->len = 0;
|
||||
}
|
||||
return (int)clen;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* A buffered version of in_read(), using a buffer to return only
|
||||
* full lines when feasible.
|
||||
*/
|
||||
static int
|
||||
in_read_buffered(HANDLE h_in, in_buffer *bb, unsigned char *buf, size_t len)
|
||||
{
|
||||
int n;
|
||||
|
||||
if (len == 0) {
|
||||
return 0;
|
||||
}
|
||||
n = in_return_bytes(bb, buf, len);
|
||||
if (n != 0) {
|
||||
return n;
|
||||
}
|
||||
for (;;) {
|
||||
INPUT_RECORD inrec;
|
||||
DWORD v;
|
||||
|
||||
if (!PeekConsoleInput(h_in, &inrec, 1, &v)) {
|
||||
fprintf(stderr, "ERROR: PeekConsoleInput()"
|
||||
" failed with 0x%08lX\n",
|
||||
(unsigned long)GetLastError());
|
||||
return -1;
|
||||
}
|
||||
if (v == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (!ReadConsoleInput(h_in, &inrec, 1, &v)) {
|
||||
fprintf(stderr, "ERROR: ReadConsoleInput()"
|
||||
" failed with 0x%08lX\n",
|
||||
(unsigned long)GetLastError());
|
||||
return -1;
|
||||
}
|
||||
if (v == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (inrec.EventType == KEY_EVENT
|
||||
&& inrec.Event.KeyEvent.bKeyDown)
|
||||
{
|
||||
int c;
|
||||
|
||||
c = inrec.Event.KeyEvent.uChar.AsciiChar;
|
||||
if (c == '\n' || c == '\r' || c == '\t'
|
||||
|| (c >= 32 && c != 127))
|
||||
{
|
||||
if (c == '\r') {
|
||||
c = '\n';
|
||||
}
|
||||
bb->buf[bb->ptr ++] = (unsigned char)c;
|
||||
printf("%c", c);
|
||||
fflush(stdout);
|
||||
bb->len = bb->ptr;
|
||||
if (bb->len == sizeof bb->buf || c == '\n') {
|
||||
bb->ptr = 0;
|
||||
return in_return_bytes(bb, buf, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
in_avail_buffered(HANDLE h_in, in_buffer *bb)
|
||||
{
|
||||
return in_read_buffered(h_in, bb, NULL, 1);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* see brssl.h */
|
||||
int
|
||||
run_ssl_engine(br_ssl_engine_context *cc, int fd, unsigned flags)
|
||||
run_ssl_engine(br_ssl_engine_context *cc, unsigned long fd, unsigned flags)
|
||||
{
|
||||
int hsdetails;
|
||||
int retcode;
|
||||
int verbose;
|
||||
int trace;
|
||||
#ifdef _WIN32
|
||||
WSAEVENT fd_event;
|
||||
int can_send, can_recv;
|
||||
HANDLE h_in, h_out;
|
||||
in_buffer bb;
|
||||
#endif
|
||||
|
||||
hsdetails = 0;
|
||||
retcode = 0;
|
||||
verbose = (flags & RUN_ENGINE_VERBOSE) != 0;
|
||||
trace = (flags & RUN_ENGINE_TRACE) != 0;
|
||||
|
||||
#ifdef _WIN32
|
||||
fd_event = WSA_INVALID_EVENT;
|
||||
can_send = 0;
|
||||
can_recv = 0;
|
||||
bb.ptr = bb.len = 0;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* On Unix systems, we need to follow three descriptors:
|
||||
* standard input (0), standard output (1), and the socket
|
||||
* itself (for both read and write). This is done with a poll()
|
||||
* call.
|
||||
*
|
||||
* On Windows systems, we use WSAEventSelect() to associate
|
||||
* an event handle with the network activity, and we use
|
||||
* WaitForMultipleObjectsEx() on that handle and the standard
|
||||
* input handle, when appropriate. Standard output is assumed
|
||||
* to be always writeable, and standard input to be the console;
|
||||
* this does not work well (or at all) with redirections (to
|
||||
* pipes or files) but it should be enough for a debug tool
|
||||
* (TODO: make something that handles redirections as well).
|
||||
*/
|
||||
|
||||
#ifdef _WIN32
|
||||
fd_event = WSACreateEvent();
|
||||
if (fd_event == WSA_INVALID_EVENT) {
|
||||
fprintf(stderr, "ERROR: WSACreateEvent() failed with %d\n",
|
||||
WSAGetLastError());
|
||||
retcode = -2;
|
||||
goto engine_exit;
|
||||
}
|
||||
WSAEventSelect(fd, fd_event, FD_READ | FD_WRITE | FD_CLOSE);
|
||||
h_in = GetStdHandle(STD_INPUT_HANDLE);
|
||||
h_out = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
SetConsoleMode(h_in, ENABLE_ECHO_INPUT
|
||||
| ENABLE_LINE_INPUT
|
||||
| ENABLE_PROCESSED_INPUT
|
||||
| ENABLE_PROCESSED_OUTPUT
|
||||
| ENABLE_WRAP_AT_EOL_OUTPUT);
|
||||
#else
|
||||
/*
|
||||
* Make sure that stdin and stdout are non-blocking.
|
||||
*/
|
||||
fcntl(0, F_SETFL, O_NONBLOCK);
|
||||
fcntl(1, F_SETFL, O_NONBLOCK);
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Perform the loop.
|
||||
|
|
@ -160,9 +315,15 @@ run_ssl_engine(br_ssl_engine_context *cc, int fd, unsigned flags)
|
|||
for (;;) {
|
||||
unsigned st;
|
||||
int sendrec, recvrec, sendapp, recvapp;
|
||||
#ifdef _WIN32
|
||||
HANDLE pfd[2];
|
||||
DWORD wt;
|
||||
#else
|
||||
struct pollfd pfd[3];
|
||||
int n;
|
||||
#endif
|
||||
size_t u, k_fd, k_in, k_out;
|
||||
int sendrec_ok, recvrec_ok, sendapp_ok, recvapp_ok;
|
||||
|
||||
/*
|
||||
* Get current engine state.
|
||||
|
|
@ -257,11 +418,74 @@ run_ssl_engine(br_ssl_engine_context *cc, int fd, unsigned flags)
|
|||
hsdetails = 1;
|
||||
}
|
||||
|
||||
k_fd = 0;
|
||||
k_in = 0;
|
||||
k_out = 0;
|
||||
k_fd = (size_t)-1;
|
||||
k_in = (size_t)-1;
|
||||
k_out = (size_t)-1;
|
||||
|
||||
u = 0;
|
||||
#ifdef _WIN32
|
||||
/*
|
||||
* If we recorded that we can send or receive data, and we
|
||||
* want to do exactly that, then we don't wait; we just do
|
||||
* it.
|
||||
*/
|
||||
recvapp_ok = 0;
|
||||
sendrec_ok = 0;
|
||||
recvrec_ok = 0;
|
||||
sendapp_ok = 0;
|
||||
|
||||
if (sendrec && can_send) {
|
||||
sendrec_ok = 1;
|
||||
} else if (recvrec && can_recv) {
|
||||
recvrec_ok = 1;
|
||||
} else if (recvapp) {
|
||||
recvapp_ok = 1;
|
||||
} else if (sendapp && in_avail_buffered(h_in, &bb)) {
|
||||
sendapp_ok = 1;
|
||||
} else {
|
||||
/*
|
||||
* If we cannot do I/O right away, then we must
|
||||
* wait for some event, and try again.
|
||||
*/
|
||||
pfd[u] = (HANDLE)fd_event;
|
||||
k_fd = u;
|
||||
u ++;
|
||||
if (sendapp) {
|
||||
pfd[u] = h_in;
|
||||
k_in = u;
|
||||
u ++;
|
||||
}
|
||||
wt = WaitForMultipleObjectsEx(u, pfd,
|
||||
FALSE, INFINITE, FALSE);
|
||||
if (wt == WAIT_FAILED) {
|
||||
fprintf(stderr, "ERROR:"
|
||||
" WaitForMultipleObjectsEx()"
|
||||
" failed with 0x%08lX",
|
||||
(unsigned long)GetLastError());
|
||||
retcode = -2;
|
||||
goto engine_exit;
|
||||
}
|
||||
if (wt == k_fd) {
|
||||
WSANETWORKEVENTS e;
|
||||
|
||||
if (WSAEnumNetworkEvents(fd, fd_event, &e)) {
|
||||
fprintf(stderr, "ERROR:"
|
||||
" WSAEnumNetworkEvents()"
|
||||
" failed with %d\n",
|
||||
WSAGetLastError());
|
||||
retcode = -2;
|
||||
goto engine_exit;
|
||||
}
|
||||
if (e.lNetworkEvents & (FD_WRITE | FD_CLOSE)) {
|
||||
can_send = 1;
|
||||
}
|
||||
if (e.lNetworkEvents & (FD_READ | FD_CLOSE)) {
|
||||
can_recv = 1;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
#else
|
||||
if (sendrec || recvrec) {
|
||||
pfd[u].fd = fd;
|
||||
pfd[u].revents = 0;
|
||||
|
|
@ -313,96 +537,142 @@ run_ssl_engine(br_ssl_engine_context *cc, int fd, unsigned flags)
|
|||
}
|
||||
}
|
||||
|
||||
recvapp_ok = recvapp && (pfd[k_out].revents & POLLOUT) != 0;
|
||||
sendrec_ok = sendrec && (pfd[k_fd].revents & POLLOUT) != 0;
|
||||
recvrec_ok = recvrec && (pfd[k_fd].revents & POLLIN) != 0;
|
||||
sendapp_ok = sendapp && (pfd[k_in].revents & POLLIN) != 0;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* We give preference to outgoing data, on stdout and on
|
||||
* the socket.
|
||||
*/
|
||||
if (recvapp) {
|
||||
if (pfd[k_out].revents & POLLOUT) {
|
||||
unsigned char *buf;
|
||||
size_t len;
|
||||
ssize_t wlen;
|
||||
if (recvapp_ok) {
|
||||
unsigned char *buf;
|
||||
size_t len;
|
||||
#ifdef _WIN32
|
||||
DWORD wlen;
|
||||
#else
|
||||
ssize_t wlen;
|
||||
#endif
|
||||
|
||||
buf = br_ssl_engine_recvapp_buf(cc, &len);
|
||||
wlen = write(1, buf, len);
|
||||
if (wlen <= 0) {
|
||||
if (verbose) {
|
||||
fprintf(stderr,
|
||||
"stdout closed...\n");
|
||||
}
|
||||
retcode = -2;
|
||||
goto engine_exit;
|
||||
buf = br_ssl_engine_recvapp_buf(cc, &len);
|
||||
#ifdef _WIN32
|
||||
if (!WriteFile(h_out, buf, len, &wlen, NULL)) {
|
||||
if (verbose) {
|
||||
fprintf(stderr, "stdout closed...\n");
|
||||
}
|
||||
br_ssl_engine_recvapp_ack(cc, wlen);
|
||||
continue;
|
||||
retcode = -2;
|
||||
goto engine_exit;
|
||||
}
|
||||
#else
|
||||
wlen = write(1, buf, len);
|
||||
if (wlen <= 0) {
|
||||
if (verbose) {
|
||||
fprintf(stderr, "stdout closed...\n");
|
||||
}
|
||||
retcode = -2;
|
||||
goto engine_exit;
|
||||
}
|
||||
#endif
|
||||
br_ssl_engine_recvapp_ack(cc, wlen);
|
||||
continue;
|
||||
}
|
||||
if (sendrec) {
|
||||
if (pfd[k_fd].revents & POLLOUT) {
|
||||
unsigned char *buf;
|
||||
size_t len;
|
||||
ssize_t wlen;
|
||||
if (sendrec_ok) {
|
||||
unsigned char *buf;
|
||||
size_t len;
|
||||
int wlen;
|
||||
|
||||
buf = br_ssl_engine_sendrec_buf(cc, &len);
|
||||
wlen = write(fd, buf, len);
|
||||
if (wlen <= 0) {
|
||||
if (verbose) {
|
||||
fprintf(stderr,
|
||||
"socket closed...\n");
|
||||
}
|
||||
retcode = -1;
|
||||
goto engine_exit;
|
||||
buf = br_ssl_engine_sendrec_buf(cc, &len);
|
||||
wlen = send(fd, buf, len, 0);
|
||||
if (wlen <= 0) {
|
||||
#ifdef _WIN32
|
||||
int err;
|
||||
|
||||
err = WSAGetLastError();
|
||||
if (err == EWOULDBLOCK
|
||||
|| err == WSAEWOULDBLOCK)
|
||||
{
|
||||
can_send = 0;
|
||||
continue;
|
||||
}
|
||||
if (trace) {
|
||||
dump_blob("Outgoing bytes", buf, wlen);
|
||||
#else
|
||||
if (errno == EINTR || errno == EWOULDBLOCK) {
|
||||
continue;
|
||||
}
|
||||
br_ssl_engine_sendrec_ack(cc, wlen);
|
||||
continue;
|
||||
#endif
|
||||
if (verbose) {
|
||||
fprintf(stderr, "socket closed...\n");
|
||||
}
|
||||
retcode = -1;
|
||||
goto engine_exit;
|
||||
}
|
||||
if (trace) {
|
||||
dump_blob("Outgoing bytes", buf, wlen);
|
||||
}
|
||||
br_ssl_engine_sendrec_ack(cc, wlen);
|
||||
continue;
|
||||
}
|
||||
if (recvrec) {
|
||||
if (pfd[k_fd].revents & POLLIN) {
|
||||
unsigned char *buf;
|
||||
size_t len;
|
||||
ssize_t rlen;
|
||||
if (recvrec_ok) {
|
||||
unsigned char *buf;
|
||||
size_t len;
|
||||
int rlen;
|
||||
|
||||
buf = br_ssl_engine_recvrec_buf(cc, &len);
|
||||
rlen = read(fd, buf, len);
|
||||
if (rlen <= 0) {
|
||||
if (verbose) {
|
||||
fprintf(stderr,
|
||||
"socket closed...\n");
|
||||
}
|
||||
retcode = -1;
|
||||
goto engine_exit;
|
||||
buf = br_ssl_engine_recvrec_buf(cc, &len);
|
||||
rlen = recv(fd, buf, len, 0);
|
||||
if (rlen <= 0) {
|
||||
#ifdef _WIN32
|
||||
int err;
|
||||
|
||||
err = WSAGetLastError();
|
||||
if (err == EWOULDBLOCK
|
||||
|| err == WSAEWOULDBLOCK)
|
||||
{
|
||||
can_recv = 0;
|
||||
continue;
|
||||
}
|
||||
if (trace) {
|
||||
dump_blob("Incoming bytes", buf, rlen);
|
||||
#else
|
||||
if (errno == EINTR || errno == EWOULDBLOCK) {
|
||||
continue;
|
||||
}
|
||||
br_ssl_engine_recvrec_ack(cc, rlen);
|
||||
continue;
|
||||
#endif
|
||||
if (verbose) {
|
||||
fprintf(stderr, "socket closed...\n");
|
||||
}
|
||||
retcode = -1;
|
||||
goto engine_exit;
|
||||
}
|
||||
if (trace) {
|
||||
dump_blob("Incoming bytes", buf, rlen);
|
||||
}
|
||||
br_ssl_engine_recvrec_ack(cc, rlen);
|
||||
continue;
|
||||
}
|
||||
if (sendapp) {
|
||||
if (pfd[k_in].revents & POLLIN) {
|
||||
unsigned char *buf;
|
||||
size_t len;
|
||||
ssize_t rlen;
|
||||
if (sendapp_ok) {
|
||||
unsigned char *buf;
|
||||
size_t len;
|
||||
#ifdef _WIN32
|
||||
int rlen;
|
||||
#else
|
||||
ssize_t rlen;
|
||||
#endif
|
||||
|
||||
buf = br_ssl_engine_sendapp_buf(cc, &len);
|
||||
rlen = read(0, buf, len);
|
||||
if (rlen <= 0) {
|
||||
if (verbose) {
|
||||
fprintf(stderr,
|
||||
"stdin closed...\n");
|
||||
}
|
||||
br_ssl_engine_close(cc);
|
||||
} else if (!run_command(cc, buf, rlen)) {
|
||||
br_ssl_engine_sendapp_ack(cc, rlen);
|
||||
buf = br_ssl_engine_sendapp_buf(cc, &len);
|
||||
#ifdef _WIN32
|
||||
rlen = in_read_buffered(h_in, &bb, buf, len);
|
||||
#else
|
||||
rlen = read(0, buf, len);
|
||||
#endif
|
||||
if (rlen <= 0) {
|
||||
if (verbose) {
|
||||
fprintf(stderr, "stdin closed...\n");
|
||||
}
|
||||
br_ssl_engine_flush(cc, 0);
|
||||
continue;
|
||||
br_ssl_engine_close(cc);
|
||||
} else if (!run_command(cc, buf, rlen)) {
|
||||
br_ssl_engine_sendapp_ack(cc, rlen);
|
||||
}
|
||||
br_ssl_engine_flush(cc, 0);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* We should never reach that point. */
|
||||
|
|
@ -415,5 +685,10 @@ run_ssl_engine(br_ssl_engine_context *cc, int fd, unsigned flags)
|
|||
* Release allocated structures.
|
||||
*/
|
||||
engine_exit:
|
||||
#ifdef _WIN32
|
||||
if (fd_event != WSA_INVALID_EVENT) {
|
||||
WSACloseEvent(fd_event);
|
||||
}
|
||||
#endif
|
||||
return retcode;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue