Add an API for retrieving the MAC address of an interface

* src/util/bridge.c, src/util/bridge.h: Add virNetDevGetMAC
This commit is contained in:
Daniel P. Berrange 2011-11-02 13:00:09 +00:00
parent dfb454cebc
commit ccc243470d
2 changed files with 38 additions and 0 deletions

View File

@ -357,6 +357,41 @@ cleanup:
return ret;
}
/**
* virNetDevGetMAC:
* @ifname: interface name to set MTU for
* @macaddr: MAC address (VIR_MAC_BUFLEN in size)
*
* This function gets the @macaddr for a given interface @ifname.
*
* Returns 0 in case of success or -1 on failure
*/
int virNetDevGetMAC(const char *ifname,
unsigned char *macaddr)
{
int fd = -1;
int ret = -1;
struct ifreq ifr;
if ((fd = virNetDevSetupControl(ifname, &ifr)) < 0)
return -1;
if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
virReportSystemError(errno,
_("Cannot get interface MAC on '%s'"),
ifname);
goto cleanup;
}
memcpy(macaddr, ifr.ifr_hwaddr.sa_data, VIR_MAC_BUFLEN);
ret = 0;
cleanup:
VIR_FORCE_CLOSE(fd);
return ret;
}
/**
* virNetDevGetMTU:
* @ifname: interface name get MTU for

View File

@ -112,6 +112,9 @@ int virNetDevTapCreate(char **ifname,
int virNetDevSetMAC(const char *ifname,
const unsigned char *macaddr)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
int virNetDevGetMAC(const char *ifname,
unsigned char *macaddr)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
int virNetDevSetMTU(const char *ifname,
int mtu)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;