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
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/*
* Copyright (C) 2012-2014 Tobias Brunner
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
/**
* @defgroup ip_packet ip_packet
* @{ @ingroup libipsec
*/
#ifndef IP_PACKET_H_
#define IP_PACKET_H_
#include <library.h>
#include <networking/host.h>
#include <networking/packet.h>
typedef struct ip_packet_t ip_packet_t;
/**
* IP packet
*/
struct ip_packet_t {
/**
* IP version of this packet
*
* @return ip version
*/
uint8_t (*get_version)(ip_packet_t *this);
/**
* Get the source address of this packet
*
* @return source host
*/
host_t *(*get_source)(ip_packet_t *this);
/**
* Get the destination address of this packet
*
* @return destination host
*/
host_t *(*get_destination)(ip_packet_t *this);
/**
* Get the protocol (IPv4) or next header (IPv6) field of this packet.
*
* @return protocol|next header field
*/
uint8_t (*get_next_header)(ip_packet_t *this);
/**
* Get the complete IP packet (including the header)
*
* @return IP packet (internal data)
*/
chunk_t (*get_encoding)(ip_packet_t *this);
/**
* Get only the payload
*
* @return IP payload (internal data)
*/
chunk_t (*get_payload)(ip_packet_t *this);
/**
* Clone the IP packet
*
* @return clone of the packet
*/
ip_packet_t *(*clone)(ip_packet_t *this);
/**
* Destroy an ip_packet_t
*/
void (*destroy)(ip_packet_t *this);
};
/**
* Create an IP packet out of data from the wire (or decapsulated from another
* packet).
*
* @note The raw IP packet gets either owned by the new object, or destroyed,
* if the data is invalid.
*
* @param packet the IP packet (including header), gets owned
* @return ip_packet_t instance, or NULL if invalid
*/
ip_packet_t *ip_packet_create(chunk_t packet);
/**
* Encode an IP packet from the given data.
*
* If src and/or dst have ports set they are applied to UDP/TCP headers found
* in the packet.
*
* @param src source address and optional port (cloned)
* @param dst destination address and optional port (cloned)
* @param next_header the protocol (IPv4) or next header (IPv6)
* @param data complete data after basic IP header (cloned)
* @return ip_packet_t instance, or NULL if invalid
*/
ip_packet_t *ip_packet_create_from_data(host_t *src, host_t *dst,
uint8_t next_header, chunk_t data);
/**
* Encode a UDP packet from the given data.
*
* @param src source address and port (cloned)
* @param dst destination address and port (cloned)
* @param data UDP data (cloned)
* @return ip_packet_t instance, or NULL if invalid
*/
ip_packet_t *ip_packet_create_udp_from_data(host_t *src, host_t *dst,
chunk_t data);
#endif /** IP_PACKET_H_ @}*/
|