| 1 | /* |
| 2 | Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010 |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; either version 3 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 General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | /* Example program using the lowlevel raw interface. |
| 19 | * This allow accurate control of the exact commands that are being used. |
| 20 | */ |
| 21 | |
| 22 | #define SERVER "10.1.1.27" |
| 23 | #define EXPORT "/shared" |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <poll.h> |
| 28 | #include "libnfs.h" |
| 29 | #include "libnfs-raw.h" |
| 30 | #include "libnfs-raw-mount.h" |
| 31 | #include "libnfs-raw-nfs.h" |
| 32 | #include "libnfs-raw-rquota.h" |
| 33 | |
| 34 | struct client { |
| 35 | char *server; |
| 36 | char *export; |
| 37 | uint32_t mount_port; |
| 38 | uint32_t rquota_port; |
| 39 | int is_finished; |
| 40 | struct nfs_fh3 rootfh; |
| 41 | }; |
| 42 | |
| 43 | void rquota_getquota_cb(struct rpc_context *rpc _U_, int status, void *data, void *private_data) |
| 44 | { |
| 45 | struct client *client = private_data; |
| 46 | GETQUOTA1res *res = data; |
| 47 | |
| 48 | if (status == RPC_STATUS_ERROR) { |
| 49 | printf("rquota/getquota call failed with \"%s\"\n", (char *)data); |
| 50 | exit(10); |
| 51 | } |
| 52 | if (status != RPC_STATUS_SUCCESS) { |
| 53 | printf("rquota/getquota call to server %s failed, status:%d\n", client->server, status); |
| 54 | exit(10); |
| 55 | } |
| 56 | |
| 57 | printf("rquota responded ok\n"); |
| 58 | client->is_finished = 1; |
| 59 | } |
| 60 | |
| 61 | void rquota_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data) |
| 62 | { |
| 63 | struct client *client = private_data; |
| 64 | |
| 65 | if (status != RPC_STATUS_SUCCESS) { |
| 66 | printf("connection to RPC.RQUOTAD on server %s failed\n", client->server); |
| 67 | exit(10); |
| 68 | } |
| 69 | |
| 70 | printf("Connected to RPC.RQUOTAD on %s:%d\n", client->server, client->rquota_port); |
| 71 | printf("Send GETQUOTA request for uid 100\n"); |
| 72 | if (rpc_rquota1_getquota_async(rpc, rquota_getquota_cb, EXPORT, 100, client) != 0) { |
| 73 | printf("Failed to send fsinfo request\n"); |
| 74 | exit(10); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void nfs_fsinfo_cb(struct rpc_context *rpc _U_, int status, void *data, void *private_data) |
| 79 | { |
| 80 | struct client *client = private_data; |
| 81 | FSINFO3res *res = data; |
| 82 | |
| 83 | if (status == RPC_STATUS_ERROR) { |
| 84 | printf("nfs/fsinfo call failed with \"%s\"\n", (char *)data); |
| 85 | exit(10); |
| 86 | } |
| 87 | if (status != RPC_STATUS_SUCCESS) { |
| 88 | printf("nfs/fsinfo call to server %s failed, status:%d\n", client->server, status); |
| 89 | exit(10); |
| 90 | } |
| 91 | |
| 92 | printf("Got reply from server for NFS/FSINFO procedure.\n"); |
| 93 | printf("Read Max:%d\n", (int)res->FSINFO3res_u.resok.rtmax); |
| 94 | printf("Write Max:%d\n", (int)res->FSINFO3res_u.resok.wtmax); |
| 95 | |
| 96 | printf("Disconnect socket from nfs server\n"); |
| 97 | if (rpc_disconnect(rpc, "normal disconnect") != 0) { |
| 98 | printf("Failed to disconnect socket to nfs\n"); |
| 99 | exit(10); |
| 100 | } |
| 101 | |
| 102 | printf("Connect to RPC.RQUOTAD on %s:%d\n", client->server, client->rquota_port); |
| 103 | if (rpc_connect_async(rpc, client->server, client->rquota_port, rquota_connect_cb, client) != 0) { |
| 104 | printf("Failed to start connection\n"); |
| 105 | exit(10); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | |
| 110 | void nfs_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data) |
| 111 | { |
| 112 | struct client *client = private_data; |
| 113 | |
| 114 | if (status != RPC_STATUS_SUCCESS) { |
| 115 | printf("connection to RPC.MOUNTD on server %s failed\n", client->server); |
| 116 | exit(10); |
| 117 | } |
| 118 | |
| 119 | printf("Connected to RPC.NFSD on %s:%d\n", client->server, client->mount_port); |
| 120 | printf("Send FSINFO request\n"); |
| 121 | if (rpc_nfs_fsinfo_async(rpc, nfs_fsinfo_cb, &client->rootfh, client) != 0) { |
| 122 | printf("Failed to send fsinfo request\n"); |
| 123 | exit(10); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void mount_mnt_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 128 | { |
| 129 | struct client *client = private_data; |
| 130 | mountres3 *mnt = data; |
| 131 | |
| 132 | if (status == RPC_STATUS_ERROR) { |
| 133 | printf("mount/mnt call failed with \"%s\"\n", (char *)data); |
| 134 | exit(10); |
| 135 | } |
| 136 | if (status != RPC_STATUS_SUCCESS) { |
| 137 | printf("mount/mnt call to server %s failed, status:%d\n", client->server, status); |
| 138 | exit(10); |
| 139 | } |
| 140 | |
| 141 | printf("Got reply from server for MOUNT/MNT procedure.\n"); |
| 142 | client->rootfh.data.data_len = mnt->mountres3_u.mountinfo.fhandle.fhandle3_len; |
| 143 | client->rootfh.data.data_val = malloc(client->rootfh.data.data_len); |
| 144 | memcpy(client->rootfh.data.data_val, mnt->mountres3_u.mountinfo.fhandle.fhandle3_val, client->rootfh.data.data_len); |
| 145 | |
| 146 | printf("Disconnect socket from mountd server\n"); |
| 147 | if (rpc_disconnect(rpc, "normal disconnect") != 0) { |
| 148 | printf("Failed to disconnect socket to mountd\n"); |
| 149 | exit(10); |
| 150 | } |
| 151 | |
| 152 | printf("Connect to RPC.NFSD on %s:%d\n", client->server, 2049); |
| 153 | if (rpc_connect_async(rpc, client->server, 2049, nfs_connect_cb, client) != 0) { |
| 154 | printf("Failed to start connection\n"); |
| 155 | exit(10); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | |
| 160 | void mount_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 161 | { |
| 162 | struct client *client = private_data; |
| 163 | |
| 164 | if (status == RPC_STATUS_ERROR) { |
| 165 | printf("mount null call failed with \"%s\"\n", (char *)data); |
| 166 | exit(10); |
| 167 | } |
| 168 | if (status != RPC_STATUS_SUCCESS) { |
| 169 | printf("mount null call to server %s failed, status:%d\n", client->server, status); |
| 170 | exit(10); |
| 171 | } |
| 172 | |
| 173 | printf("Got reply from server for MOUNT/NULL procedure.\n"); |
| 174 | printf("Send MOUNT/MNT command for %s\n", client->export); |
| 175 | if (rpc_mount_mnt_async(rpc, mount_mnt_cb, client->export, client) != 0) { |
| 176 | printf("Failed to send mnt request\n"); |
| 177 | exit(10); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void mount_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data) |
| 182 | { |
| 183 | struct client *client = private_data; |
| 184 | |
| 185 | if (status != RPC_STATUS_SUCCESS) { |
| 186 | printf("connection to RPC.MOUNTD on server %s failed\n", client->server); |
| 187 | exit(10); |
| 188 | } |
| 189 | |
| 190 | printf("Connected to RPC.MOUNTD on %s:%d\n", client->server, client->mount_port); |
| 191 | printf("Send NULL request to check if RPC.MOUNTD is actually running\n"); |
| 192 | if (rpc_mount_null_async(rpc, mount_null_cb, client) != 0) { |
| 193 | printf("Failed to send null request\n"); |
| 194 | exit(10); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | |
| 199 | void pmap_getport2_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 200 | { |
| 201 | struct client *client = private_data; |
| 202 | |
| 203 | if (status == RPC_STATUS_ERROR) { |
| 204 | printf("portmapper getport call failed with \"%s\"\n", (char *)data); |
| 205 | exit(10); |
| 206 | } |
| 207 | if (status != RPC_STATUS_SUCCESS) { |
| 208 | printf("portmapper getport call to server %s failed, status:%d\n", client->server, status); |
| 209 | exit(10); |
| 210 | } |
| 211 | |
| 212 | client->mount_port = *(uint32_t *)data; |
| 213 | printf("GETPORT returned RPC.MOUNTD is on port:%d\n", client->mount_port); |
| 214 | if (client->mount_port == 0) { |
| 215 | printf("RPC.MOUNTD is not available on server : %s:%d\n", client->server, client->mount_port); |
| 216 | exit(10); |
| 217 | } |
| 218 | |
| 219 | printf("Disconnect socket from portmap server\n"); |
| 220 | if (rpc_disconnect(rpc, "normal disconnect") != 0) { |
| 221 | printf("Failed to disconnect socket to portmapper\n"); |
| 222 | exit(10); |
| 223 | } |
| 224 | |
| 225 | printf("Connect to RPC.MOUNTD on %s:%d\n", client->server, client->mount_port); |
| 226 | if (rpc_connect_async(rpc, client->server, client->mount_port, mount_connect_cb, client) != 0) { |
| 227 | printf("Failed to start connection\n"); |
| 228 | exit(10); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void pmap_getport1_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 233 | { |
| 234 | struct client *client = private_data; |
| 235 | |
| 236 | if (status == RPC_STATUS_ERROR) { |
| 237 | printf("portmapper getport call failed with \"%s\"\n", (char *)data); |
| 238 | exit(10); |
| 239 | } |
| 240 | if (status != RPC_STATUS_SUCCESS) { |
| 241 | printf("portmapper getport call to server %s failed, status:%d\n", client->server, status); |
| 242 | exit(10); |
| 243 | } |
| 244 | |
| 245 | client->rquota_port = *(uint32_t *)data; |
| 246 | printf("GETPORT returned RPC.RQUOTAD on port:%d\n", client->rquota_port); |
| 247 | if (client->rquota_port == 0) { |
| 248 | printf("RPC.RQUOTAD is not available on server : %s:%d\n", client->server, client->rquota_port); |
| 249 | exit(10); |
| 250 | } |
| 251 | |
| 252 | printf("Send getport request asking for MOUNT port\n"); |
| 253 | if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, pmap_getport2_cb, client) != 0) { |
| 254 | printf("Failed to send getport request\n"); |
| 255 | exit(10); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void pmap_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 260 | { |
| 261 | struct client *client = private_data; |
| 262 | |
| 263 | if (status == RPC_STATUS_ERROR) { |
| 264 | printf("portmapper null call failed with \"%s\"\n", (char *)data); |
| 265 | exit(10); |
| 266 | } |
| 267 | if (status != RPC_STATUS_SUCCESS) { |
| 268 | printf("portmapper null call to server %s failed, status:%d\n", client->server, status); |
| 269 | exit(10); |
| 270 | } |
| 271 | |
| 272 | printf("Got reply from server for PORTMAP/NULL procedure.\n"); |
| 273 | printf("Send getport request asking for MOUNT port\n"); |
| 274 | if (rpc_pmap_getport_async(rpc, RQUOTA_PROGRAM, RQUOTA_V1, pmap_getport1_cb, client) != 0) { |
| 275 | printf("Failed to send getport request\n"); |
| 276 | exit(10); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | void pmap_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data) |
| 281 | { |
| 282 | struct client *client = private_data; |
| 283 | |
| 284 | printf("pmap_connect_cb status:%d.\n", status); |
| 285 | if (status != RPC_STATUS_SUCCESS) { |
| 286 | printf("connection to portmapper on server %s failed\n", client->server); |
| 287 | exit(10); |
| 288 | } |
| 289 | |
| 290 | printf("Send NULL request to check if portmapper is actually running\n"); |
| 291 | if (rpc_pmap_null_async(rpc, pmap_null_cb, client) != 0) { |
| 292 | printf("Failed to send null request\n"); |
| 293 | exit(10); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | |
| 298 | int main(int argc _U_, char *argv[] _U_) |
| 299 | { |
| 300 | struct rpc_context *rpc; |
| 301 | struct pollfd pfd; |
| 302 | struct client client; |
| 303 | |
| 304 | rpc = rpc_init_context(); |
| 305 | if (rpc == NULL) { |
| 306 | printf("failed to init context\n"); |
| 307 | exit(10); |
| 308 | } |
| 309 | |
| 310 | client.server = SERVER; |
| 311 | client.export = EXPORT; |
| 312 | client.is_finished = 0; |
| 313 | if (rpc_connect_async(rpc, client.server, 111, pmap_connect_cb, &client) != 0) { |
| 314 | printf("Failed to start connection\n"); |
| 315 | exit(10); |
| 316 | } |
| 317 | |
| 318 | for (;;) { |
| 319 | pfd.fd = rpc_get_fd(rpc); |
| 320 | pfd.events = rpc_which_events(rpc); |
| 321 | |
| 322 | if (poll(&pfd, 1, -1) < 0) { |
| 323 | printf("Poll failed"); |
| 324 | exit(10); |
| 325 | } |
| 326 | if (rpc_service(rpc, pfd.revents) < 0) { |
| 327 | printf("rpc_service failed\n"); |
| 328 | break; |
| 329 | } |
| 330 | if (client.is_finished) { |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | rpc_destroy_context(rpc); |
| 336 | rpc=NULL; |
| 337 | printf("nfsclient finished\n"); |
| 338 | return 0; |
| 339 | } |