从套接字fd获取IP地址,端口和连接类型 [英] Getting IP address, port and connection type from a socket fd

查看:470
本文介绍了从套接字fd获取IP地址,端口和连接类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个跟踪网络应用程序的跟踪程序.

I'm have a tracer process that traces a network application.

给出一个属于该被跟踪对象的套接字文件描述符,该跟踪器是否有可能找到它所对应的IP(在服务器的情况下,它所绑定的接口的IP,在客户端的情况下,用于建立连接的接口的地址),端口号和连接类型?是否有可用于完成此工作的libc API? /proc中可以使用的任何信息吗?

Given a socket file descriptor belonging to the tracee, would it be possible for the tracer to find the IP it corresponds to(in the case of a server the IP of the interface it binds to, in the case of a client the address of the interface used to make the connection), the port number and the type of connection? Are there libc APIs that could be used to make this work? Any information in /proc that could be used?

还有,是否有一种方法可以将与套接字相对应的文件描述符与与打开的文件或设备相对应的文件描述符区分开?

Also, would there be a way to distinguish a file descriptor that corresponds to a socket from one that corresponds to an open file or device?

推荐答案

对于那些使用ifreq结构的标志,您可以对其使用ioctl()系统调用.

You can use ioctl() syscall for those by invoking respective flags use ifreq structure for it.

这篇关于从套接字fd获取IP地址,端口和连接类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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