lwip ping 其他设备

来源:互联网 发布:苹果在线看岛片软件 编辑:程序博客网 时间:2024/06/05 04:00

大多数场合,基于STM32+LwIP的网络设备,需要用电脑ping测试。
很少情况下,需要STM32实现ping指令。

不过如何实现呢,还没有试过,但是可以参考Liunx是如何实现Ping命令的:
http://blog.sina.com.cn/s/blog_560193a80100y3yd.html

linux c socket之PING命令的简单实现

//copy from http://ptpress.com.cn

#include <stdio.h>#include <stdlib.h>#include <string.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<unistd.h>#include<netinet/ip_icmp.h>#define BUFFER_SIZE 1024//对ICMP包的验证u_short cal_cksum(const u_short*addr,register int len,u_short csum){register int nleft=len;const u_short * w =addr;register int sum = csum;//1,16位累加while(nleft>1){sum+= *w++;nleft-=2;}//2,字节长度为奇数,补零成偶数,并再次累加if(nleft==1){sum+=htons(*(u_char*)w<<8);}//3,字节长度为偶数sum=(sum>>16)+(sum&0xffff);sum+=(sum>>16);//4,取反,判断return ~sum;}int main(int argc,char*argv[]){int fd,tmp;struct sockaddr_in sin;int buf_size;int buf2_size;char * buf;char * buf2;int len;struct iphdr *ip_header;struct icmphdr *icmp_header;//创建套接字fd = socket(PF_INET,SOCK_RAW,IPPROTO_ICMP);if(fd<0){printf("create socket error\n");return -1;}//绑定套接字memset(&sin,0,sizeof(sin));sin.sin_port = htons(1111);if( (bind(fd,(struct sockaddr*)&sin,sizeof(sin) ))<0){printf("bind fail\n");close(fd);return -1;}//设置IPtmp=1;setsockopt(fd,0,IP_HDRINCL,&tmp,sizeof(tmp));//设置IP过程//1,申请一块内存放Ip数据=IP头+ICMP包头长度+时间搓长度buf_size = sizeof(struct iphdr)+sizeof(struct icmphdr)+sizeof(struct timeval);buf2_size=buf_size;buf = (char*)malloc(buf_size);buf2 = (char*)malloc(buf2_size);//2,填充IP头信息memset(buf,0,buf_size);memset(&sin,0,sizeof(sin));sin.sin_family = AF_INET;sin.sin_addr.s_addr= inet_addr("192.168.1.102");ip_header =(struct iphdr*)buf;ip_header->ihl = 5;//ip包头长度ip_header->version=4;//版本ip_header->tos=0;ip_header->tot_len=htons(buf_size);ip_header->id=rand();ip_header->frag_off=0x40;ip_header->ttl=64;ip_header->protocol=IPPROTO_ICMP;ip_header->check=0;ip_header->saddr=0;ip_header->daddr=inet_addr("192.168.1.102");//设置ICMPicmp_header=(struct icmphdr*)(ip_header+1);icmp_header->type=ICMP_ECHO;icmp_header->code=0;icmp_header->un.echo.id=htons(1111);icmp_header->un.echo.sequence=0;icmp_header->checksum=cal_cksum( (const u_short*)icmp_header,\sizeof(struct icmphdr)+sizeof(struct timeval),(u_short)0) ;//发送if(sendto(fd,buf,buf_size,0,( struct sockaddr* )&sin,sizeof(struct sockaddr_in) )<0){return -1;}//接收if(recvfrom(fd,buf2,buf2_size,0,(struct sockaddr*)&sin,&len)<0){return -1;}printf("received\n");return 0;}
原创粉丝点击