X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=net.c;h=f01cf5830e84eafa5e88890f5f8d9a68de4d0316;hp=c7bb8b4b71293ee89964b55175c2170fb10c3e31;hb=f77f0e0341220e10d1732404346bd2c1fe2c6835;hpb=d81f6efa05b305c9a8655124b001ea71e1c86679 diff --git a/net.c b/net.c index c7bb8b4b..f01cf583 100644 --- a/net.c +++ b/net.c @@ -95,36 +95,28 @@ void init_sockaddr(struct sockaddr_in *addr, int port, const struct hostent *he) } /* - * send out a buffer, resend on short writes + * Send out a buffer, resend on short writes. * - * \param fd the file descriptor - * \param buf The buffer to be sent - * \param len The length of \a buf - * - * Due to circumstances beyond your control, the kernel might not send all the - * data out in one chunk, and now, my friend, it's up to us to get the data out - * there (Beej's Guide to Network Programming). + * \param fd The file descriptor. + * \param buf The buffer to be sent. + * \param len The length of \a buf. * - * \return This function returns 1 on success and \a -E_SEND on errors. The - * number of bytes actually sent is stored upon successful return in \a len. + * \return Standard. In any case, the number of bytes actually sent is stored + * in \a len. */ static int sendall(int fd, const char *buf, size_t *len) { - size_t total = 0, bytesleft = *len; /* how many we have left to send */ - int n = -1; - - while (total < *len) { - n = send(fd, buf + total, bytesleft, 0); - if (n == -1) - break; - total += n; - bytesleft -= n; - if (total < *len) - PARA_DEBUG_LOG("short write (%zd byte(s) left)\n", - *len - total); + size_t total = *len; + + assert(total); + *len = 0; + while (*len < total) { + int ret = send(fd, buf + *len, total - *len, 0); + if (ret == -1) + return -ERRNO_TO_PARA_ERROR(errno); + *len += ret; } - *len = total; /* return number actually sent here */ - return n == -1? -E_SEND : 1; /* return 1 on success */ + return 1; } /** @@ -138,7 +130,7 @@ static int sendall(int fd, const char *buf, size_t *len) * out the buffer, encrypted or not, and try to resend the remaing part in case * of short writes. * - * \return Positive on success, \p -E_SEND on errors. + * \return Standard. */ int send_bin_buffer(int fd, const char *buf, size_t len) { @@ -162,14 +154,14 @@ int send_bin_buffer(int fd, const char *buf, size_t len) } /** - * encrypt and send null terminated buffer. + * Encrypt and send null terminated buffer. * - * \param fd the file descriptor - * \param buf the null-terminated buffer to be send + * \param fd The file descriptor. + * \param buf The null-terminated buffer to be send. * * This is equivalent to send_bin_buffer(fd, buf, strlen(buf)). * - * \return Positive on success, \p -E_SEND on errors. + * \return Standard. */ int send_buffer(int fd, const char *buf) { @@ -178,12 +170,12 @@ int send_buffer(int fd, const char *buf) /** - * send and encrypt a buffer given by a format string + * Send and encrypt a buffer given by a format string. * - * \param fd the file descriptor - * \param fmt a format string + * \param fd The file descriptor. + * \param fmt A format string. * - * \return Positive on success, \p -E_SEND on errors. + * \return Standard. */ __printf_2_3 int send_va_buffer(int fd, const char *fmt, ...) { @@ -203,12 +195,10 @@ __printf_2_3 int send_va_buffer(int fd, const char *fmt, ...) * \param buf the buffer to write the decrypted data to * \param size the size of \a buf * - * Receive at most \a size bytes from filedescriptor fd. If encryption is + * Receive at most \a size bytes from file descriptor \a fd. If encryption is * available, decrypt the received buffer. * - * \return The number of bytes received on success. On receive errors, -E_RECV - * is returned. On crypt errors, the corresponding crypt error number is - * returned. + * \return The number of bytes received on success, negative on errors. * * \sa recv(2) */ @@ -254,8 +244,7 @@ int recv_buffer(int fd, char *buf, size_t size) { int n; - if (!size) - return -E_RECV; + assert(size); n = recv_bin_buffer(fd, buf, size - 1); if (n >= 0) buf[n] = '\0';