From: Vaibhav Bajpai Date: Sun, 5 May 2013 12:44:46 +0000 (+0200) Subject: --csv allows multiple service names as arguments X-Git-Tag: v0.85~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d7d03cce9afa2d906c6e2ed40266e4a1ec069c28;p=thirdparty%2Fmtr.git --csv allows multiple service names as arguments --- diff --git a/display.c b/display.c index ee3934d..cc5cd59 100644 --- a/display.c +++ b/display.c @@ -112,7 +112,7 @@ void display_open(void) } -void display_close(void) +void display_close(time_t now) { switch(DisplayMode) { case DisplayReport: @@ -125,7 +125,7 @@ void display_close(void) xml_close(); break; case DisplayCSV: - csv_close(); + csv_close(now); break; case DisplayCurses: #ifndef NO_IPINFO diff --git a/display.h b/display.h index 486e25d..4ed2d66 100644 --- a/display.h +++ b/display.h @@ -32,7 +32,7 @@ enum { DisplayReport, DisplayCurses, DisplayGTK, DisplaySplit, /* Prototypes for display.c */ void display_detect(int *argc, char ***argv); void display_open(void); -void display_close(void); +void display_close(time_t now); void display_redraw(void); void display_rawping(int hostnum, int msec); void display_rawhost(int hostnum, ip_t *ip_addr); diff --git a/mtr.c b/mtr.c index 39a618f..7c6feeb 100644 --- a/mtr.c +++ b/mtr.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "mtr.h" #include "mtr-curses.h" @@ -348,11 +349,6 @@ void parse_arg (int argc, char **argv) if (optind > argc - 1) return; - Hostname = argv[optind++]; - - if (argc > optind) { - cpacketsize = atoi (argv[optind]); - } } @@ -446,84 +442,93 @@ int main(int argc, char **argv) #endif "\t\t[--psize=bytes/-s bytes]\n" /* ok */ "\t\t[--report-wide|-w] [-u|-T] [--port=PORT] [--timeout=SECONDS]\n" /* rew */ - "\t\t[--interval=SECONDS] HOSTNAME [PACKETSIZE]\n", argv[0]); + "\t\t[--interval=SECONDS] HOSTNAME\n", argv[0]); exit(0); } - if (Hostname == NULL) Hostname = "localhost"; + time_t now = time(NULL); - if (gethostname(LocalHostname, sizeof(LocalHostname))) { - strcpy(LocalHostname, "UNKNOWNHOST"); - } + while (optind < argc ) { - if (net_preopen_result != 0) { - fprintf(stderr, "mtr: Unable to get raw socket. (Executable not suid?)\n"); - exit(1); - } + Hostname = argv[optind++]; + if (Hostname == NULL) Hostname = "localhost"; + if (gethostname(LocalHostname, sizeof(LocalHostname))) { + strcpy(LocalHostname, "UNKNOWNHOST"); + } + + if (net_preopen_result != 0) { + fprintf(stderr, "mtr: Unable to get raw socket. (Executable not suid?)\n"); + exit(1); + } #ifdef ENABLE_IPV6 - /* gethostbyname2() is deprecated so we'll use getaddrinfo() instead. */ - bzero( &hints, sizeof hints ); - hints.ai_family = af; - hints.ai_socktype = SOCK_DGRAM; - error = getaddrinfo( Hostname, NULL, &hints, &res ); - if ( error ) { - if (error == EAI_SYSTEM) - perror ("Failed to resolve host"); - else - fprintf (stderr, "Failed to resolve host: %s\n", gai_strerror(error)); - exit( EXIT_FAILURE ); - } - /* Convert the first addrinfo into a hostent. */ - host = &trhost; - bzero( host, sizeof trhost ); - host->h_name = res->ai_canonname; - host->h_aliases = NULL; - host->h_addrtype = res->ai_family; - af = res->ai_family; - host->h_length = res->ai_addrlen; - host->h_addr_list = alptr; - switch ( af ) { - case AF_INET: - sa4 = (struct sockaddr_in *) res->ai_addr; - alptr[0] = (void *) &(sa4->sin_addr); - break; - case AF_INET6: - sa6 = (struct sockaddr_in6 *) res->ai_addr; - alptr[0] = (void *) &(sa6->sin6_addr); - break; - default: - fprintf( stderr, "mtr unknown address type\n" ); - exit( EXIT_FAILURE ); - } - alptr[1] = NULL; + /* gethostbyname2() is deprecated so we'll use getaddrinfo() instead. */ + bzero( &hints, sizeof hints ); + hints.ai_family = af; + hints.ai_socktype = SOCK_DGRAM; + error = getaddrinfo( Hostname, NULL, &hints, &res ); + if ( error ) { + if (error == EAI_SYSTEM) + perror ("Failed to resolve host"); + else + fprintf (stderr, "Failed to resolve host: %s\n", gai_strerror(error)); + exit( EXIT_FAILURE ); + } + /* Convert the first addrinfo into a hostent. */ + host = &trhost; + bzero( host, sizeof trhost ); + host->h_name = res->ai_canonname; + host->h_aliases = NULL; + host->h_addrtype = res->ai_family; + af = res->ai_family; + host->h_length = res->ai_addrlen; + host->h_addr_list = alptr; + switch ( af ) { + case AF_INET: + sa4 = (struct sockaddr_in *) res->ai_addr; + alptr[0] = (void *) &(sa4->sin_addr); + break; + case AF_INET6: + sa6 = (struct sockaddr_in6 *) res->ai_addr; + alptr[0] = (void *) &(sa6->sin6_addr); + break; + default: + fprintf( stderr, "mtr unknown address type\n" ); + exit( EXIT_FAILURE ); + } + alptr[1] = NULL; #else - host = gethostbyname(Hostname); - if (host == NULL) { - herror("mtr gethostbyname"); - exit(1); - } - af = host->h_addrtype; + host = gethostbyname(Hostname); + if (host == NULL) { + herror("mtr gethostbyname"); + exit(1); + } + af = host->h_addrtype; #endif - if (net_open(host) != 0) { - fprintf(stderr, "mtr: Unable to start net module.\n"); - exit(1); - } + if (net_open(host) != 0) { + fprintf(stderr, "mtr: Unable to start net module.\n"); + exit(1); + } - if (net_set_interfaceaddress (InterfaceAddress) != 0) { - fprintf( stderr, "mtr: Couldn't set interface address.\n" ); - exit( EXIT_FAILURE ); - } + if (net_set_interfaceaddress (InterfaceAddress) != 0) { + fprintf( stderr, "mtr: Couldn't set interface address.\n" ); + exit( EXIT_FAILURE ); + } + + display_open(); + dns_open(); - display_open(); - dns_open(); + display_mode = 0; + display_loop(); - display_mode = 0; - display_loop(); + net_end_transit(); + display_close(now); + + if ( DisplayMode != DisplayCSV ) break; + + } - net_end_transit(); - display_close(); net_close(); return 0; diff --git a/report.c b/report.c index da1c1e3..72e2f55 100644 --- a/report.c +++ b/report.c @@ -344,12 +344,11 @@ char *trimwhitespace(char *str) { } -void csv_close(void) +void csv_close(time_t now) { int i, j, at, max; ip_t *addr; char name[81]; - time_t now = time(NULL); for( i=0; i