ip_packet: Add function to easily encode UDP packets

This commit is contained in:
Tobias Brunner 2014-07-15 17:32:25 +02:00
parent 108a67893f
commit 16e519d42c
2 changed files with 29 additions and 0 deletions

View 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;
}

View 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_ @}*/