在不相关的进程之间共享套接字(监听) [英] Share socket (listen) between unrelated processes

查看:100
本文介绍了在不相关的进程之间共享套接字(监听)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux 3.9内核和更高版本中运行,我有一个应用程序X,它在特定的套接字上侦听连接.我想编写一个不相关的应用程序Y,该应用程序跟踪连接到此套接字的尝试次数,源IP等.

Running in Linux 3.9 kernel and later, I have an application X, which listens on a particular socket for connections. I want to write an unrelated application, Y, which tracks the number of attempts to connect to this socket, the source IP, etc.

在c ++中(理想情况下通过Qt库)是否可以共享/监视不相关进程已经使用的套接字?我发现了几个StackOverflow问题,建议分叉共享套接字,但是在这种情况下是不可能的.

Is it possible in c++ (ideally through Qt library) to share / monitor a socket already in use by an unrelated process? I found several StackOverflow questions which suggest forking to share the socket, but that's not possible in this case.

推荐答案

可以将文件描述符传输到另一个进程,其行为类似于跨进程dup(2).参见可以打开套接字吗?并将其传递给Linux中的另一个进程以获取详细信息.但这需要明确完成,即一个进程发送文件描述符,而另一个进程接收它.因此,无关"的过程必须配合.

It is possible to transfer a file descriptor to another process, which behaves like a cross process dup(2). See Can I open a socket and pass it to another process in Linux for details. But this needs to be explicitly done, i.e. one process sends the file descriptor and another receives it. Thus the "unrelated" process must cooperate.

但是不能将监听套接字用于监视.套接字只能接受连接,但是无论套接字是通过fork,线程共享还是通过文件描述符传递来共享,都无法看到另一个进程是否在同一套接字上接受了连接.

But a listen socket cannot be used for monitoring. The socket can only accept a connection but it is not possible to see if another process accepted a connection on the same socket, no matter if the sockets are shared with fork,threading or by file descriptor passing.

给出正确的权限和操作系统,您可以使用ptrace(2)或类似接口在syscall级别监视应用程序的行为.在那里,您可以看到应用程序是否使用accept以及它返回什么.或像注释中所建议的那样,您可以使用数据包捕获(tcpdump,原始套接字)来监视流量,并从成功的TCP握手中扣除某些(未知)进程必须已接受连接的信息.

Given the right permissions and OS you can monitor the behavior of an application at the syscall level using the ptrace(2) or similar interface. There you could see if the application uses accept and what it returns. Or like suggested in a comment you can use packet capturing (tcpdump, raw sockets) to simply watch the traffic and deduct from a successful TCP handshake that some (unknown) process must have accepted the connection.

这篇关于在不相关的进程之间共享套接字(监听)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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