tcp.c
上传用户:bobuwen
上传日期:2007-01-07
资源大小:10k
文件大小:3k
- /* Generate a TCP packet
- * with a FIN flag and pass 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/tcp.h>
- #else
- #include <netinet/ip.h>
- #include <netinet/tcp.h>
- #endif
- #include <string.h>
- #include <unistd.h>
- #include <time.h>
- #include <rawsock_utils.h>
- int main(int argc,char *argv[])
- {
- unsigned char packet[
- #if !defined(LINUX)
- sizeof(struct ip) +
- #else /* LINUX */
- sizeof(struct iphdr) +
- #endif /* LINUX */
- sizeof(struct tcphdr)];
- struct sockaddr_in mysocket;
- unsigned short sport, dport;
- struct in_addr saddr, daddr;
- struct tcphdr *tcp;
- unsigned long seq, ack;
- int sockd, on = 1;
-
- if(argc < 5) {
- fprintf(stderr,"usage: %s source_port source_address dest_port dest_addressn",
- 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);
- }
-
- /* Very bad random sequence number generator */
-
- srand(getpid());
-
- seq = rand()%time(NULL);
- ack = rand()%time(NULL);
-
- ip_gen(packet,IPPROTO_TCP,saddr,daddr,sizeof(packet));
-
- #if !defined(LINUX)
- tcp = (struct tcphdr *)(packet + sizeof(struct ip));
- tcp_gen((char *)tcp,sport,dport,seq,ack);
- #if !defined(SOLARIS_CKSUM_BUG)
- tcp->th_sum = trans_check(IPPROTO_TCP,(char *)tcp,
- sizeof(struct tcphdr),
- saddr,
- daddr);
-
- #else /* SOLARIS_CKSUM_BUG */
- tcp->th_sum = sizeof(struct tcphdr);
- #endif /* SOLARIS_CKSUM_BUG */
- #else /* LINUX */
-
- tcp = (struct tcphdr *)(packet + sizeof(struct iphdr));
- tcp_gen((char *)tcp,sport,dport,seq,ack);
-
- #if !defined(SOLARIS_CKSUM_BUG)
- tcp->check = trans_check(IPPROTO_TCP,(char *)tcp,
- sizeof(struct tcphdr),
- saddr,
- daddr);
- #else /* SOLARIS_CKSUM_BUG */
- tcp->check = sizeof(struct tcphdr);
- #endif /* SOLARIS_CKSUM_BUG */
- #endif /* LINUX */
-
- memset(&mysocket,' ',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);
- }