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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
|
/*
Copyright (c) 2010-2011 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-xdr.h"
#include "xdr-generic.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].type = GF_PMAP_PORT_FREE;
else
pmap->ports[i].type = GF_PMAP_PORT_FOREIGN;
}
pmap->base_port = GF_DEFAULT_BASE_PORT + 2;
pmap->last_alloc = GF_DEFAULT_BASE_PORT + 2;
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;
}
static char*
nextword (char *str)
{
while (*str && !isspace (*str))
str++;
while (*str && isspace (*str))
str++;
return str;
}
int
pmap_registry_search (xlator_t *this, const char *brickname,
gf_pmap_port_type_t type)
{
struct pmap_registry *pmap = NULL;
int p = 0;
char *brck = NULL;
char *nbrck = NULL;
pmap = pmap_registry_get (this);
for (p = pmap->base_port; p <= pmap->last_alloc; p++) {
if (!pmap->ports[p].brickname || pmap->ports[p].type != type)
continue;
for (brck = pmap->ports[p].brickname;;) {
nbrck = strtail (brck, brickname);
if (nbrck && (!*nbrck || isspace (*nbrck)))
return p;
brck = nextword (brck);
if (!*brck)
break;
}
}
return 0;
}
int
pmap_registry_search_by_xprt (xlator_t *this, void *xprt,
gf_pmap_port_type_t type)
{
struct pmap_registry *pmap = NULL;
int p = 0;
int port = 0;
pmap = pmap_registry_get (this);
for (p = pmap->base_port; p <= pmap->last_alloc; p++) {
if (!pmap->ports[p].xprt)
continue;
if (pmap->ports[p].xprt == xprt &&
pmap->ports[p].type == type) {
port = p;
break;
}
}
return port;
}
char *
pmap_registry_search_by_port (xlator_t *this, int port)
{
struct pmap_registry *pmap = NULL;
char *brickname = NULL;
if (port > 65535)
goto out;
pmap = pmap_registry_get (this);
if (pmap->ports[port].type == GF_PMAP_PORT_BRICKSERVER)
brickname = pmap->ports[port].brickname;
out:
return brickname;
}
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->last_alloc; p < 65535; p++) {
if (pmap->ports[p].type != GF_PMAP_PORT_FREE)
continue;
if (pmap_port_isfree (p)) {
pmap->ports[p].type = GF_PMAP_PORT_LEASED;
port = p;
break;
}
}
if (port)
pmap->last_alloc = port;
return port;
}
int
pmap_registry_bind (xlator_t *this, int port, const char *brickname,
gf_pmap_port_type_t type, void *xprt)
{
struct pmap_registry *pmap = NULL;
int p = 0;
pmap = pmap_registry_get (this);
if (port > 65535)
goto out;
p = port;
pmap->ports[p].type = type;
if (pmap->ports[p].brickname)
free (pmap->ports[p].brickname);
pmap->ports[p].brickname = strdup (brickname);
pmap->ports[p].type = type;
pmap->ports[p].xprt = xprt;
gf_log ("pmap", GF_LOG_INFO, "adding brick %s on port %d",
brickname, port);
if (pmap->last_alloc < p)
pmap->last_alloc = p;
out:
return 0;
}
int
pmap_registry_remove (xlator_t *this, int port, const char *brickname,
gf_pmap_port_type_t type, void *xprt)
{
struct pmap_registry *pmap = NULL;
int p = 0;
glusterd_conf_t *priv = NULL;
priv = this->private;
pmap = priv->pmap;
if (!pmap)
goto out;
if (port) {
if (port > 65535)
goto out;
p = port;
goto remove;
}
if (brickname && strchr (brickname, '/')) {
p = pmap_registry_search (this, brickname, type);
if (p)
goto remove;
}
if (xprt) {
p = pmap_registry_search_by_xprt (this, xprt, type);
if (p)
goto remove;
}
goto out;
remove:
gf_log ("pmap", GF_LOG_INFO, "removing brick %s on port %d",
pmap->ports[p].brickname, p);
if (pmap->ports[p].brickname)
free (pmap->ports[p].brickname);
pmap->ports[p].brickname = NULL;
pmap->ports[p].xprt = NULL;
out:
return 0;
}
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_generic (req->msg[0], &args,
(xdrproc_t)xdr_pmap_port_by_brick_req)) {
req->rpc_err = GARBAGE_ARGS;
goto fail;
}
brick = args.brick;
port = pmap_registry_search (THIS, brick, GF_PMAP_PORT_BRICKSERVER);
if (!port)
rsp.op_ret = -1;
rsp.port = port;
fail:
glusterd_submit_reply (req, &rsp, NULL, 0, NULL,
(xdrproc_t)xdr_pmap_port_by_brick_rsp);
if (args.brick)
free (args.brick);//malloced by xdr
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_generic (req->msg[0], &args,
(xdrproc_t)xdr_pmap_brick_by_port_req)) {
req->rpc_err = GARBAGE_ARGS;
goto fail;
}
rsp.brick = pmap_registry_search_by_port (THIS, args.port);
if (!rsp.brick) {
rsp.op_ret = -1;
rsp.brick = "";
}
fail:
glusterd_submit_reply (req, &rsp, NULL, 0, NULL,
(xdrproc_t)xdr_pmap_brick_by_port_rsp);
return 0;
}
static int
glusterd_brick_update_signin (glusterd_brickinfo_t *brickinfo,
gf_boolean_t value)
{
brickinfo->signed_in = value;
return 0;
}
int
gluster_pmap_signup (rpcsvc_request_t *req)
{
pmap_signup_req args = {0,};
pmap_signup_rsp rsp = {0,};
if (!xdr_to_generic (req->msg[0], &args,
(xdrproc_t)xdr_pmap_signup_req)) {
req->rpc_err = GARBAGE_ARGS;
goto fail;
}
rsp.op_ret = pmap_registry_bind (THIS, args.port, args.brick,
GF_PMAP_PORT_BRICKSERVER, req->trans);
fail:
glusterd_submit_reply (req, &rsp, NULL, 0, NULL,
(xdrproc_t)xdr_pmap_signup_rsp);
if (args.brick)
free (args.brick);//malloced by xdr
return 0;
}
int
gluster_pmap_signin (rpcsvc_request_t *req)
{
pmap_signin_req args = {0,};
pmap_signin_rsp rsp = {0,};
glusterd_brickinfo_t *brickinfo = NULL;
int ret = -1;
if (!xdr_to_generic (req->msg[0], &args,
(xdrproc_t)xdr_pmap_signin_req)) {
req->rpc_err = GARBAGE_ARGS;
goto fail;
}
rsp.op_ret = pmap_registry_bind (THIS, args.port, args.brick,
GF_PMAP_PORT_BRICKSERVER, req->trans);
ret = glusterd_get_brickinfo (THIS, args.brick, args.port, _gf_true,
&brickinfo);
fail:
glusterd_submit_reply (req, &rsp, NULL, 0, NULL,
(xdrproc_t)xdr_pmap_signin_rsp);
if (args.brick)
free (args.brick);//malloced by xdr
if (!ret)
glusterd_brick_update_signin (brickinfo, _gf_true);
return 0;
}
int
gluster_pmap_signout (rpcsvc_request_t *req)
{
pmap_signout_req args = {0,};
pmap_signout_rsp rsp = {0,};
int ret = -1;
glusterd_brickinfo_t *brickinfo = NULL;
if (!xdr_to_generic (req->msg[0], &args,
(xdrproc_t)xdr_pmap_signout_req)) {
//failed to decode msg;
req->rpc_err = GARBAGE_ARGS;
goto fail;
}
rsp.op_ret = pmap_registry_remove (THIS, args.port, args.brick,
GF_PMAP_PORT_BRICKSERVER, req->trans);
ret = glusterd_get_brickinfo (THIS, args.brick, args.port, _gf_true,
&brickinfo);
fail:
glusterd_submit_reply (req, &rsp, NULL, 0, NULL,
(xdrproc_t)xdr_pmap_signout_rsp);
if (args.brick)
free (args.brick);//malloced by xdr
if (!ret)
glusterd_brick_update_signin (brickinfo, _gf_false);
return 0;
}
rpcsvc_actor_t gluster_pmap_actors[] = {
[GF_PMAP_NULL] = {"NULL", GF_PMAP_NULL, NULL, NULL, NULL, 0},
[GF_PMAP_PORTBYBRICK] = {"PORTBYBRICK", GF_PMAP_PORTBYBRICK,
gluster_pmap_portbybrick, NULL, NULL, 0},
[GF_PMAP_BRICKBYPORT] = {"BRICKBYPORT", GF_PMAP_BRICKBYPORT,
gluster_pmap_brickbyport, NULL, NULL, 0},
[GF_PMAP_SIGNIN] = {"SIGNIN", GF_PMAP_SIGNIN,
gluster_pmap_signin, NULL, NULL, 0},
[GF_PMAP_SIGNOUT] = {"SIGNOUT", GF_PMAP_SIGNOUT,
gluster_pmap_signout, NULL, NULL, 0},
[GF_PMAP_SIGNUP] = {"SIGNUP", GF_PMAP_SIGNUP,
gluster_pmap_signup, NULL, NULL, 0},
};
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,
};
|