Unix Network Programming Episode 79

这篇具有很好参考价值的文章主要介绍了Unix Network Programming Episode 79。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

‘gai_strerror’ Function

The nonzero error return values from getaddrinfo have the names and meanings shown in Figure 11.7. The function gai_strerror takes one of these values as an argument and returns a pointer to the corresponding error string.

#include <netdb.h>
const char *gai_strerror (int error);
‘freeaddrinfo’ Function

All the storage returned by getaddrinfo, the addrinfo structures, the ai_addr structures, and the ai_canonname string are obtained dynamically (e.g., from malloc). This storage is returned by calling freeaddrinfo.

#include <netdb.h>
void freeaddrinfo (struct addrinfo *ai);

ai should point to the first addrinfo structure returned by getaddrinfo. All the structures in the linked list are freed, along with any dynamic storage pointed to by those structures (e.g., socket address structures and canonical hostnames).

‘getaddrinfo’ Function: IPv6

The POSIX specification defines the getaddrinfo function and the information it returns for both IPv4 and IPv6.

  • getaddrinfo is dealing with two different inputs: the type of socket address structure the caller wants back and the type of records that should be searched for in the DNS or other database.
  • The address family in the hints structure provided by the caller specifies the type of socket address structure that the caller expects to be returned. If the caller specifies AF_INET, the function must not return any sockaddr_in6 structures; if the caller specifies AF_INET6, the function must not return any sockaddr_in structures.
  • POSIX says that specifying AF_UNSPEC will return addresses that can be used with any protocol family that can be used with the hostname and service name. This implies that if a host has both AAAA records and A records, the AAAA records are returned as sockaddr_in6 structures and the A records are returned as sockaddr_in structures. It makes no sense to also return the A records as IPv4-mapped IPv6 addresses in sockaddr_in6 structures because no additional information is being returned: These addresses are already being returned in sockaddr_in structures.
  • This statement in the POSIX specification also implies that if the AI_PASSIVE flag is specified without a hostname, then the IPv6 wildcard address (IN6ADDR_ANY_INIT or 0::0) should be returned as a sockaddr_in6 structure, along with the IPv4 wildcard address (INADDR_ANY or 0.0.0.0), which is returned as a sockaddr_in structure. It also makes sense to return the IPv6 wildcard address first because we will see in Section 12.2(See 9.1.2) that an IPv6 server socket can handle both IPv6 and IPv4 clients on a dual-stack host.
  • The address family specified in the hint structure’s ai_family member, along with the flags such as AI_V4MAPPED and AI_ALL specified in the ai_flags member, dictate the type of records that are searched for in the DNS (A and/or AAAA) and what type of addresses are returned (IPv4, IPv6, and/or IPv4-mapped IPv6). We summarize this in Figure 11.8.
  • The hostname can also be either an IPv6 hex string or an IPv4 dotted-decimal string. The validity of this string depends on the address family specified by the caller. An IPv6 hex string is not acceptable if AF_INET is specified, and an IPv4 dotted-decimal string is not acceptable if AF_INET6 is specified. But, if AF_UNSPEC is specified, either is acceptable and the appropriate type of socket address structure is returned.
‘getaddrinfo’ Function: Examples

We will now show some examples of getaddrinfo using a test program that lets us enter all the parameters: the hostname, service name, address family, socket type, and AI_CANONNAME and AI_PASSIVE flags. (We do not show this test program, as it is about 350 lines of uninteresting code. It is provided with the source code for the book, as described in the Preface.) The test program outputs information on the variable number of addrinfo structures that are returned, showing the arguments for a call to socket and the address in each socket address structure.

‘host_serv’ Function

Our first interface to getaddrinfo does not require the caller to allocate a hints structure and fill it in. Instead, the two fields of interest, the address family and the socket type, are arguments to our host_serv function.

#include "unp.h"
struct addrinfo *host_serv (const char *hostname, const char *service, int family, ints socktype);
#include "unp.h"

struct addrinfo* host_serv(const char *host, const char *serv, int family, int socktype)
{
    int n;
    struct addrinfo hints, *res;
    bzero(&hints, sizeof(struct addrinfo));
    hints.ai_flags=AI_CANONNAME;
    hints.ai_family=family;
    hints.ai_socktype=socktype;

    if((n=getaddrinfo(host, serv, &hints, &res))!=0)
    {
        return NULL;
    }
    return res;
}

host_serv function文章来源地址https://www.toymoban.com/news/detail-727759.html

到了这里,关于Unix Network Programming Episode 79的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Unix Network Programming Episode 76

    We encourage the use of getaddrinfo (Section 11.6(See 8.9.6)) in new programs. The non-null pointer returned by this function points to the following hostent structure: gethostbyname differs from the other socket functions that we have described in that it does not set errno when an error occurs. Instead, it sets the global integer h_errno to one of the foll

    2024年02月09日
    浏览(23)
  • unix网络编程-简易服务器与客户端程序解析

    a -- address f -- file        eg: fputs() -- file put stream fd -- file descriptor h - host(主机) in/inet -- internet        eg: sockaddr_in; inet_aton n -- network(网络字节序)/numeric(数值) p -- protocol(协议)/presentation(表达/呈现形式) s -- socket        eg: sin -- socket internet t -- type,用于指定某种

    2024年01月16日
    浏览(54)
  • UNIX网络编程卷一 学习笔记 第三十章 客户/服务器程序设计范式

    开发一个Unix服务器程序时,我们本书做过的进程控制: 1.迭代服务器(iterative server),它的适用情形极为有限,因为这样的服务器在完成对当前客户的服务前无法处理已等待服务的新客户。 2.并发服务器(concurrent server),为每个客户调用fork派生一个子进程。传统上大多U

    2024年02月09日
    浏览(38)
  • 【开源与项目实战:开源实战】79 | 开源实战二(中):从Unix开源开发学习应对大型复杂项目开发

    我们知道,项目越复杂、代码量越多、参与开发人员越多、开发维护时间越长,我们就越是要重视代码质量。代码质量下降会导致项目研发困难重重,比如:开发效率低,招了很多人,天天加班,出活却不多;线上 bug 频发,查找 bug 困难,领导发飙,中层束手无策,工程师抱

    2024年02月11日
    浏览(34)
  • 【Socket】Unix环境下搭建简易本地时间获取服务

    本文搭建一个Unix环境下的、局域网内的、简易的本地时间获取服务。 主要用于验证: 当TCP连接成功后,可以在两个线程中分别进行读操作、写操作动作 当客户端自行终止连接后,服务端会在写操作时收到 SIGPIPE 信号 当客户端执行shutdown写操作后,客户端会在写操作时收到

    2024年02月04日
    浏览(28)
  • MobaXterm连接服务器:Network error: Connection refused

    centos7: ubuntu20.04

    2024年02月16日
    浏览(45)
  • 【Hello Network】DNS协议 NAT技术 代理服务器

    本篇博客简介:介绍DNS协议 NAT技术和代理服务器 DNS是一整套从域名映射到IP的系统 为什么要有域名 其实作为我们程序员来说 使用域名还是IP地址是无所谓的 但是站在商业公司和用户的角度就不这么认为了 商业公司希望用户能够快速的记住自己公司的网址 而用户也希望自己

    2024年02月11日
    浏览(30)
  • MobaXterm监控服务器的资源(CPU、RAM、Network、disk...) 使用情况

    使用服务器的时候比较喜欢随时查看的服务器资源使用情况,比如内存,CPU,网速,磁盘使用等情况,一次偶然的机会发现了MobaXterm提供有这项功能,在会话窗口底部: 完整窗口示意图 如果你发现你的会话窗口底部没有,可以这样开启: Settings→SSH→勾选Remote-monitoring 参考

    2024年02月14日
    浏览(32)
  • mobaxterm无法连接vmware虚拟机服务器,network error:connection refused

    场景描述: 电脑硬盘换了,重新安装vmware,ubuntu,mobaxterm..... 安装完ubuntu后,因为习惯了无UI的界面,所以关闭了ubuntu的桌面服务 (有需要的同学可以通过sudo systemctl set-default multi-user.target,然后sudo reboot就可以关闭桌面服务了,打开命令是sudo 6systemctl set-default graphical.targe

    2024年02月14日
    浏览(34)
  • /etc/netplan/network-manager-all.yaml 配置服务器ip

    本文为博主原创,转载请注明出处: /etc/netplan 是用于配置 Ubuntu 系统网络接口的目录。在 Ubuntu 中,网络配置的默认工具为   Netplan ,而 /etc/netplan 则是 Netplan 配置文件的存储位置。 在 /etc/netplan 目录中,通常会有一个或多个 YAML 格式的文件,用来定义系统中的网络接口、

    2024年02月07日
    浏览(23)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包