当前位置: 首页 > news >正文

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

http://www.lryc.cn/news/185281.html

相关文章:

  • Cesium展示——wkt 数据绘制
  • 打造完美家庭空间,让生活更加舒适
  • 解决loadDep:omelette: sill install loadAllDepsIntoIdealTree
  • 【深蓝学院】手写VIO第2章--IMU传感器--作业
  • Android多线程学习:线程
  • canvas 入门
  • 建议收藏!混迹职场多年总结出的8大技巧!
  • OpenCV4(C++)—— 视频和摄像头的加载、显示与保存
  • excel功能区(ribbonx)编程笔记6-box的使用
  • oralce配置访问白名单的方法
  • ToBeWritten之让响应团队参与并做好沟通
  • ffmpeg ts 关于av_seek_frame
  • 【C++】set map 的底层封装
  • JavaWeb整体介绍
  • 一些常见分布-正态分布、对数正态分布、伽马分布、卡方分布、t分布、F分布等
  • 科技云报道:押注向量数据库,为时过早?
  • 铭控传感亮相2023国际物联网展,聚焦“多场景物联感知方案”应用
  • 前端demo: 实现对图片进行上传前的压缩功能
  • 计算机网络(文章链接汇总)
  • 黑科技-Android
  • 450. 删除二叉搜索树中的节点
  • python安全工具开发基础
  • 26 docker前后端部署
  • [linux] SFTP文件传输基本命令 --- xshell 直接上传文件
  • Tomcat 多实例
  • 全民拼购模式:电商的新趋势和机遇
  • 免费使用,媲美Midjourney!微软在Bing Chat等提供—DALL-E 3
  • Nacos中AP和CP 切换
  • 服务器中勒索病毒怎么解决?勒索病毒解密,数据恢复
  • 全面解析UDP协议(特点、报文格式、UDP和TCP的区别)