1 /* Serial interface for raw TCP connections on Un*x like systems.
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2001, 2005, 2006,
4 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "cli/cli-decode.h"
27 #include "cli/cli-setshow.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. */
42 #define ETIMEDOUT WSAETIMEDOUT
43 #define close(fd) closesocket (fd)
44 #define ioctl ioctlsocket
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
49 #include <sys/socket.h>
50 #include <netinet/tcp.h>
54 #include "gdb_string.h"
55 #include "gdb_select.h"
57 #ifndef HAVE_SOCKLEN_T
58 typedef int socklen_t
;
61 void _initialize_ser_tcp (void);
63 /* For "set tcp" and "show tcp". */
65 static struct cmd_list_element
*tcp_set_cmdlist
;
66 static struct cmd_list_element
*tcp_show_cmdlist
;
68 /* Whether to auto-retry refused connections. */
70 static int tcp_auto_retry
= 1;
72 /* Timeout period for connections, in seconds. */
74 static int tcp_retry_limit
= 15;
76 /* how many times per second to poll deprecated_ui_loop_hook */
78 #define POLL_INTERVAL 5
80 /* Helper function to wait a while. If SCB is non-null, wait on its
81 file descriptor. Otherwise just wait on a timeout, updating *POLLS.
82 Returns -1 on timeout or interrupt, otherwise the value of select. */
85 wait_for_connect (struct serial
*scb
, int *polls
)
90 /* While we wait for the connect to complete,
91 poll the UI so it can update or the user can
93 if (deprecated_ui_loop_hook
&& deprecated_ui_loop_hook (0))
99 /* Check for timeout. */
100 if (*polls
> tcp_retry_limit
* POLL_INTERVAL
)
106 /* Back off to polling once per second after the first POLL_INTERVAL
108 if (*polls
< POLL_INTERVAL
)
111 t
.tv_usec
= 1000000 / POLL_INTERVAL
;
121 fd_set rset
, wset
, eset
;
124 FD_SET (scb
->fd
, &rset
);
128 /* POSIX systems return connection success or failure by signalling
129 wset. Windows systems return success in wset and failure in
132 We must call select here, rather than gdb_select, because
133 the serial structure has not yet been initialized - the
134 MinGW select wrapper will not know that this FD refers
136 n
= select (scb
->fd
+ 1, &rset
, &wset
, &eset
, &t
);
139 /* Use gdb_select here, since we have no file descriptors, and on
140 Windows, plain select doesn't work in that case. */
141 n
= gdb_select (0, NULL
, NULL
, NULL
, &t
);
143 /* If we didn't time out, only count it as one poll. */
144 if (n
> 0 || *polls
< POLL_INTERVAL
)
147 (*polls
) += POLL_INTERVAL
;
152 /* Open a tcp socket */
155 net_open (struct serial
*scb
, const char *name
)
157 char *port_str
, hostname
[100];
160 struct hostent
*hostent
;
161 struct sockaddr_in sockaddr
;
170 if (strncmp (name
, "udp:", 4) == 0)
175 else if (strncmp (name
, "tcp:", 4) == 0)
178 port_str
= strchr (name
, ':');
181 error (_("net_open: No colon in host name!")); /* Shouldn't ever happen */
183 tmp
= min (port_str
- name
, (int) sizeof hostname
- 1);
184 strncpy (hostname
, name
, tmp
); /* Don't want colon */
185 hostname
[tmp
] = '\000'; /* Tie off host name */
186 port
= atoi (port_str
+ 1);
188 /* default hostname is localhost */
190 strcpy (hostname
, "localhost");
192 hostent
= gethostbyname (hostname
);
195 fprintf_unfiltered (gdb_stderr
, "%s: unknown host\n", hostname
);
200 sockaddr
.sin_family
= PF_INET
;
201 sockaddr
.sin_port
= htons (port
);
202 memcpy (&sockaddr
.sin_addr
.s_addr
, hostent
->h_addr
,
203 sizeof (struct in_addr
));
208 scb
->fd
= socket (PF_INET
, SOCK_DGRAM
, 0);
210 scb
->fd
= socket (PF_INET
, SOCK_STREAM
, 0);
215 /* set socket nonblocking */
217 ioctl (scb
->fd
, FIONBIO
, &ioarg
);
219 /* Use Non-blocking connect. connect() will return 0 if connected
221 n
= connect (scb
->fd
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
));
226 int err
= WSAGetLastError();
231 /* Maybe we're waiting for the remote target to become ready to
232 accept connections. */
235 && err
== WSAECONNREFUSED
237 && err
== ECONNREFUSED
239 && wait_for_connect (NULL
, &polls
) >= 0)
247 /* Under Windows, calling "connect" with a non-blocking socket
248 results in WSAEWOULDBLOCK, not WSAEINPROGRESS. */
249 err
!= WSAEWOULDBLOCK
260 /* looks like we need to wait for the connect */
263 n
= wait_for_connect (scb
, &polls
);
273 /* Got something. Is it an error? */
279 /* On Windows, the fourth parameter to getsockopt is a "char *";
280 on UNIX systems it is generally "void *". The cast to "void *"
281 is OK everywhere, since in C "void *" can be implicitly
282 converted to any pointer type. */
283 res
= getsockopt (scb
->fd
, SOL_SOCKET
, SO_ERROR
, (void *) &err
, &len
);
286 /* Maybe the target still isn't ready to accept the connection. */
289 && err
== WSAECONNREFUSED
291 && err
== ECONNREFUSED
293 && wait_for_connect (NULL
, &polls
) >= 0)
305 /* turn off nonblocking */
307 ioctl (scb
->fd
, FIONBIO
, &ioarg
);
311 /* Disable Nagle algorithm. Needed in some cases. */
313 setsockopt (scb
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
314 (char *)&tmp
, sizeof (tmp
));
318 /* If we don't do this, then GDB simply exits
319 when the remote side dies. */
320 signal (SIGPIPE
, SIG_IGN
);
327 net_close (struct serial
*scb
)
337 net_read_prim (struct serial
*scb
, size_t count
)
339 return recv (scb
->fd
, scb
->buf
, count
, 0);
343 net_write_prim (struct serial
*scb
, const void *buf
, size_t count
)
345 return send (scb
->fd
, buf
, count
, 0);
349 ser_tcp_send_break (struct serial
*scb
)
351 /* Send telnet IAC and BREAK characters. */
352 return (serial_write (scb
, "\377\363", 2));
355 /* Support for "set tcp" and "show tcp" commands. */
358 set_tcp_cmd (char *args
, int from_tty
)
360 help_list (tcp_set_cmdlist
, "set tcp ", -1, gdb_stdout
);
364 show_tcp_cmd (char *args
, int from_tty
)
366 help_list (tcp_show_cmdlist
, "show tcp ", -1, gdb_stdout
);
371 _initialize_ser_tcp (void)
374 /* Do nothing; the TCP serial operations will be initialized in
377 struct serial_ops
*ops
;
379 ops
= XMALLOC (struct serial_ops
);
380 memset (ops
, 0, sizeof (struct serial_ops
));
383 ops
->open
= net_open
;
384 ops
->close
= net_close
;
385 ops
->readchar
= ser_base_readchar
;
386 ops
->write
= ser_base_write
;
387 ops
->flush_output
= ser_base_flush_output
;
388 ops
->flush_input
= ser_base_flush_input
;
389 ops
->send_break
= ser_tcp_send_break
;
390 ops
->go_raw
= ser_base_raw
;
391 ops
->get_tty_state
= ser_base_get_tty_state
;
392 ops
->set_tty_state
= ser_base_set_tty_state
;
393 ops
->print_tty_state
= ser_base_print_tty_state
;
394 ops
->noflush_set_tty_state
= ser_base_noflush_set_tty_state
;
395 ops
->setbaudrate
= ser_base_setbaudrate
;
396 ops
->setstopbits
= ser_base_setstopbits
;
397 ops
->drain_output
= ser_base_drain_output
;
398 ops
->async
= ser_base_async
;
399 ops
->read_prim
= net_read_prim
;
400 ops
->write_prim
= net_write_prim
;
401 serial_add_interface (ops
);
402 #endif /* USE_WIN32API */
404 add_prefix_cmd ("tcp", class_maintenance
, set_tcp_cmd
, _("\
405 TCP protocol specific variables\n\
406 Configure variables specific to remote TCP connections"),
407 &tcp_set_cmdlist
, "set tcp ",
408 0 /* allow-unknown */, &setlist
);
409 add_prefix_cmd ("tcp", class_maintenance
, show_tcp_cmd
, _("\
410 TCP protocol specific variables\n\
411 Configure variables specific to remote TCP connections"),
412 &tcp_show_cmdlist
, "show tcp ",
413 0 /* allow-unknown */, &showlist
);
415 add_setshow_boolean_cmd ("auto-retry", class_obscure
,
416 &tcp_auto_retry
, _("\
417 Set auto-retry on socket connect"), _("\
418 Show auto-retry on socket connect"),
420 &tcp_set_cmdlist
, &tcp_show_cmdlist
);
422 add_setshow_uinteger_cmd ("connect-timeout", class_obscure
,
423 &tcp_retry_limit
, _("\
424 Set timeout limit for socket connection"), _("\
425 Show timeout limit for socket connection"),
427 &tcp_set_cmdlist
, &tcp_show_cmdlist
);