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
|
/* Copyright © 2003 Russ Cox, MIT; see /sys/src/libsunrpc/COPYING */
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <sunrpc.h>
int chatty;
SunClient *client;
void
usage(void)
{
fprint(2, "usage: portmap address [cmd]\n"
"cmd is one of:\n"
"\tnull\n"
"\tset prog vers proto port\n"
"\tunset prog vers proto port\n"
"\tgetport prog vers proto\n"
"\tdump (default)\n");
threadexitsall("usage");
}
void
portCall(SunCall *c, PortCallType type)
{
c->rpc.prog = PortProgram;
c->rpc.vers = PortVersion;
c->rpc.proc = type>>1;
c->rpc.iscall = !(type&1);
c->type = type;
}
void
tnull(char **argv)
{
PortTNull tx;
PortRNull rx;
USED(argv);
memset(&tx, 0, sizeof tx);
portCall(&tx.call, PortCallTNull);
memset(&rx, 0, sizeof rx);
portCall(&rx.call, PortCallRNull);
if(sunClientRpc(client, 0, &tx.call, &rx.call, nil) < 0)
sysfatal("rpc: %r");
}
void
tset(char **argv)
{
PortTSet tx;
PortRSet rx;
memset(&tx, 0, sizeof tx);
portCall(&tx.call, PortCallTSet);
tx.map.prog = strtol(argv[0], 0, 0);
tx.map.vers = strtol(argv[1], 0, 0);
tx.map.prot = strtol(argv[2], 0, 0);
tx.map.port = strtol(argv[3], 0, 0);
memset(&rx, 0, sizeof rx);
portCall(&rx.call, PortCallRSet);
if(sunClientRpc(client, 0, &tx.call, &rx.call, nil) < 0)
sysfatal("rpc: %r");
if(rx.b == 0)
print("rejected\n");
}
void
tunset(char **argv)
{
PortTUnset tx;
PortRUnset rx;
memset(&tx, 0, sizeof tx);
portCall(&tx.call, PortCallTUnset);
tx.map.prog = strtol(argv[0], 0, 0);
tx.map.vers = strtol(argv[1], 0, 0);
tx.map.prot = strtol(argv[2], 0, 0);
tx.map.port = strtol(argv[3], 0, 0);
memset(&rx, 0, sizeof rx);
portCall(&rx.call, PortCallRUnset);
if(sunClientRpc(client, 0, &tx.call, &rx.call, nil) < 0)
sysfatal("rpc: %r");
if(rx.b == 0)
print("rejected\n");
}
void
tgetport(char **argv)
{
PortTGetport tx;
PortRGetport rx;
memset(&tx, 0, sizeof tx);
portCall(&tx.call, PortCallTGetport);
tx.map.prog = strtol(argv[0], 0, 0);
tx.map.vers = strtol(argv[1], 0, 0);
tx.map.prot = strtol(argv[2], 0, 0);
memset(&rx, 0, sizeof rx);
portCall(&rx.call, PortCallRGetport);
if(sunClientRpc(client, 0, &tx.call, &rx.call, nil) < 0)
sysfatal("rpc: %r");
print("%d\n", rx.port);
}
void
tdump(char **argv)
{
int i;
uchar *p;
PortTDump tx;
PortRDump rx;
PortMap *m;
USED(argv);
memset(&tx, 0, sizeof tx);
portCall(&tx.call, PortCallTDump);
memset(&rx, 0, sizeof rx);
portCall(&rx.call, PortCallRDump);
if(sunClientRpc(client, 0, &tx.call, &rx.call, &p) < 0)
sysfatal("rpc: %r");
for(i=0, m=rx.map; i<rx.nmap; i++, m++)
print("%ud %ud %ud %ud\n", (uint)m->prog, (uint)m->vers, (uint)m->prot, (uint)m->port);
free(p);
}
static struct {
char *cmd;
int narg;
void (*fn)(char**);
} tab[] = {
"null", 0, tnull,
"set", 4, tset,
"unset", 4, tunset,
"getport", 3, tgetport,
"dump", 0, tdump,
};
void
threadmain(int argc, char **argv)
{
char *dflt[] = { "dump", };
char *addr, *cmd;
int i;
ARGBEGIN{
case 'R':
chatty++;
break;
}ARGEND
if(argc < 1)
usage();
fmtinstall('B', sunRpcFmt);
fmtinstall('C', sunCallFmt);
fmtinstall('H', encodefmt);
sunFmtInstall(&portProg);
addr = netmkaddr(argv[0], "udp", "portmap");
if((client = sunDial(addr)) == nil)
sysfatal("dial %s: %r", addr);
client->chatty = chatty;
sunClientProg(client, &portProg);
argv++;
argc--;
if(argc == 0){
argc = 1;
argv = dflt;
}
cmd = argv[0];
argv++;
argc--;
for(i=0; i<nelem(tab); i++){
if(strcmp(tab[i].cmd, cmd) == 0){
if(tab[i].narg != argc)
usage();
(*tab[i].fn)(argv);
threadexitsall(nil);
}
}
usage();
}
|