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