dhcp/test.cpp

65 lines
3.8 KiB
C++

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include "udp4.h"
#include "ip4.h"
#include "dhcp4.h"
void dump_hex(void const * src, int length)
{
uint8_t const * u8 = reinterpret_cast<uint8_t const *>(src);
for (int i = 0; i < length; i++) {
printf("%02x ", u8[i]);
if (i % 16 == 15)
printf("\n");
}
printf("\n");
}
int main()
{
int udp_header = 8 + 339;
unsigned char payload[] = {0x2, 0x1, 0x6, 0x0, 0x12, 0x27, 0x67, 0xa8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc0, 0xa8, 0x1, 0xf0, 0xc0, 0xa8, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0xf4, 0x7b, 0x9, 0x99, 0x3e, 0x28, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x63, 0x82, 0x53, 0x63, 0x35, 0x1, 0x5, 0x36, 0x4, 0xc0, 0xa8, 0x1, 0x1, 0x33, 0x4, 0x0, 0x1, 0x51, 0x80, 0x3a, 0x4, 0x0, 0x0, 0xa8, 0xc0, 0x3b, 0x4, 0x0, 0x1, 0x27, 0x50, 0x1c, 0x4, 0xc0, 0xa8, 0x1, 0xff, 0x3, 0x4, 0xc0, 0xa8, 0x1, 0x1, 0xf, 0x3, 0x6c, 0x61, 0x6e, 0x1, 0x4, 0xff, 0xff, 0xff, 0x0, 0x6, 0x4, 0xc0, 0xa8, 0x1, 0x1, 0x7d, 0x28, 0x0, 0x0, 0xd, 0xe9, 0x23, 0x6, 0x8, 0x47, 0x52, 0x42, 0x45, 0x33, 0x33, 0x31, 0x43, 0x5, 0xf, 0x52, 0x30, 0x31, 0x43, 0x31, 0x4d, 0x32, 0x35, 0x34, 0x36, 0x30, 0x30, 0x32, 0x48, 0x45, 0x4, 0x6, 0x30, 0x30, 0x30, 0x46, 0x42, 0x33, 0xff};
uint8_t buf[16384];
struct in_addr srcA;
struct in_addr dstA;
inet_aton("192.168.1.1", &srcA);
inet_aton("192.168.1.240", &dstA);
int ip4_payload_length = (sizeof (payload)) + (sizeof (udp4::header));
int ip4_identification = 55179;
int offset = 0;
ip4::header * ip4_header = ip4::make_header(reinterpret_cast<void *>(&buf[offset]),
ip4_payload_length,
ip4_identification,
srcA.s_addr,
dstA.s_addr);
offset += (sizeof (ip4::header));
dump_hex(ip4_header, (sizeof (ip4::header)));
udp4::header * udp4_header = udp4::make_header(reinterpret_cast<void *>(&buf[offset]),
htons(67),
htons(68),
srcA.s_addr,
dstA.s_addr,
payload,
(sizeof (payload)));
offset += (sizeof (udp4::header));
dump_hex(udp4_header, (sizeof (udp4::header)));
uint32_t transaction_id = 0x122767a8;
int dhcp4_length = dhcp4::make_message(reinterpret_cast<void *>(&buf[offset]),
dhcp4::op::bootrequest,
transaction_id,
dhcp4::message_type::dhcprequest);
dump_hex(&buf[offset], dhcp4_length);
offset += dhcp4_length;
}