Make it simpler to add events to Python
[deliverable/binutils-gdb.git] / gdb / ser-tcp.c
1 /* Serial interface for raw TCP connections on Un*x like systems.
2
3 Copyright (C) 1992-2017 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21 #include "serial.h"
22 #include "ser-base.h"
23 #include "ser-tcp.h"
24 #include "gdbcmd.h"
25 #include "cli/cli-decode.h"
26 #include "cli/cli-setshow.h"
27 #include "filestuff.h"
28
29 #include <sys/types.h>
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
38 #include "gdb_sys_time.h"
39
40 #ifdef USE_WIN32API
41 #include <winsock2.h>
42 #ifndef ETIMEDOUT
43 #define ETIMEDOUT WSAETIMEDOUT
44 #endif
45 #define close(fd) closesocket (fd)
46 #define ioctl ioctlsocket
47 #else
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 #include <netdb.h>
51 #include <sys/socket.h>
52 #include <netinet/tcp.h>
53 #endif
54
55 #include <signal.h>
56 #include "gdb_select.h"
57 #include <algorithm>
58
59 #ifndef HAVE_SOCKLEN_T
60 typedef int socklen_t;
61 #endif
62
63 /* For "set tcp" and "show tcp". */
64
65 static struct cmd_list_element *tcp_set_cmdlist;
66 static struct cmd_list_element *tcp_show_cmdlist;
67
68 /* Whether to auto-retry refused connections. */
69
70 static int tcp_auto_retry = 1;
71
72 /* Timeout period for connections, in seconds. */
73
74 static unsigned int tcp_retry_limit = 15;
75
76 /* How many times per second to poll deprecated_ui_loop_hook. */
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
84 static int
85 wait_for_connect (struct serial *scb, unsigned 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
123 FD_ZERO (&rset);
124 FD_SET (scb->fd, &rset);
125 wset = rset;
126 eset = rset;
127
128 /* POSIX systems return connection success or failure by signalling
129 wset. Windows systems return success in wset and failure in
130 eset.
131
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
135 to a socket. */
136 n = select (scb->fd + 1, &rset, &wset, &eset, &t);
137 }
138 else
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);
142
143 /* If we didn't time out, only count it as one poll. */
144 if (n > 0 || *polls < POLL_INTERVAL)
145 (*polls)++;
146 else
147 (*polls) += POLL_INTERVAL;
148
149 return n;
150 }
151
152 /* Open a tcp socket. */
153
154 int
155 net_open (struct serial *scb, const char *name)
156 {
157 char hostname[100];
158 const char *port_str;
159 int n, port, tmp;
160 int use_udp;
161 struct hostent *hostent;
162 struct sockaddr_in sockaddr;
163 #ifdef USE_WIN32API
164 u_long ioarg;
165 #else
166 int ioarg;
167 #endif
168 unsigned int polls = 0;
169
170 use_udp = 0;
171 if (startswith (name, "udp:"))
172 {
173 use_udp = 1;
174 name = name + 4;
175 }
176 else if (startswith (name, "tcp:"))
177 name = name + 4;
178
179 port_str = strchr (name, ':');
180
181 if (!port_str)
182 error (_("net_open: No colon in host name!")); /* Shouldn't ever
183 happen. */
184
185 tmp = std::min (port_str - name, (ptrdiff_t) 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);
189
190 /* Default hostname is localhost. */
191 if (!hostname[0])
192 strcpy (hostname, "localhost");
193
194 hostent = gethostbyname (hostname);
195 if (!hostent)
196 {
197 fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname);
198 errno = ENOENT;
199 return -1;
200 }
201
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));
206
207 retry:
208
209 if (use_udp)
210 scb->fd = gdb_socket_cloexec (PF_INET, SOCK_DGRAM, 0);
211 else
212 scb->fd = gdb_socket_cloexec (PF_INET, SOCK_STREAM, 0);
213
214 if (scb->fd == -1)
215 return -1;
216
217 /* Set socket nonblocking. */
218 ioarg = 1;
219 ioctl (scb->fd, FIONBIO, &ioarg);
220
221 /* Use Non-blocking connect. connect() will return 0 if connected
222 already. */
223 n = connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr));
224
225 if (n < 0)
226 {
227 #ifdef USE_WIN32API
228 int err = WSAGetLastError();
229 #else
230 int err = errno;
231 #endif
232
233 /* Maybe we're waiting for the remote target to become ready to
234 accept connections. */
235 if (tcp_auto_retry
236 #ifdef USE_WIN32API
237 && err == WSAECONNREFUSED
238 #else
239 && err == ECONNREFUSED
240 #endif
241 && wait_for_connect (NULL, &polls) >= 0)
242 {
243 close (scb->fd);
244 goto retry;
245 }
246
247 if (
248 #ifdef USE_WIN32API
249 /* Under Windows, calling "connect" with a non-blocking socket
250 results in WSAEWOULDBLOCK, not WSAEINPROGRESS. */
251 err != WSAEWOULDBLOCK
252 #else
253 err != EINPROGRESS
254 #endif
255 )
256 {
257 errno = err;
258 net_close (scb);
259 return -1;
260 }
261
262 /* Looks like we need to wait for the connect. */
263 do
264 {
265 n = wait_for_connect (scb, &polls);
266 }
267 while (n == 0);
268 if (n < 0)
269 {
270 net_close (scb);
271 return -1;
272 }
273 }
274
275 /* Got something. Is it an error? */
276 {
277 int res, err;
278 socklen_t len;
279
280 len = sizeof (err);
281 /* On Windows, the fourth parameter to getsockopt is a "char *";
282 on UNIX systems it is generally "void *". The cast to "char *"
283 is OK everywhere, since in C++ any data pointer type can be
284 implicitly converted to "void *". */
285 res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, (char *) &err, &len);
286 if (res < 0 || err)
287 {
288 /* Maybe the target still isn't ready to accept the connection. */
289 if (tcp_auto_retry
290 #ifdef USE_WIN32API
291 && err == WSAECONNREFUSED
292 #else
293 && err == ECONNREFUSED
294 #endif
295 && wait_for_connect (NULL, &polls) >= 0)
296 {
297 close (scb->fd);
298 goto retry;
299 }
300 if (err)
301 errno = err;
302 net_close (scb);
303 return -1;
304 }
305 }
306
307 /* Turn off nonblocking. */
308 ioarg = 0;
309 ioctl (scb->fd, FIONBIO, &ioarg);
310
311 if (use_udp == 0)
312 {
313 /* Disable Nagle algorithm. Needed in some cases. */
314 tmp = 1;
315 setsockopt (scb->fd, IPPROTO_TCP, TCP_NODELAY,
316 (char *)&tmp, sizeof (tmp));
317 }
318
319 #ifdef SIGPIPE
320 /* If we don't do this, then GDB simply exits
321 when the remote side dies. */
322 signal (SIGPIPE, SIG_IGN);
323 #endif
324
325 return 0;
326 }
327
328 void
329 net_close (struct serial *scb)
330 {
331 if (scb->fd == -1)
332 return;
333
334 close (scb->fd);
335 scb->fd = -1;
336 }
337
338 int
339 net_read_prim (struct serial *scb, size_t count)
340 {
341 /* Need to cast to silence -Wpointer-sign on MinGW, as Winsock's
342 'recv' takes 'char *' as second argument, while 'scb->buf' is
343 'unsigned char *'. */
344 return recv (scb->fd, (char *) scb->buf, count, 0);
345 }
346
347 int
348 net_write_prim (struct serial *scb, const void *buf, size_t count)
349 {
350 /* On Windows, the second parameter to send is a "const char *"; on
351 UNIX systems it is generally "const void *". The cast to "const
352 char *" is OK everywhere, since in C++ any data pointer type can
353 be implicitly converted to "const void *". */
354 return send (scb->fd, (const char *) buf, count, 0);
355 }
356
357 int
358 ser_tcp_send_break (struct serial *scb)
359 {
360 /* Send telnet IAC and BREAK characters. */
361 return (serial_write (scb, "\377\363", 2));
362 }
363
364 /* Support for "set tcp" and "show tcp" commands. */
365
366 static void
367 set_tcp_cmd (char *args, int from_tty)
368 {
369 help_list (tcp_set_cmdlist, "set tcp ", all_commands, gdb_stdout);
370 }
371
372 static void
373 show_tcp_cmd (char *args, int from_tty)
374 {
375 help_list (tcp_show_cmdlist, "show tcp ", all_commands, gdb_stdout);
376 }
377
378 #ifndef USE_WIN32API
379
380 /* The TCP ops. */
381
382 static const struct serial_ops tcp_ops =
383 {
384 "tcp",
385 net_open,
386 net_close,
387 NULL,
388 ser_base_readchar,
389 ser_base_write,
390 ser_base_flush_output,
391 ser_base_flush_input,
392 ser_tcp_send_break,
393 ser_base_raw,
394 ser_base_get_tty_state,
395 ser_base_copy_tty_state,
396 ser_base_set_tty_state,
397 ser_base_print_tty_state,
398 ser_base_noflush_set_tty_state,
399 ser_base_setbaudrate,
400 ser_base_setstopbits,
401 ser_base_setparity,
402 ser_base_drain_output,
403 ser_base_async,
404 net_read_prim,
405 net_write_prim
406 };
407
408 #endif /* USE_WIN32API */
409
410 void
411 _initialize_ser_tcp (void)
412 {
413 #ifdef USE_WIN32API
414 /* Do nothing; the TCP serial operations will be initialized in
415 ser-mingw.c. */
416 #else
417 serial_add_interface (&tcp_ops);
418 #endif /* USE_WIN32API */
419
420 add_prefix_cmd ("tcp", class_maintenance, set_tcp_cmd, _("\
421 TCP protocol specific variables\n\
422 Configure variables specific to remote TCP connections"),
423 &tcp_set_cmdlist, "set tcp ",
424 0 /* allow-unknown */, &setlist);
425 add_prefix_cmd ("tcp", class_maintenance, show_tcp_cmd, _("\
426 TCP protocol specific variables\n\
427 Configure variables specific to remote TCP connections"),
428 &tcp_show_cmdlist, "show tcp ",
429 0 /* allow-unknown */, &showlist);
430
431 add_setshow_boolean_cmd ("auto-retry", class_obscure,
432 &tcp_auto_retry, _("\
433 Set auto-retry on socket connect"), _("\
434 Show auto-retry on socket connect"),
435 NULL, NULL, NULL,
436 &tcp_set_cmdlist, &tcp_show_cmdlist);
437
438 add_setshow_uinteger_cmd ("connect-timeout", class_obscure,
439 &tcp_retry_limit, _("\
440 Set timeout limit in seconds for socket connection"), _("\
441 Show timeout limit in seconds for socket connection"), _("\
442 If set to \"unlimited\", GDB will keep attempting to establish a\n\
443 connection forever, unless interrupted with Ctrl-c.\n\
444 The default is 15 seconds."),
445 NULL, NULL,
446 &tcp_set_cmdlist, &tcp_show_cmdlist);
447 }
This page took 0.039606 seconds and 4 git commands to generate.