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