tcp_client.pl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env perl
  2. # A simple TCP client that sends some data and expects a response.
  3. # Usage: tcp_client.pl HOSTNAME PORT DATA1 RESPONSE1
  4. # DATA: hex-encoded data to send to the server
  5. # RESPONSE: regexp that must match the server's response
  6. #
  7. # Copyright The Mbed TLS Contributors
  8. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  9. #
  10. # This file is provided under the Apache License 2.0, or the
  11. # GNU General Public License v2.0 or later.
  12. #
  13. # **********
  14. # Apache License 2.0:
  15. #
  16. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  17. # not use this file except in compliance with the License.
  18. # You may obtain a copy of the License at
  19. #
  20. # http://www.apache.org/licenses/LICENSE-2.0
  21. #
  22. # Unless required by applicable law or agreed to in writing, software
  23. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  24. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. # See the License for the specific language governing permissions and
  26. # limitations under the License.
  27. #
  28. # **********
  29. #
  30. # **********
  31. # GNU General Public License v2.0 or later:
  32. #
  33. # This program is free software; you can redistribute it and/or modify
  34. # it under the terms of the GNU General Public License as published by
  35. # the Free Software Foundation; either version 2 of the License, or
  36. # (at your option) any later version.
  37. #
  38. # This program is distributed in the hope that it will be useful,
  39. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41. # GNU General Public License for more details.
  42. #
  43. # You should have received a copy of the GNU General Public License along
  44. # with this program; if not, write to the Free Software Foundation, Inc.,
  45. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  46. #
  47. # **********
  48. use warnings;
  49. use strict;
  50. use IO::Socket::INET;
  51. # Pack hex digits into a binary string, ignoring whitespace.
  52. sub parse_hex {
  53. my ($hex) = @_;
  54. $hex =~ s/\s+//g;
  55. return pack('H*', $hex);
  56. }
  57. ## Open a TCP connection to the specified host and port.
  58. sub open_connection {
  59. my ($host, $port) = @_;
  60. my $socket = IO::Socket::INET->new(PeerAddr => $host,
  61. PeerPort => $port,
  62. Proto => 'tcp',
  63. Timeout => 1);
  64. die "Cannot connect to $host:$port: $!" unless $socket;
  65. return $socket;
  66. }
  67. ## Close the TCP connection.
  68. sub close_connection {
  69. my ($connection) = @_;
  70. $connection->shutdown(2);
  71. # Ignore shutdown failures (at least for now)
  72. return 1;
  73. }
  74. ## Write the given data, expressed as hexadecimal
  75. sub write_data {
  76. my ($connection, $hexdata) = @_;
  77. my $data = parse_hex($hexdata);
  78. my $total_sent = 0;
  79. while ($total_sent < length($data)) {
  80. my $sent = $connection->send($data, 0);
  81. if (!defined $sent) {
  82. die "Unable to send data: $!";
  83. }
  84. $total_sent += $sent;
  85. }
  86. return 1;
  87. }
  88. ## Read a response and check it against an expected prefix
  89. sub read_response {
  90. my ($connection, $expected_hex) = @_;
  91. my $expected_data = parse_hex($expected_hex);
  92. my $start_offset = 0;
  93. while ($start_offset < length($expected_data)) {
  94. my $actual_data;
  95. my $ok = $connection->recv($actual_data, length($expected_data));
  96. if (!defined $ok) {
  97. die "Unable to receive data: $!";
  98. }
  99. if (($actual_data ^ substr($expected_data, $start_offset)) =~ /[^\000]/) {
  100. printf STDERR ("Received \\x%02x instead of \\x%02x at offset %d\n",
  101. ord(substr($actual_data, $-[0], 1)),
  102. ord(substr($expected_data, $start_offset + $-[0], 1)),
  103. $start_offset + $-[0]);
  104. return 0;
  105. }
  106. $start_offset += length($actual_data);
  107. }
  108. return 1;
  109. }
  110. if (@ARGV != 4) {
  111. print STDERR "Usage: $0 HOSTNAME PORT DATA1 RESPONSE1\n";
  112. exit(3);
  113. }
  114. my ($host, $port, $data1, $response1) = @ARGV;
  115. my $connection = open_connection($host, $port);
  116. write_data($connection, $data1);
  117. if (!read_response($connection, $response1)) {
  118. exit(1);
  119. }
  120. close_connection($connection);