summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPatrick McDermott <patrick.mcdermott@libiquity.com>2019-07-29 03:17:49 (EDT)
committer Patrick McDermott <patrick.mcdermott@libiquity.com>2019-07-29 03:17:49 (EDT)
commit22a7c2d07a52864cdba979fa442311c8f43d1ff6 (patch)
treee99da7d2b4c532c3418a9a2753414c6a10114f2c /src
parent213de815be275663aec88d527a17f0e48b23ae1d (diff)
s_client: Open and connect socket
Diffstat (limited to 'src')
-rw-r--r--src/s_client.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/s_client.c b/src/s_client.c
index 7865a78..99fba35 100644
--- a/src/s_client.c
+++ b/src/s_client.c
@@ -19,10 +19,12 @@
* along with wolfssl-util. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <netdb.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <sys/socket.h>
#include <wolfssl/ssl.h>
#include <wolfssl/wolfcrypt/settings.h>
@@ -48,6 +50,48 @@ parse_host_port(char *hostport, char **host, char **port)
return true;
}
+static _Bool
+connect_socket(const char *host, const char *port)
+{
+ struct addrinfo hints = {0};
+ struct addrinfo *result;
+ int s;
+ struct addrinfo *rp;
+ int sfd;
+
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags = AI_PASSIVE;
+ hints.ai_protocol = 0;
+
+ s = getaddrinfo(host, port, &hints, &result);
+ if (s != 0) {
+ fprintf(stderr, "Failed to resolve host and port: %s\n",
+ gai_strerror(s));
+ return -1;
+ }
+
+ for (rp = result; rp != NULL; rp = rp->ai_next) {
+ sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
+ if (sfd == -1) {
+ continue;
+ }
+ if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1) {
+ break;
+ }
+ close(sfd);
+ }
+ if (rp == NULL) {
+ fputs("Failed to connect\n", stderr);
+ freeaddrinfo(result);
+ return -1;
+ }
+
+ freeaddrinfo(result);
+
+ return sfd;
+}
+
int
s_client(int argc, char **argv)
{
@@ -58,6 +102,7 @@ s_client(int argc, char **argv)
WOLFSSL_METHOD *method;
WOLFSSL_CTX *ctx;
WOLFSSL *ssl;
+ int sfd;
for (; argc > 0; --argc, ++argv) {
if (strcmp(*argv, "-quiet") == 0) {
@@ -126,6 +171,15 @@ s_client(int argc, char **argv)
goto ctx_free;
}
+ sfd = connect_socket(host, port);
+ if (sfd == -1) {
+ ret = EXIT_FAILURE;
+ goto ssl_free;
+ }
+
+ close(sfd);
+
+ssl_free:
wolfSSL_free(ssl);
ctx_free:
wolfSSL_CTX_free(ctx);