在 acl 的软件包中,lib_acl 是一个基础的库,另外,还有另一个库 lib_protocol,这个库中不只包含了 HTTP 协议的实现,并且还有一个 ICMP PING 协议的实现。前些日子,看到开源中国的微博中提到 fping 的新版本发布了,这是一个能够在一个线程里同时 PING 多个 IP 的程序,心中不尽暗痒,想到本身曾经还专门实现了一个相似的库,而且通用性可能更强,何不写出来给你们分享一下?所以,本篇主要是先以一个简单的小例子入手,演示如何使用 acl 中的这个 ICMP 协议包实现一个能够同时支持 WIN32 和 LINUX 的多目标 PING 程序。dom
部分 API 接口说明:异步
/** * 建立ICMP会话对象 * @param aio {ACL_AIO*} 若是该项不为空,则内部在通讯过程当中采用非阻塞模式, * 不然采用阻塞模式 * @param check_tid {int} 是否在校验响应包时检查数据中的线程号字段 * @return {ICMP_CHAT*} ICMP会话对象句柄 */ ICMP_API ICMP_CHAT *icmp_chat_create(ACL_AIO *aio, int check_tid); /** * 释放ICMP会话对象 * @param chat {ICMP_CHAT*} ICMP会话对象句柄 */ ICMP_API void icmp_chat_free(ICMP_CHAT *chat); /** * 判断当前的ICMP会话对象中全部探测任务是否已经完成 * @param chat {ICMP_CHAT*} 会话对象句柄 * @return {int} != 0: 表示完成; 0: 表示未完成 */ ICMP_API int icmp_chat_finish(ICMP_CHAT *chat); /** * ping 一台主机(内部默认每一个探测包长度为64个字节) * @param chat {ICMP_CHAT*} 会话对象句柄 * @param domain {const char*} 域名标识字符串,能够为空 * @param ip {const char*} 主机IP地址,不能为空 * @param npkt {size_t} 对该主机发送的数据包个数 * @param delay {int} 发送探测数据包的时间间隔(秒) * @param timeout {int} 被探测主机的响应包超时时间(秒) */ ICMP_API void icmp_ping_one(ICMP_CHAT *chat, const char *domain, const char *ip, size_t npkt, int delay, int timeout); /** * 输出当前ICMP的会话状态 * @param chat {ICMP_CHAT*} 会话对象句柄 */ ICMP_API void icmp_stat(ICMP_CHAT *chat); /** * 取得当前ICMP会话对象中的当前会话序列号值 * @param chat {ICMP_CHAT*} 会话对象句柄 * @return {unsigned short} 会话序列号值 */ ICMP_API unsigned short icmp_chat_seqno(ICMP_CHAT *chat);
下面一个简单的小例子:async
#include "lib_acl.h" #include "lib_protocol.h" static void ping_main_async(void) { int delay = 1; /* 发送 PING 数据包的时间间隔(秒) */ int npkt = 10; /* 发送的 PING 数据包个数 */ ACL_AIO *aio; ICMP_CHAT *icmp; /* 建立非阻塞异步通讯句柄 */ aio = acl_aio_create(ACL_EVENT_SELECT); acl_aio_set_keep_read(aio, 0); /* 建立 ICMP 对象 */ icmp = icmp_chat_create(aio, 1); /* PING www.baidu.com 的一个 IP 地址*/ icmp_ping_one(icmp, NULL, 61.135.169.115, npkt, delay, 1); /* PING www.sina.com.cn 的一个 IP 地址 */ icmp_ping_one(icmp, NULL, 202.108.33.60, npkt, delay, 1); /* PING www.hexun.com 的一个 IP 地址 */ icmp_ping_one(icmp, NULL, 202.99.16.169, npkt, delay, 1); /* PING www.qq.com 的一个 IP 地址 */ icmp_ping_one(icmp, NULL, 61.135.167.36, npkt, delay, 1); while (1) { /* 若是 PING 结束,则退出循环 */ if (icmp_chat_finish(icmp)) { printf("over now!, hosts' size=%d, count=%d\r\n", icmp_chat_size(icmp), icmp_chat_count(icmp)); break; } /* 异步事件循环过程 */ acl_aio_loop(aio); } /* 显示 PING 结果 */ icmp_stat(icmp);; /* 释放 ICMP 对象 */ icmp_chat_free(icmp); /* 销毁非阻塞句柄 */ acl_aio_free(aio); }
能够看出,该例子仍是很是简单的,在 acl/samples/ping 下有该例子的完整实现,编译后,运行下面命令:oop
./ping -n 10 www.baidu.com www.sina.com.cn www.hexun.com www.qq.com.net
获得以下的输出结果线程
Reply from 202.108.33.60: bytes=56 time=5.427ms TTL=202 icmp_seq=2 status=0 Reply from 61.135.169.105: bytes=64 time=5.975ms TTL=61 icmp_seq=1 status=0 Reply from 61.135.169.125: bytes=64 time=6.394ms TTL=61 icmp_seq=0 status=0 Reply from 61.135.167.36: bytes=64 time=8.147ms TTL=61 icmp_seq=4 status=0 Reply from 202.99.16.169: bytes=64 time=8.532ms TTL=202 icmp_seq=3 status=0 Reply from 61.135.169.105: bytes=64 time=4.879ms TTL=61 icmp_seq=6 status=0 Reply from 202.108.33.60: bytes=56 time=5.313ms TTL=202 icmp_seq=5 status=0 Reply from 61.135.169.125: bytes=64 time=6.695ms TTL=61 icmp_seq=7 status=0 Reply from 61.135.167.36: bytes=64 time=5.963ms TTL=61 icmp_seq=8 status=0 。。。 Ping statistics for 61.135.169.125: www.baidu.com Packets: Sent = 10, Received = 10, Lost = 0 (0.00% loss), Approximate round trip times in milli-seconds: Minimum = 5.187 ms, Maximum = 7.143 ms, Average = 6.307 ms Ping statistics for 61.135.169.105: www.baidu.com Packets: Sent = 10, Received = 10, Lost = 0 (0.00% loss), Approximate round trip times in milli-seconds: Minimum = 4.123 ms, Maximum = 8.807 ms, Average = 5.556 ms Ping statistics for 202.108.33.60: www.sina.com.cn Packets: Sent = 10, Received = 10, Lost = 0 (0.00% loss), Approximate round trip times in milli-seconds: Minimum = 4.319 ms, Maximum = 8.073 ms, Average = 5.855 ms Ping statistics for 202.99.16.169: www.hexun.com Packets: Sent = 10, Received = 10, Lost = 0 (0.00% loss), Approximate round trip times in milli-seconds: Minimum = 5.376 ms, Maximum = 8.532 ms, Average = 6.453 ms Ping statistics for 61.135.167.36: www.qq.com Packets: Sent = 10, Received = 10, Lost = 0 (0.00% loss), Approximate round trip times in milli-seconds: Minimum = 4.292 ms, Maximum = 8.147 ms, Average = 5.948 ms >>>max pkts: 50
我的微博:http://weibo.com/zsxxszcode
acl 库的下载地址:http://http://sourceforge.net/projects/acl/?source=directory对象