2007-01-18 03:26:58 +08:00
|
|
|
/* fhandler_procnet.cc: fhandler for /proc/net virtual filesystem
|
|
|
|
|
2011-05-29 02:17:09 +08:00
|
|
|
Copyright 2007, 2008, 2009, 2010, 2011 Red Hat, Inc.
|
2007-01-18 03:26:58 +08:00
|
|
|
|
|
|
|
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"
|
2009-01-21 02:04:32 +08:00
|
|
|
#include "fhandler_virtual.h"
|
2007-01-18 03:26:58 +08:00
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
|
|
|
|
|
|
|
#include <netdb.h>
|
|
|
|
#define USE_SYS_TYPES_FD_SET
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <iphlpapi.h>
|
2007-01-18 18:30:43 +08:00
|
|
|
#include <asm/byteorder.h>
|
|
|
|
#include <cygwin/in6.h>
|
2007-01-18 03:26:58 +08:00
|
|
|
|
|
|
|
#define _COMPILING_NEWLIB
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2007-01-22 06:54:05 +08:00
|
|
|
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);
|
2007-01-18 03:26:58 +08:00
|
|
|
|
2009-01-21 02:04:32 +08:00
|
|
|
static _off64_t format_procnet_ifinet6 (void *, char *&);
|
2007-01-18 03:26:58 +08:00
|
|
|
|
2009-01-21 02:04:32 +08:00
|
|
|
static const virt_tab_t procnet_tab[] =
|
2007-01-18 03:26:58 +08:00
|
|
|
{
|
2011-06-06 13:02:13 +08:00
|
|
|
{ _VN ("."), FH_PROCNET, virt_directory, NULL },
|
2010-09-06 17:47:01 +08:00
|
|
|
{ _VN (".."), FH_PROCNET, virt_directory, NULL },
|
|
|
|
{ _VN ("if_inet6"), FH_PROCNET, virt_file, format_procnet_ifinet6 },
|
2011-06-13 04:15:26 +08:00
|
|
|
{ NULL, 0, FH_NADA, virt_none, NULL }
|
2007-01-18 03:26:58 +08:00
|
|
|
};
|
|
|
|
|
2009-01-21 02:04:32 +08:00
|
|
|
static const int PROCNET_LINK_COUNT =
|
|
|
|
(sizeof (procnet_tab) / sizeof (virt_tab_t)) - 1;
|
2007-01-18 03:26:58 +08:00
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
2010-09-06 17:47:01 +08:00
|
|
|
virtual_ftype_t
|
2007-01-18 03:26:58 +08:00
|
|
|
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)
|
2010-09-06 17:47:01 +08:00
|
|
|
return virt_rootdir;
|
|
|
|
|
|
|
|
virt_tab_t *entry = virt_tab_search (path + 1, false, procnet_tab,
|
|
|
|
PROCNET_LINK_COUNT);
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
if (entry->type == virt_file)
|
|
|
|
{
|
|
|
|
if (!wincap.has_gaa_prefixes ()
|
|
|
|
|| !get_adapters_addresses (NULL, AF_INET6))
|
|
|
|
return virt_none;
|
|
|
|
}
|
|
|
|
fileid = entry - procnet_tab;
|
|
|
|
return entry->type;
|
|
|
|
}
|
2009-01-21 02:04:32 +08:00
|
|
|
return virt_none;
|
2007-01-18 03:26:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2009-01-21 02:04:32 +08:00
|
|
|
case virt_none:
|
2007-01-18 03:26:58 +08:00
|
|
|
set_errno (ENOENT);
|
|
|
|
return -1;
|
2009-01-21 02:04:32 +08:00
|
|
|
case virt_directory:
|
|
|
|
case virt_rootdir:
|
2007-01-18 03:26:58 +08:00
|
|
|
buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
|
|
|
|
buf->st_nlink = 2;
|
|
|
|
return 0;
|
2009-01-21 02:04:32 +08:00
|
|
|
case virt_file:
|
2007-01-18 03:26:58 +08:00
|
|
|
default:
|
|
|
|
buf->st_mode |= S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_procnet::readdir (DIR *dir, dirent *de)
|
|
|
|
{
|
|
|
|
int res = ENMFILE;
|
2009-01-21 02:04:32 +08:00
|
|
|
if (dir->__d_position >= PROCNET_LINK_COUNT)
|
2007-01-18 03:26:58 +08:00
|
|
|
goto out;
|
2009-01-21 02:04:32 +08:00
|
|
|
if (procnet_tab[dir->__d_position].type == virt_file)
|
2007-01-18 03:26:58 +08:00
|
|
|
{
|
2007-01-22 06:54:05 +08:00
|
|
|
if (!wincap.has_gaa_prefixes ()
|
|
|
|
|| !get_adapters_addresses (NULL, AF_INET6))
|
2007-01-18 03:26:58 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2009-01-21 02:04:32 +08:00
|
|
|
strcpy (de->d_name, procnet_tab[dir->__d_position++].name);
|
2007-01-18 03:26:58 +08:00
|
|
|
dir->__flags |= dirent_saw_dot | dirent_saw_dot_dot;
|
|
|
|
res = 0;
|
|
|
|
out:
|
2011-12-04 05:43:27 +08:00
|
|
|
syscall_printf ("%d = readdir(%p, %p) (%s)", res, dir, de, de->d_name);
|
2007-01-18 03:26:58 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_procnet::open (int flags, mode_t mode)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-06 17:47:01 +08:00
|
|
|
virt_tab_t *entry;
|
|
|
|
entry = virt_tab_search (path + 1, true, procnet_tab, PROCNET_LINK_COUNT);
|
|
|
|
if (!entry)
|
2007-01-18 03:26:58 +08:00
|
|
|
{
|
2010-09-06 17:47:01 +08:00
|
|
|
set_errno ((flags & O_CREAT) ? EROFS : ENOENT);
|
|
|
|
res = 0;
|
|
|
|
goto out;
|
2007-01-18 03:26:58 +08:00
|
|
|
}
|
|
|
|
if (flags & O_WRONLY)
|
|
|
|
{
|
|
|
|
set_errno (EROFS);
|
|
|
|
res = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-09-06 17:47:01 +08:00
|
|
|
fileid = entry - procnet_tab;
|
2007-01-18 03:26:58 +08:00
|
|
|
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:
|
2011-12-04 05:43:27 +08:00
|
|
|
syscall_printf ("%d = fhandler_proc::open(%p, %d)", res, flags, mode);
|
2007-01-18 03:26:58 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
fhandler_procnet::fill_filebuf ()
|
|
|
|
{
|
2009-01-21 02:04:32 +08:00
|
|
|
if (procnet_tab[fileid].format_func)
|
2007-01-18 03:26:58 +08:00
|
|
|
{
|
2009-01-21 02:04:32 +08:00
|
|
|
filesize = procnet_tab[fileid].format_func (NULL, filebuf);
|
|
|
|
return true;
|
2007-01-18 03:26:58 +08:00
|
|
|
}
|
2009-01-21 02:04:32 +08:00
|
|
|
return false;
|
2007-01-18 03:26:58 +08:00
|
|
|
}
|
|
|
|
|
2008-06-11 03:58:58 +08:00
|
|
|
/* Return the same scope values as Linux. */
|
|
|
|
static unsigned int
|
|
|
|
get_scope (struct in6_addr *addr)
|
|
|
|
{
|
|
|
|
if (IN6_IS_ADDR_LOOPBACK (addr))
|
|
|
|
return 0x10;
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL (addr))
|
|
|
|
return 0x20;
|
|
|
|
if (IN6_IS_ADDR_SITELOCAL (addr))
|
|
|
|
return 0x40;
|
|
|
|
if (IN6_IS_ADDR_V4COMPAT (addr))
|
|
|
|
return 0x80;
|
|
|
|
return 0x0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert DAD state into Linux compatible values. */
|
|
|
|
static unsigned int dad_to_flags[] =
|
|
|
|
{
|
|
|
|
0x02, /* Invalid -> NODAD */
|
|
|
|
0x40, /* Tentative -> TENTATIVE */
|
2008-06-11 16:38:39 +08:00
|
|
|
0xc0, /* Duplicate -> PERMANENT | TENTATIVE */
|
2008-06-11 03:58:58 +08:00
|
|
|
0x20, /* Deprecated -> DEPRECATED */
|
|
|
|
0x80 /* Preferred -> PERMANENT */
|
|
|
|
};
|
|
|
|
|
2007-01-18 03:26:58 +08:00
|
|
|
static _off64_t
|
2009-01-21 02:04:32 +08:00
|
|
|
format_procnet_ifinet6 (void *, char *&filebuf)
|
2007-01-18 03:26:58 +08:00
|
|
|
{
|
|
|
|
PIP_ADAPTER_ADDRESSES pa0 = NULL, pap;
|
|
|
|
PIP_ADAPTER_UNICAST_ADDRESS pua;
|
2007-01-22 06:54:05 +08:00
|
|
|
ULONG alloclen;
|
2007-01-18 03:26:58 +08:00
|
|
|
|
2007-01-22 06:54:05 +08:00
|
|
|
if (!wincap.has_gaa_prefixes ())
|
|
|
|
return 0;
|
2007-01-18 03:26:58 +08:00
|
|
|
_off64_t filesize = 0;
|
2007-01-22 06:54:05 +08:00
|
|
|
if (!get_adapters_addresses (&pa0, AF_INET6))
|
|
|
|
goto out;
|
2007-01-18 03:26:58 +08:00
|
|
|
alloclen = 0;
|
|
|
|
for (pap = pa0; pap; pap = pap->Next)
|
2007-01-22 06:54:05 +08:00
|
|
|
for (pua = pap->FirstUnicastAddress; pua; pua = pua->Next)
|
|
|
|
alloclen += 100;
|
|
|
|
if (!alloclen)
|
|
|
|
goto out;
|
2007-01-18 03:26:58 +08:00
|
|
|
filebuf = (char *) crealloc (filebuf, alloclen);
|
2007-01-22 06:54:05 +08:00
|
|
|
if (!filebuf)
|
|
|
|
goto out;
|
2007-01-18 03:26:58 +08:00
|
|
|
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++] = ' ';
|
2008-06-11 01:24:00 +08:00
|
|
|
filesize += sprintf (filebuf + filesize,
|
|
|
|
"%02lx %02x %02x %02x %s\n",
|
2008-06-11 03:58:58 +08:00
|
|
|
pap->Ipv6IfIndex,
|
2008-06-11 01:24:00 +08:00
|
|
|
ip_addr_prefix (pua, pap->FirstPrefix),
|
2008-06-11 03:58:58 +08:00
|
|
|
get_scope (&((struct sockaddr_in6 *)
|
|
|
|
pua->Address.lpSockaddr)->sin6_addr),
|
|
|
|
dad_to_flags [pua->DadState],
|
2008-06-11 01:24:00 +08:00
|
|
|
pap->AdapterName);
|
2007-01-18 03:26:58 +08:00
|
|
|
}
|
|
|
|
|
2007-01-22 06:54:05 +08:00
|
|
|
out:
|
|
|
|
if (pa0)
|
|
|
|
free (pa0);
|
|
|
|
return filesize;
|
2007-01-18 03:26:58 +08:00
|
|
|
}
|