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> | |
a62cb4b7 | 27 | #include <string.h> |
84004dbf RS |
28 | #include <poll.h> |
29 | #include "libnfs.h" | |
30 | #include "libnfs-raw.h" | |
31 | #include "libnfs-raw-mount.h" | |
f4b9cb0e | 32 | #include "libnfs-raw-nfs.h" |
05a777d9 | 33 | #include "libnfs-raw-rquota.h" |
84004dbf RS |
34 | |
35 | struct client { | |
36 | char *server; | |
37 | char *export; | |
38 | uint32_t mount_port; | |
05a777d9 | 39 | uint32_t rquota_port; |
84004dbf | 40 | int is_finished; |
f4b9cb0e | 41 | struct nfs_fh3 rootfh; |
84004dbf RS |
42 | }; |
43 | ||
05a777d9 RS |
44 | void rquota_getquota_cb(struct rpc_context *rpc _U_, int status, void *data, void *private_data) |
45 | { | |
46 | struct client *client = private_data; | |
440a7dfc | 47 | // GETQUOTA1res *res = data; |
05a777d9 RS |
48 | |
49 | if (status == RPC_STATUS_ERROR) { | |
50 | printf("rquota/getquota call failed with \"%s\"\n", (char *)data); | |
51 | exit(10); | |
52 | } | |
53 | if (status != RPC_STATUS_SUCCESS) { | |
54 | printf("rquota/getquota call to server %s failed, status:%d\n", client->server, status); | |
55 | exit(10); | |
56 | } | |
57 | ||
58 | printf("rquota responded ok\n"); | |
59 | client->is_finished = 1; | |
60 | } | |
61 | ||
62 | void rquota_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data) | |
63 | { | |
64 | struct client *client = private_data; | |
65 | ||
66 | if (status != RPC_STATUS_SUCCESS) { | |
67 | printf("connection to RPC.RQUOTAD on server %s failed\n", client->server); | |
68 | exit(10); | |
69 | } | |
70 | ||
71 | printf("Connected to RPC.RQUOTAD on %s:%d\n", client->server, client->rquota_port); | |
72 | printf("Send GETQUOTA request for uid 100\n"); | |
73 | if (rpc_rquota1_getquota_async(rpc, rquota_getquota_cb, EXPORT, 100, client) != 0) { | |
74 | printf("Failed to send fsinfo request\n"); | |
75 | exit(10); | |
76 | } | |
77 | } | |
78 | ||
7d0397cf | 79 | void nfs_fsinfo_cb(struct rpc_context *rpc _U_, int status, void *data, void *private_data) |
f4b9cb0e RS |
80 | { |
81 | struct client *client = private_data; | |
82 | FSINFO3res *res = data; | |
83 | ||
84 | if (status == RPC_STATUS_ERROR) { | |
85 | printf("nfs/fsinfo call failed with \"%s\"\n", (char *)data); | |
86 | exit(10); | |
87 | } | |
88 | if (status != RPC_STATUS_SUCCESS) { | |
89 | printf("nfs/fsinfo call to server %s failed, status:%d\n", client->server, status); | |
90 | exit(10); | |
91 | } | |
92 | ||
93 | printf("Got reply from server for NFS/FSINFO procedure.\n"); | |
94 | printf("Read Max:%d\n", (int)res->FSINFO3res_u.resok.rtmax); | |
95 | printf("Write Max:%d\n", (int)res->FSINFO3res_u.resok.wtmax); | |
05a777d9 RS |
96 | |
97 | printf("Disconnect socket from nfs server\n"); | |
98 | if (rpc_disconnect(rpc, "normal disconnect") != 0) { | |
99 | printf("Failed to disconnect socket to nfs\n"); | |
100 | exit(10); | |
101 | } | |
102 | ||
103 | printf("Connect to RPC.RQUOTAD on %s:%d\n", client->server, client->rquota_port); | |
104 | if (rpc_connect_async(rpc, client->server, client->rquota_port, rquota_connect_cb, client) != 0) { | |
105 | printf("Failed to start connection\n"); | |
106 | exit(10); | |
107 | } | |
f4b9cb0e RS |
108 | } |
109 | ||
110 | ||
7d0397cf | 111 | void nfs_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data) |
f4b9cb0e RS |
112 | { |
113 | struct client *client = private_data; | |
114 | ||
115 | if (status != RPC_STATUS_SUCCESS) { | |
116 | printf("connection to RPC.MOUNTD on server %s failed\n", client->server); | |
117 | exit(10); | |
118 | } | |
119 | ||
05a777d9 | 120 | printf("Connected to RPC.NFSD on %s:%d\n", client->server, client->mount_port); |
f4b9cb0e RS |
121 | printf("Send FSINFO request\n"); |
122 | if (rpc_nfs_fsinfo_async(rpc, nfs_fsinfo_cb, &client->rootfh, client) != 0) { | |
123 | printf("Failed to send fsinfo request\n"); | |
124 | exit(10); | |
125 | } | |
126 | } | |
127 | ||
84004dbf RS |
128 | void mount_mnt_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
129 | { | |
130 | struct client *client = private_data; | |
f4b9cb0e | 131 | mountres3 *mnt = data; |
84004dbf RS |
132 | |
133 | if (status == RPC_STATUS_ERROR) { | |
134 | printf("mount/mnt call failed with \"%s\"\n", (char *)data); | |
135 | exit(10); | |
136 | } | |
137 | if (status != RPC_STATUS_SUCCESS) { | |
138 | printf("mount/mnt call to server %s failed, status:%d\n", client->server, status); | |
139 | exit(10); | |
140 | } | |
141 | ||
142 | printf("Got reply from server for MOUNT/MNT procedure.\n"); | |
f4b9cb0e RS |
143 | client->rootfh.data.data_len = mnt->mountres3_u.mountinfo.fhandle.fhandle3_len; |
144 | client->rootfh.data.data_val = malloc(client->rootfh.data.data_len); | |
145 | memcpy(client->rootfh.data.data_val, mnt->mountres3_u.mountinfo.fhandle.fhandle3_val, client->rootfh.data.data_len); | |
146 | ||
147 | printf("Disconnect socket from mountd server\n"); | |
148 | if (rpc_disconnect(rpc, "normal disconnect") != 0) { | |
149 | printf("Failed to disconnect socket to mountd\n"); | |
150 | exit(10); | |
151 | } | |
152 | ||
05a777d9 | 153 | printf("Connect to RPC.NFSD on %s:%d\n", client->server, 2049); |
f4b9cb0e RS |
154 | if (rpc_connect_async(rpc, client->server, 2049, nfs_connect_cb, client) != 0) { |
155 | printf("Failed to start connection\n"); | |
156 | exit(10); | |
157 | } | |
84004dbf RS |
158 | } |
159 | ||
160 | ||
440a7dfc RS |
161 | |
162 | void mount_export_cb(struct rpc_context *rpc, int status, void *data, void *private_data) | |
84004dbf RS |
163 | { |
164 | struct client *client = private_data; | |
440a7dfc | 165 | exports export = *(exports *)data; |
84004dbf RS |
166 | |
167 | if (status == RPC_STATUS_ERROR) { | |
168 | printf("mount null call failed with \"%s\"\n", (char *)data); | |
169 | exit(10); | |
170 | } | |
171 | if (status != RPC_STATUS_SUCCESS) { | |
172 | printf("mount null call to server %s failed, status:%d\n", client->server, status); | |
173 | exit(10); | |
174 | } | |
175 | ||
440a7dfc RS |
176 | printf("Got reply from server for MOUNT/EXPORT procedure.\n"); |
177 | while (export != NULL) { | |
178 | printf("Export: %s\n", export->ex_dir); | |
179 | export = export->ex_next; | |
180 | } | |
84004dbf RS |
181 | printf("Send MOUNT/MNT command for %s\n", client->export); |
182 | if (rpc_mount_mnt_async(rpc, mount_mnt_cb, client->export, client) != 0) { | |
183 | printf("Failed to send mnt request\n"); | |
184 | exit(10); | |
185 | } | |
84004dbf RS |
186 | } |
187 | ||
440a7dfc RS |
188 | void mount_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
189 | { | |
190 | struct client *client = private_data; | |
191 | ||
192 | if (status == RPC_STATUS_ERROR) { | |
193 | printf("mount null call failed with \"%s\"\n", (char *)data); | |
194 | exit(10); | |
195 | } | |
196 | if (status != RPC_STATUS_SUCCESS) { | |
197 | printf("mount null call to server %s failed, status:%d\n", client->server, status); | |
198 | exit(10); | |
199 | } | |
200 | ||
201 | printf("Got reply from server for MOUNT/NULL procedure.\n"); | |
202 | printf("Send MOUNT/EXPORT command\n"); | |
203 | if (rpc_mount_export_async(rpc, mount_export_cb, client) != 0) { | |
204 | printf("Failed to send export request\n"); | |
205 | exit(10); | |
206 | } | |
207 | } | |
208 | ||
7d0397cf | 209 | void mount_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data) |
84004dbf RS |
210 | { |
211 | struct client *client = private_data; | |
212 | ||
213 | if (status != RPC_STATUS_SUCCESS) { | |
214 | printf("connection to RPC.MOUNTD on server %s failed\n", client->server); | |
215 | exit(10); | |
216 | } | |
217 | ||
218 | printf("Connected to RPC.MOUNTD on %s:%d\n", client->server, client->mount_port); | |
219 | printf("Send NULL request to check if RPC.MOUNTD is actually running\n"); | |
220 | if (rpc_mount_null_async(rpc, mount_null_cb, client) != 0) { | |
221 | printf("Failed to send null request\n"); | |
222 | exit(10); | |
223 | } | |
224 | } | |
225 | ||
226 | ||
05a777d9 | 227 | void pmap_getport2_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
84004dbf RS |
228 | { |
229 | struct client *client = private_data; | |
84004dbf RS |
230 | |
231 | if (status == RPC_STATUS_ERROR) { | |
232 | printf("portmapper getport call failed with \"%s\"\n", (char *)data); | |
233 | exit(10); | |
234 | } | |
235 | if (status != RPC_STATUS_SUCCESS) { | |
236 | printf("portmapper getport call to server %s failed, status:%d\n", client->server, status); | |
237 | exit(10); | |
238 | } | |
239 | ||
240 | client->mount_port = *(uint32_t *)data; | |
05a777d9 | 241 | printf("GETPORT returned RPC.MOUNTD is on port:%d\n", client->mount_port); |
84004dbf | 242 | if (client->mount_port == 0) { |
7d0397cf | 243 | printf("RPC.MOUNTD is not available on server : %s:%d\n", client->server, client->mount_port); |
84004dbf RS |
244 | exit(10); |
245 | } | |
246 | ||
247 | printf("Disconnect socket from portmap server\n"); | |
248 | if (rpc_disconnect(rpc, "normal disconnect") != 0) { | |
249 | printf("Failed to disconnect socket to portmapper\n"); | |
250 | exit(10); | |
251 | } | |
252 | ||
253 | printf("Connect to RPC.MOUNTD on %s:%d\n", client->server, client->mount_port); | |
254 | if (rpc_connect_async(rpc, client->server, client->mount_port, mount_connect_cb, client) != 0) { | |
255 | printf("Failed to start connection\n"); | |
256 | exit(10); | |
257 | } | |
258 | } | |
259 | ||
05a777d9 RS |
260 | void pmap_getport1_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
261 | { | |
262 | struct client *client = private_data; | |
263 | ||
264 | if (status == RPC_STATUS_ERROR) { | |
265 | printf("portmapper getport call failed with \"%s\"\n", (char *)data); | |
266 | exit(10); | |
267 | } | |
268 | if (status != RPC_STATUS_SUCCESS) { | |
269 | printf("portmapper getport call to server %s failed, status:%d\n", client->server, status); | |
270 | exit(10); | |
271 | } | |
272 | ||
273 | client->rquota_port = *(uint32_t *)data; | |
274 | printf("GETPORT returned RPC.RQUOTAD on port:%d\n", client->rquota_port); | |
275 | if (client->rquota_port == 0) { | |
276 | printf("RPC.RQUOTAD is not available on server : %s:%d\n", client->server, client->rquota_port); | |
440a7dfc | 277 | // exit(10); |
05a777d9 RS |
278 | } |
279 | ||
280 | printf("Send getport request asking for MOUNT port\n"); | |
281 | if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, pmap_getport2_cb, client) != 0) { | |
282 | printf("Failed to send getport request\n"); | |
283 | exit(10); | |
284 | } | |
285 | } | |
84004dbf RS |
286 | |
287 | void pmap_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data) | |
288 | { | |
289 | struct client *client = private_data; | |
290 | ||
291 | if (status == RPC_STATUS_ERROR) { | |
292 | printf("portmapper null call failed with \"%s\"\n", (char *)data); | |
293 | exit(10); | |
294 | } | |
295 | if (status != RPC_STATUS_SUCCESS) { | |
296 | printf("portmapper null call to server %s failed, status:%d\n", client->server, status); | |
297 | exit(10); | |
298 | } | |
299 | ||
300 | printf("Got reply from server for PORTMAP/NULL procedure.\n"); | |
301 | printf("Send getport request asking for MOUNT port\n"); | |
05a777d9 | 302 | if (rpc_pmap_getport_async(rpc, RQUOTA_PROGRAM, RQUOTA_V1, pmap_getport1_cb, client) != 0) { |
84004dbf RS |
303 | printf("Failed to send getport request\n"); |
304 | exit(10); | |
305 | } | |
306 | } | |
307 | ||
7d0397cf | 308 | void pmap_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data) |
84004dbf RS |
309 | { |
310 | struct client *client = private_data; | |
311 | ||
312 | printf("pmap_connect_cb status:%d.\n", status); | |
313 | if (status != RPC_STATUS_SUCCESS) { | |
314 | printf("connection to portmapper on server %s failed\n", client->server); | |
315 | exit(10); | |
316 | } | |
317 | ||
318 | printf("Send NULL request to check if portmapper is actually running\n"); | |
319 | if (rpc_pmap_null_async(rpc, pmap_null_cb, client) != 0) { | |
320 | printf("Failed to send null request\n"); | |
321 | exit(10); | |
322 | } | |
323 | } | |
324 | ||
325 | ||
7d0397cf | 326 | int main(int argc _U_, char *argv[] _U_) |
84004dbf RS |
327 | { |
328 | struct rpc_context *rpc; | |
329 | struct pollfd pfd; | |
84004dbf RS |
330 | struct client client; |
331 | ||
332 | rpc = rpc_init_context(); | |
333 | if (rpc == NULL) { | |
334 | printf("failed to init context\n"); | |
335 | exit(10); | |
336 | } | |
337 | ||
338 | client.server = SERVER; | |
339 | client.export = EXPORT; | |
340 | client.is_finished = 0; | |
341 | if (rpc_connect_async(rpc, client.server, 111, pmap_connect_cb, &client) != 0) { | |
342 | printf("Failed to start connection\n"); | |
343 | exit(10); | |
344 | } | |
345 | ||
346 | for (;;) { | |
347 | pfd.fd = rpc_get_fd(rpc); | |
348 | pfd.events = rpc_which_events(rpc); | |
349 | ||
350 | if (poll(&pfd, 1, -1) < 0) { | |
351 | printf("Poll failed"); | |
352 | exit(10); | |
353 | } | |
354 | if (rpc_service(rpc, pfd.revents) < 0) { | |
355 | printf("rpc_service failed\n"); | |
356 | break; | |
357 | } | |
358 | if (client.is_finished) { | |
359 | break; | |
360 | } | |
361 | } | |
362 | ||
363 | rpc_destroy_context(rpc); | |
364 | rpc=NULL; | |
365 | printf("nfsclient finished\n"); | |
366 | return 0; | |
367 | } |