PORTMAPv3: Add NULL and DUMP commands. Also add portmap example client.
[deb_libnfs.git] / portmap / portmap.c
CommitLineData
84004dbf
RS
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*/
a8a1b858
M
17#ifdef WIN32
18#include "win32_compat.h"
19#endif/*WIN32*/
84004dbf
RS
20
21#include <stdio.h>
763cd6e3 22#include "libnfs-zdr.h"
84004dbf
RS
23#include "libnfs.h"
24#include "libnfs-raw.h"
25#include "libnfs-private.h"
26#include "libnfs-raw-portmap.h"
27
4edd7830
RS
28/*
29 * PORTMAP v2
30 */
0f0e352f 31int rpc_pmap2_null_async(struct rpc_context *rpc, rpc_cb cb, void *private_data)
84004dbf
RS
32{
33 struct rpc_pdu *pdu;
34
0f0e352f 35 pdu = rpc_allocate_pdu(rpc, PMAP_PROGRAM, PMAP_V2, PMAP2_NULL, cb, private_data, (zdrproc_t)zdr_void, 0);
84004dbf 36 if (pdu == NULL) {
0f0e352f 37 rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for PORTMAP2/NULL call");
84004dbf
RS
38 return -1;
39 }
40
41 if (rpc_queue_pdu(rpc, pdu) != 0) {
0f0e352f 42 rpc_set_error(rpc, "Out of memory. Failed to queue pdu for PORTMAP2/NULL call");
84004dbf 43 rpc_free_pdu(rpc, pdu);
1896d37b 44 return -1;
84004dbf
RS
45 }
46
47 return 0;
48}
49
0f0e352f 50int rpc_pmap2_getport_async(struct rpc_context *rpc, int program, int version, int protocol, rpc_cb cb, void *private_data)
84004dbf
RS
51{
52 struct rpc_pdu *pdu;
4edd7830 53 struct pmap2_mapping m;
84004dbf 54
0f0e352f 55 pdu = rpc_allocate_pdu(rpc, PMAP_PROGRAM, PMAP_V2, PMAP2_GETPORT, cb, private_data, (zdrproc_t)zdr_int, sizeof(uint32_t));
84004dbf 56 if (pdu == NULL) {
0f0e352f 57 rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for PORTMAP2/GETPORT call");
84004dbf
RS
58 return -1;
59 }
60
61 m.prog = program;
62 m.vers = version;
5c6b1176 63 m.prot = protocol;
84004dbf 64 m.port = 0;
4edd7830 65 if (zdr_pmap2_mapping(&pdu->zdr, &m) == 0) {
0f0e352f 66 rpc_set_error(rpc, "ZDR error: Failed to encode data for PORTMAP2/GETPORT call");
84004dbf 67 rpc_free_pdu(rpc, pdu);
1896d37b 68 return -1;
84004dbf
RS
69 }
70
71 if (rpc_queue_pdu(rpc, pdu) != 0) {
0f0e352f 72 rpc_set_error(rpc, "Failed to queue PORTMAP2/GETPORT pdu");
84004dbf 73 rpc_free_pdu(rpc, pdu);
1896d37b 74 return -1;
84004dbf
RS
75 }
76
77 return 0;
78}
fd59fd0d 79
0f0e352f 80int rpc_pmap2_set_async(struct rpc_context *rpc, int program, int version, int protocol, int port, rpc_cb cb, void *private_data)
1fbe4080
RS
81{
82 struct rpc_pdu *pdu;
4edd7830 83 struct pmap2_mapping m;
1fbe4080 84
0f0e352f 85 pdu = rpc_allocate_pdu(rpc, PMAP_PROGRAM, PMAP_V2, PMAP2_SET, cb, private_data, (zdrproc_t)zdr_int, sizeof(uint32_t));
1fbe4080 86 if (pdu == NULL) {
0f0e352f 87 rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for PORTMAP2/SET call");
1fbe4080
RS
88 return -1;
89 }
90
91 m.prog = program;
92 m.vers = version;
93 m.prot = protocol;
94 m.port = port;
4edd7830 95 if (zdr_pmap2_mapping(&pdu->zdr, &m) == 0) {
0f0e352f 96 rpc_set_error(rpc, "ZDR error: Failed to encode data for PORTMAP2/SET call");
1fbe4080
RS
97 rpc_free_pdu(rpc, pdu);
98 return -1;
99 }
100
101 if (rpc_queue_pdu(rpc, pdu) != 0) {
0f0e352f 102 rpc_set_error(rpc, "Failed to queue PORTMAP2/SET pdu");
1fbe4080
RS
103 rpc_free_pdu(rpc, pdu);
104 return -1;
105 }
106
107 return 0;
108}
109
0f0e352f 110int rpc_pmap2_unset_async(struct rpc_context *rpc, int program, int version, int protocol, int port, rpc_cb cb, void *private_data)
1fbe4080
RS
111{
112 struct rpc_pdu *pdu;
4edd7830 113 struct pmap2_mapping m;
1fbe4080 114
0f0e352f 115 pdu = rpc_allocate_pdu(rpc, PMAP_PROGRAM, PMAP_V2, PMAP2_UNSET, cb, private_data, (zdrproc_t)zdr_int, sizeof(uint32_t));
1fbe4080 116 if (pdu == NULL) {
0f0e352f 117 rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for PORTMAP2/UNSET call");
1fbe4080
RS
118 return -1;
119 }
120
121 m.prog = program;
122 m.vers = version;
123 m.prot = protocol;
124 m.port = port;
4edd7830 125 if (zdr_pmap2_mapping(&pdu->zdr, &m) == 0) {
0f0e352f 126 rpc_set_error(rpc, "ZDR error: Failed to encode data for PORTMAP2/UNSET call");
1fbe4080
RS
127 rpc_free_pdu(rpc, pdu);
128 return -1;
129 }
130
131 if (rpc_queue_pdu(rpc, pdu) != 0) {
0f0e352f 132 rpc_set_error(rpc, "Failed to queue PORTMAP2/UNSET pdu");
1fbe4080
RS
133 rpc_free_pdu(rpc, pdu);
134 return -1;
135 }
136
137 return 0;
138}
139
0f0e352f 140int rpc_pmap2_dump_async(struct rpc_context *rpc, rpc_cb cb, void *private_data)
8ae943f6
RS
141{
142 struct rpc_pdu *pdu;
143
4edd7830 144 pdu = rpc_allocate_pdu(rpc, PMAP_PROGRAM, PMAP_V2, PMAP2_DUMP, cb, private_data, (zdrproc_t)zdr_pmap2_dump_result, sizeof(pmap2_dump_result));
8ae943f6 145 if (pdu == NULL) {
0f0e352f 146 rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for PORTMAP2/DUMP call");
8ae943f6
RS
147 return -1;
148 }
149
150 if (rpc_queue_pdu(rpc, pdu) != 0) {
0f0e352f 151 rpc_set_error(rpc, "Failed to queue PORTMAP2/DUMP pdu");
8ae943f6
RS
152 rpc_free_pdu(rpc, pdu);
153 return -1;
154 }
155
156 return 0;
157}
158
0f0e352f 159int rpc_pmap2_callit_async(struct rpc_context *rpc, int program, int version, int procedure, char *data, int datalen, rpc_cb cb, void *private_data)
fd59fd0d
RS
160{
161 struct rpc_pdu *pdu;
4edd7830 162 struct pmap2_call_args ca;
fd59fd0d 163
4edd7830 164 pdu = rpc_allocate_pdu(rpc, PMAP_PROGRAM, PMAP_V2, PMAP2_CALLIT, cb, private_data, (zdrproc_t)zdr_pmap2_call_result, sizeof(pmap2_call_result));
fd59fd0d 165 if (pdu == NULL) {
0f0e352f 166 rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for PORTMAP2/CALLIT call");
fd59fd0d
RS
167 return -1;
168 }
169
170 ca.prog = program;
171 ca.vers = version;
172 ca.proc = procedure;
173 ca.args.args_len = datalen;
174 ca.args.args_val = data;
175
4edd7830 176 if (zdr_pmap2_call_args(&pdu->zdr, &ca) == 0) {
0f0e352f 177 rpc_set_error(rpc, "ZDR error: Failed to encode data for PORTMAP2/CALLIT call");
fd59fd0d
RS
178 rpc_free_pdu(rpc, pdu);
179 return -1;
180 }
181
182 if (rpc_queue_pdu(rpc, pdu) != 0) {
0f0e352f 183 rpc_set_error(rpc, "Failed to queue PORTMAP2/CALLIT pdu: %s", rpc_get_error(rpc));
fd59fd0d
RS
184 return -1;
185 }
186
187 return 0;
188}
4edd7830
RS
189
190/*
191 * PORTMAP v3
192 */
193int rpc_pmap3_null_async(struct rpc_context *rpc, rpc_cb cb, void *private_data)
194{
195 struct rpc_pdu *pdu;
196
197 pdu = rpc_allocate_pdu(rpc, PMAP_PROGRAM, PMAP_V3, PMAP3_NULL, cb, private_data, (zdrproc_t)zdr_void, 0);
198 if (pdu == NULL) {
199 rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for PORTMAP3/NULL call");
200 return -1;
201 }
202
203 if (rpc_queue_pdu(rpc, pdu) != 0) {
204 rpc_set_error(rpc, "Out of memory. Failed to queue pdu for PORTMAP3/NULL call");
205 rpc_free_pdu(rpc, pdu);
206 return -1;
207 }
208
209 return 0;
210}
211
212int rpc_pmap3_dump_async(struct rpc_context *rpc, rpc_cb cb, void *private_data)
213{
214 struct rpc_pdu *pdu;
215
216 pdu = rpc_allocate_pdu(rpc, PMAP_PROGRAM, PMAP_V3, PMAP3_DUMP, cb, private_data, (zdrproc_t)zdr_pmap3_dump_result, sizeof(pmap3_dump_result));
217 if (pdu == NULL) {
218 rpc_set_error(rpc, "Out of memory. Failed to allocate pdu for PORTMAP3/DUMP call");
219 return -1;
220 }
221
222 if (rpc_queue_pdu(rpc, pdu) != 0) {
223 rpc_set_error(rpc, "Failed to queue PORTMAP3/DUMP pdu");
224 rpc_free_pdu(rpc, pdu);
225 return -1;
226 }
227
228 return 0;
229}