资源简介
ICMP[version 4]的echo报文运用,模拟ping命令实现pingy命令

代码片段和文件信息
#include
#include
#include
#include
#include “ping.h“
#define ICMP_ECHOREPLY 0
#define ICMP_ECHOREQ 8
// statistics
int nsend = 0 nrecv = 0;
int rrt[10];
int packsize;
void get_data(int rrt[] int size int *min int *max int *avg);
void tv_sub(struct timeval *outstruct timeval *in);
// calculate checksum of icmp header
uint16_t cal_cksum(uint16_t *addr int len)
{
int nleft = len;
uint32_t sum = 0;
uint16_t *w = addr;
uint16_t answer = 0;
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
if (nleft == 1) {
*(unsigned char *)(&answer) = *(unsigned char *)w ;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
answer = ~sum;
return(answer);
}
void print_head(char *ip)
{
packsize = sizeof(struct icmp) + sizeof(struct timeval);
printf(“Ping %s with %d bytes of data:\n“ ip packsize);
}
void print_stat(char *ip)
{
int min_rrt max_rrt avg_rrt;
int lost;
if(nsend < 1)
return;
get_data(rrt nsend &min_rrt &max_rrt &avg_rrt);
lost = nsend - nrecv;
printf(“========================================================================\n“);
printf(“Ping Statistics for %s:\n“ ip);
printf(“\tPackets: Send = %d Received = %d Lost = %d(%.1f%% lost)\n“ nsend nrecv lost (lost / (nsend * 1.0) * 100.0));
printf(“Approximate round trip times in milli-seconds:\n“);
printf(“\tMinimum = %dms Maximum = %dms Average = %dms\n“ min_rrt max_rrt avg_rrt);
}
void send_ping(int sockfd struct sockaddr_in *dstaddr)
{
char buf[100];
size_t len = sizeof(struct icmp);
socklen_t dstlen = sizeof(struct sockaddr_in);
struct icmp *echo;
memset(buf 0 sizeof(buf));
echo = (struct icmp*)buf;
echo = (struct icmp *)buf;
echo->icmp_type = ICMP_ECHOREQ;
echo->icmp_code = 0;
echo->icmp_cksum = 0;
echo->icmp_id = getpid();
echo->icmp_seq = nsend;
struct timeval *tval= (struct timeval *)echo->icmp_data;
gettimeofday(tvalNULL);
echo->icmp_cksum = cal_cksum((uint16_t *)echo packsize);
// send ping message
if(sendto(sockfd buf len 0 (struct sockaddr*)dstaddr dstlen) == -1)
printf(“Send Ping Message Error!\n“);
nsend++;
}
void recv_ping(int sockfd struct sockaddr_in *dstaddr)
{
char buf[100];
ssize_t n;
struct ip *ip;
struct icmp *icmp;
socklen_t dstlen = sizeof(struct sockaddr_in);
int ttl;
fd_set rset;
int maxfd = sockfd + 1;
struct timeval timeo *tvsend tvrecv;
unsigned char *p;
unsigned char ipaddr[100];
int time;
memset(buf 0 sizeof(buf));
timeo.tv_sec = 3;
timeo.tv_usec = 0;
FD_ZERO(&rset);
FD_SET(sockfd &rset);
gettimeofday(&tvrecv NULL);
while(1)
{
// set timeout 3s
if((n = select(maxfd &rset NULL NULL &timeo)) == -1)
{
printf(“Select Error!\n“);
exit(1);
}
if(n <= 0)
{
printf(“Request Time Out!\n“);
fflush(stdout);
break;
}
if((n = recvf
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 184 2010-03-10 11:20 PingY\makefile
文件 5182 2010-03-10 11:04 PingY\ping.c
文件 1348 2010-03-09 11:13 PingY\ping.h
文件 2148 2010-03-10 11:21 PingY\PingPro.c
目录 0 2010-03-29 20:34 PingY
----------- --------- ---------- ----- ----
8862 5
相关资源
- 实验三 消息中间件应用开发:Active
- FPGA实现PID.v
- 计算机图形学 边填充算法实现代码
-
Actionsc
ript 1.0实现能跟随鼠标运动的 - 基于蓝牙4.0的设备通信方案设计与实
- 基于PCIe的FPGA动态配置设计与实现
- SSM+Shiro+redis实现单点登陆
- 使用选择性重传协议实现UDP可靠通信
- 实现小波变换例子 upcoef 函数
- 图像二维小波变换的实现源代码
- 栈的实现及应用,六种基本算法
- 用汇编实现的学生成绩档案管理系统
- pb 实现仿BS界面 dw菜单 powerbuild
- 基于MIPS指令集的32位CPU设计与Verilog语
- python实现的ftp自动上传、下载脚本
- jQuery ajax实现简单登录验证
- 编程实现二维DCT变换
- 用Socket编程实现FTP
- gmsk调制在FPGA上实现
- 用51单片机实现G代码翻译
- NRF24L01实现51与STM32双向通讯
- 大数(链表、数组)实现
- delphi 通过Ping命令检测网络是否正常
- 关联分析Apriori算法实现
- pb 实现ean8,ean13,ean39条形码数据窗口
- websocket实现一对一聊天
- Qt Creator opengl实现四元数鼠标控制轨迹
- 数据库实现学生成绩管理系统选课管
- 8位双向移位寄存器的设计与实现
- vhdl与lcd1602实现的多控制电子钟
评论
共有 条评论