1 /******************************************************************************
6 Description: <ETH, IPv4, UDP> encapsulation functions
8 ******************************************************************************/
10 #include <netinet/udp.h>
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)
23 struct ether_header *ethh;
27 if(!h || !odata || !ndata || !*odata || !*ndata || !olength || !nlength){
28 dbgprintf(0,"Error: UDP Encapsulation given bad data!\n");
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");
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");
47 if(fill_eip4_encap((struct eip4_en_p*)state.en_priv, *odata, *olength, h)<0){
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);
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");
63 /* Check That this is IPv4 and that UDP is next*/
64 iph= (struct iphdr *) (*ndata - sizeof(struct iphdr));
66 dbgprintf(1, "Note: Packet is not IPv4\n");
69 if(iph->protocol!=0x11){
70 dbgprintf(1, "Note: Packet is not UDP\n");
74 /*set ip to indicate that tcp is next protocol*/
78 /* Adjust libpcap headers*/
79 h->caplen=sizeof(struct ether_header) +sizeof(struct iphdr);
80 h->len=sizeof(struct ether_header) +sizeof(struct iphdr);
82 /*Adjust packet length*/
83 udph=(struct udphdr*)*odata;
84 *olength=ntohs(udph->len);
86 /*Adjust New Packet Length*/
87 *nlength-=sizeof(struct ether_header) +sizeof(struct iphdr);
89 /*Move Packet Pointer past UDP header*/
90 *odata+=sizeof(struct udphdr);
94 /* encapsulation manipulation after conversion */
95 int udp_encap_post(int tlen, u_char *data)
97 return eip4_post((struct eip4_en_p*)state.en_priv, tlen, data);
100 /* Create a TCP three-way handshake */
101 int udp_encap_handshake(struct pcap_pkthdr *h)
103 return eip4_handshake((struct eip4_en_p*)state.en_priv, h);
106 /* Create a TCP ending handshake */
109 return eip4_fin((struct eip4_en_p*)state.en_priv);
115 /* The UDP Encapsulation Structure*/
116 struct encap_ops udp_encap = {
118 .post=udp_encap_post,
119 .handshake=udp_encap_handshake,