[PATCH 17/30] Added getHostName and getIpAddress.


Michael Schonberg <mschonberg@...>
 

Added to linux/port.h and linux/network.c for windows support. The API
calls to determine the hostname and ip address are os specific.

Signed-off-by: Mike Schonberg <mschonberg@nyx.com>
---
common/c_cpp/src/c/linux/network.c | 46 ++++++++++++++++++++++++++++++++++++
common/c_cpp/src/c/linux/port.h | 4 +++
2 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/common/c_cpp/src/c/linux/network.c b/common/c_cpp/src/c/linux/network.c
index f5923d9..75e5658 100644
--- a/common/c_cpp/src/c/linux/network.c
+++ b/common/c_cpp/src/c/linux/network.c
@@ -19,6 +19,9 @@
* 02110-1301 USA
*/

+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
#include "wombat/wCommon.h"


@@ -147,3 +150,46 @@ struct in_addr resolve_ip (const char * arg)
}
return (resolved);
}
+
+static const char gIPAddress[16];
+static const char* gHostName = NULL;
+
+static void lookupIPAddress (void)
+{
+ struct hostent *host = NULL;
+ char *addrStr = "not determined";
+
+ struct utsname uts;
+ memset( gIPAddress, 0, 16 );
+ uname (&uts);
+ gHostName = strdup (uts.nodename);
+
+ host = gethostbyname( gHostName );
+
+ if( gHostName == NULL ||
+ host == NULL ||
+ host->h_addr_list[0] == NULL )
+ {
+ strncpy( (char *)gIPAddress, "not determined", sizeof( gIPAddress ) );
+ }
+ else
+ {
+ addrStr = inet_ntoa( *((struct in_addr *)( host->h_addr_list[0] )));
+ }
+
+ strncpy ((char*)gIPAddress, addrStr, sizeof (gIPAddress));
+}
+
+const char* getIpAddress(void)
+{
+ if (NULL == gHostName)
+ lookupIPAddress();
+ return gIPAddress;
+}
+
+const char* getHostName(void)
+{
+ if (NULL == gHostName)
+ lookupIPAddress();
+ return gHostName;
+}
diff --git a/common/c_cpp/src/c/linux/port.h b/common/c_cpp/src/c/linux/port.h
index e289006..86d6d2b 100644
--- a/common/c_cpp/src/c/linux/port.h
+++ b/common/c_cpp/src/c/linux/port.h
@@ -163,6 +163,10 @@ int wsem_timedwait (wsem_t* sem, unsigned int ts);

#define wGetCurrentThreadId pthread_self

+/* net work utility functions */
+const char* getIpAddress (void);
+const char* getHostName (void);
+
#if defined (__cplusplus)
} /* extern "c" */
#endif
--
1.7.7.6