资源简介
网上看到很多关于Raw Socket实现UDP数据包发送的例子,可是没有IPINIP形式的UDP数据包发送,因此自己实现了一个。
环境:gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)
语言:C
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#define BUFLEN 1000
#define PORT 8848
typedef struct ip_hdr{//ipv4头部
unsigned int ip_length:4; /*little-endian*/
unsigned int ip_version:4;
unsigned char ip_tos;
unsigned short ip_total_length;
unsigned short ip_id;
unsigned short ip_flags;
unsigned char ip_ttl;
unsigned char ip_protocol;
unsigned short ip_cksum;
unsigned int ip_source;
unsigned int ip_dest;
}ip_hdr;
typedef struct udp_hdr{//udp头部
unsigned short s_port;
unsigned short d_port;
unsigned short length;
unsigned short cksum;
}udp_hdr;
typedef struct psd_header{//伪头部,用于计算校验和
unsigned int s_ip;//source ip
unsigned int d_ip;//dest ip
unsigned char mbz;//0
unsigned char proto;//proto type
unsigned short plen;//length
}psd_header;
unsigned short checksum(unsigned short* buffer int size)//校验和
{
unsigned long cksum = 0;
while(size>1){
cksum += *buffer++;
size -= sizeof(unsigned short);
}
if(size){
cksum += *(unsigned char*)buffer;
}
cksum = (cksum>>16) + (cksum&0xffff); //将高16bit与低16bit相加
cksum += (cksum>>16); //将进位到高位的16bit与低16bit 再相加
return (unsigned short)(~cksum);
}
int main(int argc char *argv[])
{
char buf[BUFLEN];
int sockfd = -1;
struct sockaddr_in6 host_addr;
if((sockfd = socket(AF_INET6 SOCK_RAW IPPROTO_IPIP))<0){
perror(“socket() error:“);
exit(1);
}
bzero(&host_addrsizeof(host_addr
- 上一篇:FPGA 60计时
- 下一篇:7-zip右键扩展tar.gz
评论
共有 条评论