Skip to content

Commit c53d576

Browse files
committed
UDP input: Add support for getting size of next UDP datagram in BSD compatible way
1 parent 26bb4f9 commit c53d576

File tree

1 file changed

+16
-2
lines changed
  • src/plugins/input/udp

1 file changed

+16
-2
lines changed

src/plugins/input/udp/udp.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,20 +915,34 @@ static void
915915
process_socket(struct udp_data *instance, int sd)
916916
{
917917
const char *err_str;
918+
ssize_t ret;
918919

919920
// Get size of the message
920921
int msg_size;
922+
#if defined(__FreeBSD__) || defined(__OpenBSD__)
923+
// ioctl(FIONREAD) on Linux returns the size of the next datagram available to read,
924+
// ioctl(FIONREAD) on *BSD returns the _total_ size of _all_ datagrams available to read
925+
ret = recv(sd, NULL, 0, MSG_PEEK | MSG_TRUNC);
926+
if (ret == -1) {
927+
ipx_strerror(errno, err_str);
928+
IPX_CTX_ERROR(instance->ctx, "Unable to get size of a next datagram. recv() failed: %s",
929+
err_str);
930+
return;
931+
}
932+
msg_size = ret;
933+
#else
921934
if (ioctl(sd, FIONREAD, &msg_size) == -1) {
922935
ipx_strerror(errno, err_str);
923936
IPX_CTX_ERROR(instance->ctx, "Unable to get size of a next datagram. ioctl() failed: %s",
924937
err_str);
925938
return;
926939
}
940+
#endif
927941

928942
if (msg_size < (int) sizeof(uint16_t) || msg_size > UINT16_MAX) {
929943
// Remove the malformed message from the buffer
930944
uint16_t mini_buffer;
931-
ssize_t ret = recvfrom(sd, &mini_buffer, sizeof(mini_buffer), 0, NULL, NULL);
945+
ret = recvfrom(sd, &mini_buffer, sizeof(mini_buffer), 0, NULL, NULL);
932946
if (ret == -1) {
933947
ipx_strerror(errno, err_str);
934948
IPX_CTX_WARNING(instance->ctx, "An error has occurred during reading a malformed "
@@ -950,7 +964,7 @@ process_socket(struct udp_data *instance, int sd)
950964
// Get the message
951965
struct sockaddr_storage addr;
952966
socklen_t addr_len = sizeof(addr);
953-
ssize_t ret = recvfrom(sd, buffer, (size_t) msg_size, 0, (struct sockaddr *) &addr, &addr_len);
967+
ret = recvfrom(sd, buffer, (size_t) msg_size, 0, (struct sockaddr *) &addr, &addr_len);
954968
if (ret == -1) {
955969
// Failed
956970
ipx_strerror(errno, err_str);

0 commit comments

Comments
 (0)