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