Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

spoof.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Copyright 2005 Daniel Ferullo                                             *
00003  *                                                                           *
00004  * Licensed under the Apache License, Version 2.0 (the "License");           *
00005  * you may not use this file except in compliance with the License.          *
00006  * You may obtain a copy of the License at                                   *
00007  *                                                                           *
00008  *    http://www.apache.org/licenses/LICENSE-2.0                             *
00009  *                                                                           *
00010  * Unless required by applicable law or agreed to in writing, software       *
00011  * distributed under the License is distributed on an "AS IS" BASIS,         *
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
00013  * See the License for the specific language governing permissions and       *
00014  * limitations under the License.                                            *
00015  *                                                                           *
00016  *****************************************************************************/
00017 
00018 /**
00019  * @file spoof.c
00020  * @author Daniel Ferullo (ferullo@cmu.edu)
00021  *
00022  * @brief functions to spoof/forge network packets
00023  */
00024 
00025 #include "spoof.h"
00026 #include "spoof_private.h"
00027 #include <libnet.h>
00028 #include "debug.h"
00029 #include "peerdef.h"
00030 
00031 errorcode spoof(tcp_packet_info_t *tcp_hdr, char *device, void *payload,
00032                                         unsigned long payload_len, short ttl){
00033 
00034         /* declare local variables */
00035         int c;
00036         libnet_t *lib;
00037         libnet_ptag_t t;
00038         char errbuf[LIBNET_ERRBUF_SIZE];
00039         unsigned char tcp_flags;
00040 
00041         /* error check arguments */
00042         CHECK_NOT_NULL(tcp_hdr,ERROR_NULL_ARG_1);
00043         CHECK_NOT_NULL(device,ERROR_NULL_ARG_2);
00044         if ( (payload==NULL) && (payload_len!=0))
00045                 return ERROR_ARG_3;
00046 
00047         /* do function */
00048 
00049         /* set the tcp_flags */
00050         tcp_flags = 0;
00051         tcp_flags |= ( (tcp_hdr->syn_flag==FLAG_SET) ? TH_SYN : 0);
00052         tcp_flags |= ( (tcp_hdr->ack_flag==FLAG_SET) ? TH_ACK : 0);
00053 
00054         /* Initialize the library.  Root priviledges are required. */
00055         lib = libnet_init(
00056                 LIBNET_RAW4,                 /* injection type */
00057                 device,                      /* network interface */
00058                 errbuf);                     /* errbuf */
00059 
00060         if (lib == NULL) {
00061                 DEBUG(DBG_SPOOF,"SPOOF:libnet_init() failed\n");
00062                 return ERROR_1;
00063         }
00064 
00065         /* make sure to put ports, seq_num, ack_num and window in host byte
00066          * order because libnet doesn't want them in network byte order. */
00067         t = libnet_build_tcp(
00068                 PORT_2HBO(tcp_hdr->s_port),     /* source port */
00069                 PORT_2HBO(tcp_hdr->d_port),     /* destination port */
00070                 SEQ_NUM_2HBO(tcp_hdr->seq_num), /* sequence number */
00071                 SEQ_NUM_2HBO(tcp_hdr->ack_num), /* acknowledgement number */
00072                 tcp_flags,                      /* control flags */
00073                 WINDOW_2HBO(tcp_hdr->window),   /* window size */
00074                 0,                              /* checksum */
00075                 0,                              /* urgent pointer */
00076                 LIBNET_TCP_H + payload_len,     /* TCP packet size */
00077                 payload,                        /* payload */
00078                 payload_len,                    /* payload size */
00079                 lib,                            /* libnet handle */
00080                 0                               /* libnet id */
00081             );
00082 
00083         if (t == -1) {
00084                 DEBUG(DBG_SPOOF,"SPOOF:can't build TCP header\n");
00085                 libnet_destroy(lib);
00086                 return ERROR_2;
00087         }
00088 
00089         t = libnet_build_ipv4(
00090                 LIBNET_IPV4_H+LIBNET_TCP_H+payload_len,  /* length */
00091                 0,                                       /* TOS */
00092                 242,                                     /* IP ID */
00093                 0,                                       /* IP Frag */
00094                 ttl,                                      /* TTL */
00095                 IPPROTO_TCP,                             /* protocol */
00096                 0,                                       /* checksum */
00097                 tcp_hdr->s_addr,                         /* source IP */
00098                 tcp_hdr->d_addr,                         /* destination IP */
00099                 NULL,                                    /* payload */
00100                 0,                                       /* payload size */
00101                 lib,                                     /* libnet handle */
00102                 0                                        /* libnet id */
00103             );
00104 
00105         if (t == -1) {
00106                 DEBUG(DBG_SPOOF,"SPOOF:can't build IP header\n");
00107                 libnet_destroy(lib);
00108                 return ERROR_3;
00109         }
00110 
00111         /* Write it to the wire. */
00112         c = libnet_write(lib);
00113 
00114         if (c == -1) {
00115                 DEBUG(DBG_SPOOF,"SPOOF:write error\n");
00116                 libnet_destroy(lib);
00117                 return ERROR_4;
00118         }
00119 
00120         libnet_destroy(lib);
00121         return SUCCESS;
00122 }
00123 

Generated on Wed Mar 30 23:20:47 2005 for NATBLASTER by  doxygen 1.3.9.1