1 /* Serial interface for raw TCP connections on Un*x like systems.
3 Copyright (C) 1992-2013 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"
28 #include <sys/types.h>
30 #ifdef HAVE_SYS_FILIO_H
31 #include <sys/filio.h> /* For FIONBIO. */
33 #ifdef HAVE_SYS_IOCTL_H
34 #include <sys/ioctl.h> /* For FIONBIO. */
42 #define ETIMEDOUT WSAETIMEDOUT
44 #define close(fd) closesocket (fd)
45 #define ioctl ioctlsocket
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
50 #include <sys/socket.h>
51 #include <netinet/tcp.h>
55 #include "gdb_string.h"
56 #include "gdb_select.h"
58 #ifndef HAVE_SOCKLEN_T
59 typedef int socklen_t
;
62 void _initialize_ser_tcp (void);
64 /* For "set tcp" and "show tcp". */
66 static struct cmd_list_element
*tcp_set_cmdlist
;
67 static struct cmd_list_element
*tcp_show_cmdlist
;
69 /* Whether to auto-retry refused connections. */
71 static int tcp_auto_retry
= 1;
73 /* Timeout period for connections, in seconds. */
75 static int tcp_retry_limit
= 15;
77 /* How many times per second to poll deprecated_ui_loop_hook. */
79 #define POLL_INTERVAL 5
81 /* Helper function to wait a while. If SCB is non-null, wait on its
82 file descriptor. Otherwise just wait on a timeout, updating *POLLS.
83 Returns -1 on timeout or interrupt, otherwise the value of select. */
86 wait_for_connect (struct serial
*scb
, int *polls
)
91 /* While we wait for the connect to complete,
92 poll the UI so it can update or the user can
94 if (deprecated_ui_loop_hook
&& deprecated_ui_loop_hook (0))
100 /* Check for timeout. */
101 if (*polls
> tcp_retry_limit
* POLL_INTERVAL
)
107 /* Back off to polling once per second after the first POLL_INTERVAL
109 if (*polls
< POLL_INTERVAL
)
112 t
.tv_usec
= 1000000 / POLL_INTERVAL
;
122 fd_set rset
, wset
, eset
;
125 FD_SET (scb
->fd
, &rset
);
129 /* POSIX systems return connection success or failure by signalling
130 wset. Windows systems return success in wset and failure in
133 We must call select here, rather than gdb_select, because
134 the serial structure has not yet been initialized - the
135 MinGW select wrapper will not know that this FD refers
137 n
= select (scb
->fd
+ 1, &rset
, &wset
, &eset
, &t
);
140 /* Use gdb_select here, since we have no file descriptors, and on
141 Windows, plain select doesn't work in that case. */
142 n
= gdb_select (0, NULL
, NULL
, NULL
, &t
);
144 /* If we didn't time out, only count it as one poll. */
145 if (n
> 0 || *polls
< POLL_INTERVAL
)
148 (*polls
) += POLL_INTERVAL
;
153 /* Open a tcp socket. */
156 net_open (struct serial
*scb
, const char *name
)
158 char *port_str
, hostname
[100];
161 struct hostent
*hostent
;
162 struct sockaddr_in sockaddr
;
171 if (strncmp (name
, "udp:", 4) == 0)
176 else if (strncmp (name
, "tcp:", 4) == 0)
179 port_str
= strchr (name
, ':');
182 error (_("net_open: No colon in host name!")); /* Shouldn't ever
185 tmp
= min (port_str
- name
, (int) sizeof hostname
- 1);
186 strncpy (hostname
, name
, tmp
); /* Don't want colon. */
187 hostname
[tmp
] = '\000'; /* Tie off host name. */
188 port
= atoi (port_str
+ 1);
190 /* Default hostname is localhost. */
192 strcpy (hostname
, "localhost");
194 hostent
= gethostbyname (hostname
);
197 fprintf_unfiltered (gdb_stderr
, "%s: unknown host\n", hostname
);
202 sockaddr
.sin_family
= PF_INET
;
203 sockaddr
.sin_port
= htons (port
);
204 memcpy (&sockaddr
.sin_addr
.s_addr
, hostent
->h_addr
,
205 sizeof (struct in_addr
));
210 scb
->fd
= socket (PF_INET
, SOCK_DGRAM
, 0);
212 scb
->fd
= socket (PF_INET
, SOCK_STREAM
, 0);
217 /* Set socket nonblocking. */
219 ioctl (scb
->fd
, FIONBIO
, &ioarg
);
221 /* Use Non-blocking connect. connect() will return 0 if connected
223 n
= connect (scb
->fd
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
));
228 int err
= WSAGetLastError();
233 /* Maybe we're waiting for the remote target to become ready to
234 accept connections. */
237 && err
== WSAECONNREFUSED
239 && err
== ECONNREFUSED
241 && wait_for_connect (NULL
, &polls
) >= 0)
249 /* Under Windows, calling "connect" with a non-blocking socket
250 results in WSAEWOULDBLOCK, not WSAEINPROGRESS. */
251 err
!= WSAEWOULDBLOCK
262 /* Looks like we need to wait for the connect. */
265 n
= wait_for_connect (scb
, &polls
);
275 /* Got something. Is it an error? */
281 /* On Windows, the fourth parameter to getsockopt is a "char *";
282 on UNIX systems it is generally "void *". The cast to "void *"
283 is OK everywhere, since in C "void *" can be implicitly
284 converted to any pointer type. */
285 res
= getsockopt (scb
->fd
, SOL_SOCKET
, SO_ERROR
, (void *) &err
, &len
);
288 /* Maybe the target still isn't ready to accept the connection. */
291 && err
== WSAECONNREFUSED
293 && err
== ECONNREFUSED
295 && wait_for_connect (NULL
, &polls
) >= 0)
307 /* Turn off nonblocking. */
309 ioctl (scb
->fd
, FIONBIO
, &ioarg
);
313 /* Disable Nagle algorithm. Needed in some cases. */
315 setsockopt (scb
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
316 (char *)&tmp
, sizeof (tmp
));
320 /* If we don't do this, then GDB simply exits
321 when the remote side dies. */
322 signal (SIGPIPE
, SIG_IGN
);
329 net_close (struct serial
*scb
)
339 net_read_prim (struct serial
*scb
, size_t count
)
341 return recv (scb
->fd
, scb
->buf
, count
, 0);
345 net_write_prim (struct serial
*scb
, const void *buf
, size_t count
)
347 return send (scb
->fd
, buf
, count
, 0);
351 ser_tcp_send_break (struct serial
*scb
)
353 /* Send telnet IAC and BREAK characters. */
354 return (serial_write (scb
, "\377\363", 2));
357 /* Support for "set tcp" and "show tcp" commands. */
360 set_tcp_cmd (char *args
, int from_tty
)
362 help_list (tcp_set_cmdlist
, "set tcp ", -1, gdb_stdout
);
366 show_tcp_cmd (char *args
, int from_tty
)
368 help_list (tcp_show_cmdlist
, "show tcp ", -1, gdb_stdout
);
373 _initialize_ser_tcp (void)
376 /* Do nothing; the TCP serial operations will be initialized in
379 struct serial_ops
*ops
;
381 ops
= XMALLOC (struct serial_ops
);
382 memset (ops
, 0, sizeof (struct serial_ops
));
385 ops
->open
= net_open
;
386 ops
->close
= net_close
;
387 ops
->readchar
= ser_base_readchar
;
388 ops
->write
= ser_base_write
;
389 ops
->flush_output
= ser_base_flush_output
;
390 ops
->flush_input
= ser_base_flush_input
;
391 ops
->send_break
= ser_tcp_send_break
;
392 ops
->go_raw
= ser_base_raw
;
393 ops
->get_tty_state
= ser_base_get_tty_state
;
394 ops
->copy_tty_state
= ser_base_copy_tty_state
;
395 ops
->set_tty_state
= ser_base_set_tty_state
;
396 ops
->print_tty_state
= ser_base_print_tty_state
;
397 ops
->noflush_set_tty_state
= ser_base_noflush_set_tty_state
;
398 ops
->setbaudrate
= ser_base_setbaudrate
;
399 ops
->setstopbits
= ser_base_setstopbits
;
400 ops
->drain_output
= ser_base_drain_output
;
401 ops
->async
= ser_base_async
;
402 ops
->read_prim
= net_read_prim
;
403 ops
->write_prim
= net_write_prim
;
404 serial_add_interface (ops
);
405 #endif /* USE_WIN32API */
407 add_prefix_cmd ("tcp", class_maintenance
, set_tcp_cmd
, _("\
408 TCP protocol specific variables\n\
409 Configure variables specific to remote TCP connections"),
410 &tcp_set_cmdlist
, "set tcp ",
411 0 /* allow-unknown */, &setlist
);
412 add_prefix_cmd ("tcp", class_maintenance
, show_tcp_cmd
, _("\
413 TCP protocol specific variables\n\
414 Configure variables specific to remote TCP connections"),
415 &tcp_show_cmdlist
, "show tcp ",
416 0 /* allow-unknown */, &showlist
);
418 add_setshow_boolean_cmd ("auto-retry", class_obscure
,
419 &tcp_auto_retry
, _("\
420 Set auto-retry on socket connect"), _("\
421 Show auto-retry on socket connect"),
423 &tcp_set_cmdlist
, &tcp_show_cmdlist
);
425 add_setshow_uinteger_cmd ("connect-timeout", class_obscure
,
426 &tcp_retry_limit
, _("\
427 Set timeout limit for socket connection"), _("\
428 Show timeout limit for socket connection"),
430 &tcp_set_cmdlist
, &tcp_show_cmdlist
);