1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
/* tcpget - get single response from a TCP port.
Copyright (C) 2013-2025 Free Software Foundation, Inc.
This file is part of GNU Inetutils.
GNU Inetutils is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Inetutils is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see `http://www.gnu.org/licenses/'. */
/* Written by Mats Erik Andersson. */
/* Tcpget receives and displays whatever text a server cares to send
* without any interaction from a client and within a set time period.
* An alarm timer is set to five seconds by default, but the limit
* can be set to another value with a command line switch, starting
* at one second, but limited upwards to one hour!
*
* Invocation:
*
* tcpget [-t secs] host tcp-port
*/
#include <config.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <progname.h>
#ifdef HAVE_LOCALE_H
# include <locale.h>
#endif
int
main (int argc, char *argv[])
{
int fd, opt, rc;
int timeout = 5; /* Defaulting to five seconds of waiting time. */
char buffer[256];
struct addrinfo hints, *ai, *res;
set_program_name (argv[0]);
#ifdef HAVE_SETLOCALE
setlocale (LC_ALL, "");
#endif
while ((opt = getopt (argc, argv, "t:")) != -1)
{
int t;
switch (opt)
{
case 't':
t = atoi (optarg);
if (t > 0 && t <= 3600 /* on hour */ )
timeout = t;
break;
default:
fprintf (stderr, "Usage: %s [-t secs] host port\n", argv[0]);
exit (EXIT_FAILURE);
}
}
if (argc < optind + 2)
return (EXIT_FAILURE);
memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
rc = getaddrinfo (argv[optind], argv[optind + 1], &hints, &res);
if (rc)
{
fprintf (stderr, "%s: %s\n", argv[0], gai_strerror (rc));
return (EXIT_FAILURE);
}
for (ai = res; ai; ai = ai->ai_next)
{
fd = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (fd < 0)
continue;
if (connect (fd, ai->ai_addr, ai->ai_addrlen) >= 0)
break;
close (fd);
}
freeaddrinfo (res);
if (ai && (fd >= 0))
{
ssize_t n;
alarm (timeout);
while ((n = recv (fd, buffer, sizeof (buffer), 0)))
write (STDOUT_FILENO, buffer, n);
close (fd);
}
close (STDIN_FILENO);
close (STDOUT_FILENO);
close (STDERR_FILENO);
return EXIT_SUCCESS;
}
|