| 1 | /* |
| 2 | Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com> |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published by |
| 6 | the Free Software Foundation; either version 2.1 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #ifdef HAVE_CONFIG_H |
| 19 | #include "config.h" |
| 20 | #endif |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <poll.h> |
| 26 | #include <string.h> |
| 27 | #include <errno.h> |
| 28 | #include <rpc/rpc.h> |
| 29 | #include <rpc/xdr.h> |
| 30 | #include <arpa/inet.h> |
| 31 | #ifdef HAVE_SYS_FILIO_H |
| 32 | #include <sys/filio.h> |
| 33 | #endif |
| 34 | #ifdef HAVE_SYS_SOCKIO_H |
| 35 | #include <sys/sockio.h> |
| 36 | #endif |
| 37 | #include <sys/ioctl.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <sys/socket.h> |
| 40 | #include <netdb.h> |
| 41 | #include "libnfs.h" |
| 42 | #include "libnfs-raw.h" |
| 43 | #include "libnfs-private.h" |
| 44 | #include "slist.h" |
| 45 | |
| 46 | static int rpc_disconnect_requeue(struct rpc_context *rpc); |
| 47 | |
| 48 | static void set_nonblocking(int fd) |
| 49 | { |
| 50 | unsigned v; |
| 51 | v = fcntl(fd, F_GETFL, 0); |
| 52 | fcntl(fd, F_SETFL, v | O_NONBLOCK); |
| 53 | } |
| 54 | |
| 55 | int rpc_get_fd(struct rpc_context *rpc) |
| 56 | { |
| 57 | return rpc->fd; |
| 58 | } |
| 59 | |
| 60 | int rpc_which_events(struct rpc_context *rpc) |
| 61 | { |
| 62 | int events = rpc->is_connected ? POLLIN : POLLOUT; |
| 63 | |
| 64 | if (rpc->is_udp != 0) { |
| 65 | /* for udp sockets we only wait for pollin */ |
| 66 | return POLLIN; |
| 67 | } |
| 68 | |
| 69 | if (rpc->outqueue) { |
| 70 | events |= POLLOUT; |
| 71 | } |
| 72 | return events; |
| 73 | } |
| 74 | |
| 75 | static int rpc_write_to_socket(struct rpc_context *rpc) |
| 76 | { |
| 77 | ssize_t count; |
| 78 | |
| 79 | if (rpc == NULL) { |
| 80 | return -1; |
| 81 | } |
| 82 | if (rpc->fd == -1) { |
| 83 | rpc_set_error(rpc, "trying to write but not connected"); |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | while (rpc->outqueue != NULL) { |
| 88 | ssize_t total; |
| 89 | |
| 90 | total = rpc->outqueue->outdata.size; |
| 91 | |
| 92 | count = write(rpc->fd, rpc->outqueue->outdata.data + rpc->outqueue->written, total - rpc->outqueue->written); |
| 93 | if (count == -1) { |
| 94 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 95 | return 0; |
| 96 | } |
| 97 | rpc_set_error(rpc, "Error when writing to socket :%s(%d)", strerror(errno), errno); |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | rpc->outqueue->written += count; |
| 102 | if (rpc->outqueue->written == total) { |
| 103 | struct rpc_pdu *pdu = rpc->outqueue; |
| 104 | |
| 105 | SLIST_REMOVE(&rpc->outqueue, pdu); |
| 106 | SLIST_ADD_END(&rpc->waitpdu, pdu); |
| 107 | } |
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int rpc_read_from_socket(struct rpc_context *rpc) |
| 113 | { |
| 114 | int available; |
| 115 | int size; |
| 116 | int pdu_size; |
| 117 | ssize_t count; |
| 118 | |
| 119 | if (ioctl(rpc->fd, FIONREAD, &available) != 0) { |
| 120 | rpc_set_error(rpc, "Ioctl FIONREAD returned error : %d. Closing socket.", errno); |
| 121 | return -1; |
| 122 | } |
| 123 | if (available == 0) { |
| 124 | rpc_set_error(rpc, "Socket has been closed"); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | if (rpc->is_udp) { |
| 129 | char *buf; |
| 130 | socklen_t socklen = sizeof(rpc->udp_src); |
| 131 | |
| 132 | buf = malloc(available); |
| 133 | if (buf == NULL) { |
| 134 | rpc_set_error(rpc, "Failed to malloc buffer for recvfrom"); |
| 135 | return -1; |
| 136 | } |
| 137 | count = recvfrom(rpc->fd, buf, available, MSG_DONTWAIT, (struct sockaddr *)&rpc->udp_src, &socklen); |
| 138 | if (count < 0) { |
| 139 | rpc_set_error(rpc, "Failed recvfrom: %s", strerror(errno)); |
| 140 | free(buf); |
| 141 | } |
| 142 | if (rpc_process_pdu(rpc, buf, count) != 0) { |
| 143 | rpc_set_error(rpc, "Invalid/garbage pdu received from server. Ignoring PDU"); |
| 144 | free(buf); |
| 145 | return -1; |
| 146 | } |
| 147 | free(buf); |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | /* read record marker, 4 bytes at the beginning of every pdu */ |
| 152 | if (rpc->inbuf == NULL) { |
| 153 | rpc->insize = 4; |
| 154 | rpc->inbuf = malloc(rpc->insize); |
| 155 | if (rpc->inbuf == NULL) { |
| 156 | rpc_set_error(rpc, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno); |
| 157 | return -1; |
| 158 | } |
| 159 | } |
| 160 | if (rpc->inpos < 4) { |
| 161 | size = 4 - rpc->inpos; |
| 162 | |
| 163 | count = read(rpc->fd, rpc->inbuf + rpc->inpos, size); |
| 164 | if (count == -1) { |
| 165 | if (errno == EINTR) { |
| 166 | return 0; |
| 167 | } |
| 168 | rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno); |
| 169 | return -1; |
| 170 | } |
| 171 | available -= count; |
| 172 | rpc->inpos += count; |
| 173 | } |
| 174 | |
| 175 | if (available == 0) { |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | pdu_size = rpc_get_pdu_size(rpc->inbuf); |
| 180 | if (rpc->insize < pdu_size) { |
| 181 | unsigned char *buf; |
| 182 | |
| 183 | buf = malloc(pdu_size); |
| 184 | if (buf == NULL) { |
| 185 | rpc_set_error(rpc, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size, errno); |
| 186 | return -1; |
| 187 | } |
| 188 | memcpy(buf, rpc->inbuf, rpc->insize); |
| 189 | free(rpc->inbuf); |
| 190 | rpc->inbuf = buf; |
| 191 | rpc->insize = rpc_get_pdu_size(rpc->inbuf); |
| 192 | } |
| 193 | |
| 194 | size = available; |
| 195 | if (size > rpc->insize - rpc->inpos) { |
| 196 | size = rpc->insize - rpc->inpos; |
| 197 | } |
| 198 | |
| 199 | count = read(rpc->fd, rpc->inbuf + rpc->inpos, size); |
| 200 | if (count == -1) { |
| 201 | if (errno == EINTR) { |
| 202 | return 0; |
| 203 | } |
| 204 | rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno); |
| 205 | return -1; |
| 206 | } |
| 207 | available -= count; |
| 208 | rpc->inpos += count; |
| 209 | |
| 210 | if (rpc->inpos == rpc->insize) { |
| 211 | if (rpc_process_pdu(rpc, rpc->inbuf, pdu_size) != 0) { |
| 212 | rpc_set_error(rpc, "Invalid/garbage pdu received from server. Closing socket"); |
| 213 | return -1; |
| 214 | } |
| 215 | free(rpc->inbuf); |
| 216 | rpc->inbuf = NULL; |
| 217 | rpc->insize = 0; |
| 218 | rpc->inpos = 0; |
| 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | |
| 226 | int rpc_service(struct rpc_context *rpc, int revents) |
| 227 | { |
| 228 | if (revents & POLLERR) { |
| 229 | int err = 0; |
| 230 | socklen_t err_size = sizeof(err); |
| 231 | |
| 232 | if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR, |
| 233 | &err, &err_size) != 0 || err != 0) { |
| 234 | if (err == 0) { |
| 235 | err = errno; |
| 236 | } |
| 237 | rpc_set_error(rpc, "rpc_service: socket error " |
| 238 | "%s(%d).", |
| 239 | strerror(err), err); |
| 240 | } else { |
| 241 | rpc_set_error(rpc, "rpc_service: POLLERR, " |
| 242 | "Unknown socket error."); |
| 243 | } |
| 244 | rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data); |
| 245 | return -1; |
| 246 | } |
| 247 | if (revents & POLLHUP) { |
| 248 | rpc_set_error(rpc, "Socket failed with POLLHUP"); |
| 249 | rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data); |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | if (rpc->is_connected == 0 && rpc->fd != -1 && revents&POLLOUT) { |
| 254 | int err = 0; |
| 255 | socklen_t err_size = sizeof(err); |
| 256 | |
| 257 | if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR, |
| 258 | &err, &err_size) != 0 || err != 0) { |
| 259 | if (err == 0) { |
| 260 | err = errno; |
| 261 | } |
| 262 | rpc_set_error(rpc, "rpc_service: socket error " |
| 263 | "%s(%d) while connecting.", |
| 264 | strerror(err), err); |
| 265 | rpc->connect_cb(rpc, RPC_STATUS_ERROR, |
| 266 | NULL, rpc->connect_data); |
| 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | rpc->is_connected = 1; |
| 271 | rpc->connect_cb(rpc, RPC_STATUS_SUCCESS, NULL, rpc->connect_data); |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | if (revents & POLLIN) { |
| 276 | if (rpc_read_from_socket(rpc) != 0) { |
| 277 | rpc_disconnect_requeue(rpc); |
| 278 | return 0; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | if (revents & POLLOUT && rpc->outqueue != NULL) { |
| 283 | if (rpc_write_to_socket(rpc) != 0) { |
| 284 | rpc_set_error(rpc, "write to socket failed"); |
| 285 | return -1; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | |
| 293 | int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc_cb cb, void *private_data) |
| 294 | { |
| 295 | struct sockaddr_storage s; |
| 296 | struct sockaddr_in *sin = (struct sockaddr_in *)&s; |
| 297 | int socksize; |
| 298 | |
| 299 | if (rpc->fd != -1) { |
| 300 | rpc_set_error(rpc, "Trying to connect while already connected"); |
| 301 | return -1; |
| 302 | } |
| 303 | |
| 304 | if (rpc->is_udp != 0) { |
| 305 | rpc_set_error(rpc, "Trying to connect on UDP socket"); |
| 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | sin->sin_family = AF_INET; |
| 310 | sin->sin_port = htons(port); |
| 311 | if (inet_pton(AF_INET, server, &sin->sin_addr) != 1) { |
| 312 | rpc_set_error(rpc, "Not a valid server ip address"); |
| 313 | return -1; |
| 314 | } |
| 315 | |
| 316 | switch (s.ss_family) { |
| 317 | case AF_INET: |
| 318 | socksize = sizeof(struct sockaddr_in); |
| 319 | #ifdef HAVE_SOCKADDR_LEN |
| 320 | sin->sin_len = socksize; |
| 321 | #endif |
| 322 | rpc->fd = socket(AF_INET, SOCK_STREAM, 0); |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | if (rpc->fd == -1) { |
| 327 | rpc_set_error(rpc, "Failed to open socket"); |
| 328 | return -1; |
| 329 | } |
| 330 | |
| 331 | rpc->connect_cb = cb; |
| 332 | rpc->connect_data = private_data; |
| 333 | |
| 334 | set_nonblocking(rpc->fd); |
| 335 | |
| 336 | if (connect(rpc->fd, (struct sockaddr *)&s, socksize) != 0 && errno != EINPROGRESS) { |
| 337 | rpc_set_error(rpc, "connect() to server failed"); |
| 338 | return -1; |
| 339 | } |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | int rpc_disconnect(struct rpc_context *rpc, char *error) |
| 345 | { |
| 346 | if (rpc->fd != -1) { |
| 347 | close(rpc->fd); |
| 348 | } |
| 349 | rpc->fd = -1; |
| 350 | |
| 351 | rpc->is_connected = 0; |
| 352 | |
| 353 | rpc_error_all_pdus(rpc, error); |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | /* disconnect but do not error all PDUs, just move pdus in-flight back to the outqueue */ |
| 359 | static int rpc_disconnect_requeue(struct rpc_context *rpc) |
| 360 | { |
| 361 | struct rpc_pdu *pdu; |
| 362 | |
| 363 | if (rpc->fd != -1) { |
| 364 | close(rpc->fd); |
| 365 | } |
| 366 | rpc->fd = -1; |
| 367 | |
| 368 | rpc->is_connected = 0; |
| 369 | |
| 370 | /* socket is closed so we will not get any replies to any commands |
| 371 | * in flight. Move them all over from the waitpdu queue back to the out queue |
| 372 | */ |
| 373 | for (pdu=rpc->waitpdu; pdu; pdu=pdu->next) { |
| 374 | SLIST_REMOVE(&rpc->waitpdu, pdu); |
| 375 | SLIST_ADD(&rpc->outqueue, pdu); |
| 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | int rpc_bind_udp(struct rpc_context *rpc, char *addr, int port) |
| 383 | { |
| 384 | struct addrinfo *ai = NULL; |
| 385 | char service[6]; |
| 386 | |
| 387 | if (rpc->is_udp == 0) { |
| 388 | rpc_set_error(rpc, "Cant not bind UDP. Not UDP context"); |
| 389 | return -1; |
| 390 | } |
| 391 | |
| 392 | snprintf(service, 6, "%d", port); |
| 393 | if (getaddrinfo(addr, service, NULL, &ai) != 0) { |
| 394 | rpc_set_error(rpc, "Invalid address:%s. " |
| 395 | "Can not resolv into IPv4/v6 structure."); |
| 396 | return -1; |
| 397 | } |
| 398 | |
| 399 | switch(ai->ai_family) { |
| 400 | case AF_INET: |
| 401 | rpc->fd = socket(ai->ai_family, SOCK_DGRAM, 0); |
| 402 | if (rpc->fd == -1) { |
| 403 | rpc_set_error(rpc, "Failed to create UDP socket: %s", strerror(errno)); |
| 404 | freeaddrinfo(ai); |
| 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | if (bind(rpc->fd, (struct sockaddr *)ai->ai_addr, sizeof(struct sockaddr_in)) != 0) { |
| 409 | rpc_set_error(rpc, "Failed to bind to UDP socket: %s",strerror(errno)); |
| 410 | freeaddrinfo(ai); |
| 411 | return -1; |
| 412 | } |
| 413 | break; |
| 414 | default: |
| 415 | rpc_set_error(rpc, "Can not handle UPD sockets of family %d yet", ai->ai_family); |
| 416 | freeaddrinfo(ai); |
| 417 | return -1; |
| 418 | } |
| 419 | |
| 420 | freeaddrinfo(ai); |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | int rpc_set_udp_destination(struct rpc_context *rpc, char *addr, int port, int is_broadcast) |
| 426 | { |
| 427 | struct addrinfo *ai = NULL; |
| 428 | char service[6]; |
| 429 | |
| 430 | if (rpc->is_udp == 0) { |
| 431 | rpc_set_error(rpc, "Can not set destination sockaddr. Not UDP context"); |
| 432 | return -1; |
| 433 | } |
| 434 | |
| 435 | snprintf(service, 6, "%d", port); |
| 436 | if (getaddrinfo(addr, service, NULL, &ai) != 0) { |
| 437 | rpc_set_error(rpc, "Invalid address:%s. " |
| 438 | "Can not resolv into IPv4/v6 structure."); |
| 439 | return -1; |
| 440 | } |
| 441 | |
| 442 | if (rpc->udp_dest) { |
| 443 | free(rpc->udp_dest); |
| 444 | rpc->udp_dest = NULL; |
| 445 | } |
| 446 | rpc->udp_dest = malloc(ai->ai_addrlen); |
| 447 | if (rpc->udp_dest == NULL) { |
| 448 | rpc_set_error(rpc, "Out of memory. Failed to allocate sockaddr structure"); |
| 449 | return -1; |
| 450 | } |
| 451 | memcpy(rpc->udp_dest, ai->ai_addr, ai->ai_addrlen); |
| 452 | freeaddrinfo(ai); |
| 453 | |
| 454 | rpc->is_broadcast = is_broadcast; |
| 455 | setsockopt(rpc->fd, SOL_SOCKET, SO_BROADCAST, &is_broadcast, sizeof(is_broadcast)); |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | struct sockaddr *rpc_get_recv_sockaddr(struct rpc_context *rpc) |
| 461 | { |
| 462 | return (struct sockaddr *)&rpc->udp_src; |
| 463 | } |