| 1 | # -*- mode: perl -*- | ||
| 2 | # ============================================================================ | ||
| 3 | |||
| 4 | package Net::SNMP::Transport::IPv4::TCP; | ||
| 5 | |||
| 6 | # $Id: TCP.pm,v 3.0 2009/09/09 15:05:33 dtown Rel $ | ||
| 7 | |||
| 8 | # Object that handles the TCP/IPv4 Transport Domain for the SNMP Engine. | ||
| 9 | |||
| 10 | # Copyright (c) 2004-2009 David M. Town <dtown@cpan.org> | ||
| 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 | use strict; | ||
| 19 | |||
| 20 | use Net::SNMP::Transport qw( | ||
| 21 | MSG_SIZE_MAXIMUM DOMAIN_TCPIPV4 TRUE FALSE DEBUG_INFO | ||
| 22 | ); | ||
| 23 | |||
| 24 | use Net::SNMP::Message qw( SEQUENCE ); | ||
| 25 | |||
| 26 | use IO::Socket qw( SOCK_STREAM ); | ||
| 27 | |||
| 28 | ## Version of the Net::SNMP::Transport::IPv4::TCP module | ||
| 29 | |||
| 30 | our $VERSION = v3.0.0; | ||
| 31 | |||
| 32 | ## Handle importing/exporting of symbols | ||
| 33 | |||
| 34 | use base qw( Net::SNMP::Transport::IPv4 Net::SNMP::Transport ); | ||
| 35 | |||
| 36 | sub import | ||
| 37 | { | ||
| 38 | return Net::SNMP::Transport->export_to_level(1, @_); | ||
| 39 | } | ||
| 40 | |||
| 41 | ## RFC 3411 - snmpEngineMaxMessageSize::=INTEGER (484..2147483647) | ||
| 42 | |||
| 43 | sub MSG_SIZE_DEFAULT_TCP4 { 1460 } # Ethernet(1500) - IPv4(20) - TCP(20) | ||
| 44 | |||
| 45 | # [public methods] ----------------------------------------------------------- | ||
| 46 | |||
| 47 | sub new | ||
| 48 | { | ||
| 49 | my ($this, $error) = shift->SUPER::_new(@_); | ||
| 50 | |||
| 51 | if (defined $this) { | ||
| 52 | if (!defined $this->_reasm_init()) { | ||
| 53 | return wantarray ? (undef, $this->error()) : undef; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | return wantarray ? ($this, $error) : $this; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub accept | ||
| 61 | { | ||
| 62 | my ($this) = @_; | ||
| 63 | |||
| 64 | $this->_error_clear(); | ||
| 65 | |||
| 66 | my $socket = $this->{_socket}->accept(); | ||
| 67 | |||
| 68 | if (!defined $socket) { | ||
| 69 | return $this->_perror('Failed to accept the connection'); | ||
| 70 | } | ||
| 71 | |||
| 72 | DEBUG_INFO('opened %s socket [%d]', $this->type(), $socket->fileno()); | ||
| 73 | |||
| 74 | # Create a new object by copying the current object. | ||
| 75 | |||
| 76 | my $new = bless { %{$this} }, ref $this; | ||
| 77 | |||
| 78 | # Now update the appropriate fields. | ||
| 79 | |||
| 80 | $new->{_socket} = $socket; | ||
| 81 | $new->{_dest_name} = $socket->peername(); | ||
| 82 | $new->{_dest_hostname} = $new->sock_address(); | ||
| 83 | |||
| 84 | if (!defined $new->_reasm_init()) { | ||
| 85 | return $this->_error($new->error()); | ||
| 86 | } | ||
| 87 | |||
| 88 | # Return the new object. | ||
| 89 | return $new; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub send | ||
| 93 | { | ||
| 94 | my $this = shift; | ||
| 95 | |||
| 96 | $this->_error_clear(); | ||
| 97 | |||
| 98 | if (length($_[0]) > $this->{_max_msg_size}) { | ||
| 99 | return $this->_error( | ||
| 100 | 'The message size %d exceeds the maxMsgSize %d', | ||
| 101 | length($_[0]), $this->{_max_msg_size} | ||
| 102 | ); | ||
| 103 | } | ||
| 104 | |||
| 105 | if (!defined $this->{_socket}->connected()) { | ||
| 106 | return $this->_error( | ||
| 107 | q{Not connected to the remote host '%s'}, $this->dest_hostname() | ||
| 108 | ); | ||
| 109 | } | ||
| 110 | |||
| 111 | my $bytes = $this->{_socket}->send($_[0], 0); | ||
| 112 | |||
| 113 | return defined($bytes) ? $bytes : $this->_perror('Send failure'); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub recv | ||
| 117 | { | ||
| 118 | my $this = shift; | ||
| 119 | |||
| 120 | $this->_error_clear(); | ||
| 121 | |||
| 122 | if (!defined $this->{_socket}->connected()) { | ||
| 123 | $this->_reasm_reset(); | ||
| 124 | return $this->_error( | ||
| 125 | q{Not connected to the remote host '%s'}, $this->dest_hostname() | ||
| 126 | ); | ||
| 127 | } | ||
| 128 | |||
| 129 | # RCF 3430 Section 2.1 - "It is possible that the underlying TCP | ||
| 130 | # implementation delivers byte sequences that do not align with | ||
| 131 | # SNMP message boundaries. A receiving SNMP engine MUST therefore | ||
| 132 | # use the length field in the BER-encoded SNMP message to separate | ||
| 133 | # multiple requests sent over a single TCP connection (framing). | ||
| 134 | # An SNMP engine which looses framing (for example due to ASN.1 | ||
| 135 | # parse errors) SHOULD close the TCP connection." | ||
| 136 | |||
| 137 | # If the reassembly bufer is empty then there is no partial message | ||
| 138 | # waiting for completion. We must then process the message length | ||
| 139 | # to properly determine how much data to receive. | ||
| 140 | |||
| 141 | my $name; | ||
| 142 | |||
| 143 | if ($this->{_reasm_buffer} eq q{}) { | ||
| 144 | |||
| 145 | if (!defined $this->{_reasm_object}) { | ||
| 146 | return $this->_error('The reassembly object is not defined'); | ||
| 147 | } | ||
| 148 | |||
| 149 | # Read enough data to parse the ASN.1 type and length. | ||
| 150 | |||
| 151 | $name = $this->{_socket}->recv($this->{_reasm_buffer}, 6, 0); | ||
| 152 | |||
| 153 | if ((!defined $name) || ($!)) { | ||
| 154 | $this->_reasm_reset(); | ||
| 155 | return $this->_perror('Receive failure'); | ||
| 156 | } elsif (!length $this->{_reasm_buffer}) { | ||
| 157 | $this->_reasm_reset(); | ||
| 158 | return $this->_error( | ||
| 159 | q{The connection was closed by the remote host '%s'}, | ||
| 160 | $this->dest_hostname() | ||
| 161 | ); | ||
| 162 | } | ||
| 163 | |||
| 164 | $this->{_reasm_object}->append($this->{_reasm_buffer}); | ||
| 165 | |||
| 166 | $this->{_reasm_length} = $this->{_reasm_object}->process(SEQUENCE) || 0; | ||
| 167 | |||
| 168 | if ((!$this->{_reasm_length}) || | ||
| 169 | ($this->{_reasm_length} > MSG_SIZE_MAXIMUM)) | ||
| 170 | { | ||
| 171 | $this->_reasm_reset(); | ||
| 172 | return $this->_error( | ||
| 173 | q{Message framing was lost with the remote host '%s'}, | ||
| 174 | $this->dest_hostname() | ||
| 175 | ); | ||
| 176 | } | ||
| 177 | |||
| 178 | # Add in the bytes parsed to define the expected message length. | ||
| 179 | $this->{_reasm_length} += $this->{_reasm_object}->index(); | ||
| 180 | |||
| 181 | } | ||
| 182 | |||
| 183 | # Setup a temporary buffer for the message and set the length | ||
| 184 | # based upon the contents of the reassembly buffer. | ||
| 185 | |||
| 186 | my $buf = q{}; | ||
| 187 | my $buf_len = length $this->{_reasm_buffer}; | ||
| 188 | |||
| 189 | # Read the rest of the message. | ||
| 190 | |||
| 191 | $name = $this->{_socket}->recv($buf, ($this->{_reasm_length} - $buf_len), 0); | ||
| 192 | |||
| 193 | if ((!defined $name) || ($!)) { | ||
| 194 | $this->_reasm_reset(); | ||
| 195 | return $this->_perror('Receive failure'); | ||
| 196 | } elsif (!length $buf) { | ||
| 197 | $this->_reasm_reset(); | ||
| 198 | return $this->_error( | ||
| 199 | q{The connection was closed by the remote host '%s'}, | ||
| 200 | $this->dest_hostname() | ||
| 201 | ); | ||
| 202 | } | ||
| 203 | |||
| 204 | # Now see if we have the complete message. If it is not complete, | ||
| 205 | # success is returned with an empty buffer. The application must | ||
| 206 | # continue to call recv() until the message is reassembled. | ||
| 207 | |||
| 208 | $buf_len += length $buf; | ||
| 209 | $this->{_reasm_buffer} .= $buf; | ||
| 210 | |||
| 211 | if ($buf_len < $this->{_reasm_length}) { | ||
| 212 | DEBUG_INFO( | ||
| 213 | 'message is incomplete (expect %u bytes, have %u bytes)', | ||
| 214 | $this->{_reasm_length}, $buf_len | ||
| 215 | ); | ||
| 216 | $_[0] = q{}; | ||
| 217 | return $name || $this->{_socket}->connected(); | ||
| 218 | } | ||
| 219 | |||
| 220 | # Validate the maxMsgSize. | ||
| 221 | if ($buf_len > $this->{_max_msg_size}) { | ||
| 222 | $this->_reasm_reset(); | ||
| 223 | return $this->_error( | ||
| 224 | 'Incoming message size %d exceeded the maxMsgSize %d', | ||
| 225 | $buf_len, $this->{_max_msg_size} | ||
| 226 | ); | ||
| 227 | } | ||
| 228 | |||
| 229 | # The message is complete, copy the buffer to the caller. | ||
| 230 | $_[0] = $this->{_reasm_buffer}; | ||
| 231 | |||
| 232 | # Clear the reassembly buffer and length. | ||
| 233 | $this->_reasm_reset(); | ||
| 234 | |||
| 235 | return $name || $this->{_socket}->connected(); | ||
| 236 | } | ||
| 237 | |||
| 238 | sub connectionless | ||
| 239 | { | ||
| 240 | return FALSE; | ||
| 241 | } | ||
| 242 | |||
| 243 | sub domain | ||
| 244 | { | ||
| 245 | return DOMAIN_TCPIPV4; # transportDomainTcpIpv4 | ||
| 246 | } | ||
| 247 | |||
| 248 | sub type | ||
| 249 | { | ||
| 250 | return 'TCP/IPv4'; # tcpIpv4(5) | ||
| 251 | } | ||
| 252 | |||
| 253 | sub agent_addr | ||
| 254 | { | ||
| 255 | return shift->sock_address(); | ||
| 256 | } | ||
| 257 | |||
| 258 | # [private methods] ---------------------------------------------------------- | ||
| 259 | |||
| 260 | sub _protocol_name | ||
| 261 | { | ||
| 262 | return 'tcp'; | ||
| 263 | } | ||
| 264 | |||
| 265 | sub _protocol_type | ||
| 266 | { | ||
| 267 | return SOCK_STREAM; | ||
| 268 | } | ||
| 269 | |||
| 270 | sub _msg_size_default | ||
| 271 | { | ||
| 272 | return MSG_SIZE_DEFAULT_TCP4; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub _reasm_init | ||
| 276 | { | ||
| 277 | my ($this) = @_; | ||
| 278 | |||
| 279 | my $error; | ||
| 280 | |||
| 281 | ($this->{_reasm_object}, $error) = Net::SNMP::Message->new(); | ||
| 282 | |||
| 283 | if (!defined $this->{_reasm_object}) { | ||
| 284 | return $this->_error( | ||
| 285 | 'Failed to create the reassembly object: %s', $error | ||
| 286 | ); | ||
| 287 | } | ||
| 288 | |||
| 289 | $this->_reasm_reset(); | ||
| 290 | |||
| 291 | return TRUE; | ||
| 292 | } | ||
| 293 | |||
| 294 | sub _reasm_reset | ||
| 295 | { | ||
| 296 | my ($this) = @_; | ||
| 297 | |||
| 298 | if (defined $this->{_reasm_object}) { | ||
| 299 | $this->{_reasm_object}->error(undef); | ||
| 300 | $this->{_reasm_object}->clear(); | ||
| 301 | } | ||
| 302 | |||
| 303 | $this->{_reasm_buffer} = q{}; | ||
| 304 | $this->{_reasm_length} = 0; | ||
| 305 | |||
| 306 | return TRUE; | ||
| 307 | } | ||
| 308 | |||
| 309 | sub _tdomain | ||
| 310 | { | ||
| 311 | return DOMAIN_TCPIPV4; # transportDomainTcpIpv4 | ||
| 312 | } | ||
| 313 | |||
| 314 | # ============================================================================ | ||
| 315 | 1; # [end Net::SNMP::Transport::IPv4::TCP] | ||
| 316 |