add winpcap 4.0.2 from url http://www.winpcap.org/
[natblaster.git] / src / peer / spoof.c
blob1eb644e702b78c8e46d5bcf8b63806c097e10f1d
1 /*****************************************************************************
2 * Copyright 2005 Daniel Ferullo *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 *****************************************************************************/
18 /**
19 * @file spoof.c
20 * @author Daniel Ferullo (ferullo@cmu.edu)
22 * @brief functions to spoof/forge network packets
25 #include "spoof.h"
26 #include "spoof_private.h"
27 #include <libnet.h>
28 #include "debug.h"
29 #include "peerdef.h"
31 errorcode spoof(tcp_packet_info_t *tcp_hdr, char *device, void *payload,
32 unsigned long payload_len, short ttl){
34 /* declare local variables */
35 int c;
36 libnet_t *lib;
37 libnet_ptag_t t;
38 char errbuf[LIBNET_ERRBUF_SIZE];
39 unsigned char tcp_flags;
41 /* error check arguments */
42 CHECK_NOT_NULL(tcp_hdr,ERROR_NULL_ARG_1);
43 CHECK_NOT_NULL(device,ERROR_NULL_ARG_2);
44 if ( (payload==NULL) && (payload_len!=0))
45 return ERROR_ARG_3;
47 /* do function */
49 /* set the tcp_flags */
50 tcp_flags = 0;
51 tcp_flags |= ( (tcp_hdr->syn_flag==FLAG_SET) ? TH_SYN : 0);
52 tcp_flags |= ( (tcp_hdr->ack_flag==FLAG_SET) ? TH_ACK : 0);
54 /* Initialize the library. Root priviledges are required. */
55 lib = libnet_init(
56 LIBNET_RAW4, /* injection type */
57 device, /* network interface */
58 errbuf); /* errbuf */
60 if (lib == NULL) {
61 DEBUG(DBG_SPOOF,"SPOOF:libnet_init() failed\n");
62 return ERROR_1;
65 /* make sure to put ports, seq_num, ack_num and window in host byte
66 * order because libnet doesn't want them in network byte order. */
67 t = libnet_build_tcp(
68 PORT_2HBO(tcp_hdr->s_port), /* source port */
69 PORT_2HBO(tcp_hdr->d_port), /* destination port */
70 SEQ_NUM_2HBO(tcp_hdr->seq_num), /* sequence number */
71 SEQ_NUM_2HBO(tcp_hdr->ack_num), /* acknowledgement number */
72 tcp_flags, /* control flags */
73 WINDOW_2HBO(tcp_hdr->window), /* window size */
74 0, /* checksum */
75 0, /* urgent pointer */
76 LIBNET_TCP_H + payload_len, /* TCP packet size */
77 payload, /* payload */
78 payload_len, /* payload size */
79 lib, /* libnet handle */
80 0 /* libnet id */
83 if (t == -1) {
84 DEBUG(DBG_SPOOF,"SPOOF:can't build TCP header\n");
85 libnet_destroy(lib);
86 return ERROR_2;
89 t = libnet_build_ipv4(
90 LIBNET_IPV4_H+LIBNET_TCP_H+payload_len, /* length */
91 0, /* TOS */
92 242, /* IP ID */
93 0, /* IP Frag */
94 ttl, /* TTL */
95 IPPROTO_TCP, /* protocol */
96 0, /* checksum */
97 tcp_hdr->s_addr, /* source IP */
98 tcp_hdr->d_addr, /* destination IP */
99 NULL, /* payload */
100 0, /* payload size */
101 lib, /* libnet handle */
102 0 /* libnet id */
105 if (t == -1) {
106 DEBUG(DBG_SPOOF,"SPOOF:can't build IP header\n");
107 libnet_destroy(lib);
108 return ERROR_3;
111 /* Write it to the wire. */
112 c = libnet_write(lib);
114 if (c == -1) {
115 DEBUG(DBG_SPOOF,"SPOOF:write error\n");
116 libnet_destroy(lib);
117 return ERROR_4;
120 libnet_destroy(lib);
121 return SUCCESS;