使用rawsocket构造UDP数据包方法
2008-12-31 19:25
441 查看
/rudp-sender.c 用rawsocket实现的发送UDP数据包工具。
//引用时,需要多源目的IP以及端口做些修改。linux下可以直接编译。
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #include <errno.h> #define BUFLEN 38 #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; void swap(unsigned int *a, unsigned int *b)//交换 { *a = (*a)^(*b); *b = (*a)^(*b); *a = (*a)^(*b); } 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_in host_addr; if((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP))<0) { printf("socket() error!/n"); exit(1); } memset(&host_addr, 0, sizeof(host_addr)); host_addr.sin_family = AF_INET; host_addr.sin_port = htons(PORT); host_addr.sin_addr.s_addr = inet_addr("200.200.30.42"); const int on = 1; if(setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on))<0) { printf("setsockopt() error!/n"); exit(0); } int addr_len = sizeof(host_addr); while(1) { ip_hdr *iphdr; iphdr = (ip_hdr*)buf; udp_hdr *udphdr; udphdr = (udp_hdr*)(buf+20); iphdr->ip_length = 5; iphdr->ip_version= 4; iphdr->ip_tos = 0; iphdr->ip_total_length = htons(sizeof(buf)); iphdr->ip_id = 0; iphdr->ip_flags = 0x40; iphdr->ip_ttl = 0x40; iphdr->ip_protocol = 0x11; iphdr->ip_cksum = 0; iphdr->ip_source = inet_addr("200.200.30.14");//源地址
iphdr->ip_dest = inet_addr("200.200.30.42");//目的地址
iphdr->ip_cksum = checksum((unsigned short*)buf, 20); udphdr->s_port = htons(80);//源端口 udphdr->d_port = htons(1024);//目的端口 udphdr->length = htons(sizeof(buf)-20); udphdr->cksum = 0; psd_header psd; psd.s_ip = iphdr->ip_source; psd.d_ip = iphdr->ip_dest; psd.mbz = 0; psd.proto = 0x11; psd.plen = udphdr->length; char tmp[sizeof(psd)+ntohs(udphdr->length)]; memcpy(tmp, &psd, sizeof(psd)); memcpy(tmp+sizeof(psd), buf+20, sizeof(buf)-20); udphdr->cksum = checksum((unsigned short*)tmp, sizeof(tmp)); int res =sendto(sockfd, buf, sizeof(buf), 0, (struct sockaddr*)&host_addr, sizeof(host_addr)); sleep(1); } return 0; }
相关文章推荐
- 使用SharpPcap发送虚假构造的伪IP的UDP数据包
- Android使用wifi通过UDP协议发送广播数据包给PC接收不到问题解决方法
- 004.UDP--拼接UDP数据包,构造ip头和udp头通信(使用原始套接字)
- 使用SharpPcap发送虚假构造的伪IP的UDP数据包
- 构造使用IN子句的动态Transact-SQL方法进行编号查询
- C++使用构造和析构函数管理同步锁的一种方法Guard
- 构造方法的使用注意
- 构造方法------使用构造方法设置x和y的值,x和y是坐标
- 使用应用程序测试网卡收发UDP数据包性能
- 方法重载、方法重写、构造方法的含义及使用 方法重载、方法重写、构造方法的含义及使用
- 考虑使用静态工厂方法替代构造方法
- linux使用tcpdump命令监视指定网络数据包的方法
- 构造方法注入bean 的使用
- [Effective Java Distilled] Item 1 考虑使用静态工厂方法来替换构造方法
- (第三季)101-字符串类string基础使用102-字符串类string更多的方法103-字符串类StringBuilder的构造 104-string和StringBuilder的区别105-St
- 匿名对象与构造方法的使用
- UDP 服务端与客户端均可以bind()绑定端口, 并且可以使用connect()链接后,使用send()与recv()方法!
- 解决 ”不允许在查询中显式构造实体类型“问题及使用其他方法实现返回 List<Model对象>或者IQueryable<Model对象>对象
- 一阶段-fourteenWork-2-使用Date的Date(int year, int month, int date) 构造方法
- 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法