From d2141a7b549f0898ac0c65e059692d81f6df9eba Mon Sep 17 00:00:00 2001 From: cameronrich Date: Thu, 14 Jun 2007 23:38:59 +0000 Subject: [PATCH] some improvments to the samples git-svn-id: svn://svn.code.sf.net/p/axtls/code/trunk@110 9a5d90b5-6617-0410-8a86-bb477d3ed2e3 --- samples/c/axssl.c | 118 +++++++++++++++++++++--------------------- samples/perl/axssl.pl | 53 +++++++------------ 2 files changed, 79 insertions(+), 92 deletions(-) diff --git a/samples/c/axssl.c b/samples/c/axssl.c index 7e155e414..999f3f8ac 100644 --- a/samples/c/axssl.c +++ b/samples/c/axssl.c @@ -199,40 +199,6 @@ static void do_server(int argc, char *argv[]) i++; } - /* Create socket for incoming connections */ - if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) - { - perror("socket"); - return; - } - - setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); - - /* Construct local address structure */ - memset(&serv_addr, 0, sizeof(serv_addr)); /* Zero out structure */ - serv_addr.sin_family = AF_INET; /* Internet address family */ - serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */ - serv_addr.sin_port = htons(port); /* Local port */ - - /* Bind to the local address */ - if (bind(server_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) - { - perror("bind"); - exit(1); - } - - if (listen(server_fd, 5) < 0) - { - perror("listen"); - exit(1); - } - - client_len = sizeof(client_addr); - - /************************************************************************* - * This is where the interesting stuff happens. Up until now we've - * just been setting up sockets etc. Now we do the SSL handshake. - *************************************************************************/ if ((ssl_ctx = ssl_ctx_new(options, SSL_DEFAULT_SVR_SESS)) == NULL) { fprintf(stderr, "Error: Server context is invalid\n"); @@ -284,6 +250,40 @@ static void do_server(int argc, char *argv[]) free(cert); #endif + /* Create socket for incoming connections */ + if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) + { + perror("socket"); + return; + } + + setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); + + /* Construct local address structure */ + memset(&serv_addr, 0, sizeof(serv_addr)); /* Zero out structure */ + serv_addr.sin_family = AF_INET; /* Internet address family */ + serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */ + serv_addr.sin_port = htons(port); /* Local port */ + + /* Bind to the local address */ + if (bind(server_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) + { + perror("bind"); + exit(1); + } + + if (listen(server_fd, 5) < 0) + { + perror("listen"); + exit(1); + } + + client_len = sizeof(client_addr); + + /************************************************************************* + * This is where the interesting stuff happens. Up until now we've + * just been setting up sockets etc. Now we do the SSL handshake. + *************************************************************************/ for (;;) { SSL *ssl; @@ -368,12 +368,12 @@ static void do_server(int argc, char *argv[]) } } - if (res > 0) /* display our interesting output */ + if (res > SSL_OK) /* display our interesting output */ { printf("%s", read_buf); TTY_FLUSH(); } - else if (res < 0 && !quiet) + else if (res < SSL_OK && !quiet) { ssl_display_error(res); } @@ -534,29 +534,6 @@ static void do_client(int argc, char *argv[]) i++; } - client_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - memset(&client_addr, 0, sizeof(client_addr)); - client_addr.sin_family = AF_INET; - client_addr.sin_port = htons(port); - client_addr.sin_addr.s_addr = sin_addr; - - if (connect(client_fd, (struct sockaddr *)&client_addr, - sizeof(client_addr)) < 0) - { - perror("connect"); - exit(1); - } - - if (!quiet) - { - printf("CONNECTED\n"); - TTY_FLUSH(); - } - - /************************************************************************* - * This is where the interesting stuff happens. Up until now we've - * just been setting up sockets etc. Now we do the SSL handshake. - *************************************************************************/ if ((ssl_ctx = ssl_ctx_new(options, SSL_DEFAULT_CLNT_SESS)) == NULL) { fprintf(stderr, "Error: Client context is invalid\n"); @@ -602,6 +579,29 @@ static void do_client(int argc, char *argv[]) free(cert); free(ca_cert); + /************************************************************************* + * This is where the interesting stuff happens. Up until now we've + * just been setting up sockets etc. Now we do the SSL handshake. + *************************************************************************/ + client_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + memset(&client_addr, 0, sizeof(client_addr)); + client_addr.sin_family = AF_INET; + client_addr.sin_port = htons(port); + client_addr.sin_addr.s_addr = sin_addr; + + if (connect(client_fd, (struct sockaddr *)&client_addr, + sizeof(client_addr)) < 0) + { + perror("connect"); + exit(1); + } + + if (!quiet) + { + printf("CONNECTED\n"); + TTY_FLUSH(); + } + /* Try session resumption? */ if (reconnect) { diff --git a/samples/perl/axssl.pl b/samples/perl/axssl.pl index cf04254dd..15b052798 100755 --- a/samples/perl/axssl.pl +++ b/samples/perl/axssl.pl @@ -230,53 +230,40 @@ sub do_server # do the actual SSL handshake my $res; my $buf; + my $connected = 0; while (1) { ($res, $buf) = axtlsp::ssl_read($ssl, undef); last if $res != $axtlsp::SSL_OK; - # check when the connection has been established - last if axtlsp::ssl_handshake_status($ssl) == $axtlsp::SSL_OK; - - # could do something else here - } - - if ($res == $axtlsp::SSL_OK) # connection established and ok - { - if (!$quiet) + if ($res == $axtlsp::SSL_OK) # connection established and ok { - display_session_id($ssl); - display_cipher($ssl); + if (axtlsp::ssl_handshake_status($ssl) == $axtlsp::SSL_OK) + { + if (!$quiet && !$connected) + { + display_session_id($ssl); + display_cipher($ssl); + } + + $connected = 1; + } } - # now read (and display) whatever the client sends us - for (;;) + if ($res > $axtlsp::SSL_OK) { - # keep reading until we get something interesting - while (1) - { - ($res, $buf) = axtlsp::ssl_read($ssl, undef); - last if $res != $axtlsp::SSL_OK; - - # could do something else here - } - - if ($res < $axtlsp::SSL_OK) - { - printf("CONNECTION CLOSED\n") if not $quiet; - last; - } - printf($$buf); } - } - elsif (!$quiet) - { - axtlsp::ssl_display_error($res); + else if ($res < $axtlsp::SSL_OK) + { + axtlsp::ssl_display_error($res) if not $quiet; + last; + } } # client was disconnected or the handshake failed. + printf("CONNECTION CLOSED\n") if not $quiet; axtlsp::ssl_free($ssl); $client_sock->close; } @@ -518,7 +505,7 @@ sub print_server_options { printf(" -cert arg\t- certificate file to add (in addition to default)". " to chain -\n". - "\t\t default DER format. Can repeat up to %d times\n", $cert_size); + "\t\t Can repeat up to %d times\n", $cert_size); printf(" -key arg\t- Private key file to use - default DER format\n"); printf(" -pass\t\t- private key file pass phrase source\n"); }