17 #include <netinet/if_ether.h> 18 #include <netinet/ip.h> 19 #include <netinet/tcp.h> 52 struct pkt_buff *
pktb_alloc(
int family,
void *data,
size_t len,
size_t extra)
54 struct pkt_buff *pktb;
55 struct ethhdr *ethhdr;
58 pktb = calloc(1,
sizeof(
struct pkt_buff) + len + extra);
63 pkt_data = (uint8_t *)pktb +
sizeof(
struct pkt_buff);
64 memcpy(pkt_data, data, len);
67 pktb->data_len = len + extra;
69 pktb->data = pkt_data;
74 pktb->network_header = pktb->data;
77 ethhdr = (
struct ethhdr *)pktb->data;
78 pktb->mac_header = pktb->data;
80 switch(ethhdr->h_proto) {
83 pktb->network_header = pktb->data + ETH_HLEN;
87 errno = EPROTONOSUPPORT;
190 void pktb_put(
struct pkt_buff *pktb,
unsigned int len)
222 return pktb->data_len - pktb->len;
235 return pktb->mac_header;
247 return pktb->network_header;
262 return pktb->transport_header;
269 static int pktb_expand_tail(
struct pkt_buff *pktb,
int extra)
275 if (pktb->len + extra > pktb->data_len)
282 static int enlarge_pkt(
struct pkt_buff *pktb,
unsigned int extra)
284 if (pktb->len + extra > 65535)
316 unsigned int match_offset,
317 unsigned int match_len,
318 const char *rep_buffer,
319 unsigned int rep_len)
323 if (rep_len > match_len &&
325 !enlarge_pkt(pktb, rep_len - match_len))
328 data = pktb->network_header + dataoff;
331 memmove(data + match_offset + rep_len,
332 data + match_offset + match_len,
333 pktb_tail(pktb) - (pktb->network_header + dataoff +
334 match_offset + match_len));
337 memcpy(data + match_offset, rep_buffer, rep_len);
340 if (rep_len > match_len)
341 pktb_put(pktb, rep_len - match_len);
343 pktb_trim(pktb, pktb->len + rep_len - match_len);
345 pktb->mangled =
true;
361 return pktb->mangled;
bool pktb_mangled(const struct pkt_buff *pktb)
uint8_t * pktb_transport_header(struct pkt_buff *pktb)
uint8_t * pktb_data(struct pkt_buff *pktb)
struct pkt_buff * pktb_alloc(int family, void *data, size_t len, size_t extra)
void pktb_trim(struct pkt_buff *pktb, unsigned int len)
uint8_t * pktb_mac_header(struct pkt_buff *pktb)
void pktb_put(struct pkt_buff *pktb, unsigned int len)
void pktb_push(struct pkt_buff *pktb, unsigned int len)
void pktb_free(struct pkt_buff *pktb)
void pktb_pull(struct pkt_buff *pktb, unsigned int len)
uint8_t * pktb_network_header(struct pkt_buff *pktb)
uint32_t pktb_len(struct pkt_buff *pktb)
int pktb_mangle(struct pkt_buff *pktb, int dataoff, unsigned int match_offset, unsigned int match_len, const char *rep_buffer, unsigned int rep_len)
unsigned int pktb_tailroom(struct pkt_buff *pktb)