]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
ip_packet: Add function to easily encode UDP packets
authorTobias Brunner <tobias@strongswan.org>
Tue, 15 Jul 2014 15:32:25 +0000 (17:32 +0200)
committerTobias Brunner <tobias@strongswan.org>
Tue, 22 Jul 2014 09:10:35 +0000 (11:10 +0200)
src/libipsec/ip_packet.c
src/libipsec/ip_packet.h

index 7de4658b9e69d221c9c33ab9a1f90ea47b067f2d..f6e08c0cb4f37e4c0ed12b15cf1a11ec55957d93 100644 (file)
@@ -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;
+}
index 73721cfd1d8655ca89d3a018e72b7537f69ac594..fa38eac2ccc69c4ea849386b7b84381521c0c7c1 100644 (file)
@@ -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_ @}*/