]> sjero.net Git - ltp2tcp/blob - udp_encap.c
b14ede83d9b1aba22a18479f09252168f5849346
[ltp2tcp] / udp_encap.c
1 /******************************************************************************
2 Author: Samuel Jero
3
4 Date: 12/2010
5
6 Description:  <ETH, IPv4, UDP> encapsulation functions
7
8 ******************************************************************************/
9 #include "ltp2tcp.h"
10 #include <netinet/udp.h>
11
12
13
14 extern int debug;
15
16
17
18
19 /* encapsulation manipulation previous to packet conversion */
20 int udp_encap_pre(struct pcap_pkthdr *h, const u_char **odata, u_char **ndata, int* olength, int* nlength)
21 {
22         struct iphdr                    *iph;
23         struct ether_header             *ethh;
24         struct udphdr                   *udph;
25
26         /*Safety checks*/
27         if(!h || !odata || !ndata || !*odata || !*ndata || !olength || !nlength){
28                 dbgprintf(0,"Error: UDP Encapsulation given bad data!\n");
29                 exit(1);
30         }
31         if(*olength < sizeof(struct ether_header)+sizeof(struct iphdr)+sizeof(struct udphdr)
32                         || *nlength < sizeof(struct ether_header)+sizeof(struct iphdr)+sizeof(struct tcphdr)){
33                         dbgprintf(0, "Error: UDP Encapsulation given packet of wrong size!\n");
34                         return -1;
35         }
36
37         /*initialize encapsulation private data*/
38         if(state.en_priv==NULL){
39                 /* First time, allocate memory and copy libpcap header and encap headers
40                  * this guarantees the IP "direction" of the encap headers */
41                 state.en_priv=malloc(sizeof(struct eip4_en_p));
42                 if(state.en_priv==NULL){
43                         dbgprintf(0,"Error: Couldn't allocate Memory\n");
44                         exit(1);
45                 }
46         }
47         if(fill_eip4_encap((struct eip4_en_p*)state.en_priv, *odata, *olength, h)<0){
48                 return -1;
49         }
50
51         /*Copy Ethernet and IPv4 headers over*/
52         memcpy(*ndata, *odata, sizeof(struct ether_header)+sizeof(struct iphdr));
53         *odata+=sizeof(struct ether_header)+ sizeof(struct iphdr);
54         *ndata+=sizeof(struct ether_header)+ sizeof(struct iphdr);
55
56         /*Confirm that this is Ethernet and that IPv4 is next*/
57         ethh=(struct ether_header*)(*odata -sizeof(struct ether_header)- sizeof(struct iphdr));
58         if(ethh->ether_type!=htons(ETHERTYPE_IP)){
59                 dbgprintf(1, "Note: Packet not Ethernet or Not IPv4 next\n");
60                 return -1;
61         }
62
63         /* Check That this is IPv4 and that UDP is next*/
64         iph= (struct iphdr *) (*ndata - sizeof(struct iphdr));
65         if(iph->version!=4){
66                 dbgprintf(1, "Note: Packet is not IPv4\n");
67                 return -1;
68         }
69         if(iph->protocol!=0x11){
70                 dbgprintf(1, "Note: Packet is not UDP\n");
71                 return -1;
72         }
73
74         /*set ip to indicate that tcp is next protocol*/
75         iph->protocol=6;
76         iph->check=htonl(0);
77
78         /* Adjust libpcap headers*/
79         h->caplen=sizeof(struct ether_header) +sizeof(struct iphdr);
80         h->len=sizeof(struct ether_header) +sizeof(struct iphdr);
81
82         /*Adjust packet length*/
83         udph=(struct udphdr*)*odata;
84         *olength=ntohs(udph->len);
85
86         /*Adjust New Packet Length*/
87         *nlength-=sizeof(struct ether_header) +sizeof(struct iphdr);
88
89         /*Move Packet Pointer past UDP header*/
90         *odata+=sizeof(struct udphdr);
91 return 0;
92 }
93
94 /* encapsulation manipulation after conversion */
95 int udp_encap_post(int tlen, u_char *data)
96 {
97         return eip4_post((struct eip4_en_p*)state.en_priv, tlen, data);
98 }
99
100 /* Create a TCP three-way handshake */
101 int udp_encap_handshake(struct pcap_pkthdr *h)
102 {
103         return eip4_handshake((struct eip4_en_p*)state.en_priv, h);
104 }
105
106 /* Create a TCP ending handshake */
107 int udp_encap_fin()
108 {
109         return eip4_fin((struct eip4_en_p*)state.en_priv);
110 }
111
112
113
114
115 /* The UDP Encapsulation Structure*/
116 struct encap_ops udp_encap = {
117         .pre=udp_encap_pre,
118         .post=udp_encap_post,
119         .handshake=udp_encap_handshake,
120         .fin=udp_encap_fin
121 };