kopia lustrzana https://github.com/F5OEO/tstools
93 wiersze
1.9 KiB
C
93 wiersze
1.9 KiB
C
|
/* ipv4.c */
|
||
|
/*
|
||
|
* Routines for dissecting ipv4
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
#include "ipv4.h"
|
||
|
#include "misc_fns.h"
|
||
|
#include <string.h>
|
||
|
|
||
|
int ipv4_from_payload(const uint8_t *data,
|
||
|
const uint32_t len,
|
||
|
ipv4_header_t *out_hdr,
|
||
|
uint32_t *out_st,
|
||
|
uint32_t *out_len)
|
||
|
{
|
||
|
uint32_t cur_field;
|
||
|
|
||
|
// Min length of an ipv4 header is 20 bytes (5 words)
|
||
|
if (len < 20)
|
||
|
{
|
||
|
return IPV4_ERR_PKT_TOO_SHORT;
|
||
|
}
|
||
|
|
||
|
// Field 0
|
||
|
cur_field = uint_32_be(&data[0]);
|
||
|
|
||
|
out_hdr->version = (cur_field >> 28)&0xf;
|
||
|
out_hdr->hdr_length = (cur_field >> 24)& 0xf;
|
||
|
out_hdr->serv_type = (cur_field >> 16) & 0xff;
|
||
|
out_hdr->length = (cur_field) & 0xffff;
|
||
|
|
||
|
// Field 1
|
||
|
cur_field = uint_32_be(&data[4]);
|
||
|
|
||
|
out_hdr->ident = (cur_field >> 16) & 0xffff;
|
||
|
out_hdr->flags = (cur_field >> 14) & 3;
|
||
|
out_hdr->frag_offset = (cur_field & 0x3fff);
|
||
|
|
||
|
// Field 2
|
||
|
cur_field = uint_32_be(&data[8]);
|
||
|
|
||
|
out_hdr->ttl = (cur_field >> 24) & 0xff;
|
||
|
out_hdr->proto = (cur_field >> 16) & 0xff;
|
||
|
out_hdr->csum = (cur_field) & 0xffff;
|
||
|
|
||
|
// Field 3 - src address.
|
||
|
out_hdr->src_addr = uint_32_be(&data[12]);
|
||
|
// Field 4 - dest address.
|
||
|
out_hdr->dest_addr = uint_32_be(&data[16]);
|
||
|
|
||
|
// Now the data ..
|
||
|
(*out_st) = (out_hdr->hdr_length << 2);
|
||
|
(*out_len) = len - (out_hdr->hdr_length << 2);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
int ipv4_udp_from_payload(const uint8_t *data,
|
||
|
const uint32_t len,
|
||
|
ipv4_udp_header_t *out_hdr,
|
||
|
uint32_t *out_st,
|
||
|
uint32_t *out_len)
|
||
|
{
|
||
|
uint32_t cur_field;
|
||
|
|
||
|
// UDP headers are 8 bytes long.
|
||
|
if (len < 8)
|
||
|
{
|
||
|
return IPV4_ERR_PKT_TOO_SHORT;
|
||
|
}
|
||
|
|
||
|
cur_field = uint_32_be(&data[0]);
|
||
|
out_hdr->source_port = (cur_field >> 16)&0xffff;
|
||
|
out_hdr->dest_port = (cur_field) & 0xffff;
|
||
|
|
||
|
cur_field = uint_32_be(&data[4]);
|
||
|
out_hdr->length = (cur_field >> 16) & 0xffff;
|
||
|
out_hdr->csum = (cur_field) &0xffff;
|
||
|
|
||
|
(*out_st) = 8;
|
||
|
(*out_len) = len - 8;
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
/* End file */
|
||
|
|