Signed-off-by: Huw Davies huw@codeweavers.com --- dlls/nsi/tests/nsi.c | 59 +++++++++ dlls/nsiproxy.sys/Makefile.in | 3 +- dlls/nsiproxy.sys/nsi.c | 1 + dlls/nsiproxy.sys/nsiproxy_private.h | 1 + dlls/nsiproxy.sys/tcp.c | 190 +++++++++++++++++++++++++++ include/wine/nsi.h | 29 ++++ 6 files changed, 282 insertions(+), 1 deletion(-) create mode 100644 dlls/nsiproxy.sys/tcp.c
diff --git a/dlls/nsi/tests/nsi.c b/dlls/nsi/tests/nsi.c index a21ac897e2c..fb95ab67d3b 100644 --- a/dlls/nsi/tests/nsi.c +++ b/dlls/nsi/tests/nsi.c @@ -33,6 +33,11 @@ static int bounded( ULONG64 val, ULONG64 lo, ULONG64 hi ) return lo <= val && val <= hi; }
+static int unstable( int val ) +{ + return !winetest_interactive || val; +} + static void test_nsi_api( void ) { DWORD rw_sizes[] = { FIELD_OFFSET(struct nsi_ndis_ifinfo_rw, name2), FIELD_OFFSET(struct nsi_ndis_ifinfo_rw, unk), @@ -795,6 +800,57 @@ static void test_ip_forward( int family ) winetest_pop_context(); }
+static void test_tcp_stats( int family ) +{ + DWORD err; + USHORT key = family; + struct nsi_tcp_stats_dynamic dyn, dyn2; + struct nsi_tcp_stats_static stat; + MIB_TCPSTATS table; + + winetest_push_context( family == AF_INET ? "AF_INET" : "AF_INET6" ); + + err = NsiGetAllParameters( 1, &NPI_MS_TCP_MODULEID, NSI_TCP_STATS_TABLE, &key, sizeof(key), NULL, 0, + &dyn, sizeof(dyn), &stat, sizeof(stat) ); + ok( !err, "got %x\n", err ); + + err = GetTcpStatisticsEx( &table, family ); +todo_wine_if(family == AF_INET6) + ok( !err, "got %d\n", err ); + if (err) goto err; + + err = NsiGetAllParameters( 1, &NPI_MS_TCP_MODULEID, NSI_TCP_STATS_TABLE, &key, sizeof(key), NULL, 0, + &dyn2, sizeof(dyn), NULL, 0 ); + ok( !err, "got %x\n", err ); + + ok( table.dwRtoAlgorithm == stat.rto_algo, "%d vs %d\n", table.dwRtoAlgorithm, stat.rto_algo ); + ok( table.dwRtoMin == stat.rto_min, "%d vs %d\n", table.dwRtoMin, stat.rto_min ); + ok( table.dwRtoMax == stat.rto_max, "%d vs %d\n", table.dwRtoMax, stat.rto_max ); + ok( table.dwMaxConn == stat.max_conns, "%d vs %d\n", table.dwMaxConn, stat.max_conns ); + + ok( unstable( table.dwActiveOpens == dyn.active_opens ), "%d vs %d\n", table.dwActiveOpens, dyn.active_opens ); + ok( unstable( table.dwPassiveOpens == dyn.passive_opens ), "%d vs %d\n", table.dwPassiveOpens, dyn.passive_opens ); + ok( bounded( table.dwAttemptFails, dyn.attempt_fails, dyn2.attempt_fails ), "%d vs [%d %d]\n", + table.dwAttemptFails, dyn.attempt_fails, dyn2.attempt_fails ); + ok( bounded( table.dwEstabResets, dyn.est_rsts, dyn2.est_rsts ), "%d vs [%d %d]\n", + table.dwEstabResets, dyn.est_rsts, dyn2.est_rsts ); + ok( unstable( table.dwCurrEstab == dyn.cur_est ), "%d vs %d\n", table.dwCurrEstab, dyn.cur_est ); + ok( bounded( table.dwInSegs, dyn.in_segs, dyn2.in_segs ), "%d vs [%I64d %I64d]\n", + table.dwInSegs, dyn.in_segs, dyn2.in_segs ); + ok( bounded( table.dwOutSegs, dyn.out_segs, dyn2.out_segs ), "%d vs [%I64d %I64d]\n", + table.dwOutSegs, dyn.out_segs, dyn2.out_segs ); + ok( bounded( table.dwRetransSegs, dyn.retrans_segs, dyn2.retrans_segs ), "%d vs [%d %d]\n", + table.dwRetransSegs, dyn.retrans_segs, dyn2.retrans_segs ); + ok( bounded( table.dwInErrs, dyn.in_errs, dyn2.in_errs ), "%d vs [%d %d]\n", + table.dwInErrs, dyn.in_errs, dyn2.in_errs ); + ok( bounded( table.dwOutRsts, dyn.out_rsts, dyn2.out_rsts ), "%d vs [%d %d]\n", + table.dwOutRsts, dyn.out_rsts, dyn2.out_rsts ); + ok( unstable( table.dwNumConns == dyn.num_conns ), "%d vs %d\n", table.dwNumConns, dyn.num_conns ); + +err: + winetest_pop_context(); +} + START_TEST( nsi ) { test_nsi_api(); @@ -814,4 +870,7 @@ START_TEST( nsi ) test_ip_neighbour( AF_INET6 ); test_ip_forward( AF_INET ); test_ip_forward( AF_INET6 ); + + test_tcp_stats( AF_INET ); + test_tcp_stats( AF_INET6 ); } diff --git a/dlls/nsiproxy.sys/Makefile.in b/dlls/nsiproxy.sys/Makefile.in index 07c2738e55b..3f8965236ba 100644 --- a/dlls/nsiproxy.sys/Makefile.in +++ b/dlls/nsiproxy.sys/Makefile.in @@ -6,4 +6,5 @@ C_SRCS = \ device.c \ ip.c \ ndis.c \ - nsi.c + nsi.c \ + tcp.c diff --git a/dlls/nsiproxy.sys/nsi.c b/dlls/nsiproxy.sys/nsi.c index 8e084a076fd..ef57c65ce4d 100644 --- a/dlls/nsiproxy.sys/nsi.c +++ b/dlls/nsiproxy.sys/nsi.c @@ -40,6 +40,7 @@ static const struct module *modules[] = &ndis_module, &ipv4_module, &ipv6_module, + &tcp_module, };
static const struct module_table *get_module_table( const NPI_MODULEID *id, DWORD table ) diff --git a/dlls/nsiproxy.sys/nsiproxy_private.h b/dlls/nsiproxy.sys/nsiproxy_private.h index 24f40e184a4..17e0189b378 100644 --- a/dlls/nsiproxy.sys/nsiproxy_private.h +++ b/dlls/nsiproxy.sys/nsiproxy_private.h @@ -115,3 +115,4 @@ struct module extern const struct module ndis_module DECLSPEC_HIDDEN; extern const struct module ipv4_module DECLSPEC_HIDDEN; extern const struct module ipv6_module DECLSPEC_HIDDEN; +extern const struct module tcp_module DECLSPEC_HIDDEN; diff --git a/dlls/nsiproxy.sys/tcp.c b/dlls/nsiproxy.sys/tcp.c new file mode 100644 index 00000000000..ffac9971316 --- /dev/null +++ b/dlls/nsiproxy.sys/tcp.c @@ -0,0 +1,190 @@ +/* + * nsiproxy.sys tcp module + * + * Copyright 2003, 2006, 2011 Juan Lang + * Copyright 2007 TransGaming Technologies Inc. + * Copyright 2021 Huw Davies + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ +#include "config.h" +#include <stdarg.h> + +#ifdef HAVE_SYS_TYPES_H +#include <sys/types.h> +#endif + +#ifdef HAVE_SYS_SOCKET_H +#include <sys/socket.h> +#endif + +#ifdef HAVE_SYS_SOCKETVAR_H +#include <sys/socketvar.h> +#endif + +#ifdef HAVE_NETINET_IN_H +#include <netinet/in.h> +#endif + +#ifdef HAVE_NETINET_TCP_VAR_H +#include <netinet/tcp_var.h> +#endif + +#ifdef HAVE_SYS_SYSCTL_H +#include <sys/sysctl.h> +#endif + +#include "ntstatus.h" +#define WIN32_NO_STATUS +#include "windef.h" +#include "winbase.h" +#include "winternl.h" +#define USE_WS_PREFIX +#include "winsock2.h" +#include "ifdef.h" +#include "netiodef.h" +#include "ws2ipdef.h" +#include "tcpmib.h" +#include "wine/nsi.h" +#include "wine/debug.h" + +#include "nsiproxy_private.h" + +WINE_DEFAULT_DEBUG_CHANNEL(nsi); + +static NTSTATUS tcp_stats_get_all_parameters( const void *key, DWORD key_size, void *rw_data, DWORD rw_size, + void *dynamic_data, DWORD dynamic_size, void *static_data, DWORD static_size ) +{ + struct nsi_tcp_stats_dynamic dyn; + struct nsi_tcp_stats_static stat; + const USHORT *family = key; + + TRACE( "%p %d %p %d %p %d %p %d\n", key, key_size, rw_data, rw_size, dynamic_data, dynamic_size, + static_data, static_size ); + + if (*family != WS_AF_INET && *family != WS_AF_INET6) return STATUS_NOT_SUPPORTED; + + memset( &dyn, 0, sizeof(dyn) ); + memset( &stat, 0, sizeof(stat) ); + +#ifdef __linux__ + { + NTSTATUS status = STATUS_NOT_SUPPORTED; + static const char hdr[] = "Tcp:"; + char buf[512], *ptr; + FILE *fp; + + /* linux merges tcp4 and tcp6 stats, so simply supply that for either family */ + if (!(fp = fopen( "/proc/net/snmp", "r" ))) return STATUS_NOT_SUPPORTED; + + while ((ptr = fgets( buf, sizeof(buf), fp ))) + { + if (_strnicmp( buf, hdr, sizeof(hdr) - 1 )) continue; + /* last line was a header, get another */ + if (!(ptr = fgets( buf, sizeof(buf), fp ))) break; + if (!_strnicmp( buf, hdr, sizeof(hdr) - 1 )) + { + DWORD in_segs, out_segs; + ptr += sizeof(hdr); + sscanf( ptr, "%u %u %u %u %u %u %u %u %u %u %u %u %u %u", + &stat.rto_algo, + &stat.rto_min, + &stat.rto_max, + &stat.max_conns, + &dyn.active_opens, + &dyn.passive_opens, + &dyn.attempt_fails, + &dyn.est_rsts, + &dyn.cur_est, + &in_segs, + &out_segs, + &dyn.retrans_segs, + &dyn.in_errs, + &dyn.out_rsts ); + dyn.in_segs = in_segs; + dyn.out_segs = out_segs; + if (dynamic_data) *(struct nsi_tcp_stats_dynamic *)dynamic_data = dyn; + if (static_data) *(struct nsi_tcp_stats_static *)static_data = stat; + status = STATUS_SUCCESS; + break; + } + } + fclose( fp ); + return status; + } +#elif defined(HAVE_SYS_SYSCTL_H) && defined(TCPCTL_STATS) && (defined(HAVE_STRUCT_TCPSTAT_TCPS_CONNATTEMPT) || defined(HAVE_STRUCT_TCP_STATS_TCPS_CONNATTEMPT)) + { +#ifndef TCPTV_MIN /* got removed in Mac OS X for some reason */ +#define TCPTV_MIN 2 +#define TCPTV_REXMTMAX 128 +#endif + int mib[] = { CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_STATS }; +#define hz 1000 +#if defined(HAVE_STRUCT_TCPSTAT_TCPS_CONNATTEMPT) + struct tcpstat tcp_stat; +#elif defined(HAVE_STRUCT_TCP_STATS_TCPS_CONNATTEMPT) + struct tcp_stats tcp_stat; +#endif + size_t needed = sizeof(tcp_stat); + + if (sysctl( mib, ARRAY_SIZE(mib), &tcp_stat, &needed, NULL, 0 ) == -1) return STATUS_NOT_SUPPORTED; + + stat.rto_algo = MIB_TCP_RTO_VANJ; + stat.rto_min = TCPTV_MIN; + stat.rto_max = TCPTV_REXMTMAX; + stat.max_conns = -1; + dyn.active_opens = tcp_stat.tcps_connattempt; + dyn.passive_opens = tcp_stat.tcps_accepts; + dyn.attempt_fails = tcp_stat.tcps_conndrops; + dyn.est_rsts = tcp_stat.tcps_drops; + dyn.cur_est = 0; + dyn.pad = 0; + dyn.in_segs = tcp_stat.tcps_rcvtotal; + dyn.out_segs = tcp_stat.tcps_sndtotal - tcp_stat.tcps_sndrexmitpack; + dyn.retrans_segs = tcp_stat.tcps_sndrexmitpack; + dyn.out_rsts = tcp_stat.tcps_sndctrl - tcp_stat.tcps_closed; + dyn.in_errs = tcp_stat.tcps_rcvbadsum + tcp_stat.tcps_rcvbadoff + tcp_stat.tcps_rcvmemdrop + tcp_stat.tcps_rcvshort; + dyn.num_conns = tcp_stat.tcps_connects; + if (dynamic_data) *(struct nsi_tcp_stats_dynamic *)dynamic_data = dyn; + if (static_data) *(struct nsi_tcp_stats_static *)static_data = stat; + return STATUS_SUCCESS; + } +#else + FIXME( "not implemented\n" ); + return STATUS_NOT_IMPLEMENTED; +#endif +} + +static struct module_table tcp_tables[] = +{ + { + NSI_TCP_STATS_TABLE, + { + sizeof(USHORT), 0, + sizeof(struct nsi_tcp_stats_dynamic), sizeof(struct nsi_tcp_stats_static) + }, + NULL, + tcp_stats_get_all_parameters, + }, + { + ~0u + } +}; + +const struct module tcp_module = +{ + &NPI_MS_TCP_MODULEID, + tcp_tables +}; diff --git a/include/wine/nsi.h b/include/wine/nsi.h index 2c72c16aedc..3c92fb81aae 100644 --- a/include/wine/nsi.h +++ b/include/wine/nsi.h @@ -292,6 +292,35 @@ struct nsi_ip_forward_static DWORD if_index; };
+/* Undocumented NSI TCP tables */ +#define NSI_TCP_STATS_TABLE 0 + +struct nsi_tcp_stats_dynamic +{ + DWORD active_opens; + DWORD passive_opens; + DWORD attempt_fails; + DWORD est_rsts; + DWORD cur_est; + DWORD pad; /* ? */ + ULONGLONG in_segs; + ULONGLONG out_segs; + DWORD retrans_segs; + DWORD out_rsts; + DWORD in_errs; + DWORD num_conns; + DWORD unk[12]; +}; + +struct nsi_tcp_stats_static +{ + DWORD rto_algo; + DWORD rto_min; + DWORD rto_max; + DWORD max_conns; + DWORD unk; +}; + /* Wine specific ioctl interface */
#define IOCTL_NSIPROXY_WINE_ENUMERATE_ALL CTL_CODE(FILE_DEVICE_NETWORK, 0x400, METHOD_BUFFERED, 0)