line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- mode: perl -*- |
2
|
|
|
|
|
|
|
# ============================================================================ |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Net::SNMP::Transport; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# $Id: Transport.pm,v 3.0 2009/09/09 15:05:33 dtown Rel $ |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# Base object for the Net::SNMP Transport Domain objects. |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# Copyright (c) 2004-2009 David M. Town |
11
|
|
|
|
|
|
|
# All rights reserved. |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# This program is free software; you may redistribute it and/or modify it |
14
|
|
|
|
|
|
|
# under the same terms as the Perl 5 programming language system itself. |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# ============================================================================ |
17
|
|
|
|
|
|
|
|
18
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
113
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
## Version of the Net::SNMP::Transport module |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our $VERSION = v3.0.0; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
## Handle importing/exporting of symbols |
25
|
|
|
|
|
|
|
|
26
|
2
|
|
|
2
|
|
10
|
use base qw( Exporter ); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
11235
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
our @EXPORT_OK = qw( TRUE FALSE DEBUG_INFO ); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
31
|
|
|
|
|
|
|
domains => [ |
32
|
|
|
|
|
|
|
qw( DOMAIN_UDP DOMAIN_UDPIPV4 DOMAIN_UDPIPV6 DOMAIN_UDPIPV6Z |
33
|
|
|
|
|
|
|
DOMAIN_TCPIPV4 DOMAIN_TCPIPV6 DOMAIN_TCPIPV6Z ) |
34
|
|
|
|
|
|
|
], |
35
|
|
|
|
|
|
|
msgsize => [ qw( MSG_SIZE_DEFAULT MSG_SIZE_MINIMUM MSG_SIZE_MAXIMUM ) ], |
36
|
|
|
|
|
|
|
ports => [ qw( SNMP_PORT SNMP_TRAP_PORT ) ], |
37
|
|
|
|
|
|
|
retries => [ qw( RETRIES_DEFAULT RETRIES_MINIMUM RETRIES_MAXIMUM ) ], |
38
|
|
|
|
|
|
|
timeout => [ qw( TIMEOUT_DEFAULT TIMEOUT_MINIMUM TIMEOUT_MAXIMUM ) ], |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Exporter::export_ok_tags( qw( domains msgsize ports retries timeout ) ); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$EXPORT_TAGS{ALL} = [ @EXPORT_OK ]; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
## Transport Layer Domain definitions |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# RFC 3417 Transport Mappings for SNMP |
48
|
|
|
|
|
|
|
# Presuhn, Case, McCloghrie, Rose, and Waldbusser; December 2002 |
49
|
|
|
|
|
|
|
|
50
|
2
|
|
|
2
|
0
|
8
|
sub DOMAIN_UDP { '1.3.6.1.6.1.1' } # snmpUDPDomain |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# RFC 3419 Textual Conventions for Transport Addresses |
53
|
|
|
|
|
|
|
# Consultant, Schoenwaelder, and Braunschweig; December 2002 |
54
|
|
|
|
|
|
|
|
55
|
11
|
|
|
11
|
0
|
29
|
sub DOMAIN_UDPIPV4 { '1.3.6.1.2.1.100.1.1' } # transportDomainUdpIpv4 |
56
|
11
|
|
|
11
|
0
|
45
|
sub DOMAIN_UDPIPV6 { '1.3.6.1.2.1.100.1.2' } # transportDomainUdpIpv6 |
57
|
2
|
|
|
2
|
0
|
6
|
sub DOMAIN_UDPIPV6Z { '1.3.6.1.2.1.100.1.4' } # transportDomainUdpIpv6z |
58
|
9
|
|
|
9
|
0
|
29
|
sub DOMAIN_TCPIPV4 { '1.3.6.1.2.1.100.1.5' } # transportDomainTcpIpv4 |
59
|
11
|
|
|
11
|
0
|
75
|
sub DOMAIN_TCPIPV6 { '1.3.6.1.2.1.100.1.6' } # transportDomainTcpIpv6 |
60
|
2
|
|
|
2
|
0
|
7
|
sub DOMAIN_TCPIPV6Z { '1.3.6.1.2.1.100.1.8' } # transportDomainTcpIpv6z |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
## SNMP well-known ports |
63
|
|
|
|
|
|
|
|
64
|
3
|
|
|
3
|
0
|
20
|
sub SNMP_PORT { 161 } |
65
|
0
|
|
|
0
|
0
|
0
|
sub SNMP_TRAP_PORT { 162 } |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
## RFC 3411 - snmpEngineMaxMessageSize::=INTEGER (484..2147483647) |
68
|
|
|
|
|
|
|
|
69
|
0
|
|
|
0
|
0
|
0
|
sub MSG_SIZE_DEFAULT { 484 } |
70
|
0
|
|
|
0
|
0
|
0
|
sub MSG_SIZE_MINIMUM { 484 } |
71
|
0
|
|
|
0
|
0
|
0
|
sub MSG_SIZE_MAXIMUM { 65535 } # 2147483647 is not reasonable |
72
|
|
|
|
|
|
|
|
73
|
3
|
|
|
3
|
0
|
10
|
sub RETRIES_DEFAULT { 1 } |
74
|
0
|
|
|
0
|
0
|
0
|
sub RETRIES_MINIMUM { 0 } |
75
|
0
|
|
|
0
|
0
|
0
|
sub RETRIES_MAXIMUM { 20 } |
76
|
|
|
|
|
|
|
|
77
|
3
|
|
|
3
|
0
|
40
|
sub TIMEOUT_DEFAULT { 5.0 } |
78
|
0
|
|
|
0
|
0
|
0
|
sub TIMEOUT_MINIMUM { 1.0 } |
79
|
0
|
|
|
0
|
0
|
0
|
sub TIMEOUT_MAXIMUM { 60.0 } |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
## Truth values |
82
|
|
|
|
|
|
|
|
83
|
12
|
|
|
12
|
0
|
65
|
sub TRUE { 0x01 } |
84
|
5
|
|
|
5
|
0
|
19
|
sub FALSE { 0x00 } |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
## Shared socket array indexes |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
0
|
|
0
|
sub _SHARED_SOCKET { 0 } # Shared Socket object |
89
|
2
|
|
|
2
|
|
8
|
sub _SHARED_REFC { 1 } # Reference count |
90
|
1
|
|
|
1
|
|
7
|
sub _SHARED_MAXSIZE { 2 } # Shared maxMsgSize |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
## Package variables |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
our $DEBUG = FALSE; # Debug flag |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
our $AUTOLOAD; # Used by the AUTOLOAD method |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
our $SOCKETS = {}; # List of shared sockets |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# [public methods] ----------------------------------------------------------- |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
{ |
103
|
|
|
|
|
|
|
my $domains = { |
104
|
|
|
|
|
|
|
'udp/?(?:ip)?v?4?', DOMAIN_UDPIPV4, |
105
|
|
|
|
|
|
|
quotemeta DOMAIN_UDP, DOMAIN_UDPIPV4, |
106
|
|
|
|
|
|
|
quotemeta DOMAIN_UDPIPV4, DOMAIN_UDPIPV4, |
107
|
|
|
|
|
|
|
'udp/?(?:ip)?v?6', DOMAIN_UDPIPV6, |
108
|
|
|
|
|
|
|
quotemeta DOMAIN_UDPIPV6, DOMAIN_UDPIPV6, |
109
|
|
|
|
|
|
|
quotemeta DOMAIN_UDPIPV6Z, DOMAIN_UDPIPV6, |
110
|
|
|
|
|
|
|
'tcp/?(?:ip)?v?4?', DOMAIN_TCPIPV4, |
111
|
|
|
|
|
|
|
quotemeta DOMAIN_TCPIPV4, DOMAIN_TCPIPV4, |
112
|
|
|
|
|
|
|
'tcp/?(?:ip)?v?6', DOMAIN_TCPIPV6, |
113
|
|
|
|
|
|
|
quotemeta DOMAIN_TCPIPV6, DOMAIN_TCPIPV6, |
114
|
|
|
|
|
|
|
quotemeta DOMAIN_TCPIPV6Z, DOMAIN_TCPIPV6, |
115
|
|
|
|
|
|
|
}; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub new |
118
|
|
|
|
|
|
|
{ |
119
|
3
|
|
|
3
|
0
|
264
|
my ($class, %argv) = @_; |
120
|
|
|
|
|
|
|
|
121
|
3
|
|
|
|
|
13
|
my $domain = DOMAIN_UDPIPV4; |
122
|
3
|
|
|
|
|
9
|
my $error = q{}; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# See if a Transport Layer Domain argument has been passed. |
125
|
|
|
|
|
|
|
|
126
|
3
|
|
|
|
|
12
|
for (keys %argv) { |
127
|
|
|
|
|
|
|
|
128
|
2
|
50
|
|
|
|
15
|
if (/^-?domain$/i) { |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
0
|
my $key = $argv{$_}; |
131
|
0
|
|
|
|
|
0
|
$domain = undef; |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
0
|
for (keys %{$domains}) { |
|
0
|
|
|
|
|
0
|
|
134
|
0
|
0
|
|
|
|
0
|
if ($key =~ /^$_$/i) { |
135
|
0
|
|
|
|
|
0
|
$domain = $domains->{$_}; |
136
|
0
|
|
|
|
|
0
|
last; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
0
|
0
|
|
|
|
0
|
if (!defined $domain) { |
141
|
0
|
|
|
|
|
0
|
$error = err_msg( |
142
|
|
|
|
|
|
|
'The transport domain "%s" is unknown', $argv{$_} |
143
|
|
|
|
|
|
|
); |
144
|
0
|
0
|
|
|
|
0
|
return wantarray ? (undef, $error) : undef; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
0
|
$argv{$_} = $domain; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
# Return the appropriate object based on the Transport Domain. To |
153
|
|
|
|
|
|
|
# avoid consuming unnecessary resources, only load the appropriate |
154
|
|
|
|
|
|
|
# module when requested. Some modules require non-core modules and |
155
|
|
|
|
|
|
|
# if these modules are not present, we gracefully return an error. |
156
|
|
|
|
|
|
|
|
157
|
3
|
50
|
|
|
|
30
|
if ($domain eq DOMAIN_UDPIPV6) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
158
|
|
|
|
|
|
|
|
159
|
0
|
0
|
|
|
|
0
|
if (defined ($error = load_module('Net::SNMP::Transport::IPv6::UDP'))) |
160
|
|
|
|
|
|
|
{ |
161
|
0
|
|
|
|
|
0
|
$error = 'UDP/IPv6 support is unavailable ' . $error; |
162
|
0
|
0
|
|
|
|
0
|
return wantarray ? (undef, $error) : undef; |
163
|
|
|
|
|
|
|
} |
164
|
0
|
|
|
|
|
0
|
return Net::SNMP::Transport::IPv6::UDP->new(%argv); |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
} elsif ($domain eq DOMAIN_TCPIPV6) { |
167
|
|
|
|
|
|
|
|
168
|
0
|
0
|
|
|
|
0
|
if (defined ($error = load_module('Net::SNMP::Transport::IPv6::TCP'))) |
169
|
|
|
|
|
|
|
{ |
170
|
0
|
|
|
|
|
0
|
$error = 'TCP/IPv6 support is unavailable ' . $error; |
171
|
0
|
0
|
|
|
|
0
|
return wantarray ? (undef, $error) : undef; |
172
|
|
|
|
|
|
|
} |
173
|
0
|
|
|
|
|
0
|
return Net::SNMP::Transport::IPv6::TCP->new(%argv); |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
} elsif ($domain eq DOMAIN_TCPIPV4) { |
176
|
|
|
|
|
|
|
|
177
|
0
|
0
|
|
|
|
0
|
if (defined ($error = load_module('Net::SNMP::Transport::IPv4::TCP'))) |
178
|
|
|
|
|
|
|
{ |
179
|
0
|
|
|
|
|
0
|
$error = 'TCP/IPv4 support is unavailable ' . $error; |
180
|
0
|
0
|
|
|
|
0
|
return wantarray ? (undef, $error) : undef; |
181
|
|
|
|
|
|
|
} |
182
|
0
|
|
|
|
|
0
|
return Net::SNMP::Transport::IPv4::TCP->new(%argv); |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
# Load the default Transport Domain module without eval protection. |
187
|
|
|
|
|
|
|
|
188
|
3
|
|
|
|
|
1362
|
require Net::SNMP::Transport::IPv4::UDP; |
189
|
3
|
|
|
|
|
27
|
return Net::SNMP::Transport::IPv4::UDP->new(%argv); |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
sub max_msg_size |
195
|
|
|
|
|
|
|
{ |
196
|
0
|
|
|
0
|
0
|
0
|
my ($this, $size) = @_; |
197
|
|
|
|
|
|
|
|
198
|
0
|
0
|
|
|
|
0
|
if (@_ < 2) { |
199
|
0
|
|
|
|
|
0
|
return $this->{_max_msg_size}; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
0
|
|
|
|
|
0
|
$this->_error_clear(); |
203
|
|
|
|
|
|
|
|
204
|
0
|
0
|
|
|
|
0
|
if ($size !~ m/^\d+$/) { |
205
|
0
|
|
|
|
|
0
|
return $this->_error( |
206
|
|
|
|
|
|
|
'The maxMsgSize value "%s" is expected in positive numeric format', |
207
|
|
|
|
|
|
|
$size |
208
|
|
|
|
|
|
|
); |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
0
|
0
|
0
|
|
|
0
|
if ($size < MSG_SIZE_MINIMUM || $size > MSG_SIZE_MAXIMUM) { |
212
|
0
|
|
|
|
|
0
|
return $this->_error( |
213
|
|
|
|
|
|
|
'The maxMsgSize value %s is out of range (%d..%d)', |
214
|
|
|
|
|
|
|
$size, MSG_SIZE_MINIMUM, MSG_SIZE_MAXIMUM |
215
|
|
|
|
|
|
|
); |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
# Adjust the share maximum size if necessary. |
219
|
0
|
|
|
|
|
0
|
$this->_shared_max_size($size); |
220
|
|
|
|
|
|
|
|
221
|
0
|
|
|
|
|
0
|
return $this->{_max_msg_size} = $size; |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
sub timeout |
225
|
|
|
|
|
|
|
{ |
226
|
0
|
|
|
0
|
0
|
0
|
my ($this, $timeout) = @_; |
227
|
|
|
|
|
|
|
|
228
|
0
|
0
|
|
|
|
0
|
if (@_ < 2) { |
229
|
0
|
|
|
|
|
0
|
return $this->{_timeout}; |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
|
232
|
0
|
|
|
|
|
0
|
$this->_error_clear(); |
233
|
|
|
|
|
|
|
|
234
|
0
|
0
|
|
|
|
0
|
if ($timeout !~ m/^\d+(?:\.\d+)?$/) { |
235
|
0
|
|
|
|
|
0
|
return $this->_error( |
236
|
|
|
|
|
|
|
'The timeout value "%s" is expected in positive numeric format', |
237
|
|
|
|
|
|
|
$timeout |
238
|
|
|
|
|
|
|
); |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
0
|
0
|
0
|
|
|
0
|
if ($timeout < TIMEOUT_MINIMUM || $timeout > TIMEOUT_MAXIMUM) { |
242
|
0
|
|
|
|
|
0
|
return $this->_error( |
243
|
|
|
|
|
|
|
'The timeout value %s is out of range (%d..%d)', |
244
|
|
|
|
|
|
|
$timeout, TIMEOUT_MINIMUM, TIMEOUT_MAXIMUM |
245
|
|
|
|
|
|
|
); |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
|
248
|
0
|
|
|
|
|
0
|
return $this->{_timeout} = $timeout; |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
sub retries |
252
|
|
|
|
|
|
|
{ |
253
|
0
|
|
|
0
|
0
|
0
|
my ($this, $retries) = @_; |
254
|
|
|
|
|
|
|
|
255
|
0
|
0
|
|
|
|
0
|
if (@_ < 2) { |
256
|
0
|
|
|
|
|
0
|
return $this->{_retries}; |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
|
259
|
0
|
|
|
|
|
0
|
$this->_error_clear(); |
260
|
|
|
|
|
|
|
|
261
|
0
|
0
|
|
|
|
0
|
if ($retries !~ m/^\d+$/) { |
262
|
0
|
|
|
|
|
0
|
return $this->_error( |
263
|
|
|
|
|
|
|
'The retries value "%s" is expected in positive numeric format', |
264
|
|
|
|
|
|
|
$retries |
265
|
|
|
|
|
|
|
); |
266
|
|
|
|
|
|
|
} |
267
|
|
|
|
|
|
|
|
268
|
0
|
0
|
0
|
|
|
0
|
if ($retries < RETRIES_MINIMUM || $retries > RETRIES_MAXIMUM) { |
269
|
0
|
|
|
|
|
0
|
return $this->_error( |
270
|
|
|
|
|
|
|
'The retries value %s is out of range (%d..%d)', |
271
|
|
|
|
|
|
|
$retries, RETRIES_MINIMUM, RETRIES_MAXIMUM |
272
|
|
|
|
|
|
|
); |
273
|
|
|
|
|
|
|
} |
274
|
|
|
|
|
|
|
|
275
|
0
|
|
|
|
|
0
|
return $this->{_retries} = $retries; |
276
|
|
|
|
|
|
|
} |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
sub agent_addr |
279
|
|
|
|
|
|
|
{ |
280
|
0
|
|
|
0
|
0
|
0
|
return '0.0.0.0'; |
281
|
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
sub connectionless |
284
|
|
|
|
|
|
|
{ |
285
|
12
|
|
|
12
|
0
|
34
|
return TRUE; |
286
|
|
|
|
|
|
|
} |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
sub debug |
289
|
|
|
|
|
|
|
{ |
290
|
0
|
0
|
|
0
|
0
|
0
|
return (@_ == 2) ? $DEBUG = ($_[1]) ? TRUE : FALSE : $DEBUG; |
|
|
0
|
|
|
|
|
|
291
|
|
|
|
|
|
|
} |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
sub domain |
294
|
|
|
|
|
|
|
{ |
295
|
0
|
|
|
0
|
0
|
0
|
return '0.0'; |
296
|
|
|
|
|
|
|
} |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
sub error |
299
|
|
|
|
|
|
|
{ |
300
|
0
|
|
0
|
0
|
0
|
0
|
return $_[0]->{_error} || q{}; |
301
|
|
|
|
|
|
|
} |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
sub fileno |
304
|
|
|
|
|
|
|
{ |
305
|
5
|
50
|
|
5
|
0
|
51
|
return defined($_[0]->{_socket}) ? $_[0]->{_socket}->fileno() : undef; |
306
|
|
|
|
|
|
|
} |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
sub socket |
309
|
|
|
|
|
|
|
{ |
310
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->{_socket}; |
311
|
|
|
|
|
|
|
} |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
sub type |
314
|
|
|
|
|
|
|
{ |
315
|
0
|
|
|
0
|
0
|
0
|
return ''; # unknown(0) |
316
|
|
|
|
|
|
|
} |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
sub sock_name |
319
|
|
|
|
|
|
|
{ |
320
|
0
|
0
|
|
0
|
0
|
0
|
if (defined $_[0]->{_socket}) { |
321
|
0
|
|
0
|
|
|
0
|
return $_[0]->{_socket}->sockname() || $_[0]->{_sock_name}; |
322
|
|
|
|
|
|
|
} else { |
323
|
0
|
|
|
|
|
0
|
return $_[0]->{_sock_name}; |
324
|
|
|
|
|
|
|
} |
325
|
|
|
|
|
|
|
} |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
sub sock_hostname |
328
|
|
|
|
|
|
|
{ |
329
|
0
|
|
0
|
0
|
0
|
0
|
return $_[0]->{_sock_hostname} || $_[0]->sock_address(); |
330
|
|
|
|
|
|
|
} |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
sub sock_address |
333
|
|
|
|
|
|
|
{ |
334
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_address($_[0]->sock_name()); |
335
|
|
|
|
|
|
|
} |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
sub sock_addr |
338
|
|
|
|
|
|
|
{ |
339
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_addr($_[0]->sock_name()); |
340
|
|
|
|
|
|
|
} |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
sub sock_port |
343
|
|
|
|
|
|
|
{ |
344
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_port($_[0]->sock_name()); |
345
|
|
|
|
|
|
|
} |
346
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
sub sock_taddress |
348
|
|
|
|
|
|
|
{ |
349
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_taddress($_[0]->sock_name()); |
350
|
|
|
|
|
|
|
} |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
sub sock_taddr |
353
|
|
|
|
|
|
|
{ |
354
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_taddr($_[0]->sock_name()); |
355
|
|
|
|
|
|
|
} |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
sub sock_tdomain |
358
|
|
|
|
|
|
|
{ |
359
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_tdomain($_[0]->sock_name()); |
360
|
|
|
|
|
|
|
} |
361
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
sub dest_name |
363
|
|
|
|
|
|
|
{ |
364
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->{_dest_name}; |
365
|
|
|
|
|
|
|
} |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
sub dest_hostname |
368
|
|
|
|
|
|
|
{ |
369
|
0
|
|
0
|
0
|
0
|
0
|
return $_[0]->{_dest_hostname} || $_[0]->dest_address(); |
370
|
|
|
|
|
|
|
} |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
sub dest_address |
373
|
|
|
|
|
|
|
{ |
374
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_address($_[0]->dest_name()); |
375
|
|
|
|
|
|
|
} |
376
|
|
|
|
|
|
|
|
377
|
|
|
|
|
|
|
sub dest_addr |
378
|
|
|
|
|
|
|
{ |
379
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_addr($_[0]->dest_name()); |
380
|
|
|
|
|
|
|
} |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
sub dest_port |
383
|
|
|
|
|
|
|
{ |
384
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_port($_[0]->dest_name()); |
385
|
|
|
|
|
|
|
} |
386
|
|
|
|
|
|
|
|
387
|
|
|
|
|
|
|
sub dest_taddress |
388
|
|
|
|
|
|
|
{ |
389
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_taddress($_[0]->dest_name()); |
390
|
|
|
|
|
|
|
} |
391
|
|
|
|
|
|
|
|
392
|
|
|
|
|
|
|
sub dest_taddr |
393
|
|
|
|
|
|
|
{ |
394
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_taddr($_[0]->dest_name()); |
395
|
|
|
|
|
|
|
} |
396
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
sub dest_tdomain |
398
|
|
|
|
|
|
|
{ |
399
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_tdomain($_[0]->dest_name()); |
400
|
|
|
|
|
|
|
} |
401
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
sub peer_name |
403
|
|
|
|
|
|
|
{ |
404
|
0
|
0
|
|
0
|
0
|
0
|
if (defined $_[0]->{_socket}) { |
405
|
0
|
|
0
|
|
|
0
|
return $_[0]->{_socket}->peername() || $_[0]->dest_name(); |
406
|
|
|
|
|
|
|
} else { |
407
|
0
|
|
|
|
|
0
|
return $_[0]->dest_name(); |
408
|
|
|
|
|
|
|
} |
409
|
|
|
|
|
|
|
} |
410
|
|
|
|
|
|
|
|
411
|
|
|
|
|
|
|
sub peer_hostname |
412
|
|
|
|
|
|
|
{ |
413
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->peer_address(); |
414
|
|
|
|
|
|
|
} |
415
|
|
|
|
|
|
|
|
416
|
|
|
|
|
|
|
sub peer_address |
417
|
|
|
|
|
|
|
{ |
418
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_address($_[0]->peer_name()); |
419
|
|
|
|
|
|
|
} |
420
|
|
|
|
|
|
|
|
421
|
|
|
|
|
|
|
sub peer_addr |
422
|
|
|
|
|
|
|
{ |
423
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_addr($_[0]->peer_name()); |
424
|
|
|
|
|
|
|
} |
425
|
|
|
|
|
|
|
|
426
|
|
|
|
|
|
|
sub peer_port |
427
|
|
|
|
|
|
|
{ |
428
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_port($_[0]->peer_name()); |
429
|
|
|
|
|
|
|
} |
430
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
sub peer_taddress |
432
|
|
|
|
|
|
|
{ |
433
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_taddress($_[0]->peer_name()); |
434
|
|
|
|
|
|
|
} |
435
|
|
|
|
|
|
|
|
436
|
|
|
|
|
|
|
sub peer_taddr |
437
|
|
|
|
|
|
|
{ |
438
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_taddr($_[0]->peer_name()); |
439
|
|
|
|
|
|
|
} |
440
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
sub peer_tdomain |
442
|
|
|
|
|
|
|
{ |
443
|
0
|
|
|
0
|
0
|
0
|
return $_[0]->_tdomain($_[0]->peer_name()); |
444
|
|
|
|
|
|
|
} |
445
|
|
|
|
|
|
|
|
446
|
|
|
|
|
|
|
sub AUTOLOAD |
447
|
|
|
|
|
|
|
{ |
448
|
0
|
|
|
0
|
|
0
|
my $this = shift; |
449
|
|
|
|
|
|
|
|
450
|
0
|
0
|
|
|
|
0
|
return if $AUTOLOAD =~ /::DESTROY$/; |
451
|
|
|
|
|
|
|
|
452
|
0
|
|
|
|
|
0
|
$AUTOLOAD =~ s/.*://; |
453
|
|
|
|
|
|
|
|
454
|
0
|
0
|
|
|
|
0
|
if (ref $this) { |
455
|
0
|
0
|
0
|
|
|
0
|
if (defined($this->{_socket}) && ($this->{_socket}->can($AUTOLOAD))) { |
456
|
0
|
|
|
|
|
0
|
return $this->{_socket}->$AUTOLOAD(@_); |
457
|
|
|
|
|
|
|
} else { |
458
|
0
|
|
|
|
|
0
|
$this->_error_clear(); |
459
|
0
|
|
|
|
|
0
|
return $this->_error( |
460
|
|
|
|
|
|
|
'The method "%s" is not supported by this Transport Domain', |
461
|
|
|
|
|
|
|
$AUTOLOAD |
462
|
|
|
|
|
|
|
); |
463
|
|
|
|
|
|
|
} |
464
|
|
|
|
|
|
|
} else { |
465
|
0
|
|
|
|
|
0
|
require Carp; |
466
|
0
|
|
|
|
|
0
|
Carp::croak(sprintf 'The function "%s" is not supported', $AUTOLOAD); |
467
|
|
|
|
|
|
|
} |
468
|
|
|
|
|
|
|
|
469
|
|
|
|
|
|
|
# Never get here. |
470
|
0
|
|
|
|
|
0
|
return; |
471
|
|
|
|
|
|
|
} |
472
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
sub DESTROY |
474
|
|
|
|
|
|
|
{ |
475
|
2
|
|
|
2
|
|
24
|
my ($this) = @_; |
476
|
|
|
|
|
|
|
|
477
|
|
|
|
|
|
|
# Connection-oriented transports do not share sockets. |
478
|
2
|
50
|
|
|
|
7
|
return if !$this->connectionless(); |
479
|
|
|
|
|
|
|
|
480
|
|
|
|
|
|
|
# If the shared socket structure exists, decrement the reference count |
481
|
|
|
|
|
|
|
# and clear the shared socket structure if it is no longer being used. |
482
|
|
|
|
|
|
|
|
483
|
2
|
50
|
33
|
|
|
17
|
if (defined($this->{_sock_name}) && exists $SOCKETS->{$this->{_sock_name}}) { |
484
|
2
|
50
|
|
|
|
7
|
if (--$SOCKETS->{$this->{_sock_name}}->[_SHARED_REFC] < 1) { |
485
|
2
|
|
|
|
|
6
|
delete $SOCKETS->{$this->{_sock_name}}; |
486
|
|
|
|
|
|
|
} |
487
|
|
|
|
|
|
|
} |
488
|
|
|
|
|
|
|
|
489
|
2
|
|
|
|
|
145
|
return; |
490
|
|
|
|
|
|
|
} |
491
|
|
|
|
|
|
|
|
492
|
|
|
|
|
|
|
## Obsolete methods - previous deprecated |
493
|
|
|
|
|
|
|
|
494
|
|
|
|
|
|
|
sub OBSOLETE |
495
|
|
|
|
|
|
|
{ |
496
|
0
|
|
|
0
|
0
|
0
|
my ($this, $method) = splice @_, 0, 2; |
497
|
|
|
|
|
|
|
|
498
|
0
|
|
|
|
|
0
|
require Carp; |
499
|
0
|
|
|
|
|
0
|
Carp::croak( |
500
|
|
|
|
|
|
|
sprintf '%s() is obsolete, use %s() instead', (caller 1)[3], $method |
501
|
|
|
|
|
|
|
); |
502
|
|
|
|
|
|
|
|
503
|
|
|
|
|
|
|
# Never get here. |
504
|
0
|
|
|
|
|
0
|
return $this->${\$method}(@_); |
|
0
|
|
|
|
|
0
|
|
505
|
|
|
|
|
|
|
} |
506
|
|
|
|
|
|
|
|
507
|
0
|
|
|
0
|
0
|
0
|
sub name { return $_[0]->OBSOLETE('type'); } |
508
|
|
|
|
|
|
|
|
509
|
0
|
|
|
0
|
0
|
0
|
sub srcaddr { return $_[0]->OBSOLETE('sock_addr'); } |
510
|
|
|
|
|
|
|
|
511
|
0
|
|
|
0
|
0
|
0
|
sub srcport { return $_[0]->OBSOLETE('sock_port'); } |
512
|
|
|
|
|
|
|
|
513
|
0
|
|
|
0
|
0
|
0
|
sub srchost { return $_[0]->OBSOLETE('sock_address'); } |
514
|
|
|
|
|
|
|
|
515
|
0
|
|
|
0
|
0
|
0
|
sub srcname { return $_[0]->OBSOLETE('sock_address'); } |
516
|
|
|
|
|
|
|
|
517
|
0
|
|
|
0
|
0
|
0
|
sub dstaddr { return $_[0]->OBSOLETE('dest_addr'); } |
518
|
|
|
|
|
|
|
|
519
|
0
|
|
|
0
|
0
|
0
|
sub dstport { return $_[0]->OBSOLETE('dest_port'); } |
520
|
|
|
|
|
|
|
|
521
|
0
|
|
|
0
|
0
|
0
|
sub dsthost { return $_[0]->OBSOLETE('dest_address'); } |
522
|
|
|
|
|
|
|
|
523
|
0
|
|
|
0
|
0
|
0
|
sub dstname { return $_[0]->OBSOLETE('dest_hostname'); } |
524
|
|
|
|
|
|
|
|
525
|
0
|
|
|
0
|
0
|
0
|
sub recvaddr { return $_[0]->OBSOLETE('peer_addr'); } |
526
|
|
|
|
|
|
|
|
527
|
0
|
|
|
0
|
0
|
0
|
sub recvport { return $_[0]->OBSOLETE('peer_port'); } |
528
|
|
|
|
|
|
|
|
529
|
0
|
|
|
0
|
0
|
0
|
sub recvhost { return $_[0]->OBSOLETE('peer_address'); } |
530
|
|
|
|
|
|
|
|
531
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
# [private methods] ---------------------------------------------------------- |
533
|
|
|
|
|
|
|
|
534
|
|
|
|
|
|
|
sub _new |
535
|
|
|
|
|
|
|
{ |
536
|
3
|
|
|
3
|
|
9
|
my ($class, %argv) = @_; |
537
|
|
|
|
|
|
|
|
538
|
3
|
|
|
|
|
15
|
my $this = bless { |
539
|
|
|
|
|
|
|
'_dest_hostname' => 'localhost', # Destination hostname |
540
|
|
|
|
|
|
|
'_dest_name' => undef, # Destination sockaddr |
541
|
|
|
|
|
|
|
'_error' => undef, # Error message |
542
|
|
|
|
|
|
|
'_max_msg_size' => $class->_msg_size_default(), # maxMsgSize |
543
|
|
|
|
|
|
|
'_retries' => RETRIES_DEFAULT, # Number of retries |
544
|
|
|
|
|
|
|
'_socket' => undef, # Socket object |
545
|
|
|
|
|
|
|
'_sock_hostname' => q{}, # Socket hostname |
546
|
|
|
|
|
|
|
'_sock_name' => undef, # Socket sockaddr |
547
|
|
|
|
|
|
|
'_timeout' => TIMEOUT_DEFAULT, # Timeout period (secs) |
548
|
|
|
|
|
|
|
}, $class; |
549
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
# Default the values for the "name (sockaddr) hashes". |
551
|
|
|
|
|
|
|
|
552
|
3
|
|
|
|
|
21
|
my $sock_nh = { port => 0, addr => $this->_addr_any() }; |
553
|
3
|
|
|
|
|
11
|
my $dest_nh = { port => SNMP_PORT, addr => $this->_addr_loopback() }; |
554
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
# Validate the "port" arguments first to allow for a consistency |
556
|
|
|
|
|
|
|
# check with any values passed with the "address" arguments. |
557
|
|
|
|
|
|
|
|
558
|
3
|
|
|
|
|
9
|
my ($dest_port, $sock_port, $listen) = (undef, undef, 0); |
559
|
|
|
|
|
|
|
|
560
|
3
|
|
|
|
|
50
|
for (keys %argv) { |
561
|
|
|
|
|
|
|
|
562
|
2
|
50
|
|
|
|
39
|
if (/^-?debug$/i) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
563
|
0
|
|
|
|
|
0
|
$this->debug(delete $argv{$_}); |
564
|
|
|
|
|
|
|
} elsif (/^-?(?:de?st|peer)?port$/i) { |
565
|
1
|
|
|
|
|
5
|
$this->_service_resolve(delete($argv{$_}), $dest_nh); |
566
|
1
|
|
|
|
|
2
|
$dest_port = $dest_nh->{port}; |
567
|
|
|
|
|
|
|
} elsif (/^-?(?:src|sock|local)port$/i) { |
568
|
1
|
|
|
|
|
11
|
$this->_service_resolve(delete($argv{$_}), $sock_nh); |
569
|
1
|
|
|
|
|
2
|
$sock_port = $sock_nh->{port}; |
570
|
|
|
|
|
|
|
} |
571
|
|
|
|
|
|
|
|
572
|
2
|
50
|
|
|
|
17
|
if (defined $this->{_error}) { |
573
|
0
|
0
|
|
|
|
0
|
return wantarray ? (undef, $this->{_error}) : undef; |
574
|
|
|
|
|
|
|
} |
575
|
|
|
|
|
|
|
} |
576
|
|
|
|
|
|
|
|
577
|
|
|
|
|
|
|
# Validate the rest of the arguments. |
578
|
|
|
|
|
|
|
|
579
|
3
|
|
|
|
|
9
|
for (keys %argv) { |
580
|
|
|
|
|
|
|
|
581
|
0
|
0
|
0
|
|
|
0
|
if (/^-?domain$/i) { |
|
|
0
|
0
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
582
|
0
|
0
|
|
|
|
0
|
if ($argv{$_} ne $this->domain()) { |
583
|
0
|
|
|
|
|
0
|
$this->_error( |
584
|
|
|
|
|
|
|
'The domain value "%s" was expected, but "%s" was found', |
585
|
|
|
|
|
|
|
$this->domain(), $argv{$_} |
586
|
|
|
|
|
|
|
); |
587
|
|
|
|
|
|
|
} |
588
|
|
|
|
|
|
|
} elsif ((/^-?hostname$/i) || (/^-?(?:de?st|peer)?addr$/i)) { |
589
|
0
|
|
|
|
|
0
|
$this->_hostname_resolve( |
590
|
|
|
|
|
|
|
$this->{_dest_hostname} = $argv{$_}, $dest_nh |
591
|
|
|
|
|
|
|
); |
592
|
0
|
0
|
0
|
|
|
0
|
if (defined($dest_port) && ($dest_port != $dest_nh->{port})) { |
593
|
0
|
|
|
|
|
0
|
$this->_error( |
594
|
|
|
|
|
|
|
'Inconsistent %s port information was specified (%d != %d)', |
595
|
|
|
|
|
|
|
$this->type(), $dest_port, $dest_nh->{port} |
596
|
|
|
|
|
|
|
); |
597
|
|
|
|
|
|
|
} |
598
|
|
|
|
|
|
|
} elsif (/^-?(?:src|sock|local)addr$/i) { |
599
|
0
|
|
|
|
|
0
|
$this->_hostname_resolve( |
600
|
|
|
|
|
|
|
$this->{_sock_hostname} = $argv{$_}, $sock_nh |
601
|
|
|
|
|
|
|
); |
602
|
0
|
0
|
0
|
|
|
0
|
if (defined($sock_port) && ($sock_port != $sock_nh->{port})) { |
603
|
0
|
|
|
|
|
0
|
$this->_error( |
604
|
|
|
|
|
|
|
'Inconsistent %s port information was specified (%d != %d)', |
605
|
|
|
|
|
|
|
$this->type(), $sock_port, $sock_nh->{port} |
606
|
|
|
|
|
|
|
); |
607
|
|
|
|
|
|
|
} |
608
|
|
|
|
|
|
|
} elsif (/^-?listen$/i) { |
609
|
0
|
0
|
0
|
|
|
0
|
if (($argv{$_} !~ /^\d+$/) || ($argv{$_} < 1)) { |
|
|
0
|
|
|
|
|
|
610
|
0
|
|
|
|
|
0
|
$this->_error( |
611
|
|
|
|
|
|
|
'The listen queue size value "%s" was expected in positive ' . |
612
|
|
|
|
|
|
|
'non-zero numeric format', $argv{$_} |
613
|
|
|
|
|
|
|
); |
614
|
|
|
|
|
|
|
} elsif (!$this->connectionless()) { |
615
|
0
|
|
|
|
|
0
|
$listen = $argv{$_}; |
616
|
|
|
|
|
|
|
} |
617
|
|
|
|
|
|
|
} elsif ((/^-?maxmsgsize$/i) || (/^-?mtu$/i)) { |
618
|
0
|
|
|
|
|
0
|
$this->max_msg_size($argv{$_}); |
619
|
|
|
|
|
|
|
} elsif (/^-?retries$/i) { |
620
|
0
|
|
|
|
|
0
|
$this->retries($argv{$_}); |
621
|
|
|
|
|
|
|
} elsif (/^-?timeout$/i) { |
622
|
0
|
|
|
|
|
0
|
$this->timeout($argv{$_}); |
623
|
|
|
|
|
|
|
} else { |
624
|
0
|
|
|
|
|
0
|
$this->_error('The argument "%s" is unknown', $_); |
625
|
|
|
|
|
|
|
} |
626
|
|
|
|
|
|
|
|
627
|
0
|
0
|
|
|
|
0
|
if (defined $this->{_error}) { |
628
|
0
|
0
|
|
|
|
0
|
return wantarray ? (undef, $this->{_error}) : undef; |
629
|
|
|
|
|
|
|
} |
630
|
|
|
|
|
|
|
|
631
|
|
|
|
|
|
|
} |
632
|
|
|
|
|
|
|
|
633
|
|
|
|
|
|
|
# Pack the socket name (sockaddr) information. |
634
|
3
|
|
|
|
|
21
|
$this->{_sock_name} = $this->_name_pack($sock_nh); |
635
|
|
|
|
|
|
|
|
636
|
|
|
|
|
|
|
# Pack the destination name (sockaddr) information. |
637
|
3
|
|
|
|
|
49
|
$this->{_dest_name} = $this->_name_pack($dest_nh); |
638
|
|
|
|
|
|
|
|
639
|
|
|
|
|
|
|
# For all connection-oriented transports and for each unique source |
640
|
|
|
|
|
|
|
# address for connectionless transports, create a new socket. |
641
|
|
|
|
|
|
|
|
642
|
3
|
50
|
33
|
|
|
39
|
if (!$this->connectionless() || !exists $SOCKETS->{$this->{_sock_name}}) { |
643
|
|
|
|
|
|
|
|
644
|
|
|
|
|
|
|
# Create a new IO::Socket object. |
645
|
|
|
|
|
|
|
|
646
|
3
|
50
|
|
|
|
18
|
if (!defined ($this->{_socket} = $this->_socket_create())) { |
647
|
0
|
|
|
|
|
0
|
$this->_perror('Failed to open %s socket', $this->type()); |
648
|
0
|
0
|
|
|
|
0
|
return wantarray ? (undef, $this->{_error}) : undef |
649
|
|
|
|
|
|
|
} |
650
|
|
|
|
|
|
|
|
651
|
3
|
|
|
|
|
178
|
DEBUG_INFO('opened %s socket [%d]', $this->type(), $this->fileno()); |
652
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
# Bind the socket. |
654
|
|
|
|
|
|
|
|
655
|
3
|
50
|
|
|
|
18
|
if (!defined $this->{_socket}->bind($this->{_sock_name})) { |
656
|
0
|
|
|
|
|
0
|
$this->_perror('Failed to bind %s socket', $this->type()); |
657
|
0
|
0
|
|
|
|
0
|
return wantarray ? (undef, $this->{_error}) : undef |
658
|
|
|
|
|
|
|
} |
659
|
|
|
|
|
|
|
|
660
|
|
|
|
|
|
|
# For connection-oriented transports, we either listen or connect. |
661
|
|
|
|
|
|
|
|
662
|
3
|
50
|
|
|
|
66
|
if (!$this->connectionless()) { |
663
|
|
|
|
|
|
|
|
664
|
0
|
0
|
|
|
|
0
|
if ($listen) { |
665
|
0
|
0
|
|
|
|
0
|
if (!defined $this->{_socket}->listen($listen)) { |
666
|
0
|
|
|
|
|
0
|
$this->_perror('Failed to listen on %s socket', $this->type()); |
667
|
0
|
0
|
|
|
|
0
|
return wantarray ? (undef, $this->{_error}) : undef |
668
|
|
|
|
|
|
|
} |
669
|
|
|
|
|
|
|
} else { |
670
|
0
|
0
|
|
|
|
0
|
if (!defined $this->{_socket}->connect($this->{_dest_name})) { |
671
|
0
|
|
|
|
|
0
|
$this->_perror( |
672
|
|
|
|
|
|
|
q{Failed to connect to remote host '%s'}, |
673
|
|
|
|
|
|
|
$this->dest_hostname() |
674
|
|
|
|
|
|
|
); |
675
|
0
|
0
|
|
|
|
0
|
return wantarray ? (undef, $this->{_error}) : undef |
676
|
|
|
|
|
|
|
} |
677
|
|
|
|
|
|
|
} |
678
|
|
|
|
|
|
|
} |
679
|
|
|
|
|
|
|
|
680
|
|
|
|
|
|
|
# Flag the socket as non-blocking outside of socket creation or |
681
|
|
|
|
|
|
|
# the object instantiation fails on some systems (e.g. MSWin32). |
682
|
|
|
|
|
|
|
|
683
|
3
|
|
|
|
|
12
|
$this->{_socket}->blocking(FALSE); |
684
|
|
|
|
|
|
|
|
685
|
|
|
|
|
|
|
# Add the socket to the global socket list with a reference |
686
|
|
|
|
|
|
|
# count to track when to close the socket and the maxMsgSize |
687
|
|
|
|
|
|
|
# associated with this new object for connectionless transports. |
688
|
|
|
|
|
|
|
|
689
|
3
|
50
|
|
|
|
55
|
if ($this->connectionless()) { |
690
|
3
|
|
|
|
|
18
|
$SOCKETS->{$this->{_sock_name}} = [ |
691
|
|
|
|
|
|
|
$this->{_socket}, # Shared Socket object |
692
|
|
|
|
|
|
|
1, # Reference count |
693
|
|
|
|
|
|
|
$this->{_max_msg_size}, # Shared maximum message size |
694
|
|
|
|
|
|
|
]; |
695
|
|
|
|
|
|
|
} |
696
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
} else { |
698
|
|
|
|
|
|
|
|
699
|
|
|
|
|
|
|
# Bump up the reference count. |
700
|
0
|
|
|
|
|
0
|
$SOCKETS->{$this->{_sock_name}}->[_SHARED_REFC]++; |
701
|
|
|
|
|
|
|
|
702
|
|
|
|
|
|
|
# Assign the socket to the object. |
703
|
0
|
|
|
|
|
0
|
$this->{_socket} = $SOCKETS->{$this->{_sock_name}}->[_SHARED_SOCKET]; |
704
|
|
|
|
|
|
|
|
705
|
|
|
|
|
|
|
# Adjust the shared maxMsgSize if necessary. |
706
|
0
|
|
|
|
|
0
|
$this->_shared_max_size($this->{_max_msg_size}); |
707
|
|
|
|
|
|
|
|
708
|
0
|
|
|
|
|
0
|
DEBUG_INFO('reused %s socket [%d]', $this->type(), $this->fileno()); |
709
|
|
|
|
|
|
|
|
710
|
|
|
|
|
|
|
} |
711
|
|
|
|
|
|
|
|
712
|
|
|
|
|
|
|
# Return the object and empty error message (in list context) |
713
|
3
|
100
|
|
|
|
33
|
return wantarray ? ($this, q{}) : $this; |
714
|
|
|
|
|
|
|
} |
715
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
sub _service_resolve |
717
|
|
|
|
|
|
|
{ |
718
|
2
|
|
|
2
|
|
4
|
my ($this, $serv, $nh) = @_; |
719
|
|
|
|
|
|
|
|
720
|
2
|
|
|
|
|
5
|
$nh->{port} = undef; |
721
|
|
|
|
|
|
|
|
722
|
2
|
50
|
|
|
|
14
|
if ($serv !~ /^\d+$/) { |
|
|
50
|
|
|
|
|
|
723
|
0
|
0
|
|
|
|
0
|
my $port = ($serv =~ s/\((\d+)\)$//) ? ($1 > 65535) ? undef : $1 : undef; |
|
|
0
|
|
|
|
|
|
724
|
0
|
|
0
|
|
|
0
|
$nh->{port} = getservbyname($serv, $this->_protocol_name()) || $port; |
725
|
0
|
0
|
|
|
|
0
|
if (!defined $nh->{port}) { |
726
|
0
|
|
|
|
|
0
|
return $this->_error( |
727
|
|
|
|
|
|
|
'Unable to resolve the %s service name "%s"', $this->type(), $_[1] |
728
|
|
|
|
|
|
|
); |
729
|
|
|
|
|
|
|
} |
730
|
|
|
|
|
|
|
} elsif ($serv > 65535) { |
731
|
0
|
|
|
|
|
0
|
return $this->_error( |
732
|
|
|
|
|
|
|
'The %s port number %s is out of range (0..65535)', |
733
|
|
|
|
|
|
|
$this->type(), $serv |
734
|
|
|
|
|
|
|
); |
735
|
|
|
|
|
|
|
} else { |
736
|
2
|
|
|
|
|
6
|
$nh->{port} = $serv; |
737
|
|
|
|
|
|
|
} |
738
|
|
|
|
|
|
|
|
739
|
2
|
|
|
|
|
4
|
return $nh->{port}; |
740
|
|
|
|
|
|
|
} |
741
|
|
|
|
|
|
|
|
742
|
|
|
|
|
|
|
sub _protocol |
743
|
|
|
|
|
|
|
{ |
744
|
3
|
|
|
3
|
|
13
|
return (getprotobyname $_[0]->_protocol_name())[2]; |
745
|
|
|
|
|
|
|
} |
746
|
|
|
|
|
|
|
|
747
|
|
|
|
|
|
|
sub _shared_max_size |
748
|
|
|
|
|
|
|
{ |
749
|
1
|
|
|
1
|
|
4
|
my ($this, $size) = @_; |
750
|
|
|
|
|
|
|
|
751
|
|
|
|
|
|
|
# Connection-oriented transports do not share sockets. |
752
|
1
|
50
|
|
|
|
6
|
if (!$this->connectionless()) { |
753
|
0
|
|
|
|
|
0
|
return $this->{_max_msg_size}; |
754
|
|
|
|
|
|
|
} |
755
|
|
|
|
|
|
|
|
756
|
1
|
50
|
|
|
|
13
|
if (@_ == 2) { |
757
|
|
|
|
|
|
|
|
758
|
|
|
|
|
|
|
# Handle calls during object creation. |
759
|
0
|
0
|
|
|
|
0
|
if (!defined $this->{_sock_name}) { |
760
|
0
|
|
|
|
|
0
|
return $this->{_max_msg_size}; |
761
|
|
|
|
|
|
|
} |
762
|
|
|
|
|
|
|
|
763
|
|
|
|
|
|
|
# Update the shared maxMsgSize if the passed |
764
|
|
|
|
|
|
|
# value is greater than the current size. |
765
|
|
|
|
|
|
|
|
766
|
0
|
0
|
|
|
|
0
|
if ($size > $SOCKETS->{$this->{_sock_name}}->[_SHARED_MAXSIZE]) { |
767
|
0
|
|
|
|
|
0
|
$SOCKETS->{$this->{_sock_name}}->[_SHARED_MAXSIZE] = $size; |
768
|
|
|
|
|
|
|
} |
769
|
|
|
|
|
|
|
|
770
|
|
|
|
|
|
|
} |
771
|
|
|
|
|
|
|
|
772
|
1
|
|
|
|
|
7
|
return $SOCKETS->{$this->{_sock_name}}->[_SHARED_MAXSIZE]; |
773
|
|
|
|
|
|
|
} |
774
|
|
|
|
|
|
|
|
775
|
|
|
|
|
|
|
sub _msg_size_default |
776
|
|
|
|
|
|
|
{ |
777
|
0
|
|
|
0
|
|
0
|
return MSG_SIZE_DEFAULT; |
778
|
|
|
|
|
|
|
} |
779
|
|
|
|
|
|
|
|
780
|
|
|
|
|
|
|
sub _error |
781
|
|
|
|
|
|
|
{ |
782
|
0
|
|
|
0
|
|
0
|
my $this = shift; |
783
|
|
|
|
|
|
|
|
784
|
0
|
0
|
|
|
|
0
|
if (!defined $this->{_error}) { |
785
|
0
|
0
|
|
|
|
0
|
$this->{_error} = (@_ > 1) ? sprintf(shift(@_), @_) : $_[0]; |
786
|
0
|
0
|
|
|
|
0
|
if ($this->debug()) { |
787
|
0
|
|
|
|
|
0
|
printf "error: [%d] %s(): %s\n", |
788
|
|
|
|
|
|
|
(caller 0)[2], (caller 1)[3], $this->{_error}; |
789
|
|
|
|
|
|
|
} |
790
|
|
|
|
|
|
|
} |
791
|
|
|
|
|
|
|
|
792
|
0
|
|
|
|
|
0
|
return; |
793
|
|
|
|
|
|
|
} |
794
|
|
|
|
|
|
|
|
795
|
|
|
|
|
|
|
sub strerror |
796
|
|
|
|
|
|
|
{ |
797
|
0
|
0
|
|
0
|
0
|
0
|
if ($! =~ /^Unknown error/) { |
798
|
0
|
0
|
|
|
|
0
|
return sprintf '%s', $^E if ($^E); |
799
|
0
|
|
|
|
|
0
|
require Errno; |
800
|
0
|
|
|
1
|
|
0
|
for (keys (%!)) { |
|
1
|
|
|
|
|
941
|
|
|
1
|
|
|
|
|
1610
|
|
|
1
|
|
|
|
|
704
|
|
801
|
0
|
0
|
|
|
|
0
|
if ($!{$_}) { |
802
|
0
|
|
|
|
|
0
|
return sprintf 'Error %s', $_; |
803
|
|
|
|
|
|
|
} |
804
|
|
|
|
|
|
|
} |
805
|
0
|
|
|
|
|
0
|
return sprintf '%s (%d)', $!, $!; |
806
|
|
|
|
|
|
|
} |
807
|
|
|
|
|
|
|
|
808
|
0
|
0
|
|
|
|
0
|
return $! ? sprintf('%s', $!) : 'No error'; |
809
|
|
|
|
|
|
|
} |
810
|
|
|
|
|
|
|
|
811
|
|
|
|
|
|
|
sub _perror |
812
|
|
|
|
|
|
|
{ |
813
|
0
|
|
|
0
|
|
0
|
my $this = shift; |
814
|
|
|
|
|
|
|
|
815
|
0
|
0
|
|
|
|
0
|
if (!defined $this->{_error}) { |
816
|
0
|
|
0
|
|
|
0
|
$this->{_error} = ((@_ > 1) ? sprintf(shift(@_), @_) : $_[0]) || q{}; |
817
|
0
|
0
|
|
|
|
0
|
$this->{_error} .= (($this->{_error}) ? ': ' : q{}) . strerror(); |
818
|
0
|
0
|
|
|
|
0
|
if ($this->debug()) { |
819
|
0
|
|
|
|
|
0
|
printf "error: [%d] %s(): %s\n", |
820
|
|
|
|
|
|
|
(caller 0)[2], (caller 1)[3], $this->{_error}; |
821
|
|
|
|
|
|
|
} |
822
|
|
|
|
|
|
|
} |
823
|
|
|
|
|
|
|
|
824
|
0
|
|
|
|
|
0
|
return; |
825
|
|
|
|
|
|
|
} |
826
|
|
|
|
|
|
|
|
827
|
|
|
|
|
|
|
sub _error_clear |
828
|
|
|
|
|
|
|
{ |
829
|
2
|
|
|
2
|
|
8
|
$! = 0; |
830
|
2
|
|
|
|
|
8
|
return $_[0]->{_error} = undef; |
831
|
|
|
|
|
|
|
} |
832
|
|
|
|
|
|
|
|
833
|
|
|
|
|
|
|
{ |
834
|
|
|
|
|
|
|
my %modules; |
835
|
|
|
|
|
|
|
|
836
|
|
|
|
|
|
|
sub load_module |
837
|
|
|
|
|
|
|
{ |
838
|
0
|
|
|
0
|
0
|
0
|
my ($module) = @_; |
839
|
|
|
|
|
|
|
|
840
|
|
|
|
|
|
|
# We attempt to load the required module under the protection of an |
841
|
|
|
|
|
|
|
# eval statement. If there is a failure, typically it is due to a |
842
|
|
|
|
|
|
|
# missing module required by the requested module and we attempt to |
843
|
|
|
|
|
|
|
# simplify the error message by just listing that module. We also |
844
|
|
|
|
|
|
|
# need to track failures since require() only produces an error on |
845
|
|
|
|
|
|
|
# the first attempt to load the module. |
846
|
|
|
|
|
|
|
|
847
|
|
|
|
|
|
|
# NOTE: Contrary to our typical convention, a return value of "undef" |
848
|
|
|
|
|
|
|
# actually means success and a defined value means error. |
849
|
|
|
|
|
|
|
|
850
|
0
|
0
|
|
|
|
0
|
return $modules{$module} if exists $modules{$module}; |
851
|
|
|
|
|
|
|
|
852
|
0
|
0
|
|
|
|
0
|
if (!eval "require $module") { |
853
|
0
|
0
|
|
|
|
0
|
if ($@ =~ /locate (\S+\.pm)/) { |
|
|
0
|
|
|
|
|
|
854
|
0
|
|
|
|
|
0
|
$modules{$module} = err_msg('(Required module %s not found)', $1); |
855
|
|
|
|
|
|
|
} elsif ($@ =~ /(.*)\n/) { |
856
|
0
|
|
|
|
|
0
|
$modules{$module} = err_msg('(%s)', $1); |
857
|
|
|
|
|
|
|
} else { |
858
|
0
|
|
|
|
|
0
|
$modules{$module} = err_msg('(%s)', $@); |
859
|
|
|
|
|
|
|
} |
860
|
|
|
|
|
|
|
} else { |
861
|
0
|
|
|
|
|
0
|
$modules{$module} = undef; |
862
|
|
|
|
|
|
|
} |
863
|
|
|
|
|
|
|
|
864
|
0
|
|
|
|
|
0
|
return $modules{$module}; |
865
|
|
|
|
|
|
|
} |
866
|
|
|
|
|
|
|
} |
867
|
|
|
|
|
|
|
|
868
|
|
|
|
|
|
|
sub err_msg |
869
|
|
|
|
|
|
|
{ |
870
|
0
|
0
|
|
0
|
0
|
0
|
my $msg = (@_ > 1) ? sprintf(shift(@_), @_) : $_[0]; |
871
|
|
|
|
|
|
|
|
872
|
0
|
0
|
|
|
|
0
|
if ($DEBUG) { |
873
|
0
|
|
|
|
|
0
|
printf "error: [%d] %s(): %s\n", (caller 0)[2], (caller 1)[3], $msg; |
874
|
|
|
|
|
|
|
} |
875
|
|
|
|
|
|
|
|
876
|
0
|
|
|
|
|
0
|
return $msg; |
877
|
|
|
|
|
|
|
} |
878
|
|
|
|
|
|
|
|
879
|
|
|
|
|
|
|
sub DEBUG_INFO |
880
|
|
|
|
|
|
|
{ |
881
|
3
|
50
|
|
3
|
0
|
33
|
return $DEBUG if (!$DEBUG); |
882
|
|
|
|
|
|
|
|
883
|
0
|
0
|
|
|
|
0
|
return printf |
884
|
|
|
|
|
|
|
sprintf('debug: [%d] %s(): ', (caller 0)[2], (caller 1)[3]) . |
885
|
|
|
|
|
|
|
((@_ > 1) ? shift(@_) : '%s') . |
886
|
|
|
|
|
|
|
"\n", |
887
|
|
|
|
|
|
|
@_; |
888
|
|
|
|
|
|
|
} |
889
|
|
|
|
|
|
|
|
890
|
|
|
|
|
|
|
# ============================================================================ |
891
|
|
|
|
|
|
|
1; # [end Net::SNMP::Transport] |