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