gethostbyname timeout

本文探讨了如何在程序中为gethostbyname函数设置超时机制,详细介绍了使用alarm函数不可行的原因,并提供了绕过非线程安全系统函数的方法。通过采用子进程、线程与gethostbyname_r或getaddrinfo函数结合的方式,实现超时控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

总结: 下面第一种方法经过测试是可行的,第二种方法(多线程中使用)不能够用信号中断。

so , 对信号的使用要非常小心。



gethostbyname 是阻塞的, 非线程安全的,所以最好使用getaddrinfo , 它是线程安全的。

要给gethostbyname 设置一个超时值, 用信号(alarm)实现是不行的(目前项目嵌入式平台上面测试不行) 。


下面是网上摘录的解决该问题的方法, 对怎么绕过其他系统函数(无论是线程安全还是非安全的)也有很好的借鉴意义


Hello,

I need to add timeout to gethostbyname function in my program. I
googled and searched archives, found some examples with alert(), but
can't make it work :/ I would be grateful of someone could tell me how
to do it.

You can't. Or rather, you shouldn't.

The trick with alarm(2) (not alert()), is to longjmp(3) from the signal
handler to a context created with setjmp(3) before calling gethostbyname(3).
This is, unfortunately, very common in Perl code (in Perl die() actually
uses longjmp() internally to a context set from an eval statement).

However, gethostbyname() keeps internal state, and afterward its unsafe
to call the function again. gethostbyname_r() might be relatively safer,
but you've probably also leaked a file descriptor and memory,
meaning you could only do it so many times from a single process before it
won't work anymore. (And from a strict C perspective, jumping from the
signal handler itself is questionable.)

You do have options.

1) Multiple processes. Use child processes and the
gethostbyname()+alarm() trick, and return the answer down a pipe to
the parent. The child will just kill itself if it timeouts (or
alternatively the parent will set the alarm and kill the child), since it
cannot reliably do it's job afterward.

2) Use threads in conjunction with gethostbyname_r(), or preferably
getaddrinfo(3). Still, you cannot interrupt these functions within the
thread, and so you have a few more decisions to make in terms of how you
handle timeouts.

3) Use a third-party asynchronous DNS library: ADNS, C-Ares and UDNS are
the first ones which comes to mind, the former two probably being the most
popular.
import socket import os import struct import select import time # ICMP协议号 ICMP_PROTO = socket.getprotobyname('icmp') # 创建原始套接字 try: sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, ICMP_PROTO) except PermissionError: print("错误:需要管理员权限运行此程序。") exit(1) # 设置超时 sock.settimeout(1) def checksum(source_string): sum = 0 countTo = (len(source_string) // 2) * 2 count = 0 while count < countTo: thisVal = source_string[count + 1] * 256 + source_string[count] sum = sum + thisVal sum = sum & 0xffffffff count = count + 2 if countTo < len(source_string): sum = sum + source_string[len(source_string) - 1] sum = sum & 0xffffffff sum = (sum >> 16) + (sum & 0xffff) sum = sum + (sum >> 16) answer = ~sum answer = answer & 0xffff answer = answer >> 8 | (answer << 8 & 0xff00) return answer def create_packet(id): # ICMP头:类型(8), 代码(0), 校验和(0), ID, 序列号(1) header = struct.pack('bbHHh', 8, 0, 0, id, 1) data = b'abcdefghijklmnopqrstuvwabcdefghi' my_checksum = checksum(header + data) header = struct.pack('bbHHh', 8, 0, socket.htons(my_checksum), id, 1) return header + data def send_ping(dest_addr): packet_id = os.getpid() & 0xFFFF packet = create_packet(packet_id) sock.sendto(packet, (dest_addr, 1)) send_time = time.time() return packet_id, send_time def receive_ping(packet_id, timeout): while True: ready = select.select([sock], [], [], timeout) if ready[0] == []: return None recv_time = time.time() recv_packet, addr = sock.recvfrom(1024) icmp_header = recv_packet[20:28] type, code, checksum, p_id, sequence = struct.unpack('bbHHh', icmp_header) if p_id == packet_id: return recv_time timeout = timeout - (time.time() - recv_time) if timeout <= 0: return None 请为我讲解一下这段代码
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值