From 0268794fe33d4889702ef4a72abb513bb30a77ef Mon Sep 17 00:00:00 2001 From: Ronnie Sahlberg Date: Sun, 26 Jun 2011 18:30:26 +1000 Subject: [PATCH] add capability to read UDP datagrams from the socket and pass them to the parser --- lib/socket.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/socket.c b/lib/socket.c index 2c77ef6..ffccd27 100644 --- a/lib/socket.c +++ b/lib/socket.c @@ -111,6 +111,29 @@ static int rpc_read_from_socket(struct rpc_context *rpc) return -1; } + if (rpc->is_udp) { + char *buf; + socklen_t socklen = sizeof(rpc->udp_src); + + buf = malloc(available); + if (buf == NULL) { + rpc_set_error(rpc, "Failed to malloc buffer for recvfrom"); + return -1; + } + count = recvfrom(rpc->fd, buf, available, MSG_DONTWAIT, (struct sockaddr *)&rpc->udp_src, &socklen); + if (count < 0) { + rpc_set_error(rpc, "Failed recvfrom: %s", strerror(errno)); + free(buf); + } + if (rpc_process_pdu(rpc, buf, count) != 0) { + rpc_set_error(rpc, "Invalid/garbage pdu received from server. Ignoring PDU"); + free(buf); + return -1; + } + free(buf); + return 0; + } + /* read record marker, 4 bytes at the beginning of every pdu */ if (rpc->inbuf == NULL) { rpc->insize = 4; -- 2.34.1