| 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 | #include <sys/ioctl.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/socket.h> |
| 37 | #include <netdb.h> |
| 38 | #include "libnfs.h" |
| 39 | #include "libnfs-raw.h" |
| 40 | #include "libnfs-private.h" |
| 41 | #include "slist.h" |
| 42 | |
| 43 | static void set_nonblocking(int fd) |
| 44 | { |
| 45 | unsigned v; |
| 46 | v = fcntl(fd, F_GETFL, 0); |
| 47 | fcntl(fd, F_SETFL, v | O_NONBLOCK); |
| 48 | } |
| 49 | |
| 50 | int rpc_get_fd(struct rpc_context *rpc) |
| 51 | { |
| 52 | return rpc->fd; |
| 53 | } |
| 54 | |
| 55 | int rpc_which_events(struct rpc_context *rpc) |
| 56 | { |
| 57 | int events = rpc->is_connected ? POLLIN : POLLOUT; |
| 58 | |
| 59 | if (rpc->is_udp != 0) { |
| 60 | /* for udp sockets we only wait for pollin */ |
| 61 | return POLLIN; |
| 62 | } |
| 63 | |
| 64 | if (rpc->outqueue) { |
| 65 | events |= POLLOUT; |
| 66 | } |
| 67 | return events; |
| 68 | } |
| 69 | |
| 70 | static int rpc_write_to_socket(struct rpc_context *rpc) |
| 71 | { |
| 72 | ssize_t count; |
| 73 | |
| 74 | if (rpc == NULL) { |
| 75 | return -1; |
| 76 | } |
| 77 | if (rpc->fd == -1) { |
| 78 | rpc_set_error(rpc, "trying to write but not connected"); |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | while (rpc->outqueue != NULL) { |
| 83 | ssize_t total; |
| 84 | |
| 85 | total = rpc->outqueue->outdata.size; |
| 86 | |
| 87 | count = write(rpc->fd, rpc->outqueue->outdata.data + rpc->outqueue->written, total - rpc->outqueue->written); |
| 88 | if (count == -1) { |
| 89 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 90 | return 0; |
| 91 | } |
| 92 | rpc_set_error(rpc, "Error when writing to socket :%s(%d)", strerror(errno), errno); |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | rpc->outqueue->written += count; |
| 97 | if (rpc->outqueue->written == total) { |
| 98 | struct rpc_pdu *pdu = rpc->outqueue; |
| 99 | |
| 100 | SLIST_REMOVE(&rpc->outqueue, pdu); |
| 101 | SLIST_ADD_END(&rpc->waitpdu, pdu); |
| 102 | } |
| 103 | } |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static int rpc_read_from_socket(struct rpc_context *rpc) |
| 108 | { |
| 109 | int available; |
| 110 | int size; |
| 111 | int pdu_size; |
| 112 | ssize_t count; |
| 113 | |
| 114 | if (ioctl(rpc->fd, FIONREAD, &available) != 0) { |
| 115 | rpc_set_error(rpc, "Ioctl FIONREAD returned error : %d. Closing socket.", errno); |
| 116 | return -1; |
| 117 | } |
| 118 | if (available == 0) { |
| 119 | rpc_set_error(rpc, "Socket has been closed"); |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | if (rpc->is_udp) { |
| 124 | char *buf; |
| 125 | socklen_t socklen = sizeof(rpc->udp_src); |
| 126 | |
| 127 | buf = malloc(available); |
| 128 | if (buf == NULL) { |
| 129 | rpc_set_error(rpc, "Failed to malloc buffer for recvfrom"); |
| 130 | return -1; |
| 131 | } |
| 132 | count = recvfrom(rpc->fd, buf, available, MSG_DONTWAIT, (struct sockaddr *)&rpc->udp_src, &socklen); |
| 133 | if (count < 0) { |
| 134 | rpc_set_error(rpc, "Failed recvfrom: %s", strerror(errno)); |
| 135 | free(buf); |
| 136 | } |
| 137 | if (rpc_process_pdu(rpc, buf, count) != 0) { |
| 138 | rpc_set_error(rpc, "Invalid/garbage pdu received from server. Ignoring PDU"); |
| 139 | free(buf); |
| 140 | return -1; |
| 141 | } |
| 142 | free(buf); |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | /* read record marker, 4 bytes at the beginning of every pdu */ |
| 147 | if (rpc->inbuf == NULL) { |
| 148 | rpc->insize = 4; |
| 149 | rpc->inbuf = malloc(rpc->insize); |
| 150 | if (rpc->inbuf == NULL) { |
| 151 | rpc_set_error(rpc, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno); |
| 152 | return -1; |
| 153 | } |
| 154 | } |
| 155 | if (rpc->inpos < 4) { |
| 156 | size = 4 - rpc->inpos; |
| 157 | |
| 158 | count = read(rpc->fd, rpc->inbuf + rpc->inpos, size); |
| 159 | if (count == -1) { |
| 160 | if (errno == EINTR) { |
| 161 | return 0; |
| 162 | } |
| 163 | rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno); |
| 164 | return -1; |
| 165 | } |
| 166 | available -= count; |
| 167 | rpc->inpos += count; |
| 168 | } |
| 169 | |
| 170 | if (available == 0) { |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | pdu_size = rpc_get_pdu_size(rpc->inbuf); |
| 175 | if (rpc->insize < pdu_size) { |
| 176 | unsigned char *buf; |
| 177 | |
| 178 | buf = malloc(pdu_size); |
| 179 | if (buf == NULL) { |
| 180 | rpc_set_error(rpc, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size, errno); |
| 181 | return -1; |
| 182 | } |
| 183 | memcpy(buf, rpc->inbuf, rpc->insize); |
| 184 | free(rpc->inbuf); |
| 185 | rpc->inbuf = buf; |
| 186 | rpc->insize = rpc_get_pdu_size(rpc->inbuf); |
| 187 | } |
| 188 | |
| 189 | size = available; |
| 190 | if (size > rpc->insize - rpc->inpos) { |
| 191 | size = rpc->insize - rpc->inpos; |
| 192 | } |
| 193 | |
| 194 | count = read(rpc->fd, rpc->inbuf + rpc->inpos, size); |
| 195 | if (count == -1) { |
| 196 | if (errno == EINTR) { |
| 197 | return 0; |
| 198 | } |
| 199 | rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno); |
| 200 | return -1; |
| 201 | } |
| 202 | available -= count; |
| 203 | rpc->inpos += count; |
| 204 | |
| 205 | if (rpc->inpos == rpc->insize) { |
| 206 | if (rpc_process_pdu(rpc, rpc->inbuf, pdu_size) != 0) { |
| 207 | rpc_set_error(rpc, "Invalid/garbage pdu received from server. Closing socket"); |
| 208 | return -1; |
| 209 | } |
| 210 | free(rpc->inbuf); |
| 211 | rpc->inbuf = NULL; |
| 212 | rpc->insize = 0; |
| 213 | rpc->inpos = 0; |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | |
| 221 | int rpc_service(struct rpc_context *rpc, int revents) |
| 222 | { |
| 223 | if (revents & POLLERR) { |
| 224 | int err = 0; |
| 225 | socklen_t err_size = sizeof(err); |
| 226 | |
| 227 | if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR, |
| 228 | &err, &err_size) != 0 || err != 0) { |
| 229 | if (err == 0) { |
| 230 | err = errno; |
| 231 | } |
| 232 | rpc_set_error(rpc, "rpc_service: socket error " |
| 233 | "%s(%d).", |
| 234 | strerror(err), err); |
| 235 | } else { |
| 236 | rpc_set_error(rpc, "rpc_service: POLLERR, " |
| 237 | "Unknown socket error."); |
| 238 | } |
| 239 | rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data); |
| 240 | return -1; |
| 241 | } |
| 242 | if (revents & POLLHUP) { |
| 243 | rpc_set_error(rpc, "Socket failed with POLLHUP"); |
| 244 | rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data); |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | if (rpc->is_connected == 0 && rpc->fd != -1 && revents&POLLOUT) { |
| 249 | int err = 0; |
| 250 | socklen_t err_size = sizeof(err); |
| 251 | |
| 252 | if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR, |
| 253 | &err, &err_size) != 0 || err != 0) { |
| 254 | if (err == 0) { |
| 255 | err = errno; |
| 256 | } |
| 257 | rpc_set_error(rpc, "rpc_service: socket error " |
| 258 | "%s(%d) while connecting.", |
| 259 | strerror(err), err); |
| 260 | rpc->connect_cb(rpc, RPC_STATUS_ERROR, |
| 261 | NULL, rpc->connect_data); |
| 262 | return -1; |
| 263 | } |
| 264 | |
| 265 | rpc->is_connected = 1; |
| 266 | rpc->connect_cb(rpc, RPC_STATUS_SUCCESS, NULL, rpc->connect_data); |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | if (revents & POLLOUT && rpc->outqueue != NULL) { |
| 271 | if (rpc_write_to_socket(rpc) != 0) { |
| 272 | rpc_set_error(rpc, "write to socket failed"); |
| 273 | return -1; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | if (revents & POLLIN) { |
| 278 | if (rpc_read_from_socket(rpc) != 0) { |
| 279 | rpc_disconnect(rpc, rpc_get_error(rpc)); |
| 280 | return -1; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | |
| 288 | int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc_cb cb, void *private_data) |
| 289 | { |
| 290 | struct sockaddr_storage s; |
| 291 | struct sockaddr_in *sin = (struct sockaddr_in *)&s; |
| 292 | int socksize; |
| 293 | |
| 294 | if (rpc->fd != -1) { |
| 295 | rpc_set_error(rpc, "Trying to connect while already connected"); |
| 296 | return -1; |
| 297 | } |
| 298 | |
| 299 | if (rpc->is_udp != 0) { |
| 300 | rpc_set_error(rpc, "Trying to connect on UDP socket"); |
| 301 | return -1; |
| 302 | } |
| 303 | |
| 304 | sin->sin_family = AF_INET; |
| 305 | sin->sin_port = htons(port); |
| 306 | if (inet_pton(AF_INET, server, &sin->sin_addr) != 1) { |
| 307 | rpc_set_error(rpc, "Not a valid server ip address"); |
| 308 | return -1; |
| 309 | } |
| 310 | |
| 311 | switch (s.ss_family) { |
| 312 | case AF_INET: |
| 313 | socksize = sizeof(struct sockaddr_in); |
| 314 | #ifdef HAVE_SOCKADDR_LEN |
| 315 | sin->sin_len = socksize; |
| 316 | #endif |
| 317 | rpc->fd = socket(AF_INET, SOCK_STREAM, 0); |
| 318 | break; |
| 319 | } |
| 320 | |
| 321 | if (rpc->fd == -1) { |
| 322 | rpc_set_error(rpc, "Failed to open socket"); |
| 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | rpc->connect_cb = cb; |
| 327 | rpc->connect_data = private_data; |
| 328 | |
| 329 | set_nonblocking(rpc->fd); |
| 330 | |
| 331 | if (connect(rpc->fd, (struct sockaddr *)&s, socksize) != 0 && errno != EINPROGRESS) { |
| 332 | rpc_set_error(rpc, "connect() to server failed"); |
| 333 | return -1; |
| 334 | } |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | int rpc_disconnect(struct rpc_context *rpc, char *error) |
| 340 | { |
| 341 | if (rpc->fd != -1) { |
| 342 | close(rpc->fd); |
| 343 | } |
| 344 | rpc->fd = -1; |
| 345 | |
| 346 | rpc->is_connected = 0; |
| 347 | |
| 348 | rpc_error_all_pdus(rpc, error); |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | |
| 354 | int rpc_bind_udp(struct rpc_context *rpc, char *addr, int port) |
| 355 | { |
| 356 | struct addrinfo *ai = NULL; |
| 357 | char service[6]; |
| 358 | |
| 359 | if (rpc->is_udp == 0) { |
| 360 | rpc_set_error(rpc, "Cant not bind UDP. Not UDP context"); |
| 361 | return -1; |
| 362 | } |
| 363 | |
| 364 | snprintf(service, 6, "%d", port); |
| 365 | if (getaddrinfo(addr, service, NULL, &ai) != 0) { |
| 366 | rpc_set_error(rpc, "Invalid address:%s. " |
| 367 | "Can not resolv into IPv4/v6 structure."); |
| 368 | return -1; |
| 369 | } |
| 370 | |
| 371 | switch(ai->ai_family) { |
| 372 | case AF_INET: |
| 373 | rpc->fd = socket(ai->ai_family, SOCK_DGRAM, 0); |
| 374 | if (rpc->fd == -1) { |
| 375 | rpc_set_error(rpc, "Failed to create UDP socket: %s", strerror(errno)); |
| 376 | freeaddrinfo(ai); |
| 377 | return -1; |
| 378 | } |
| 379 | |
| 380 | if (bind(rpc->fd, (struct sockaddr *)ai->ai_addr, sizeof(struct sockaddr_in)) != 0) { |
| 381 | rpc_set_error(rpc, "Failed to bind to UDP socket: %s",strerror(errno)); |
| 382 | freeaddrinfo(ai); |
| 383 | return -1; |
| 384 | } |
| 385 | break; |
| 386 | default: |
| 387 | rpc_set_error(rpc, "Can not handle UPD sockets of family %d yet", ai->ai_family); |
| 388 | freeaddrinfo(ai); |
| 389 | return -1; |
| 390 | } |
| 391 | |
| 392 | freeaddrinfo(ai); |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | int rpc_set_udp_destination(struct rpc_context *rpc, char *addr, int port, int is_broadcast) |
| 398 | { |
| 399 | struct addrinfo *ai = NULL; |
| 400 | char service[6]; |
| 401 | |
| 402 | if (rpc->is_udp == 0) { |
| 403 | rpc_set_error(rpc, "Can not set destination sockaddr. Not UDP context"); |
| 404 | return -1; |
| 405 | } |
| 406 | |
| 407 | snprintf(service, 6, "%d", port); |
| 408 | if (getaddrinfo(addr, service, NULL, &ai) != 0) { |
| 409 | rpc_set_error(rpc, "Invalid address:%s. " |
| 410 | "Can not resolv into IPv4/v6 structure."); |
| 411 | return -1; |
| 412 | } |
| 413 | |
| 414 | if (rpc->udp_dest) { |
| 415 | free(rpc->udp_dest); |
| 416 | rpc->udp_dest = NULL; |
| 417 | } |
| 418 | rpc->udp_dest = malloc(ai->ai_addrlen); |
| 419 | if (rpc->udp_dest == NULL) { |
| 420 | rpc_set_error(rpc, "Out of memory. Failed to allocate sockaddr structure"); |
| 421 | return -1; |
| 422 | } |
| 423 | memcpy(rpc->udp_dest, ai->ai_addr, ai->ai_addrlen); |
| 424 | freeaddrinfo(ai); |
| 425 | |
| 426 | rpc->is_broadcast = is_broadcast; |
| 427 | setsockopt(rpc->fd, SOL_SOCKET, SO_BROADCAST, &is_broadcast, sizeof(is_broadcast)); |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | struct sockaddr *rpc_get_recv_sockaddr(struct rpc_context *rpc) |
| 433 | { |
| 434 | return (struct sockaddr *)&rpc->udp_src; |
| 435 | } |