Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ dhcp4relay/debian/*
!dhcp4relay/debian/compat
!dhcp4relay/debian/control
!dhcp4relay/debian/rules

# Build artifacts
*.buildinfo
*.changes
dhcp4relay/PcapPlusPlus-*/
dhcp4relay/pcappp.stamp
19 changes: 12 additions & 7 deletions dhcp4relay/src/dhcp4_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@
*
* @return boolean True if packet successfully sent
*/
void bootp_pad(uint8_t *out, const uint8_t *buffer, uint32_t *len, bool pad) {
if (pad && *len < BOOTP_MIN_LEN) {
memcpy(out, buffer, *len);
*len = BOOTP_MIN_LEN;
}
}

#ifndef UNIT_TEST
bool send_udp(int sock, uint8_t *buffer, struct sockaddr_in target, uint32_t len, in_addr src_ip, bool use_src_ip, bool pad) {
/* Pad additional bytes if length is lesser than 300
* to make DHCP packet length to minimum of 300 bytes */
if (pad && len < BOOTP_MIN_LEN) {
auto pad_len = BOOTP_MIN_LEN - len;
memset(buffer+len, 0, pad_len);
len = BOOTP_MIN_LEN;
}
uint8_t padded[BOOTP_MIN_LEN] = {};
uint32_t orig_len = len;
bootp_pad(padded, buffer, &len, pad);
if (len != orig_len)
buffer = padded;

if (use_src_ip && src_ip.s_addr != 0) {
// Enable IP_PKTINFO on the socket
Expand Down
21 changes: 20 additions & 1 deletion dhcp4relay/src/dhcp4_sender.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,33 @@
#include <string>

#define BOOTP_MIN_LEN 300

/**
* @brief Apply BOOTP minimum-length padding.
*
* Copies *buffer into out and updates *len to BOOTP_MIN_LEN when pad is true
* and *len < BOOTP_MIN_LEN. out must point to at least BOOTP_MIN_LEN
* zero-initialised bytes; the caller is responsible for switching to out as
* the send buffer when the returned length differs from the input length.
*
* Compiled in both production and UNIT_TEST builds so tests can exercise the
* padding logic directly without linking the real send_udp().
*
* @param out destination buffer (>= BOOTP_MIN_LEN bytes, zero-filled)
* @param buffer source packet bytes
* @param len packet length; updated to BOOTP_MIN_LEN if padding applied
* @param pad enable padding
*/
void bootp_pad(uint8_t *out, const uint8_t *buffer, uint32_t *len, bool pad);

/**
* @code bool send_udp(int sock, uint8_t *buffer, struct sockaddr_in target, uint32_t len, const char* src_ip, bool use_src_ip);
*
* @brief send udp packet and return true if successful
*
* @param *buffer message buffer
* @param sockaddr_in target target socket
* @param len length of message
* @param len length of message
* @param src_ip source IP address as string (optional)
* @param use_src_ip if true, use src_ip as source address
* @param pad if true, do padding
Expand Down
Loading
Loading