Commit | Line | Data |
---|---|---|
84004dbf RS |
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" | |
f4b9cb0e | 23 | #define EXPORT "/shared" |
84004dbf RS |
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" | |
f4b9cb0e | 31 | #include "libnfs-raw-nfs.h" |
84004dbf RS |
32 | |
33 | struct client { | |
34 | char *server; | |
35 | char *export; | |
36 | uint32_t mount_port; | |
37 | int is_finished; | |
f4b9cb0e | 38 | struct nfs_fh3 rootfh; |
84004dbf RS |
39 | }; |
40 | ||
7d0397cf | 41 | void nfs_fsinfo_cb(struct rpc_context *rpc _U_, int status, void *data, void *private_data) |
f4b9cb0e RS |
42 | { |
43 | struct client *client = private_data; | |
44 | FSINFO3res *res = data; | |
45 | ||
46 | if (status == RPC_STATUS_ERROR) { | |
47 | printf("nfs/fsinfo call failed with \"%s\"\n", (char *)data); | |
48 | exit(10); | |
49 | } | |
50 | if (status != RPC_STATUS_SUCCESS) { | |
51 | printf("nfs/fsinfo call to server %s failed, status:%d\n", client->server, status); | |
52 | exit(10); | |
53 | } | |
54 | ||
55 | printf("Got reply from server for NFS/FSINFO procedure.\n"); | |
56 | printf("Read Max:%d\n", (int)res->FSINFO3res_u.resok.rtmax); | |
57 | printf("Write Max:%d\n", (int)res->FSINFO3res_u.resok.wtmax); | |
58 | client->is_finished = 1; | |
59 | } | |
60 | ||
61 | ||
7d0397cf | 62 | void nfs_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data) |
f4b9cb0e RS |
63 | { |
64 | struct client *client = private_data; | |
65 | ||
66 | if (status != RPC_STATUS_SUCCESS) { | |
67 | printf("connection to RPC.MOUNTD on server %s failed\n", client->server); | |
68 | exit(10); | |
69 | } | |
70 | ||
71 | printf("Connected to RPC.NFSDD on %s:%d\n", client->server, client->mount_port); | |
72 | printf("Send FSINFO request\n"); | |
73 | if (rpc_nfs_fsinfo_async(rpc, nfs_fsinfo_cb, &client->rootfh, client) != 0) { | |
74 | printf("Failed to send fsinfo request\n"); | |
75 | exit(10); | |
76 | } | |
77 | } | |
78 | ||
84004dbf RS |
79 | void mount_mnt_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
80 | { | |
81 | struct client *client = private_data; | |
f4b9cb0e | 82 | mountres3 *mnt = data; |
84004dbf RS |
83 | |
84 | if (status == RPC_STATUS_ERROR) { | |
85 | printf("mount/mnt call failed with \"%s\"\n", (char *)data); | |
86 | exit(10); | |
87 | } | |
88 | if (status != RPC_STATUS_SUCCESS) { | |
89 | printf("mount/mnt call to server %s failed, status:%d\n", client->server, status); | |
90 | exit(10); | |
91 | } | |
92 | ||
93 | printf("Got reply from server for MOUNT/MNT procedure.\n"); | |
f4b9cb0e RS |
94 | client->rootfh.data.data_len = mnt->mountres3_u.mountinfo.fhandle.fhandle3_len; |
95 | client->rootfh.data.data_val = malloc(client->rootfh.data.data_len); | |
96 | memcpy(client->rootfh.data.data_val, mnt->mountres3_u.mountinfo.fhandle.fhandle3_val, client->rootfh.data.data_len); | |
97 | ||
98 | printf("Disconnect socket from mountd server\n"); | |
99 | if (rpc_disconnect(rpc, "normal disconnect") != 0) { | |
100 | printf("Failed to disconnect socket to mountd\n"); | |
101 | exit(10); | |
102 | } | |
103 | ||
104 | printf("Connect to RPC.NFSDD on %s:%d\n", client->server, 2049); | |
105 | if (rpc_connect_async(rpc, client->server, 2049, nfs_connect_cb, client) != 0) { | |
106 | printf("Failed to start connection\n"); | |
107 | exit(10); | |
108 | } | |
84004dbf RS |
109 | } |
110 | ||
111 | ||
112 | void mount_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data) | |
113 | { | |
114 | struct client *client = private_data; | |
115 | ||
116 | if (status == RPC_STATUS_ERROR) { | |
117 | printf("mount null call failed with \"%s\"\n", (char *)data); | |
118 | exit(10); | |
119 | } | |
120 | if (status != RPC_STATUS_SUCCESS) { | |
121 | printf("mount null call to server %s failed, status:%d\n", client->server, status); | |
122 | exit(10); | |
123 | } | |
124 | ||
125 | printf("Got reply from server for MOUNT/NULL procedure.\n"); | |
126 | printf("Send MOUNT/MNT command for %s\n", client->export); | |
127 | if (rpc_mount_mnt_async(rpc, mount_mnt_cb, client->export, client) != 0) { | |
128 | printf("Failed to send mnt request\n"); | |
129 | exit(10); | |
130 | } | |
84004dbf RS |
131 | } |
132 | ||
7d0397cf | 133 | void mount_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data) |
84004dbf RS |
134 | { |
135 | struct client *client = private_data; | |
136 | ||
137 | if (status != RPC_STATUS_SUCCESS) { | |
138 | printf("connection to RPC.MOUNTD on server %s failed\n", client->server); | |
139 | exit(10); | |
140 | } | |
141 | ||
142 | printf("Connected to RPC.MOUNTD on %s:%d\n", client->server, client->mount_port); | |
143 | printf("Send NULL request to check if RPC.MOUNTD is actually running\n"); | |
144 | if (rpc_mount_null_async(rpc, mount_null_cb, client) != 0) { | |
145 | printf("Failed to send null request\n"); | |
146 | exit(10); | |
147 | } | |
148 | } | |
149 | ||
150 | ||
151 | void pmap_getport_cb(struct rpc_context *rpc, int status, void *data, void *private_data) | |
152 | { | |
153 | struct client *client = private_data; | |
84004dbf RS |
154 | |
155 | if (status == RPC_STATUS_ERROR) { | |
156 | printf("portmapper getport call failed with \"%s\"\n", (char *)data); | |
157 | exit(10); | |
158 | } | |
159 | if (status != RPC_STATUS_SUCCESS) { | |
160 | printf("portmapper getport call to server %s failed, status:%d\n", client->server, status); | |
161 | exit(10); | |
162 | } | |
163 | ||
164 | client->mount_port = *(uint32_t *)data; | |
165 | printf("GETPORT returned Port:%d\n", client->mount_port); | |
166 | if (client->mount_port == 0) { | |
7d0397cf | 167 | printf("RPC.MOUNTD is not available on server : %s:%d\n", client->server, client->mount_port); |
84004dbf RS |
168 | exit(10); |
169 | } | |
170 | ||
171 | printf("Disconnect socket from portmap server\n"); | |
172 | if (rpc_disconnect(rpc, "normal disconnect") != 0) { | |
173 | printf("Failed to disconnect socket to portmapper\n"); | |
174 | exit(10); | |
175 | } | |
176 | ||
177 | printf("Connect to RPC.MOUNTD on %s:%d\n", client->server, client->mount_port); | |
178 | if (rpc_connect_async(rpc, client->server, client->mount_port, mount_connect_cb, client) != 0) { | |
179 | printf("Failed to start connection\n"); | |
180 | exit(10); | |
181 | } | |
182 | } | |
183 | ||
184 | ||
185 | void pmap_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data) | |
186 | { | |
187 | struct client *client = private_data; | |
188 | ||
189 | if (status == RPC_STATUS_ERROR) { | |
190 | printf("portmapper null call failed with \"%s\"\n", (char *)data); | |
191 | exit(10); | |
192 | } | |
193 | if (status != RPC_STATUS_SUCCESS) { | |
194 | printf("portmapper null call to server %s failed, status:%d\n", client->server, status); | |
195 | exit(10); | |
196 | } | |
197 | ||
198 | printf("Got reply from server for PORTMAP/NULL procedure.\n"); | |
199 | printf("Send getport request asking for MOUNT port\n"); | |
200 | if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, pmap_getport_cb, client) != 0) { | |
201 | printf("Failed to send getport request\n"); | |
202 | exit(10); | |
203 | } | |
204 | } | |
205 | ||
7d0397cf | 206 | void pmap_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data) |
84004dbf RS |
207 | { |
208 | struct client *client = private_data; | |
209 | ||
210 | printf("pmap_connect_cb status:%d.\n", status); | |
211 | if (status != RPC_STATUS_SUCCESS) { | |
212 | printf("connection to portmapper on server %s failed\n", client->server); | |
213 | exit(10); | |
214 | } | |
215 | ||
216 | printf("Send NULL request to check if portmapper is actually running\n"); | |
217 | if (rpc_pmap_null_async(rpc, pmap_null_cb, client) != 0) { | |
218 | printf("Failed to send null request\n"); | |
219 | exit(10); | |
220 | } | |
221 | } | |
222 | ||
223 | ||
7d0397cf | 224 | int main(int argc _U_, char *argv[] _U_) |
84004dbf RS |
225 | { |
226 | struct rpc_context *rpc; | |
227 | struct pollfd pfd; | |
84004dbf RS |
228 | struct client client; |
229 | ||
230 | rpc = rpc_init_context(); | |
231 | if (rpc == NULL) { | |
232 | printf("failed to init context\n"); | |
233 | exit(10); | |
234 | } | |
235 | ||
236 | client.server = SERVER; | |
237 | client.export = EXPORT; | |
238 | client.is_finished = 0; | |
239 | if (rpc_connect_async(rpc, client.server, 111, pmap_connect_cb, &client) != 0) { | |
240 | printf("Failed to start connection\n"); | |
241 | exit(10); | |
242 | } | |
243 | ||
244 | for (;;) { | |
245 | pfd.fd = rpc_get_fd(rpc); | |
246 | pfd.events = rpc_which_events(rpc); | |
247 | ||
248 | if (poll(&pfd, 1, -1) < 0) { | |
249 | printf("Poll failed"); | |
250 | exit(10); | |
251 | } | |
252 | if (rpc_service(rpc, pfd.revents) < 0) { | |
253 | printf("rpc_service failed\n"); | |
254 | break; | |
255 | } | |
256 | if (client.is_finished) { | |
257 | break; | |
258 | } | |
259 | } | |
260 | ||
261 | rpc_destroy_context(rpc); | |
262 | rpc=NULL; | |
263 | printf("nfsclient finished\n"); | |
264 | return 0; | |
265 | } |