📄 udp.c
字号:
/* Write a udp packet and send it through * a raw socket. * Thamer Al-Herbish shadows@whitefang.com */#include <stdlib.h>#include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netinet/in_systm.h>#if defined(LINUX)#include <linux/ip.h>#include <linux/udp.h>#else#include <netinet/ip.h>#include <netinet/udp.h>#endif#if defined(SOLARIS_CKSUM_BUG)#include <netinet/tcp.h>#endif#include <rawsock_utils.h>#include <string.h>#define MESG_LENGTH 12int main(int argc,char *argv[]){ unsigned char packet[#if !defined(LINUX) sizeof(struct ip) +#else /* LINUX */ sizeof(struct iphdr) +#endif /* LINUX */ sizeof(struct udphdr) + MESG_LENGTH]; struct in_addr saddr, daddr; unsigned short sport, dport; struct sockaddr_in mysocket; struct udphdr *udphdr; int sockd, on = 1; if(argc < 5) { fprintf(stderr,"usage: %s source_port source_address dest_port dest_address\n", argv[0]); exit(1); } sport = (unsigned short)atoi(argv[1]); saddr.s_addr = inet_addr(argv[2]); dport = (unsigned short)atoi(argv[3]); daddr.s_addr = inet_addr(argv[4]); if((sockd = socket(AF_INET,SOCK_RAW,IPPROTO_RAW)) < 0) { perror("socket"); exit(1); } if(setsockopt(sockd,IPPROTO_IP,IP_HDRINCL,(char *)&on,sizeof(on)) < 0) { perror("setsockopt"); exit(1); } ip_gen(packet,IPPROTO_UDP,saddr,daddr,sizeof(packet));#if !defined(LINUX) udphdr = (struct udphdr *)(packet + sizeof(struct ip)); memset((packet+sizeof(struct udphdr)+sizeof(struct ip)), 0,MESG_LENGTH); /* Just zero out the message content. */ udp_gen((char *)udphdr,sport,dport,(sizeof(struct udphdr) + MESG_LENGTH));#if !defined(SOLARIS_CKSUM_BUG) udphdr->uh_sum = trans_check(IPPROTO_UDP,(char *)udphdr, (MESG_LENGTH + sizeof(struct udphdr)), saddr, daddr);#else /* SOLARIS_CKSUM_BUG */ udphdr->uh_sum = sizeof(struct tcphdr);#endif /* SOLARIS_CKSUM_BUG */#else /* LINUX */ udphdr = (struct udphdr *)(packet + sizeof(struct iphdr)); memset((packet+sizeof(struct udphdr)+sizeof(struct iphdr)), '0',MESG_LENGTH); /* Just zero out the message content. */ udp_gen((char *)udphdr,sport,dport,(sizeof(struct udphdr) + MESG_LENGTH));#if !defined(SOLARIS_CKSUM_BUG) udphdr->check = trans_check(IPPROTO_UDP,(char *)udphdr, (MESG_LENGTH + sizeof(struct udphdr)), saddr, daddr);#else /* SOLARIS_CKSUM_BUG */ udphdr->check = sizeof(struct tcphdr);#endif /* SOLARIS_CKSUM_BUG */#endif /* LINUX */ memset(&mysocket,'\0',sizeof(mysocket)); mysocket.sin_family = AF_INET; mysocket.sin_port = htons(dport); mysocket.sin_addr = daddr; if(sendto(sockd,&packet,sizeof(packet),0x0,(struct sockaddr *)&mysocket, sizeof(mysocket)) != sizeof(packet)) { perror("sendto"); exit(1); } exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -