#include <string.h>
#ifdef _WIN32
# include <libpcapng/win_compat.h>
#else
# include <arpa/inet.h>
#endif
#include <time.h>
#include <libpcapng/protocols/udp.h>
#include <libpcapng/protocols/ntp.h>
void libpcapng_build_ntp_request(const uint8_t src_mac[6],
const uint8_t dst_mac[6],
uint32_t src_ip,
uint32_t dst_ip,
uint16_t src_port,
uint16_t dst_port,
uint8_t *frame_out,
size_t *frame_len)
{
struct libpcapng_ntp_hdr ntp;
memset(&ntp, 0, sizeof(ntp));
ntp.li_vn_mode = (0 << 6) | (4 << 3) | 3;
libpcapng_udp_packet_build(
src_mac,
dst_mac,
src_ip,
dst_ip,
src_port,
dst_port,
(uint8_t *)&ntp,
sizeof(ntp),
frame_out,
frame_len
);
}
void libpcapng_build_ntp_reply(const uint8_t src_mac[6],
const uint8_t dst_mac[6],
uint32_t src_ip,
uint32_t dst_ip,
uint16_t src_port,
uint16_t dst_port,
const struct libpcapng_ntp_hdr *request,
uint8_t *frame_out,
size_t *frame_len)
{
struct libpcapng_ntp_hdr ntp;
memset(&ntp, 0, sizeof(ntp));
ntp.li_vn_mode = (0 << 6) | (4 << 3) | 4;
ntp.orig_timestamp_secs = request->tx_timestamp_secs;
ntp.orig_timestamp_frac = request->tx_timestamp_frac;
#ifdef _WIN32
ntp.recv_timestamp_secs = htonl((uint32_t)time(NULL) + 2208988800UL);
#else
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
ntp.recv_timestamp_secs = htonl((uint32_t)ts.tv_sec + 2208988800UL);
#endif
ntp.recv_timestamp_frac = 0;
ntp.tx_timestamp_secs = ntp.recv_timestamp_secs;
ntp.tx_timestamp_frac = ntp.recv_timestamp_frac;
libpcapng_udp_packet_build(
src_mac,
dst_mac,
src_ip,
dst_ip,
src_port,
dst_port,
(uint8_t *)&ntp,
sizeof(ntp),
frame_out,
frame_len
);
}