16#elif (defined(__unix__) || defined(__APPLE__))
18#include <sys/socket.h>
32#if (defined(PLATFORM_WIN32) || defined(PLATFORM_UNIX))
37bool o2n_internet_enabled =
true;
40bool o2n_network_enabled =
true;
43bool o2n_network_found =
false;
45void o2n_get_internal_ip(
char *internal_ip)
50 assert(!o2n_network_found);
51 assert(o2n_network_enabled);
56 ULONG outbuflen = 15000;
58 ULONG flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST |
59 GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME;
61 PIP_ADAPTER_ADDRESSES addresses;
63 addresses = (PIP_ADAPTER_ADDRESSES) O2_MALLOC(outbuflen);
64 if (addresses == NULL) {
67 rslt = GetAdaptersAddresses(AF_INET, flags, NULL,
68 addresses, &outbuflen);
69 if (rslt == ERROR_BUFFER_OVERFLOW) {
73 }
while (rslt == ERROR_BUFFER_OVERFLOW && iterations++ < 3);
74 if (rslt != NO_ERROR) {
77 PIP_ADAPTER_ADDRESSES cur = addresses;
79 if (cur->IfType != IF_TYPE_SOFTWARE_LOOPBACK &&
80 cur->OperStatus == IfOperStatusUp) {
81 PIP_ADAPTER_UNICAST_ADDRESS addr;
82 for (addr = cur->FirstUnicastAddress; addr != NULL;
84 SOCKADDR_IN *saddr = (SOCKADDR_IN *)addr->Address.lpSockaddr;
85 snprintf(internal_ip, O2N_IP_LEN,
"%08x",
86 ntohl(saddr->sin_addr.S_un.S_addr));
87 if (!(streql(internal_ip,
"7f000001"))) {
88 o2n_network_found =
true;
97 struct ifaddrs *ifap, *ifa;
98 if (getifaddrs(&ifap)) {
99 perror(
"getting IP address");
102 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
103 if (ifa->ifa_addr->sa_family == AF_INET) {
104 struct sockaddr_in *sa = (
struct sockaddr_in *) ifa->ifa_addr;
105 snprintf(internal_ip, O2N_IP_LEN,
"%08x",
106 ntohl(sa->sin_addr.s_addr));
107 if (!streql(internal_ip,
"7f000001")) {
108 o2n_network_found =
true;
116 if (!internal_ip[0]) {
117 strcpy(internal_ip,
"7f000001");
123static int hex_to_nibble(
char hex)
125 if (isdigit(hex))
return hex -
'0';
126 else if (hex >=
'A' && hex <=
'F')
return hex -
'A' + 10;
127 else if (hex >=
'a' && hex <=
'f')
return hex -
'a' + 10;
129 printf(
"ERROR: bad hex character passed to hex_to_nibble()\n");
136int o2_hex_to_byte(
const char *hex)
138 return (hex_to_nibble(hex[0]) << 4) + hex_to_nibble(hex[1]);
146 while ((h = *hex++)) {
147 i = (i << 4) + hex_to_nibble(h);
158 int i1 = o2_hex_to_byte(hex);
159 int i2 = o2_hex_to_byte(hex + 2);
160 int i3 = o2_hex_to_byte(hex + 4);
161 int i4 = o2_hex_to_byte(hex + 6);
162 snprintf(dot, 16,
"%d.%d.%d.%d", i1, i2, i3, i4);
void o2_hex_to_dot(const char *hex, char *dot)
Convert from hex format to dot format IP address.
Definition: hostipimpl.h:156
unsigned int o2_hex_to_int(const char *hex)
Convert hex string to integer.
Definition: hostipimpl.h:142