structhostent { char *h_name; /* official name of host */ char **h_aliases; /* alias list */ int h_addrtype; /* host address type */ int h_length; /* length of address */ char **h_addr_list; /* list of addresses from name server */ #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE) #define h_addr h_addr_list[0] /* address, for backward compatibility */ #endif/* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */ };
int res_query __P((constchar*, int, int, u_char*, int)); 函数原型为: int res_query(constchar*dname, intclass, inttype, unsignedchar*answer, intanslen)
这个方式需要在项目中添加 libresolv.tbd 库,因为要依赖于库中的函数去解析。 res_query 用来发出一个指定类 (由参数 class 指定) 和类型 (由参数 type 指定) 的 DNS 询问. dname 是要查询的主机名。返回信息被存储在 answser 指向的内存区域中。信息的长度不能大于 anslen 个字节。这个函数会创建一个 DNS 查询报文并把它发送到指定的 DNS 服务器。
/* * CFHostStartInfoResolution() * * Discussion: * Performs a lookup for the given host. It will search for the * requested information if there is no other active request. * Previously cached information of the given type will be released. * * Mac OS X threading: * Thread safe * * Parameters: * * theHost: //需要被解决的CFHostRef的对象 * The CFHostRef which should be resolved. Must be non-NULL. If * this reference is not a valid CFHostRef, the behavior is * undefined. * * info: 返回值的类型 数组/Data/string.. * The enum representing the type of information to be retrieved. * If the value is not a valid type, the behavior is undefined. * * error: 错误 * A reference to a CFStreamError structure which will be filled * with any error information should an error occur. May be set * to NULL if error information is not wanted. * * Result: 解析结果成功还是失败 * Returns TRUE on success and FALSE on failure. In asynchronous * mode, this function will return immediately. In synchronous * mode, it will block until the resolve has completed or until the * resolve is cancelled. * */ CFN_EXPORT __nullable CFArrayRef CFHostGetAddressing(CFHostRef theHost, Boolean * __nullable hasBeenResolved) CF_AVAILABLE(10_3, 2_0);