[go: up one dir, main page]

File: ipv4.c

package info (click to toggle)
tstools 1.11-1
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, squeeze, stretch, wheezy
  • size: 2,268 kB
  • ctags: 2,040
  • sloc: ansic: 33,549; java: 2,243; makefile: 343; python: 263; sh: 5
file content (116 lines) | stat: -rw-r--r-- 2,863 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* ipv4.c */
/* 
 * Routines for dissecting ipv4 
 *
 *
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the MPEG TS, PS and ES tools.
 *
 * The Initial Developer of the Original Code is Amino Communications Ltd.
 * Portions created by the Initial Developer are Copyright (C) 2008
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Richard Watts, Kynesim <rrw@kynesim.co.uk>
 *
 * ***** END LICENSE BLOCK *****
 */

#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 */