import strstr and strerror modules
[deliverable/binutils-gdb.git] / gdb / ser-tcp.c
CommitLineData
f9f87d2c
MK
1/* Serial interface for raw TCP connections on Un*x like systems.
2
28e7fd62 3 Copyright (C) 1992-2013 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
c906108c 38#include <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
c906108c
SS
48#include <netinet/in.h>
49#include <arpa/inet.h>
50#include <netdb.h>
51#include <sys/socket.h>
c906108c 52#include <netinet/tcp.h>
b4505029 53#endif
c906108c 54
042be3a9 55#include <signal.h>
c906108c 56#include "gdb_string.h"
84603566 57#include "gdb_select.h"
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{
7c7a201a
MH
159 char *port_str, hostname[100];
160 int n, port, tmp;
9db8d71f 161 int use_udp;
c906108c
SS
162 struct hostent *hostent;
163 struct sockaddr_in sockaddr;
b4505029
MM
164#ifdef USE_WIN32API
165 u_long ioarg;
166#else
167 int ioarg;
168#endif
2c619be2 169 unsigned int polls = 0;
c906108c 170
9db8d71f
DJ
171 use_udp = 0;
172 if (strncmp (name, "udp:", 4) == 0)
173 {
174 use_udp = 1;
175 name = name + 4;
176 }
177 else if (strncmp (name, "tcp:", 4) == 0)
178 name = name + 4;
179
c906108c
SS
180 port_str = strchr (name, ':');
181
182 if (!port_str)
c378eb4e
MS
183 error (_("net_open: No colon in host name!")); /* Shouldn't ever
184 happen. */
c906108c
SS
185
186 tmp = min (port_str - name, (int) sizeof hostname - 1);
c378eb4e
MS
187 strncpy (hostname, name, tmp); /* Don't want colon. */
188 hostname[tmp] = '\000'; /* Tie off host name. */
c906108c
SS
189 port = atoi (port_str + 1);
190
c378eb4e 191 /* Default hostname is localhost. */
ad4571f3
CV
192 if (!hostname[0])
193 strcpy (hostname, "localhost");
194
c906108c 195 hostent = gethostbyname (hostname);
c906108c
SS
196 if (!hostent)
197 {
198 fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname);
199 errno = ENOENT;
200 return -1;
201 }
202
84603566
SL
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));
207
208 retry:
209
9db8d71f 210 if (use_udp)
614c279d 211 scb->fd = gdb_socket_cloexec (PF_INET, SOCK_DGRAM, 0);
9db8d71f 212 else
614c279d 213 scb->fd = gdb_socket_cloexec (PF_INET, SOCK_STREAM, 0);
9db8d71f 214
363a6e9f 215 if (scb->fd == -1)
7c7a201a
MH
216 return -1;
217
c378eb4e 218 /* Set socket nonblocking. */
b4505029
MM
219 ioarg = 1;
220 ioctl (scb->fd, FIONBIO, &ioarg);
c906108c 221
3e43a32a 222 /* Use Non-blocking connect. connect() will return 0 if connected
c378eb4e 223 already. */
7c7a201a 224 n = connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr));
c906108c 225
84603566
SL
226 if (n < 0)
227 {
b4505029 228#ifdef USE_WIN32API
84603566 229 int err = WSAGetLastError();
b4505029 230#else
84603566 231 int err = errno;
b4505029 232#endif
84603566
SL
233
234 /* Maybe we're waiting for the remote target to become ready to
235 accept connections. */
236 if (tcp_auto_retry
b4505029 237#ifdef USE_WIN32API
84603566
SL
238 && err == WSAECONNREFUSED
239#else
240 && err == ECONNREFUSED
b4505029 241#endif
84603566
SL
242 && wait_for_connect (NULL, &polls) >= 0)
243 {
244 close (scb->fd);
245 goto retry;
246 }
c906108c 247
84603566
SL
248 if (
249#ifdef USE_WIN32API
250 /* Under Windows, calling "connect" with a non-blocking socket
251 results in WSAEWOULDBLOCK, not WSAEINPROGRESS. */
252 err != WSAEWOULDBLOCK
253#else
254 err != EINPROGRESS
255#endif
256 )
257 {
258 errno = err;
259 net_close (scb);
260 return -1;
261 }
7c7a201a 262
c378eb4e 263 /* Looks like we need to wait for the connect. */
7c7a201a
MH
264 do
265 {
84603566 266 n = wait_for_connect (scb, &polls);
7c7a201a 267 }
84603566
SL
268 while (n == 0);
269 if (n < 0)
7c7a201a 270 {
9db8d71f 271 net_close (scb);
7c7a201a
MH
272 return -1;
273 }
274 }
c906108c 275
c378eb4e 276 /* Got something. Is it an error? */
7c7a201a 277 {
47b667de
AC
278 int res, err;
279 socklen_t len;
433759f7 280
0ea3f30e 281 len = sizeof (err);
b4505029
MM
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);
7c7a201a
MH
287 if (res < 0 || err)
288 {
84603566
SL
289 /* Maybe the target still isn't ready to accept the connection. */
290 if (tcp_auto_retry
291#ifdef USE_WIN32API
292 && err == WSAECONNREFUSED
293#else
294 && err == ECONNREFUSED
295#endif
296 && wait_for_connect (NULL, &polls) >= 0)
297 {
298 close (scb->fd);
299 goto retry;
300 }
7c7a201a
MH
301 if (err)
302 errno = err;
9db8d71f 303 net_close (scb);
7c7a201a
MH
304 return -1;
305 }
306 }
9db8d71f 307
c378eb4e 308 /* Turn off nonblocking. */
b4505029
MM
309 ioarg = 0;
310 ioctl (scb->fd, FIONBIO, &ioarg);
7c7a201a 311
9db8d71f
DJ
312 if (use_udp == 0)
313 {
c378eb4e 314 /* Disable Nagle algorithm. Needed in some cases. */
9db8d71f
DJ
315 tmp = 1;
316 setsockopt (scb->fd, IPPROTO_TCP, TCP_NODELAY,
317 (char *)&tmp, sizeof (tmp));
318 }
319
6d318c73 320#ifdef SIGPIPE
7c7a201a
MH
321 /* If we don't do this, then GDB simply exits
322 when the remote side dies. */
323 signal (SIGPIPE, SIG_IGN);
6d318c73 324#endif
c906108c
SS
325
326 return 0;
327}
328
0ea3f30e 329void
9db8d71f 330net_close (struct serial *scb)
c906108c 331{
363a6e9f 332 if (scb->fd == -1)
c906108c
SS
333 return;
334
c5aa993b 335 close (scb->fd);
c906108c
SS
336 scb->fd = -1;
337}
338
0ea3f30e 339int
b4505029
MM
340net_read_prim (struct serial *scb, size_t count)
341{
c49e7f76
PA
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);
b4505029
MM
346}
347
0ea3f30e 348int
b4505029
MM
349net_write_prim (struct serial *scb, const void *buf, size_t count)
350{
351 return send (scb->fd, buf, count, 0);
352}
353
8775bb90
MS
354int
355ser_tcp_send_break (struct serial *scb)
356{
c378eb4e 357 /* Send telnet IAC and BREAK characters. */
8775bb90
MS
358 return (serial_write (scb, "\377\363", 2));
359}
360
84603566
SL
361/* Support for "set tcp" and "show tcp" commands. */
362
363static void
364set_tcp_cmd (char *args, int from_tty)
365{
366 help_list (tcp_set_cmdlist, "set tcp ", -1, gdb_stdout);
367}
368
369static void
370show_tcp_cmd (char *args, int from_tty)
371{
372 help_list (tcp_show_cmdlist, "show tcp ", -1, gdb_stdout);
373}
374
375
c906108c 376void
c2c6d25f 377_initialize_ser_tcp (void)
c906108c 378{
b4505029 379#ifdef USE_WIN32API
0ea3f30e
DJ
380 /* Do nothing; the TCP serial operations will be initialized in
381 ser-mingw.c. */
0ea3f30e
DJ
382#else
383 struct serial_ops *ops;
433759f7 384
b4505029 385 ops = XMALLOC (struct serial_ops);
2fdbdd39 386 memset (ops, 0, sizeof (struct serial_ops));
c2c6d25f
JM
387 ops->name = "tcp";
388 ops->next = 0;
9db8d71f
DJ
389 ops->open = net_open;
390 ops->close = net_close;
b4505029 391 ops->readchar = ser_base_readchar;
dd5da072
MM
392 ops->write = ser_base_write;
393 ops->flush_output = ser_base_flush_output;
394 ops->flush_input = ser_base_flush_input;
8775bb90 395 ops->send_break = ser_tcp_send_break;
dd5da072
MM
396 ops->go_raw = ser_base_raw;
397 ops->get_tty_state = ser_base_get_tty_state;
1e182ce8 398 ops->copy_tty_state = ser_base_copy_tty_state;
dd5da072
MM
399 ops->set_tty_state = ser_base_set_tty_state;
400 ops->print_tty_state = ser_base_print_tty_state;
401 ops->noflush_set_tty_state = ser_base_noflush_set_tty_state;
402 ops->setbaudrate = ser_base_setbaudrate;
403 ops->setstopbits = ser_base_setstopbits;
404 ops->drain_output = ser_base_drain_output;
405 ops->async = ser_base_async;
b4505029
MM
406 ops->read_prim = net_read_prim;
407 ops->write_prim = net_write_prim;
c2c6d25f 408 serial_add_interface (ops);
0ea3f30e 409#endif /* USE_WIN32API */
84603566
SL
410
411 add_prefix_cmd ("tcp", class_maintenance, set_tcp_cmd, _("\
412TCP protocol specific variables\n\
413Configure variables specific to remote TCP connections"),
414 &tcp_set_cmdlist, "set tcp ",
415 0 /* allow-unknown */, &setlist);
416 add_prefix_cmd ("tcp", class_maintenance, show_tcp_cmd, _("\
417TCP protocol specific variables\n\
418Configure variables specific to remote TCP connections"),
419 &tcp_show_cmdlist, "show tcp ",
420 0 /* allow-unknown */, &showlist);
421
422 add_setshow_boolean_cmd ("auto-retry", class_obscure,
423 &tcp_auto_retry, _("\
424Set auto-retry on socket connect"), _("\
425Show auto-retry on socket connect"),
426 NULL, NULL, NULL,
427 &tcp_set_cmdlist, &tcp_show_cmdlist);
428
429 add_setshow_uinteger_cmd ("connect-timeout", class_obscure,
430 &tcp_retry_limit, _("\
f81d1120
PA
431Set timeout limit in seconds for socket connection"), _("\
432Show timeout limit in seconds for socket connection"), _("\
433If set to \"unlimited\", GDB will keep attempting to establish a\n\
434connection forever, unless interrupted with Ctrl-c.\n\
435The default is 15 seconds."),
436 NULL, NULL,
437 &tcp_set_cmdlist, &tcp_show_cmdlist);
c906108c 438}
This page took 1.344978 seconds and 4 git commands to generate.