static size_t snprint_addr(char *dst, size_t dst_len, ip_t *addr) { if(addrcmp((void *) addr, (void *) &unspec_addr, af)) { struct hostent *host = dns ? addr2host((void *) addr, af) : NULL; if (!host) return snprintf(dst, dst_len, "%s", strlongip(addr)); else if (dns && show_ips) return snprintf(dst, dst_len, "%s (%s)", host->h_name, strlongip(addr)); else return snprintf(dst, dst_len, "%s", host->h_name); } else return snprintf(dst, dst_len, "%s", "???"); }
void report_close(void) { int i, j, at, max; ip_t *addr; char name[81]; char buf[1024]; char fmt[16]; int len=0; int len_hosts = 33; struct hostent *host; if (reportwide) { // get the longest hostname len_hosts = strlen(LocalHostname); max = net_max(); at = net_min(); for (; at < max; at++) { addr = net_addr(at); if( addrcmp( (void *) addr, (void *) &unspec_addr, af ) != 0 ) { host = dns ? addr2host( (void *) addr, af ) : NULL; if (host != NULL) { strncpy( name, host->h_name, (sizeof name) - 1 ); name[ (sizeof name) - 1 ] = '\0'; } else { snprintf(name, sizeof(name), "%s", strlongip( addr ) ); } if (len_hosts < strlen(name)) { len_hosts = strlen(name); } } } } snprintf( fmt, sizeof(fmt), "HOST: %%-%ds", len_hosts); snprintf(buf, sizeof(buf), fmt, LocalHostname); len = reportwide ? strlen(buf) : len_hosts; for( i=0; i<MAXFLD; i++ ) { j = fld_index[fld_active[i]]; if (j < 0) continue; snprintf( fmt, sizeof(fmt), "%%%ds", data_fields[j].length ); snprintf( buf + len, sizeof(buf), fmt, data_fields[j].title ); len += data_fields[j].length; } printf("%s\n",buf); max = net_max(); at = net_min(); for(; at < max; at++) { addr = net_addr(at); if( addrcmp( (void *) addr, (void *) &unspec_addr, af ) == 0 ) { sprintf(name, "???"); } else { host = dns ? addr2host( (void *) addr, af ) : NULL; if (host != NULL) { strncpy( name, host->h_name, (sizeof name) - 1 ); name[ (sizeof name) - 1 ] = '\0'; } else { snprintf(name, sizeof(name), "%s", strlongip( addr ) ); } } snprintf( fmt, sizeof(fmt), " %%2d. %%-%ds", len_hosts); snprintf(buf, sizeof(buf), fmt, at+1, name); len = reportwide ? strlen(buf) : len_hosts; for( i=0; i<MAXFLD; i++ ) { j = fld_index[fld_active [i]]; if (j < 0) continue; /* 1000.0 is a temporay hack for stats usec to ms, impacted net_loss. */ if( index( data_fields[j].format, 'f' ) ) { snprintf( buf + len, sizeof(buf), data_fields[j].format, data_fields[j].net_xxx(at) /1000.0 ); } else { snprintf( buf + len, sizeof(buf), data_fields[j].format, data_fields[j].net_xxx(at) ); } len += data_fields[j].length; } printf("%s\n",buf); } }
void csv_close(void) { int i, j, at, max; ip_t *addr; char name[81]; struct hostent *host; /* Caption */ printf("<SRC=%s DST=%s", LocalHostname, Hostname); printf(" TOS=0x%X", tos); if(cpacketsize >= 0) { printf(" PSIZE=%d", cpacketsize); } else { printf(" PSIZE=rand(%d-%d)",MINPACKET, -cpacketsize); } if( bitpattern>=0 ) { printf(" BITPATTERN=0x%02X", (unsigned char)(bitpattern)); } else { printf(" BITPATTERN=rand(0x00-FF)"); } printf(" TESTS=%d>\n", MaxPing); /* Header */ printf("HUPCOUNT, HOST"); for( i=0; i<MAXFLD; i++ ) { j = fld_index[fld_active[i]]; if (j < 0) continue; printf( ", %s", data_fields[j].title ); } printf("\n"); max = net_max(); at = net_min(); for(; at < max; at++) { addr = net_addr(at); if( addrcmp( (void *) addr, (void *) &unspec_addr, af ) == 0 ) { sprintf(name, "???"); } else { host = dns ? addr2host( (void *) addr, af ) : NULL; if (host != NULL) { strncpy( name, host->h_name, (sizeof name) - 1 ); name[ (sizeof name) - 1 ] = '\0'; } else { sprintf(name, "%s", strlongip( addr ) ); } } printf("%d, %s", at+1, name); for( i=0; i<MAXFLD; i++ ) { j = fld_index[fld_active[j]]; if (j < 0) continue; /* 1000.0 is a temporay hack for stats usec to ms, impacted net_loss. */ if( index( data_fields[j].format, 'f' ) ) { printf( ", %.2f", data_fields[j].net_xxx(at) / 1000.0); } else { printf( ", %d", data_fields[j].net_xxx(at) ); } } printf("\n"); } }