-
大小: 239KB文件类型: .7z金币: 1下载: 0 次发布日期: 2021-06-04
- 语言: 其他
- 标签: ping traceroute
资源简介
参考别人的源码。略有改动。ping 可以ping 域名。traceroute 不会卡在某个路径死掉。
代码片段和文件信息
#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);
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 = recvfrom(sockfd buf
- 上一篇:各种混沌序列的产生
- 下一篇:数字电路篮球竞赛30秒计时器
相关资源
- 基于backstepping的船舶航向自适应鲁棒
- closed_loop_control_system_for_stepping_motor.
- icmp协议ping程序实现
- arduino new ping
- 域名ip批量ping工具
- Programming Assignment 3: UDP Pinger Lab
- 织梦ping插件破解版
- pingFang Regular
- Two Dimensional Phase Unwrapping Theory Algori
- SnippingToolFor2008
- quickping快速ping ip地址
- Linux设备NDIS调试步骤.rar
- 达内项目 购物车 shoppingcart
- Ping界面实现
- 科大讯飞语音评测使用相关类
- 可视化的Ping工具 v1.0
- Smokeping For Windows 详细安装文档
- model_of_Two_phase_Hybrid_stepping_motor.slx
- Xposed框架以及JustTrusetMe安装包
- PingFang SC Regular.ttf
- stm32cube+lwip无法PING通,解决思路
- shopping online 购物系统论文演示ppt
- gmapping论文
- traceroute工具
- 自动白平衡、色彩恒常性源代码之:
- ping加上时间——写入日志
- An Approach to Energy Usage Audit for Mobile C
- SnippingTool.zip
- Charge polarization and quantum trapping in Ag
- Blow up for the solutions of the Euler-Poisson
评论
共有 条评论