src/network/core/host.cpp
author glx
Fri, 08 Aug 2008 15:49:16 +0000
changeset 9878 8142e032b4e0
parent 9853 1fa928bd5771
child 10201 4c181fbe8f2a
permissions -rw-r--r--
(svn r14026) -Cleanup (r13995, r14004): trailing whitespaces
9844
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
     1
/* $Id$ */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
     2
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
     3
/** @file host.cpp Functions related to getting host specific data (IPs). */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
     4
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
     5
#ifdef ENABLE_NETWORK
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
     6
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
     7
#include "../../stdafx.h"
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
     8
#include "../../debug.h"
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
     9
#include "os_abstraction.h"
9846
8fd9f778830f (svn r13990) -Fix (r13988): win32 compilation failed
glx
parents: 9844
diff changeset
    10
#include "../../core/alloc_func.hpp"
9844
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    11
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    12
/**
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    13
 * Internal implementation for finding the broadcast IPs.
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    14
 * This function is implemented multiple times for multiple targets.
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    15
 * @param broadcast the list of broadcasts to write into.
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    16
 * @param limit     the maximum number of items to add.
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    17
 */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    18
static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    19
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    20
#if defined(PSP)
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    21
static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // PSP implementation
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    22
{
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    23
	return 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    24
}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    25
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    26
#elif defined(BEOS_NET_SERVER) /* doesn't have neither getifaddrs or net/if.h */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    27
/* Based on Andrew Bachmann's netstat+.c. Big thanks to him! */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    28
int _netstat(int fd, char **output, int verbose);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    29
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    30
int seek_past_header(char **pos, const char *header)
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    31
{
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    32
	char *new_pos = strstr(*pos, header);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    33
	if (new_pos == 0) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    34
		return B_ERROR;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    35
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    36
	*pos += strlen(header) + new_pos - *pos + 1;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    37
	return B_OK;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    38
}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    39
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    40
static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // BEOS implementation
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    41
{
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    42
	int sock = socket(AF_INET, SOCK_DGRAM, 0);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    43
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    44
	if (sock < 0) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    45
		DEBUG(net, 0, "[core] error creating socket");
9851
20680f5b9c3a (svn r13995) -Fix (r13988): some warnings of the compile farm.
rubidium
parents: 9846
diff changeset
    46
		return 0;
9844
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    47
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    48
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    49
	char *output_pointer = NULL;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    50
	int output_length = _netstat(sock, &output_pointer, 1);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    51
	if (output_length < 0) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    52
		DEBUG(net, 0, "[core] error running _netstat");
9851
20680f5b9c3a (svn r13995) -Fix (r13988): some warnings of the compile farm.
rubidium
parents: 9846
diff changeset
    53
		return 0;
9844
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    54
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    55
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    56
	int index;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    57
	char **output = &output_pointer;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    58
	if (seek_past_header(output, "IP Interfaces:") == B_OK) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    59
		while (index != limit) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    60
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    61
			uint32 n, fields, read;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    62
			uint8 i1, i2, i3, i4, j1, j2, j3, j4;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    63
			struct in_addr inaddr;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    64
			uint32 ip;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    65
			uint32 netmask;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    66
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    67
			fields = sscanf(*output, "%u: %hhu.%hhu.%hhu.%hhu, netmask %hhu.%hhu.%hhu.%hhu%n",
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    68
												&n, &i1, &i2, &i3, &i4, &j1, &j2, &j3, &j4, &read);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    69
			read += 1;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    70
			if (fields != 9) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    71
				break;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    72
			}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    73
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    74
			ip      = (uint32)i1 << 24 | (uint32)i2 << 16 | (uint32)i3 << 8 | (uint32)i4;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    75
			netmask = (uint32)j1 << 24 | (uint32)j2 << 16 | (uint32)j3 << 8 | (uint32)j4;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    76
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    77
			if (ip != INADDR_LOOPBACK && ip != INADDR_ANY) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    78
				inaddr.s_addr = htonl(ip | ~netmask);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    79
				broadcast[index] = inaddr.s_addr;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    80
				index++;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    81
			}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    82
			if (read < 0) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    83
				break;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    84
			}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    85
			*output += read;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    86
		}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    87
		closesocket(sock);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    88
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    89
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    90
	return index;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    91
}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    92
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    93
#elif defined(HAVE_GETIFADDRS)
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    94
static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // GETIFADDRS implementation
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    95
{
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    96
	struct ifaddrs *ifap, *ifa;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    97
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    98
	if (getifaddrs(&ifap) != 0) return 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    99
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   100
	int index = 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   101
	for (ifa = ifap; ifa != NULL && index != limit; ifa = ifa->ifa_next) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   102
		if (!(ifa->ifa_flags & IFF_BROADCAST)) continue;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   103
		if (ifa->ifa_broadaddr == NULL) continue;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   104
		if (ifa->ifa_broadaddr->sa_family != AF_INET) continue;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   105
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   106
		broadcast[index] = ((struct sockaddr_in*)ifa->ifa_broadaddr)->sin_addr.s_addr;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   107
		index++;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   108
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   109
	freeifaddrs(ifap);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   110
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   111
	return index;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   112
}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   113
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   114
#elif defined(WIN32)
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   115
static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // Win32 implementation
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   116
{
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   117
	SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   118
	if (sock == INVALID_SOCKET) return 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   119
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   120
	DWORD len = 0;
9846
8fd9f778830f (svn r13990) -Fix (r13988): win32 compilation failed
glx
parents: 9844
diff changeset
   121
	INTERFACE_INFO *ifo = AllocaM(INTERFACE_INFO, limit);
8fd9f778830f (svn r13990) -Fix (r13988): win32 compilation failed
glx
parents: 9844
diff changeset
   122
	memset(ifo, 0, limit * sizeof(*ifo));
8fd9f778830f (svn r13990) -Fix (r13988): win32 compilation failed
glx
parents: 9844
diff changeset
   123
	if ((WSAIoctl(sock, SIO_GET_INTERFACE_LIST, NULL, 0, &ifo[0], limit * sizeof(*ifo), &len, NULL, NULL)) != 0) {
9844
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   124
		closesocket(sock);
9846
8fd9f778830f (svn r13990) -Fix (r13988): win32 compilation failed
glx
parents: 9844
diff changeset
   125
		return 0;
9844
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   126
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   127
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   128
	int index = 0;
9846
8fd9f778830f (svn r13990) -Fix (r13988): win32 compilation failed
glx
parents: 9844
diff changeset
   129
	for (uint j = 0; j < len / sizeof(*ifo) && index != limit; j++) {
9844
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   130
		if (ifo[j].iiFlags & IFF_LOOPBACK) continue;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   131
		if (!(ifo[j].iiFlags & IFF_BROADCAST)) continue;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   132
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   133
		/* iiBroadcast is unusable, because it always seems to be set to 255.255.255.255. */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   134
		broadcast[index++] = ifo[j].iiAddress.AddressIn.sin_addr.s_addr | ~ifo[j].iiNetmask.AddressIn.sin_addr.s_addr;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   135
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   136
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   137
	closesocket(sock);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   138
	return index;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   139
}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   140
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   141
#else /* not HAVE_GETIFADDRS */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   142
static int NetworkFindBroadcastIPsInternal(uint32 *broadcast, int limit) // !GETIFADDRS implementation
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   143
{
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   144
	SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   145
	if (sock == INVALID_SOCKET) return 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   146
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   147
	char buf[4 * 1024]; // Arbitrary buffer size
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   148
	struct ifconf ifconf;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   149
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   150
	ifconf.ifc_len = sizeof(buf);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   151
	ifconf.ifc_buf = buf;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   152
	if (ioctl(sock, SIOCGIFCONF, &ifconf) == -1) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   153
		closesocket(sock);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   154
		return 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   155
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   156
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   157
	const char *buf_end = buf + ifconf.ifc_len;
9851
20680f5b9c3a (svn r13995) -Fix (r13988): some warnings of the compile farm.
rubidium
parents: 9846
diff changeset
   158
	int index = 0;
9853
1fa928bd5771 (svn r13997) -Fix (r13988): in some cases no broadcast addresses would be found.
rubidium
parents: 9851
diff changeset
   159
	for (const char *p = buf; p < buf_end && index != limit;) {
9844
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   160
		const struct ifreq* req = (const struct ifreq*)p;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   161
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   162
		if (req->ifr_addr.sa_family == AF_INET) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   163
			struct ifreq r;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   164
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   165
			strncpy(r.ifr_name, req->ifr_name, lengthof(r.ifr_name));
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   166
			if (ioctl(sock, SIOCGIFFLAGS, &r) != -1 &&
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   167
					r.ifr_flags & IFF_BROADCAST &&
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   168
					ioctl(sock, SIOCGIFBRDADDR, &r) != -1) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   169
				broadcast[index++] = ((struct sockaddr_in*)&r.ifr_broadaddr)->sin_addr.s_addr;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   170
			}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   171
		}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   172
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   173
		p += sizeof(struct ifreq);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   174
#if defined(AF_LINK) && !defined(SUNOS)
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   175
		p += req->ifr_addr.sa_len - sizeof(struct sockaddr);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   176
#endif
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   177
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   178
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   179
	closesocket(sock);
9878
8142e032b4e0 (svn r14026) -Cleanup (r13995, r14004): trailing whitespaces
glx
parents: 9853
diff changeset
   180
9851
20680f5b9c3a (svn r13995) -Fix (r13988): some warnings of the compile farm.
rubidium
parents: 9846
diff changeset
   181
	return index;
9844
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   182
}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   183
#endif /* all NetworkFindBroadcastIPsInternals */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   184
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   185
/**
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   186
 * Find the IPs to broadcast.
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   187
 * @param broadcast the list of broadcasts to write into.
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   188
 * @param limit     the maximum number of items to add.
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   189
 */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   190
void NetworkFindBroadcastIPs(uint32 *broadcast, int limit)
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   191
{
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   192
	int count = NetworkFindBroadcastIPsInternal(broadcast, limit);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   193
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   194
	/* Make sure the list is terminated. */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   195
	broadcast[count] = 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   196
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   197
	/* Now display to the debug all the detected ips */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   198
	DEBUG(net, 3, "Detected broadcast addresses:");
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   199
	for (int i = 0; broadcast[i] != 0; i++) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   200
		DEBUG(net, 3, "%d) %s", i, inet_ntoa(*(struct in_addr *)&broadcast[i])); //inet_ntoa(inaddr));
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   201
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   202
}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   203
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   204
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   205
/**
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   206
 * Resolve a hostname to an ip.
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   207
 * @param hsotname the hostname to resolve
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   208
 * @return the IP belonging to that hostname, or 0 on failure.
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   209
 */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   210
uint32 NetworkResolveHost(const char *hostname)
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   211
{
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   212
	/* Is this an IP address? */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   213
	in_addr_t ip = inet_addr(hostname);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   214
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   215
	if (ip != INADDR_NONE) return ip;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   216
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   217
	/* No, try to resolve the name */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   218
	struct in_addr addr;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   219
#if !defined(PSP)
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   220
	struct hostent *he = gethostbyname(hostname);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   221
	if (he == NULL) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   222
		DEBUG(net, 0, "[NET] Cannot resolve %s", hostname);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   223
		return 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   224
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   225
	addr = *(struct in_addr *)he->h_addr_list[0];
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   226
#else
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   227
	int rid = -1;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   228
	char buf[1024];
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   229
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   230
	/* Create a resolver */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   231
	if (sceNetResolverCreate(&rid, buf, sizeof(buf)) < 0) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   232
		DEBUG(net, 0, "[NET] Error connecting resolver");
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   233
		return 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   234
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   235
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   236
	/* Try to resolve the name */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   237
	if (sceNetResolverStartNtoA(rid, hostname, &addr, 2, 3) < 0) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   238
		DEBUG(net, 0, "[NET] Cannot resolve %s", hostname);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   239
		sceNetResolverDelete(rid);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   240
		return 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   241
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   242
	sceNetResolverDelete(rid);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   243
#endif /* PSP */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   244
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   245
	DEBUG(net, 1, "[NET] Resolved %s to %s", hostname, inet_ntoa(addr));
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   246
	ip = addr.s_addr;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   247
	return ip;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   248
}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   249
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   250
#endif /* ENABLE_NETWORK */