blob: be696e06e23d94631f6c902d6c5cb6702b114696 [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 {
61 // Specify what I/O operations we care about
62 if (mode & WAIT_MODE_R) {
63 FD_ZERO(&rfds);
64 FD_SET(*sock, &rfds);
65 }
66 if (mode & WAIT_MODE_W) {
67 FD_ZERO(&wfds);
68 FD_SET(*sock, &wfds);
69 }
70
71 // Check for timeout
72 t = end - __gettime();
73 if (t < 0.0) {
74 break;
75 }
76
77 // Wait
78 tv.tv_sec = (int)t;
79 tv.tv_usec = (int)((t - tv.tv_sec) * 1.0e6);
80 ret = select(*sock+1, &rfds, &wfds, NULL, &tv);
81 } while (ret == -1 && errno == EINTR);
82 if (ret == -1) {
83 return errno;
84 }
85
86 // Check for timeout
87 if (ret == 0) {
88 return TIMEOUT;
89 }
90
91 // Verify that we can actually read from the remote host
92 if (mode & WAIT_MODE_C && FD_ISSET(*sock, &rfds) &&
93 recv(*sock, (char*) &rfds, 0, 0) != 0) {
94 return errno;
95 }
96
97 return SUCCESS;
98}
99
100////////////////////////////////////////////////////////////////////////////////
101// General
102
103T_ERRCODE socket_create(p_socket sock, int domain, int type, int protocol) {
104 *sock = socket(domain, type, protocol);
105 if (*sock > 0) {
106 return SUCCESS;
107 } else {
108 return errno;
109 }
110}
111
112T_ERRCODE socket_destroy(p_socket sock) {
113 // TODO Figure out if I should be free-ing this
114 if (*sock > 0) {
115 socket_setblocking(sock);
116 close(*sock);
117 *sock = -1;
118 }
119 return SUCCESS;
120}
121
122T_ERRCODE socket_bind(p_socket sock, p_sa addr, int addr_len) {
123 int ret = SUCCESS;
124 socket_setblocking(sock);
125 if (bind(*sock, addr, addr_len)) {
126 ret = errno;
127 }
128 socket_setnonblocking(sock);
129 return ret;
130}
131
132T_ERRCODE socket_get_info(p_socket sock, short *port, char *buf, size_t len) {
133 struct sockaddr_in sa;
134 socklen_t addrlen;
135 memset(&sa, 0, sizeof(sa));
136 int rc = getsockname(*sock, (struct sockaddr*)&sa, &addrlen);
137 if (!rc) {
138 char *addr = inet_ntoa(sa.sin_addr);
139 *port = ntohs(sa.sin_port);
140 if (strlen(addr) < len) {
141 len = strlen(addr);
142 }
143 memcpy(buf, addr, len);
144 return SUCCESS;
145 }
146 return rc;
147}
148
149////////////////////////////////////////////////////////////////////////////////
150// Server
151
152T_ERRCODE socket_accept(p_socket sock, p_socket client,
153 p_sa addr, socklen_t *addrlen, int timeout) {
154 int err;
155 if (*sock < 0) {
156 return CLOSED;
157 }
158 do {
159 *client = accept(*sock, addr, addrlen);
160 if (*client > 0) {
161 return SUCCESS;
162 }
163 err = errno;
164 } while (err != EINTR);
165 if (err == EAGAIN || err == ECONNABORTED) {
166 return socket_wait(sock, WAIT_MODE_R, timeout);
167 }
168 return err;
169}
170
171T_ERRCODE socket_listen(p_socket sock, int backlog) {
172 int ret = SUCCESS;
173 socket_setblocking(sock);
174 if (listen(*sock, backlog)) {
175 ret = errno;
176 }
177 socket_setnonblocking(sock);
178 return ret;
179}
180
181////////////////////////////////////////////////////////////////////////////////
182// Client
183
184T_ERRCODE socket_connect(p_socket sock, p_sa addr, int addr_len, int timeout) {
185 int err;
186 if (*sock < 0) {
187 return CLOSED;
188 }
189
190 do {
191 if (connect(*sock, addr, addr_len) == 0) {
192 return SUCCESS;
193 }
194 } while ((err = errno) == EINTR);
195 if (err != EINPROGRESS && err != EAGAIN) {
196 return err;
197 }
198 return socket_wait(sock, WAIT_MODE_C, timeout);
199}
200
201T_ERRCODE socket_send(
202 p_socket sock, const char *data, size_t len, int timeout) {
203 int err, put = 0;
204 if (*sock < 0) {
205 return CLOSED;
206 }
207 do {
208 put = send(*sock, data, len, 0);
209 if (put > 0) {
210 return SUCCESS;
211 }
212 err = errno;
213 } while (err != EINTR);
214
215 if (err == EAGAIN) {
216 return socket_wait(sock, WAIT_MODE_W, timeout);
217 }
218 return err;
219}
220
221T_ERRCODE socket_recv(
222 p_socket sock, char *data, size_t len, int timeout, int *received) {
223 int err, got = 0;
224 if (*sock < 0) {
225 return CLOSED;
226 }
227
228 int flags = fcntl(*sock, F_GETFL, 0);
229 do {
230 got = recv(*sock, data, len, 0);
231 if (got > 0) {
232 *received = got;
233 return SUCCESS;
234 }
235 err = errno;
236
237 // Connection has been closed by peer
238 if (got == 0) {
239 return CLOSED;
240 }
241 } while (err != EINTR);
242
243 if (err == EAGAIN) {
244 return socket_wait(sock, WAIT_MODE_R, timeout);
245 }
246 return err;
247}
248
249////////////////////////////////////////////////////////////////////////////////
250// Util
251
252void socket_setnonblocking(p_socket sock) {
253 int flags = fcntl(*sock, F_GETFL, 0);
254 flags |= O_NONBLOCK;
255 fcntl(*sock, F_SETFL, flags);
256}
257
258void socket_setblocking(p_socket sock) {
259 int flags = fcntl(*sock, F_GETFL, 0);
260 flags &= (~(O_NONBLOCK));
261 fcntl(*sock, F_SETFL, flags);
262}
263
264////////////////////////////////////////////////////////////////////////////////
265// TCP
266
267#define ERRORSTR_RETURN(err) \
268 if (err == SUCCESS) { \
269 return NULL; \
270 } else if (err == TIMEOUT) { \
271 return TIMEOUT_MSG; \
272 } else if (err == CLOSED) { \
273 return CLOSED_MSG; \
274 } \
275 return strerror(err)
276
277const char * tcp_create(p_socket sock) {
278 int err = socket_create(sock, AF_INET, SOCK_STREAM, 0);
279 ERRORSTR_RETURN(err);
280}
281
282const char * tcp_destroy(p_socket sock) {
283 int err = socket_destroy(sock);
284 ERRORSTR_RETURN(err);
285}
286
287const char * tcp_bind(p_socket sock, const char *host, unsigned short port) {
288 int err;
289 struct hostent *h;
290 struct sockaddr_in local;
291 memset(&local, 0, sizeof(local));
292 local.sin_family = AF_INET;
293 local.sin_addr.s_addr = htonl(INADDR_ANY);
294 local.sin_port = htons(port);
295 if (strcmp(host, "*") && !inet_aton(host, &local.sin_addr)) {
296 h = gethostbyname(host);
297 if (!h) {
298 return hstrerror(h_errno);
299 }
300 memcpy(&local.sin_addr,
301 (struct in_addr *)h->h_addr_list[0],
302 sizeof(struct in_addr));
303 }
304 err = socket_bind(sock, (p_sa) &local, sizeof(local));
305 ERRORSTR_RETURN(err);
306}
307
308const char * tcp_listen(p_socket sock, int backlog) {
309 int err = socket_listen(sock, backlog);
310 ERRORSTR_RETURN(err);
311}
312
313const char * tcp_accept(p_socket sock, p_socket client, int timeout) {
314 int err = socket_accept(sock, client, NULL, NULL, timeout);
315 ERRORSTR_RETURN(err);
316}
317
318const char * tcp_connect(p_socket sock,
319 const char *host,
320 unsigned short port,
321 int timeout) {
322 int err;
323 struct hostent *h;
324 struct sockaddr_in remote;
325 memset(&remote, 0, sizeof(remote));
326 remote.sin_family = AF_INET;
327 remote.sin_port = htons(port);
328 if (strcmp(host, "*") && !inet_aton(host, &remote.sin_addr)) {
329 h = gethostbyname(host);
330 if (!h) {
331 return hstrerror(h_errno);
332 }
333 memcpy(&remote.sin_addr,
334 (struct in_addr *)h->h_addr_list[0],
335 sizeof(struct in_addr));
336 }
337 err = socket_connect(sock, (p_sa) &remote, sizeof(remote), timeout);
338 ERRORSTR_RETURN(err);
339}
340
341#define WRITE_STEP 8192
342const char * tcp_send(
343 p_socket sock, const char * data, size_t w_len, int timeout) {
344 int err;
345 size_t put = 0, step;
346 if (!w_len) {
347 return NULL;
348 }
349
350 do {
351 step = (WRITE_STEP < w_len - put ? WRITE_STEP : w_len - put);
352 err = socket_send(sock, data + put, step, timeout);
353 put += step;
354 } while (err == SUCCESS && put < w_len);
355 ERRORSTR_RETURN(err);
356}
357
358const char * tcp_raw_receive(
359 p_socket sock, char * data, size_t r_len, int timeout, int *received) {
360 int err = socket_recv(sock, data, r_len, timeout, received);
361 ERRORSTR_RETURN(err);
362}