1 /******************************************************************************
6 Description: Encapsulation Functions for DCCP conversion to TCP
8 ******************************************************************************/
13 /*Encapsulation start point and link layer selector*/
14 int do_encap(int link, struct pcap_pkthdr *h, u_char **nptr, int *nlength, const u_char **optr, int *length)
19 if(!ethernet_encap(h, nptr, nlength, optr, length)){
25 if(!ipv4_encap(h, nptr, nlength, optr, length)){
30 /*Linux Cooked Capture*/
31 if(!linux_cooked_encap(h, nptr, nlength, optr, length)){
36 dbgprintf(0, "Unknown Link Layer\n");
40 /*Adjust libpcap header*/
41 if(h->caplen >= h->len || h->caplen >= *nlength){
49 /*Standard Ethernet Encapsulation*/
50 int ethernet_encap(struct pcap_pkthdr *h, u_char **nptr, int *nlength, const u_char **optr, int *length)
52 struct ether_header *ethh;
56 const u_char *next_optr;
59 if(!h || !nptr || !nlength || !optr || !length || !*nptr || !*optr){
60 dbgprintf(0,"Error: Ethernet Encapsulation Function given bad data!\n");
63 if(*length < sizeof(struct ether_header) || *nlength < sizeof(struct ether_header)){
64 dbgprintf(0, "Error: Ethernet Encapsulation Function given packet of wrong size!\n");
68 /*Copy Ethernet header over*/
69 memcpy(*nptr, *optr, sizeof(struct ether_header));
72 ethh=(struct ether_header*)(*nptr);
74 /*Adjust pointers and lengths*/
75 next_optr= *optr+ sizeof(struct ether_header);
76 next_nptr= *nptr+ sizeof(struct ether_header);
77 next_len= *length- sizeof(struct ether_header);
78 next_nlen= *nlength- sizeof(struct ether_header);
80 /*Select Next Protocol*/
81 switch(ntohs(ethh->ether_type)){
83 if(!ipv4_encap(h, &next_nptr, &next_nlen, &next_optr, &next_len)){
88 dbgprintf(1, "Unknown Next Protocol at Ethernet\n");
94 *nlength=next_nlen + sizeof(struct ether_header);
98 /*IPv4 Encapsulation*/
99 int ipv4_encap(struct pcap_pkthdr *h, u_char **nptr, int *nlength, const u_char **optr, int *length)
105 const u_char *next_optr;
108 if(!h || !nptr || !nlength || !optr || !length || !*nptr || !*optr){
109 dbgprintf(0,"Error: IPv4 Encapsulation Function given bad data!\n");
112 if(*length < sizeof(struct iphdr) || *nlength < sizeof(struct iphdr)){
113 dbgprintf(0, "Error: IPv4 Encapsulation Function given packet of wrong size!\n");
117 /*Copy IPv4 header over*/
118 memcpy(*nptr, *optr, sizeof(struct iphdr));
121 iph=(struct iphdr*)(*nptr);
123 /*Adjust pointers and lengths*/
124 next_optr= *optr +iph->ihl*4;
125 next_nptr= *nptr +iph->ihl*4;
126 next_len= *length -iph->ihl*4;
127 next_nlen= *nlength-iph->ihl*4;
129 /*Confirm that this is IPv4*/
131 dbgprintf(1, "Note: Packet is not IPv4\n");
135 /*Select Next Protocol*/
136 switch(iph->protocol){
139 if(!convert_packet(h, &next_nptr, &next_nlen, &next_optr, &next_len)){
144 dbgprintf(1, "Unknown Next Protocol at IPv4\n");
149 /*set ip to indicate that TCP is next protocol*/
154 *nlength=next_nlen + iph->ihl*4;
156 /*Determine if computed length is reasonable*/
157 if(*nlength > 0xFFFF){
158 dbgprintf(1, "Error: Given TCP header+data length is too large for an IPv4 packet!\n");
162 /*Adjust IPv4 header to account for packet's total length*/
163 iph->tot_len=htons(*nlength);
167 int linux_cooked_encap(struct pcap_pkthdr *h, u_char **nptr, int *nlength, const u_char **optr, int *length)
169 struct sll_header *slh;
173 const u_char *next_optr;
177 if(!h || !nptr || !nlength || !optr || !length || !*nptr || !*optr){
178 dbgprintf(0,"Error: SLL Encapsulation Function given bad data!\n");
181 if(*length < sizeof(struct sll_header) || *nlength < sizeof(struct sll_header)){
182 dbgprintf(0, "Error: SLL Encapsulation Function given packet of wrong size!\n");
186 /*Copy SLL header over*/
187 memcpy(*nptr, *optr, sizeof(struct sll_header));
190 slh=(struct sll_header*)(*nptr);
192 /*Adjust pointers and lengths*/
193 next_optr= *optr + sizeof(struct sll_header);
194 next_nptr= *nptr + sizeof(struct sll_header);
195 next_len= *length - sizeof(struct sll_header);
196 next_nlen= *nlength- sizeof(struct sll_header);
198 /*Confirm that this is SLL*/
199 if(ntohs(slh->sll_pkttype) > 4){
200 dbgprintf(1, "Note: Packet is not SLL (Linux Cooked Capture)\n");
204 /*Select Next Protocol*/
205 switch(ntohs(slh->sll_protocol)){
207 if(!ipv4_encap(h, &next_nptr, &next_nlen, &next_optr, &next_len)){
212 dbgprintf(1, "Unknown Next Protocol at SLL\n");
218 *nlength=next_nlen + sizeof(struct sll_header);