1 /* Serial interface for raw TCP connections on Un*x like systems
2 Copyright 1992, 1993, 1998 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include <sys/types.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
27 #include <sys/socket.h>
33 #include <netinet/tcp.h>
37 #include "gdb_string.h"
39 extern int (*ui_loop_hook) PARAMS ((int));
46 static int tcp_open PARAMS ((serial_t scb, const char *name));
47 static void tcp_raw PARAMS ((serial_t scb));
48 static int wait_for PARAMS ((serial_t scb, int timeout));
49 static int tcp_readchar PARAMS ((serial_t scb, int timeout));
50 static int tcp_setbaudrate PARAMS ((serial_t scb, int rate));
51 static int tcp_setstopbits PARAMS ((serial_t scb, int num));
52 static int tcp_write PARAMS ((serial_t scb, const char *str, int len));
53 /* FIXME: static void tcp_restore PARAMS ((serial_t scb)); */
54 static void tcp_close PARAMS ((serial_t scb));
55 static serial_ttystate tcp_get_tty_state PARAMS ((serial_t scb));
56 static int tcp_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
57 static int tcp_return_0 PARAMS ((serial_t));
58 static int tcp_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate,
60 static void tcp_print_tty_state PARAMS ((serial_t, serial_ttystate));
62 void _initialize_ser_tcp PARAMS ((void));
64 /* Open up a raw tcp socket */
73 struct hostent *hostent;
74 struct sockaddr_in sockaddr;
77 struct protoent *protoent;
80 port_str = strchr (name, ':');
83 error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */
85 tmp = min (port_str - name, (int) sizeof hostname - 1);
86 strncpy (hostname, name, tmp); /* Don't want colon */
87 hostname[tmp] = '\000'; /* Tie off host name */
88 port = atoi (port_str + 1);
90 hostent = gethostbyname (hostname);
94 fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname);
99 for (i = 1; i <= 15; i++)
101 scb->fd = socket (PF_INET, SOCK_STREAM, 0);
105 /* Allow rapid reuse of this port. */
107 setsockopt (scb->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&tmp, sizeof(tmp));
109 /* Enable TCP keep alive process. */
111 setsockopt (scb->fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&tmp, sizeof(tmp));
113 sockaddr.sin_family = PF_INET;
114 sockaddr.sin_port = htons(port);
115 memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
116 sizeof (struct in_addr));
118 if (!connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)))
124 /* We retry for ECONNREFUSED because that is often a temporary condition, which
125 happens when the server is being restarted. */
127 if (errno != ECONNREFUSED)
133 protoent = getprotobyname ("tcp");
138 if (setsockopt (scb->fd, protoent->p_proto, TCP_NODELAY,
139 (char *)&tmp, sizeof(tmp)))
142 signal(SIGPIPE, SIG_IGN); /* If we don't do this, then GDB simply exits
143 when the remote side dies. */
148 static serial_ttystate
149 tcp_get_tty_state(scb)
152 struct tcp_ttystate *state;
154 state = (struct tcp_ttystate *)xmalloc(sizeof *state);
156 return (serial_ttystate)state;
160 tcp_set_tty_state(scb, ttystate)
162 serial_ttystate ttystate;
164 struct tcp_ttystate *state;
166 state = (struct tcp_ttystate *)ttystate;
182 return; /* Always in raw mode */
185 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
186 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
188 For termio{s}, we actually just setup VTIME if necessary, and let the
189 timeout occur in the read() in tcp_read().
193 wait_for (scb, timeout)
199 fd_set readfds, exceptfds;
202 FD_ZERO (&exceptfds);
207 FD_SET(scb->fd, &readfds);
208 FD_SET(scb->fd, &exceptfds);
213 numfds = select(scb->fd+1, &readfds, 0, &exceptfds, &tv);
215 numfds = select(scb->fd+1, &readfds, 0, &exceptfds, 0);
220 return SERIAL_TIMEOUT;
221 else if (errno == EINTR)
224 return SERIAL_ERROR; /* Got an error from select or poll */
231 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
232 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
233 char if successful. Returns -2 if timeout expired, EOF if line dropped
234 dead, or -3 for any other error (see errno in that case). */
237 tcp_readchar (scb, timeout)
244 if (scb->bufcnt-- > 0)
247 /* We have to be able to keep the GUI alive here, so we break the original
248 timeout into steps of 1 second, running the "keep the GUI alive" hook
249 each time through the loop.
251 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
252 will only go through the loop once. */
254 delta = (timeout == 0 ? 0 : 1);
258 /* N.B. The UI may destroy our world (for instance by calling
259 remote_stop,) in which case we want to get out of here as
260 quickly as possible. It is not safe to touch scb, since
261 someone else might have freed it. The ui_loop_hook signals that
262 we should exit by returning 1. */
266 if (ui_loop_hook (0))
267 return SERIAL_TIMEOUT;
270 status = wait_for (scb, delta);
273 /* If we got a character or an error back from wait_for, then we can
274 break from the loop before the timeout is completed. */
276 if (status != SERIAL_TIMEOUT)
281 /* If we have exhausted the original timeout, then generate
282 a SERIAL_TIMEOUT, and pass it out of the loop. */
284 else if (timeout == 0)
286 status == SERIAL_TIMEOUT;
296 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
297 if (scb->bufcnt != -1 || errno != EINTR)
301 if (scb->bufcnt <= 0)
303 if (scb->bufcnt == 0)
304 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
305 distinguish between EOF & timeouts
308 return SERIAL_ERROR; /* Got an error from read */
312 scb->bufp = scb->buf;
317 tcp_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
319 serial_ttystate new_ttystate;
320 serial_ttystate old_ttystate;
326 tcp_print_tty_state (scb, ttystate)
328 serial_ttystate ttystate;
330 /* Nothing to print. */
335 tcp_setbaudrate(scb, rate)
339 return 0; /* Never fails! */
343 tcp_setstopbits(scb, num)
347 return 0; /* Never fails! */
351 tcp_write(scb, str, len)
360 cc = write(scb->fd, str, len);
381 static struct serial_ops tcp_ops =
389 tcp_return_0, /* flush output */
390 tcp_return_0, /* flush input */
391 tcp_return_0, /* send break */
396 tcp_noflush_set_tty_state,
399 tcp_return_0, /* wait for output to drain */
403 _initialize_ser_tcp ()
405 serial_add_interface (&tcp_ops);