From 34711329c2fa7a5f97a7e8770497027dae5701c4 Mon Sep 17 00:00:00 2001 From: Dmitry Rakhchev Date: Mon, 4 Jul 2016 18:28:21 +0300 Subject: [PATCH] Add mac2str() and str2mac() to print/parse MAC address --- include/ipmitool/helper.h | 2 + lib/helper.c | 47 +++++++++++++++++++++++ lib/ipmi_delloem.c | 29 +++----------- lib/ipmi_lanp.c | 79 ++++++++++----------------------------- lib/ipmi_pef.c | 5 +-- lib/ipmi_session.c | 18 ++------- 6 files changed, 78 insertions(+), 102 deletions(-) diff --git a/include/ipmitool/helper.h b/include/ipmitool/helper.h index 81b8825..bfaf284 100644 --- a/include/ipmitool/helper.h +++ b/include/ipmitool/helper.h @@ -100,6 +100,8 @@ uint32_t buf2long(uint8_t * buf); #define BUF2STR_MAXIMUM_OUTPUT_SIZE (3*1024 + 1) const char * buf2str_extended(const uint8_t *buf, int len, const char *sep); const char * buf2str(const uint8_t *buf, int len); +int str2mac(const char *arg, uint8_t *buf); +const char * mac2str(const uint8_t *buf); int ipmi_parse_hex(const char *str, uint8_t *out, int size); void printbuf(const uint8_t * buf, int len, const char * desc); uint8_t ipmi_csum(uint8_t * d, int s); diff --git a/lib/helper.c b/lib/helper.c index ad5a898..de91438 100644 --- a/lib/helper.c +++ b/lib/helper.c @@ -234,6 +234,53 @@ void printbuf(const uint8_t * buf, int len, const char * desc) fprintf(stderr, "\n"); } +/* str2mac - parse-out MAC address from given string and store it + * into buffer. + * + * @arg: string to be parsed. + * @buf: buffer of 6 to hold parsed MAC address. + * + * returns zero on success, (-1) on error and error message is printed-out. + */ +int +str2mac(const char *arg, uint8_t *buf) +{ + unsigned int m1 = 0; + unsigned int m2 = 0; + unsigned int m3 = 0; + unsigned int m4 = 0; + unsigned int m5 = 0; + unsigned int m6 = 0; + if (sscanf(arg, "%02x:%02x:%02x:%02x:%02x:%02x", + &m1, &m2, &m3, &m4, &m5, &m6) != 6) { + lprintf(LOG_ERR, "Invalid MAC address: %s", arg); + return -1; + } + if (m1 > UINT8_MAX || m2 > UINT8_MAX + || m3 > UINT8_MAX || m4 > UINT8_MAX + || m5 > UINT8_MAX || m6 > UINT8_MAX) { + lprintf(LOG_ERR, "Invalid MAC address: %s", arg); + return -1; + } + buf[0] = (uint8_t)m1; + buf[1] = (uint8_t)m2; + buf[2] = (uint8_t)m3; + buf[3] = (uint8_t)m4; + buf[4] = (uint8_t)m5; + buf[5] = (uint8_t)m6; + return 0; +} + +/* mac2str -- return MAC address as a string + * + * @buf: buffer of 6 to hold parsed MAC address. + */ +const char * +mac2str(const uint8_t *buf) +{ + return buf2str_extended(buf, 6, ":"); +} + const char * val2str(uint16_t val, const struct valstr *vs) { static char un_str[32]; diff --git a/lib/ipmi_delloem.c b/lib/ipmi_delloem.c index b253e3e..bc78c1d 100644 --- a/lib/ipmi_delloem.c +++ b/lib/ipmi_delloem.c @@ -1652,11 +1652,7 @@ ipmi_macinfo_drac_idrac_virtual_mac(struct ipmi_intf* intf,uint8_t NicNum) printf("\niDRAC6 MAC Address "); } - for (j = 0; j < 5; j++) { - printf("%02x:", VirtualMacAddress[j]); - } - printf("%02x", VirtualMacAddress[j]); - printf("\n"); + printf("%s\n", mac2str(VirtualMacAddress)); return 0; } /* @@ -1724,11 +1720,7 @@ ipmi_macinfo_drac_idrac_mac(struct ipmi_intf* intf,uint8_t NicNum) printf("\niDRAC6 MAC Address "); } - for (j = 0; j < 5; j++) { - printf("%02x:", iDRAC6MacAddressByte[j]); - } - printf("%02x", iDRAC6MacAddressByte[j]); - printf("\n"); + printf("%s\n", mac2str(iDRAC6MacAddressByte)); return 0; } /* @@ -1786,13 +1778,8 @@ ipmi_macinfo_10g(struct ipmi_intf* intf, uint8_t NicNum) for (i = 0; i < Total_No_NICs; i++) { if ((0xff == NicNum) || (i == NicNum)) { printf("\n%d",i); - printf("\t\t"); - for (j = 0 ; j < 5; j++) { - printf("%02x:", - EmbeddedNICMacAddress_10G.MacAddress[i].MacAddressByte[j]); - } - printf("%02x", - EmbeddedNICMacAddress_10G.MacAddress[i].MacAddressByte[j]); + printf("\t\t%s", + mac2str(EmbeddedNICMacAddress_10G.MacAddress[i].MacAddressByte)); } } printf("\n"); @@ -1889,13 +1876,7 @@ ipmi_macinfo_11g(struct ipmi_intf* intf, uint8_t NicNum) if ((0xff==NicNum) || (NicNum == EmbeddedNICMacAddress.LOMMacAddress[i].NICNumber)) { printf("\n%d",EmbeddedNICMacAddress.LOMMacAddress[i].NICNumber); - printf("\t\t"); - for (j = 0; j < 5; j++) { - printf("%02x:", - EmbeddedNICMacAddress.LOMMacAddress[i].MacAddressByte[j]); - } - printf("%02x", - EmbeddedNICMacAddress.LOMMacAddress[i].MacAddressByte[j]); + printf("\t\t%s", mac2str(EmbeddedNICMacAddress.LOMMacAddress[i].MacAddressByte)); if (LOM_ETHERNET_ENABLED == EmbeddedNICMacAddress.LOMMacAddress[i].EthernetStatus) { diff --git a/lib/ipmi_lanp.c b/lib/ipmi_lanp.c index ce6af4a..16a3a3e 100644 --- a/lib/ipmi_lanp.c +++ b/lib/ipmi_lanp.c @@ -704,8 +704,7 @@ ipmi_lan_print(struct ipmi_intf * intf, uint8_t chan) if (p == NULL) return -1; if (p->data != NULL) - printf("%-24s: %02x:%02x:%02x:%02x:%02x:%02x\n", p->desc, - p->data[0], p->data[1], p->data[2], p->data[3], p->data[4], p->data[5]); + printf("%-24s: %s\n", p->desc, mac2str(p->data)); p = get_lan_param(intf, chan, IPMI_LANP_SNMP_STRING); if (p == NULL) @@ -744,8 +743,7 @@ ipmi_lan_print(struct ipmi_intf * intf, uint8_t chan) if (p == NULL) return -1; if (p->data != NULL) - printf("%-24s: %02x:%02x:%02x:%02x:%02x:%02x\n", p->desc, - p->data[0], p->data[1], p->data[2], p->data[3], p->data[4], p->data[5]); + printf("%-24s: %s\n", p->desc, mac2str(p->data)); p = get_lan_param(intf, chan, IPMI_LANP_BAK_GATEWAY_IP); if (p == NULL) @@ -758,8 +756,7 @@ ipmi_lan_print(struct ipmi_intf * intf, uint8_t chan) if (p == NULL) return -1; if (p->data != NULL) - printf("%-24s: %02x:%02x:%02x:%02x:%02x:%02x\n", p->desc, - p->data[0], p->data[1], p->data[2], p->data[3], p->data[4], p->data[5]); + printf("%-24s: %s\n", p->desc, mac2str(p->data)); p = get_lan_param(intf, chan, IPMI_LANP_VLAN_ID); if (p != NULL && p->data != NULL) { @@ -1103,42 +1100,6 @@ ipmi_set_user_access(struct ipmi_intf *intf, uint8_t channel, uint8_t user_id) } } -/* get_cmdline_macaddr - parse-out MAC address from given string and store it - * into buffer. - * - * @arg: string to be parsed. - * @buf: buffer of 6 to hold parsed MAC address. - * - * returns zero on success, (-1) on error and error message is printed-out. - */ -static int -get_cmdline_macaddr(char *arg, uint8_t *buf) -{ - uint32_t m1 = 0; - uint32_t m2 = 0; - uint32_t m3 = 0; - uint32_t m4 = 0; - uint32_t m5 = 0; - uint32_t m6 = 0; - if (sscanf(arg, "%02x:%02x:%02x:%02x:%02x:%02x", - &m1, &m2, &m3, &m4, &m5, &m6) != 6) { - lprintf(LOG_ERR, "Invalid MAC address: %s", arg); - return -1; - } - if (m1 > UINT8_MAX || m2 > UINT8_MAX - || m3 > UINT8_MAX || m4 > UINT8_MAX - || m5 > UINT8_MAX || m6 > UINT8_MAX) { - lprintf(LOG_ERR, "Invalid MAC address: %s", arg); - return -1; - } - buf[0] = (uint8_t)m1; - buf[1] = (uint8_t)m2; - buf[2] = (uint8_t)m3; - buf[3] = (uint8_t)m4; - buf[4] = (uint8_t)m5; - buf[5] = (uint8_t)m6; - return 0; -} static int @@ -1540,11 +1501,11 @@ ipmi_lan_set(struct ipmi_intf * intf, int argc, char ** argv) print_lan_set_usage(); return -1; } - rc = get_cmdline_macaddr(argv[2], data); + rc = str2mac(argv[2], data); if (rc == 0) { - printf("Setting LAN %s to %02x:%02x:%02x:%02x:%02x:%02x\n", - ipmi_lan_params[IPMI_LANP_MAC_ADDR].desc, - data[0], data[1], data[2], data[3], data[4], data[5]); + printf("Setting LAN %s to %s\n", + ipmi_lan_params[IPMI_LANP_MAC_ADDR].desc, + mac2str(data)); rc = set_lan_param(intf, chan, IPMI_LANP_MAC_ADDR, data, 6); } } @@ -1566,10 +1527,10 @@ ipmi_lan_set(struct ipmi_intf * intf, int argc, char ** argv) rc = set_lan_param(intf, chan, IPMI_LANP_DEF_GATEWAY_IP, data, 4); } else if ((strncmp(argv[2], "macaddr", 7) == 0) && - (get_cmdline_macaddr(argv[3], data) == 0)) { - printf("Setting LAN %s to %02x:%02x:%02x:%02x:%02x:%02x\n", - ipmi_lan_params[IPMI_LANP_DEF_GATEWAY_MAC].desc, - data[0], data[1], data[2], data[3], data[4], data[5]); + (str2mac(argv[3], data) == 0)) { + printf("Setting LAN %s to %s\n", + ipmi_lan_params[IPMI_LANP_DEF_GATEWAY_MAC].desc, + mac2str(data)); rc = set_lan_param(intf, chan, IPMI_LANP_DEF_GATEWAY_MAC, data, 6); } else { @@ -1595,10 +1556,10 @@ ipmi_lan_set(struct ipmi_intf * intf, int argc, char ** argv) rc = set_lan_param(intf, chan, IPMI_LANP_BAK_GATEWAY_IP, data, 4); } else if ((strncmp(argv[2], "macaddr", 7) == 0) && - (get_cmdline_macaddr(argv[3], data) == 0)) { - printf("Setting LAN %s to %02x:%02x:%02x:%02x:%02x:%02x\n", - ipmi_lan_params[IPMI_LANP_BAK_GATEWAY_MAC].desc, - data[0], data[1], data[2], data[3], data[4], data[5]); + (str2mac(argv[3], data) == 0)) { + printf("Setting LAN %s to %s\n", + ipmi_lan_params[IPMI_LANP_BAK_GATEWAY_MAC].desc, + mac2str(data)); rc = set_lan_param(intf, chan, IPMI_LANP_BAK_GATEWAY_MAC, data, 6); } else { @@ -1776,9 +1737,8 @@ ipmi_lan_alert_print(struct ipmi_intf * intf, uint8_t channel, uint8_t alert) printf("%-24s: %d.%d.%d.%d\n", "Alert IP Address", paddr[3], paddr[4], paddr[5], paddr[6]); - printf("%-24s: %02x:%02x:%02x:%02x:%02x:%02x\n", "Alert MAC Address", - paddr[7], paddr[8], paddr[9], - paddr[10], paddr[11], paddr[12]); + printf("%-24s: %s\n", "Alert MAC Address", + mac2str(&paddr[7])); printf("\n"); return 0; @@ -1843,7 +1803,7 @@ ipmi_lan_alert_set(struct ipmi_intf * intf, uint8_t chan, uint8_t alert, } /* alert destination mac address */ else if (strncasecmp(argv[0], "macaddr", 7) == 0 && - (get_cmdline_macaddr(argv[1], temp) == 0)) { + (str2mac(argv[1], temp) == 0)) { /* get current parameter */ p = get_lan_param_select(intf, chan, IPMI_LANP_DEST_ADDR, alert); if (p == NULL) { @@ -1853,8 +1813,7 @@ ipmi_lan_alert_set(struct ipmi_intf * intf, uint8_t chan, uint8_t alert, /* set new macaddr */ memcpy(data+7, temp, 6); printf("Setting LAN Alert %d MAC Address to " - "%02x:%02x:%02x:%02x:%02x:%02x\n", alert, - data[7], data[8], data[9], data[10], data[11], data[12]); + "%s\n", alert, mac2str(&data[7])); rc = set_lan_param_nowait(intf, chan, IPMI_LANP_DEST_ADDR, data, p->data_len); } /* alert destination gateway selector */ diff --git a/lib/ipmi_pef.c b/lib/ipmi_pef.c index 2f7e483..bbf25f2 100644 --- a/lib/ipmi_pef.c +++ b/lib/ipmi_pef.c @@ -720,10 +720,7 @@ ipmi_pef_print_lan_dest(struct ipmi_intf * intf, uint8_t ch, uint8_t dest) pinfo->ip[0], pinfo->ip[1], pinfo->ip[2], pinfo->ip[3]); ipmi_pef_print_str("IP address", buf); - sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", - pinfo->mac[0], pinfo->mac[1], pinfo->mac[2], - pinfo->mac[3], pinfo->mac[4], pinfo->mac[5]); - ipmi_pef_print_str("MAC address", buf); + ipmi_pef_print_str("MAC address", mac2str(pinfo->mac)); } } diff --git a/lib/ipmi_session.c b/lib/ipmi_session.c index 4855bc4..141f0f4 100644 --- a/lib/ipmi_session.c +++ b/lib/ipmi_session.c @@ -99,13 +99,8 @@ print_session_info_csv(const struct get_session_info_rsp * session_info, buffer, 16)); - printf(",%02x:%02x:%02x:%02x:%02x:%02x", - session_info->channel_data.lan_data.console_mac[0], - session_info->channel_data.lan_data.console_mac[1], - session_info->channel_data.lan_data.console_mac[2], - session_info->channel_data.lan_data.console_mac[3], - session_info->channel_data.lan_data.console_mac[4], - session_info->channel_data.lan_data.console_mac[5]); + printf(",%s", mac2str( + session_info->channel_data.lan_data.console_mac)); console_port_tmp = session_info->channel_data.lan_data.console_port; #if WORDS_BIGENDIAN @@ -187,13 +182,8 @@ print_session_info_verbose(const struct get_session_info_rsp * session_info, buffer, 16)); - printf("console mac : %02x:%02x:%02x:%02x:%02x:%02x\n", - session_info->channel_data.lan_data.console_mac[0], - session_info->channel_data.lan_data.console_mac[1], - session_info->channel_data.lan_data.console_mac[2], - session_info->channel_data.lan_data.console_mac[3], - session_info->channel_data.lan_data.console_mac[4], - session_info->channel_data.lan_data.console_mac[5]); + printf("console mac : %s\n", mac2str( + session_info->channel_data.lan_data.console_mac)); console_port_tmp = session_info->channel_data.lan_data.console_port; #if WORDS_BIGENDIAN