Wrapper of NetworkSocketAPI for BSD sockets on POSIX systems
Dependents: BSDInterfaceTests HelloBSDInterface
Revision 3:eabc4ea66a64, committed 2016-02-28
- Comitter:
- Christopher Haster
- Date:
- Sun Feb 28 10:54:31 2016 -0600
- Parent:
- 2:0978e139e1c5
- Child:
- 4:28479a7d15ad
- Commit message:
- Added getIPAddress to BSDInterface
Changed in this revision
BSDInterface.cpp | Show annotated file Show diff for this revision Revisions of this file |
BSDInterface.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/BSDInterface.cpp Sun Feb 28 10:32:22 2016 -0600 +++ b/BSDInterface.cpp Sun Feb 28 10:54:31 2016 -0600 @@ -29,14 +29,7 @@ // BSDInterface implementation -const char *BSDInterface::getMACAddress() -{ - static char mac_address[NS_MAC_SIZE] = "\0"; - if (mac_address[0]) { - return mac_address; - } - - struct ifreq ifr; +static bool ifctl(struct ifreq *ifr, unsigned req) { struct ifconf ifc; char buffer[1024]; @@ -48,28 +41,62 @@ ifc.ifc_buf = buffer; ifc.ifc_len = sizeof buffer; if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { - return 0; + ::close(sock); + return false; } for (int i = 0; i < ifc.ifc_len; i++) { - strcpy(ifr.ifr_name, ifc.ifc_req[i].ifr_name); - - if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0 - && !(ifr.ifr_flags & IFF_LOOPBACK) - && ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) { + strcpy(ifr->ifr_name, ifc.ifc_req[i].ifr_name); - sprintf(mac_address, "%02x:%02x:%02x:%02x:%02x:%02x", - (unsigned char)ifr.ifr_hwaddr.sa_data[0], - (unsigned char)ifr.ifr_hwaddr.sa_data[1], - (unsigned char)ifr.ifr_hwaddr.sa_data[2], - (unsigned char)ifr.ifr_hwaddr.sa_data[3], - (unsigned char)ifr.ifr_hwaddr.sa_data[4], - (unsigned char)ifr.ifr_hwaddr.sa_data[5]); - return mac_address; + if (ioctl(sock, SIOCGIFFLAGS, ifr) == 0 + && !(ifr->ifr_flags & IFF_LOOPBACK) + && ioctl(sock, req, ifr) == 0) { + ::close(sock); + return true; } } - return 0; + ::close(sock); + return false; +} + +const char *BSDInterface::getIPAddress() +{ + static char ip_address[NS_IP_SIZE] = "\0"; + if (ip_address[0]) { + return ip_address; + } + + struct ifreq ifr; + if (!ifctl(&ifr, SIOCGIFADDR)) { + return 0; + } + + struct in_addr addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr; + strcpy(ip_address, inet_ntoa(addr)); + return ip_address; +} + +const char *BSDInterface::getMACAddress() +{ + static char mac_address[NS_MAC_SIZE] = "\0"; + if (mac_address[0]) { + return mac_address; + } + + struct ifreq ifr; + if (!ifctl(&ifr, SIOCGIFHWADDR)) { + return 0; + } + + sprintf(mac_address, "%02x:%02x:%02x:%02x:%02x:%02x", + (unsigned char)ifr.ifr_hwaddr.sa_data[0], + (unsigned char)ifr.ifr_hwaddr.sa_data[1], + (unsigned char)ifr.ifr_hwaddr.sa_data[2], + (unsigned char)ifr.ifr_hwaddr.sa_data[3], + (unsigned char)ifr.ifr_hwaddr.sa_data[4], + (unsigned char)ifr.ifr_hwaddr.sa_data[5]); + return mac_address; }
--- a/BSDInterface.h Sun Feb 28 10:32:22 2016 -0600 +++ b/BSDInterface.h Sun Feb 28 10:54:31 2016 -0600 @@ -27,6 +27,7 @@ { public: // Implementation of NetworkInterface + virtual const char *getIPAddress(); virtual const char *getMACAddress(); virtual int32_t getHostByName(const char *host, char *ip);