version 1.8, 2014/11/18 20:54:28 |
version 1.9, 2014/12/02 19:44:49 |
|
|
|
|
#include <sys/types.h> |
#include <sys/types.h> |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
#include <sys/select.h> |
|
#include <sys/socket.h> |
#include <sys/socket.h> |
|
|
#include <netinet/in.h> |
#include <netinet/in.h> |
|
|
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
#include <unistd.h> |
#include <unistd.h> |
|
#include <poll.h> |
|
|
#include "apps.h" |
#include "apps.h" |
|
|
|
|
{ |
{ |
unsigned int off = 0, clr = 0; |
unsigned int off = 0, clr = 0; |
SSL *con = NULL; |
SSL *con = NULL; |
int s, k, width, state = 0, af = AF_UNSPEC; |
int s, k, state = 0, af = AF_UNSPEC; |
char *cbuf = NULL, *sbuf = NULL, *mbuf = NULL; |
char *cbuf = NULL, *sbuf = NULL, *mbuf = NULL; |
int cbuf_len, cbuf_off; |
int cbuf_len, cbuf_off; |
int sbuf_len, sbuf_off; |
int sbuf_len, sbuf_off; |
fd_set readfds, writefds; |
|
char *port = PORT_STR; |
char *port = PORT_STR; |
int full_log = 1; |
int full_log = 1; |
char *host = SSL_HOST_NAME; |
char *host = SSL_HOST_NAME; |
|
|
int socket_type = SOCK_STREAM; |
int socket_type = SOCK_STREAM; |
BIO *sbio; |
BIO *sbio; |
int mbuf_len = 0; |
int mbuf_len = 0; |
struct timeval timeout, *timeoutp; |
struct timeval timeout; |
const char *errstr = NULL; |
const char *errstr = NULL; |
#ifndef OPENSSL_NO_ENGINE |
#ifndef OPENSSL_NO_ENGINE |
char *engine_id = NULL; |
char *engine_id = NULL; |
|
|
SSL_set_connect_state(con); |
SSL_set_connect_state(con); |
|
|
/* ok, lets connect */ |
/* ok, lets connect */ |
width = SSL_get_fd(con) + 1; |
|
|
|
read_tty = 1; |
read_tty = 1; |
write_tty = 0; |
write_tty = 0; |
tty_on = 0; |
tty_on = 0; |
|
|
mbuf[0] = 0; |
mbuf[0] = 0; |
} |
} |
for (;;) { |
for (;;) { |
FD_ZERO(&readfds); |
struct pollfd pfd[3]; /* stdin, stdout, socket */ |
FD_ZERO(&writefds); |
int ptimeout = -1; |
|
|
if ((SSL_version(con) == DTLS1_VERSION) && |
if ((SSL_version(con) == DTLS1_VERSION) && |
DTLSv1_get_timeout(con, &timeout)) |
DTLSv1_get_timeout(con, &timeout)) |
timeoutp = &timeout; |
ptimeout = timeout.tv_sec * 1000 + timeout.tv_usec / 1000; |
else |
|
timeoutp = NULL; |
|
|
|
if (SSL_in_init(con) && !SSL_total_renegotiations(con)) { |
if (SSL_in_init(con) && !SSL_total_renegotiations(con)) { |
in_init = 1; |
in_init = 1; |
|
|
|
|
ssl_pending = read_ssl && SSL_pending(con); |
ssl_pending = read_ssl && SSL_pending(con); |
|
|
/* XXX should add tests for fd_set overflow */ |
pfd[0].fd = -1; |
|
pfd[1].fd = -1; |
if (!ssl_pending) { |
if (!ssl_pending) { |
if (tty_on) { |
if (tty_on) { |
if (read_tty) |
if (read_tty) { |
FD_SET(fileno(stdin), &readfds); |
pfd[0].fd = fileno(stdin); |
if (write_tty) |
pfd[0].events = POLLIN; |
FD_SET(fileno(stdout), &writefds); |
} |
|
if (write_tty) { |
|
pfd[1].fd = fileno(stdout); |
|
pfd[1].events = POLLOUT; |
|
} |
} |
} |
|
|
|
pfd[2].fd = SSL_get_fd(con); |
|
pfd[2].events = 0; |
if (read_ssl) |
if (read_ssl) |
FD_SET(SSL_get_fd(con), &readfds); |
pfd[2].events |= POLLIN; |
if (write_ssl) |
if (write_ssl) |
FD_SET(SSL_get_fd(con), &writefds); |
pfd[2].events |= POLLOUT; |
|
|
/* printf("mode tty(%d %d%d) ssl(%d%d)\n", |
/* printf("mode tty(%d %d%d) ssl(%d%d)\n", |
tty_on,read_tty,write_tty,read_ssl,write_ssl);*/ |
tty_on,read_tty,write_tty,read_ssl,write_ssl);*/ |
|
|
i = select(width, &readfds, &writefds, |
i = poll(pfd, 3, ptimeout); |
NULL, timeoutp); |
|
if (i < 0) { |
if (i < 0) { |
BIO_printf(bio_err, "bad select %d\n", |
BIO_printf(bio_err, "bad select %d\n", |
errno); |
errno); |
|
|
if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0) { |
if ((SSL_version(con) == DTLS1_VERSION) && DTLSv1_handle_timeout(con) > 0) { |
BIO_printf(bio_err, "TIMEOUT occured\n"); |
BIO_printf(bio_err, "TIMEOUT occured\n"); |
} |
} |
if (!ssl_pending && FD_ISSET(SSL_get_fd(con), &writefds)) { |
if (!ssl_pending && (pfd[2].revents & (POLLOUT|POLLERR|POLLNVAL))) { |
|
if (pfd[2].revents & (POLLERR|POLLNVAL)) { |
|
BIO_printf(bio_err, "poll error"); |
|
goto shut; |
|
} |
k = SSL_write(con, &(cbuf[cbuf_off]), |
k = SSL_write(con, &(cbuf[cbuf_off]), |
(unsigned int) cbuf_len); |
(unsigned int) cbuf_len); |
switch (SSL_get_error(con, k)) { |
switch (SSL_get_error(con, k)) { |
|
|
ERR_print_errors(bio_err); |
ERR_print_errors(bio_err); |
goto shut; |
goto shut; |
} |
} |
} else if (!ssl_pending && FD_ISSET(fileno(stdout), &writefds)) { |
} else if (!ssl_pending && |
|
(pfd[1].revents & (POLLOUT|POLLERR|POLLNVAL))) { |
|
if (pfd[1].revents & (POLLERR|POLLNVAL)) { |
|
BIO_printf(bio_err, "poll error"); |
|
goto shut; |
|
} |
i = write(fileno(stdout), &(sbuf[sbuf_off]), sbuf_len); |
i = write(fileno(stdout), &(sbuf[sbuf_off]), sbuf_len); |
|
|
if (i <= 0) { |
if (i <= 0) { |
|
|
read_ssl = 1; |
read_ssl = 1; |
write_tty = 0; |
write_tty = 0; |
} |
} |
} else if (ssl_pending || FD_ISSET(SSL_get_fd(con), &readfds)) { |
} else if (ssl_pending || (pfd[2].revents & (POLLIN|POLLHUP))) { |
#ifdef RENEG |
#ifdef RENEG |
{ |
{ |
static int iiii; |
static int iiii; |
|
|
goto shut; |
goto shut; |
/* break; */ |
/* break; */ |
} |
} |
} else if (FD_ISSET(fileno(stdin), &readfds)) { |
} else if (pfd[0].revents) { |
|
if (pfd[0].revents & (POLLERR|POLLNVAL)) { |
|
BIO_printf(bio_err, "poll error"); |
|
goto shut; |
|
} |
if (crlf) { |
if (crlf) { |
int j, lf_num; |
int j, lf_num; |
|
|