blob: 21c0bac9e928c06e877a97012cb6dc37c5b3e779 [file] [log] [blame]
Roger Meier6cf0ffc2014-04-05 00:45:42 +02001//
2// Licensed to the Apache Software Foundation (ASF) under one
3// or more contributor license agreements. See the NOTICE file
4// distributed with this work for additional information
5// regarding copyright ownership. The ASF licenses this file
6// to you under the Apache License, Version 2.0 (the
7// "License"); you may not use this file except in compliance
8// with the License. You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing,
13// software distributed under the License is distributed on an
14// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15// KIND, either express or implied. See the License for the
16// specific language governing permissions and limitations
17// under the License.
18//
19
20#include <sys/time.h>
21#include <sys/types.h>
22#include <arpa/inet.h>
23#include <netdb.h>
24#include <string.h>
25#include <unistd.h>
26#include <fcntl.h>
27#include <errno.h>
28
29#include <stdio.h> // TODO REMOVE
30
31#include "socket.h"
32
33////////////////////////////////////////////////////////////////////////////////
34// Private
35
36// Num seconds since Jan 1 1970 (UTC)
37#ifdef _WIN32
38// SOL
39#else
40 double __gettime() {
41 struct timeval v;
42 gettimeofday(&v, (struct timezone*) NULL);
43 return v.tv_sec + v.tv_usec/1.0e6;
44 }
45#endif
46
47#define WAIT_MODE_R 1
48#define WAIT_MODE_W 2
49#define WAIT_MODE_C (WAIT_MODE_R|WAIT_MODE_W)
50T_ERRCODE socket_wait(p_socket sock, int mode, int timeout) {
51 int ret = 0;
52 fd_set rfds, wfds;
53 struct timeval tv;
54 double end, t;
55 if (!timeout) {
56 return TIMEOUT;
57 }
58
59 end = __gettime() + timeout/1000;
60 do {
James E. King, III36200902016-10-05 14:47:18 -040061 FD_ZERO(&rfds);
62 FD_ZERO(&wfds);
63
Roger Meier6cf0ffc2014-04-05 00:45:42 +020064 // Specify what I/O operations we care about
65 if (mode & WAIT_MODE_R) {
Roger Meier6cf0ffc2014-04-05 00:45:42 +020066 FD_SET(*sock, &rfds);
67 }
68 if (mode & WAIT_MODE_W) {
Roger Meier6cf0ffc2014-04-05 00:45:42 +020069 FD_SET(*sock, &wfds);
70 }
71
72 // Check for timeout
73 t = end - __gettime();
74 if (t < 0.0) {
75 break;
76 }
77
78 // Wait
79 tv.tv_sec = (int)t;
80 tv.tv_usec = (int)((t - tv.tv_sec) * 1.0e6);
81 ret = select(*sock+1, &rfds, &wfds, NULL, &tv);
82 } while (ret == -1 && errno == EINTR);
83 if (ret == -1) {
84 return errno;
85 }
86
87 // Check for timeout
88 if (ret == 0) {
89 return TIMEOUT;
90 }
91
Roger Meier6cf0ffc2014-04-05 00:45:42 +020092 return SUCCESS;
93}
94
95////////////////////////////////////////////////////////////////////////////////
96// General
97
98T_ERRCODE socket_create(p_socket sock, int domain, int type, int protocol) {
99 *sock = socket(domain, type, protocol);
100 if (*sock > 0) {
101 return SUCCESS;
102 } else {
103 return errno;
104 }
105}
106
107T_ERRCODE socket_destroy(p_socket sock) {
108 // TODO Figure out if I should be free-ing this
109 if (*sock > 0) {
James E. King, III533405e2017-10-28 18:25:45 -0400110 (void)socket_setblocking(sock);
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200111 close(*sock);
112 *sock = -1;
113 }
114 return SUCCESS;
115}
116
117T_ERRCODE socket_bind(p_socket sock, p_sa addr, int addr_len) {
James E. King, III533405e2017-10-28 18:25:45 -0400118 int ret = socket_setblocking(sock);
119 if (ret != SUCCESS) {
120 return ret;
121 }
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200122 if (bind(*sock, addr, addr_len)) {
123 ret = errno;
124 }
James E. King, III533405e2017-10-28 18:25:45 -0400125 int ret2 = socket_setnonblocking(sock);
126 return ret == SUCCESS ? ret2 : ret;
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200127}
128
129T_ERRCODE socket_get_info(p_socket sock, short *port, char *buf, size_t len) {
James E. King, IIIea5ea8b2016-11-12 15:12:33 -0500130 struct sockaddr_storage sa;
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200131 memset(&sa, 0, sizeof(sa));
James E. King, III36200902016-10-05 14:47:18 -0400132 socklen_t addrlen = sizeof(sa);
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200133 int rc = getsockname(*sock, (struct sockaddr*)&sa, &addrlen);
134 if (!rc) {
James E. King, IIIea5ea8b2016-11-12 15:12:33 -0500135 if (sa.ss_family == AF_INET6) {
136 struct sockaddr_in6* sin = (struct sockaddr_in6*)(&sa);
137 if (!inet_ntop(AF_INET6, &sin->sin6_addr, buf, len)) {
138 return errno;
139 }
140 *port = ntohs(sin->sin6_port);
141 } else {
142 struct sockaddr_in* sin = (struct sockaddr_in*)(&sa);
143 if (!inet_ntop(AF_INET, &sin->sin_addr, buf, len)) {
144 return errno;
145 }
146 *port = ntohs(sin->sin_port);
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200147 }
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200148 return SUCCESS;
149 }
James E. King, IIIea5ea8b2016-11-12 15:12:33 -0500150 return errno;
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200151}
152
153////////////////////////////////////////////////////////////////////////////////
154// Server
155
156T_ERRCODE socket_accept(p_socket sock, p_socket client,
157 p_sa addr, socklen_t *addrlen, int timeout) {
158 int err;
159 if (*sock < 0) {
160 return CLOSED;
161 }
162 do {
163 *client = accept(*sock, addr, addrlen);
164 if (*client > 0) {
165 return SUCCESS;
166 }
James E. King, III533405e2017-10-28 18:25:45 -0400167 } while ((err = errno) == EINTR);
168
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200169 if (err == EAGAIN || err == ECONNABORTED) {
170 return socket_wait(sock, WAIT_MODE_R, timeout);
171 }
James E. King, III533405e2017-10-28 18:25:45 -0400172
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200173 return err;
174}
175
176T_ERRCODE socket_listen(p_socket sock, int backlog) {
James E. King, III533405e2017-10-28 18:25:45 -0400177 int ret = socket_setblocking(sock);
178 if (ret != SUCCESS) {
179 return ret;
180 }
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200181 if (listen(*sock, backlog)) {
182 ret = errno;
183 }
James E. King, III533405e2017-10-28 18:25:45 -0400184 int ret2 = socket_setnonblocking(sock);
185 return ret == SUCCESS ? ret2 : ret;
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200186}
187
188////////////////////////////////////////////////////////////////////////////////
189// Client
190
191T_ERRCODE socket_connect(p_socket sock, p_sa addr, int addr_len, int timeout) {
192 int err;
193 if (*sock < 0) {
194 return CLOSED;
195 }
196
197 do {
198 if (connect(*sock, addr, addr_len) == 0) {
199 return SUCCESS;
200 }
201 } while ((err = errno) == EINTR);
202 if (err != EINPROGRESS && err != EAGAIN) {
203 return err;
204 }
205 return socket_wait(sock, WAIT_MODE_C, timeout);
206}
207
208T_ERRCODE socket_send(
209 p_socket sock, const char *data, size_t len, int timeout) {
210 int err, put = 0;
211 if (*sock < 0) {
212 return CLOSED;
213 }
214 do {
215 put = send(*sock, data, len, 0);
216 if (put > 0) {
217 return SUCCESS;
218 }
James E. King, III533405e2017-10-28 18:25:45 -0400219 } while ((err = errno) == EINTR);
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200220
221 if (err == EAGAIN) {
222 return socket_wait(sock, WAIT_MODE_W, timeout);
223 }
James E. King, III533405e2017-10-28 18:25:45 -0400224
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200225 return err;
226}
227
228T_ERRCODE socket_recv(
229 p_socket sock, char *data, size_t len, int timeout, int *received) {
230 int err, got = 0;
231 if (*sock < 0) {
232 return CLOSED;
233 }
James E. King, III533405e2017-10-28 18:25:45 -0400234 *received = 0;
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200235
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200236 do {
237 got = recv(*sock, data, len, 0);
238 if (got > 0) {
239 *received = got;
240 return SUCCESS;
241 }
242 err = errno;
243
244 // Connection has been closed by peer
245 if (got == 0) {
246 return CLOSED;
247 }
James E. King, III533405e2017-10-28 18:25:45 -0400248 } while (err == EINTR);
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200249
250 if (err == EAGAIN) {
251 return socket_wait(sock, WAIT_MODE_R, timeout);
252 }
James E. King, III533405e2017-10-28 18:25:45 -0400253
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200254 return err;
255}
256
257////////////////////////////////////////////////////////////////////////////////
258// Util
259
James E. King, III533405e2017-10-28 18:25:45 -0400260T_ERRCODE socket_setnonblocking(p_socket sock) {
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200261 int flags = fcntl(*sock, F_GETFL, 0);
262 flags |= O_NONBLOCK;
James E. King, III533405e2017-10-28 18:25:45 -0400263 return fcntl(*sock, F_SETFL, flags) != -1 ? SUCCESS : errno;
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200264}
265
James E. King, III533405e2017-10-28 18:25:45 -0400266T_ERRCODE socket_setblocking(p_socket sock) {
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200267 int flags = fcntl(*sock, F_GETFL, 0);
268 flags &= (~(O_NONBLOCK));
James E. King, III533405e2017-10-28 18:25:45 -0400269 return fcntl(*sock, F_SETFL, flags) != -1 ? SUCCESS : errno;
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200270}
271
272////////////////////////////////////////////////////////////////////////////////
273// TCP
274
275#define ERRORSTR_RETURN(err) \
276 if (err == SUCCESS) { \
277 return NULL; \
278 } else if (err == TIMEOUT) { \
279 return TIMEOUT_MSG; \
280 } else if (err == CLOSED) { \
281 return CLOSED_MSG; \
282 } \
283 return strerror(err)
284
285const char * tcp_create(p_socket sock) {
Jeffrey Hane872b352020-09-24 10:41:12 -0700286 // TODO support IPv6
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200287 int err = socket_create(sock, AF_INET, SOCK_STREAM, 0);
288 ERRORSTR_RETURN(err);
289}
290
291const char * tcp_destroy(p_socket sock) {
292 int err = socket_destroy(sock);
293 ERRORSTR_RETURN(err);
294}
295
296const char * tcp_bind(p_socket sock, const char *host, unsigned short port) {
Jeffrey Hane872b352020-09-24 10:41:12 -0700297 // TODO support IPv6
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200298 int err;
299 struct hostent *h;
300 struct sockaddr_in local;
301 memset(&local, 0, sizeof(local));
302 local.sin_family = AF_INET;
303 local.sin_addr.s_addr = htonl(INADDR_ANY);
304 local.sin_port = htons(port);
305 if (strcmp(host, "*") && !inet_aton(host, &local.sin_addr)) {
306 h = gethostbyname(host);
307 if (!h) {
308 return hstrerror(h_errno);
309 }
310 memcpy(&local.sin_addr,
311 (struct in_addr *)h->h_addr_list[0],
312 sizeof(struct in_addr));
313 }
314 err = socket_bind(sock, (p_sa) &local, sizeof(local));
315 ERRORSTR_RETURN(err);
316}
317
318const char * tcp_listen(p_socket sock, int backlog) {
319 int err = socket_listen(sock, backlog);
320 ERRORSTR_RETURN(err);
321}
322
323const char * tcp_accept(p_socket sock, p_socket client, int timeout) {
324 int err = socket_accept(sock, client, NULL, NULL, timeout);
325 ERRORSTR_RETURN(err);
326}
327
328const char * tcp_connect(p_socket sock,
329 const char *host,
330 unsigned short port,
331 int timeout) {
Jeffrey Hane872b352020-09-24 10:41:12 -0700332 // TODO support IPv6
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200333 int err;
334 struct hostent *h;
335 struct sockaddr_in remote;
336 memset(&remote, 0, sizeof(remote));
337 remote.sin_family = AF_INET;
338 remote.sin_port = htons(port);
339 if (strcmp(host, "*") && !inet_aton(host, &remote.sin_addr)) {
340 h = gethostbyname(host);
341 if (!h) {
342 return hstrerror(h_errno);
343 }
344 memcpy(&remote.sin_addr,
345 (struct in_addr *)h->h_addr_list[0],
346 sizeof(struct in_addr));
347 }
348 err = socket_connect(sock, (p_sa) &remote, sizeof(remote), timeout);
349 ERRORSTR_RETURN(err);
350}
351
Jeffrey Hane872b352020-09-24 10:41:12 -0700352const char * tcp_create_and_connect(p_socket sock,
353 const char *host,
354 unsigned short port,
355 int timeout) {
356 int err;
357 struct sockaddr_in sa4;
358 struct sockaddr_in6 sa6;
359
360 memset(&sa4, 0, sizeof(sa4));
361 sa4.sin_family = AF_INET;
362 sa4.sin_port = htons(port);
363 memset(&sa6, 0, sizeof(sa6));
364 sa6.sin6_family = AF_INET6;
365 sa6.sin6_port = htons(port);
366
367 if (inet_pton(AF_INET, host, &sa4.sin_addr)) {
368 socket_create(sock, AF_INET, SOCK_STREAM, 0);
369 err = socket_connect(sock, (p_sa) &sa4, sizeof(sa4), timeout);
370 ERRORSTR_RETURN(err);
371 } else if (inet_pton(AF_INET6, host, &sa6.sin6_addr)) {
372 socket_create(sock, AF_INET6, SOCK_STREAM, 0);
373 err = socket_connect(sock, (p_sa) &sa6, sizeof(sa6), timeout);
374 ERRORSTR_RETURN(err);
375 } else {
376 struct addrinfo hints, *servinfo, *rp;
377 char portStr[6];
378 int rv;
379
380 memset(&hints, 0, sizeof(hints));
381 hints.ai_family = AF_UNSPEC;
382 hints.ai_socktype = SOCK_STREAM;
383
384 sprintf(portStr, "%u", port);
385
386 if ((rv = getaddrinfo(host, portStr, &hints, &servinfo)) != 0) {
387 return gai_strerror(rv);
388 }
389
390 err = TIMEOUT;
391 for (rp = servinfo; rp != NULL; rp = rp->ai_next) {
392 err = socket_create(sock, rp->ai_family, rp->ai_socktype, rp->ai_protocol);
393 if (err != SUCCESS) {
394 continue;
395 }
396 err = socket_connect(sock, (p_sa) rp->ai_addr, rp->ai_addrlen, timeout);
397 if (err == SUCCESS) {
398 break;
399 }
400 close(*sock);
401 }
402 freeaddrinfo(servinfo);
403 if (rp == NULL) {
404 *sock = -1;
405 return "Failed to connect";
406 } else {
407 ERRORSTR_RETURN(err);
408 }
409 }
410}
411
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200412#define WRITE_STEP 8192
413const char * tcp_send(
414 p_socket sock, const char * data, size_t w_len, int timeout) {
415 int err;
416 size_t put = 0, step;
417 if (!w_len) {
418 return NULL;
419 }
420
421 do {
422 step = (WRITE_STEP < w_len - put ? WRITE_STEP : w_len - put);
423 err = socket_send(sock, data + put, step, timeout);
424 put += step;
425 } while (err == SUCCESS && put < w_len);
426 ERRORSTR_RETURN(err);
427}
428
429const char * tcp_raw_receive(
430 p_socket sock, char * data, size_t r_len, int timeout, int *received) {
431 int err = socket_recv(sock, data, r_len, timeout, received);
432 ERRORSTR_RETURN(err);
433}