1 /* Serial interface for raw TCP connections on Un*x like systems.
3 Copyright (C) 1992-2014 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. */
43 #define ETIMEDOUT WSAETIMEDOUT
45 #define close(fd) closesocket (fd)
46 #define ioctl ioctlsocket
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
51 #include <sys/socket.h>
52 #include <netinet/tcp.h>
57 #include "gdb_select.h"
59 #ifndef HAVE_SOCKLEN_T
60 typedef int socklen_t
;
63 void _initialize_ser_tcp (void);
65 /* For "set tcp" and "show tcp". */
67 static struct cmd_list_element
*tcp_set_cmdlist
;
68 static struct cmd_list_element
*tcp_show_cmdlist
;
70 /* Whether to auto-retry refused connections. */
72 static int tcp_auto_retry
= 1;
74 /* Timeout period for connections, in seconds. */
76 static unsigned int tcp_retry_limit
= 15;
78 /* How many times per second to poll deprecated_ui_loop_hook. */
80 #define POLL_INTERVAL 5
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. */
87 wait_for_connect (struct serial
*scb
, unsigned int *polls
)
92 /* While we wait for the connect to complete,
93 poll the UI so it can update or the user can
95 if (deprecated_ui_loop_hook
&& deprecated_ui_loop_hook (0))
101 /* Check for timeout. */
102 if (*polls
> tcp_retry_limit
* POLL_INTERVAL
)
108 /* Back off to polling once per second after the first POLL_INTERVAL
110 if (*polls
< POLL_INTERVAL
)
113 t
.tv_usec
= 1000000 / POLL_INTERVAL
;
123 fd_set rset
, wset
, eset
;
126 FD_SET (scb
->fd
, &rset
);
130 /* POSIX systems return connection success or failure by signalling
131 wset. Windows systems return success in wset and failure in
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
138 n
= select (scb
->fd
+ 1, &rset
, &wset
, &eset
, &t
);
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
);
145 /* If we didn't time out, only count it as one poll. */
146 if (n
> 0 || *polls
< POLL_INTERVAL
)
149 (*polls
) += POLL_INTERVAL
;
154 /* Open a tcp socket. */
157 net_open (struct serial
*scb
, const char *name
)
159 char *port_str
, hostname
[100];
162 struct hostent
*hostent
;
163 struct sockaddr_in sockaddr
;
169 unsigned int polls
= 0;
172 if (strncmp (name
, "udp:", 4) == 0)
177 else if (strncmp (name
, "tcp:", 4) == 0)
180 port_str
= strchr (name
, ':');
183 error (_("net_open: No colon in host name!")); /* Shouldn't ever
186 tmp
= min (port_str
- name
, (int) sizeof hostname
- 1);
187 strncpy (hostname
, name
, tmp
); /* Don't want colon. */
188 hostname
[tmp
] = '\000'; /* Tie off host name. */
189 port
= atoi (port_str
+ 1);
191 /* Default hostname is localhost. */
193 strcpy (hostname
, "localhost");
195 hostent
= gethostbyname (hostname
);
198 fprintf_unfiltered (gdb_stderr
, "%s: unknown host\n", hostname
);
203 sockaddr
.sin_family
= PF_INET
;
204 sockaddr
.sin_port
= htons (port
);
205 memcpy (&sockaddr
.sin_addr
.s_addr
, hostent
->h_addr
,
206 sizeof (struct in_addr
));
211 scb
->fd
= gdb_socket_cloexec (PF_INET
, SOCK_DGRAM
, 0);
213 scb
->fd
= gdb_socket_cloexec (PF_INET
, SOCK_STREAM
, 0);
218 /* Set socket nonblocking. */
220 ioctl (scb
->fd
, FIONBIO
, &ioarg
);
222 /* Use Non-blocking connect. connect() will return 0 if connected
224 n
= connect (scb
->fd
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
));
229 int err
= WSAGetLastError();
234 /* Maybe we're waiting for the remote target to become ready to
235 accept connections. */
238 && err
== WSAECONNREFUSED
240 && err
== ECONNREFUSED
242 && wait_for_connect (NULL
, &polls
) >= 0)
250 /* Under Windows, calling "connect" with a non-blocking socket
251 results in WSAEWOULDBLOCK, not WSAEINPROGRESS. */
252 err
!= WSAEWOULDBLOCK
263 /* Looks like we need to wait for the connect. */
266 n
= wait_for_connect (scb
, &polls
);
276 /* Got something. Is it an error? */
282 /* On Windows, the fourth parameter to getsockopt is a "char *";
283 on UNIX systems it is generally "void *". The cast to "void *"
284 is OK everywhere, since in C "void *" can be implicitly
285 converted to any pointer type. */
286 res
= getsockopt (scb
->fd
, SOL_SOCKET
, SO_ERROR
, (void *) &err
, &len
);
289 /* Maybe the target still isn't ready to accept the connection. */
292 && err
== WSAECONNREFUSED
294 && err
== ECONNREFUSED
296 && wait_for_connect (NULL
, &polls
) >= 0)
308 /* Turn off nonblocking. */
310 ioctl (scb
->fd
, FIONBIO
, &ioarg
);
314 /* Disable Nagle algorithm. Needed in some cases. */
316 setsockopt (scb
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
317 (char *)&tmp
, sizeof (tmp
));
321 /* If we don't do this, then GDB simply exits
322 when the remote side dies. */
323 signal (SIGPIPE
, SIG_IGN
);
330 net_close (struct serial
*scb
)
340 net_read_prim (struct serial
*scb
, size_t count
)
342 /* Need to cast to silence -Wpointer-sign on MinGW, as Winsock's
343 'recv' takes 'char *' as second argument, while 'scb->buf' is
344 'unsigned char *'. */
345 return recv (scb
->fd
, (void *) scb
->buf
, count
, 0);
349 net_write_prim (struct serial
*scb
, const void *buf
, size_t count
)
351 return send (scb
->fd
, buf
, count
, 0);
355 ser_tcp_send_break (struct serial
*scb
)
357 /* Send telnet IAC and BREAK characters. */
358 return (serial_write (scb
, "\377\363", 2));
361 /* Support for "set tcp" and "show tcp" commands. */
364 set_tcp_cmd (char *args
, int from_tty
)
366 help_list (tcp_set_cmdlist
, "set tcp ", -1, gdb_stdout
);
370 show_tcp_cmd (char *args
, int from_tty
)
372 help_list (tcp_show_cmdlist
, "show tcp ", -1, gdb_stdout
);
379 static const struct serial_ops tcp_ops
=
387 ser_base_flush_output
,
388 ser_base_flush_input
,
391 ser_base_get_tty_state
,
392 ser_base_copy_tty_state
,
393 ser_base_set_tty_state
,
394 ser_base_print_tty_state
,
395 ser_base_noflush_set_tty_state
,
396 ser_base_setbaudrate
,
397 ser_base_setstopbits
,
398 ser_base_drain_output
,
404 #endif /* USE_WIN32API */
407 _initialize_ser_tcp (void)
410 /* Do nothing; the TCP serial operations will be initialized in
413 serial_add_interface (&tcp_ops
);
414 #endif /* USE_WIN32API */
416 add_prefix_cmd ("tcp", class_maintenance
, set_tcp_cmd
, _("\
417 TCP protocol specific variables\n\
418 Configure variables specific to remote TCP connections"),
419 &tcp_set_cmdlist
, "set tcp ",
420 0 /* allow-unknown */, &setlist
);
421 add_prefix_cmd ("tcp", class_maintenance
, show_tcp_cmd
, _("\
422 TCP protocol specific variables\n\
423 Configure variables specific to remote TCP connections"),
424 &tcp_show_cmdlist
, "show tcp ",
425 0 /* allow-unknown */, &showlist
);
427 add_setshow_boolean_cmd ("auto-retry", class_obscure
,
428 &tcp_auto_retry
, _("\
429 Set auto-retry on socket connect"), _("\
430 Show auto-retry on socket connect"),
432 &tcp_set_cmdlist
, &tcp_show_cmdlist
);
434 add_setshow_uinteger_cmd ("connect-timeout", class_obscure
,
435 &tcp_retry_limit
, _("\
436 Set timeout limit in seconds for socket connection"), _("\
437 Show timeout limit in seconds for socket connection"), _("\
438 If set to \"unlimited\", GDB will keep attempting to establish a\n\
439 connection forever, unless interrupted with Ctrl-c.\n\
440 The default is 15 seconds."),
442 &tcp_set_cmdlist
, &tcp_show_cmdlist
);