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

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

问题描述

我听说,我可以让我自己的IP地址(而不是127.0.0.1),通过创建一个 UDP套接字和连接()到有效目标IP地址像谷歌。

不过,我找不到这方面的任何参考或示例。

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


 的char * get_my_ip(){
    INT的sockfd;
    结构sockaddr_storage remoteaddr; //客户端地址
    socklen_t的addrlen中;
    焦炭REMOTEIP [INET6_ADDRSTRLEN]
    字符*的ip_addr;
    ip_addr中的malloc =(sizeof的(char)的* INET6_ADDRSTRLEN);
    INT RV;    结构addrinfo中的提示,* AI,* P;    memset的(安培;提示,0,sizeof的提示);
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_DGRAM;
    如果((RV =的getaddrinfo(8.8.8.8,HTTP,&放大器;提示,&放大器;!AI))= 0){
        fprintf中(标准错误的getaddrinfo:%S \\ n,gai_strerror(RV));
        出口(1);
    }
    //通过所有的结果循环,使插座
    为(P =爱; P!= NULL; P = P-> ai_next){
        如果((的sockfd =插座(对GT; ai_family,P-> ai_socktype,
            P-> ai_protocol))== -1){
            PERROR(UDP:插座);
            继续;
        }        如果(连接(的sockfd,P-> ai_addr,P-> ai_addrlen)== -1){
            接近(的sockfd);
            PERROR(UDP:连接);
            继续;
        }
        打破;
    }
    如果(P == NULL){
        fprintf中(标准错误,UDP:无法绑定套接字\\ n);
        出口(2);
    }    getsockname(的sockfd,(结构sockaddr *)及remoteaddr,&安培; addrlen中);    //处理IPv4和IPv6:
    如果(remoteaddr.ss_family == AF_INET){
        结构SOCKADDR_IN * S =(结构SOCKADDR_IN *)及remoteaddr;
        inet_ntop(AF_INET,&安培; S-> sin_addr,属于REMOTEIP,addrlen中);
    }
    其他{// AF_INET6
        结构* sockaddr_in6的S =(结构* sockaddr_in6的)及remoteaddr;
        inet_ntop(AF_INET6,&安培; S-> sin6_addr,REMOTEIP,addrlen中);
    }
    的printf(IP地址:%S,REMOTEIP);    freeaddrinfo(AI); //所有这种结构完成
    接近(的sockfd);    的strcpy(ip_addr中,REMOTEIP);
    返回的ip_addr;
}


解决方案

呼叫连接()然后 getsockname()您的插座上。它会返回的IP地址的套接字被绑定到,这已被选定的IP路由表作为目标地址的最佳路径。

但不一定是你正在寻找的IP地址,除非你只有一个外部IP地址。

如果你在谈论你的公共的一个调制解调器或路由器的另一端的IP地址,这种技术没有实用性可言。

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\n", 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\n");
        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;
}

解决方案

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.

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

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套接字连接()ING得到我自己的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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