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