42 lines
1.3 KiB
C++
42 lines
1.3 KiB
C++
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include "ipv4.h"
|
|
#include "udp4.h"
|
|
|
|
namespace udp4 {
|
|
header * make_header(void * buf,
|
|
uint32_t source_port,
|
|
uint32_t destination_port,
|
|
uint32_t source_address,
|
|
uint32_t destination_address,
|
|
void const * payload,
|
|
int payload_length)
|
|
{
|
|
memset(buf, 0, (sizeof (header)));
|
|
header * hdr = reinterpret_cast<header *>(buf);
|
|
|
|
hdr->source_port = source_port;
|
|
hdr->destination_port = destination_port;
|
|
hdr->length = htons(payload_length + (sizeof (header)));
|
|
|
|
ipv4_psuedo_header phdr;
|
|
phdr.source_address = source_address;
|
|
phdr.destination_address = destination_address;
|
|
phdr.zeros = 0;
|
|
phdr.protocol = ipv4::protocol::udp;
|
|
phdr.udp_length = hdr->length; // already big endian
|
|
|
|
uint32_t sum = 0;
|
|
sum = ipv4::checksum_partial(sum, reinterpret_cast<void const *>(&phdr), (sizeof (ipv4_psuedo_header)));
|
|
sum = ipv4::checksum_partial(sum, reinterpret_cast<void const *>(hdr), (sizeof (header)));
|
|
sum = ipv4::checksum_partial(sum, reinterpret_cast<void const *>(payload), payload_length);
|
|
|
|
hdr->checksum = htons(ipv4::checksum_finish(sum));
|
|
|
|
return hdr;
|
|
}
|
|
}
|