aboutsummaryrefslogtreecommitdiff
path: root/src/util/io.h
blob: 7586e07a74dbdede89f26cc214e2e82a2216109d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* SPDX-License-Identifier: BSD-2-Clause */
/*
 * Copyright (c) 2015 - 2018 Intel Corporation
 * All rights reserved.
 */
#ifndef UTIL_IO_H
#define UTIL_IO_H

#ifdef _WIN32
#include <basetsd.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#ifdef _MSC_VER
typedef SSIZE_T ssize_t;
#endif
#define _HOST_NAME_MAX MAX_COMPUTERNAME_LENGTH

#else
#include <arpa/inet.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#define _HOST_NAME_MAX _POSIX_HOST_NAME_MAX
#define SOCKET int
#define INVALID_SOCKET - 1
#define SOCKET_ERROR - 1
#endif

#include "tss2_tpm2_types.h"

#ifdef _WIN32
#define TEMP_RETRY(dest, exp) \
{   int __ret; \
    do { \
        __ret = exp; \
    } while (__ret == SOCKET_ERROR && WSAGetLastError() == WSAEINTR); \
    dest = __ret; }
#else
#define TEMP_RETRY(dest, exp) \
{   int __ret; \
    do { \
        __ret = exp; \
    } while (__ret == SOCKET_ERROR && errno == EINTR); \
    dest =__ret; }
#endif

#ifdef __cplusplus
extern "C" {
#endif

/*
 * Read 'size' bytes from file descriptor 'fd' into buffer 'buf'. Additionally
 * this function will retry calls to the 'read' function when temporary errors
 * are detected. This is currently limited to interrupted system calls and
 * short reads.
 */
ssize_t
read_all (
    SOCKET fd,
    uint8_t *data,
    size_t size);
/*
 * Write 'size' bytes from 'buf' to file descriptor 'fd'. Additionally this
 * function will retry calls to the 'write' function when recoverable errors
 * are detected. This is currently limited to interrupted system calls and
 * short writes.
 */
ssize_t
write_all (
    SOCKET fd,
    const uint8_t *buf,
    size_t size);
TSS2_RC
socket_connect (
    const char *hostname,
    uint16_t port,
    SOCKET *socket);
TSS2_RC
socket_close (
    SOCKET *socket);
ssize_t
socket_recv_buf (
    SOCKET sock,
    uint8_t *data,
    size_t size);
TSS2_RC
socket_xmit_buf (
    SOCKET sock,
    const void *buf,
    size_t size);

#ifdef __cplusplus
}
#endif
#endif /* UTIL_IO_H */