ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

UNIX编程 GetAddrInfo笔记

2019-12-06 16:00:56  阅读:316  来源: 互联网

标签:GetAddrInfo addr service 编程 host UNIX flags family hints


实现参考:

  • 《UNIX环境高级编程》16.3.3 地址查询
  • 《UNIX系统编程手册 下》59.10.1
  • man getaddrinfo

addr:

#include <cstdio>
#include <cstring>

#include <iostream>
#include <string>

#include <sys/socket.h>
#include <netdb.h>

using namespace std;

string getnameinfo_str(const sockaddr *addr, socklen_t addrlen, int flags) {
    char host[NI_MAXHOST];
    char service[NI_MAXSERV];
    int ret = getnameinfo(addr, addrlen, host, sizeof(host), service, sizeof(service), flags);
    if (ret != 0) {
        fprintf(stderr, "getnameinfo failed, ret: %d, str: %s\n", ret, gai_strerror(ret));
    }
    return host + string(":") + service;
}

void work(const string &host, const string &service, int hints_flags, int getaddrinfo_flags) {
    printf("host: %s, service: %s, hints_flags: %d, getaddrinfo_flags: %d\n", host.c_str(), service.c_str(), hints_flags, getaddrinfo_flags);
    addrinfo hints;
    addrinfo *result;
    memset(&hints, 0, sizeof(hints));

    hints.ai_socktype = SOCK_STREAM;
    hints.ai_family = AF_UNSPEC;
    hints.ai_flags = hints_flags;

    int ret = getaddrinfo(host == "-" ? NULL : host.c_str(), service == "-" ? NULL : service.c_str(), &hints, &result);
    if (ret != 0) {
        fprintf(stderr, "getaddrinfo, host: %s, service: %s, ret: %d, str: %s\n", host.c_str(), service.c_str(), ret, gai_strerror(ret));
        return;
    }
    for (addrinfo *p = result; p != NULL; p = p->ai_next) {
        printf("addr: %s, flags: %d, family: %d, socktype: %d, procotol: %d\n",
               getnameinfo_str(p->ai_addr, p->ai_addrlen, getaddrinfo_flags).c_str(), p->ai_flags, p->ai_family, p->ai_socktype, p->ai_protocol);
        if (p == result && (hints_flags & AI_CANONNAME)) {
            printf("canonname: %s\n", p->ai_canonname);
        }
    }

    freeaddrinfo(result);

    puts("-----------");
}

int main(int argc, char **argv) {
    string host, service;
    int hints_flags, getaddrinfo_flags;
    while (cin >> host >> service >> hints_flags >> getaddrinfo_flags) {
        work(host, service, hints_flags, getaddrinfo_flags);
    }

    work("-", "https", AI_PASSIVE, 0);
    work("-", "https", 0, 0);
    work("-", "https", AI_PASSIVE | AI_CANONNAME, 0);

    work("127.0.0.1", "https", AI_PASSIVE, 0);
    work("127.0.0.1", "https", AI_PASSIVE | AI_CANONNAME, 0);
    work("127.0.0.1", "https", AI_PASSIVE | AI_CANONNAME, NI_NUMERICHOST | NI_NUMERICSERV);

    work("localhost", "http", AI_CANONNAME, 0);
    work("localhost", "http", AI_CANONNAME, NI_NUMERICHOST | NI_NUMERICSERV);

    work("baidu.com", "http", AI_CANONNAME, 0);
    work("baidu.com", "http", AI_CANONNAME, NI_NUMERICHOST | NI_NUMERICSERV);

    work("google.com", "http", AI_CANONNAME, 0);
    work("google.com", "http", AI_CANONNAME, NI_NUMERICHOST | NI_NUMERICSERV);

    return 0;
}
$ ./addr
host: -, service: https, hints_flags: 1, getaddrinfo_flags: 0
addr: :::https, flags: 0, family: 30, socktype: 1, procotol: 6
addr: 0.0.0.0:https, flags: 0, family: 2, socktype: 1, procotol: 6
-----------
host: -, service: https, hints_flags: 0, getaddrinfo_flags: 0
addr: localhost:https, flags: 0, family: 30, socktype: 1, procotol: 6
addr: localhost:https, flags: 0, family: 2, socktype: 1, procotol: 6
-----------
host: -, service: https, hints_flags: 3, getaddrinfo_flags: 0
addr: :::https, flags: 0, family: 30, socktype: 1, procotol: 6
canonname: localhost
addr: 0.0.0.0:https, flags: 0, family: 2, socktype: 1, procotol: 6
-----------
host: 127.0.0.1, service: https, hints_flags: 1, getaddrinfo_flags: 0
addr: localhost:https, flags: 0, family: 2, socktype: 1, procotol: 6
-----------
host: 127.0.0.1, service: https, hints_flags: 3, getaddrinfo_flags: 0
addr: localhost:https, flags: 0, family: 2, socktype: 1, procotol: 6
canonname: (null)
-----------
host: 127.0.0.1, service: https, hints_flags: 3, getaddrinfo_flags: 10
addr: 127.0.0.1:443, flags: 0, family: 2, socktype: 1, procotol: 6
canonname: (null)
-----------
host: localhost, service: http, hints_flags: 2, getaddrinfo_flags: 0
addr: localhost:http, flags: 0, family: 30, socktype: 1, procotol: 6
canonname: localhost
addr: localhost:http, flags: 0, family: 2, socktype: 1, procotol: 6
-----------
host: localhost, service: http, hints_flags: 2, getaddrinfo_flags: 10
addr: ::1:80, flags: 0, family: 30, socktype: 1, procotol: 6
canonname: localhost
addr: 127.0.0.1:80, flags: 0, family: 2, socktype: 1, procotol: 6
-----------
host: baidu.com, service: http, hints_flags: 2, getaddrinfo_flags: 0
addr: 220.181.38.148:http, flags: 0, family: 2, socktype: 1, procotol: 6
canonname: baidu.com
addr: 39.156.69.79:http, flags: 0, family: 2, socktype: 1, procotol: 6
-----------
host: baidu.com, service: http, hints_flags: 2, getaddrinfo_flags: 10
addr: 220.181.38.148:80, flags: 0, family: 2, socktype: 1, procotol: 6
canonname: baidu.com
addr: 39.156.69.79:80, flags: 0, family: 2, socktype: 1, procotol: 6
-----------
host: google.com, service: http, hints_flags: 2, getaddrinfo_flags: 0
addr: hkg07s22-in-f110.1e100.net:http, flags: 0, family: 2, socktype: 1, procotol: 6
canonname: google.com
addr: hkg07s22-in-x0e.1e100.net:http, flags: 0, family: 30, socktype: 1, procotol: 6
-----------
host: google.com, service: http, hints_flags: 2, getaddrinfo_flags: 10
addr: 216.58.199.110:80, flags: 0, family: 2, socktype: 1, procotol: 6
canonname: google.com
addr: 2404:6800:4005:803::200e:80, flags: 0, family: 30, socktype: 1, procotol: 6
-----------

标签:GetAddrInfo,addr,service,编程,host,UNIX,flags,family,hints
来源: https://www.cnblogs.com/ToRapture/p/11995788.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有