-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsock.c
148 lines (130 loc) · 4.01 KB
/
sock.c
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* ========================================================================= */
/**
* @file sock.c
*
* @copyright
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "def.h"
#include "log.h"
#include "sock.h"
#include "time.h"
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <poll.h>
#include <unistd.h>
#include <sys/types.h>
/* == Methods ============================================================== */
/* ------------------------------------------------------------------------- */
bool bs_sock_set_blocking(int fd, bool blocking)
{
int flags, rv;
flags = fcntl(fd, F_GETFL);
if (-1 == flags) {
bs_log(BS_ERROR | BS_ERRNO, "Failed fcntl(%d, F_GETFL)", fd);
return false;
}
if (blocking) {
flags &= ~O_NONBLOCK;
} else {
flags |= O_NONBLOCK;
}
rv = fcntl(fd, F_SETFL, flags);
if (0 != rv) {
bs_log(BS_ERROR | BS_ERRNO,
"Failed fcntl(%d, F_SETFL, 0x%x)", fd, flags);
return false;
}
return true;
}
/* ------------------------------------------------------------------------- */
int bs_sock_poll_read(int fd, int msec)
{
struct pollfd poll_fd;
int rv;
poll_fd.fd = fd;
poll_fd.events = POLLIN;
rv = poll(&poll_fd, 1, msec);
if (0 > rv) {
rv = errno;
bs_log(BS_ERROR | BS_ERRNO, "Failed poll(%d, 1, %d)", fd, msec);
errno = rv;
return -1;
}
return rv;
}
/* ------------------------------------------------------------------------- */
ssize_t bs_sock_read(int fd, void *buf_ptr, size_t count, int msec)
{
ssize_t consumed_bytes, read_bytes;
int rv;
uint8_t *byte_buf_ptr;
uint64_t start_msec;
int timeout;
/* catch boundary conditions on count */
if (INT32_MAX < count) {
errno = EINVAL;
return -1;
}
if (0 >= count) {
return 0;
}
if (0 > msec) {
start_msec = bs_usec() / 1000;
} else {
start_msec = 0;
}
timeout = msec;
consumed_bytes = 0;
byte_buf_ptr = (uint8_t*)buf_ptr;
while ((size_t)consumed_bytes < count) {
if (0 <= msec) {
uint64_t now_msec = bs_usec() / 1000;
if (start_msec + msec > now_msec) {
timeout = BS_MIN(now_msec - (start_msec + msec),
(uint64_t)INT32_MAX);
} else {
timeout = 0;
}
}
rv = bs_sock_poll_read(fd, timeout);
if (0 > rv) {
return -1;
} else if (0 == rv) {
/* no more data */
return consumed_bytes;
}
read_bytes = read(fd, &byte_buf_ptr[consumed_bytes],
count - consumed_bytes);
if (0 == read_bytes) {
/* connection was closed, return prescribed error. */
errno = EPIPE;
return -1;
}
if ((0 > read_bytes) &&
(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)) {
rv = errno;
bs_log(BS_ERROR | BS_ERRNO, "Failed read(%d, %p, %zu)",
fd, &byte_buf_ptr[consumed_bytes], count - consumed_bytes);
errno = rv;
return -1;
}
consumed_bytes += read_bytes;
}
return consumed_bytes;
}
/* == End of sock.c ======================================================== */