forked from prashanthpai/sunrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportmapper.go
183 lines (144 loc) · 4.1 KB
/
portmapper.go
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package sunrpc
import (
"errors"
"net"
"net/rpc"
"strconv"
"sync"
)
const (
pmapPort = 111
portmapperProgramNumber = 100000
portmapperProgramVersion = 2
)
// Protocol is a type representing the protocol (TCP or UDP) over which the
// program/server being registered listens on.
type Protocol uint32
const (
// IPProtoTCP is the protocol number for TCP/IP
IPProtoTCP Protocol = 6
// IPProtoUDP is the protocol number for UDP/IP
IPProtoUDP Protocol = 17
)
var defaultAddress = "127.0.0.1:" + strconv.Itoa(pmapPort)
// PortMapping is a mapping between (program, version, protocol) to port number
type PortMapping struct {
Program uint32
Version uint32
Protocol uint32
Port uint32
}
var registryInit sync.Once
func initRegistry() {
procedureID := ProcedureID{
ProgramNumber: portmapperProgramNumber,
ProgramVersion: portmapperProgramVersion,
}
// This is ordered as per procedure number
remoteProcedures := [6]string{
"Pmap.ProcNull", "Pmap.ProcSet", "Pmap.ProcUnset",
"Pmap.ProcGetPort", "Pmap.ProcDump", "Pmap.ProcCallIt"}
for id, procName := range remoteProcedures {
procedureID.ProcedureNumber = uint32(id)
_ = RegisterProcedure(Procedure{procedureID, procName}, true)
}
}
func initPmapClient(host string) *rpc.Client {
if host == "" {
host = defaultAddress
}
registryInit.Do(initRegistry)
conn, err := net.Dial("tcp", host)
if err != nil {
return nil
}
return rpc.NewClientWithCodec(NewClientCodec(conn, nil))
}
// PmapSet creates port mapping of the program specified. It return true on
// success and false otherwise.
func PmapSet(programNumber, programVersion uint32, protocol Protocol, port uint32) (bool, error) {
var result bool
client := initPmapClient("")
if client == nil {
return result, errors.New("Could not create pmap client")
}
defer client.Close()
mapping := &PortMapping{
Program: programNumber,
Version: programVersion,
Protocol: uint32(protocol),
Port: port,
}
err := client.Call("Pmap.ProcSet", mapping, &result)
return result, err
}
// PmapUnset will unregister the program specified. It returns true on success
// and false otherwise.
func PmapUnset(programNumber, programVersion uint32) (bool, error) {
var result bool
client := initPmapClient("")
if client == nil {
return result, errors.New("Could not create pmap client")
}
defer client.Close()
mapping := &PortMapping{
Program: programNumber,
Version: programVersion,
}
err := client.Call("Pmap.ProcUnset", mapping, &result)
return result, err
}
// PmapGetPort returns the port number on which the program specified is
// awaiting call requests. If host is empty string, localhost is used.
func PmapGetPort(host string, programNumber, programVersion uint32, protocol Protocol) (uint32, error) {
var port uint32
client := initPmapClient(host)
if client == nil {
return port, errors.New("Could not create pmap client")
}
defer client.Close()
mapping := &PortMapping{
Program: programNumber,
Version: programVersion,
Protocol: uint32(protocol),
}
err := client.Call("Pmap.ProcGetPort", mapping, &port)
return port, err
}
type portMappingList struct {
Map PortMapping
Next *portMappingList `xdr:"optional"`
}
type getMapsReply struct {
Next *portMappingList `xdr:"optional"`
}
// PmapGetMaps returns a list of PortMapping entries present in portmapper's
// database. If host is empty string, localhost is used.
func PmapGetMaps(host string) ([]PortMapping, error) {
var mappings []PortMapping
var result getMapsReply
client := initPmapClient(host)
if client == nil {
return nil, errors.New("Could not create pmap client")
}
defer client.Close()
err := client.Call("Pmap.ProcDump", nil, &result)
if err != nil {
return nil, err
}
if result.Next != nil {
trav := result.Next
for {
entry := PortMapping(trav.Map)
mappings = append(mappings, entry)
trav = trav.Next
if trav == nil {
break
}
}
}
return mappings, nil
}