blob: 1a1b549a0c6c56cb2a58b5c7197622458dc007c1 [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
92 // Verify that we can actually read from the remote host
93 if (mode & WAIT_MODE_C && FD_ISSET(*sock, &rfds) &&
94 recv(*sock, (char*) &rfds, 0, 0) != 0) {
95 return errno;
96 }
97
98 return SUCCESS;
99}
100
101////////////////////////////////////////////////////////////////////////////////
102// General
103
104T_ERRCODE socket_create(p_socket sock, int domain, int type, int protocol) {
105 *sock = socket(domain, type, protocol);
106 if (*sock > 0) {
107 return SUCCESS;
108 } else {
109 return errno;
110 }
111}
112
113T_ERRCODE socket_destroy(p_socket sock) {
114 // TODO Figure out if I should be free-ing this
115 if (*sock > 0) {
James E. King, III533405e2017-10-28 18:25:45 -0400116 (void)socket_setblocking(sock);
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200117 close(*sock);
118 *sock = -1;
119 }
120 return SUCCESS;
121}
122
123T_ERRCODE socket_bind(p_socket sock, p_sa addr, int addr_len) {
James E. King, III533405e2017-10-28 18:25:45 -0400124 int ret = socket_setblocking(sock);
125 if (ret != SUCCESS) {
126 return ret;
127 }
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200128 if (bind(*sock, addr, addr_len)) {
129 ret = errno;
130 }
James E. King, III533405e2017-10-28 18:25:45 -0400131 int ret2 = socket_setnonblocking(sock);
132 return ret == SUCCESS ? ret2 : ret;
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200133}
134
135T_ERRCODE socket_get_info(p_socket sock, short *port, char *buf, size_t len) {
James E. King, IIIea5ea8b2016-11-12 15:12:33 -0500136 struct sockaddr_storage sa;
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200137 memset(&sa, 0, sizeof(sa));
James E. King, III36200902016-10-05 14:47:18 -0400138 socklen_t addrlen = sizeof(sa);
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200139 int rc = getsockname(*sock, (struct sockaddr*)&sa, &addrlen);
140 if (!rc) {
James E. King, IIIea5ea8b2016-11-12 15:12:33 -0500141 if (sa.ss_family == AF_INET6) {
142 struct sockaddr_in6* sin = (struct sockaddr_in6*)(&sa);
143 if (!inet_ntop(AF_INET6, &sin->sin6_addr, buf, len)) {
144 return errno;
145 }
146 *port = ntohs(sin->sin6_port);
147 } else {
148 struct sockaddr_in* sin = (struct sockaddr_in*)(&sa);
149 if (!inet_ntop(AF_INET, &sin->sin_addr, buf, len)) {
150 return errno;
151 }
152 *port = ntohs(sin->sin_port);
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200153 }
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200154 return SUCCESS;
155 }
James E. King, IIIea5ea8b2016-11-12 15:12:33 -0500156 return errno;
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200157}
158
159////////////////////////////////////////////////////////////////////////////////
160// Server
161
162T_ERRCODE socket_accept(p_socket sock, p_socket client,
163 p_sa addr, socklen_t *addrlen, int timeout) {
164 int err;
165 if (*sock < 0) {
166 return CLOSED;
167 }
168 do {
169 *client = accept(*sock, addr, addrlen);
170 if (*client > 0) {
171 return SUCCESS;
172 }
James E. King, III533405e2017-10-28 18:25:45 -0400173 } while ((err = errno) == EINTR);
174
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200175 if (err == EAGAIN || err == ECONNABORTED) {
176 return socket_wait(sock, WAIT_MODE_R, timeout);
177 }
James E. King, III533405e2017-10-28 18:25:45 -0400178
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200179 return err;
180}
181
182T_ERRCODE socket_listen(p_socket sock, int backlog) {
James E. King, III533405e2017-10-28 18:25:45 -0400183 int ret = socket_setblocking(sock);
184 if (ret != SUCCESS) {
185 return ret;
186 }
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200187 if (listen(*sock, backlog)) {
188 ret = errno;
189 }
James E. King, III533405e2017-10-28 18:25:45 -0400190 int ret2 = socket_setnonblocking(sock);
191 return ret == SUCCESS ? ret2 : ret;
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200192}
193
194////////////////////////////////////////////////////////////////////////////////
195// Client
196
197T_ERRCODE socket_connect(p_socket sock, p_sa addr, int addr_len, int timeout) {
198 int err;
199 if (*sock < 0) {
200 return CLOSED;
201 }
202
203 do {
204 if (connect(*sock, addr, addr_len) == 0) {
205 return SUCCESS;
206 }
207 } while ((err = errno) == EINTR);
208 if (err != EINPROGRESS && err != EAGAIN) {
209 return err;
210 }
211 return socket_wait(sock, WAIT_MODE_C, timeout);
212}
213
214T_ERRCODE socket_send(
215 p_socket sock, const char *data, size_t len, int timeout) {
216 int err, put = 0;
217 if (*sock < 0) {
218 return CLOSED;
219 }
220 do {
221 put = send(*sock, data, len, 0);
222 if (put > 0) {
223 return SUCCESS;
224 }
James E. King, III533405e2017-10-28 18:25:45 -0400225 } while ((err = errno) == EINTR);
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200226
227 if (err == EAGAIN) {
228 return socket_wait(sock, WAIT_MODE_W, timeout);
229 }
James E. King, III533405e2017-10-28 18:25:45 -0400230
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200231 return err;
232}
233
234T_ERRCODE socket_recv(
235 p_socket sock, char *data, size_t len, int timeout, int *received) {
236 int err, got = 0;
237 if (*sock < 0) {
238 return CLOSED;
239 }
James E. King, III533405e2017-10-28 18:25:45 -0400240 *received = 0;
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200241
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200242 do {
243 got = recv(*sock, data, len, 0);
244 if (got > 0) {
245 *received = got;
246 return SUCCESS;
247 }
248 err = errno;
249
250 // Connection has been closed by peer
251 if (got == 0) {
252 return CLOSED;
253 }
James E. King, III533405e2017-10-28 18:25:45 -0400254 } while (err == EINTR);
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200255
256 if (err == EAGAIN) {
257 return socket_wait(sock, WAIT_MODE_R, timeout);
258 }
James E. King, III533405e2017-10-28 18:25:45 -0400259
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200260 return err;
261}
262
263////////////////////////////////////////////////////////////////////////////////
264// Util
265
James E. King, III533405e2017-10-28 18:25:45 -0400266T_ERRCODE socket_setnonblocking(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
James E. King, III533405e2017-10-28 18:25:45 -0400272T_ERRCODE socket_setblocking(p_socket sock) {
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200273 int flags = fcntl(*sock, F_GETFL, 0);
274 flags &= (~(O_NONBLOCK));
James E. King, III533405e2017-10-28 18:25:45 -0400275 return fcntl(*sock, F_SETFL, flags) != -1 ? SUCCESS : errno;
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200276}
277
278////////////////////////////////////////////////////////////////////////////////
279// TCP
280
281#define ERRORSTR_RETURN(err) \
282 if (err == SUCCESS) { \
283 return NULL; \
284 } else if (err == TIMEOUT) { \
285 return TIMEOUT_MSG; \
286 } else if (err == CLOSED) { \
287 return CLOSED_MSG; \
288 } \
289 return strerror(err)
290
291const char * tcp_create(p_socket sock) {
292 int err = socket_create(sock, AF_INET, SOCK_STREAM, 0);
293 ERRORSTR_RETURN(err);
294}
295
296const char * tcp_destroy(p_socket sock) {
297 int err = socket_destroy(sock);
298 ERRORSTR_RETURN(err);
299}
300
301const char * tcp_bind(p_socket sock, const char *host, unsigned short port) {
302 int err;
303 struct hostent *h;
304 struct sockaddr_in local;
305 memset(&local, 0, sizeof(local));
306 local.sin_family = AF_INET;
307 local.sin_addr.s_addr = htonl(INADDR_ANY);
308 local.sin_port = htons(port);
309 if (strcmp(host, "*") && !inet_aton(host, &local.sin_addr)) {
310 h = gethostbyname(host);
311 if (!h) {
312 return hstrerror(h_errno);
313 }
314 memcpy(&local.sin_addr,
315 (struct in_addr *)h->h_addr_list[0],
316 sizeof(struct in_addr));
317 }
318 err = socket_bind(sock, (p_sa) &local, sizeof(local));
319 ERRORSTR_RETURN(err);
320}
321
322const char * tcp_listen(p_socket sock, int backlog) {
323 int err = socket_listen(sock, backlog);
324 ERRORSTR_RETURN(err);
325}
326
327const char * tcp_accept(p_socket sock, p_socket client, int timeout) {
328 int err = socket_accept(sock, client, NULL, NULL, timeout);
329 ERRORSTR_RETURN(err);
330}
331
332const char * tcp_connect(p_socket sock,
333 const char *host,
334 unsigned short port,
335 int timeout) {
336 int err;
337 struct hostent *h;
338 struct sockaddr_in remote;
339 memset(&remote, 0, sizeof(remote));
340 remote.sin_family = AF_INET;
341 remote.sin_port = htons(port);
342 if (strcmp(host, "*") && !inet_aton(host, &remote.sin_addr)) {
343 h = gethostbyname(host);
344 if (!h) {
345 return hstrerror(h_errno);
346 }
347 memcpy(&remote.sin_addr,
348 (struct in_addr *)h->h_addr_list[0],
349 sizeof(struct in_addr));
350 }
351 err = socket_connect(sock, (p_sa) &remote, sizeof(remote), timeout);
352 ERRORSTR_RETURN(err);
353}
354
355#define WRITE_STEP 8192
356const char * tcp_send(
357 p_socket sock, const char * data, size_t w_len, int timeout) {
358 int err;
359 size_t put = 0, step;
360 if (!w_len) {
361 return NULL;
362 }
363
364 do {
365 step = (WRITE_STEP < w_len - put ? WRITE_STEP : w_len - put);
366 err = socket_send(sock, data + put, step, timeout);
367 put += step;
368 } while (err == SUCCESS && put < w_len);
369 ERRORSTR_RETURN(err);
370}
371
372const char * tcp_raw_receive(
373 p_socket sock, char * data, size_t r_len, int timeout, int *received) {
374 int err = socket_recv(sock, data, r_len, timeout, received);
375 ERRORSTR_RETURN(err);
376}