如何知道TCP连接是否在同一台机器上的两个进程之间?

发布于 2024-12-24 19:34:30 字数 132 浏览 0 评论 0原文

使用套接字编程 API(例如,socket()、connect()、accept() ...),我如何知道 TCP 连接是否位于同一台机器上的两个进程之间?比如说,我有套接字文件描述符和远程 IP。我可以简单地检查远程IP是否为127.0.0.1吗?

Using socket programming APIs (e.g., socket(), connect(), accept() ...), how can I know if a TCP connection is between two processes on the same machine? Say, I have the socket file descriptor, and the remote ip. Can I simply inspect if the remote ip is 127.0.0.1?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酷遇一生 2024-12-31 19:34:30

没有真正可靠的方法来确定这一点 - 您可以使用全局路由的 IP 地址连接到本地进程(即本地进程可以使用 127.0.0.1 之外的 IP)。如果您处于虚拟化环境中,进程也可以在同一物理硬件上的不同虚拟机中运行。

但请注意,如果远程 IP(通过 getpeername)或本地 IP(通过 getsockname)以 127 开头(包括 127.0 .0.1),那么它确实是本地连接;但是,如果是不同的地址对,则不能排除它可能是本地连接的可能性。

There's no really reliable way to determine this - you can connect to local processes using a globally routed IP address (ie, local processes can use IPs other than 127.0.0.1). It's also possible for a process to run in a different virtual machine on the same physical hardware, if you're in a virtualized environment.

Note, however, that if the remote IP (via getpeername) or local IP (via getsockname) starts with 127 (including 127.0.0.1), then it is indeed a local connection; however, you can't rule out the possibility that it might be a local connection if it's a different pair of addresses.

初懵 2024-12-31 19:34:30

使用 getsockname()getpeername() 检索与连接关联的两个 IP,然后使用 gethostname()gethostbyname( )(或其他特定于平台的 API,例如 Windows 上的 GetAdaptersInfo()GetAdapterAddresses())来确定属于本地计算机的 IP,然后您可以将连接 IP 与本地计算机 IP 进行比较,看看它们是否都匹配。一台机器可以分配多个IP,同一台机器上的多个IP可以互相通信。

Use getsockname() and getpeername() to retreive the two IPs associated with the connection, then use gethostname() and gethostbyname() (or other platform-specific APIs, like GetAdaptersInfo() and GetAdapterAddresses() on Windows) to determine the IPs that belong to the local machine, then you can compare the connection IPs to the local machine IPs to see if they both match. A machine can have multiple IPs assigned to it, and multiple IPs on the same machine can communicate with each other.

ゞ花落谁相伴 2024-12-31 19:34:30

这是我使用的方法。这个想法是尝试将侦听器绑定到该 IP 地址,并使用失败/成功代码来确定该地址是否是本地地址。

我并不是说这特别有效,但它应该相当可靠,并且对于我的应用程序来说它是合适的。

#include <sys/socket.h>
#include <errno.h>
/* ...probably need some other headers I am forgetting... */

int
is_local(const struct sockaddr *addr, socklen_t addr_len)
{
    const char *func = "is_local()";
    int result = 0;

    int tmp = socket(addr->sa_family, SOCK_STREAM, 0);
    if (tmp < 0) {
        printf("%s: socket(%d,SOCK_STREAM,0) failed, errno %d\n",
               func, addr->sa_family);

        goto out;
    }

    /* If bind() succeeds, or if it fails because the address is in
       use, then the address must be local to this system. */
    if (bind(tmp, addr, addr_len) < 0) {
        if (errno == EADDRINUSE)
            result = 1;
        else if (errno == EADDRNOTAVAIL)
            ; /* do nothing; address is remote */
        else
            printf("%s: bind() unexpected error %d\n", func, errno);
    }
    else {
        result = 1;
    }

    close(tmp);

 out:
    return result;
}

你这样称呼它:

struct sockaddr_storage client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int fd = accept(listener, &client_addr, &client_addr_len);

if (is_local((struct sockaddr *)&client_addr, client_addr_len))
    /* peer is local */

Here is the approach I have used. The idea is to attempt to bind a listener to that IP address and use the failure/success codes to decide whether the address is local.

I am not claiming this is particularly efficient, but it should be fairly reliable, and for my application it was appropriate.

#include <sys/socket.h>
#include <errno.h>
/* ...probably need some other headers I am forgetting... */

int
is_local(const struct sockaddr *addr, socklen_t addr_len)
{
    const char *func = "is_local()";
    int result = 0;

    int tmp = socket(addr->sa_family, SOCK_STREAM, 0);
    if (tmp < 0) {
        printf("%s: socket(%d,SOCK_STREAM,0) failed, errno %d\n",
               func, addr->sa_family);

        goto out;
    }

    /* If bind() succeeds, or if it fails because the address is in
       use, then the address must be local to this system. */
    if (bind(tmp, addr, addr_len) < 0) {
        if (errno == EADDRINUSE)
            result = 1;
        else if (errno == EADDRNOTAVAIL)
            ; /* do nothing; address is remote */
        else
            printf("%s: bind() unexpected error %d\n", func, errno);
    }
    else {
        result = 1;
    }

    close(tmp);

 out:
    return result;
}

You call it like this:

struct sockaddr_storage client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int fd = accept(listener, &client_addr, &client_addr_len);

if (is_local((struct sockaddr *)&client_addr, client_addr_len))
    /* peer is local */

如果你已经有了远程IP地址,你可以检查它是否是环回地址或者是否是主机的IP地址,因为,正如cnicutar指出的那样,它不必通过环回地址来成为一个本地连接。

If you already have the remote ip address, you can check if it is the loopback address or if it is the ip address of the host, because, as cnicutar points out, it doesn't have to be over the loopback address to be a local connection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文