使用 UDP 套接字通过 connect()ing 获取我自己的 IP 地址? [英] Getting my own IP address by connect()ing using UDP socket?

查看:19
本文介绍了使用 UDP 套接字通过 connect()ing 获取我自己的 IP 地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说我可以通过创建 UDP 套接字 并connect() 到像 Google 这样的有效目标 IP 地址来获得自己的 IP 地址(不是 127.0.0.1).

I've heard that I can get my own IP address(not 127.0.0.1), by creating a UDP socket and connecting() to a valid destination IP address like Google.

但是,我找不到任何参考或示例.

However, I could not find any reference or example for this.

这可能吗?如果是这样,我该怎么做?

Is this possible? If so, how can I do this?

char* get_my_ip() {
    int sockfd;
    struct sockaddr_storage remoteaddr; // client address
    socklen_t addrlen;
    char remoteIP[INET6_ADDRSTRLEN];
    char *ip_addr;
    ip_addr = malloc(sizeof(char) * INET6_ADDRSTRLEN);
    int rv;

    struct addrinfo hints, *ai, *p;

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_DGRAM;
    if ((rv = getaddrinfo("8.8.8.8", "http", &hints, &ai)) != 0) {
        fprintf(stderr, "getaddrinfo: %s
", gai_strerror(rv));
        exit(1);
    }
    // loop through all the results and make a socket
    for(p = ai; p != NULL; p = p->ai_next) {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,
            p->ai_protocol)) == -1) {
            perror("UDP: socket");
            continue;
        }

        if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
            close(sockfd);
            perror("UDP: connect");
            continue;
        }
        break;
    }
    if (p == NULL) {
        fprintf(stderr, "UDP: failed to bind socket
");
        exit(2);
    }

    getsockname(sockfd, (struct sockaddr*)&remoteaddr, &addrlen);

    // deal with both IPv4 and IPv6:
    if (remoteaddr.ss_family == AF_INET) {
        struct sockaddr_in *s = (struct sockaddr_in *)&remoteaddr;
        inet_ntop(AF_INET, &s->sin_addr, remoteIP, addrlen);
    }
    else { // AF_INET6
        struct sockaddr_in6 *s = (struct sockaddr_in6 *)&remoteaddr;
        inet_ntop(AF_INET6, &s->sin6_addr, remoteIP, addrlen);
    }
    printf("IP_ADDRESS:%s", remoteIP);

    freeaddrinfo(ai); // all done with this structure
    close(sockfd);

    strcpy(ip_addr, remoteIP);
    return ip_addr;
}

推荐答案

在你的套接字上调用 connect() 然后 getsockname().它将返回套接字现在绑定的 IP 地址,该地址已被 IP 路由表选择为到目标地址的最佳路由.

Call connect() and then getsockname() on your socket. It will return the IP address the socket is now bound to, which has been chosen by the IP routing tables as the best route to the target address.

但这不一定是您要查找的 IP 地址,除非您只有一个外部 IP 地址.

However that is not necessarily the IP address you are looking for, unless you only have one external IP address.

如果您在调制解调器或路由器的另一端谈论您的公共 IP 地址,则此技术根本不适用.

If you're talking about your public IP address on the other side of a modem or router, this technique has no applicability at all.

这篇关于使用 UDP 套接字通过 connect()ing 获取我自己的 IP 地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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