]>
Commit | Line | Data |
---|---|---|
f9f87d2c MK |
1 | /* Serial interface for raw TCP connections on Un*x like systems. |
2 | ||
32d0add0 | 3 | Copyright (C) 1992-2015 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 | |
9eb1356e | 48 | #include <netinet/in.h> |
c906108c SS |
49 | #include <arpa/inet.h> |
50 | #include <netdb.h> | |
9eb1356e | 51 | #include <sys/socket.h> |
c906108c | 52 | #include <netinet/tcp.h> |
b4505029 | 53 | #endif |
c906108c | 54 | |
042be3a9 | 55 | #include <signal.h> |
84603566 | 56 | #include "gdb_select.h" |
c906108c | 57 | |
f9f87d2c MK |
58 | #ifndef HAVE_SOCKLEN_T |
59 | typedef int socklen_t; | |
60 | #endif | |
61 | ||
c2c6d25f | 62 | void _initialize_ser_tcp (void); |
c906108c | 63 | |
84603566 SL |
64 | /* For "set tcp" and "show tcp". */ |
65 | ||
66 | static struct cmd_list_element *tcp_set_cmdlist; | |
67 | static struct cmd_list_element *tcp_show_cmdlist; | |
68 | ||
69 | /* Whether to auto-retry refused connections. */ | |
70 | ||
71 | static int tcp_auto_retry = 1; | |
72 | ||
73 | /* Timeout period for connections, in seconds. */ | |
74 | ||
964b8317 | 75 | static unsigned int tcp_retry_limit = 15; |
84603566 | 76 | |
c378eb4e | 77 | /* How many times per second to poll deprecated_ui_loop_hook. */ |
84603566 SL |
78 | |
79 | #define POLL_INTERVAL 5 | |
80 | ||
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. */ | |
84 | ||
85 | static int | |
2c619be2 | 86 | wait_for_connect (struct serial *scb, unsigned int *polls) |
84603566 SL |
87 | { |
88 | struct timeval t; | |
89 | int n; | |
90 | ||
91 | /* While we wait for the connect to complete, | |
92 | poll the UI so it can update or the user can | |
93 | interrupt. */ | |
94 | if (deprecated_ui_loop_hook && deprecated_ui_loop_hook (0)) | |
95 | { | |
96 | errno = EINTR; | |
97 | return -1; | |
98 | } | |
99 | ||
100 | /* Check for timeout. */ | |
101 | if (*polls > tcp_retry_limit * POLL_INTERVAL) | |
102 | { | |
103 | errno = ETIMEDOUT; | |
104 | return -1; | |
105 | } | |
106 | ||
107 | /* Back off to polling once per second after the first POLL_INTERVAL | |
108 | polls. */ | |
109 | if (*polls < POLL_INTERVAL) | |
110 | { | |
111 | t.tv_sec = 0; | |
112 | t.tv_usec = 1000000 / POLL_INTERVAL; | |
113 | } | |
114 | else | |
115 | { | |
116 | t.tv_sec = 1; | |
117 | t.tv_usec = 0; | |
118 | } | |
119 | ||
120 | if (scb) | |
121 | { | |
122 | fd_set rset, wset, eset; | |
433759f7 | 123 | |
84603566 SL |
124 | FD_ZERO (&rset); |
125 | FD_SET (scb->fd, &rset); | |
126 | wset = rset; | |
127 | eset = rset; | |
128 | ||
129 | /* POSIX systems return connection success or failure by signalling | |
130 | wset. Windows systems return success in wset and failure in | |
131 | eset. | |
132 | ||
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 | |
136 | to a socket. */ | |
137 | n = select (scb->fd + 1, &rset, &wset, &eset, &t); | |
138 | } | |
139 | else | |
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); | |
143 | ||
144 | /* If we didn't time out, only count it as one poll. */ | |
145 | if (n > 0 || *polls < POLL_INTERVAL) | |
146 | (*polls)++; | |
147 | else | |
148 | (*polls) += POLL_INTERVAL; | |
149 | ||
150 | return n; | |
151 | } | |
7c7a201a | 152 | |
c378eb4e | 153 | /* Open a tcp socket. */ |
c906108c | 154 | |
0ea3f30e | 155 | int |
9db8d71f | 156 | net_open (struct serial *scb, const char *name) |
c906108c | 157 | { |
7c7a201a MH |
158 | char *port_str, hostname[100]; |
159 | int n, port, tmp; | |
9db8d71f | 160 | int use_udp; |
c906108c | 161 | struct hostent *hostent; |
9eb1356e | 162 | struct sockaddr_in sockaddr; |
b4505029 MM |
163 | #ifdef USE_WIN32API |
164 | u_long ioarg; | |
165 | #else | |
166 | int ioarg; | |
167 | #endif | |
2c619be2 | 168 | unsigned int polls = 0; |
c906108c | 169 | |
9db8d71f | 170 | use_udp = 0; |
61012eef | 171 | if (startswith (name, "udp:")) |
9db8d71f DJ |
172 | { |
173 | use_udp = 1; | |
174 | name = name + 4; | |
175 | } | |
61012eef | 176 | else if (startswith (name, "tcp:")) |
9db8d71f DJ |
177 | name = name + 4; |
178 | ||
c906108c SS |
179 | port_str = strchr (name, ':'); |
180 | ||
181 | if (!port_str) | |
c378eb4e MS |
182 | error (_("net_open: No colon in host name!")); /* Shouldn't ever |
183 | happen. */ | |
c906108c SS |
184 | |
185 | tmp = min (port_str - name, (int) sizeof hostname - 1); | |
c378eb4e MS |
186 | strncpy (hostname, name, tmp); /* Don't want colon. */ |
187 | hostname[tmp] = '\000'; /* Tie off host name. */ | |
c906108c SS |
188 | port = atoi (port_str + 1); |
189 | ||
c378eb4e | 190 | /* Default hostname is localhost. */ |
ad4571f3 CV |
191 | if (!hostname[0]) |
192 | strcpy (hostname, "localhost"); | |
193 | ||
c906108c | 194 | hostent = gethostbyname (hostname); |
c906108c SS |
195 | if (!hostent) |
196 | { | |
197 | fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname); | |
198 | errno = ENOENT; | |
199 | return -1; | |
200 | } | |
201 | ||
9eb1356e PA |
202 | sockaddr.sin_family = PF_INET; |
203 | sockaddr.sin_port = htons (port); | |
204 | memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr, | |
84603566 SL |
205 | sizeof (struct in_addr)); |
206 | ||
207 | retry: | |
208 | ||
9db8d71f | 209 | if (use_udp) |
614c279d | 210 | scb->fd = gdb_socket_cloexec (PF_INET, SOCK_DGRAM, 0); |
9db8d71f | 211 | else |
614c279d | 212 | scb->fd = gdb_socket_cloexec (PF_INET, SOCK_STREAM, 0); |
9db8d71f | 213 | |
363a6e9f | 214 | if (scb->fd == -1) |
7c7a201a MH |
215 | return -1; |
216 | ||
c378eb4e | 217 | /* Set socket nonblocking. */ |
b4505029 MM |
218 | ioarg = 1; |
219 | ioctl (scb->fd, FIONBIO, &ioarg); | |
c906108c | 220 | |
3e43a32a | 221 | /* Use Non-blocking connect. connect() will return 0 if connected |
c378eb4e | 222 | already. */ |
9eb1356e | 223 | n = connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr)); |
c906108c | 224 | |
84603566 SL |
225 | if (n < 0) |
226 | { | |
b4505029 | 227 | #ifdef USE_WIN32API |
84603566 | 228 | int err = WSAGetLastError(); |
b4505029 | 229 | #else |
84603566 | 230 | int err = errno; |
b4505029 | 231 | #endif |
84603566 SL |
232 | |
233 | /* Maybe we're waiting for the remote target to become ready to | |
234 | accept connections. */ | |
235 | if (tcp_auto_retry | |
b4505029 | 236 | #ifdef USE_WIN32API |
84603566 SL |
237 | && err == WSAECONNREFUSED |
238 | #else | |
239 | && err == ECONNREFUSED | |
b4505029 | 240 | #endif |
84603566 SL |
241 | && wait_for_connect (NULL, &polls) >= 0) |
242 | { | |
243 | close (scb->fd); | |
244 | goto retry; | |
245 | } | |
c906108c | 246 | |
84603566 SL |
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 | } | |
7c7a201a | 261 | |
c378eb4e | 262 | /* Looks like we need to wait for the connect. */ |
7c7a201a MH |
263 | do |
264 | { | |
84603566 | 265 | n = wait_for_connect (scb, &polls); |
7c7a201a | 266 | } |
84603566 SL |
267 | while (n == 0); |
268 | if (n < 0) | |
7c7a201a | 269 | { |
9db8d71f | 270 | net_close (scb); |
7c7a201a MH |
271 | return -1; |
272 | } | |
273 | } | |
c906108c | 274 | |
c378eb4e | 275 | /* Got something. Is it an error? */ |
7c7a201a | 276 | { |
47b667de AC |
277 | int res, err; |
278 | socklen_t len; | |
433759f7 | 279 | |
0ea3f30e | 280 | len = sizeof (err); |
b4505029 MM |
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); | |
7c7a201a MH |
286 | if (res < 0 || err) |
287 | { | |
84603566 SL |
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 | } | |
7c7a201a MH |
300 | if (err) |
301 | errno = err; | |
9db8d71f | 302 | net_close (scb); |
7c7a201a MH |
303 | return -1; |
304 | } | |
305 | } | |
9db8d71f | 306 | |
c378eb4e | 307 | /* Turn off nonblocking. */ |
b4505029 MM |
308 | ioarg = 0; |
309 | ioctl (scb->fd, FIONBIO, &ioarg); | |
7c7a201a | 310 | |
9db8d71f DJ |
311 | if (use_udp == 0) |
312 | { | |
c378eb4e | 313 | /* Disable Nagle algorithm. Needed in some cases. */ |
9db8d71f DJ |
314 | tmp = 1; |
315 | setsockopt (scb->fd, IPPROTO_TCP, TCP_NODELAY, | |
316 | (char *)&tmp, sizeof (tmp)); | |
317 | } | |
318 | ||
6d318c73 | 319 | #ifdef SIGPIPE |
7c7a201a MH |
320 | /* If we don't do this, then GDB simply exits |
321 | when the remote side dies. */ | |
322 | signal (SIGPIPE, SIG_IGN); | |
6d318c73 | 323 | #endif |
c906108c SS |
324 | |
325 | return 0; | |
326 | } | |
327 | ||
0ea3f30e | 328 | void |
9db8d71f | 329 | net_close (struct serial *scb) |
c906108c | 330 | { |
363a6e9f | 331 | if (scb->fd == -1) |
c906108c SS |
332 | return; |
333 | ||
c5aa993b | 334 | close (scb->fd); |
c906108c SS |
335 | scb->fd = -1; |
336 | } | |
337 | ||
0ea3f30e | 338 | int |
b4505029 MM |
339 | net_read_prim (struct serial *scb, size_t count) |
340 | { | |
c49e7f76 PA |
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, (void *) scb->buf, count, 0); | |
b4505029 MM |
345 | } |
346 | ||
0ea3f30e | 347 | int |
b4505029 MM |
348 | net_write_prim (struct serial *scb, const void *buf, size_t count) |
349 | { | |
350 | return send (scb->fd, buf, count, 0); | |
351 | } | |
352 | ||
8775bb90 MS |
353 | int |
354 | ser_tcp_send_break (struct serial *scb) | |
355 | { | |
c378eb4e | 356 | /* Send telnet IAC and BREAK characters. */ |
8775bb90 MS |
357 | return (serial_write (scb, "\377\363", 2)); |
358 | } | |
359 | ||
84603566 SL |
360 | /* Support for "set tcp" and "show tcp" commands. */ |
361 | ||
362 | static void | |
363 | set_tcp_cmd (char *args, int from_tty) | |
364 | { | |
635c7e8a | 365 | help_list (tcp_set_cmdlist, "set tcp ", all_commands, gdb_stdout); |
84603566 SL |
366 | } |
367 | ||
368 | static void | |
369 | show_tcp_cmd (char *args, int from_tty) | |
370 | { | |
635c7e8a | 371 | help_list (tcp_show_cmdlist, "show tcp ", all_commands, gdb_stdout); |
84603566 SL |
372 | } |
373 | ||
12e8c7d7 TT |
374 | #ifndef USE_WIN32API |
375 | ||
376 | /* The TCP ops. */ | |
377 | ||
378 | static const struct serial_ops tcp_ops = | |
379 | { | |
380 | "tcp", | |
381 | net_open, | |
382 | net_close, | |
383 | NULL, | |
384 | ser_base_readchar, | |
385 | ser_base_write, | |
386 | ser_base_flush_output, | |
387 | ser_base_flush_input, | |
388 | ser_tcp_send_break, | |
389 | ser_base_raw, | |
390 | ser_base_get_tty_state, | |
391 | ser_base_copy_tty_state, | |
392 | ser_base_set_tty_state, | |
393 | ser_base_print_tty_state, | |
394 | ser_base_noflush_set_tty_state, | |
395 | ser_base_setbaudrate, | |
396 | ser_base_setstopbits, | |
236af5e3 | 397 | ser_base_setparity, |
12e8c7d7 TT |
398 | ser_base_drain_output, |
399 | ser_base_async, | |
400 | net_read_prim, | |
401 | net_write_prim | |
402 | }; | |
403 | ||
404 | #endif /* USE_WIN32API */ | |
84603566 | 405 | |
c906108c | 406 | void |
c2c6d25f | 407 | _initialize_ser_tcp (void) |
c906108c | 408 | { |
b4505029 | 409 | #ifdef USE_WIN32API |
0ea3f30e DJ |
410 | /* Do nothing; the TCP serial operations will be initialized in |
411 | ser-mingw.c. */ | |
0ea3f30e | 412 | #else |
12e8c7d7 | 413 | serial_add_interface (&tcp_ops); |
0ea3f30e | 414 | #endif /* USE_WIN32API */ |
84603566 SL |
415 | |
416 | add_prefix_cmd ("tcp", class_maintenance, set_tcp_cmd, _("\ | |
417 | TCP protocol specific variables\n\ | |
418 | Configure variables specific to remote TCP connections"), | |
419 | &tcp_set_cmdlist, "set tcp ", | |
420 | 0 /* allow-unknown */, &setlist); | |
421 | add_prefix_cmd ("tcp", class_maintenance, show_tcp_cmd, _("\ | |
422 | TCP protocol specific variables\n\ | |
423 | Configure variables specific to remote TCP connections"), | |
424 | &tcp_show_cmdlist, "show tcp ", | |
425 | 0 /* allow-unknown */, &showlist); | |
426 | ||
427 | add_setshow_boolean_cmd ("auto-retry", class_obscure, | |
428 | &tcp_auto_retry, _("\ | |
429 | Set auto-retry on socket connect"), _("\ | |
430 | Show auto-retry on socket connect"), | |
431 | NULL, NULL, NULL, | |
432 | &tcp_set_cmdlist, &tcp_show_cmdlist); | |
433 | ||
434 | add_setshow_uinteger_cmd ("connect-timeout", class_obscure, | |
435 | &tcp_retry_limit, _("\ | |
f81d1120 PA |
436 | Set timeout limit in seconds for socket connection"), _("\ |
437 | Show timeout limit in seconds for socket connection"), _("\ | |
438 | If set to \"unlimited\", GDB will keep attempting to establish a\n\ | |
439 | connection forever, unless interrupted with Ctrl-c.\n\ | |
440 | The default is 15 seconds."), | |
441 | NULL, NULL, | |
442 | &tcp_set_cmdlist, &tcp_show_cmdlist); | |
c906108c | 443 | } |