Moved address-to-string translation code to blocks that depend on the 'verbose' flag.
authorThomas Pornin <pornin@bolet.org>
Sat, 12 Nov 2016 15:49:27 +0000 (16:49 +0100)
committerThomas Pornin <pornin@bolet.org>
Sat, 12 Nov 2016 15:49:27 +0000 (16:49 +0100)
tools/client.c
tools/server.c

index 7acfebf..8fc3cd8 100644 (file)
@@ -57,27 +57,29 @@ host_connect(const char *host, const char *port, int verbose)
        }
        fd = -1;
        for (p = si; p != NULL; p = p->ai_next) {
-               struct sockaddr *sa;
-               void *addr;
-               char tmp[INET6_ADDRSTRLEN + 50];
+               if (verbose) {
+                       struct sockaddr *sa;
+                       void *addr;
+                       char tmp[INET6_ADDRSTRLEN + 50];
 
-               sa = (struct sockaddr *)p->ai_addr;
-               if (sa->sa_family == AF_INET) {
-                       addr = &((struct sockaddr_in *)sa)->sin_addr;
-               } else if (sa->sa_family == AF_INET6) {
-                       addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
-               } else {
-                       addr = NULL;
-               }
-               if (addr != NULL) {
-                       if (!inet_ntop(p->ai_family, addr, tmp, sizeof tmp)) {
-                               strcpy(tmp, "<invalid>");
+                       sa = (struct sockaddr *)p->ai_addr;
+                       if (sa->sa_family == AF_INET) {
+                               addr = &((struct sockaddr_in *)sa)->sin_addr;
+                       } else if (sa->sa_family == AF_INET6) {
+                               addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
+                       } else {
+                               addr = NULL;
+                       }
+                       if (addr != NULL) {
+                               if (!inet_ntop(p->ai_family, addr,
+                                       tmp, sizeof tmp))
+                               {
+                                       strcpy(tmp, "<invalid>");
+                               }
+                       } else {
+                               sprintf(tmp, "<unknown family: %d>",
+                                       (int)sa->sa_family);
                        }
-               } else {
-                       sprintf(tmp, "<unknown family: %d>",
-                               (int)sa->sa_family);
-               }
-               if (verbose) {
                        fprintf(stderr, "connecting to: %s\n", tmp);
                }
                fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
index 983fbe6..9424bf5 100644 (file)
@@ -62,7 +62,6 @@ host_bind(const char *host, const char *port, int verbose)
                struct sockaddr_in6 sa6;
                size_t sa_len;
                void *addr;
-               char tmp[INET6_ADDRSTRLEN + 50];
                int opt;
 
                sa = (struct sockaddr *)p->ai_addr;
@@ -86,15 +85,19 @@ host_bind(const char *host, const char *port, int verbose)
                        addr = NULL;
                        sa_len = p->ai_addrlen;
                }
-               if (addr != NULL) {
-                       if (!inet_ntop(p->ai_family, addr, tmp, sizeof tmp)) {
-                               strcpy(tmp, "<invalid>");
-                       }
-               } else {
-                       sprintf(tmp, "<unknown family: %d>",
-                               (int)sa->sa_family);
-               }
                if (verbose) {
+                       char tmp[INET6_ADDRSTRLEN + 50];
+
+                       if (addr != NULL) {
+                               if (!inet_ntop(p->ai_family, addr,
+                                       tmp, sizeof tmp))
+                               {
+                                       strcpy(tmp, "<invalid>");
+                               }
+                       } else {
+                               sprintf(tmp, "<unknown family: %d>",
+                                       (int)sa->sa_family);
+                       }
                        fprintf(stderr, "binding to: %s\n", tmp);
                }
                fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);