| 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 | #ifdef HAVE_CONFIG_H |
| 18 | #include "config.h" /* HAVE_SOCKADDR_STORAGE ? */ |
| 19 | #endif |
| 20 | |
| 21 | #include <sys/socket.h> /* struct sockaddr_storage */ |
| 22 | |
| 23 | #include "libnfs-zdr.h" |
| 24 | |
| 25 | #ifndef HAVE_SOCKADDR_STORAGE |
| 26 | /* |
| 27 | * RFC 2553: protocol-independent placeholder for socket addresses |
| 28 | */ |
| 29 | #define _SS_MAXSIZE 128 |
| 30 | #define _SS_ALIGNSIZE (sizeof(double)) |
| 31 | #define _SS_PAD1SIZE (_SS_ALIGNSIZE - sizeof(unsigned char) * 2) |
| 32 | #define _SS_PAD2SIZE (_SS_MAXSIZE - sizeof(unsigned char) * 2 - \ |
| 33 | _SS_PAD1SIZE - _SS_ALIGNSIZE) |
| 34 | |
| 35 | struct sockaddr_storage { |
| 36 | #ifdef HAVE_SOCKADDR_LEN |
| 37 | unsigned char ss_len; /* address length */ |
| 38 | unsigned char ss_family; /* address family */ |
| 39 | #else |
| 40 | unsigned short ss_family; |
| 41 | #endif |
| 42 | char __ss_pad1[_SS_PAD1SIZE]; |
| 43 | double __ss_align; /* force desired structure storage alignment */ |
| 44 | char __ss_pad2[_SS_PAD2SIZE]; |
| 45 | }; |
| 46 | #endif |
| 47 | |
| 48 | |
| 49 | struct rpc_fragment { |
| 50 | struct rpc_fragment *next; |
| 51 | uint64_t size; |
| 52 | char *data; |
| 53 | }; |
| 54 | |
| 55 | #define RPC_CONTEXT_MAGIC 0xc6e46435 |
| 56 | |
| 57 | struct rpc_context { |
| 58 | uint32_t magic; |
| 59 | int fd; |
| 60 | int is_connected; |
| 61 | |
| 62 | char *error_string; |
| 63 | |
| 64 | rpc_cb connect_cb; |
| 65 | void *connect_data; |
| 66 | |
| 67 | struct AUTH *auth; |
| 68 | unsigned long xid; |
| 69 | |
| 70 | /* buffer used for encoding RPC PDU */ |
| 71 | char *encodebuf; |
| 72 | int encodebuflen; |
| 73 | |
| 74 | struct rpc_pdu *outqueue; |
| 75 | struct sockaddr_storage udp_src; |
| 76 | struct rpc_pdu *waitpdu; |
| 77 | |
| 78 | uint32_t inpos; |
| 79 | uint32_t insize; |
| 80 | char *inbuf; |
| 81 | |
| 82 | /* special fields for UDP, which can sometimes be BROADCASTed */ |
| 83 | int is_udp; |
| 84 | struct sockaddr *udp_dest; |
| 85 | int is_broadcast; |
| 86 | |
| 87 | /* track the address we connect to so we can auto-reconnect on session failure */ |
| 88 | struct sockaddr_storage s; |
| 89 | int auto_reconnect; |
| 90 | |
| 91 | /* fragment reassembly */ |
| 92 | struct rpc_fragment *fragments; |
| 93 | }; |
| 94 | |
| 95 | struct rpc_pdu { |
| 96 | struct rpc_pdu *next; |
| 97 | |
| 98 | unsigned long xid; |
| 99 | ZDR zdr; |
| 100 | |
| 101 | uint32_t written; |
| 102 | struct rpc_data outdata; |
| 103 | |
| 104 | rpc_cb cb; |
| 105 | void *private_data; |
| 106 | |
| 107 | /* function to decode the zdr reply data and buffer to decode into */ |
| 108 | zdrproc_t zdr_decode_fn; |
| 109 | caddr_t zdr_decode_buf; |
| 110 | uint32_t zdr_decode_bufsize; |
| 111 | }; |
| 112 | |
| 113 | struct rpc_pdu *rpc_allocate_pdu(struct rpc_context *rpc, int program, int version, int procedure, rpc_cb cb, void *private_data, zdrproc_t zdr_decode_fn, int zdr_bufsize); |
| 114 | void rpc_free_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu); |
| 115 | int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu); |
| 116 | int rpc_get_pdu_size(char *buf); |
| 117 | int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size); |
| 118 | void rpc_error_all_pdus(struct rpc_context *rpc, char *error); |
| 119 | |
| 120 | void rpc_set_error(struct rpc_context *rpc, char *error_string, ...); |
| 121 | void nfs_set_error(struct nfs_context *nfs, char *error_string, ...); |
| 122 | |
| 123 | const char *nfs_get_server(struct nfs_context *nfs); |
| 124 | const char *nfs_get_export(struct nfs_context *nfs); |
| 125 | |
| 126 | /* we dont want to expose UDP to normal applications/users this is private to libnfs to use exclusively for broadcast RPC */ |
| 127 | int rpc_bind_udp(struct rpc_context *rpc, char *addr, int port); |
| 128 | int rpc_set_udp_destination(struct rpc_context *rpc, char *addr, int port, int is_broadcast); |
| 129 | struct rpc_context *rpc_init_udp_context(void); |
| 130 | struct sockaddr *rpc_get_recv_sockaddr(struct rpc_context *rpc); |
| 131 | |
| 132 | void rpc_set_autoreconnect(struct rpc_context *rpc); |
| 133 | void rpc_unset_autoreconnect(struct rpc_context *rpc); |
| 134 | |
| 135 | int rpc_add_fragment(struct rpc_context *rpc, char *data, uint64_t size); |
| 136 | void rpc_free_all_fragments(struct rpc_context *rpc); |
| 137 | |
| 138 | const struct nfs_fh3 *nfs_get_rootfh(struct nfs_context *nfs); |