aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Brunner <tobias@strongswan.org>2014-07-15 17:32:25 +0200
committerTobias Brunner <tobias@strongswan.org>2014-07-22 11:10:35 +0200
commit16e519d42c807aa2397c77d5f294423fd4bafc0b (patch)
tree858759dfd4f1b153f67d1b5027f3c9dc59237a71
parent108a67893f0a32ecee44742c452964e03c0a6c63 (diff)
downloadstrongswan-16e519d42c807aa2397c77d5f294423fd4bafc0b.tar.bz2
strongswan-16e519d42c807aa2397c77d5f294423fd4bafc0b.tar.xz
ip_packet: Add function to easily encode UDP packets
-rw-r--r--src/libipsec/ip_packet.c18
-rw-r--r--src/libipsec/ip_packet.h11
2 files changed, 29 insertions, 0 deletions
diff --git a/src/libipsec/ip_packet.c b/src/libipsec/ip_packet.c
index 7de4658b9..f6e08c0cb 100644
--- a/src/libipsec/ip_packet.c
+++ b/src/libipsec/ip_packet.c
@@ -435,3 +435,21 @@ ip_packet_t *ip_packet_create_from_data(host_t *src, host_t *dst,
return NULL;
}
}
+
+/**
+ * Described in header.
+ */
+ip_packet_t *ip_packet_create_udp_from_data(host_t *src, host_t *dst,
+ chunk_t data)
+{
+ struct udphdr udp = {
+ .len = htons(8 + data.len),
+ .check = 0,
+ };
+ ip_packet_t *packet;
+
+ data = chunk_cat("cc", chunk_from_thing(udp), data);
+ packet = ip_packet_create_from_data(src, dst, IPPROTO_UDP, data);
+ chunk_free(&data);
+ return packet;
+}
diff --git a/src/libipsec/ip_packet.h b/src/libipsec/ip_packet.h
index 73721cfd1..fa38eac2c 100644
--- a/src/libipsec/ip_packet.h
+++ b/src/libipsec/ip_packet.h
@@ -115,4 +115,15 @@ ip_packet_t *ip_packet_create(chunk_t packet);
ip_packet_t *ip_packet_create_from_data(host_t *src, host_t *dst,
u_int8_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_ @}*/