Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 1 | // |
| 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) |
| 50 | T_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, III | 3620090 | 2016-10-05 14:47:18 -0400 | [diff] [blame^] | 61 | FD_ZERO(&rfds); |
| 62 | FD_ZERO(&wfds); |
| 63 | |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 64 | // Specify what I/O operations we care about |
| 65 | if (mode & WAIT_MODE_R) { |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 66 | FD_SET(*sock, &rfds); |
| 67 | } |
| 68 | if (mode & WAIT_MODE_W) { |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 69 | 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 | |
| 104 | T_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 | |
| 113 | T_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 | |
| 123 | T_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 | |
| 133 | T_ERRCODE socket_get_info(p_socket sock, short *port, char *buf, size_t len) { |
| 134 | struct sockaddr_in sa; |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 135 | memset(&sa, 0, sizeof(sa)); |
James E. King, III | 3620090 | 2016-10-05 14:47:18 -0400 | [diff] [blame^] | 136 | socklen_t addrlen = sizeof(sa); |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 137 | int rc = getsockname(*sock, (struct sockaddr*)&sa, &addrlen); |
| 138 | if (!rc) { |
| 139 | char *addr = inet_ntoa(sa.sin_addr); |
| 140 | *port = ntohs(sa.sin_port); |
| 141 | if (strlen(addr) < len) { |
| 142 | len = strlen(addr); |
| 143 | } |
| 144 | memcpy(buf, addr, len); |
| 145 | return SUCCESS; |
| 146 | } |
| 147 | return rc; |
| 148 | } |
| 149 | |
| 150 | //////////////////////////////////////////////////////////////////////////////// |
| 151 | // Server |
| 152 | |
| 153 | T_ERRCODE socket_accept(p_socket sock, p_socket client, |
| 154 | p_sa addr, socklen_t *addrlen, int timeout) { |
| 155 | int err; |
| 156 | if (*sock < 0) { |
| 157 | return CLOSED; |
| 158 | } |
| 159 | do { |
| 160 | *client = accept(*sock, addr, addrlen); |
| 161 | if (*client > 0) { |
| 162 | return SUCCESS; |
| 163 | } |
| 164 | err = errno; |
| 165 | } while (err != EINTR); |
| 166 | if (err == EAGAIN || err == ECONNABORTED) { |
| 167 | return socket_wait(sock, WAIT_MODE_R, timeout); |
| 168 | } |
| 169 | return err; |
| 170 | } |
| 171 | |
| 172 | T_ERRCODE socket_listen(p_socket sock, int backlog) { |
| 173 | int ret = SUCCESS; |
| 174 | socket_setblocking(sock); |
| 175 | if (listen(*sock, backlog)) { |
| 176 | ret = errno; |
| 177 | } |
| 178 | socket_setnonblocking(sock); |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | //////////////////////////////////////////////////////////////////////////////// |
| 183 | // Client |
| 184 | |
| 185 | T_ERRCODE socket_connect(p_socket sock, p_sa addr, int addr_len, int timeout) { |
| 186 | int err; |
| 187 | if (*sock < 0) { |
| 188 | return CLOSED; |
| 189 | } |
| 190 | |
| 191 | do { |
| 192 | if (connect(*sock, addr, addr_len) == 0) { |
| 193 | return SUCCESS; |
| 194 | } |
| 195 | } while ((err = errno) == EINTR); |
| 196 | if (err != EINPROGRESS && err != EAGAIN) { |
| 197 | return err; |
| 198 | } |
| 199 | return socket_wait(sock, WAIT_MODE_C, timeout); |
| 200 | } |
| 201 | |
| 202 | T_ERRCODE socket_send( |
| 203 | p_socket sock, const char *data, size_t len, int timeout) { |
| 204 | int err, put = 0; |
| 205 | if (*sock < 0) { |
| 206 | return CLOSED; |
| 207 | } |
| 208 | do { |
| 209 | put = send(*sock, data, len, 0); |
| 210 | if (put > 0) { |
| 211 | return SUCCESS; |
| 212 | } |
| 213 | err = errno; |
| 214 | } while (err != EINTR); |
| 215 | |
| 216 | if (err == EAGAIN) { |
| 217 | return socket_wait(sock, WAIT_MODE_W, timeout); |
| 218 | } |
| 219 | return err; |
| 220 | } |
| 221 | |
| 222 | T_ERRCODE socket_recv( |
| 223 | p_socket sock, char *data, size_t len, int timeout, int *received) { |
| 224 | int err, got = 0; |
| 225 | if (*sock < 0) { |
| 226 | return CLOSED; |
| 227 | } |
| 228 | |
| 229 | int flags = fcntl(*sock, F_GETFL, 0); |
| 230 | do { |
| 231 | got = recv(*sock, data, len, 0); |
| 232 | if (got > 0) { |
| 233 | *received = got; |
| 234 | return SUCCESS; |
| 235 | } |
| 236 | err = errno; |
| 237 | |
| 238 | // Connection has been closed by peer |
| 239 | if (got == 0) { |
| 240 | return CLOSED; |
| 241 | } |
| 242 | } while (err != EINTR); |
| 243 | |
| 244 | if (err == EAGAIN) { |
| 245 | return socket_wait(sock, WAIT_MODE_R, timeout); |
| 246 | } |
| 247 | return err; |
| 248 | } |
| 249 | |
| 250 | //////////////////////////////////////////////////////////////////////////////// |
| 251 | // Util |
| 252 | |
| 253 | void socket_setnonblocking(p_socket sock) { |
| 254 | int flags = fcntl(*sock, F_GETFL, 0); |
| 255 | flags |= O_NONBLOCK; |
| 256 | fcntl(*sock, F_SETFL, flags); |
| 257 | } |
| 258 | |
| 259 | void socket_setblocking(p_socket sock) { |
| 260 | int flags = fcntl(*sock, F_GETFL, 0); |
| 261 | flags &= (~(O_NONBLOCK)); |
| 262 | fcntl(*sock, F_SETFL, flags); |
| 263 | } |
| 264 | |
| 265 | //////////////////////////////////////////////////////////////////////////////// |
| 266 | // TCP |
| 267 | |
| 268 | #define ERRORSTR_RETURN(err) \ |
| 269 | if (err == SUCCESS) { \ |
| 270 | return NULL; \ |
| 271 | } else if (err == TIMEOUT) { \ |
| 272 | return TIMEOUT_MSG; \ |
| 273 | } else if (err == CLOSED) { \ |
| 274 | return CLOSED_MSG; \ |
| 275 | } \ |
| 276 | return strerror(err) |
| 277 | |
| 278 | const char * tcp_create(p_socket sock) { |
| 279 | int err = socket_create(sock, AF_INET, SOCK_STREAM, 0); |
| 280 | ERRORSTR_RETURN(err); |
| 281 | } |
| 282 | |
| 283 | const char * tcp_destroy(p_socket sock) { |
| 284 | int err = socket_destroy(sock); |
| 285 | ERRORSTR_RETURN(err); |
| 286 | } |
| 287 | |
| 288 | const char * tcp_bind(p_socket sock, const char *host, unsigned short port) { |
| 289 | int err; |
| 290 | struct hostent *h; |
| 291 | struct sockaddr_in local; |
| 292 | memset(&local, 0, sizeof(local)); |
| 293 | local.sin_family = AF_INET; |
| 294 | local.sin_addr.s_addr = htonl(INADDR_ANY); |
| 295 | local.sin_port = htons(port); |
| 296 | if (strcmp(host, "*") && !inet_aton(host, &local.sin_addr)) { |
| 297 | h = gethostbyname(host); |
| 298 | if (!h) { |
| 299 | return hstrerror(h_errno); |
| 300 | } |
| 301 | memcpy(&local.sin_addr, |
| 302 | (struct in_addr *)h->h_addr_list[0], |
| 303 | sizeof(struct in_addr)); |
| 304 | } |
| 305 | err = socket_bind(sock, (p_sa) &local, sizeof(local)); |
| 306 | ERRORSTR_RETURN(err); |
| 307 | } |
| 308 | |
| 309 | const char * tcp_listen(p_socket sock, int backlog) { |
| 310 | int err = socket_listen(sock, backlog); |
| 311 | ERRORSTR_RETURN(err); |
| 312 | } |
| 313 | |
| 314 | const char * tcp_accept(p_socket sock, p_socket client, int timeout) { |
| 315 | int err = socket_accept(sock, client, NULL, NULL, timeout); |
| 316 | ERRORSTR_RETURN(err); |
| 317 | } |
| 318 | |
| 319 | const char * tcp_connect(p_socket sock, |
| 320 | const char *host, |
| 321 | unsigned short port, |
| 322 | int timeout) { |
| 323 | int err; |
| 324 | struct hostent *h; |
| 325 | struct sockaddr_in remote; |
| 326 | memset(&remote, 0, sizeof(remote)); |
| 327 | remote.sin_family = AF_INET; |
| 328 | remote.sin_port = htons(port); |
| 329 | if (strcmp(host, "*") && !inet_aton(host, &remote.sin_addr)) { |
| 330 | h = gethostbyname(host); |
| 331 | if (!h) { |
| 332 | return hstrerror(h_errno); |
| 333 | } |
| 334 | memcpy(&remote.sin_addr, |
| 335 | (struct in_addr *)h->h_addr_list[0], |
| 336 | sizeof(struct in_addr)); |
| 337 | } |
| 338 | err = socket_connect(sock, (p_sa) &remote, sizeof(remote), timeout); |
| 339 | ERRORSTR_RETURN(err); |
| 340 | } |
| 341 | |
| 342 | #define WRITE_STEP 8192 |
| 343 | const char * tcp_send( |
| 344 | p_socket sock, const char * data, size_t w_len, int timeout) { |
| 345 | int err; |
| 346 | size_t put = 0, step; |
| 347 | if (!w_len) { |
| 348 | return NULL; |
| 349 | } |
| 350 | |
| 351 | do { |
| 352 | step = (WRITE_STEP < w_len - put ? WRITE_STEP : w_len - put); |
| 353 | err = socket_send(sock, data + put, step, timeout); |
| 354 | put += step; |
| 355 | } while (err == SUCCESS && put < w_len); |
| 356 | ERRORSTR_RETURN(err); |
| 357 | } |
| 358 | |
| 359 | const char * tcp_raw_receive( |
| 360 | p_socket sock, char * data, size_t r_len, int timeout, int *received) { |
| 361 | int err = socket_recv(sock, data, r_len, timeout, received); |
| 362 | ERRORSTR_RETURN(err); |
| 363 | } |