| 1 | #!/usr/bin/perl | ||
| 2 | # | ||
| 3 | # Copyright 1997 - 2019 by IXIA Keysight | ||
| 4 | # | ||
| 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 6 | # of this software and associated documentation files (the "Software"), | ||
| 7 | # to deal in the Software without restriction, including without limitation | ||
| 8 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
| 9 | # and/or sell copies of the Software, and to permit persons to whom the | ||
| 10 | # Software is furnished to do so, subject to the following conditions: | ||
| 11 | # | ||
| 12 | # The above copyright notice and this permission notice shall be included in | ||
| 13 | # all copies or substantial portions of the Software. | ||
| 14 | # | ||
| 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| 21 | # THE SOFTWARE. | ||
| 22 | |||
| 23 | use strict; | ||
| 24 | use Fcntl; | ||
| 25 | use Socket; | ||
| 26 | use File::stat; | ||
| 27 | use File::Basename; | ||
| 28 | use IO::Socket; | ||
| 29 | use validate; | ||
| 30 | |||
| 31 | package IxNetworkLegacy; | ||
| 32 | |||
| 33 | =head1 new | ||
| 34 | =cut | ||
| 35 | sub new | ||
| 36 | { | ||
| 37 | my $class = shift; | ||
| 38 | my $self = { | ||
| 39 | _root => '::ixNet::OBJ-/', | ||
| 40 | _null => '::ixNet::OBJ-null', | ||
| 41 | _port => undef, | ||
| 42 | _address => undef, | ||
| 43 | _sessionId => undef, | ||
| 44 | _serverusername => undef, | ||
| 45 | _socket => undef, | ||
| 46 | _proxySocket => undef, | ||
| 47 | _connectTokens => undef, | ||
| 48 | _evalError => '1', | ||
| 49 | _evalSuccess => '0', | ||
| 50 | _evalResult => '0', | ||
| 51 | _addContentSeparator => 0, | ||
| 52 | _firstItem => undef, | ||
| 53 | _sendContent => '', | ||
| 54 | _buffer => undef, | ||
| 55 | _sendBuffer => '', | ||
| 56 | _decoratedResult => '', | ||
| 57 | _filename => '', | ||
| 58 | _debug => undef, | ||
| 59 | _async => undef, | ||
| 60 | _timeout => undef, | ||
| 61 | _transportType => 'TclSocket', | ||
| 62 | _OK => '::ixNet::OK', | ||
| 63 | _version => '9.00.1915.16' | ||
| 64 | }; | ||
| 65 | bless $self, $class; | ||
| 66 | return $self; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub setDebug | ||
| 70 | { | ||
| 71 | my($self, $debug) = @_; | ||
| 72 | if ($_[1]) { | ||
| 73 | $self->{_debug} = 1; | ||
| 74 | } else { | ||
| 75 | $self->{_debug} = undef; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | sub getRoot | ||
| 80 | { | ||
| 81 | my($self) = @_; | ||
| 82 | return $self->{_root}; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub getNull | ||
| 86 | { | ||
| 87 | my($self) = @_; | ||
| 88 | return $self->{_null}; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub setAsync | ||
| 92 | { | ||
| 93 | my($self) = @_; | ||
| 94 | $self->{_async} = '1'; | ||
| 95 | return $self; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub setTimeout | ||
| 99 | { | ||
| 100 | my($self, $timeout) = @_; | ||
| 101 | if (defined $timeout) | ||
| 102 | { | ||
| 103 | $self->{_timeout} = $timeout; | ||
| 104 | } | ||
| 105 | else | ||
| 106 | { | ||
| 107 | die "A timeout value must be provided\n"; | ||
| 108 | } | ||
| 109 | return $self; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub connect { | ||
| 113 | my($self) = @_; | ||
| 114 | my $address = $_[1]; | ||
| 115 | my $sessionId = undef; | ||
| 116 | my $serverusername = undef; | ||
| 117 | |||
| 118 | my @args = @_[1..scalar(@_) - 1]; | ||
| 119 | |||
| 120 | if (defined $self->{_socket}) { | ||
| 121 | my $ret = eval { | ||
| 122 | $self->_SendRecv('ixNet', 'help'); | ||
| 123 | 1; | ||
| 124 | }; | ||
| 125 | if (!$ret and $@) { | ||
| 126 | $self->_Close(); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | my %nameValuePairs = ( | ||
| 131 | '-sessionId' => $sessionId, | ||
| 132 | '-serverUsername' => $serverusername, | ||
| 133 | '-port' => '8009', | ||
| 134 | '-closeServerOnDisconnect' => 'true', | ||
| 135 | '-applicationVersion' => undef, | ||
| 136 | '-persistentApplicationVersion' => undef, | ||
| 137 | '-forceVersion' => undef, | ||
| 138 | '-clientType' => 'perl', | ||
| 139 | '-clientusername' => getlogin() | ||
| 140 | ); | ||
| 141 | my $name = undef; | ||
| 142 | foreach my $arg (@args) { | ||
| 143 | if (index($arg, '-', 0) == 0 and not defined $name) { | ||
| 144 | $name = $arg; | ||
| 145 | } elsif (defined $name) { | ||
| 146 | $nameValuePairs{$name} = $arg; | ||
| 147 | $name = undef; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | my $options = ''; | ||
| 152 | foreach my $name (keys(%nameValuePairs)) { | ||
| 153 | if ($name ne '-port' and defined $nameValuePairs{$name}) { | ||
| 154 | $options .= $name.' '.$nameValuePairs{$name}.' '; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | my $port = $nameValuePairs{'-port'}; | ||
| 158 | $serverusername = $nameValuePairs{'-serverusername'}; | ||
| 159 | $sessionId = $nameValuePairs{'-sessionId'}; | ||
| 160 | if (not defined $self->{_socket}) { | ||
| 161 | my $ret = eval { | ||
| 162 | $self->_InitialConnect($address, $port, $options); | ||
| 163 | $self->{_port} = $port; | ||
| 164 | $self->{_address} = $address; | ||
| 165 | $self->{_serverusername} = $serverusername; | ||
| 166 | $self->{_sessionId} = $self->getSessionId(); | ||
| 167 | 1; | ||
| 168 | }; | ||
| 169 | if(!$ret and $@) { | ||
| 170 | die "Unable to connect to $address:$port. Error: $@\n"; | ||
| 171 | } else { | ||
| 172 | my $conRes = $self->_SendRecv('ixNet', 'connect', $address, split(/ /, $options)); | ||
| 173 | $self->_CheckClientVersion(); | ||
| 174 | return $conRes; | ||
| 175 | } | ||
| 176 | } else { | ||
| 177 | if ($address ne $self->{_address} or $port ne $self->{_port}) { | ||
| 178 | return "Cannot connect to ".$address.':'.$port." as a connection is already established to ".$self->{_address}.':'.$self->{_port}.". Please execute disconnect before trying this command again.\n"; | ||
| 179 | } elsif ($sessionId and $sessionId != $self->{_sessionId}) { | ||
| 180 | return "Cannot connect to session ".$sessionId." as a connection is already established to session ".$self->{_sessionId}.". Please execute disconnect before trying this command again.\n"; | ||
| 181 | } elsif ($serverusername and $serverusername ne $self->{_serverUsername}) { | ||
| 182 | return "Cannot connect to a session associated with ".$serverusername." as a connection is already established to a session associated with ".$self->{_serverUsername}.". Please execute disconnect before trying this command again.\n"; | ||
| 183 | } else { | ||
| 184 | return $self->{_OK}; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | sub disconnect { | ||
| 190 | my($self) = @_; | ||
| 191 | |||
| 192 | if (not defined $self->{_socket}) { | ||
| 193 | return "not connected"; | ||
| 194 | } | ||
| 195 | |||
| 196 | $self->_Close(); | ||
| 197 | return $self->{_OK}; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub getSessionInfo { | ||
| 201 | my $self = shift; | ||
| 202 | |||
| 203 | $self->_isConnected(1); | ||
| 204 | |||
| 205 | my $backendType = undef; | ||
| 206 | if (defined $self->{_proxySocket}) | ||
| 207 | { | ||
| 208 | $backendType = 'connectionmanager'; | ||
| 209 | } else { | ||
| 210 | $backendType = 'ixnetwork'; | ||
| 211 | } | ||
| 212 | return { | ||
| 213 | 'id' => $self->getSessionId(), | ||
| 214 | 'port' => $self->{_port}, | ||
| 215 | 'applicationType' => 'ixntcl', | ||
| 216 | 'backendType' => $backendType, | ||
| 217 | 'state' => 'active', | ||
| 218 | 'inUse' => 1 | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | sub help { | ||
| 223 | my($self) = @_; | ||
| 224 | my @args = @_[1..scalar(@_) - 1]; | ||
| 225 | |||
| 226 | return $self->_SendRecv('ixNet', 'help', @args); | ||
| 227 | } | ||
| 228 | |||
| 229 | sub setSessionParameter { | ||
| 230 | my($self) = @_; | ||
| 231 | my @args = @_[1..scalar(@_) - 1]; | ||
| 232 | |||
| 233 | if (scalar(@args) % 2 == 0) { | ||
| 234 | return $self->_SendRecv('ixNet', 'setSessionParameter', @args); | ||
| 235 | } else { | ||
| 236 | die "setSessionParameter requires an even number of name/value pairs\n"; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | sub getSessionId { | ||
| 240 | my($self) = @_; | ||
| 241 | $self->_isConnected(1); | ||
| 242 | if(join(' ', $self->_SendRecv('ixNet', 'setSessionParameter')) =~ m/sessionId (\d+)/) { | ||
| 243 | return $1; | ||
| 244 | } | ||
| 245 | return -1;; | ||
| 246 | } | ||
| 247 | |||
| 248 | sub getVersion { | ||
| 249 | my($self) = @_; | ||
| 250 | |||
| 251 | if (not defined $self->{_socket}) { | ||
| 252 | return $self->{_version}; | ||
| 253 | } else { | ||
| 254 | return $self->_SendRecv('ixNet', 'getVersion'); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | sub getParent { | ||
| 259 | my($self) = @_; | ||
| 260 | |||
| 261 | return $self->_SendRecv('ixNet', 'getParent', $_[1]); | ||
| 262 | } | ||
| 263 | |||
| 264 | sub exists { | ||
| 265 | my($self) = @_; | ||
| 266 | |||
| 267 | return $self->_SendRecv('ixNet', 'exists', $_[1]); | ||
| 268 | } | ||
| 269 | |||
| 270 | sub commit { | ||
| 271 | my($self) = @_; | ||
| 272 | |||
| 273 | return $self->_SendRecv('ixNet', 'commit'); | ||
| 274 | } | ||
| 275 | |||
| 276 | sub rollback { | ||
| 277 | my($self) = @_; | ||
| 278 | |||
| 279 | return $self->_SendRecv('ixNet', 'rollback'); | ||
| 280 | } | ||
| 281 | |||
| 282 | sub execute { | ||
| 283 | my($self) = @_; | ||
| 284 | my @args = @_[1..scalar(@_) - 1]; | ||
| 285 | |||
| 286 | return $self->_SendRecv('ixNet', 'exec', @args); | ||
| 287 | } | ||
| 288 | |||
| 289 | sub add { | ||
| 290 | my($self) = @_; | ||
| 291 | my @args = @_[1..scalar(@_) - 1]; | ||
| 292 | |||
| 293 | return $self->_SendRecv('ixNet', 'add', @args); | ||
| 294 | } | ||
| 295 | |||
| 296 | sub remove { | ||
| 297 | my($self) = @_; | ||
| 298 | my @args = @_[1..scalar(@_) - 1]; | ||
| 299 | |||
| 300 | return $self->_SendRecv('ixNet', 'remove', @args); | ||
| 301 | } | ||
| 302 | |||
| 303 | sub setAttribute { | ||
| 304 | my($self) = @_; | ||
| 305 | my @args = @_[1..scalar(@_) - 1]; | ||
| 306 | $self->{_buffer} = 1; | ||
| 307 | |||
| 308 | return$self->_SendRecv('ixNet', 'setAttribute', @args); | ||
| 309 | } | ||
| 310 | |||
| 311 | sub setMultiAttribute { | ||
| 312 | my($self) = @_; | ||
| 313 | my @args = @_[1..scalar(@_) - 1]; | ||
| 314 | $self->{_buffer} = 1; | ||
| 315 | |||
| 316 | return $self->_SendRecv('ixNet', 'setMultiAttribute', @args); | ||
| 317 | } | ||
| 318 | |||
| 319 | sub getAttribute { | ||
| 320 | my($self) = @_; | ||
| 321 | my @args = @_[1..scalar(@_) - 1]; | ||
| 322 | |||
| 323 | return $self->_SendRecv('ixNet', 'getAttribute', @args) | ||
| 324 | } | ||
| 325 | |||
| 326 | sub getList { | ||
| 327 | my($self) = @_; | ||
| 328 | my @args = @_[1..scalar(@_) - 1]; | ||
| 329 | |||
| 330 | return $self->_SendRecv('ixNet', 'getList', @args); | ||
| 331 | } | ||
| 332 | |||
| 333 | sub getFilteredList { | ||
| 334 | my($self) = @_; | ||
| 335 | my @args = @_[1..scalar(@_) - 1]; | ||
| 336 | |||
| 337 | return $self->_SendRecv('ixNet', 'getFilteredList', @args); | ||
| 338 | } | ||
| 339 | |||
| 340 | sub adjustIndexes { | ||
| 341 | my($self) = @_; | ||
| 342 | my @args = @_[1..scalar(@_) - 1]; | ||
| 343 | |||
| 344 | return $self->_SendRecv('ixNet', 'adjustIndexes', @args); | ||
| 345 | } | ||
| 346 | |||
| 347 | sub remapIds { | ||
| 348 | my($self) = @_; | ||
| 349 | my @args = @_[1..scalar(@_) - 1]; | ||
| 350 | |||
| 351 | return $self->_SendRecv('ixNet', 'remapIds', @args); | ||
| 352 | } | ||
| 353 | |||
| 354 | sub getResult { | ||
| 355 | my($self) = @_; | ||
| 356 | my @args = @_[1..scalar(@_) - 1]; | ||
| 357 | |||
| 358 | return $self->_SendRecv('ixNet', 'getResult', @args); | ||
| 359 | } | ||
| 360 | |||
| 361 | sub wait { | ||
| 362 | my($self) = @_; | ||
| 363 | my @args = @_[1..scalar(@_) - 1]; | ||
| 364 | |||
| 365 | return $self->_SendRecv('ixNet', 'wait', @args); | ||
| 366 | } | ||
| 367 | |||
| 368 | sub isDone | ||
| 369 | { | ||
| 370 | my($self) = @_; | ||
| 371 | my @args = @_[1..scalar(@_) - 1]; | ||
| 372 | |||
| 373 | return $self->_SendRecv('ixNet', 'isDone', @args); | ||
| 374 | } | ||
| 375 | |||
| 376 | sub isSuccess { | ||
| 377 | my($self) = @_; | ||
| 378 | my @args = @_[1..scalar(@_) - 1]; | ||
| 379 | |||
| 380 | return $self->_SendRecv('ixNet', 'isSuccess', @args); | ||
| 381 | } | ||
| 382 | |||
| 383 | sub xpath { | ||
| 384 | my($self) = @_; | ||
| 385 | my @args = @_[1..scalar(@_) - 1]; | ||
| 386 | |||
| 387 | return $self->_SendRecv('ixNet', 'xpath', @args); | ||
| 388 | } | ||
| 389 | |||
| 390 | sub writeTo { | ||
| 391 | my($self) = @_; | ||
| 392 | my $filename = $_[1]; | ||
| 393 | my @args = @_[2..scalar(@_) - 1]; | ||
| 394 | |||
| 395 | my $relativeFile = 0; | ||
| 396 | for (@args) { | ||
| 397 | if ($_ eq '-ixNetRelative') { | ||
| 398 | $relativeFile = 1; | ||
| 399 | last; | ||
| 400 | } | ||
| 401 | } | ||
| 402 | if ($relativeFile) { | ||
| 403 | return $self->_SendRecv('ixNet', 'writeTo', $filename, @args); | ||
| 404 | } else { | ||
| 405 | return $self->_CreateFileOnServer($filename); | ||
| 406 | } | ||
| 407 | } | ||
| 408 | |||
| 409 | sub readFrom { | ||
| 410 | my($self) = @_; | ||
| 411 | my $filename = $_[1]; | ||
| 412 | my @args = @_[2..scalar(@_) - 1]; | ||
| 413 | |||
| 414 | my $relativeFile = 0; | ||
| 415 | for (@args) { | ||
| 416 | if ($_ eq '-ixNetRelative') { | ||
| 417 | $relativeFile = 1; | ||
| 418 | last; | ||
| 419 | } | ||
| 420 | } | ||
| 421 | if ($relativeFile) { | ||
| 422 | return $self->_SendRecv('ixNet', 'readFrom', $filename, @args); | ||
| 423 | } else { | ||
| 424 | return $self->_PutFileOnServer($filename); | ||
| 425 | } | ||
| 426 | } | ||
| 427 | |||
| 428 | sub _PutFileOnServer { | ||
| 429 | my($self) = @_; | ||
| 430 | my $filename = $_[1]; | ||
| 431 | my $fid = undef; | ||
| 432 | |||
| 433 | my $truncatedFilename = File::Basename::fileparse($filename); | ||
| 434 | my $fileStat = File::stat::stat($filename); | ||
| 435 | open($fid, '<:raw', $filename) or die "Unable to open file $filename; please check that the file exists\n"; | ||
| 436 | my $fileSize = $fileStat->size(); | ||
| 437 | $self->_Send(sprintf("<001><005><007%d>%s<009%d>", length($truncatedFilename), $truncatedFilename, $fileSize)); | ||
| 438 | my $fileContents = ''; | ||
| 439 | my $readSize = read($fid, $fileContents, $fileSize); | ||
| 440 | $self->_SendBinary($fileContents); | ||
| 441 | close($fid); | ||
| 442 | my $remoteFilename = $self->_Recv(); | ||
| 443 | |||
| 444 | return $self->_SendRecv('ixNet', 'readFrom', $remoteFilename, '-ixNetRelative'); | ||
| 445 | } | ||
| 446 | |||
| 447 | sub _CreateFileOnServer { | ||
| 448 | my($self) = @_; | ||
| 449 | my $filename = $_[1]; | ||
| 450 | |||
| 451 | $self->_Send(sprintf("<001><006><007%d>%s<009>", length($filename), $filename)); | ||
| 452 | my $remoteFilename = $self->_Recv(); | ||
| 453 | |||
| 454 | return $self->_SendRecv('ixNet', 'writeTo', $remoteFilename, '-ixNetRelative', '-overwrite') | ||
| 455 | } | ||
| 456 | |||
| 457 | sub _InitialConnect { | ||
| 458 | my($self) = @_; | ||
| 459 | my $address = $_[1]; | ||
| 460 | my $port = $_[2]; | ||
| 461 | my $options = $_[3]; | ||
| 462 | |||
| 463 | # make an initial socket connection | ||
| 464 | # this will keep trying as it could be connecting to the proxy | ||
| 465 | # which may not have an available application instance at that time | ||
| 466 | my $attempts = 0; | ||
| 467 | while(1) | ||
| 468 | { | ||
| 469 | my $ret = eval { | ||
| 470 | if (validate::isIpv6($address) == 0) { | ||
| 471 | # connect to IPv4 address (or a host name). | ||
| 472 | my $protocol = getprotobyname('tcp'); | ||
| 473 | socket($self->{_socket}, Socket::AF_INET, Socket::SOCK_STREAM, $protocol); | ||
| 474 | my $inetAddress = gethostbyname($address); | ||
| 475 | my $sockAddress = Socket::sockaddr_in($port, $inetAddress); | ||
| 476 | CORE::connect($self->{_socket}, $sockAddress); | ||
| 477 | } else { | ||
| 478 | if (!$checkDependencies::ipv6LoadError) { | ||
| 479 | require LWP::Protocol::INET6; | ||
| 480 | $self->{_socket} = LWP::Protocol::INET6->new( | ||
| 481 | PeerAddr => $address, | ||
| 482 | PeerPort => $port, | ||
| 483 | Proto => 'tcp', | ||
| 484 | ) | ||
| 485 | or die("Unknown Ipv6 address $address\n"); | ||
| 486 | } else { | ||
| 487 | die("Unable to load perl IPv6 module\n"); | ||
| 488 | } | ||
| 489 | } | ||
| 490 | 1; | ||
| 491 | }; | ||
| 492 | if (!$ret and $@) { | ||
| 493 | if ((defined $self->{_proxySocket}) && $attempts < 120) { | ||
| 494 | sleep(2); | ||
| 495 | $attempts++; | ||
| 496 | } else { | ||
| 497 | $self->_Close(); | ||
| 498 | die "$@\n"; | ||
| 499 | } | ||
| 500 | } else { | ||
| 501 | last; | ||
| 502 | } | ||
| 503 | } | ||
| 504 | |||
| 505 | ## a socket connection has been made now read the type of connection | ||
| 506 | ## setup to timeout if the remote endpoint is not valid | ||
| 507 | my $sock = $self->{_socket}; | ||
| 508 | $self->{_socket}->blocking(0); | ||
| 509 | my $rin = ''; | ||
| 510 | vec($rin, fileno($sock), 1) = 1; | ||
| 511 | my $timeout = 30; | ||
| 512 | my $nfound = select(my $rout = $rin, undef, undef, $timeout); | ||
| 513 | if ($nfound == 0) | ||
| 514 | { | ||
| 515 | $self->_Close(); | ||
| 516 | die "Connection handshake timed out after $timeout seconds\n"; | ||
| 517 | } | ||
| 518 | $sock->blocking(1); | ||
| 519 | my $ret = eval { | ||
| 520 | my $connectString = $self->_Recv(); | ||
| 521 | if($connectString eq 'proxy') { | ||
| 522 | $self->{_socket}->write($options); | ||
| 523 | $self->{_socket}->flush(); | ||
| 524 | $self->{_connectTokens} = $self->_Recv(); | ||
| 525 | my %connectTokens = split(/ /, $self->{_connectTokens}); | ||
| 526 | $self->{_proxySocket} = $self->{_socket}; | ||
| 527 | $self->{_socket} = undef; | ||
| 528 | $self->_InitialConnect($address, $connectTokens{-port}, ''); | ||
| 529 | } | ||
| 530 | 1; | ||
| 531 | }; | ||
| 532 | if (!$ret and $@) { | ||
| 533 | my $error = $@; | ||
| 534 | $self->_Close(); | ||
| 535 | die "$error"; | ||
| 536 | } | ||
| 537 | } | ||
| 538 | |||
| 539 | sub _Close { | ||
| 540 | my($self) = @_; | ||
| 541 | |||
| 542 | eval { | ||
| 543 | if (defined $self->{_socket}) { | ||
| 544 | close($self->{_socket}); | ||
| 545 | $self->{_socket} = undef; | ||
| 546 | } | ||
| 547 | if (defined $self->{_proxySocket}) { | ||
| 548 | close($self->{_proxySocket}); | ||
| 549 | $self->{_proxySocket} = undef; | ||
| 550 | } | ||
| 551 | $self->{_port} = undef; | ||
| 552 | $self->{_address} = undef; | ||
| 553 | $self->{_sessionId} = undef; | ||
| 554 | $self->{_serverusername} = undef; | ||
| 555 | }; | ||
| 556 | } | ||
| 557 | |||
| 558 | sub _Join { | ||
| 559 | my($self) = @_; | ||
| 560 | my @args = @_[1..scalar(@_) - 1]; | ||
| 561 | |||
| 562 | foreach my $arg (@args) { | ||
| 563 | if (ref($arg) eq 'ARRAY') { | ||
| 564 | if ($self->{_addContentSeparator} == 0) { | ||
| 565 | $self->{_sendContent} .= chr(0x02); | ||
| 566 | } | ||
| 567 | if ($self->{_addContentSeparator} > 0) { | ||
| 568 | $self->{_sendContent} .= '{'; | ||
| 569 | } | ||
| 570 | $self->{_addContentSeparator} += 1; | ||
| 571 | $self->{_firstItem} = 1; | ||
| 572 | if (scalar(@$arg) == 0) { | ||
| 573 | $self->{_sendContent} .= '{}'; | ||
| 574 | } else { | ||
| 575 | foreach my $item ($arg) { | ||
| 576 | $self->_Join(@$item); | ||
| 577 | } | ||
| 578 | } | ||
| 579 | if ($self->{_addContentSeparator} > 1) { | ||
| 580 | $self->{_sendContent} .= '}'; | ||
| 581 | } | ||
| 582 | $self->{_addContentSeparator} -= 1; | ||
| 583 | } else { | ||
| 584 | if ($self->{_addContentSeparator} == 0 and length($self->{_sendContent}) > 0) { | ||
| 585 | $self->{_sendContent} .= chr(0x02); | ||
| 586 | } elsif ($self->{_addContentSeparator} > 0) { | ||
| 587 | if (not defined $self->{_firstItem}) { | ||
| 588 | $self->{_sendContent} .= ' '; | ||
| 589 | } else { | ||
| 590 | $self->{_firstItem} = undef; | ||
| 591 | } | ||
| 592 | } | ||
| 593 | if (not defined $arg) { | ||
| 594 | $arg = ''; | ||
| 595 | } | ||
| 596 | if (length($arg) == 0 and length($self->{_sendContent}) > 0) { | ||
| 597 | $self->{_sendContent} .= '{}'; | ||
| 598 | } elsif (index($arg, ' ') != -1 and $self->{_addContentSeparator} > 0) { | ||
| 599 | $self->{_sendContent} .= "{$arg}"; | ||
| 600 | } else { | ||
| 601 | $self->{_sendContent} .= $arg; | ||
| 602 | } | ||
| 603 | } | ||
| 604 | } | ||
| 605 | } | ||
| 606 | |||
| 607 | sub _SendRecv { | ||
| 608 | my($self) = @_; | ||
| 609 | my @args = @_[1..scalar(@_) - 1]; | ||
| 610 | $self->{_addContentSeparator} = undef; | ||
| 611 | $self->{_firstItem} = 1; | ||
| 612 | |||
| 613 | my @argList = @args; | ||
| 614 | |||
| 615 | if (defined $self->{_async}) { | ||
| 616 | splice(@argList, 1, 0, '-async'); | ||
| 617 | } | ||
| 618 | |||
| 619 | if (defined $self->{_timeout}) { | ||
| 620 | splice(@argList, 1, 0, $self->{_timeout}); | ||
| 621 | splice(@argList, 1, 0, '-timeout'); | ||
| 622 | } | ||
| 623 | |||
| 624 | foreach my $arg (@argList) { | ||
| 625 | $self->_Join($arg); | ||
| 626 | } | ||
| 627 | |||
| 628 | $self->{_sendBuffer} .= $self->{_sendContent}.chr(0x03); | ||
| 629 | if (not defined $self->{_buffer}) { | ||
| 630 | if (defined $self->{_debug}) { | ||
| 631 | $self->_log('Sending: '.$self->{_sendBuffer}.chr(0x0a)); | ||
| 632 | } | ||
| 633 | $self->_Send(sprintf("<001><002><009%d>%s", length($self->{_sendBuffer}), $self->{_sendBuffer})); | ||
| 634 | $self->{_sendBuffer} = ''; | ||
| 635 | } | ||
| 636 | |||
| 637 | $self->{_async} = undef; | ||
| 638 | $self->{_timeout} = undef; | ||
| 639 | $self->{_buffer} = undef; | ||
| 640 | $self->{_sendContent} = ''; | ||
| 641 | |||
| 642 | if (length($self->{_sendBuffer}) > 0) { | ||
| 643 | return $self->{_OK}; | ||
| 644 | } else { | ||
| 645 | return $self->_Recv(); | ||
| 646 | } | ||
| 647 | } | ||
| 648 | |||
| 649 | sub _Send { | ||
| 650 | my($self) = @_; | ||
| 651 | my $content = $_[1]; | ||
| 652 | |||
| 653 | if (not defined $self->{_socket}) { | ||
| 654 | die "not connected\n"; | ||
| 655 | } else { | ||
| 656 | eval { | ||
| 657 | binmode($self->{_socket}, ":raw"); | ||
| 658 | $self->{_socket}->write($content); | ||
| 659 | $self->{_socket}->flush(); | ||
| 660 | }; | ||
| 661 | if($@) { | ||
| 662 | die "$@\n"; | ||
| 663 | } | ||
| 664 | } | ||
| 665 | } | ||
| 666 | |||
| 667 | sub _SendBinary { | ||
| 668 | my($self) = @_; | ||
| 669 | my $content = $_[1]; | ||
| 670 | |||
| 671 | if (not defined $self->{_socket}) { | ||
| 672 | die "not connected\n"; | ||
| 673 | } else { | ||
| 674 | eval { | ||
| 675 | binmode($self->{_socket}, ":raw"); | ||
| 676 | $self->{_socket}->write($content); | ||
| 677 | $self->{_socket}->flush(); | ||
| 678 | #binmode($self->{_socket}, ":crlf"); | ||
| 679 | }; | ||
| 680 | if($@) { | ||
| 681 | die "$@\n"; | ||
| 682 | } | ||
| 683 | } | ||
| 684 | } | ||
| 685 | |||
| 686 | sub _Recv { | ||
| 687 | my($self) = @_; | ||
| 688 | $self->{_decoratedResult} = ''; | ||
| 689 | my $responseBuffer = ''; | ||
| 690 | my $ret = eval { | ||
| 691 | while(1) { | ||
| 692 | $responseBuffer = ''; | ||
| 693 | my $commandId = undef; | ||
| 694 | my $contentLength = int(0); | ||
| 695 | |||
| 696 | while(1) { | ||
| 697 | my $buffer = ''; | ||
| 698 | $self->{_socket}->read($buffer, 1); | ||
| 699 | if (length($buffer) == 0) { | ||
| 700 | die "Socket connection is closed\n"; | ||
| 701 | } | ||
| 702 | $responseBuffer .= $buffer; | ||
| 703 | my $startIndex = index($responseBuffer, '<'); | ||
| 704 | my $stopIndex = index($responseBuffer, '>'); | ||
| 705 | if ($startIndex != -1 && $stopIndex != -1) { | ||
| 706 | $commandId = substr($responseBuffer, $startIndex + 1, $startIndex + 3); | ||
| 707 | if ($startIndex + 4 < $stopIndex) { | ||
| 708 | $contentLength = int(substr($responseBuffer, $startIndex + 4, $stopIndex)); | ||
| 709 | } | ||
| 710 | last; | ||
| 711 | } | ||
| 712 | } | ||
| 713 | |||
| 714 | if ($commandId == 1) { | ||
| 715 | my $buffer = ''; | ||
| 716 | $self->{_evalResult} = $self->{_evalError}; | ||
| 717 | $self->{_socket}->read($buffer, $contentLength); | ||
| 718 | } elsif ($commandId == 3) { | ||
| 719 | my $buffer = ''; | ||
| 720 | $self->{_socket}->read($buffer, $contentLength); | ||
| 721 | } elsif ($commandId == 4) { | ||
| 722 | $self->{_socket}->read($self->{_evalResult}, $contentLength); | ||
| 723 | } elsif ($commandId == 7) { | ||
| 724 | $self->{_socket}->read($self->{_filename}, $contentLength); | ||
| 725 | } elsif ($commandId == 8) { | ||
| 726 | open(my $binaryFile, '>:raw', $self->{_filename}); | ||
| 727 | my $chunk = ''; | ||
| 728 | my $bytesToRead = 32767; | ||
| 729 | while ($contentLength > 0) { | ||
| 730 | if ($contentLength < $bytesToRead) { | ||
| 731 | $bytesToRead = $contentLength; | ||
| 732 | } | ||
| 733 | $self->{_socket}->read($chunk, $bytesToRead); | ||
| 734 | $binaryFile->write($chunk); | ||
| 735 | $contentLength -= length($chunk); | ||
| 736 | } | ||
| 737 | $binaryFile->close(); | ||
| 738 | } elsif ($commandId == 9) { | ||
| 739 | $self->{_decoratedResult} = ''; | ||
| 740 | my $chunk = ''; | ||
| 741 | my $bytesToRead = 32767; | ||
| 742 | while($contentLength > 0) { | ||
| 743 | if ($contentLength < $bytesToRead) { | ||
| 744 | $bytesToRead = $contentLength; | ||
| 745 | } | ||
| 746 | $self->{_socket}->read($chunk, $bytesToRead); | ||
| 747 | $self->{_decoratedResult} .= $chunk; | ||
| 748 | $contentLength -= length($chunk); | ||
| 749 | } | ||
| 750 | last; | ||
| 751 | } | ||
| 752 | } | ||
| 753 | 1; | ||
| 754 | }; | ||
| 755 | if (!$ret and $@) { | ||
| 756 | $self->_Close(); | ||
| 757 | die "$@\n"; | ||
| 758 | } | ||
| 759 | |||
| 760 | if (defined $self->{_debug}) { | ||
| 761 | $self->_log('Received: '.$self->{_decoratedResult}.chr(0x0a)); | ||
| 762 | } | ||
| 763 | |||
| 764 | if ($self->{_evalResult} eq $self->{_evalError}) { | ||
| 765 | die "$self->{_decoratedResult}\n"; | ||
| 766 | } | ||
| 767 | |||
| 768 | if (index($self->{_decoratedResult}, '(') == 0) { | ||
| 769 | my @array; | ||
| 770 | eval '@array = '.$self->{_decoratedResult}; | ||
| 771 | return @array; | ||
| 772 | } else { | ||
| 773 | return $self->{_decoratedResult}; | ||
| 774 | } | ||
| 775 | } | ||
| 776 | |||
| 777 | sub _CheckClientVersion { | ||
| 778 | my($self) = @_; | ||
| 779 | if ($self->{_version} ne $self->getVersion()) { | ||
| 780 | print("WARNING: IxNetwork Perl library version ".$self->{_version}." is not matching the IxNetwork client version ".$self->getVersion()."\n"); | ||
| 781 | } | ||
| 782 | } | ||
| 783 | |||
| 784 | sub _isConnected { | ||
| 785 | my ($self, $raiseError) = @_; | ||
| 786 | if (not defined $raiseError) { | ||
| 787 | $raiseError = 0; | ||
| 788 | } | ||
| 789 | |||
| 790 | if (!defined($self->{_socket})) { | ||
| 791 | if ($raiseError) { | ||
| 792 | die "not connected\n"; | ||
| 793 | } else { | ||
| 794 | return 0; | ||
| 795 | } | ||
| 796 | } else { | ||
| 797 | return 1; | ||
| 798 | } | ||
| 799 | } | ||
| 800 | |||
| 801 | sub _log { | ||
| 802 | my $self = shift; | ||
| 803 | my $msg = shift; | ||
| 804 | |||
| 805 | if ($self->{_debug}) { | ||
| 806 | my $now = localtime(time); | ||
| 807 | if (length($msg) > 1024) { | ||
| 808 | $msg = substr($msg, 0, 1024)."...\n"; | ||
| 809 | } | ||
| 810 | print '['.$now.'] [IxNet] [debug] '.$msg; | ||
| 811 | } | ||
| 812 | } | ||
| 813 | |||
| 814 | 1; |