Disambiguate info_print_options
[deliverable/binutils-gdb.git] / gdb / ser-tcp.c
CommitLineData
f9f87d2c
MK
1/* Serial interface for raw TCP connections on Un*x like systems.
2
b811d2c2 3 Copyright (C) 1992-2020 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
21#include "serial.h"
3eb25fda 22#include "ser-base.h"
0ea3f30e 23#include "ser-tcp.h"
84603566
SL
24#include "gdbcmd.h"
25#include "cli/cli-decode.h"
26#include "cli/cli-setshow.h"
268a13a5
TT
27#include "gdbsupport/filestuff.h"
28#include "gdbsupport/netstuff.h"
c2c6d25f 29
c906108c 30#include <sys/types.h>
0cf3e697
MH
31
32#ifdef HAVE_SYS_FILIO_H
c378eb4e 33#include <sys/filio.h> /* For FIONBIO. */
0cf3e697
MH
34#endif
35#ifdef HAVE_SYS_IOCTL_H
c378eb4e 36#include <sys/ioctl.h> /* For FIONBIO. */
0cf3e697
MH
37#endif
38
268a13a5 39#include "gdbsupport/gdb_sys_time.h"
b4505029
MM
40
41#ifdef USE_WIN32API
41fa577f 42#include <ws2tcpip.h>
8e7ebaf5 43#ifndef ETIMEDOUT
b4505029 44#define ETIMEDOUT WSAETIMEDOUT
8e7ebaf5 45#endif
6ec2e0f5
SDJ
46/* Gnulib defines close too, but gnulib's replacement
47 doesn't call closesocket unless we import the
48 socketlib module. */
49#undef close
056d7646 50#define close(fd) closesocket (fd)
b4505029
MM
51#define ioctl ioctlsocket
52#else
9eb1356e 53#include <netinet/in.h>
c906108c
SS
54#include <arpa/inet.h>
55#include <netdb.h>
9eb1356e 56#include <sys/socket.h>
c906108c 57#include <netinet/tcp.h>
b4505029 58#endif
c906108c 59
042be3a9 60#include <signal.h>
84603566 61#include "gdb_select.h"
325fac50 62#include <algorithm>
c906108c 63
f9f87d2c
MK
64#ifndef HAVE_SOCKLEN_T
65typedef int socklen_t;
66#endif
67
84603566
SL
68/* For "set tcp" and "show tcp". */
69
70static struct cmd_list_element *tcp_set_cmdlist;
71static struct cmd_list_element *tcp_show_cmdlist;
72
73/* Whether to auto-retry refused connections. */
74
491144b5 75static bool tcp_auto_retry = true;
84603566
SL
76
77/* Timeout period for connections, in seconds. */
78
964b8317 79static unsigned int tcp_retry_limit = 15;
84603566 80
c378eb4e 81/* How many times per second to poll deprecated_ui_loop_hook. */
84603566
SL
82
83#define POLL_INTERVAL 5
84
c7ab0aef
SDJ
85/* Helper function to wait a while. If SOCK is not -1, wait on its
86 file descriptor. Otherwise just wait on a timeout, updating
87 *POLLS. Returns -1 on timeout or interrupt, otherwise the value of
88 select. */
84603566
SL
89
90static int
c7ab0aef 91wait_for_connect (int sock, unsigned int *polls)
84603566
SL
92{
93 struct timeval t;
94 int n;
95
96 /* While we wait for the connect to complete,
97 poll the UI so it can update or the user can
98 interrupt. */
99 if (deprecated_ui_loop_hook && deprecated_ui_loop_hook (0))
100 {
101 errno = EINTR;
102 return -1;
103 }
104
105 /* Check for timeout. */
106 if (*polls > tcp_retry_limit * POLL_INTERVAL)
107 {
108 errno = ETIMEDOUT;
109 return -1;
110 }
111
112 /* Back off to polling once per second after the first POLL_INTERVAL
113 polls. */
114 if (*polls < POLL_INTERVAL)
115 {
116 t.tv_sec = 0;
117 t.tv_usec = 1000000 / POLL_INTERVAL;
118 }
119 else
120 {
121 t.tv_sec = 1;
122 t.tv_usec = 0;
123 }
124
c7ab0aef 125 if (sock >= 0)
84603566
SL
126 {
127 fd_set rset, wset, eset;
433759f7 128
84603566 129 FD_ZERO (&rset);
c7ab0aef 130 FD_SET (sock, &rset);
84603566
SL
131 wset = rset;
132 eset = rset;
c7ab0aef 133
84603566
SL
134 /* POSIX systems return connection success or failure by signalling
135 wset. Windows systems return success in wset and failure in
136 eset.
c7ab0aef 137
84603566
SL
138 We must call select here, rather than gdb_select, because
139 the serial structure has not yet been initialized - the
140 MinGW select wrapper will not know that this FD refers
141 to a socket. */
c7ab0aef 142 n = select (sock + 1, &rset, &wset, &eset, &t);
84603566
SL
143 }
144 else
145 /* Use gdb_select here, since we have no file descriptors, and on
146 Windows, plain select doesn't work in that case. */
147 n = gdb_select (0, NULL, NULL, NULL, &t);
148
149 /* If we didn't time out, only count it as one poll. */
150 if (n > 0 || *polls < POLL_INTERVAL)
151 (*polls)++;
152 else
153 (*polls) += POLL_INTERVAL;
154
155 return n;
156}
7c7a201a 157
c7ab0aef
SDJ
158/* Try to connect to the host represented by AINFO. If the connection
159 succeeds, return its socket. Otherwise, return -1 and set ERRNO
160 accordingly. POLLS is used when 'connect' returns EINPROGRESS, and
161 we need to invoke 'wait_for_connect' to obtain the status. */
c906108c 162
c7ab0aef
SDJ
163static int
164try_connect (const struct addrinfo *ainfo, unsigned int *polls)
c906108c 165{
c7ab0aef
SDJ
166 int sock = gdb_socket_cloexec (ainfo->ai_family, ainfo->ai_socktype,
167 ainfo->ai_protocol);
84603566 168
c7ab0aef 169 if (sock < 0)
7c7a201a 170 return -1;
c7ab0aef 171
c378eb4e 172 /* Set socket nonblocking. */
64b58472
SDJ
173#ifdef USE_WIN32API
174 u_long ioarg = 1;
175#else
c7ab0aef 176 int ioarg = 1;
64b58472 177#endif
c7ab0aef
SDJ
178
179 ioctl (sock, FIONBIO, &ioarg);
c906108c 180
3e43a32a 181 /* Use Non-blocking connect. connect() will return 0 if connected
c378eb4e 182 already. */
c7ab0aef 183 if (connect (sock, ainfo->ai_addr, ainfo->ai_addrlen) < 0)
84603566 184 {
b4505029 185#ifdef USE_WIN32API
84603566 186 int err = WSAGetLastError();
b4505029 187#else
84603566 188 int err = errno;
b4505029 189#endif
84603566 190
c7ab0aef
SDJ
191 /* If we've got a "connection refused" error, just return
192 -1. The caller will know what to do. */
193 if (
b4505029 194#ifdef USE_WIN32API
c7ab0aef 195 err == WSAECONNREFUSED
84603566 196#else
c7ab0aef 197 err == ECONNREFUSED
b4505029 198#endif
c7ab0aef 199 )
84603566 200 {
c7ab0aef
SDJ
201 close (sock);
202 errno = err;
203 return -1;
84603566 204 }
c906108c 205
84603566 206 if (
c7ab0aef
SDJ
207 /* Any other error (except EINPROGRESS) will be "swallowed"
208 here. We return without specifying a return value, and
209 set errno if the caller wants to inspect what
210 happened. */
84603566
SL
211#ifdef USE_WIN32API
212 /* Under Windows, calling "connect" with a non-blocking socket
213 results in WSAEWOULDBLOCK, not WSAEINPROGRESS. */
214 err != WSAEWOULDBLOCK
215#else
216 err != EINPROGRESS
217#endif
218 )
219 {
c7ab0aef 220 close (sock);
84603566 221 errno = err;
84603566
SL
222 return -1;
223 }
7c7a201a 224
c378eb4e 225 /* Looks like we need to wait for the connect. */
c7ab0aef
SDJ
226 int n;
227
228 do
229 n = wait_for_connect (sock, polls);
84603566 230 while (n == 0);
c7ab0aef 231
84603566 232 if (n < 0)
7c7a201a 233 {
c7ab0aef
SDJ
234 int saved_errno = errno;
235
236 /* A negative value here means that we either timed out or
237 got interrupted by the user. Just return. */
238 close (sock);
239 errno = saved_errno;
7c7a201a
MH
240 return -1;
241 }
242 }
c906108c 243
c378eb4e 244 /* Got something. Is it an error? */
c7ab0aef
SDJ
245 int err;
246 socklen_t len = sizeof (err);
247
248 /* On Windows, the fourth parameter to getsockopt is a "char *";
249 on UNIX systems it is generally "void *". The cast to "char *"
250 is OK everywhere, since in C++ any data pointer type can be
251 implicitly converted to "void *". */
252 int ret = getsockopt (sock, SOL_SOCKET, SO_ERROR, (char *) &err, &len);
253
254 if (ret < 0)
255 {
256 int saved_errno = errno;
257
258 close (sock);
259 errno = saved_errno;
260 return -1;
261 }
262 else if (ret == 0 && err != 0)
263 {
264 close (sock);
265 errno = err;
266 return -1;
267 }
268
269 /* The connection succeeded. Return the socket. */
270 return sock;
271}
272
273/* Open a tcp socket. */
274
275int
276net_open (struct serial *scb, const char *name)
277{
278 struct addrinfo hint;
279 struct addrinfo *ainfo;
280
281 memset (&hint, 0, sizeof (hint));
282 /* Assume no prefix will be passed, therefore we should use
283 AF_UNSPEC. */
284 hint.ai_family = AF_UNSPEC;
285 hint.ai_socktype = SOCK_STREAM;
286 hint.ai_protocol = IPPROTO_TCP;
287
288 parsed_connection_spec parsed = parse_connection_spec (name, &hint);
289
290 if (parsed.port_str.empty ())
291 error (_("Missing port on hostname '%s'"), name);
292
293 int r = getaddrinfo (parsed.host_str.c_str (),
294 parsed.port_str.c_str (),
295 &hint, &ainfo);
296
297 if (r != 0)
298 {
299 fprintf_unfiltered (gdb_stderr, _("%s: cannot resolve name: %s\n"),
300 name, gai_strerror (r));
301 errno = ENOENT;
302 return -1;
303 }
304
305 scoped_free_addrinfo free_ainfo (ainfo);
306
307 /* Flag to indicate whether we've got a connection refused. It will
308 be true if any of the connections tried was refused. */
309 bool got_connrefused;
30baf67b 310 /* If a connection succeeds, SUCCESS_AINFO will point to the
c7ab0aef
SDJ
311 'struct addrinfo' that succeed. */
312 struct addrinfo *success_ainfo = NULL;
313 unsigned int polls = 0;
314
315 /* Assume the worst. */
316 scb->fd = -1;
317
318 do
319 {
320 got_connrefused = false;
321
bde09ab7 322 for (addrinfo *iter = ainfo; iter != NULL; iter = iter->ai_next)
c7ab0aef
SDJ
323 {
324 /* Iterate over the list of possible addresses to connect
325 to. For each, we'll try to connect and see if it
326 succeeds. */
327 int sock = try_connect (iter, &polls);
328
329 if (sock >= 0)
330 {
331 /* We've gotten a successful connection. Save its
332 'struct addrinfo', the socket, and break. */
333 success_ainfo = iter;
334 scb->fd = sock;
335 break;
336 }
337 else if (
84603566 338#ifdef USE_WIN32API
c7ab0aef 339 errno == WSAECONNREFUSED
84603566 340#else
c7ab0aef 341 errno == ECONNREFUSED
84603566 342#endif
c7ab0aef
SDJ
343 )
344 got_connrefused = true;
345 }
346 }
347 /* Just retry if:
348
349 - tcp_auto_retry is true, and
350 - We haven't gotten a connection yet, and
351 - Any of our connection attempts returned with ECONNREFUSED, and
352 - wait_for_connect signals that we can keep going. */
353 while (tcp_auto_retry
354 && success_ainfo == NULL
355 && got_connrefused
356 && wait_for_connect (-1, &polls) >= 0);
357
358 if (success_ainfo == NULL)
359 {
360 net_close (scb);
361 return -1;
362 }
9db8d71f 363
c378eb4e 364 /* Turn off nonblocking. */
c7ab0aef
SDJ
365#ifdef USE_WIN32API
366 u_long ioarg = 0;
367#else
368 int ioarg = 0;
369#endif
370
b4505029 371 ioctl (scb->fd, FIONBIO, &ioarg);
7c7a201a 372
425699f5 373 if (success_ainfo->ai_protocol == IPPROTO_TCP)
9db8d71f 374 {
c378eb4e 375 /* Disable Nagle algorithm. Needed in some cases. */
c7ab0aef
SDJ
376 int tmp = 1;
377
9db8d71f 378 setsockopt (scb->fd, IPPROTO_TCP, TCP_NODELAY,
c7ab0aef 379 (char *) &tmp, sizeof (tmp));
9db8d71f
DJ
380 }
381
6d318c73 382#ifdef SIGPIPE
7c7a201a
MH
383 /* If we don't do this, then GDB simply exits
384 when the remote side dies. */
385 signal (SIGPIPE, SIG_IGN);
6d318c73 386#endif
c906108c
SS
387
388 return 0;
389}
390
0ea3f30e 391void
9db8d71f 392net_close (struct serial *scb)
c906108c 393{
363a6e9f 394 if (scb->fd == -1)
c906108c
SS
395 return;
396
c5aa993b 397 close (scb->fd);
c906108c
SS
398 scb->fd = -1;
399}
400
0ea3f30e 401int
b4505029
MM
402net_read_prim (struct serial *scb, size_t count)
403{
c49e7f76
PA
404 /* Need to cast to silence -Wpointer-sign on MinGW, as Winsock's
405 'recv' takes 'char *' as second argument, while 'scb->buf' is
406 'unsigned char *'. */
69e976f8 407 return recv (scb->fd, (char *) scb->buf, count, 0);
b4505029
MM
408}
409
0ea3f30e 410int
b4505029
MM
411net_write_prim (struct serial *scb, const void *buf, size_t count)
412{
69e976f8
PA
413 /* On Windows, the second parameter to send is a "const char *"; on
414 UNIX systems it is generally "const void *". The cast to "const
415 char *" is OK everywhere, since in C++ any data pointer type can
416 be implicitly converted to "const void *". */
417 return send (scb->fd, (const char *) buf, count, 0);
b4505029
MM
418}
419
8775bb90
MS
420int
421ser_tcp_send_break (struct serial *scb)
422{
c378eb4e 423 /* Send telnet IAC and BREAK characters. */
8775bb90
MS
424 return (serial_write (scb, "\377\363", 2));
425}
426
84603566
SL
427/* Support for "set tcp" and "show tcp" commands. */
428
429static void
981a3fb3 430set_tcp_cmd (const char *args, int from_tty)
84603566 431{
635c7e8a 432 help_list (tcp_set_cmdlist, "set tcp ", all_commands, gdb_stdout);
84603566
SL
433}
434
435static void
981a3fb3 436show_tcp_cmd (const char *args, int from_tty)
84603566 437{
635c7e8a 438 help_list (tcp_show_cmdlist, "show tcp ", all_commands, gdb_stdout);
84603566
SL
439}
440
12e8c7d7
TT
441#ifndef USE_WIN32API
442
443/* The TCP ops. */
444
445static const struct serial_ops tcp_ops =
446{
447 "tcp",
448 net_open,
449 net_close,
450 NULL,
451 ser_base_readchar,
452 ser_base_write,
453 ser_base_flush_output,
454 ser_base_flush_input,
455 ser_tcp_send_break,
456 ser_base_raw,
457 ser_base_get_tty_state,
458 ser_base_copy_tty_state,
459 ser_base_set_tty_state,
460 ser_base_print_tty_state,
12e8c7d7
TT
461 ser_base_setbaudrate,
462 ser_base_setstopbits,
236af5e3 463 ser_base_setparity,
12e8c7d7
TT
464 ser_base_drain_output,
465 ser_base_async,
466 net_read_prim,
467 net_write_prim
468};
469
470#endif /* USE_WIN32API */
84603566 471
6c265988 472void _initialize_ser_tcp ();
c906108c 473void
6c265988 474_initialize_ser_tcp ()
c906108c 475{
b4505029 476#ifdef USE_WIN32API
0ea3f30e
DJ
477 /* Do nothing; the TCP serial operations will be initialized in
478 ser-mingw.c. */
0ea3f30e 479#else
12e8c7d7 480 serial_add_interface (&tcp_ops);
0ea3f30e 481#endif /* USE_WIN32API */
84603566
SL
482
483 add_prefix_cmd ("tcp", class_maintenance, set_tcp_cmd, _("\
590042fc
PW
484TCP protocol specific variables.\n\
485Configure variables specific to remote TCP connections."),
84603566
SL
486 &tcp_set_cmdlist, "set tcp ",
487 0 /* allow-unknown */, &setlist);
488 add_prefix_cmd ("tcp", class_maintenance, show_tcp_cmd, _("\
590042fc
PW
489TCP protocol specific variables.\n\
490Configure variables specific to remote TCP connections."),
84603566
SL
491 &tcp_show_cmdlist, "show tcp ",
492 0 /* allow-unknown */, &showlist);
493
494 add_setshow_boolean_cmd ("auto-retry", class_obscure,
495 &tcp_auto_retry, _("\
590042fc
PW
496Set auto-retry on socket connect."), _("\
497Show auto-retry on socket connect."),
84603566
SL
498 NULL, NULL, NULL,
499 &tcp_set_cmdlist, &tcp_show_cmdlist);
500
501 add_setshow_uinteger_cmd ("connect-timeout", class_obscure,
502 &tcp_retry_limit, _("\
590042fc
PW
503Set timeout limit in seconds for socket connection."), _("\
504Show timeout limit in seconds for socket connection."), _("\
f81d1120
PA
505If set to \"unlimited\", GDB will keep attempting to establish a\n\
506connection forever, unless interrupted with Ctrl-c.\n\
507The default is 15 seconds."),
508 NULL, NULL,
509 &tcp_set_cmdlist, &tcp_show_cmdlist);
c906108c 510}
This page took 1.87778 seconds and 4 git commands to generate.