-
Notifications
You must be signed in to change notification settings - Fork 5
/
portmap_transport.rb
98 lines (79 loc) · 2.14 KB
/
portmap_transport.rb
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
# Helpers to simplify the creation of SUNRPC clients and servers that
# are clients of portmap.
#
# Author: Brian Ollenberger
require 'portmap'
module PortmapTransport
class UDPClient < SUNRPC::UDPClient
def initialize(program, version, address = '127.0.0.1',
portmap_port = Portmap::PMAP_PORT)
# Ask portmap for the port.
client = SUNRPC::UDPClient.new(Portmap::PMAP_PROG, Portmap::PMAP_VERS,
portmap_port, address)
port = client.GETPORT({
:prog => program.number,
:vers => version,
:prot => :UDP_IP,
:port => 0
})
client.shutdown
super(program, version, address, port)
end
end
def PortmapTransport::make_server_shutdown_finalizer(
programs, port, portmap_port)
proc do |id|
# Unregister the programs from the portmap server
SUNRPC::UDPClient.new(Portmap::PMAP_PROG,
Portmap::PMAP_VERS, portmap_port, '127.0.0.1') do |portmap_client|
programs.each_value do |program|
program.each_version do |version|
portmap_client.UNSET({
:prog => program.number,
:vers => version.number,
:prot => :UDP_IP,
:port => port
})
end
end
end
end
end
class UDPServer < SUNRPC::UDPServer
def initialize(programs, listen_port = nil, address = '0.0.0.0',
portmap_port = Portmap::PMAP_PORT)
super(programs, listen_port, address, &nil)
# Tell portmap about each version of each program that we have.
ObjectSpace.define_finalizer(self,
PortmapTransport::make_server_shutdown_finalizer(
@programs, self.port, portmap_port))
SUNRPC::UDPClient.new(Portmap::PMAP_PROG,
Portmap::PMAP_VERS, portmap_port, '127.0.0.1') do |portmap_client|
@programs.each_value do |program|
program.each_version do |version|
if :TRUE != portmap_client.SET({
:prog => program.number,
:vers => version.number,
:prot => :UDP_IP,
:port => self.port})
raise 'unable to add port mapping for program ' +
program.number.to_s + ' version ' +
version.number.to_s
end
end
end
end
if block_given?
begin
yield(self)
ensure
shutdown
end
end
end
def shutdown
@portmap_shutdown.shutdown
super
end
end
end