使用 acl 异步库及ICMP协议库编写了一个同时PING多个目标IP的程序

      在 acl 的软件包中,lib_acl 是一个基础的库,另外,还有另外一个库 lib_protocol,这个库中不仅包含了 HTTP 协议的实现,而且还有一个 ICMP PING 协议的实现。前些日子,看到开源中国的微博中提到 fping 的新版本发布了,这是一个可以在一个线程里同时 PING 多个 IP 的程序,心中不尽暗痒,想到自己曾经还专门实现了一个类似的库,并且通用性可能更强,何不写出来给大家分享一下?因此,本篇主要是先以一个简单的小例子入手,演示如何使用 acl 中的这个 ICMP 协议包实现一个可以同时支持 WIN32 和 LINUX 的多目标 PING 程序。

   部分 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);

 

      下面一个简单的小例子:

 

 

#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 下有该例子的完整实现,编译后,运行下面命令:

      ./ping -n 10 www.baidu.com www.sina.com.cn www.hexun.com www.qq.com

      得到如下的输出结果

 

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/zsxxsz

    acl 库的下载地址:http://http://sourceforge.net/projects/acl/?source=directory

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值