bff4389137
(SendARP): Define. * cygwin.din: Export if_freenameindex, if_indextoname, if_nameindex and if_nametoindex. * fhandler_procnet.cc: Drop including wchar.h. Drop definitions of GAA_FLAG_INCLUDE_ALL_INTERFACES, IP_ADAPTER_UNICAST_ADDRESS_VISTA. (fhandler_procnet::exists): Check for has_gaa_prefixes. Call get_adapters_addresses here. (fhandler_procnet::readdir): Ditto. (prefix): Move to net.cc. (fhandler_procnet::fill_filebuf): Call get_adapters_addresses here. Simplify allocation. Use AdapterName rather than FriendlyName as interface name. Use IfIndex if available, Ipv6IfIndex otherwise. (in6_are_prefix_equal): Move to net.cc. * fhandler_socket.cc: Define old SIOCGxxx values. (CONV_OLD_TO_NEW_SIO): Convert old SIOCGxxx value to new one. (struct __old_ifreq): Define old struct ifreq. (fhandler_socket::ioctl): Handle old SIOCGxxx values. Handle new SIOCGIFFRNDLYNAM command. Simplify copying ifreq data to user space. Call get_ifconf with additional SOCKET parameter. * net.cc (IP_ADAPTER_UNICAST_ADDRESS_LH): Define. (IP_ADAPTER_ADDRESSES_LH): Define. (SIO_GET_INTERFACE_LIST): Define. (sockaddr_in6_old): Define. (sockaddr_gen): Define. (INTERFACE_INFO): Define. (IN_LOOPBACK): Define. (in_are_prefix_equal): New static function. (ip_addr_prefix): New function, replaces prefix function, add AF_INET handling. (GAA_FLAG_INCLUDE_ALL_INTERFACES): Define. (get_adapters_addresses): New function. (WS_IFF_xxx): Define Winsock interface flag values. (convert_ifr_flags): New function to convert Winsock interface flag values to Cygwin interface flag values. (get_xp_ifconf): New get_ifconf implementation for XP SP1 and above. (get_2k_ifconf): Fix interface index. Fix formatting. (get_nt_ifconf): Fix formatting. (get_95_ifconf): Ditto. (get_ifconf): Take additional SOCKET parameter. Call get_xp_ifconf on XP SP1 and above. (if_nametoindex): New function. (if_indextoname): New function. (if_nameindex): New function. (if_freenameindex): New function. (in6_are_prefix_equal): Moved here from fhandler_procnet.cc. * wincap.cc (wincap_xp): Define has_gaa_prefixes as true by default. (wincapc::init): Assume has_osversioninfoex by default. Call GetVersionEx with OSVERSIONINFOEX first. Call with OSVERSIONINFO only if that fails. Simplify NT4 case and try to avoid strcmp. Check XP Service Pack using version.wServicePackMajor to avoid strcmp. * include/asm/socket.h (SIOCGIFFRNDLYNAM): Define. * include/cygwin/if.h: Fix formatting. (IFF_POINTTOPOINT): Define. (IFF_NOARP): Define. (IFF_LOWER_UP): Define. (IFF_DORMANT): Define. (struct if_nameindex): Define. (IFRF_FRIENDLYNAMESIZ): Define. (struct ifreq_frndlyname): Define. (IFNAMSIZ): Redefine as 44. (IF_NAMESIZE): Define. (struct ifreq): Redefine ifru_flags as int. Define ifru_data. Pad size to sizeof sockaddr_in6 for further extensions. (ifr_data): Define. (ifr_frndlyname): Define. (if_nametoindex): Declare. (if_indextoname): Declare. (if_nameindex): Declare. (if_freenameindex): Declare. * include/cygwin/version.h: Bump API minor number. (CYGWIN_VERSION_CHECK_FOR_OLD_IFREQ): Define check for old vs. new ifreq structure.
287 lines
5.7 KiB
C++
287 lines
5.7 KiB
C++
/* fhandler_procnet.cc: fhandler for /proc/net virtual filesystem
|
|
|
|
Copyright 2007 Red Hat, Inc.
|
|
|
|
This file is part of Cygwin.
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
details. */
|
|
|
|
#define __INSIDE_CYGWIN_NET__
|
|
|
|
#include "winsup.h"
|
|
#include "cygerrno.h"
|
|
#include "security.h"
|
|
#include "path.h"
|
|
#include "fhandler.h"
|
|
#include "dtable.h"
|
|
#include "cygheap.h"
|
|
|
|
#include <netdb.h>
|
|
#define USE_SYS_TYPES_FD_SET
|
|
#include <winsock2.h>
|
|
#include <iphlpapi.h>
|
|
#include <asm/byteorder.h>
|
|
#include <cygwin/in6.h>
|
|
|
|
#define _COMPILING_NEWLIB
|
|
#include <dirent.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
extern "C" int ip_addr_prefix (PIP_ADAPTER_UNICAST_ADDRESS pua,
|
|
PIP_ADAPTER_PREFIX pap);
|
|
bool get_adapters_addresses (PIP_ADAPTER_ADDRESSES *pa0, ULONG family);
|
|
|
|
static const int PROCNET_IFINET6 = 2;
|
|
|
|
static const char * const process_listing[] =
|
|
{
|
|
".",
|
|
"..",
|
|
"if_inet6",
|
|
NULL
|
|
};
|
|
|
|
static const int PROCESS_LINK_COUNT =
|
|
(sizeof (process_listing) / sizeof (const char *)) - 1;
|
|
|
|
static _off64_t format_procnet_ifinet6 (char *&filebuf);
|
|
|
|
/* Returns 0 if path doesn't exist, >0 if path is a directory,
|
|
* -1 if path is a file, -2 if path is a symlink, -3 if path is a pipe,
|
|
* -4 if path is a socket.
|
|
*/
|
|
int
|
|
fhandler_procnet::exists ()
|
|
{
|
|
const char *path = get_name ();
|
|
debug_printf ("exists (%s)", path);
|
|
path += proc_len + 1;
|
|
while (*path != 0 && !isdirsep (*path))
|
|
path++;
|
|
if (*path == 0)
|
|
return 1;
|
|
|
|
for (int i = 0; process_listing[i]; i++)
|
|
if (pathmatch (path + 1, process_listing[i]))
|
|
{
|
|
if (i == PROCNET_IFINET6)
|
|
{
|
|
if (!wincap.has_gaa_prefixes ()
|
|
|| !get_adapters_addresses (NULL, AF_INET6))
|
|
return 0;
|
|
}
|
|
fileid = i;
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
fhandler_procnet::fhandler_procnet ():
|
|
fhandler_proc ()
|
|
{
|
|
}
|
|
|
|
int
|
|
fhandler_procnet::fstat (struct __stat64 *buf)
|
|
{
|
|
fhandler_base::fstat (buf);
|
|
buf->st_mode &= ~_IFMT & NO_W;
|
|
int file_type = exists ();
|
|
switch (file_type)
|
|
{
|
|
case 0:
|
|
set_errno (ENOENT);
|
|
return -1;
|
|
case 1:
|
|
case 2:
|
|
buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
|
|
buf->st_nlink = 2;
|
|
return 0;
|
|
case -1:
|
|
default:
|
|
buf->st_mode |= S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
DIR *
|
|
fhandler_procnet::opendir ()
|
|
{
|
|
DIR *dir = fhandler_virtual::opendir ();
|
|
if (dir)
|
|
dir->__flags = 0;
|
|
return dir;
|
|
}
|
|
|
|
int
|
|
fhandler_procnet::readdir (DIR *dir, dirent *de)
|
|
{
|
|
int res = ENMFILE;
|
|
if (dir->__d_position >= PROCESS_LINK_COUNT)
|
|
goto out;
|
|
if (dir->__d_position == PROCNET_IFINET6)
|
|
{
|
|
if (!wincap.has_gaa_prefixes ()
|
|
|| !get_adapters_addresses (NULL, AF_INET6))
|
|
goto out;
|
|
}
|
|
strcpy (de->d_name, process_listing[dir->__d_position++]);
|
|
dir->__flags |= dirent_saw_dot | dirent_saw_dot_dot;
|
|
res = 0;
|
|
out:
|
|
syscall_printf ("%d = readdir (%p, %p) (%s)", res, dir, de, de->d_name);
|
|
return res;
|
|
}
|
|
|
|
int
|
|
fhandler_procnet::open (int flags, mode_t mode)
|
|
{
|
|
int process_file_no = -1;
|
|
|
|
int res = fhandler_virtual::open (flags, mode);
|
|
if (!res)
|
|
goto out;
|
|
|
|
nohandle (true);
|
|
|
|
const char *path;
|
|
path = get_name () + proc_len + 1;
|
|
while (*path != 0 && !isdirsep (*path))
|
|
path++;
|
|
|
|
if (*path == 0)
|
|
{
|
|
if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
|
|
{
|
|
set_errno (EEXIST);
|
|
res = 0;
|
|
goto out;
|
|
}
|
|
else if (flags & O_WRONLY)
|
|
{
|
|
set_errno (EISDIR);
|
|
res = 0;
|
|
goto out;
|
|
}
|
|
else
|
|
{
|
|
flags |= O_DIROPEN;
|
|
goto success;
|
|
}
|
|
}
|
|
|
|
process_file_no = -1;
|
|
for (int i = 0; process_listing[i]; i++)
|
|
{
|
|
if (path_prefix_p
|
|
(process_listing[i], path + 1, strlen (process_listing[i])))
|
|
process_file_no = i;
|
|
}
|
|
if (process_file_no == -1)
|
|
{
|
|
if (flags & O_CREAT)
|
|
{
|
|
set_errno (EROFS);
|
|
res = 0;
|
|
goto out;
|
|
}
|
|
else
|
|
{
|
|
set_errno (ENOENT);
|
|
res = 0;
|
|
goto out;
|
|
}
|
|
}
|
|
if (flags & O_WRONLY)
|
|
{
|
|
set_errno (EROFS);
|
|
res = 0;
|
|
goto out;
|
|
}
|
|
|
|
fileid = process_file_no;
|
|
if (!fill_filebuf ())
|
|
{
|
|
res = 0;
|
|
goto out;
|
|
}
|
|
|
|
if (flags & O_APPEND)
|
|
position = filesize;
|
|
else
|
|
position = 0;
|
|
|
|
success:
|
|
res = 1;
|
|
set_flags ((flags & ~O_TEXT) | O_BINARY);
|
|
set_open_status ();
|
|
out:
|
|
syscall_printf ("%d = fhandler_proc::open (%p, %d)", res, flags, mode);
|
|
return res;
|
|
}
|
|
|
|
bool
|
|
fhandler_procnet::fill_filebuf ()
|
|
{
|
|
switch (fileid)
|
|
{
|
|
case PROCNET_IFINET6:
|
|
{
|
|
bufalloc = filesize = format_procnet_ifinet6 (filebuf);
|
|
break;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static _off64_t
|
|
format_procnet_ifinet6 (char *&filebuf)
|
|
{
|
|
PIP_ADAPTER_ADDRESSES pa0 = NULL, pap;
|
|
PIP_ADAPTER_UNICAST_ADDRESS pua;
|
|
ULONG alloclen;
|
|
|
|
if (!wincap.has_gaa_prefixes ())
|
|
return 0;
|
|
_off64_t filesize = 0;
|
|
if (!get_adapters_addresses (&pa0, AF_INET6))
|
|
goto out;
|
|
alloclen = 0;
|
|
for (pap = pa0; pap; pap = pap->Next)
|
|
for (pua = pap->FirstUnicastAddress; pua; pua = pua->Next)
|
|
alloclen += 100;
|
|
if (!alloclen)
|
|
goto out;
|
|
filebuf = (char *) crealloc (filebuf, alloclen);
|
|
if (!filebuf)
|
|
goto out;
|
|
for (pap = pa0; pap; pap = pap->Next)
|
|
for (pua = pap->FirstUnicastAddress; pua; pua = pua->Next)
|
|
{
|
|
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)
|
|
pua->Address.lpSockaddr;
|
|
for (int i = 0; i < 8; ++i)
|
|
/* __small_sprintf generates upper-case hex. */
|
|
filesize += sprintf (filebuf + filesize, "%04x",
|
|
ntohs (sin6->sin6_addr.s6_addr16[i]));
|
|
filebuf[filesize++] = ' ';
|
|
filesize += __small_sprintf (filebuf + filesize,
|
|
"%02x %02x %02x %02x %s\n",
|
|
pap->IfIndex ?: pap->Ipv6IfIndex,
|
|
ip_addr_prefix (pua, pap->FirstPrefix),
|
|
((struct sockaddr_in6 *)
|
|
pua->Address.lpSockaddr)->sin6_scope_id,
|
|
pua->DadState,
|
|
pap->AdapterName);
|
|
}
|
|
|
|
out:
|
|
if (pa0)
|
|
free (pa0);
|
|
return filesize;
|
|
}
|