getaddrinfo( ) - getnameinfo( )/ inet_ntop( )



/*   addrinfo_ntop.c by vinco at 2012-05-18
 *   i386-Red Hat-gcc-4.1.2 
 *   http://blog.csdn.net/xuyunzhang/
 *   Copyright: All Reserved
 */

#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

int addrinfo(char* host, char *serv);
int addrinfo_ntop(const struct addrinfo *paddrinfo,
                       char *host, size_t hostlen,
                       char *serv, size_t servlen);
int main()
{
	char host[64]="member.qq.com", serv[64]="8000";
	//char host[64]="183.60.49.253", serv[64]="8000";
	addrinfo( host, serv);
}

int addrinfo(char* host, char *serv)
{
	struct addrinfo hints, *res = NULL, *paddrinfo = NULL;
	char hostname[64]={0}, servname[64]={0};
	int ret = -1;

	if( !host || !serv )
		return -1;
	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_UNSPEC; /* AF_INET6 or AF_INET */
	hints.ai_socktype = SOCK_DGRAM;
	//hints.ai_flags = ;
	
	if( (ret = getaddrinfo( host, serv, &hints, &res)) )
	{
		printf("getaddrinfo error:%s\n", gai_strerror(ret) );
		return -1;
	}
		
	for(paddrinfo = res; paddrinfo; paddrinfo = paddrinfo->ai_next )
	{
		if( addrinfo_ntop(paddrinfo,
					hostname, sizeof(hostname),
					servname, sizeof(servname)) == 0 )
		{
			ret = 0;
			fprintf(stderr, "addrinfo_ntop host/port = %s/%s\t", hostname, servname);
		}
		
		if( getnameinfo(paddrinfo->ai_addr, paddrinfo->ai_addrlen, 
					hostname, sizeof(hostname), 
					servname, sizeof(servname), 
					/*NI_NUMERICHOST | NI_NUMERICSERV*/ 0) == 0 ) 
		{
			ret = 0;
			fprintf(stderr, "getnameinfo host/port = %s/%s\n", hostname, servname);
		}
	}

	putchar('\n');
	
	freeaddrinfo(res);
	return ret;
	
}

int addrinfo_ntop(const struct addrinfo *paddrinfo,
                       char *host, size_t hostlen,
                       char *serv, size_t servlen)
{
	struct sockaddr_in *sain = NULL;
	struct sockaddr_in6 *sain6 = NULL;

	if( !paddrinfo || !host || !serv )
		return -1;
	switch ( paddrinfo->ai_family) 
	{
		case AF_INET: 
		{
			sain = (struct sockaddr_in *)(paddrinfo->ai_addr);
			inet_ntop( paddrinfo->ai_family, &(sain->sin_addr), host, hostlen);
			snprintf( serv, servlen, "%d", ntohs(sain->sin_port) );
			return 0;
		}
		break;

		case AF_INET6: 
		{
			sain6 = (struct sockaddr_in6 *)(paddrinfo->ai_addr);
			inet_ntop( paddrinfo->ai_family, &(sain6->sin6_addr), host, hostlen);
			snprintf( serv, servlen, "%d", ntohs(sain6->sin6_port) );
			return 0;
		};
		break;

		default:
		{
			return -1;
			break;
		}
	}
}

run && stdout


[root@localhost ctest]# 
[root@localhost ctest]# gcc addrinfo_ntop.c -o addrinfo_ntop
[root@localhost ctest]# 
[root@localhost ctest]# 
[root@localhost ctest]# ./addrinfo_ntop
addrinfo_ntop host/port = 58.251.150.157/8000	getnameinfo host/port = reverse.gdsz.cncnet.net/irdmi
addrinfo_ntop host/port = 112.90.143.245/8000	getnameinfo host/port = 112.90.143.245/irdmi

[root@localhost ctest]# 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Traceback (most recent call last): File "D:\02-study\python_scripts\load\venv\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen httplib_response = self._make_request( File "D:\02-study\python_scripts\load\venv\lib\site-packages\urllib3\connectionpool.py", line 398, in _make_request conn.request(method, url, **httplib_request_kw) File "D:\02-study\python_scripts\load\venv\lib\site-packages\urllib3\connection.py", line 239, in request super(HTTPConnection, self).request(method, url, body=body, headers=headers) File "D:\02-study\python\lib\http\client.py", line 1282, in request self._send_request(method, url, body, headers, encode_chunked) File "D:\02-study\python\lib\http\client.py", line 1328, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "D:\02-study\python\lib\http\client.py", line 1277, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "D:\02-study\python\lib\http\client.py", line 1037, in _send_output self.send(msg) File "D:\02-study\python\lib\http\client.py", line 975, in send self.connect() File "D:\02-study\python_scripts\load\venv\lib\site-packages\urllib3\connection.py", line 205, in connect conn = self._new_conn() File "D:\02-study\python_scripts\load\venv\lib\site-packages\urllib3\connection.py", line 186, in _new_conn raise NewConnectionError( urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001CC3180D4B0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\02-study\python_scripts\load\venv\lib\site-packages\requests\adapters.py", line 489, in send resp = conn.urlopen( File "D:\02-study\python_scripts\load\venv\lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen retries = retries.increment( File "D:\02-study\python_scripts\load\venv\lib\site-packages\urllib3\util\retry.py", line 592, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='uem-uat.yun.hihonor.com', port=80): Max retries exceeded with url: /uem-gateway/analytics-metrics/services/user-access-detail/access-list/1/10?t=1679290718262 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001CC3180D4B0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed')) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\02-study\python_scripts\load\script\code_validate.py", line 61, in <module> res = q.query(role_code='China_Area#Country Representative') File "D:\02-study\python_scripts\load\script\code_validate.py", line 54, in query res = self.sess.request('post', url=url_pro, headers=header, json=json) File "D:\02-study\python_scripts\load\venv\lib\site-packages\requests\sessions.py", line 587, in request resp = self.send(prep, **send_kwargs) File "D:\02-study\python_scripts\load\venv\lib\site-packages\requests\sessions.py", line 701, in send r = adapter.send(request, **kwargs) File "D:\02-study\python_scripts\load\venv\lib\site-packages\requests\adapters.py", line 565, in send raise ConnectionError(e, request=request) requests.exceptions.ConnectionError: HTTPConnectionPool(host='uem-uat.yun.hihonor.com', port=80): Max retries exceeded with url: /uem-gateway/analytics-metrics/services/user-access-detail/access-list/1/10?t=1679290718262 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001CC3180D4B0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
最新发布
04-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值