GDB (xrefs)
/tmp/gdb-8.1/gdb/ser-tcp.c
Go to the documentation of this file.
1 /* Serial interface for raw TCP connections on Un*x like systems.
2 
3  Copyright (C) 1992-2018 Free Software Foundation, Inc.
4 
5  This file is part of GDB.
6 
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
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11 
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.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 
20 #include "defs.h"
21 #include "serial.h"
22 #include "ser-base.h"
23 #include "ser-tcp.h"
24 #include "gdbcmd.h"
25 #include "cli/cli-decode.h"
26 #include "cli/cli-setshow.h"
27 #include "filestuff.h"
28 
29 #include <sys/types.h>
30 
31 #ifdef HAVE_SYS_FILIO_H
32 #include <sys/filio.h> /* For FIONBIO. */
33 #endif
34 #ifdef HAVE_SYS_IOCTL_H
35 #include <sys/ioctl.h> /* For FIONBIO. */
36 #endif
37 
38 #include "gdb_sys_time.h"
39 
40 #ifdef USE_WIN32API
41 #include <winsock2.h>
42 #ifndef ETIMEDOUT
43 #define ETIMEDOUT WSAETIMEDOUT
44 #endif
45 /* Gnulib defines close too, but gnulib's replacement
46  doesn't call closesocket unless we import the
47  socketlib module. */
48 #undef close
49 #define close(fd) closesocket (fd)
50 #define ioctl ioctlsocket
51 #else
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54 #include <netdb.h>
55 #include <sys/socket.h>
56 #include <netinet/tcp.h>
57 #endif
58 
59 #include <signal.h>
60 #include "gdb_select.h"
61 #include <algorithm>
62 
63 #ifndef HAVE_SOCKLEN_T
64 typedef int socklen_t;
65 #endif
66 
67 /* For "set tcp" and "show tcp". */
68 
71 
72 /* Whether to auto-retry refused connections. */
73 
74 static int tcp_auto_retry = 1;
75 
76 /* Timeout period for connections, in seconds. */
77 
78 static unsigned int tcp_retry_limit = 15;
79 
80 /* How many times per second to poll deprecated_ui_loop_hook. */
81 
82 #define POLL_INTERVAL 5
83 
84 /* Helper function to wait a while. If SCB is non-null, wait on its
85  file descriptor. Otherwise just wait on a timeout, updating *POLLS.
86  Returns -1 on timeout or interrupt, otherwise the value of select. */
87 
88 static int
89 wait_for_connect (struct serial *scb, unsigned int *polls)
90 {
91  struct timeval t;
92  int n;
93 
94  /* While we wait for the connect to complete,
95  poll the UI so it can update or the user can
96  interrupt. */
98  {
99  errno = EINTR;
100  return -1;
101  }
102 
103  /* Check for timeout. */
104  if (*polls > tcp_retry_limit * POLL_INTERVAL)
105  {
106  errno = ETIMEDOUT;
107  return -1;
108  }
109 
110  /* Back off to polling once per second after the first POLL_INTERVAL
111  polls. */
112  if (*polls < POLL_INTERVAL)
113  {
114  t.tv_sec = 0;
115  t.tv_usec = 1000000 / POLL_INTERVAL;
116  }
117  else
118  {
119  t.tv_sec = 1;
120  t.tv_usec = 0;
121  }
122 
123  if (scb)
124  {
125  fd_set rset, wset, eset;
126 
127  FD_ZERO (&rset);
128  FD_SET (scb->fd, &rset);
129  wset = rset;
130  eset = rset;
131 
132  /* POSIX systems return connection success or failure by signalling
133  wset. Windows systems return success in wset and failure in
134  eset.
135 
136  We must call select here, rather than gdb_select, because
137  the serial structure has not yet been initialized - the
138  MinGW select wrapper will not know that this FD refers
139  to a socket. */
140  n = select (scb->fd + 1, &rset, &wset, &eset, &t);
141  }
142  else
143  /* Use gdb_select here, since we have no file descriptors, and on
144  Windows, plain select doesn't work in that case. */
145  n = gdb_select (0, NULL, NULL, NULL, &t);
146 
147  /* If we didn't time out, only count it as one poll. */
148  if (n > 0 || *polls < POLL_INTERVAL)
149  (*polls)++;
150  else
151  (*polls) += POLL_INTERVAL;
152 
153  return n;
154 }
155 
156 /* Open a tcp socket. */
157 
158 int
159 net_open (struct serial *scb, const char *name)
160 {
161  char hostname[100];
162  const char *port_str;
163  int n, port, tmp;
164  int use_udp;
165  struct hostent *hostent;
166  struct sockaddr_in sockaddr;
167 #ifdef USE_WIN32API
168  u_long ioarg;
169 #else
170  int ioarg;
171 #endif
172  unsigned int polls = 0;
173 
174  use_udp = 0;
175  if (startswith (name, "udp:"))
176  {
177  use_udp = 1;
178  name = name + 4;
179  }
180  else if (startswith (name, "tcp:"))
181  name = name + 4;
182 
183  port_str = strchr (name, ':');
184 
185  if (!port_str)
186  error (_("net_open: No colon in host name!")); /* Shouldn't ever
187  happen. */
188 
189  tmp = std::min (port_str - name, (ptrdiff_t) sizeof hostname - 1);
190  strncpy (hostname, name, tmp); /* Don't want colon. */
191  hostname[tmp] = '\000'; /* Tie off host name. */
192  port = atoi (port_str + 1);
193 
194  /* Default hostname is localhost. */
195  if (!hostname[0])
196  strcpy (hostname, "localhost");
197 
198  hostent = gethostbyname (hostname);
199  if (!hostent)
200  {
201  fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname);
202  errno = ENOENT;
203  return -1;
204  }
205 
206  sockaddr.sin_family = PF_INET;
207  sockaddr.sin_port = htons (port);
208  memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
209  sizeof (struct in_addr));
210 
211  retry:
212 
213  if (use_udp)
214  scb->fd = gdb_socket_cloexec (PF_INET, SOCK_DGRAM, 0);
215  else
216  scb->fd = gdb_socket_cloexec (PF_INET, SOCK_STREAM, 0);
217 
218  if (scb->fd == -1)
219  return -1;
220 
221  /* Set socket nonblocking. */
222  ioarg = 1;
223  ioctl (scb->fd, FIONBIO, &ioarg);
224 
225  /* Use Non-blocking connect. connect() will return 0 if connected
226  already. */
227  n = connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr));
228 
229  if (n < 0)
230  {
231 #ifdef USE_WIN32API
232  int err = WSAGetLastError();
233 #else
234  int err = errno;
235 #endif
236 
237  /* Maybe we're waiting for the remote target to become ready to
238  accept connections. */
239  if (tcp_auto_retry
240 #ifdef USE_WIN32API
241  && err == WSAECONNREFUSED
242 #else
243  && err == ECONNREFUSED
244 #endif
245  && wait_for_connect (NULL, &polls) >= 0)
246  {
247  close (scb->fd);
248  goto retry;
249  }
250 
251  if (
252 #ifdef USE_WIN32API
253  /* Under Windows, calling "connect" with a non-blocking socket
254  results in WSAEWOULDBLOCK, not WSAEINPROGRESS. */
255  err != WSAEWOULDBLOCK
256 #else
257  err != EINPROGRESS
258 #endif
259  )
260  {
261  errno = err;
262  net_close (scb);
263  return -1;
264  }
265 
266  /* Looks like we need to wait for the connect. */
267  do
268  {
269  n = wait_for_connect (scb, &polls);
270  }
271  while (n == 0);
272  if (n < 0)
273  {
274  net_close (scb);
275  return -1;
276  }
277  }
278 
279  /* Got something. Is it an error? */
280  {
281  int res, err;
282  socklen_t len;
283 
284  len = sizeof (err);
285  /* On Windows, the fourth parameter to getsockopt is a "char *";
286  on UNIX systems it is generally "void *". The cast to "char *"
287  is OK everywhere, since in C++ any data pointer type can be
288  implicitly converted to "void *". */
289  res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, (char *) &err, &len);
290  if (res < 0 || err)
291  {
292  /* Maybe the target still isn't ready to accept the connection. */
293  if (tcp_auto_retry
294 #ifdef USE_WIN32API
295  && err == WSAECONNREFUSED
296 #else
297  && err == ECONNREFUSED
298 #endif
299  && wait_for_connect (NULL, &polls) >= 0)
300  {
301  close (scb->fd);
302  goto retry;
303  }
304  if (err)
305  errno = err;
306  net_close (scb);
307  return -1;
308  }
309  }
310 
311  /* Turn off nonblocking. */
312  ioarg = 0;
313  ioctl (scb->fd, FIONBIO, &ioarg);
314 
315  if (use_udp == 0)
316  {
317  /* Disable Nagle algorithm. Needed in some cases. */
318  tmp = 1;
319  setsockopt (scb->fd, IPPROTO_TCP, TCP_NODELAY,
320  (char *)&tmp, sizeof (tmp));
321  }
322 
323 #ifdef SIGPIPE
324  /* If we don't do this, then GDB simply exits
325  when the remote side dies. */
326  signal (SIGPIPE, SIG_IGN);
327 #endif
328 
329  return 0;
330 }
331 
332 void
333 net_close (struct serial *scb)
334 {
335  if (scb->fd == -1)
336  return;
337 
338  close (scb->fd);
339  scb->fd = -1;
340 }
341 
342 int
343 net_read_prim (struct serial *scb, size_t count)
344 {
345  /* Need to cast to silence -Wpointer-sign on MinGW, as Winsock's
346  'recv' takes 'char *' as second argument, while 'scb->buf' is
347  'unsigned char *'. */
348  return recv (scb->fd, (char *) scb->buf, count, 0);
349 }
350 
351 int
352 net_write_prim (struct serial *scb, const void *buf, size_t count)
353 {
354  /* On Windows, the second parameter to send is a "const char *"; on
355  UNIX systems it is generally "const void *". The cast to "const
356  char *" is OK everywhere, since in C++ any data pointer type can
357  be implicitly converted to "const void *". */
358  return send (scb->fd, (const char *) buf, count, 0);
359 }
360 
361 int
363 {
364  /* Send telnet IAC and BREAK characters. */
365  return (serial_write (scb, "\377\363", 2));
366 }
367 
368 /* Support for "set tcp" and "show tcp" commands. */
369 
370 static void
371 set_tcp_cmd (const char *args, int from_tty)
372 {
374 }
375 
376 static void
377 show_tcp_cmd (const char *args, int from_tty)
378 {
380 }
381 
382 #ifndef USE_WIN32API
383 
384 /* The TCP ops. */
385 
386 static const struct serial_ops tcp_ops =
387 {
388  "tcp",
389  net_open,
390  net_close,
391  NULL,
397  ser_base_raw,
409 };
410 
411 #endif /* USE_WIN32API */
412 
413 void
415 {
416 #ifdef USE_WIN32API
417  /* Do nothing; the TCP serial operations will be initialized in
418  ser-mingw.c. */
419 #else
421 #endif /* USE_WIN32API */
422 
424 TCP protocol specific variables\n\
425 Configure variables specific to remote TCP connections"),
426  &tcp_set_cmdlist, "set tcp ",
427  0 /* allow-unknown */, &setlist);
429 TCP protocol specific variables\n\
430 Configure variables specific to remote TCP connections"),
431  &tcp_show_cmdlist, "show tcp ",
432  0 /* allow-unknown */, &showlist);
433 
434  add_setshow_boolean_cmd ("auto-retry", class_obscure,
435  &tcp_auto_retry, _("\
436 Set auto-retry on socket connect"), _("\
437 Show auto-retry on socket connect"),
438  NULL, NULL, NULL,
440 
441  add_setshow_uinteger_cmd ("connect-timeout", class_obscure,
442  &tcp_retry_limit, _("\
443 Set timeout limit in seconds for socket connection"), _("\
444 Show timeout limit in seconds for socket connection"), _("\
445 If set to \"unlimited\", GDB will keep attempting to establish a\n\
446 connection forever, unless interrupted with Ctrl-c.\n\
447 The default is 15 seconds."),
448  NULL, NULL,
450 }
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t err
Definition: gnu-nat.c:1822
int ser_base_set_tty_state(struct serial *scb, serial_ttystate ttystate)
Definition: ser-base.c:549
int ser_base_flush_output(struct serial *scb)
Definition: ser-base.c:498
static const struct serial_ops tcp_ops
Definition: ser-tcp.c:386
int net_write_prim(struct serial *scb, const void *buf, size_t count)
Definition: ser-tcp.c:352
static int tcp_auto_retry
Definition: ser-tcp.c:74
int serial_write(struct serial *scb, const void *buf, size_t count)
Definition: serial.c:411
int ser_base_setbaudrate(struct serial *scb, int rate)
Definition: ser-base.c:564
#define _(String)
Definition: gdb_locale.h:35
int gdb_socket_cloexec(int domain, int style, int protocol)
Definition: filestuff.c:359
int ser_base_setstopbits(struct serial *scb, int num)
Definition: ser-base.c:570
static unsigned int tcp_retry_limit
Definition: ser-tcp.c:78
struct cmd_list_element * add_prefix_cmd(const char *name, enum command_class theclass, cmd_const_cfunc_ftype *fun, const char *doc, struct cmd_list_element **prefixlist, const char *prefixname, int allow_unknown, struct cmd_list_element **list)
Definition: cli-decode.c:367
void ser_base_raw(struct serial *scb)
Definition: ser-base.c:529
void ser_base_async(struct serial *scb, int async_p)
Definition: ser-base.c:586
struct cmd_list_element * setlist
Definition: cli-cmds.c:111
void add_setshow_uinteger_cmd(const char *name, enum command_class theclass, unsigned int *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_const_sfunc_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition: cli-decode.c:723
const char *const name
Definition: aarch64-tdep.c:76
int net_read_prim(struct serial *scb, size_t count)
Definition: ser-tcp.c:343
unsigned long u_long
Definition: ser-go32.c:130
void fprintf_unfiltered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2018
struct cmd_list_element * showlist
Definition: cli-cmds.c:119
static int startswith(const char *string, const char *pattern)
Definition: common-utils.h:107
Definition: serial.h:224
int gdb_select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
Definition: mingw-hdep.c:51
static struct cmd_list_element * tcp_show_cmdlist
Definition: ser-tcp.c:70
void _initialize_ser_tcp(void)
Definition: ser-tcp.c:414
int ser_base_drain_output(struct serial *scb)
Definition: ser-base.c:523
unsigned char buf[BUFSIZ]
Definition: serial.h:242
static struct cmd_list_element * tcp_set_cmdlist
Definition: ser-tcp.c:69
static int wait_for_connect(struct serial *scb, unsigned int *polls)
Definition: ser-tcp.c:89
void ser_base_print_tty_state(struct serial *scb, serial_ttystate ttystate, struct ui_file *stream)
Definition: ser-base.c:555
void help_list(struct cmd_list_element *list, const char *cmdtype, enum command_class theclass, struct ui_file *stream)
Definition: cli-decode.c:1071
int ser_base_setparity(struct serial *scb, int parity)
Definition: ser-base.c:578
void serial_add_interface(const struct serial_ops *optable)
Definition: serial.c:160
#define gdb_stderr
Definition: utils.h:344
int ser_base_flush_input(struct serial *scb)
Definition: ser-base.c:504
int ser_base_readchar(struct serial *scb, int timeout)
Definition: ser-base.c:468
#define POLL_INTERVAL
Definition: ser-tcp.c:82
void add_setshow_boolean_cmd(const char *name, enum command_class theclass, int *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_const_sfunc_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition: cli-decode.c:569
int fd
Definition: serial.h:230
int ser_base_write(struct serial *scb, const void *buf, size_t count)
Definition: ser-base.c:474
static void show_tcp_cmd(const char *args, int from_tty)
Definition: ser-tcp.c:377
void net_close(struct serial *scb)
Definition: ser-tcp.c:333
int ser_tcp_send_break(struct serial *scb)
Definition: ser-tcp.c:362
serial_ttystate ser_base_copy_tty_state(struct serial *scb, serial_ttystate ttystate)
Definition: ser-base.c:542
void error(const char *fmt,...)
Definition: errors.c:38
int(* deprecated_ui_loop_hook)(int signo)
Definition: top.c:185
serial_ttystate ser_base_get_tty_state(struct serial *scb)
Definition: ser-base.c:535
#define gdb_stdout
Definition: utils.h:340
static void set_tcp_cmd(const char *args, int from_tty)
Definition: ser-tcp.c:371
int net_open(struct serial *scb, const char *name)
Definition: ser-tcp.c:159