2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
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.
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.
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/>.
18 /* Example program using the lowlevel raw interface.
19 * This allow accurate control of the exact commands that are being used.
22 #define SERVER "10.1.1.27"
23 #define EXPORT "/VIRTUAL"
28 #include <ccan/nfs/nfs.h>
29 #include <ccan/nfs/libnfs-raw.h>
30 #include <ccan/nfs/rpc/mount.h>
39 void mount_mnt_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
42 struct client *client = private_data;
44 if (status == RPC_STATUS_ERROR) {
45 printf("mount/mnt call failed with \"%s\"\n", (char *)data);
48 if (status != RPC_STATUS_SUCCESS) {
49 printf("mount/mnt call to server %s failed, status:%d\n", client->server, status);
54 if (res->fhs_status != MNT3_OK) {
55 printf("RPC error: Mount failed with error %s(%d) %s(%d)", mountstat3_to_str(res->fhs_status), res->fhs_status, strerror(-mountstat3_to_errno(res->fhs_status)), -mountstat3_to_errno(res->fhs_status));
59 printf("Got reply from server for MOUNT/MNT procedure.\n");
60 client->is_finished = 1;
64 void mount_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
66 struct client *client = private_data;
68 if (status == RPC_STATUS_ERROR) {
69 printf("mount null call failed with \"%s\"\n", (char *)data);
72 if (status != RPC_STATUS_SUCCESS) {
73 printf("mount null call to server %s failed, status:%d\n", client->server, status);
77 printf("Got reply from server for MOUNT/NULL procedure.\n");
78 printf("Send MOUNT/MNT command for %s\n", client->export);
79 if (rpc_mount_mnt_async(rpc, mount_mnt_cb, client->export, client) != 0) {
80 printf("Failed to send mnt request\n");
86 void mount_connect_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
88 struct client *client = private_data;
90 if (status != RPC_STATUS_SUCCESS) {
91 printf("connection to RPC.MOUNTD on server %s failed\n", client->server);
95 printf("Connected to RPC.MOUNTD on %s:%d\n", client->server, client->mount_port);
96 printf("Send NULL request to check if RPC.MOUNTD is actually running\n");
97 if (rpc_mount_null_async(rpc, mount_null_cb, client) != 0) {
98 printf("Failed to send null request\n");
104 void pmap_getport_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
106 struct client *client = private_data;
109 if (status == RPC_STATUS_ERROR) {
110 printf("portmapper getport call failed with \"%s\"\n", (char *)data);
113 if (status != RPC_STATUS_SUCCESS) {
114 printf("portmapper getport call to server %s failed, status:%d\n", client->server, status);
118 client->mount_port = *(uint32_t *)data;
119 printf("GETPORT returned Port:%d\n", client->mount_port);
120 if (client->mount_port == 0) {
121 printf("RPC.MOUNTD is not available on server : %s\n", client->server, client->mount_port);
125 printf("Disconnect socket from portmap server\n");
126 if (rpc_disconnect(rpc, "normal disconnect") != 0) {
127 printf("Failed to disconnect socket to portmapper\n");
131 printf("Connect to RPC.MOUNTD on %s:%d\n", client->server, client->mount_port);
132 if (rpc_connect_async(rpc, client->server, client->mount_port, 1, mount_connect_cb, client) != 0) {
133 printf("Failed to start connection\n");
139 void pmap_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
141 struct client *client = private_data;
143 if (status == RPC_STATUS_ERROR) {
144 printf("portmapper null call failed with \"%s\"\n", (char *)data);
147 if (status != RPC_STATUS_SUCCESS) {
148 printf("portmapper null call to server %s failed, status:%d\n", client->server, status);
152 printf("Got reply from server for PORTMAP/NULL procedure.\n");
153 printf("Send getport request asking for MOUNT port\n");
154 if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, pmap_getport_cb, client) != 0) {
155 printf("Failed to send getport request\n");
160 void pmap_connect_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
162 struct client *client = private_data;
164 printf("pmap_connect_cb status:%d.\n", status);
165 if (status != RPC_STATUS_SUCCESS) {
166 printf("connection to portmapper on server %s failed\n", client->server);
170 printf("Send NULL request to check if portmapper is actually running\n");
171 if (rpc_pmap_null_async(rpc, pmap_null_cb, client) != 0) {
172 printf("Failed to send null request\n");
178 int main(int argc, char *argv[])
180 struct rpc_context *rpc;
183 struct client client;
185 rpc = rpc_init_context();
187 printf("failed to init context\n");
191 client.server = SERVER;
192 client.export = EXPORT;
193 client.is_finished = 0;
194 if (rpc_connect_async(rpc, client.server, 111, 0, pmap_connect_cb, &client) != 0) {
195 printf("Failed to start connection\n");
200 pfd.fd = rpc_get_fd(rpc);
201 pfd.events = rpc_which_events(rpc);
203 if (poll(&pfd, 1, -1) < 0) {
204 printf("Poll failed");
207 if (rpc_service(rpc, pfd.revents) < 0) {
208 printf("rpc_service failed\n");
211 if (client.is_finished) {
216 rpc_destroy_context(rpc);
218 printf("nfsclient finished\n");