src/network/core/host.cpp
author rubidium
Mon, 04 Aug 2008 12:56:38 +0000
changeset 9844 705fc23bedba
child 9846 8fd9f778830f
permissions -rw-r--r--
(svn r13988) -Codechange: move the to IP resolving functions to a separate file.
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"
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    10
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
 * 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
    13
 * 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
    14
 * @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
    15
 * @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
    16
 */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    17
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
    18
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    19
#if defined(PSP)
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    20
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
    21
{
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    22
	return 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    23
}
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
#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
    26
/* 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
    27
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
    28
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    29
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
    30
{
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    31
	char *new_pos = strstr(*pos, header);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    32
	if (new_pos == 0) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    33
		return B_ERROR;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    34
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    35
	*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
    36
	return B_OK;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    37
}
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
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
    40
{
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    41
	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
    42
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    43
	if (sock < 0) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    44
		DEBUG(net, 0, "[core] error creating socket");
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    45
		return;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    46
	}
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
	char *output_pointer = NULL;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    49
	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
    50
	if (output_length < 0) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    51
		DEBUG(net, 0, "[core] error running _netstat");
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    52
		return;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    53
	}
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
	int index;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    56
	char **output = &output_pointer;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    57
	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
    58
		while (index != limit) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    59
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    60
			uint32 n, fields, read;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    61
			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
    62
			struct in_addr inaddr;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    63
			uint32 ip;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    64
			uint32 netmask;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    65
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    66
			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
    67
												&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
    68
			read += 1;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    69
			if (fields != 9) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    70
				break;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    71
			}
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
			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
    74
			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
    75
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    76
			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
    77
				inaddr.s_addr = htonl(ip | ~netmask);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    78
				broadcast[index] = inaddr.s_addr;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    79
				index++;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    80
			}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    81
			if (read < 0) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    82
				break;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    83
			}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    84
			*output += read;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    85
		}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    86
		closesocket(sock);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    87
	}
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
	return index;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    90
}
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
#elif defined(HAVE_GETIFADDRS)
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    93
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
    94
{
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    95
	struct ifaddrs *ifap, *ifa;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    96
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    97
	if (getifaddrs(&ifap) != 0) return 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    98
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
    99
	int index = 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   100
	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
   101
		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
   102
		if (ifa->ifa_broadaddr == NULL) continue;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   103
		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
   104
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   105
		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
   106
		index++;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   107
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   108
	freeifaddrs(ifap);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   109
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   110
	return index;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   111
}
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
#elif defined(WIN32)
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   114
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
   115
{
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   116
	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
   117
	if (sock == INVALID_SOCKET) return 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   118
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   119
	DWORD len = 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   120
	INTERFACE_INFO ifo[MAX_INTERFACES];
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   121
	memset(&ifo[0], 0, sizeof(ifo));
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   122
	if ((WSAIoctl(sock, SIO_GET_INTERFACE_LIST, NULL, 0, &ifo[0], sizeof(ifo), &len, NULL, NULL)) != 0) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   123
		closesocket(sock);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   124
		return;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   125
	}
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
	int index = 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   128
	for (int j = 0; j < len / sizeof(*ifo) && index != limit; j++) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   129
		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
   130
		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
   131
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   132
		/* 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
   133
		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
   134
	}
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
	closesocket(sock);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   137
	return index;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   138
}
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
#else /* not HAVE_GETIFADDRS */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   141
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
   142
{
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   143
	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
   144
	if (sock == INVALID_SOCKET) return 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   145
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   146
	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
   147
	struct ifconf ifconf;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   148
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   149
	ifconf.ifc_len = sizeof(buf);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   150
	ifconf.ifc_buf = buf;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   151
	if (ioctl(sock, SIOCGIFCONF, &ifconf) == -1) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   152
		closesocket(sock);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   153
		return 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   154
	}
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
	const char *buf_end = buf + ifconf.ifc_len;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   157
	int index;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   158
	for (const char *p = buf; p < buf_end && index != 0;) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   159
		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
   160
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   161
		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
   162
			struct ifreq r;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   163
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   164
			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
   165
			if (ioctl(sock, SIOCGIFFLAGS, &r) != -1 &&
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   166
					r.ifr_flags & IFF_BROADCAST &&
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   167
					ioctl(sock, SIOCGIFBRDADDR, &r) != -1) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   168
				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
   169
			}
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
		p += sizeof(struct ifreq);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   173
#if defined(AF_LINK) && !defined(SUNOS)
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   174
		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
   175
#endif
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   176
	}
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
	closesocket(sock);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   179
}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   180
#endif /* all NetworkFindBroadcastIPsInternals */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   181
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
 * Find the IPs to broadcast.
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   184
 * @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
   185
 * @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
   186
 */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   187
void NetworkFindBroadcastIPs(uint32 *broadcast, int limit)
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   188
{
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   189
	int count = NetworkFindBroadcastIPsInternal(broadcast, limit);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   190
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   191
	/* Make sure the list is terminated. */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   192
	broadcast[count] = 0;
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
	/* 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
   195
	DEBUG(net, 3, "Detected broadcast addresses:");
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   196
	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
   197
		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
   198
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   199
}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   200
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
 * Resolve a hostname to an ip.
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   204
 * @param hsotname the hostname to resolve
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   205
 * @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
   206
 */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   207
uint32 NetworkResolveHost(const char *hostname)
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   208
{
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   209
	/* Is this an IP address? */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   210
	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
   211
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   212
	if (ip != INADDR_NONE) return ip;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   213
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   214
	/* No, try to resolve the name */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   215
	struct in_addr addr;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   216
#if !defined(PSP)
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   217
	struct hostent *he = gethostbyname(hostname);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   218
	if (he == NULL) {
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   219
		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
   220
		return 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   221
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   222
	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
   223
#else
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   224
	int rid = -1;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   225
	char buf[1024];
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   226
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   227
	/* Create a resolver */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   228
	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
   229
		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
   230
		return 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   231
	}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   232
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   233
	/* Try to resolve the name */
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   234
	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
   235
		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
   236
		sceNetResolverDelete(rid);
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   237
		return 0;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   238
	}
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
#endif /* PSP */
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
	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
   243
	ip = addr.s_addr;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   244
	return ip;
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   245
}
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   246
705fc23bedba (svn r13988) -Codechange: move the to IP resolving functions to a separate file.
rubidium
parents:
diff changeset
   247
#endif /* ENABLE_NETWORK */