From: Ronnie Sahlberg Date: Sun, 26 Jun 2011 08:30:26 +0000 (+1000) Subject: add capability to read UDP datagrams from the socket and pass them to the parser X-Git-Tag: upstream/1.9.6^2~376 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=0268794fe33d4889702ef4a72abb513bb30a77ef;p=deb_libnfs.git add capability to read UDP datagrams from the socket and pass them to the parser --- 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;