Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
tcp.c
Package: raw_socket.zip [view]
Upload User: bobuwen
Upload Date: 2007-01-07
Package Size: 10k
Code Size: 3k
Category:
Linux-Unix program
Development Platform:
Unix_Linux
- /* 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);
- }