libtcti-socket: Refactor tctiRecvBytes into socket module.

This removes some unnecessary layers of abstraction that either wasn't
getting used or was being used to no particular benefit. This includes
no functional changes other than preferring `read` over `recv` and
handling temporary failures / interrupted system calls properly. The
read calls are still blocking calls.

Signed-off-by: Philip Tricca <philip.b.tricca@intel.com>
diff --git a/Makefile-test.am b/Makefile-test.am
index b8627fe..dbe5c74 100644
--- a/Makefile-test.am
+++ b/Makefile-test.am
@@ -125,7 +125,7 @@
 
 test_unit_tcti_socket_CFLAGS  = $(CMOCKA_CFLAGS) $(AM_CFLAGS) $(URIPARSER_CFLAGS)
 test_unit_tcti_socket_LDADD   = $(CMOCKA_LIBS) $(libmarshal) $(URIPARSER_LIBS) $(libutil)
-test_unit_tcti_socket_LDFLAGS = -Wl,--wrap=connect,--wrap=recv,--wrap=read,--wrap=select,--wrap=write
+test_unit_tcti_socket_LDFLAGS = -Wl,--wrap=connect,--wrap=read,--wrap=select,--wrap=write
 test_unit_tcti_socket_SOURCES = tcti/platformcommand.c tcti/tcti_socket.c \
     tcti/sockets.c tcti/sockets.h test/unit/tcti-socket.c
 
diff --git a/tcti/sockets.c b/tcti/sockets.c
index 1e857f8..557a496 100644
--- a/tcti/sockets.c
+++ b/tcti/sockets.c
@@ -31,36 +31,37 @@
 #include <unistd.h>
 
 #include "sockets.h"
+#include "tcti.h"
 #define LOGMODULE tcti
 #include "log/log.h"
 
 void WSACleanup() {}
 int WSAGetLastError() { return errno; }
-int wasInterrupted() { return errno == EINTR; }
 
-TSS2_RC recvBytes( SOCKET tpmSock, unsigned char *data, int len )
+ssize_t
+socket_recv_buf (
+    SOCKET sock,
+    unsigned char *data,
+    size_t size)
 {
-    int iResult = 0;
-    int length;
-    int bytesRead;
+    ssize_t recvd;
+    size_t recvd_total = 0;
 
-    for( bytesRead = 0, length = len; bytesRead != len; )
-    {
-        iResult = recv( tpmSock, (char *)&( data[bytesRead] ), length, 0);
-        if (iResult == SOCKET_ERROR)
-        {
-            if (wasInterrupted())
-                continue;
-            return TSS2_TCTI_RC_IO_ERROR;
+    LOG_DEBUG ("reading %zu bytes from socket %d to buffer at 0x%" PRIxPTR,
+               size, sock, (uintptr_t)data);
+    do {
+        recvd = TEMP_RETRY (read (sock, &data [recvd_total], size));
+        if (recvd < 0) {
+            LOG_WARNING ("read on socket %d failed with errno %d: %s",
+                         sock, errno, strerror (errno));
+            return recvd_total;
         }
-        else if (!iResult)
-            return TSS2_TCTI_RC_IO_ERROR;
+        LOGBLOB_DEBUG (&data [recvd_total], recvd, "read %zd bytes from socket %d:", recvd, sock);
+        recvd_total += recvd;
+        size -= recvd;
+    } while (size > 0);
 
-        length -= iResult;
-        bytesRead += iResult;
-    }
-
-    return TSS2_RC_SUCCESS;
+    return recvd_total;
 }
 
 TSS2_RC
diff --git a/tcti/sockets.h b/tcti/sockets.h
index ec188c8..4cfb649 100644
--- a/tcti/sockets.h
+++ b/tcti/sockets.h
@@ -26,7 +26,11 @@
 TSS2_RC
 socket_close (
     SOCKET *socket);
-TSS2_RC recvBytes( SOCKET tpmSock, unsigned char *data, int len );
+ssize_t
+socket_recv_buf (
+    SOCKET sock,
+    unsigned char *data,
+    size_t size);
 
 #ifdef __cplusplus
 }
diff --git a/tcti/tcti_socket.c b/tcti/tcti_socket.c
index 0445b03..7163791 100644
--- a/tcti/tcti_socket.c
+++ b/tcti/tcti_socket.c
@@ -46,25 +46,6 @@
 #define TCTI_SOCKET_DEFAULT_CONF "tcp://127.0.0.1:2321"
 #define TCTI_SOCKET_DEFAULT_PORT 2321
 
-static TSS2_RC tctiRecvBytes (
-    TSS2_TCTI_CONTEXT *tctiContext,
-    SOCKET sock,
-    unsigned char *data,
-    int len
-    )
-{
-    TSS2_RC result = 0;
-    result = recvBytes (sock, data, len);
-    if ((INT32)result == SOCKET_ERROR) {
-        LOG_ERROR("In recvBytes, recv failed (socket: 0x%x) with error: %d",
-                  sock, WSAGetLastError ());
-        return TSS2_TCTI_RC_IO_ERROR;
-    }
-    LOGBLOB_DEBUG(data, len, "Receive Bytes from socket #0x%x:", sock);
-
-    return TSS2_RC_SUCCESS;
-}
-
 static TSS2_RC xmit_buf (
     SOCKET sock,
     const void *buf,
@@ -334,15 +315,16 @@
 
     if (tcti_intel->status.protocolResponseSizeReceived != 1) {
         /* Receive the size of the response. */
-        rval = tctiRecvBytes (tctiContext,
-                              tcti_intel->tpmSock,
-                              (unsigned char *)&tcti_intel->responseSize,
-                              4);
-        if (rval != TSS2_RC_SUCCESS) {
+        iResult = socket_recv_buf (tcti_intel->tpmSock,
+                                   (unsigned char *)&tcti_intel->responseSize,
+                                   4);
+        if (iResult != 4) {
+            rval = TSS2_TCTI_RC_IO_ERROR;
             goto retSocketReceiveTpmResponse;
         }
 
         tcti_intel->responseSize = BE_TO_HOST_32 (tcti_intel->responseSize);
+        LOG_DEBUG ("response size: %" PRIu32, tcti_intel->responseSize);
         tcti_intel->status.protocolResponseSizeReceived = 1;
     }
 
@@ -360,11 +342,11 @@
         if (*response_size >= sizeof (TPM2_ST) &&
             tcti_intel->status.tagReceived == 0)
         {
-            rval = tctiRecvBytes (tctiContext,
-                                  tcti_intel->tpmSock,
-                                  (unsigned char *)&tcti_intel->tag,
-                                  2);
-            if (rval != TSS2_RC_SUCCESS) {
+            iResult = socket_recv_buf (tcti_intel->tpmSock,
+                                       (unsigned char *)&tcti_intel->tag,
+                                       2);
+            if (iResult < 2) {
+                rval = TSS2_TCTI_RC_IO_ERROR;
                 goto retSocketReceiveTpmResponse;
             } else {
                 tcti_intel->status.tagReceived = 1;
@@ -375,11 +357,11 @@
         if (*response_size >= (sizeof (TPM2_ST) + sizeof (TPM2_RC)) &&
             tcti_intel->status.responseSizeReceived == 0)
         {
-            rval = tctiRecvBytes (tctiContext,
-                                  tcti_intel->tpmSock,
-                                  (unsigned char *)&tcti_intel->responseSize,
-                                  4);
-            if (rval != TSS2_RC_SUCCESS) {
+            iResult = socket_recv_buf (tcti_intel->tpmSock,
+                                       (unsigned char *)&tcti_intel->responseSize,
+                                       4);
+            if (iResult != 4) {
+                rval = TSS2_TCTI_RC_IO_ERROR;
                 goto retSocketReceiveTpmResponse;
             } else {
                 tcti_intel->responseSize = BE_TO_HOST_32 (tcti_intel->responseSize);
@@ -407,22 +389,22 @@
         }
 
         /* Receive the TPM response. */
-        rval = tctiRecvBytes (tctiContext,
-                              tcti_intel->tpmSock,
-                              (unsigned char *)response_buffer,
-                              tcti_intel->responseSize - responseSizeDelta);
-        if (rval != TSS2_RC_SUCCESS) {
+        iResult = socket_recv_buf (tcti_intel->tpmSock,
+                                   (unsigned char *)response_buffer,
+                                   tcti_intel->responseSize - responseSizeDelta);
+        if (iResult < 0) {
+            rval = TSS2_TCTI_RC_IO_ERROR;
             goto retSocketReceiveTpmResponse;
         }
         LOGBLOB_DEBUG(response_buffer, tcti_intel->responseSize,
             "Received response buffer=");
 
         /* Receive the appended four bytes of 0's */
-        rval = tctiRecvBytes (tctiContext,
-                              tcti_intel->tpmSock,
-                              (unsigned char *)&trash,
-                              4);
-        if (rval != TSS2_RC_SUCCESS) {
+        iResult = socket_recv_buf (tcti_intel->tpmSock,
+                                   (unsigned char *)&trash,
+                                   4);
+        if (iResult != 4) {
+            rval = TSS2_TCTI_RC_IO_ERROR;
             goto retSocketReceiveTpmResponse;
         }
     }
diff --git a/test/unit/tcti-socket.c b/test/unit/tcti-socket.c
index e94664c..f6dd29a 100644
--- a/test/unit/tcti-socket.c
+++ b/test/unit/tcti-socket.c
@@ -147,18 +147,6 @@
  * a buffer to copy data from to return to the caller.
  */
 ssize_t
-__wrap_recv (int sockfd,
-             void *buf,
-             size_t len,
-             int flags)
-{
-    ssize_t  ret = mock_type (ssize_t);
-    uint8_t *buf_in = mock_ptr_type (uint8_t*);
-
-    memcpy (buf, buf_in, ret);
-    return ret;
-}
-ssize_t
 __wrap_read (int sockfd,
              void *buf,
              size_t len)
@@ -277,20 +265,20 @@
     /* select returns 1 fd ready for recv-ing */
     will_return (__wrap_select, 1);
     /* receive response size */
-    will_return (__wrap_recv, 4);
-    will_return (__wrap_recv, &response_in [2]);
+    will_return (__wrap_read, 4);
+    will_return (__wrap_read, &response_in [2]);
     /* receive tag */
-    will_return (__wrap_recv, 2);
-    will_return (__wrap_recv, response_in);
+    will_return (__wrap_read, 2);
+    will_return (__wrap_read, response_in);
     /* receive size (again)  */
-    will_return (__wrap_recv, 4);
-    will_return (__wrap_recv, &response_in [2]);
+    will_return (__wrap_read, 4);
+    will_return (__wrap_read, &response_in [2]);
     /* receive the rest of the command */
-    will_return (__wrap_recv, 0xc - sizeof (TPM2_ST) - sizeof (UINT32));
-    will_return (__wrap_recv, &response_in [6]);
+    will_return (__wrap_read, 0xc - sizeof (TPM2_ST) - sizeof (UINT32));
+    will_return (__wrap_read, &response_in [6]);
     /* receive the 4 bytes of 0's appended by the simulator */
-    will_return (__wrap_recv, 4);
-    will_return (__wrap_recv, &response_in [12]);
+    will_return (__wrap_read, 4);
+    will_return (__wrap_read, &response_in [12]);
     /* platform command sends 4 bytes and receives the same */
     will_return (__wrap_write, 4);
     will_return (__wrap_read, 4);