]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
tools: allow specifying a hostname with a port attached
authorNikos Mavrogiannopoulos <nmav@redhat.com>
Mon, 30 May 2016 10:57:55 +0000 (12:57 +0200)
committerNikos Mavrogiannopoulos <nmav@redhat.com>
Mon, 30 May 2016 10:57:59 +0000 (12:57 +0200)
That is: gnutls-cli www.example.com:443 is equivalent to
gnutls-cli www.example.com -p 443

src/cli-debug.c
src/cli.c
src/socket.c
src/socket.h

index 6f81fd48bfba170b927166fa941cf7edd0c191d9..d4f9f3269ac482c63732cc18c947dc50b331f03c 100644 (file)
@@ -44,7 +44,7 @@ static void cmd_parser(int argc, char **argv);
 
 /* global stuff here */
 int resume;
-const char *hostname = NULL;
+char *hostname = NULL;
 int port;
 int record_max_size;
 int fingerprint;
@@ -251,6 +251,8 @@ int main(int argc, char **argv)
        i = 0;
 
        printf("GnuTLS debug client %s\n", gnutls_check_version(NULL));
+
+       canonicalize_host(hostname, portname, sizeof(portname));
        printf("Checking %s:%s\n", hostname, portname);
        do {
 
@@ -273,8 +275,7 @@ int main(int argc, char **argv)
 
                gnutls_init(&state, GNUTLS_CLIENT | GNUTLS_NO_EXTENSIONS);
 
-               gnutls_transport_set_ptr(state, (gnutls_transport_ptr_t)
-                                        gl_fd_to_handle(hd.fd));
+               gnutls_transport_set_int(state, hd.fd);
                set_read_funcs(state);
                if (hostname && is_ip(hostname) == 0)
                        gnutls_server_name_set(state, GNUTLS_NAME_DNS,
@@ -334,7 +335,8 @@ int main(int argc, char **argv)
 
 static void cmd_parser(int argc, char **argv)
 {
-       const char *rest = NULL;
+       char *rest = NULL;
+       static char lh[] = "localhost";
        int optct = optionProcess(&gnutls_cli_debugOptions, argc, argv);
        argc -= optct;
        argv += optct;
@@ -352,7 +354,7 @@ static void cmd_parser(int argc, char **argv)
        }
 
        if (rest == NULL)
-               hostname = "localhost";
+               hostname = lh;
        else
                hostname = rest;
 
index 479a9eb2f23ab09895157ccbadaf9e8fdfd32ae0..1e60b6c7d846c7d66a850c917343416d407aaf9e 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -73,7 +73,7 @@
 /* global stuff here */
 int resume, starttls, insecure, ranges, rehandshake, udp, mtu,
     inline_commands;
-const char *hostname = NULL;
+char *hostname = NULL;
 char service[32]="";
 int record_max_size;
 int fingerprint;
@@ -904,6 +904,8 @@ static int try_resume(socket_st * hd)
        printf("- Disconnecting\n");
        socket_bye(hd);
 
+       canonicalize_host(hostname, service, sizeof(service));
+
        printf
            ("\n\n- Connecting again- trying to resume previous session\n");
        socket_open(hd, hostname, service, udp, CONNECT_MSG);
@@ -1207,6 +1209,8 @@ int main(int argc, char **argv)
 
        init_global_tls_stuff();
 
+       canonicalize_host(hostname, service, sizeof(service));
+
        socket_open(&hd, hostname, service, udp, CONNECT_MSG);
        hd.verbose = verbose;
 
@@ -1478,7 +1482,7 @@ void print_priority_list(void)
 
 static void cmd_parser(int argc, char **argv)
 {
-       const char *rest = NULL;
+       char *rest = NULL;
 
        int optct = optionProcess(&gnutls_cliOptions, argc, argv);
        argc -= optct;
index 979c387ba95bef967f153e53fdff97370d0436c3..ddf9b0aec7431267f4c4083281e566698f5f7539 100644 (file)
@@ -333,6 +333,24 @@ void socket_bye(socket_st * socket)
        socket->secure = 0;
 }
 
+/* Handle host:port format.
+ */
+void canonicalize_host(char *hostname, char *service, unsigned service_size)
+{
+       char *p;
+       unsigned char buf[sizeof(struct in6_addr)];
+
+       p = strchr(hostname, ':');
+       if (p == NULL)
+               return;
+
+       if (inet_pton(AF_INET6, hostname, buf) == 1)
+               return;
+
+       *p = 0;
+       snprintf(service, service_size, "%s", p+1);
+}
+
 void
 socket_open(socket_st * hd, const char *hostname, const char *service,
            int udp, const char *msg)
index 37c10acc37fa733ce58fb0b361fc80046c6b5839..4928065f3b41ac21d0a3e11097d34e2e1c8c0ef6 100644 (file)
@@ -36,4 +36,6 @@ const char *port_to_service(const char *sport, const char *proto);
 int starttls_proto_to_port(const char *app_proto);
 const char *starttls_proto_to_service(const char *app_proto);
 
+void canonicalize_host(char *hostname, char *service, unsigned service_size);
+
 #define CONNECT_MSG "Connecting to"