Debian Codegraph @

c/src/reactor/io/posix/io.c

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include "reactor/io.h"
23 #include "reactor/selector.h"
24 #include "platform/platform.h" // pn_i_error_from_errno
25
26 #include <proton/object.h>
27
28 #include <ctype.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
35 #include <netdb.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <assert.h>
39
40 #define MAX_HOST (1024)
41 #define MAX_SERV (64)
42
43 struct pn_io_t {
44 char host[MAX_HOST];
45 char serv[MAX_SERV];
46 pn_error_t *error;
47 pn_selector_t *selector;
48 bool wouldblock;
49 };
50
51 void pn_io_initialize(void *obj)
52 {
53 pn_io_t *io = (pn_io_t *) obj;
54 io->error = pn_error();
55 io->wouldblock = false;
56 io->selector = NULL;
57 }
58
59 void pn_io_finalize(void *obj)
60 {
61 pn_io_t *io = (pn_io_t *) obj;
62 pn_error_free(io->error);
63 }
64
65 #define pn_io_hashcode NULL
66 #define pn_io_compare NULL
67 #define pn_io_inspect NULL
68
69 pn_io_t *pn_io(void)
70 {
71 static const pn_class_t clazz = PN_CLASS(pn_io);
72 pn_io_t *io = (pn_io_t *) pn_class_new(&clazz, sizeof(pn_io_t));
73 return io;
74 }
75
76 void pn_io_free(pn_io_t *io)
77 {
78 pn_free(io);
79 }
80
81 pn_error_t *pn_io_error(pn_io_t *io)
82 {
83 assert(io);
84 return io->error;
85 }
86
87 int pn_pipe(pn_io_t *io, pn_socket_t *dest)
88 {
89 int n = pipe(dest);
90 if (n) {
91 pn_i_error_from_errno(io->error, "pipe");
92 }
93
94 return n;
95 }
96
97 static void pn_configure_sock(pn_io_t *io, pn_socket_t sock) {
98 // this would be nice, but doesn't appear to exist on linux
99 /*
100 int set = 1;
101 if (!setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int))) {
102 pn_i_error_from_errno(io->error, "setsockopt");
103 };
104 */
105
106 int flags = fcntl(sock, F_GETFL);
107 flags |= O_NONBLOCK;
108
109 if (fcntl(sock, F_SETFL, flags) < 0) {
110 pn_i_error_from_errno(io->error, "fcntl");
111 }
112
113 //
114 // Disable the Nagle algorithm on TCP connections.
115 //
116 // Note: It would be more correct for the "level" argument to be SOL_TCP. However, there
117 // are portability issues with this macro so we use IPPROTO_TCP instead.
118 //
119 int tcp_nodelay = 1;
120 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*) &tcp_nodelay, sizeof(tcp_nodelay)) < 0) {
121 pn_i_error_from_errno(io->error, "setsockopt");
122 }
123 }
124
125 static inline int pn_create_socket(int af, int protocol);
126
127 pn_socket_t pn_listen(pn_io_t *io, const char *host, const char *port)
128 {
129 struct addrinfo *addr;
130 struct addrinfo hints = {0, AF_UNSPEC, SOCK_STREAM};
131 int code = getaddrinfo(host, port, &hints, &addr);
132 if (code) {
133 pn_error_format(io->error, PN_ERR, "getaddrinfo(%s, %s): %s\n", host, port, gai_strerror(code));
134 return PN_INVALID_SOCKET;
135 }
136
137 pn_socket_t sock = pn_create_socket(addr->ai_family, addr->ai_protocol);
138 if (sock == PN_INVALID_SOCKET) {
139 freeaddrinfo(addr);
140 pn_i_error_from_errno(io->error, "pn_create_socket");
141 return PN_INVALID_SOCKET;
142 }
143
144 int optval = 1;
145 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) {
146 pn_i_error_from_errno(io->error, "setsockopt");
147 freeaddrinfo(addr);
148 close(sock);
149 return PN_INVALID_SOCKET;
150 }
151
152 if (bind(sock, addr->ai_addr, addr->ai_addrlen) == -1) {
153 pn_i_error_from_errno(io->error, "bind");
154 freeaddrinfo(addr);
155 close(sock);
156 return PN_INVALID_SOCKET;
157 }
158
159 freeaddrinfo(addr);
160
161 if (listen(sock, 50) == -1) {
162 pn_i_error_from_errno(io->error, "listen");
163 close(sock);
164 return PN_INVALID_SOCKET;
165 }
166
167 return sock;
168 }
169
170 pn_socket_t pn_connect(pn_io_t *io, const char *host, const char *port)
171 {
172 struct addrinfo *addr;
173 struct addrinfo hints = {0, AF_UNSPEC, SOCK_STREAM};
174 int code = getaddrinfo(host, port, &hints, &addr);
175 if (code) {
176 pn_error_format(io->error, PN_ERR, "getaddrinfo(%s, %s): %s", host, port, gai_strerror(code));
177 return PN_INVALID_SOCKET;
178 }
179
180 pn_socket_t sock = pn_create_socket(addr->ai_family, addr->ai_protocol);
181 if (sock == PN_INVALID_SOCKET) {
182 pn_i_error_from_errno(io->error, "pn_create_socket");
183 freeaddrinfo(addr);
184 return PN_INVALID_SOCKET;
185 }
186
187 pn_configure_sock(io, sock);
188
189 if (connect(sock, addr->ai_addr, addr->ai_addrlen) == -1) {
190 if (errno != EINPROGRESS) {
191 pn_i_error_from_errno(io->error, "connect");
192 freeaddrinfo(addr);
193 close(sock);
194 return PN_INVALID_SOCKET;
195 }
196 }
197
198 freeaddrinfo(addr);
199
200 return sock;
201 }
202
203 pn_socket_t pn_accept(pn_io_t *io, pn_socket_t socket, char *name, size_t size)
204 {
205 struct sockaddr_storage addr;
206 socklen_t addrlen = sizeof(addr);
207 *name = '\0';
208 pn_socket_t sock = accept(socket, (struct sockaddr *) &addr, &addrlen);
209 if (sock == PN_INVALID_SOCKET) {
210 pn_i_error_from_errno(io->error, "accept");
211 return sock;
212 } else {
213 int code;
214 if ((code = getnameinfo((struct sockaddr *) &addr, addrlen, io->host, MAX_HOST, io->serv, MAX_SERV, 0))) {
215 pn_error_format(io->error, PN_ERR, "getnameinfo: %s\n", gai_strerror(code));
216 if (close(sock) == -1)
217 pn_i_error_from_errno(io->error, "close");
218 return PN_INVALID_SOCKET;
219 } else {
220 pn_configure_sock(io, sock);
221 snprintf(name, size, "%s:%s", io->host, io->serv);
222 return sock;
223 }
224 }
225 }
226
227 /* Abstract away turning off SIGPIPE */
228 #ifdef MSG_NOSIGNAL
229 ssize_t pn_send(pn_io_t *io, pn_socket_t socket, const void *buf, size_t len) {
230 ssize_t count = send(socket, buf, len, MSG_NOSIGNAL);
231 io->wouldblock = (errno == EAGAIN || errno == EWOULDBLOCK);
232 if (count < 0) { pn_i_error_from_errno(io->error, "send"); }
233 return count;
234 }
235
236 static inline int pn_create_socket(int af, int protocol) {
237 return socket(af, SOCK_STREAM, protocol);
238 }
239 #elif defined(SO_NOSIGPIPE)
240 ssize_t pn_send(pn_io_t *io, pn_socket_t socket, const void *buf, size_t size) {
241 ssize_t count = send(socket, buf, size, 0);
242 io->wouldblock = (errno == EAGAIN || errno == EWOULDBLOCK);
243 if (count < 0) { pn_i_error_from_errno(io->error, "send"); }
244 return count;
245 }
246
247 static inline int pn_create_socket(int af, int protocol) {
248 int sock;
249 sock = socket(af, SOCK_STREAM, protocol);
250 if (sock == -1) return sock;
251
252 int optval = 1;
253 if (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval)) == -1) {
254 close(sock);
255 return -1;
256 }
257 return sock;
258 }
259 #else
260
261 #include <signal.h>
262
263 static inline int pn_create_socket(int af, int protocol) {
264 return socket(af, SOCK_STREAM, protocol);
265 }
266
267 static ssize_t nosigpipe_send(int fd, const void *buffer, size_t size) {
268 sigset_t pendingSignals, oldSignals, newSignals;
269 ssize_t count;
270 int sendErrno, sigmaskErr;
271
272 sigpending(&pendingSignals);
273 int sigpipeIsPending = sigismember(&pendingSignals, SIGPIPE);
274 if (!sigpipeIsPending) {
275 sigemptyset(&newSignals);
276 sigaddset(&newSignals, SIGPIPE);
277 if (sigmaskErr = pthread_sigmask(SIG_BLOCK, (const sigset_t *)&newSignals, (sigset_t *)&oldSignals))
278 {
279 errno = sigmaskErr;
280 return -1;
281 }
282 }
283
284 count = send(fd, buffer, size, 0);
285 if (!sigpipeIsPending) {
286 sendErrno = errno;
287 if (count == -1 && errno == EPIPE) {
288 while (-1 == sigtimedwait(&newSignals, NULL, &(struct timespec){ 0, 0 }) && errno == EINTR)
289 ; //do nothing
290 }
291 if (sigmaskErr = pthread_sigmask(SIG_SETMASK, (const sigset_t *)&oldSignals, (sigset_t *)NULL))
292 {
293 errno = sigmaskErr;
294 return -1;
295 }
296 errno = sendErrno;
297 }
298 return count;
299 }
300
301 ssize_t pn_send(pn_io_t *io, pn_socket_t socket, const void *buf, size_t size) {
302 ssize_t count = nosigpipe_send(socket, buf, size);
303 io->wouldblock = (errno == EAGAIN || errno == EWOULDBLOCK);
304 if (count < 0) { pn_i_error_from_errno(io->error, "send"); }
305 return count;
306 }
307 #endif
308
309 ssize_t pn_recv(pn_io_t *io, pn_socket_t socket, void *buf, size_t size)
310 {
311 ssize_t count = recv(socket, buf, size, 0);
312 io->wouldblock = count < 0 && (errno == EAGAIN || errno == EWOULDBLOCK);
313 if (count < 0) { pn_i_error_from_errno(io->error, "recv"); }
314 return count;
315 }
316
317 ssize_t pn_write(pn_io_t *io, pn_socket_t socket, const void *buf, size_t size)
318 {
319 return write(socket, buf, size);
320 }
321
322 ssize_t pn_read(pn_io_t *io, pn_socket_t socket, void *buf, size_t size)
323 {
324 return read(socket, buf, size);
325 }
326
327 void pn_close(pn_io_t *io, pn_socket_t socket)
328 {
329 close(socket);
330 }
331
332 bool pn_wouldblock(pn_io_t *io)
333 {
334 return io->wouldblock;
335 }
336
337 pn_selector_t *pn_io_selector(pn_io_t *io)
338 {
339 if (io->selector == NULL)
340 io->selector = pni_selector();
341 return io->selector;
342 }