Add mac2str() and str2mac() to print/parse MAC address

This commit is contained in:
Dmitry Rakhchev 2016-07-04 18:28:21 +03:00 committed by Zdenek Styblik
parent 2f76ab3d3a
commit 34711329c2
6 changed files with 78 additions and 102 deletions

View File

@ -100,6 +100,8 @@ uint32_t buf2long(uint8_t * buf);
#define BUF2STR_MAXIMUM_OUTPUT_SIZE (3*1024 + 1) #define BUF2STR_MAXIMUM_OUTPUT_SIZE (3*1024 + 1)
const char * buf2str_extended(const uint8_t *buf, int len, const char *sep); const char * buf2str_extended(const uint8_t *buf, int len, const char *sep);
const char * buf2str(const uint8_t *buf, int len); 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); int ipmi_parse_hex(const char *str, uint8_t *out, int size);
void printbuf(const uint8_t * buf, int len, const char * desc); void printbuf(const uint8_t * buf, int len, const char * desc);
uint8_t ipmi_csum(uint8_t * d, int s); uint8_t ipmi_csum(uint8_t * d, int s);

View File

@ -234,6 +234,53 @@ void printbuf(const uint8_t * buf, int len, const char * desc)
fprintf(stderr, "\n"); 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) const char * val2str(uint16_t val, const struct valstr *vs)
{ {
static char un_str[32]; static char un_str[32];

View File

@ -1652,11 +1652,7 @@ ipmi_macinfo_drac_idrac_virtual_mac(struct ipmi_intf* intf,uint8_t NicNum)
printf("\niDRAC6 MAC Address "); printf("\niDRAC6 MAC Address ");
} }
for (j = 0; j < 5; j++) { printf("%s\n", mac2str(VirtualMacAddress));
printf("%02x:", VirtualMacAddress[j]);
}
printf("%02x", VirtualMacAddress[j]);
printf("\n");
return 0; return 0;
} }
/* /*
@ -1724,11 +1720,7 @@ ipmi_macinfo_drac_idrac_mac(struct ipmi_intf* intf,uint8_t NicNum)
printf("\niDRAC6 MAC Address "); printf("\niDRAC6 MAC Address ");
} }
for (j = 0; j < 5; j++) { printf("%s\n", mac2str(iDRAC6MacAddressByte));
printf("%02x:", iDRAC6MacAddressByte[j]);
}
printf("%02x", iDRAC6MacAddressByte[j]);
printf("\n");
return 0; return 0;
} }
/* /*
@ -1786,13 +1778,8 @@ ipmi_macinfo_10g(struct ipmi_intf* intf, uint8_t NicNum)
for (i = 0; i < Total_No_NICs; i++) { for (i = 0; i < Total_No_NICs; i++) {
if ((0xff == NicNum) || (i == NicNum)) { if ((0xff == NicNum) || (i == NicNum)) {
printf("\n%d",i); printf("\n%d",i);
printf("\t\t"); printf("\t\t%s",
for (j = 0 ; j < 5; j++) { mac2str(EmbeddedNICMacAddress_10G.MacAddress[i].MacAddressByte));
printf("%02x:",
EmbeddedNICMacAddress_10G.MacAddress[i].MacAddressByte[j]);
}
printf("%02x",
EmbeddedNICMacAddress_10G.MacAddress[i].MacAddressByte[j]);
} }
} }
printf("\n"); printf("\n");
@ -1889,13 +1876,7 @@ ipmi_macinfo_11g(struct ipmi_intf* intf, uint8_t NicNum)
if ((0xff==NicNum) if ((0xff==NicNum)
|| (NicNum == EmbeddedNICMacAddress.LOMMacAddress[i].NICNumber)) { || (NicNum == EmbeddedNICMacAddress.LOMMacAddress[i].NICNumber)) {
printf("\n%d",EmbeddedNICMacAddress.LOMMacAddress[i].NICNumber); printf("\n%d",EmbeddedNICMacAddress.LOMMacAddress[i].NICNumber);
printf("\t\t"); printf("\t\t%s", mac2str(EmbeddedNICMacAddress.LOMMacAddress[i].MacAddressByte));
for (j = 0; j < 5; j++) {
printf("%02x:",
EmbeddedNICMacAddress.LOMMacAddress[i].MacAddressByte[j]);
}
printf("%02x",
EmbeddedNICMacAddress.LOMMacAddress[i].MacAddressByte[j]);
if (LOM_ETHERNET_ENABLED if (LOM_ETHERNET_ENABLED
== EmbeddedNICMacAddress.LOMMacAddress[i].EthernetStatus) { == EmbeddedNICMacAddress.LOMMacAddress[i].EthernetStatus) {

View File

@ -704,8 +704,7 @@ ipmi_lan_print(struct ipmi_intf * intf, uint8_t chan)
if (p == NULL) if (p == NULL)
return -1; return -1;
if (p->data != NULL) if (p->data != NULL)
printf("%-24s: %02x:%02x:%02x:%02x:%02x:%02x\n", p->desc, printf("%-24s: %s\n", p->desc, mac2str(p->data));
p->data[0], p->data[1], p->data[2], p->data[3], p->data[4], p->data[5]);
p = get_lan_param(intf, chan, IPMI_LANP_SNMP_STRING); p = get_lan_param(intf, chan, IPMI_LANP_SNMP_STRING);
if (p == NULL) if (p == NULL)
@ -744,8 +743,7 @@ ipmi_lan_print(struct ipmi_intf * intf, uint8_t chan)
if (p == NULL) if (p == NULL)
return -1; return -1;
if (p->data != NULL) if (p->data != NULL)
printf("%-24s: %02x:%02x:%02x:%02x:%02x:%02x\n", p->desc, printf("%-24s: %s\n", p->desc, mac2str(p->data));
p->data[0], p->data[1], p->data[2], p->data[3], p->data[4], p->data[5]);
p = get_lan_param(intf, chan, IPMI_LANP_BAK_GATEWAY_IP); p = get_lan_param(intf, chan, IPMI_LANP_BAK_GATEWAY_IP);
if (p == NULL) if (p == NULL)
@ -758,8 +756,7 @@ ipmi_lan_print(struct ipmi_intf * intf, uint8_t chan)
if (p == NULL) if (p == NULL)
return -1; return -1;
if (p->data != NULL) if (p->data != NULL)
printf("%-24s: %02x:%02x:%02x:%02x:%02x:%02x\n", p->desc, printf("%-24s: %s\n", p->desc, mac2str(p->data));
p->data[0], p->data[1], p->data[2], p->data[3], p->data[4], p->data[5]);
p = get_lan_param(intf, chan, IPMI_LANP_VLAN_ID); p = get_lan_param(intf, chan, IPMI_LANP_VLAN_ID);
if (p != NULL && p->data != NULL) { 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 static int
@ -1540,11 +1501,11 @@ ipmi_lan_set(struct ipmi_intf * intf, int argc, char ** argv)
print_lan_set_usage(); print_lan_set_usage();
return -1; return -1;
} }
rc = get_cmdline_macaddr(argv[2], data); rc = str2mac(argv[2], data);
if (rc == 0) { if (rc == 0) {
printf("Setting LAN %s to %02x:%02x:%02x:%02x:%02x:%02x\n", printf("Setting LAN %s to %s\n",
ipmi_lan_params[IPMI_LANP_MAC_ADDR].desc, ipmi_lan_params[IPMI_LANP_MAC_ADDR].desc,
data[0], data[1], data[2], data[3], data[4], data[5]); mac2str(data));
rc = set_lan_param(intf, chan, IPMI_LANP_MAC_ADDR, data, 6); 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); rc = set_lan_param(intf, chan, IPMI_LANP_DEF_GATEWAY_IP, data, 4);
} }
else if ((strncmp(argv[2], "macaddr", 7) == 0) && else if ((strncmp(argv[2], "macaddr", 7) == 0) &&
(get_cmdline_macaddr(argv[3], data) == 0)) { (str2mac(argv[3], data) == 0)) {
printf("Setting LAN %s to %02x:%02x:%02x:%02x:%02x:%02x\n", printf("Setting LAN %s to %s\n",
ipmi_lan_params[IPMI_LANP_DEF_GATEWAY_MAC].desc, ipmi_lan_params[IPMI_LANP_DEF_GATEWAY_MAC].desc,
data[0], data[1], data[2], data[3], data[4], data[5]); mac2str(data));
rc = set_lan_param(intf, chan, IPMI_LANP_DEF_GATEWAY_MAC, data, 6); rc = set_lan_param(intf, chan, IPMI_LANP_DEF_GATEWAY_MAC, data, 6);
} }
else { 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); rc = set_lan_param(intf, chan, IPMI_LANP_BAK_GATEWAY_IP, data, 4);
} }
else if ((strncmp(argv[2], "macaddr", 7) == 0) && else if ((strncmp(argv[2], "macaddr", 7) == 0) &&
(get_cmdline_macaddr(argv[3], data) == 0)) { (str2mac(argv[3], data) == 0)) {
printf("Setting LAN %s to %02x:%02x:%02x:%02x:%02x:%02x\n", printf("Setting LAN %s to %s\n",
ipmi_lan_params[IPMI_LANP_BAK_GATEWAY_MAC].desc, ipmi_lan_params[IPMI_LANP_BAK_GATEWAY_MAC].desc,
data[0], data[1], data[2], data[3], data[4], data[5]); mac2str(data));
rc = set_lan_param(intf, chan, IPMI_LANP_BAK_GATEWAY_MAC, data, 6); rc = set_lan_param(intf, chan, IPMI_LANP_BAK_GATEWAY_MAC, data, 6);
} }
else { 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", printf("%-24s: %d.%d.%d.%d\n", "Alert IP Address",
paddr[3], paddr[4], paddr[5], paddr[6]); paddr[3], paddr[4], paddr[5], paddr[6]);
printf("%-24s: %02x:%02x:%02x:%02x:%02x:%02x\n", "Alert MAC Address", printf("%-24s: %s\n", "Alert MAC Address",
paddr[7], paddr[8], paddr[9], mac2str(&paddr[7]));
paddr[10], paddr[11], paddr[12]);
printf("\n"); printf("\n");
return 0; return 0;
@ -1843,7 +1803,7 @@ ipmi_lan_alert_set(struct ipmi_intf * intf, uint8_t chan, uint8_t alert,
} }
/* alert destination mac address */ /* alert destination mac address */
else if (strncasecmp(argv[0], "macaddr", 7) == 0 && else if (strncasecmp(argv[0], "macaddr", 7) == 0 &&
(get_cmdline_macaddr(argv[1], temp) == 0)) { (str2mac(argv[1], temp) == 0)) {
/* get current parameter */ /* get current parameter */
p = get_lan_param_select(intf, chan, IPMI_LANP_DEST_ADDR, alert); p = get_lan_param_select(intf, chan, IPMI_LANP_DEST_ADDR, alert);
if (p == NULL) { if (p == NULL) {
@ -1853,8 +1813,7 @@ ipmi_lan_alert_set(struct ipmi_intf * intf, uint8_t chan, uint8_t alert,
/* set new macaddr */ /* set new macaddr */
memcpy(data+7, temp, 6); memcpy(data+7, temp, 6);
printf("Setting LAN Alert %d MAC Address to " printf("Setting LAN Alert %d MAC Address to "
"%02x:%02x:%02x:%02x:%02x:%02x\n", alert, "%s\n", alert, mac2str(&data[7]));
data[7], data[8], data[9], data[10], data[11], data[12]);
rc = set_lan_param_nowait(intf, chan, IPMI_LANP_DEST_ADDR, data, p->data_len); rc = set_lan_param_nowait(intf, chan, IPMI_LANP_DEST_ADDR, data, p->data_len);
} }
/* alert destination gateway selector */ /* alert destination gateway selector */

View File

@ -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]); pinfo->ip[0], pinfo->ip[1], pinfo->ip[2], pinfo->ip[3]);
ipmi_pef_print_str("IP address", buf); ipmi_pef_print_str("IP address", buf);
sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", ipmi_pef_print_str("MAC address", mac2str(pinfo->mac));
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);
} }
} }

View File

@ -99,13 +99,8 @@ print_session_info_csv(const struct get_session_info_rsp * session_info,
buffer, buffer,
16)); 16));
printf(",%02x:%02x:%02x:%02x:%02x:%02x", printf(",%s", mac2str(
session_info->channel_data.lan_data.console_mac[0], session_info->channel_data.lan_data.console_mac));
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]);
console_port_tmp = session_info->channel_data.lan_data.console_port; console_port_tmp = session_info->channel_data.lan_data.console_port;
#if WORDS_BIGENDIAN #if WORDS_BIGENDIAN
@ -187,13 +182,8 @@ print_session_info_verbose(const struct get_session_info_rsp * session_info,
buffer, buffer,
16)); 16));
printf("console mac : %02x:%02x:%02x:%02x:%02x:%02x\n", printf("console mac : %s\n", mac2str(
session_info->channel_data.lan_data.console_mac[0], session_info->channel_data.lan_data.console_mac));
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]);
console_port_tmp = session_info->channel_data.lan_data.console_port; console_port_tmp = session_info->channel_data.lan_data.console_port;
#if WORDS_BIGENDIAN #if WORDS_BIGENDIAN