* net.cc: Run indent.

* autoload.cc (GetIfEntry): Add symbol.
	* net.cc (get_2k_ifconf): Get the interface entries with the GetIfEntry
	call intead of the GetIfTable call. Clean-up multiple IP address naming.
This commit is contained in:
Corinna Vinschen 2002-11-10 13:43:26 +00:00
parent 5d7bffe927
commit 7922485391
3 changed files with 531 additions and 428 deletions

View File

@ -1,3 +1,13 @@
2002-11-10 Corinna Vinschen <corinna@vinschen.de>
* net.cc: Run indent.
2002-11-10 Alexander Gottwald <Alexander.Gottwald@s1999.tu-chemnitz.de>
* autoload.cc (GetIfEntry): Add symbol.
* net.cc (get_2k_ifconf): Get the interface entries with the GetIfEntry
call intead of the GetIfTable call. Clean-up multiple IP address naming.
2002-11-09 Sergey Okhapkin <sos@prospect.com.ru>
* fhandler.h (class fhandler_pipe): New ioctl() method.

View File

@ -485,6 +485,7 @@ LoadDLLfuncEx (WSAEventSelect, 12, ws2_32, 1)
LoadDLLfuncEx (WSAEnumNetworkEvents, 12, ws2_32, 1)
LoadDLLfuncEx (GetIfTable, 12, iphlpapi, 1)
LoadDLLfuncEx (GetIfEntry, 4, iphlpapi, 1)
LoadDLLfuncEx (GetIpAddrTable, 12, iphlpapi, 1)
LoadDLLfunc (CoInitialize, 4, ole32)

View File

@ -34,7 +34,8 @@ details. */
#include "registry.h"
#include "wsock_event.h"
extern "C" {
extern "C"
{
int h_errno;
int __stdcall rcmd (char **ahost, unsigned short inport, char *locuser,
@ -104,10 +105,12 @@ static fhandler_socket *
get (const int fd)
{
cygheap_fdget cfd (fd);
if (cfd < 0)
return 0;
fhandler_socket *const fh = cfd->is_socket ();
if (!fh)
set_errno (ENOTSOCK);
@ -119,6 +122,7 @@ static SOCKET __stdcall
set_socket_inheritance (SOCKET sock)
{
SOCKET osock = sock;
if (!DuplicateHandle (hMainProc, (HANDLE) sock, hMainProc, (HANDLE *) &sock,
0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
system_printf ("DuplicateHandle failed %E");
@ -178,6 +182,7 @@ cygwin_inet_ntoa (struct in_addr in)
#endif
char *res = inet_ntoa (in);
if (ntoa_buf)
{
free (ntoa_buf);
@ -195,6 +200,7 @@ cygwin_inet_addr (const char *cp)
if (check_null_str_errno (cp))
return INADDR_NONE;
unsigned long res = inet_addr (cp);
return res;
}
@ -208,6 +214,7 @@ cygwin_inet_aton (const char *cp, struct in_addr *inp)
return 0;
unsigned long res = inet_addr (cp);
if (res == INADDR_NONE && strcmp (cp, "255.255.255.255"))
return 0;
if (inp)
@ -224,6 +231,7 @@ cygwin_inet_network (const char *cp)
if (check_null_str_errno (cp))
return INADDR_NONE;
unsigned int res = inet_network (cp);
return res;
}
@ -280,8 +288,7 @@ struct tl
int e;
};
static NO_COPY struct tl errmap[] =
{
static NO_COPY struct tl errmap[] = {
{WSAEINTR, "WSAEINTR", EINTR},
{WSAEWOULDBLOCK, "WSAEWOULDBLOCK", EWOULDBLOCK},
{WSAEINPROGRESS, "WSAEINPROGRESS", EINPROGRESS},
@ -342,6 +349,7 @@ __set_winsock_errno (const char *fn, int ln)
{
DWORD werr = WSAGetLastError ();
int err = find_winsock_errno (werr);
set_errno (err);
syscall_printf ("%s:%d - winsock error %d -> errno %d", fn, ln, werr, err);
}
@ -350,8 +358,7 @@ __set_winsock_errno (const char *fn, int ln)
* Since the member `s' isn't used for debug output we can use it
* for the error text returned by herror and hstrerror.
*/
static NO_COPY struct tl host_errmap[] =
{
static NO_COPY struct tl host_errmap[] = {
{WSAHOST_NOT_FOUND, "Unknown host", HOST_NOT_FOUND},
{WSATRY_AGAIN, "Host name lookup failure", TRY_AGAIN},
{WSANO_RECOVERY, "Unknown server error", NO_RECOVERY},
@ -366,6 +373,7 @@ set_host_errno ()
int i;
int why = WSAGetLastError ();
for (i = 0; host_errmap[i].w != 0; ++i)
if (why == host_errmap[i].w)
break;
@ -445,6 +453,7 @@ dup_protoent_ptr (struct protoent *src)
return NULL;
struct protoent *dst = (struct protoent *) calloc (1, sizeof *dst);
if (!dst)
return NULL;
@ -508,11 +517,14 @@ fdsock (int& fd, const char *name, SOCKET soc)
{
/* NT systems apparently set sockets to inheritable by default */
SetHandleInformation ((HANDLE) soc, HANDLE_FLAG_INHERIT, 0);
debug_printf ("reset socket inheritance since winsock2_active %d", winsock2_active);
debug_printf ("reset socket inheritance since winsock2_active %d",
winsock2_active);
}
else
debug_printf ("not setting socket inheritance since winsock2_active %d", winsock2_active);
fhandler_socket *fh = (fhandler_socket *) cygheap->fdtab.build_fhandler (fd, FH_SOCKET, name);
debug_printf ("not setting socket inheritance since winsock2_active %d",
winsock2_active);
fhandler_socket *fh =
(fhandler_socket *) cygheap->fdtab.build_fhandler (fd, FH_SOCKET, name);
fh->set_io_handle ((HANDLE) soc);
fh->set_flags (O_RDWR | O_BINARY);
fh->set_r_no_interrupt (winsock2_active);
@ -543,6 +555,7 @@ cygwin_socket (int af, int type, int protocol)
}
const char *name;
if (af == AF_INET)
name = (type == SOCK_STREAM ? "/dev/tcp" : "/dev/udp");
else
@ -729,6 +742,7 @@ cygwin_getsockopt (int fd, int level, int optname, void *optval, int *optlen)
if (optname == SO_ERROR)
{
int *e = (int *) optval;
*e = find_winsock_errno (*e);
}
@ -784,6 +798,7 @@ struct pservent
short s_port;
char *s_proto;
};
#pragma pack(pop)
static struct servent *
dup_servent_ptr (struct servent *src)
@ -792,6 +807,7 @@ dup_servent_ptr (struct servent *src)
return NULL;
struct servent *dst = (struct servent *) calloc (1, sizeof *dst);
if (!dst)
return NULL;
@ -803,6 +819,7 @@ dup_servent_ptr (struct servent *src)
if (src->s_aliases && !(dst->s_aliases = dup_char_list (src->s_aliases)))
goto out;
char *s_proto;
if (IsBadReadPtr (src->s_proto, sizeof (src->s_proto))
&& !IsBadReadPtr (((pservent *) src)->s_proto, sizeof (src->s_proto)))
s_proto = ((pservent *) src)->s_proto;
@ -868,8 +885,7 @@ cygwin_gethostname (char *name, size_t len)
if (__check_null_invalid_struct_errno (name, len))
return -1;
if (wsock32_handle == NULL ||
win32_gethostname (name, len) == SOCKET_ERROR)
if (wsock32_handle == NULL || win32_gethostname (name, len) == SOCKET_ERROR)
{
DWORD local_len = len;
@ -906,6 +922,7 @@ dup_hostent_ptr (struct hostent *src)
return NULL;
struct hostent *dst = (struct hostent *) calloc (1, sizeof *dst);
if (!dst)
return NULL;
@ -1192,8 +1209,7 @@ getdomainname (char *domain, size_t len)
(!wincap.is_winnt ()) ? "System" : "SYSTEM",
"CurrentControlSet", "Services",
(!wincap.is_winnt ()) ? "VxD" : "Tcpip",
(!wincap.is_winnt ()) ? "MSTCP" : "Parameters",
NULL);
(!wincap.is_winnt ()) ? "MSTCP" : "Parameters", NULL);
/* FIXME: Are registry keys case sensitive? */
if (r.error () || r.get_string ("Domain", domain, len, "") != ERROR_SUCCESS)
@ -1216,59 +1232,139 @@ static void
get_2k_ifconf (struct ifconf *ifc, int what)
{
int cnt = 0;
char eth[2] = "/", ppp[2] = "/", slp[2] = "/", sub[2] = "0", tok[2] = "/";
int ethId = 0, pppId = 0, slpId = 0, tokId = 0;
/* Union maps buffer to correct struct */
struct ifreq *ifr = ifc->ifc_req;
DWORD if_cnt, ip_cnt, lip, lnp;
DWORD siz_if_table = 0;
DWORD ip_cnt, lip, lnp;
DWORD siz_ip_table = 0;
PMIB_IFTABLE ift;
PMIB_IPADDRTABLE ipt;
PMIB_IFROW ifrow;
struct sockaddr_in *sa = NULL;
struct sockaddr *so = NULL;
if (GetIfTable (NULL, &siz_if_table, TRUE) == ERROR_INSUFFICIENT_BUFFER &&
GetIpAddrTable (NULL, &siz_ip_table, TRUE) == ERROR_INSUFFICIENT_BUFFER &&
(ift = (PMIB_IFTABLE) alloca (siz_if_table)) &&
(ipt = (PMIB_IPADDRTABLE) alloca (siz_ip_table)) &&
!GetIfTable (ift, &siz_if_table, TRUE) &&
!GetIpAddrTable (ipt, &siz_ip_table, TRUE))
typedef struct ifcount_t
{
/* Iterate over all known interfaces */
for (if_cnt = 0; if_cnt < ift->dwNumEntries; ++if_cnt)
DWORD ifIndex;
size_t count;
unsigned int enumerated; // for eth0:1
unsigned int classId; // for eth0, tok0 ...
};
ifcount_t *iflist, *ifEntry;
if (GetIpAddrTable (NULL, &siz_ip_table, TRUE) == ERROR_INSUFFICIENT_BUFFER
&& (ifrow = (PMIB_IFROW) alloca (sizeof (MIB_IFROW)))
&& (ipt = (PMIB_IPADDRTABLE) alloca (siz_ip_table))
&& !GetIpAddrTable (ipt, &siz_ip_table, TRUE))
{
*sub = '0';
iflist =
(ifcount_t *) alloca (sizeof (ifcount_t) * (ipt->dwNumEntries + 1));
memset (iflist, 0, sizeof (ifcount_t) * (ipt->dwNumEntries + 1));
for (ip_cnt = 0; ip_cnt < ipt->dwNumEntries; ++ip_cnt)
{
ifEntry = iflist;
/* search for matching entry (and stop at first free entry) */
while (ifEntry->count != 0)
{
if (ifEntry->ifIndex == ipt->table[ip_cnt].dwIndex)
break;
ifEntry++;
}
if (ifEntry->count == 0)
{
ifEntry->count = 1;
ifEntry->ifIndex = ipt->table[ip_cnt].dwIndex;
}
else
{
ifEntry->count++;
}
}
// reset the last element. This is just the stopper for the loop.
iflist[ipt->dwNumEntries].count = 0;
/* Iterate over all configured IP-addresses */
for (ip_cnt = 0; ip_cnt < ipt->dwNumEntries; ++ip_cnt)
{
/* Does the IP address belong to the interface? */
if (ipt->table[ip_cnt].dwIndex == ift->table[if_cnt].dwIndex)
memset (ifrow, 0, sizeof (MIB_IFROW));
ifrow->dwIndex = ipt->table[ip_cnt].dwIndex;
if (GetIfEntry (ifrow) != NO_ERROR)
continue;
ifcount_t *ifEntry = iflist;
/* search for matching entry (and stop at first free entry) */
while (ifEntry->count != 0)
{
if (ifEntry->ifIndex == ipt->table[ip_cnt].dwIndex)
break;
ifEntry++;
}
/* Setup the interface name */
switch (ift->table[if_cnt].dwType)
switch (ifrow->dwType)
{
case MIB_IF_TYPE_TOKENRING:
++*tok;
strcpy (ifr->ifr_name, "tok");
strcat (ifr->ifr_name, tok);
if (ifEntry->enumerated == 0)
{
ifEntry->classId = tokId++;
__small_sprintf (ifr->ifr_name, "tok%u",
ifEntry->classId);
}
else
{
__small_sprintf (ifr->ifr_name, "tok%u:%u",
ifEntry->classId,
ifEntry->enumerated - 1);
}
ifEntry->enumerated++;
break;
case MIB_IF_TYPE_ETHERNET:
if (*sub == '0')
++*eth;
strcpy (ifr->ifr_name, "eth");
strcat (ifr->ifr_name, eth);
if (ifEntry->enumerated == 0)
{
ifEntry->classId = ethId++;
__small_sprintf (ifr->ifr_name, "eth%u",
ifEntry->classId);
}
else
{
__small_sprintf (ifr->ifr_name, "eth%u:%u",
ifEntry->classId,
ifEntry->enumerated - 1);
}
ifEntry->enumerated++;
break;
case MIB_IF_TYPE_PPP:
++*ppp;
strcpy (ifr->ifr_name, "ppp");
strcat (ifr->ifr_name, ppp);
if (ifEntry->enumerated == 0)
{
ifEntry->classId = pppId++;
__small_sprintf (ifr->ifr_name, "ppp%u",
ifEntry->classId);
}
else
{
__small_sprintf (ifr->ifr_name, "ppp%u:%u",
ifEntry->classId,
ifEntry->enumerated - 1);
}
ifEntry->enumerated++;
break;
case MIB_IF_TYPE_SLIP:
++*slp;
strcpy (ifr->ifr_name, "slp");
strcat (ifr->ifr_name, slp);
if (ifEntry->enumerated == 0)
{
ifEntry->classId = slpId++;
__small_sprintf (ifr->ifr_name, "slp%u",
ifEntry->classId);
}
else
{
__small_sprintf (ifr->ifr_name, "slp%u:%u",
ifEntry->classId,
ifEntry->enumerated - 1);
}
ifEntry->enumerated++;
break;
case MIB_IF_TYPE_LOOPBACK:
strcpy (ifr->ifr_name, "lo");
@ -1276,12 +1372,6 @@ get_2k_ifconf (struct ifconf *ifc, int what)
default:
continue;
}
if (*sub > '0')
{
strcat (ifr->ifr_name, ":");
strcat (ifr->ifr_name, sub);
}
++*sub;
/* setup sockaddr struct */
switch (what)
{
@ -1314,17 +1404,17 @@ get_2k_ifconf (struct ifconf *ifc, int what)
case SIOCGIFHWADDR:
so = &ifr->ifr_hwaddr;
for (UINT i = 0; i < IFHWADDRLEN; ++i)
if (i >= ift->table[if_cnt].dwPhysAddrLen)
if (i >= ifrow->dwPhysAddrLen)
so->sa_data[i] = '\0';
else
so->sa_data[i] = ift->table[if_cnt].bPhysAddr[i];
so->sa_data[i] = ifrow->bPhysAddr[i];
so->sa_family = AF_INET;
break;
case SIOCGIFMETRIC:
ifr->ifr_metric = 1;
break;
case SIOCGIFMTU:
ifr->ifr_mtu = ift->table[if_cnt].dwMtu;
ifr->ifr_mtu = ifrow->dwMtu;
break;
}
++cnt;
@ -1333,8 +1423,7 @@ get_2k_ifconf (struct ifconf *ifc, int what)
goto done;
}
}
}
}
done:
/* Set the correct length */
ifc->ifc_len = cnt * sizeof (struct ifreq);
@ -1370,8 +1459,7 @@ get_nt_ifconf (struct ifconf *ifc, int what)
"SYSTEM\\"
"CurrentControlSet\\"
"Services\\"
"Tcpip\\"
"Linkage",
"Tcpip\\" "Linkage",
0, KEY_READ, &key) == ERROR_SUCCESS)
{
if (RegQueryValueEx (key, "Bind",
@ -1423,8 +1511,7 @@ get_nt_ifconf (struct ifconf *ifc, int what)
ip += strlen (ip) + 1, np += strlen (np) + 1)
{
if ((caddr_t)++ ifr > ifc->ifc_buf
+ ifc->ifc_len
- sizeof (struct ifreq))
+ ifc->ifc_len - sizeof (struct ifreq))
break;
if (!strncmp (bp, "NdisWan", 7))
@ -1456,7 +1543,8 @@ get_nt_ifconf (struct ifconf *ifc, int what)
case SIOCGIFCONF:
case SIOCGIFADDR:
sa = (struct sockaddr_in *) &ifr->ifr_addr;
sa->sin_addr.s_addr = cygwin_inet_addr (dhcpaddress);
sa->sin_addr.s_addr =
cygwin_inet_addr (dhcpaddress);
sa->sin_family = AF_INET;
sa->sin_port = 0;
break;
@ -1591,8 +1679,7 @@ get_95_ifconf (struct ifconf *ifc, int what)
char adapter[256], ip[256], np[256];
if (res != ERROR_SUCCESS
|| RegOpenKeyEx (key, ifname, 0,
KEY_READ, &ifkey) != ERROR_SUCCESS)
|| RegOpenKeyEx (key, ifname, 0, KEY_READ, &ifkey) != ERROR_SUCCESS)
continue;
if (RegQueryValueEx (ifkey, "Driver", 0,
@ -1620,8 +1707,7 @@ get_95_ifconf (struct ifconf *ifc, int what)
(size = sizeof np, &size)) == ERROR_SUCCESS)
{
if ((caddr_t)++ ifr > ifc->ifc_buf
+ ifc->ifc_len
- sizeof (struct ifreq))
+ ifc->ifc_len - sizeof (struct ifreq))
goto out;
switch (what)
@ -1766,6 +1852,7 @@ get_ifconf (struct ifconf *ifc, int what)
}
OSVERSIONINFO os_version_info;
memset (&os_version_info, 0, sizeof os_version_info);
os_version_info.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
GetVersionEx (&os_version_info);
@ -1794,12 +1881,14 @@ cygwin_rcmd (char **ahost, unsigned short inport, char *locuser,
return (int) INVALID_SOCKET;
cygheap_fdnew res_fd;
if (res_fd < 0)
goto done;
if (fd2p)
{
cygheap_fdnew newfd (res_fd, false);
if (*fd2p < 0)
goto done;
*fd2p = newfd;
@ -1833,6 +1922,7 @@ cygwin_rresvport (int *port)
return -1;
cygheap_fdnew res_fd;
if (res_fd < 0)
res = -1;
else
@ -1866,11 +1956,13 @@ cygwin_rexec (char **ahost, unsigned short inport, char *locuser,
return (int) INVALID_SOCKET;
cygheap_fdnew res_fd;
if (res_fd < 0)
goto done;
if (fd2p)
{
cygheap_fdnew newfd (res_fd);
if (newfd < 0)
goto done;
*fd2p = newfd;
@ -1929,6 +2021,7 @@ socketpair (int family, int type, int protocol, int *sb)
{
sb[0] = sb0;
cygheap_fdnew sb1 (sb0, false);
if (sb1 < 0)
goto done;
@ -2009,8 +2102,7 @@ socketpair (int family, int type, int protocol, int *sb)
sock_out.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
/* Do a connect */
if (connect (outsock, (struct sockaddr *) &sock_in,
sizeof (sock_in)) < 0)
if (connect (outsock, (struct sockaddr *) &sock_in, sizeof (sock_in)) < 0)
{
debug_printf ("connect error");
set_winsock_errno ();