blob: 864fa3654d80e0fd9c4ad298dfc1d0240858121a [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) {
116 socket_setblocking(sock);
117 close(*sock);
118 *sock = -1;
119 }
120 return SUCCESS;
121}
122
123T_ERRCODE socket_bind(p_socket sock, p_sa addr, int addr_len) {
124 int ret = SUCCESS;
125 socket_setblocking(sock);
126 if (bind(*sock, addr, addr_len)) {
127 ret = errno;
128 }
129 socket_setnonblocking(sock);
130 return ret;
131}
132
133T_ERRCODE socket_get_info(p_socket sock, short *port, char *buf, size_t len) {
James E. King, IIIea5ea8b2016-11-12 15:12:33 -0500134 struct sockaddr_storage sa;
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200135 memset(&sa, 0, sizeof(sa));
James E. King, III36200902016-10-05 14:47:18 -0400136 socklen_t addrlen = sizeof(sa);
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200137 int rc = getsockname(*sock, (struct sockaddr*)&sa, &addrlen);
138 if (!rc) {
James E. King, IIIea5ea8b2016-11-12 15:12:33 -0500139 if (sa.ss_family == AF_INET6) {
140 struct sockaddr_in6* sin = (struct sockaddr_in6*)(&sa);
141 if (!inet_ntop(AF_INET6, &sin->sin6_addr, buf, len)) {
142 return errno;
143 }
144 *port = ntohs(sin->sin6_port);
145 } else {
146 struct sockaddr_in* sin = (struct sockaddr_in*)(&sa);
147 if (!inet_ntop(AF_INET, &sin->sin_addr, buf, len)) {
148 return errno;
149 }
150 *port = ntohs(sin->sin_port);
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200151 }
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200152 return SUCCESS;
153 }
James E. King, IIIea5ea8b2016-11-12 15:12:33 -0500154 return errno;
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200155}
156
157////////////////////////////////////////////////////////////////////////////////
158// Server
159
160T_ERRCODE socket_accept(p_socket sock, p_socket client,
161 p_sa addr, socklen_t *addrlen, int timeout) {
162 int err;
163 if (*sock < 0) {
164 return CLOSED;
165 }
166 do {
167 *client = accept(*sock, addr, addrlen);
168 if (*client > 0) {
169 return SUCCESS;
170 }
171 err = errno;
172 } while (err != EINTR);
173 if (err == EAGAIN || err == ECONNABORTED) {
174 return socket_wait(sock, WAIT_MODE_R, timeout);
175 }
176 return err;
177}
178
179T_ERRCODE socket_listen(p_socket sock, int backlog) {
180 int ret = SUCCESS;
181 socket_setblocking(sock);
182 if (listen(*sock, backlog)) {
183 ret = errno;
184 }
185 socket_setnonblocking(sock);
186 return ret;
187}
188
189////////////////////////////////////////////////////////////////////////////////
190// Client
191
192T_ERRCODE socket_connect(p_socket sock, p_sa addr, int addr_len, int timeout) {
193 int err;
194 if (*sock < 0) {
195 return CLOSED;
196 }
197
198 do {
199 if (connect(*sock, addr, addr_len) == 0) {
200 return SUCCESS;
201 }
202 } while ((err = errno) == EINTR);
203 if (err != EINPROGRESS && err != EAGAIN) {
204 return err;
205 }
206 return socket_wait(sock, WAIT_MODE_C, timeout);
207}
208
209T_ERRCODE socket_send(
210 p_socket sock, const char *data, size_t len, int timeout) {
211 int err, put = 0;
212 if (*sock < 0) {
213 return CLOSED;
214 }
215 do {
216 put = send(*sock, data, len, 0);
217 if (put > 0) {
218 return SUCCESS;
219 }
220 err = errno;
221 } while (err != EINTR);
222
223 if (err == EAGAIN) {
224 return socket_wait(sock, WAIT_MODE_W, timeout);
225 }
226 return err;
227}
228
229T_ERRCODE socket_recv(
230 p_socket sock, char *data, size_t len, int timeout, int *received) {
231 int err, got = 0;
232 if (*sock < 0) {
233 return CLOSED;
234 }
235
236 int flags = fcntl(*sock, F_GETFL, 0);
237 do {
238 got = recv(*sock, data, len, 0);
239 if (got > 0) {
240 *received = got;
241 return SUCCESS;
242 }
243 err = errno;
244
245 // Connection has been closed by peer
246 if (got == 0) {
247 return CLOSED;
248 }
249 } while (err != EINTR);
250
251 if (err == EAGAIN) {
252 return socket_wait(sock, WAIT_MODE_R, timeout);
253 }
254 return err;
255}
256
257////////////////////////////////////////////////////////////////////////////////
258// Util
259
260void socket_setnonblocking(p_socket sock) {
261 int flags = fcntl(*sock, F_GETFL, 0);
262 flags |= O_NONBLOCK;
263 fcntl(*sock, F_SETFL, flags);
264}
265
266void socket_setblocking(p_socket sock) {
267 int flags = fcntl(*sock, F_GETFL, 0);
268 flags &= (~(O_NONBLOCK));
269 fcntl(*sock, F_SETFL, flags);
270}
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}