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