通行证: 用户 密码
http://www.pconline.cn/
汽车网 | 游戏网 | 时尚女性
北京 上海 广州 深圳 香港 广西
重庆 山东 江苏 辽宁 福建 成都
首 页 | 今日报价 | 产业资讯 | 数码世界 | 硬件资讯 | 软件资讯 | 下 载 | 游 戏 |
论 坛 | 产 品 库 | 渠道商情 | 招聘培训 | 二    手 | 评 测 室 | 导 购 | 社 区 |
摄影
部落
手 机
数码相机
随身听
 DIY配件
笔记本
 台式机
摄像机
精品廊
-
您现在的位置: 招聘与培训 > 程序开发 > 其它开发认证 > 开发认证其它 免费新闻代码RSS新闻聚合
  微软ping命令的源代码 出处:CnXHacker.Net
[ 2005-09-29 10:29:01 ] 作者:佚名 责任编辑:lujiezhen

memset(icmp_data,0,MAX_PACKET);
fill_icmp_data(icmp_data,datasize);

while(1) {
int bwrote;

((IcmpHeader*)icmp_data)->i_cksum = 0;
((IcmpHeader*)icmp_data)->timestamp = GetTickCount();

((IcmpHeader*)icmp_data)->i_seq = seq_no++;
((IcmpHeader*)icmp_data)->i_cksum = checksum((USHORT*)icmp_data,
datasize);

bwrote = sendto(sockRaw,icmp_data,datasize,0,(struct sockaddr*)&dest,
sizeof(dest));
if (bwrote == SOCKET_ERROR){
if (WSAGetLastError() == WSAETIMEDOUT) {
printf("timed out\n");
continue;
}
fprintf(stderr,"sendto failed: %d\n",WSAGetLastError());
ExitProcess(STATUS_FAILED);
}
if (bwrote < datasize ) {
fprintf(stdout,"Wrote %d bytes\n",bwrote);
}
bread = recvfrom(sockRaw,recvbuf,MAX_PACKET,0,(struct sockaddr*)&from,
&fromlen);
if (bread == SOCKET_ERROR){
if (WSAGetLastError() == WSAETIMEDOUT) {
printf("timed out\n");
continue;
}
fprintf(stderr,"recvfrom failed: %d\n",WSAGetLastError());
ExitProcess(STATUS_FAILED);
}
decode_resp(recvbuf,bread,&from);
Sleep(1000);

}
return 0;

}
/*
The response is an IP packet. We must decode the IP header to locate
the ICMP data
*/
void decode_resp(char *buf, int bytes,struct sockaddr_in *from) {

IpHeader *iphdr;
IcmpHeader *icmphdr;
unsigned short iphdrlen;

iphdr = (IpHeader *)buf;

iphdrlen = iphdr->h_len * 4 ; // number of 32-bit words *4 = bytes

if (bytes < iphdrlen + ICMP_MIN) {
printf("Too few bytes from %s\n",inet_ntoa(from->sin_addr));
}

icmphdr = (IcmpHeader*)(buf + iphdrlen);

if (icmphdr->i_type != ICMP_ECHOREPLY) {
fprintf(stderr,"non-echo type %d recvd\n",icmphdr->i_type);
return;
}
if (icmphdr->i_id != (USHORT)GetCurrentProcessId()) {
fprintf(stderr,"someone else's packet!\n");
return ;
}
printf("%d bytes from %s:",bytes, inet_ntoa(from->sin_addr));
printf(" icmp_seq = %d. ",icmphdr->i_seq);
printf(" time: %d ms ",GetTickCount()-icmphdr->timestamp);
printf("\n");

}


USHORT checksum(USHORT *buffer, int size) {

unsigned long cksum=0;

while(size >1) {
cksum+=*buffer++;
size -=sizeof(USHORT);
}

if(size ) {
cksum += *(UCHAR*)buffer;
}

cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (USHORT)(~cksum);
}
/*
Helper function to fill in various stuff in our ICMP request.
*/
void fill_icmp_data(char * icmp_data, int datasize){

IcmpHeader *icmp_hdr;
char *datapart;

icmp_hdr = (IcmpHeader*)icmp_data;

icmp_hdr->i_type = ICMP_ECHO;
icmp_hdr->i_code = 0;
icmp_hdr->i_id = (USHORT)GetCurrentProcessId();
icmp_hdr->i_cksum = 0;
icmp_hdr->i_seq = 0;

datapart = icmp_data + sizeof(IcmpHeader);
//
// Place some junk in the buffer.
//
memset(datapart,'E', datasize - sizeof(IcmpHeader));

}


[上一页] [1] [2]
更多相关搜索: ping.. 代码..
正在加载评论,请稍候…

招聘与培训
05届毕业生就业率:大专生:本科生=40:70
招聘与培训
再见深圳!我在深圳8年的职场与人生感言
招聘与培训
大学生求职最郁闷的十种遭遇(组图)
招聘与培训
中产阶层调查:我国中产阶层占就业人口11%
 
发给好友 我要报错 投稿给我们 加入收藏 返回顶部
本栏今日更新
相关文章
·一个患病打工仔:打工人啊,你千万别生病
·Linux服务器的攻防技术介绍
·业务"拉单族",拉着梦想奔走在大街小巷
·个人网页设计常用代码集合
·初体验甲方乙方 毕业生签劳动合同全攻略
·交换技术全攻略 第7章(组图)
·清华博士生宣布退学 暴露我国IT教育问题
·高质量C++/C编程指南-附录A
·个人网页设计常用代码集合
·程序员趣味读物:谈谈Unicode编码
·编程案例:QQ登录口令加密算法及其源代码
·学习收藏:RSS的语言编码大全
·编程心得:关于代码加密解密保护
·揭密:各种各样网页病毒源码解析
·一种简单实用的实现程序全屏的方法
·C++编程中Windows消息解析大全
新闻搜索
高级搜索

本栏今日焦点
-
频道精选