ETHER: When gathering host NIC address, avoid invoking non existent programs
Historically on *nix platforms ifconfig was used to find the host system's interface MAC address. This isn't always available on all systems since it's being replaced by the ip command. We now only invoke commands that exist.
This commit is contained in:
parent
6d04cf814a
commit
153d849c13
1 changed files with 39 additions and 27 deletions
66
sim_ether.c
66
sim_ether.c
|
@ -1753,6 +1753,11 @@ static void eth_get_nic_hw_addr(ETH_DEV* dev, const char *devname)
|
||||||
char command[1024];
|
char command[1024];
|
||||||
FILE *f;
|
FILE *f;
|
||||||
int i;
|
int i;
|
||||||
|
char tool[CBUFSIZE];
|
||||||
|
const char *turnon[] = {
|
||||||
|
"ip link set dev %.*s up",
|
||||||
|
"ifconfig %.*s up",
|
||||||
|
NULL};
|
||||||
const char *patterns[] = {
|
const char *patterns[] = {
|
||||||
"ip link show %.*s | grep [0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]",
|
"ip link show %.*s | grep [0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]",
|
||||||
"ip link show %.*s | egrep [0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]",
|
"ip link show %.*s | egrep [0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]:[0-9a-fA-F]?[0-9a-fA-F]",
|
||||||
|
@ -1762,40 +1767,47 @@ static void eth_get_nic_hw_addr(ETH_DEV* dev, const char *devname)
|
||||||
|
|
||||||
memset(command, 0, sizeof(command));
|
memset(command, 0, sizeof(command));
|
||||||
/* try to force an otherwise unused interface to be turned on */
|
/* try to force an otherwise unused interface to be turned on */
|
||||||
snprintf(command, sizeof(command)-1, "ip link set dev %.*s up", (int)(sizeof(command) - 21), devname);
|
for (i=0; turnon[i]; ++i) {
|
||||||
if (system(command)) {};
|
snprintf(command, sizeof(command), turnon[i], (int)(sizeof(command) - (2 + strlen(patterns[i]))), devname);
|
||||||
snprintf(command, sizeof(command)-1, "ifconfig %.*s up", (int)(sizeof(command) - 14), devname);
|
get_glyph_nc (command, tool, 0);
|
||||||
if (system(command)) {};
|
if (sim_get_tool_path (tool)[0]) {
|
||||||
|
if (NULL != (f = popen(command, "r")))
|
||||||
|
pclose(f);
|
||||||
|
}
|
||||||
|
}
|
||||||
for (i=0; patterns[i] && (0 == dev->have_host_nic_phy_addr); ++i) {
|
for (i=0; patterns[i] && (0 == dev->have_host_nic_phy_addr); ++i) {
|
||||||
snprintf(command, sizeof(command)-1, patterns[i], (int)(sizeof(command) - (2 + strlen(patterns[i]))), devname);
|
snprintf(command, sizeof(command), patterns[i], (int)(sizeof(command) - (2 + strlen(patterns[i]))), devname);
|
||||||
if (NULL != (f = popen(command, "r"))) {
|
get_glyph_nc (command, tool, 0);
|
||||||
while (0 == dev->have_host_nic_phy_addr) {
|
if (sim_get_tool_path (tool)[0]) {
|
||||||
if (fgets(command, sizeof(command)-1, f)) {
|
if (NULL != (f = popen(command, "r"))) {
|
||||||
char *p1, *p2;
|
while (0 == dev->have_host_nic_phy_addr) {
|
||||||
|
if (fgets(command, sizeof(command)-1, f)) {
|
||||||
|
char *p1, *p2;
|
||||||
|
|
||||||
p1 = strchr(command, ':');
|
p1 = strchr(command, ':');
|
||||||
while (p1) {
|
while (p1) {
|
||||||
p2 = strchr(p1+1, ':');
|
p2 = strchr(p1+1, ':');
|
||||||
if (p2 <= p1+3) {
|
if (p2 <= p1+3) {
|
||||||
unsigned int mac_bytes[6];
|
unsigned int mac_bytes[6];
|
||||||
if (6 == sscanf(p1-2, "%02x:%02x:%02x:%02x:%02x:%02x", &mac_bytes[0], &mac_bytes[1], &mac_bytes[2], &mac_bytes[3], &mac_bytes[4], &mac_bytes[5])) {
|
if (6 == sscanf(p1-2, "%02x:%02x:%02x:%02x:%02x:%02x", &mac_bytes[0], &mac_bytes[1], &mac_bytes[2], &mac_bytes[3], &mac_bytes[4], &mac_bytes[5])) {
|
||||||
dev->host_nic_phy_hw_addr[0] = mac_bytes[0];
|
dev->host_nic_phy_hw_addr[0] = mac_bytes[0];
|
||||||
dev->host_nic_phy_hw_addr[1] = mac_bytes[1];
|
dev->host_nic_phy_hw_addr[1] = mac_bytes[1];
|
||||||
dev->host_nic_phy_hw_addr[2] = mac_bytes[2];
|
dev->host_nic_phy_hw_addr[2] = mac_bytes[2];
|
||||||
dev->host_nic_phy_hw_addr[3] = mac_bytes[3];
|
dev->host_nic_phy_hw_addr[3] = mac_bytes[3];
|
||||||
dev->host_nic_phy_hw_addr[4] = mac_bytes[4];
|
dev->host_nic_phy_hw_addr[4] = mac_bytes[4];
|
||||||
dev->host_nic_phy_hw_addr[5] = mac_bytes[5];
|
dev->host_nic_phy_hw_addr[5] = mac_bytes[5];
|
||||||
dev->have_host_nic_phy_addr = 1;
|
dev->have_host_nic_phy_addr = 1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
break;
|
p1 = p2;
|
||||||
}
|
}
|
||||||
p1 = p2;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
else
|
pclose(f);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
pclose(f);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue