1 /* Serial interface for raw TCP connections on Un*x like systems.
3 Copyright (C) 1992-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
25 #include "cli/cli-decode.h"
26 #include "cli/cli-setshow.h"
27 #include "filestuff.h"
29 #include <sys/types.h>
31 #ifdef HAVE_SYS_FILIO_H
32 #include <sys/filio.h> /* For FIONBIO. */
34 #ifdef HAVE_SYS_IOCTL_H
35 #include <sys/ioctl.h> /* For FIONBIO. */
38 #include "gdb_sys_time.h"
43 #define ETIMEDOUT WSAETIMEDOUT
45 /* Gnulib defines close too, but gnulib's replacement
46 doesn't call closesocket unless we import the
49 #define close(fd) closesocket (fd)
50 #define ioctl ioctlsocket
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
55 #include <sys/socket.h>
56 #include <netinet/tcp.h>
60 #include "gdb_select.h"
63 #ifndef HAVE_SOCKLEN_T
64 typedef int socklen_t
;
67 /* For "set tcp" and "show tcp". */
69 static struct cmd_list_element
*tcp_set_cmdlist
;
70 static struct cmd_list_element
*tcp_show_cmdlist
;
72 /* Whether to auto-retry refused connections. */
74 static int tcp_auto_retry
= 1;
76 /* Timeout period for connections, in seconds. */
78 static unsigned int tcp_retry_limit
= 15;
80 /* How many times per second to poll deprecated_ui_loop_hook. */
82 #define POLL_INTERVAL 5
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. */
89 wait_for_connect (struct serial
*scb
, unsigned int *polls
)
94 /* While we wait for the connect to complete,
95 poll the UI so it can update or the user can
97 if (deprecated_ui_loop_hook
&& deprecated_ui_loop_hook (0))
103 /* Check for timeout. */
104 if (*polls
> tcp_retry_limit
* POLL_INTERVAL
)
110 /* Back off to polling once per second after the first POLL_INTERVAL
112 if (*polls
< POLL_INTERVAL
)
115 t
.tv_usec
= 1000000 / POLL_INTERVAL
;
125 fd_set rset
, wset
, eset
;
128 FD_SET (scb
->fd
, &rset
);
132 /* POSIX systems return connection success or failure by signalling
133 wset. Windows systems return success in wset and failure in
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
140 n
= select (scb
->fd
+ 1, &rset
, &wset
, &eset
, &t
);
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
);
147 /* If we didn't time out, only count it as one poll. */
148 if (n
> 0 || *polls
< POLL_INTERVAL
)
151 (*polls
) += POLL_INTERVAL
;
156 /* Open a tcp socket. */
159 net_open (struct serial
*scb
, const char *name
)
162 const char *port_str
;
165 struct hostent
*hostent
;
166 struct sockaddr_in sockaddr
;
172 unsigned int polls
= 0;
175 if (startswith (name
, "udp:"))
180 else if (startswith (name
, "tcp:"))
183 port_str
= strchr (name
, ':');
186 error (_("net_open: No colon in host name!")); /* Shouldn't ever
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);
194 /* Default hostname is localhost. */
196 strcpy (hostname
, "localhost");
198 hostent
= gethostbyname (hostname
);
201 fprintf_unfiltered (gdb_stderr
, "%s: unknown host\n", hostname
);
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
));
214 scb
->fd
= gdb_socket_cloexec (PF_INET
, SOCK_DGRAM
, 0);
216 scb
->fd
= gdb_socket_cloexec (PF_INET
, SOCK_STREAM
, 0);
221 /* Set socket nonblocking. */
223 ioctl (scb
->fd
, FIONBIO
, &ioarg
);
225 /* Use Non-blocking connect. connect() will return 0 if connected
227 n
= connect (scb
->fd
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
));
232 int err
= WSAGetLastError();
237 /* Maybe we're waiting for the remote target to become ready to
238 accept connections. */
241 && err
== WSAECONNREFUSED
243 && err
== ECONNREFUSED
245 && wait_for_connect (NULL
, &polls
) >= 0)
253 /* Under Windows, calling "connect" with a non-blocking socket
254 results in WSAEWOULDBLOCK, not WSAEINPROGRESS. */
255 err
!= WSAEWOULDBLOCK
266 /* Looks like we need to wait for the connect. */
269 n
= wait_for_connect (scb
, &polls
);
279 /* Got something. Is it an error? */
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
);
292 /* Maybe the target still isn't ready to accept the connection. */
295 && err
== WSAECONNREFUSED
297 && err
== ECONNREFUSED
299 && wait_for_connect (NULL
, &polls
) >= 0)
311 /* Turn off nonblocking. */
313 ioctl (scb
->fd
, FIONBIO
, &ioarg
);
317 /* Disable Nagle algorithm. Needed in some cases. */
319 setsockopt (scb
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
320 (char *)&tmp
, sizeof (tmp
));
324 /* If we don't do this, then GDB simply exits
325 when the remote side dies. */
326 signal (SIGPIPE
, SIG_IGN
);
333 net_close (struct serial
*scb
)
343 net_read_prim (struct serial
*scb
, size_t count
)
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);
352 net_write_prim (struct serial
*scb
, const void *buf
, size_t count
)
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);
362 ser_tcp_send_break (struct serial
*scb
)
364 /* Send telnet IAC and BREAK characters. */
365 return (serial_write (scb
, "\377\363", 2));
368 /* Support for "set tcp" and "show tcp" commands. */
371 set_tcp_cmd (const char *args
, int from_tty
)
373 help_list (tcp_set_cmdlist
, "set tcp ", all_commands
, gdb_stdout
);
377 show_tcp_cmd (const char *args
, int from_tty
)
379 help_list (tcp_show_cmdlist
, "show tcp ", all_commands
, gdb_stdout
);
386 static const struct serial_ops tcp_ops
=
394 ser_base_flush_output
,
395 ser_base_flush_input
,
398 ser_base_get_tty_state
,
399 ser_base_copy_tty_state
,
400 ser_base_set_tty_state
,
401 ser_base_print_tty_state
,
402 ser_base_noflush_set_tty_state
,
403 ser_base_setbaudrate
,
404 ser_base_setstopbits
,
406 ser_base_drain_output
,
412 #endif /* USE_WIN32API */
415 _initialize_ser_tcp (void)
418 /* Do nothing; the TCP serial operations will be initialized in
421 serial_add_interface (&tcp_ops
);
422 #endif /* USE_WIN32API */
424 add_prefix_cmd ("tcp", class_maintenance
, set_tcp_cmd
, _("\
425 TCP protocol specific variables\n\
426 Configure variables specific to remote TCP connections"),
427 &tcp_set_cmdlist
, "set tcp ",
428 0 /* allow-unknown */, &setlist
);
429 add_prefix_cmd ("tcp", class_maintenance
, show_tcp_cmd
, _("\
430 TCP protocol specific variables\n\
431 Configure variables specific to remote TCP connections"),
432 &tcp_show_cmdlist
, "show tcp ",
433 0 /* allow-unknown */, &showlist
);
435 add_setshow_boolean_cmd ("auto-retry", class_obscure
,
436 &tcp_auto_retry
, _("\
437 Set auto-retry on socket connect"), _("\
438 Show auto-retry on socket connect"),
440 &tcp_set_cmdlist
, &tcp_show_cmdlist
);
442 add_setshow_uinteger_cmd ("connect-timeout", class_obscure
,
443 &tcp_retry_limit
, _("\
444 Set timeout limit in seconds for socket connection"), _("\
445 Show timeout limit in seconds for socket connection"), _("\
446 If set to \"unlimited\", GDB will keep attempting to establish a\n\
447 connection forever, unless interrupted with Ctrl-c.\n\
448 The default is 15 seconds."),
450 &tcp_set_cmdlist
, &tcp_show_cmdlist
);