Share parts of gdb/gdbthread.h with gdbserver
[deliverable/binutils-gdb.git] / gdb / ser-tcp.c
CommitLineData
f9f87d2c
MK
1/* Serial interface for raw TCP connections on Un*x like systems.
2
61baf725 3 Copyright (C) 1992-2017 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"
614c279d 27#include "filestuff.h"
c2c6d25f 28
c906108c 29#include <sys/types.h>
0cf3e697
MH
30
31#ifdef HAVE_SYS_FILIO_H
c378eb4e 32#include <sys/filio.h> /* For FIONBIO. */
0cf3e697
MH
33#endif
34#ifdef HAVE_SYS_IOCTL_H
c378eb4e 35#include <sys/ioctl.h> /* For FIONBIO. */
0cf3e697
MH
36#endif
37
438e1e42 38#include "gdb_sys_time.h"
b4505029
MM
39
40#ifdef USE_WIN32API
41#include <winsock2.h>
8e7ebaf5 42#ifndef ETIMEDOUT
b4505029 43#define ETIMEDOUT WSAETIMEDOUT
8e7ebaf5 44#endif
056d7646 45#define close(fd) closesocket (fd)
b4505029
MM
46#define ioctl ioctlsocket
47#else
9eb1356e 48#include <netinet/in.h>
c906108c
SS
49#include <arpa/inet.h>
50#include <netdb.h>
9eb1356e 51#include <sys/socket.h>
c906108c 52#include <netinet/tcp.h>
b4505029 53#endif
c906108c 54
042be3a9 55#include <signal.h>
84603566 56#include "gdb_select.h"
325fac50 57#include <algorithm>
c906108c 58
f9f87d2c
MK
59#ifndef HAVE_SOCKLEN_T
60typedef int socklen_t;
61#endif
62
c2c6d25f 63void _initialize_ser_tcp (void);
c906108c 64
84603566
SL
65/* For "set tcp" and "show tcp". */
66
67static struct cmd_list_element *tcp_set_cmdlist;
68static struct cmd_list_element *tcp_show_cmdlist;
69
70/* Whether to auto-retry refused connections. */
71
72static int tcp_auto_retry = 1;
73
74/* Timeout period for connections, in seconds. */
75
964b8317 76static unsigned int tcp_retry_limit = 15;
84603566 77
c378eb4e 78/* How many times per second to poll deprecated_ui_loop_hook. */
84603566
SL
79
80#define POLL_INTERVAL 5
81
82/* Helper function to wait a while. If SCB is non-null, wait on its
83 file descriptor. Otherwise just wait on a timeout, updating *POLLS.
84 Returns -1 on timeout or interrupt, otherwise the value of select. */
85
86static int
2c619be2 87wait_for_connect (struct serial *scb, unsigned int *polls)
84603566
SL
88{
89 struct timeval t;
90 int n;
91
92 /* While we wait for the connect to complete,
93 poll the UI so it can update or the user can
94 interrupt. */
95 if (deprecated_ui_loop_hook && deprecated_ui_loop_hook (0))
96 {
97 errno = EINTR;
98 return -1;
99 }
100
101 /* Check for timeout. */
102 if (*polls > tcp_retry_limit * POLL_INTERVAL)
103 {
104 errno = ETIMEDOUT;
105 return -1;
106 }
107
108 /* Back off to polling once per second after the first POLL_INTERVAL
109 polls. */
110 if (*polls < POLL_INTERVAL)
111 {
112 t.tv_sec = 0;
113 t.tv_usec = 1000000 / POLL_INTERVAL;
114 }
115 else
116 {
117 t.tv_sec = 1;
118 t.tv_usec = 0;
119 }
120
121 if (scb)
122 {
123 fd_set rset, wset, eset;
433759f7 124
84603566
SL
125 FD_ZERO (&rset);
126 FD_SET (scb->fd, &rset);
127 wset = rset;
128 eset = rset;
129
130 /* POSIX systems return connection success or failure by signalling
131 wset. Windows systems return success in wset and failure in
132 eset.
133
134 We must call select here, rather than gdb_select, because
135 the serial structure has not yet been initialized - the
136 MinGW select wrapper will not know that this FD refers
137 to a socket. */
138 n = select (scb->fd + 1, &rset, &wset, &eset, &t);
139 }
140 else
141 /* Use gdb_select here, since we have no file descriptors, and on
142 Windows, plain select doesn't work in that case. */
143 n = gdb_select (0, NULL, NULL, NULL, &t);
144
145 /* If we didn't time out, only count it as one poll. */
146 if (n > 0 || *polls < POLL_INTERVAL)
147 (*polls)++;
148 else
149 (*polls) += POLL_INTERVAL;
150
151 return n;
152}
7c7a201a 153
c378eb4e 154/* Open a tcp socket. */
c906108c 155
0ea3f30e 156int
9db8d71f 157net_open (struct serial *scb, const char *name)
c906108c 158{
e6a959d6
PA
159 char hostname[100];
160 const char *port_str;
7c7a201a 161 int n, port, tmp;
9db8d71f 162 int use_udp;
c906108c 163 struct hostent *hostent;
9eb1356e 164 struct sockaddr_in sockaddr;
b4505029
MM
165#ifdef USE_WIN32API
166 u_long ioarg;
167#else
168 int ioarg;
169#endif
2c619be2 170 unsigned int polls = 0;
c906108c 171
9db8d71f 172 use_udp = 0;
61012eef 173 if (startswith (name, "udp:"))
9db8d71f
DJ
174 {
175 use_udp = 1;
176 name = name + 4;
177 }
61012eef 178 else if (startswith (name, "tcp:"))
9db8d71f
DJ
179 name = name + 4;
180
c906108c
SS
181 port_str = strchr (name, ':');
182
183 if (!port_str)
c378eb4e
MS
184 error (_("net_open: No colon in host name!")); /* Shouldn't ever
185 happen. */
c906108c 186
325fac50 187 tmp = std::min (port_str - name, (ptrdiff_t) sizeof hostname - 1);
c378eb4e
MS
188 strncpy (hostname, name, tmp); /* Don't want colon. */
189 hostname[tmp] = '\000'; /* Tie off host name. */
c906108c
SS
190 port = atoi (port_str + 1);
191
c378eb4e 192 /* Default hostname is localhost. */
ad4571f3
CV
193 if (!hostname[0])
194 strcpy (hostname, "localhost");
195
c906108c 196 hostent = gethostbyname (hostname);
c906108c
SS
197 if (!hostent)
198 {
199 fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname);
200 errno = ENOENT;
201 return -1;
202 }
203
9eb1356e
PA
204 sockaddr.sin_family = PF_INET;
205 sockaddr.sin_port = htons (port);
206 memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
84603566
SL
207 sizeof (struct in_addr));
208
209 retry:
210
9db8d71f 211 if (use_udp)
614c279d 212 scb->fd = gdb_socket_cloexec (PF_INET, SOCK_DGRAM, 0);
9db8d71f 213 else
614c279d 214 scb->fd = gdb_socket_cloexec (PF_INET, SOCK_STREAM, 0);
9db8d71f 215
363a6e9f 216 if (scb->fd == -1)
7c7a201a
MH
217 return -1;
218
c378eb4e 219 /* Set socket nonblocking. */
b4505029
MM
220 ioarg = 1;
221 ioctl (scb->fd, FIONBIO, &ioarg);
c906108c 222
3e43a32a 223 /* Use Non-blocking connect. connect() will return 0 if connected
c378eb4e 224 already. */
9eb1356e 225 n = connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr));
c906108c 226
84603566
SL
227 if (n < 0)
228 {
b4505029 229#ifdef USE_WIN32API
84603566 230 int err = WSAGetLastError();
b4505029 231#else
84603566 232 int err = errno;
b4505029 233#endif
84603566
SL
234
235 /* Maybe we're waiting for the remote target to become ready to
236 accept connections. */
237 if (tcp_auto_retry
b4505029 238#ifdef USE_WIN32API
84603566
SL
239 && err == WSAECONNREFUSED
240#else
241 && err == ECONNREFUSED
b4505029 242#endif
84603566
SL
243 && wait_for_connect (NULL, &polls) >= 0)
244 {
245 close (scb->fd);
246 goto retry;
247 }
c906108c 248
84603566
SL
249 if (
250#ifdef USE_WIN32API
251 /* Under Windows, calling "connect" with a non-blocking socket
252 results in WSAEWOULDBLOCK, not WSAEINPROGRESS. */
253 err != WSAEWOULDBLOCK
254#else
255 err != EINPROGRESS
256#endif
257 )
258 {
259 errno = err;
260 net_close (scb);
261 return -1;
262 }
7c7a201a 263
c378eb4e 264 /* Looks like we need to wait for the connect. */
7c7a201a
MH
265 do
266 {
84603566 267 n = wait_for_connect (scb, &polls);
7c7a201a 268 }
84603566
SL
269 while (n == 0);
270 if (n < 0)
7c7a201a 271 {
9db8d71f 272 net_close (scb);
7c7a201a
MH
273 return -1;
274 }
275 }
c906108c 276
c378eb4e 277 /* Got something. Is it an error? */
7c7a201a 278 {
47b667de
AC
279 int res, err;
280 socklen_t len;
433759f7 281
0ea3f30e 282 len = sizeof (err);
b4505029 283 /* On Windows, the fourth parameter to getsockopt is a "char *";
69e976f8
PA
284 on UNIX systems it is generally "void *". The cast to "char *"
285 is OK everywhere, since in C++ any data pointer type can be
286 implicitly converted to "void *". */
287 res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, (char *) &err, &len);
7c7a201a
MH
288 if (res < 0 || err)
289 {
84603566
SL
290 /* Maybe the target still isn't ready to accept the connection. */
291 if (tcp_auto_retry
292#ifdef USE_WIN32API
293 && err == WSAECONNREFUSED
294#else
295 && err == ECONNREFUSED
296#endif
297 && wait_for_connect (NULL, &polls) >= 0)
298 {
299 close (scb->fd);
300 goto retry;
301 }
7c7a201a
MH
302 if (err)
303 errno = err;
9db8d71f 304 net_close (scb);
7c7a201a
MH
305 return -1;
306 }
307 }
9db8d71f 308
c378eb4e 309 /* Turn off nonblocking. */
b4505029
MM
310 ioarg = 0;
311 ioctl (scb->fd, FIONBIO, &ioarg);
7c7a201a 312
9db8d71f
DJ
313 if (use_udp == 0)
314 {
c378eb4e 315 /* Disable Nagle algorithm. Needed in some cases. */
9db8d71f
DJ
316 tmp = 1;
317 setsockopt (scb->fd, IPPROTO_TCP, TCP_NODELAY,
318 (char *)&tmp, sizeof (tmp));
319 }
320
6d318c73 321#ifdef SIGPIPE
7c7a201a
MH
322 /* If we don't do this, then GDB simply exits
323 when the remote side dies. */
324 signal (SIGPIPE, SIG_IGN);
6d318c73 325#endif
c906108c
SS
326
327 return 0;
328}
329
0ea3f30e 330void
9db8d71f 331net_close (struct serial *scb)
c906108c 332{
363a6e9f 333 if (scb->fd == -1)
c906108c
SS
334 return;
335
c5aa993b 336 close (scb->fd);
c906108c
SS
337 scb->fd = -1;
338}
339
0ea3f30e 340int
b4505029
MM
341net_read_prim (struct serial *scb, size_t count)
342{
c49e7f76
PA
343 /* Need to cast to silence -Wpointer-sign on MinGW, as Winsock's
344 'recv' takes 'char *' as second argument, while 'scb->buf' is
345 'unsigned char *'. */
69e976f8 346 return recv (scb->fd, (char *) scb->buf, count, 0);
b4505029
MM
347}
348
0ea3f30e 349int
b4505029
MM
350net_write_prim (struct serial *scb, const void *buf, size_t count)
351{
69e976f8
PA
352 /* On Windows, the second parameter to send is a "const char *"; on
353 UNIX systems it is generally "const void *". The cast to "const
354 char *" is OK everywhere, since in C++ any data pointer type can
355 be implicitly converted to "const void *". */
356 return send (scb->fd, (const char *) buf, count, 0);
b4505029
MM
357}
358
8775bb90
MS
359int
360ser_tcp_send_break (struct serial *scb)
361{
c378eb4e 362 /* Send telnet IAC and BREAK characters. */
8775bb90
MS
363 return (serial_write (scb, "\377\363", 2));
364}
365
84603566
SL
366/* Support for "set tcp" and "show tcp" commands. */
367
368static void
369set_tcp_cmd (char *args, int from_tty)
370{
635c7e8a 371 help_list (tcp_set_cmdlist, "set tcp ", all_commands, gdb_stdout);
84603566
SL
372}
373
374static void
375show_tcp_cmd (char *args, int from_tty)
376{
635c7e8a 377 help_list (tcp_show_cmdlist, "show tcp ", all_commands, gdb_stdout);
84603566
SL
378}
379
12e8c7d7
TT
380#ifndef USE_WIN32API
381
382/* The TCP ops. */
383
384static const struct serial_ops tcp_ops =
385{
386 "tcp",
387 net_open,
388 net_close,
389 NULL,
390 ser_base_readchar,
391 ser_base_write,
392 ser_base_flush_output,
393 ser_base_flush_input,
394 ser_tcp_send_break,
395 ser_base_raw,
396 ser_base_get_tty_state,
397 ser_base_copy_tty_state,
398 ser_base_set_tty_state,
399 ser_base_print_tty_state,
400 ser_base_noflush_set_tty_state,
401 ser_base_setbaudrate,
402 ser_base_setstopbits,
236af5e3 403 ser_base_setparity,
12e8c7d7
TT
404 ser_base_drain_output,
405 ser_base_async,
406 net_read_prim,
407 net_write_prim
408};
409
410#endif /* USE_WIN32API */
84603566 411
c906108c 412void
c2c6d25f 413_initialize_ser_tcp (void)
c906108c 414{
b4505029 415#ifdef USE_WIN32API
0ea3f30e
DJ
416 /* Do nothing; the TCP serial operations will be initialized in
417 ser-mingw.c. */
0ea3f30e 418#else
12e8c7d7 419 serial_add_interface (&tcp_ops);
0ea3f30e 420#endif /* USE_WIN32API */
84603566
SL
421
422 add_prefix_cmd ("tcp", class_maintenance, set_tcp_cmd, _("\
423TCP protocol specific variables\n\
424Configure variables specific to remote TCP connections"),
425 &tcp_set_cmdlist, "set tcp ",
426 0 /* allow-unknown */, &setlist);
427 add_prefix_cmd ("tcp", class_maintenance, show_tcp_cmd, _("\
428TCP protocol specific variables\n\
429Configure variables specific to remote TCP connections"),
430 &tcp_show_cmdlist, "show tcp ",
431 0 /* allow-unknown */, &showlist);
432
433 add_setshow_boolean_cmd ("auto-retry", class_obscure,
434 &tcp_auto_retry, _("\
435Set auto-retry on socket connect"), _("\
436Show auto-retry on socket connect"),
437 NULL, NULL, NULL,
438 &tcp_set_cmdlist, &tcp_show_cmdlist);
439
440 add_setshow_uinteger_cmd ("connect-timeout", class_obscure,
441 &tcp_retry_limit, _("\
f81d1120
PA
442Set timeout limit in seconds for socket connection"), _("\
443Show timeout limit in seconds for socket connection"), _("\
444If set to \"unlimited\", GDB will keep attempting to establish a\n\
445connection forever, unless interrupted with Ctrl-c.\n\
446The default is 15 seconds."),
447 NULL, NULL,
448 &tcp_set_cmdlist, &tcp_show_cmdlist);
c906108c 449}
This page took 1.902209 seconds and 4 git commands to generate.