-
大小: 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秒计时器
相关资源
- delphi 通过Ping命令检测网络是否正常
- ping 程序 C语言
- window ping命令加时间并记录日志
- Exact Conditions of Blow-up and Global Existen
- Orientation-resolved 3d5/2 energy shift of Rh
- Photoluminescence and photoabsorbance blue shi
- Coordination-resolved 4f binding energy shift
- API实现ping测试源码通信测试_API测试源
- Co-doping effect on the martensitic transforma
- 自动获取本机网关并自动ping网管的
- 赤裸裸的统计学_Naked_Statistics_Strippi
- Shopping+在线购物商城的实现Spring+Spr
- 依据ICMP协议实现网络工具ping,并将时
- Adaptive Back-Stepping Sliding Mode Attitude T
- 手把手 网络抓包学习视频 跟着pingi
- springmvc_2020.rar
- turtlebot上hokuyo和gmapping建图和amcl导航
- Lidar Odometry and mapping in real time
- ping程序的设计与实现.pdf
- [Damelio_Robert]_The_Basics_of_Process_Mapping
- Simulating planar reflection using two-pass re
- WIN10qudiaopinggushuiyinxiaogongju.zip
- fping-msys2.0.zip
- GMTSAR软件InSAR数据处理流程及使用详细
- 计算机网络课程设计ping和tracert的实现
- Isidoro-ShadowMapping
- 好用的ping命令工具软件
- Developing Web Services with Apache Axis2
- 网上商城(ShoppingMallSystem(UML))设
- 品优购项目源码
评论
共有 条评论