From: Miek Gieben Date: Wed, 2 Feb 2005 10:54:56 +0000 (+0000) Subject: added general read/write stuff for sockets. Lifted from NSD X-Git-Tag: release-0.50~466 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8f0e06a2fbcd994cd55892efbe0f6459854af43b;p=thirdparty%2Fldns.git added general read/write stuff for sockets. Lifted from NSD --- diff --git a/net.c b/net.c index 386c839b..bd081280 100644 --- a/net.c +++ b/net.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "util.h" @@ -212,6 +213,8 @@ ldns_send_tcp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t to return NULL; } + return NULL; + bytes = sendto(sockfd, ldns_buffer_begin(qbin), ldns_buffer_position(qbin), 0, (struct sockaddr *)to, tolen); @@ -262,3 +265,53 @@ ldns_send_tcp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t to return answer_pkt; } } + +/* + * Read SIZE bytes from the socket into BUF. Keep reading unless an + * error occurs (except for EINTR) or EOF is reached. + */ +static bool +read_socket(int s, void *buf, size_t size) +{ + char *data = buf; + size_t total_count = 0; + + while (total_count < size) { + ssize_t count = read(s, data + total_count, size - + total_count); + if (count == -1) { + if (errno != EINTR) { + return false; + } else { + continue; + } + } + total_count += count; + } + return true; +} + +/* + * Write the complete buffer to the socket, irrespective of short + * writes or interrupts. + */ +static bool +write_socket(int s, const void *buf, size_t size) +{ + const char *data = buf; + size_t total_count = 0; + + while (total_count < size) { + ssize_t count = write(s, data + total_count, size - + total_count); + if (count == -1) { + if (errno != EINTR) { + return false; + } else { + continue; + } + } + total_count += count; + } + return true; +}