--- A/netstat.c 2004-10-10 22:51:53.000000000 +0900 +++ B/netstat.c 2004-10-11 03:01:51.610905846 +0900 @@ -58,6 +58,11 @@ * *990420 {1.38} Tuan Hoang removed a useless assignment from igmp_do_one() *20010404 {1.39} Arnaldo Carvalho de Melo - use setlocale + *20041011 {1.39} Kalin KOZHUHAROV + * cleaned up code for printing by defining some macros + * fixed truncating of IPv4 addresses mapped into IPv6 + * FIXME: write a better printer for column output :-| + * * * This program is free software; you can redistribute it * and/or modify it under the terms of the GNU General @@ -95,6 +100,31 @@ #include "interface.h" #include "util.h" +/* Usually an IPv4 socket address can be represented as aaa.bbb.ccc.ddd:ppppp or + 15+1+5+'\0'=22 octets. However with IPv6 this becomes 45+1+5+'\0'=52 octets max + if we keep the same number of ports (see /usr/include/netinet/in.h for + INET_ADDRSTRLEN and INET6_ADDRSTRLEN ). */ + +/* For now let's try to keep the old behaviour of aligning (if possible) to 23rd + column, but WITHOUT truncating the output (of longer addresses). + But why 23 and not 22 ??? + FIXME: Make a better macro for ALIGN_AT_LEN_FORMAT below */ +#define ALIGN_AT_LEN 23 +#define ALIGN_AT_LEN_FORMAT "%-23s" +#define SPRINT_ADDRESS(addr) \ +{char tmp_str[128]; \ +if (strlen(addr) <= ALIGN_AT_LEN) { \ + sprintf(tmp_str, ALIGN_AT_LEN_FORMAT,addr); \ + safe_strncpy(addr,tmp_str,sizeof(addr)); \ + }} + +/* Cut addresses at the length below to prevent possible (?) buffer overflows. + 52 is enough (<<128), but why is it needed at all? It was 22 in v1.39 */ +#define CUT_AT_LEN 66 +#define CUT_ADDR_AT_LEN(which) \ + if ((strlen(which) + strlen(buffer)) > CUT_AT_LEN)\ + which[CUT_AT_LEN - strlen(buffer)] = '\0'; + #define PROGNAME_WIDTH 20 #if !defined(s6_addr32) && defined(in6a_words) @@ -772,17 +802,13 @@ get_sname(htons(local_port), "tcp", flag_not & FLAG_NUM_PORT)); - if ((strlen(local_addr) + strlen(buffer)) > 22) - local_addr[22 - strlen(buffer)] = '\0'; - + CUT_ADDR_AT_LEN(local_addr) strcat(local_addr, ":"); strcat(local_addr, buffer); snprintf(buffer, sizeof(buffer), "%s", get_sname(htons(rem_port), "tcp", flag_not & FLAG_NUM_PORT)); - if ((strlen(rem_addr) + strlen(buffer)) > 22) - rem_addr[22 - strlen(buffer)] = '\0'; - + CUT_ADDR_AT_LEN(rem_addr) strcat(rem_addr, ":"); strcat(rem_addr, buffer); timers[0] = '\0'; @@ -813,7 +839,9 @@ timer_run, (double) time_len / HZ, retr, timeout); break; } - printf("tcp %6ld %6ld %-23s %-23s %-12s", + SPRINT_ADDRESS(local_addr) + SPRINT_ADDRESS(rem_addr) + printf("tcp %6ld %6ld %s %s %-12s", rxq, txq, local_addr, rem_addr, _(tcp_state[state])); finish_this_one(uid,inode,timers); @@ -922,8 +950,8 @@ snprintf(buffer, sizeof(buffer), "%s", get_sname(htons(local_port), "udp", flag_not & FLAG_NUM_PORT)); - if ((strlen(local_addr) + strlen(buffer)) > 22) - local_addr[22 - strlen(buffer)] = '\0'; + + CUT_ADDR_AT_LEN(local_addr) strcat(local_addr, ":"); strcat(local_addr, buffer); @@ -931,8 +959,8 @@ get_sname(htons(rem_port), "udp", flag_not & FLAG_NUM_PORT)); safe_strncpy(rem_addr, ap->sprint((struct sockaddr *) &remaddr, flag_not), sizeof(rem_addr)); - if ((strlen(rem_addr) + strlen(buffer)) > 22) - rem_addr[22 - strlen(buffer)] = '\0'; + + CUT_ADDR_AT_LEN(rem_addr) strcat(rem_addr, ":"); strcat(rem_addr, buffer); @@ -953,7 +981,9 @@ retr, timeout); break; } - printf("udp %6ld %6ld %-23s %-23s %-12s", + SPRINT_ADDRESS(local_addr) + SPRINT_ADDRESS(rem_addr) + printf("udp %6ld %6ld %s %s %-12s", rxq, txq, local_addr, rem_addr, udp_state); finish_this_one(uid,inode,timers); @@ -1041,8 +1071,8 @@ flag_not & FLAG_NUM_PORT)); safe_strncpy(local_addr, ap->sprint((struct sockaddr *) &localaddr, flag_not), sizeof(local_addr)); - if ((strlen(local_addr) + strlen(buffer)) > 22) - local_addr[22 - strlen(buffer)] = '\0'; + + CUT_ADDR_AT_LEN(local_addr) strcat(local_addr, ":"); strcat(local_addr, buffer); @@ -1050,8 +1080,8 @@ get_sname(htons(rem_port), "raw", flag_not & FLAG_NUM_PORT)); safe_strncpy(rem_addr, ap->sprint((struct sockaddr *) &remaddr, flag_not), sizeof(rem_addr)); - if ((strlen(rem_addr) + strlen(buffer)) > 22) - rem_addr[22 - strlen(buffer)] = '\0'; + + CUT_ADDR_AT_LEN(rem_addr) strcat(rem_addr, ":"); strcat(rem_addr, buffer); @@ -1074,7 +1104,9 @@ retr, timeout); break; } - printf("raw %6ld %6ld %-23s %-23s %-12d", + SPRINT_ADDRESS(local_addr) + SPRINT_ADDRESS(rem_addr) + printf("raw %6ld %6ld %s %s %-12d", rxq, txq, local_addr, rem_addr, state); finish_this_one(uid,inode,timers);