blob: 7c77651ad8abdeb1078d00d4e59a5211a15f1007 [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) {
286 int err = socket_create(sock, AF_INET, SOCK_STREAM, 0);
287 ERRORSTR_RETURN(err);
288}
289
290const char * tcp_destroy(p_socket sock) {
291 int err = socket_destroy(sock);
292 ERRORSTR_RETURN(err);
293}
294
295const char * tcp_bind(p_socket sock, const char *host, unsigned short port) {
296 int err;
297 struct hostent *h;
298 struct sockaddr_in local;
299 memset(&local, 0, sizeof(local));
300 local.sin_family = AF_INET;
301 local.sin_addr.s_addr = htonl(INADDR_ANY);
302 local.sin_port = htons(port);
303 if (strcmp(host, "*") && !inet_aton(host, &local.sin_addr)) {
304 h = gethostbyname(host);
305 if (!h) {
306 return hstrerror(h_errno);
307 }
308 memcpy(&local.sin_addr,
309 (struct in_addr *)h->h_addr_list[0],
310 sizeof(struct in_addr));
311 }
312 err = socket_bind(sock, (p_sa) &local, sizeof(local));
313 ERRORSTR_RETURN(err);
314}
315
316const char * tcp_listen(p_socket sock, int backlog) {
317 int err = socket_listen(sock, backlog);
318 ERRORSTR_RETURN(err);
319}
320
321const char * tcp_accept(p_socket sock, p_socket client, int timeout) {
322 int err = socket_accept(sock, client, NULL, NULL, timeout);
323 ERRORSTR_RETURN(err);
324}
325
326const char * tcp_connect(p_socket sock,
327 const char *host,
328 unsigned short port,
329 int timeout) {
330 int err;
331 struct hostent *h;
332 struct sockaddr_in remote;
333 memset(&remote, 0, sizeof(remote));
334 remote.sin_family = AF_INET;
335 remote.sin_port = htons(port);
336 if (strcmp(host, "*") && !inet_aton(host, &remote.sin_addr)) {
337 h = gethostbyname(host);
338 if (!h) {
339 return hstrerror(h_errno);
340 }
341 memcpy(&remote.sin_addr,
342 (struct in_addr *)h->h_addr_list[0],
343 sizeof(struct in_addr));
344 }
345 err = socket_connect(sock, (p_sa) &remote, sizeof(remote), timeout);
346 ERRORSTR_RETURN(err);
347}
348
349#define WRITE_STEP 8192
350const char * tcp_send(
351 p_socket sock, const char * data, size_t w_len, int timeout) {
352 int err;
353 size_t put = 0, step;
354 if (!w_len) {
355 return NULL;
356 }
357
358 do {
359 step = (WRITE_STEP < w_len - put ? WRITE_STEP : w_len - put);
360 err = socket_send(sock, data + put, step, timeout);
361 put += step;
362 } while (err == SUCCESS && put < w_len);
363 ERRORSTR_RETURN(err);
364}
365
366const char * tcp_raw_receive(
367 p_socket sock, char * data, size_t r_len, int timeout, int *received) {
368 int err = socket_recv(sock, data, r_len, timeout, received);
369 ERRORSTR_RETURN(err);
370}