1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/*
Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com>
This file is part of GlusterFS.
GlusterFS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
GlusterFS is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif
#include "xlator.h"
#include "glusterfs.h"
#include "compat-errno.h"
#include "glusterd.h"
#include "glusterd-utils.h"
#include "portmap.h"
#include "protocol-common.h"
#include "rpcsvc.h"
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
int
pmap_port_isfree (int port)
{
struct sockaddr_in sin;
int sock = -1;
int ret = 0;
memset (&sin, 0, sizeof (sin));
sin.sin_family = PF_INET;
sin.sin_port = hton16 (port);
sock = socket (PF_INET, SOCK_STREAM, 0);
if (sock == -1)
return -1;
ret = bind (sock, (struct sockaddr *)&sin, sizeof (sin));
close (sock);
return (ret == 0) ? 1 : 0;
}
struct pmap_registry *
pmap_registry_new (void)
{
struct pmap_registry *pmap = NULL;
int i = 0;
pmap = CALLOC (sizeof (*pmap), 1);
if (!pmap)
return NULL;
for (i = 0; i < 65536; i++) {
if (!pmap_port_isfree (i))
pmap->ports[i].used = 1;
}
pmap->base_port = 6969;
pmap->last_alloc = 6969;
return pmap;
}
struct pmap_registry *
pmap_registry_get (xlator_t *this)
{
glusterd_conf_t *priv = NULL;
struct pmap_registry *pmap = NULL;
priv = this->private;
pmap = priv->pmap;
if (!pmap) {
pmap = pmap_registry_new ();
if (!pmap)
return NULL;
priv->pmap = pmap;
}
return pmap;
}
int
pmap_registry_search (xlator_t *this, const char *brickname)
{
struct pmap_registry *pmap = NULL;
int p = 0;
int port = 0;
pmap = pmap_registry_get (this);
for (p = pmap->base_port; p < 65535; p++) {
if (!pmap->ports[p].brickname)
continue;
if (strcmp (pmap->ports[p].brickname, brickname) == 0) {
port = p;
break;
}
}
return port;
}
int
pmap_registry_alloc (xlator_t *this)
{
struct pmap_registry *pmap = NULL;
int p = 0;
int port = 0;
pmap = pmap_registry_get (this);
for (p = pmap->base_port; p < 65535; p++) {
if (pmap->ports[p].used)
continue;
if (pmap_port_isfree (p)) {
pmap->ports[p].used = 1;
port = p;
break;
}
}
return port;
}
int
pmap_registry_bind (xlator_t *this, int port, const char *brickname)
{
struct pmap_registry *pmap = NULL;
int p = 0;
pmap = pmap_registry_get (this);
if (port > 65535)
goto out;
p = port;
pmap->ports[p].used = 1;
if (pmap->ports[p].brickname)
free (pmap->ports[p].brickname);
pmap->ports[p].brickname = strdup (brickname);
out:
return 0;
}
typedef ssize_t (*gfs_serialize_t) (struct iovec outmsg, void *data);
static int
xdr_to_glusterfs_req (rpcsvc_request_t *req, void *arg, gfs_serialize_t sfunc)
{
int ret = -1;
if (!req)
return -1;
ret = sfunc (req->msg[0], arg);
if (ret > 0)
ret = 0;
return ret;
}
int
gluster_pmap_portbybrick (rpcsvc_request_t *req)
{
pmap_port_by_brick_req args = {0,};
pmap_port_by_brick_rsp rsp = {0,};
char *brick = NULL;
int port = 0;
if (xdr_to_glusterfs_req (req, &args, xdr_to_pmap_port_by_brick_req)) {
req->rpc_err = GARBAGE_ARGS;
goto fail;
}
brick = args.brick;
port = pmap_registry_search (THIS, brick);
if (!port)
rsp.op_ret = -1;
rsp.port = port;
fail:
glusterd_submit_reply (req, &rsp, NULL, 0, NULL,
(gd_serialize_t)xdr_from_pmap_port_by_brick_rsp);
return 0;
}
int
gluster_pmap_brickbyport (rpcsvc_request_t *req)
{
pmap_brick_by_port_req args = {0,};
pmap_brick_by_port_rsp rsp = {0,};
if (xdr_to_glusterfs_req (req, &args, xdr_to_pmap_brick_by_port_req)) {
req->rpc_err = GARBAGE_ARGS;
goto fail;
}
fail:
glusterd_submit_reply (req, &rsp, NULL, 0, NULL,
(gd_serialize_t)xdr_from_pmap_brick_by_port_rsp);
return 0;
}
int
gluster_pmap_signup (rpcsvc_request_t *req)
{
pmap_signup_req args = {0,};
pmap_signup_rsp rsp = {0,};
if (xdr_to_glusterfs_req (req, &args, xdr_to_pmap_signup_req)) {
req->rpc_err = GARBAGE_ARGS;
goto fail;
}
fail:
glusterd_submit_reply (req, &rsp, NULL, 0, NULL,
(gd_serialize_t)xdr_from_pmap_signup_rsp);
return 0;
}
int
gluster_pmap_signin (rpcsvc_request_t *req)
{
pmap_signin_req args = {0,};
pmap_signin_rsp rsp = {0,};
if (xdr_to_glusterfs_req (req, &args, xdr_to_pmap_signin_req)) {
req->rpc_err = GARBAGE_ARGS;
goto fail;
}
fail:
glusterd_submit_reply (req, &rsp, NULL, 0, NULL,
(gd_serialize_t)xdr_from_pmap_signin_rsp);
return 0;
}
int
gluster_pmap_signout (rpcsvc_request_t *req)
{
pmap_signout_req args = {0,};
pmap_signout_rsp rsp = {0,};
if (xdr_to_glusterfs_req (req, &args, xdr_to_pmap_signout_req)) {
//failed to decode msg;
req->rpc_err = GARBAGE_ARGS;
goto fail;
}
fail:
glusterd_submit_reply (req, &rsp, NULL, 0, NULL,
(gd_serialize_t)xdr_from_pmap_signout_rsp);
return 0;
}
rpcsvc_actor_t gluster_pmap_actors[] = {
[GF_PMAP_NULL] = {"NULL", GF_HNDSK_NULL, NULL, NULL, NULL },
[GF_PMAP_PORTBYBRICK] = {"PORTBYBRICK", GF_PMAP_PORTBYBRICK,
gluster_pmap_portbybrick, NULL, NULL },
[GF_PMAP_BRICKBYPORT] = {"BRICKBYPORT", GF_PMAP_BRICKBYPORT,
gluster_pmap_brickbyport, NULL, NULL },
[GF_PMAP_SIGNIN] = {"SIGNIN", GF_PMAP_SIGNIN,
gluster_pmap_signin, NULL, NULL },
[GF_PMAP_SIGNOUT] = {"SIGNOUT", GF_PMAP_SIGNOUT,
gluster_pmap_signout, NULL, NULL },
[GF_PMAP_SIGNUP] = {"SIGNUP", GF_PMAP_SIGNUP,
gluster_pmap_signup, NULL, NULL },
};
struct rpcsvc_program gluster_pmap_prog = {
.progname = "Gluster Portmap",
.prognum = GLUSTER_PMAP_PROGRAM,
.progver = GLUSTER_PMAP_VERSION,
.actors = gluster_pmap_actors,
.numactors = GF_PMAP_MAXVALUE,
};
|