Wrapper of NetworkSocketAPI for BSD sockets on POSIX systems

Dependents:   BSDInterfaceTests HelloBSDInterface

Files at this revision

API Documentation at this revision

Comitter:
Christopher Haster
Date:
Sun Feb 28 10:32:22 2016 -0600
Parent:
1:29c61c1420c8
Child:
3:eabc4ea66a64
Commit message:
Fixed link level naming conflict with sockets

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	Sat Feb 27 21:01:54 2016 -0600
+++ b/BSDInterface.cpp	Sun Feb 28 10:32:22 2016 -0600
@@ -17,6 +17,7 @@
 #include "BSDInterface.h"
 
 #include <sys/ioctl.h>
+#include <netdb.h>
 #include <net/if.h> 
 #include <unistd.h>
 #include <netinet/in.h>
@@ -39,7 +40,7 @@
     struct ifconf ifc;
     char buffer[1024];
 
-    int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
+    int sock = ::socket(AF_INET, SOCK_DGRAM, 0);
     if (sock < 0) {
         return 0;
     }
@@ -58,12 +59,12 @@
             && ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
 
             sprintf(mac_address, "%02x:%02x:%02x:%02x:%02x:%02x",
-                    ifr.ifr_hwaddr.sa_data[0],
-                    ifr.ifr_hwaddr.sa_data[1],
-                    ifr.ifr_hwaddr.sa_data[2],
-                    ifr.ifr_hwaddr.sa_data[3],
-                    ifr.ifr_hwaddr.sa_data[4],
-                    ifr.ifr_hwaddr.sa_data[5]);
+                    (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;
         }
     }
@@ -72,6 +73,18 @@
 }
 
 
+int32_t BSDInterface::getHostByName(const char *name, char *ip)
+{
+    struct hostent *host = gethostbyname(name);
+    if (!host || host->h_addrtype != AF_INET) {
+        return NS_ERROR_DNS_FAILURE;
+    }
+
+    strcpy(ip, inet_ntoa(*(struct in_addr *)host->h_addr));
+    return 0;
+}
+
+
 /** BSDSocket class
  *  Implementation of the SocketInterface for BSD
  */
@@ -94,8 +107,7 @@
 SocketInterface *BSDInterface::createSocket(socket_protocol_t proto)
 {
     int type = (proto == SOCK_UDP) ? SOCK_DGRAM : SOCK_STREAM;
-    int prot = (proto == SOCK_UDP) ? IPPROTO_UDP : IPPROTO_TCP;
-    int fd = socket(PF_INET, type, prot);
+    int fd = ::socket(AF_INET, type, 0);
     if (fd < 0) {
         return 0;
     }
@@ -105,7 +117,7 @@
 
 void BSDInterface::destroySocket(SocketInterface *socket)
 {
-    close(((BSDSocket*)socket)->fd);
+    ::close(((BSDSocket*)socket)->fd);
     delete socket;
 }
 
@@ -119,7 +131,7 @@
     host.sin_port = htons(port);
     inet_pton(AF_INET, ip, &host.sin_addr);
 
-    if (connect(fd, (struct sockaddr *)&host, sizeof host) < 0) {
+    if (::connect(fd, (struct sockaddr *)&host, sizeof host) < 0) {
         return NS_ERROR_NO_CONNECTION;
     }
 
--- a/BSDInterface.h	Sat Feb 27 21:01:54 2016 -0600
+++ b/BSDInterface.h	Sun Feb 28 10:32:22 2016 -0600
@@ -29,6 +29,8 @@
     // Implementation of NetworkInterface
     virtual const char *getMACAddress();
 
+    virtual int32_t getHostByName(const char *host, char *ip);
+
     virtual SocketInterface *createSocket(socket_protocol_t proto);
     virtual void destroySocket(SocketInterface *socket);
 };