From cb6fe86292fd85a67dfd930cdc351fec0ddf96bc Mon Sep 17 00:00:00 2001 From: Håkon Nessjøen Date: Thu, 24 Feb 2011 16:46:30 +0100 Subject: Code cleanup --- macping.c | 10 +++++++--- mactelnet.c | 48 ++++-------------------------------------------- protocol.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ protocol.h | 1 + 4 files changed, 65 insertions(+), 47 deletions(-) diff --git a/macping.c b/macping.c index 1f23e04..7731176 100644 --- a/macping.c +++ b/macping.c @@ -89,7 +89,7 @@ static void setup_devices() { } } -long long int toddiff(struct timeval *tod1, struct timeval *tod2) +static long long int toddiff(struct timeval *tod1, struct timeval *tod2) { long long t1, t2; t1 = tod1->tv_sec * 1000000 + tod1->tv_usec; @@ -97,7 +97,7 @@ long long int toddiff(struct timeval *tod1, struct timeval *tod2) return t1 - t2; } -void display_results() { +static void display_results() { int percent = (int)((100.f/ping_sent) * pong_received); if (percent > 100) percent = 0; @@ -194,7 +194,11 @@ int main(int argc, char **argv) { return 1; } - ether_aton_r(argv[optind], (struct ether_addr *)dstmac); + /* Get mac-address from string, or check for hostname via mndp */ + if (!query_mndp_verbose(argv[optind], dstmac)) { + /* No valid mac address found, abort */ + return 1; + } /* Open a UDP socket handle */ sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); diff --git a/mactelnet.c b/mactelnet.c index e69c88f..2b2f74a 100644 --- a/mactelnet.c +++ b/mactelnet.c @@ -461,50 +461,10 @@ int main (int argc, char **argv) { /* Need to use, to be able to autodetect which interface to use */ setsockopt(insockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (optval)); - /* Check for identity name or mac address */ - { - char *p = argv[optind]; - int colons = 0; - int dashs = 0; - while (*p++) { - if (*p == ':') { - colons++; - } - else if (*p == '-') { - dashs++; - } - } - - /* - * Windows users often enter macs with dash instead - * of colon. - */ - if (colons == 0 && dashs == 5) { - p = argv[optind]; - while (*p++) { - if (*p == '-') { - *p = ':'; - } - } - colons = dashs; - } - - if (colons != 5) { - fprintf(stderr, "Searching for '%s'...", argv[optind]); - - /* Search for Router by identity name, using MNDP */ - if (!query_mndp(argv[optind], dstmac)) { - fprintf(stderr, "not found\n"); - return 1; - } - - /* Router found, display mac and continue */ - fprintf(stderr, "found\n"); - - } else { - /* Convert mac address string to ether_addr struct */ - ether_aton_r(argv[optind], (struct ether_addr *)dstmac); - } + /* Get mac-address from string, or check for hostname via mndp */ + if (!query_mndp_verbose(argv[optind], dstmac)) { + /* No valid mac address found, abort */ + return 1; } if (!have_username) { diff --git a/protocol.c b/protocol.c index a207494..2de5833 100644 --- a/protocol.c +++ b/protocol.c @@ -472,3 +472,56 @@ done: close(sock); return 0; } + +/* + * This function accepts either a full MAC address using : or - as seperators. + * Or a router hostname. The hostname will be searched for via MNDP broadcast packets. + */ +int query_mndp_verbose(char *address, unsigned char *dstmac) { + char *p = address; + int colons = 0; + int dashs = 0; + + while (*p++) { + if (*p == ':') { + colons++; + } + else if (*p == '-') { + dashs++; + } + } + + /* + * Windows users often enter macs with dash instead + * of colon. + */ + if (colons == 0 && dashs == 5) { + p = address; + while (*p++) { + if (*p == '-') { + *p = ':'; + } + } + colons = dashs; + } + + if (colons != 5) { + /* + * Not a valid mac-address. + * Search for Router by identity name, using MNDP + */ + fprintf(stderr, "Searching for '%s'...", address); + if (!query_mndp(address, dstmac)) { + fprintf(stderr, "not found\n"); + return 0; + } + + /* Router found, display mac and continue */ + fprintf(stderr, "found\n"); + } else { + /* Convert mac address string to ether_addr struct */ + ether_aton_r(address, (struct ether_addr *)dstmac); + } + + return 1; +} diff --git a/protocol.h b/protocol.h index c1cca1f..a2c5d8b 100644 --- a/protocol.h +++ b/protocol.h @@ -127,6 +127,7 @@ extern int mndp_add_attribute(struct mt_packet *packet, enum mt_mndp_attrtype at extern struct mt_mndp_info *parse_mndp(const unsigned char *data, const int packet_len); int query_mndp(const char *identity, unsigned char *mac); +int query_mndp_verbose(char *address, unsigned char *dstmac); /* Number of milliseconds between each retransmission */ #define MAX_RETRANSMIT_INTERVALS 9 -- cgit v1.2.3