1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2015 - 2018 Intel Corporation 4 * All rights reserved. 5 */ 6 #ifndef UTIL_IO_H 7 #define UTIL_IO_H 8 9 #ifdef _WIN32 10 #include <BaseTsd.h> 11 #include <winsock2.h> 12 #include <ws2tcpip.h> 13 typedef SSIZE_T ssize_t; 14 #define _HOST_NAME_MAX MAX_COMPUTERNAME_LENGTH 15 16 #else 17 #include <arpa/inet.h> 18 #include <errno.h> 19 #include <sys/socket.h> 20 #include <sys/un.h> 21 #define _HOST_NAME_MAX _POSIX_HOST_NAME_MAX 22 #define SOCKET int 23 #define INVALID_SOCKET - 1 24 #define SOCKET_ERROR - 1 25 #endif 26 27 #include "tss2_tpm2_types.h" 28 29 #ifdef _WIN32 30 #define TEMP_RETRY(dest, exp) \ 31 { int __ret; \ 32 do { \ 33 __ret = exp; \ 34 } while (__ret == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); \ 35 dest = __ret; } 36 #else 37 #define TEMP_RETRY(dest, exp) \ 38 { int __ret; \ 39 do { \ 40 __ret = exp; \ 41 } while (__ret == SOCKET_ERROR && errno == EINTR); \ 42 dest =__ret; } 43 #endif 44 45 #ifdef __cplusplus 46 extern "C" { 47 #endif 48 49 /* 50 * Read 'size' bytes from file descriptor 'fd' into buffer 'buf'. Additionally 51 * this function will retry calls to the 'read' function when temporary errors 52 * are detected. This is currently limited to interrupted system calls and 53 * short reads. 54 */ 55 ssize_t 56 read_all ( 57 SOCKET fd, 58 uint8_t *data, 59 size_t size); 60 /* 61 * Write 'size' bytes from 'buf' to file descriptor 'fd'. Additionally this 62 * function will retry calls to the 'write' function when recoverable errors 63 * are detected. This is currently limited to interrupted system calls and 64 * short writes. 65 */ 66 ssize_t 67 write_all ( 68 SOCKET fd, 69 const uint8_t *buf, 70 size_t size); 71 TSS2_RC 72 socket_connect ( 73 const char *hostname, 74 uint16_t port, 75 SOCKET *socket); 76 TSS2_RC 77 socket_close ( 78 SOCKET *socket); 79 ssize_t 80 socket_recv_buf ( 81 SOCKET sock, 82 uint8_t *data, 83 size_t size); 84 TSS2_RC 85 socket_xmit_buf ( 86 SOCKET sock, 87 const void *buf, 88 size_t size); 89 90 #ifdef __cplusplus 91 } 92 #endif 93 #endif /* UTIL_IO_H */ 94