Debian Codegraph @

tests/tcpget.c

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