如何从任何进程关闭 Windows 上的套接字(ipv4 和 ipv6)连接? [英] How do I close a socket (ipv4 and ipv6) connection on Windows from any process?

查看:34
本文介绍了如何从任何进程关闭 Windows 上的套接字(ipv4 和 ipv6)连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Windows 上关闭 tcp v4 和 tcp v6 连接?我不想终止具有开放连接的整个进程,因为这显然会使其他所有人退出该进程.我需要从单独的进程执行此操作,因此无法访问套接字句柄等.我使用 Windows API 获取 tcp 表等,因此我知道哪些连接处于活动状态.

How do I close tcp v4 and tcp v6 connections on Windows? I don't want to kill the entire process that has the open connection as this obviously will kick everyone else off that process. I need to do this from a separate process, and so will not have access to socket handles, etc. I am using Windows API to get tcp table, etc. so I know which connections are active.

推荐答案

由于我使用的是 C#,我无法 PInvoke SetTcpEntry,即使作为管理员使用 app.manifest 文件,它总是发送 317 错误.所以我创建了一个 C++ .exe 来使用 SetTcpEntry 在命令行上关闭一个逗号分隔的 ipv4 地址列表,即使没有 app.manifest 文件也能正常工作.这解决了踢 ipv4 连接的问题.

Since I'm using C#, I cannot PInvoke SetTcpEntry, even as administrator with an app.manifest file, it always sends a 317 error. So I created a C++ .exe to close a comma separated list of ipv4 addresses on the command line using SetTcpEntry, works fine even without an app.manifest file. That solves kicking ipv4 connections.

我尝试将 get handles 方法与 NtQuerySystemInformation 结合使用,但始终无法使其正常工作,而且它是一个私有的、几乎没有文档记录的 API,使用起来似乎不安全.

I tried using the get handles approach with NtQuerySystemInformation but never could get it working quite right, and it is a private mostly undocumented API and seems unsafe to use.

因此,对于 ipv6,我使用 windivert 并将 RST 标志注入具有特定 IP 地址的 ipv6 数据包.它就像设置传入数据包的 RST 标志一样简单,然后再使用 windivert 将其发送出去.缺点是,如果客户端从不发送另一个数据包,则 ipv6 套接字仍然无限期地保持打开状态.

So, for ipv6, I am using windivert and injecting RST flag to ipv6 packets with certain ip addresses. It is as simple as setting the RST flag of an incoming packet before sending it on through with windivert. The downside is, if the client never sends another packet, the ipv6 socket still stays open indefinitely.

也许有一天微软会添加一个 SetTcpEntry6 函数,但在那之前这似乎是唯一现实的方法.

Perhaps someday Microsoft will add a SetTcpEntry6 function, but until then this appears to be the only realistic way.

这篇关于如何从任何进程关闭 Windows 上的套接字(ipv4 和 ipv6)连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆