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,
19 Boston, MA 02111-1307, USA. */
23 #include <sys/types.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
28 #include <sys/socket.h>
31 #include <netinet/tcp.h>
35 #include "gdb_string.h"
37 extern int (*ui_loop_hook
) PARAMS ((int));
44 static int tcp_open
PARAMS ((serial_t scb
, const char *name
));
45 static void tcp_raw
PARAMS ((serial_t scb
));
46 static int wait_for
PARAMS ((serial_t scb
, int timeout
));
47 static int tcp_readchar
PARAMS ((serial_t scb
, int timeout
));
48 static int tcp_setbaudrate
PARAMS ((serial_t scb
, int rate
));
49 static int tcp_setstopbits
PARAMS ((serial_t scb
, int num
));
50 static int tcp_write
PARAMS ((serial_t scb
, const char *str
, int len
));
51 /* FIXME: static void tcp_restore PARAMS ((serial_t scb)); */
52 static void tcp_close
PARAMS ((serial_t scb
));
53 static serial_ttystate tcp_get_tty_state
PARAMS ((serial_t scb
));
54 static int tcp_set_tty_state
PARAMS ((serial_t scb
, serial_ttystate state
));
55 static int tcp_return_0
PARAMS ((serial_t
));
56 static int tcp_noflush_set_tty_state
PARAMS ((serial_t
, serial_ttystate
,
58 static void tcp_print_tty_state
PARAMS ((serial_t
, serial_ttystate
));
60 void _initialize_ser_tcp
PARAMS ((void));
62 /* Open up a raw tcp socket */
71 struct hostent
*hostent
;
72 struct sockaddr_in sockaddr
;
75 struct protoent
*protoent
;
78 port_str
= strchr (name
, ':');
81 error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */
83 tmp
= min (port_str
- name
, (int) sizeof hostname
- 1);
84 strncpy (hostname
, name
, tmp
); /* Don't want colon */
85 hostname
[tmp
] = '\000'; /* Tie off host name */
86 port
= atoi (port_str
+ 1);
88 hostent
= gethostbyname (hostname
);
92 fprintf_unfiltered (gdb_stderr
, "%s: unknown host\n", hostname
);
97 for (i
= 1; i
<= 15; i
++)
99 scb
->fd
= socket (PF_INET
, SOCK_STREAM
, 0);
103 /* Allow rapid reuse of this port. */
105 setsockopt (scb
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &tmp
, sizeof (tmp
));
107 /* Enable TCP keep alive process. */
109 setsockopt (scb
->fd
, SOL_SOCKET
, SO_KEEPALIVE
, (char *) &tmp
, sizeof (tmp
));
111 sockaddr
.sin_family
= PF_INET
;
112 sockaddr
.sin_port
= htons (port
);
113 memcpy (&sockaddr
.sin_addr
.s_addr
, hostent
->h_addr
,
114 sizeof (struct in_addr
));
116 if (!connect (scb
->fd
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
)))
122 /* We retry for ECONNREFUSED because that is often a temporary condition, which
123 happens when the server is being restarted. */
125 if (errno
!= ECONNREFUSED
)
131 protoent
= getprotobyname ("tcp");
136 if (setsockopt (scb
->fd
, protoent
->p_proto
, TCP_NODELAY
,
137 (char *) &tmp
, sizeof (tmp
)))
140 signal (SIGPIPE
, SIG_IGN
); /* If we don't do this, then GDB simply exits
141 when the remote side dies. */
146 static serial_ttystate
147 tcp_get_tty_state (scb
)
150 struct tcp_ttystate
*state
;
152 state
= (struct tcp_ttystate
*) xmalloc (sizeof *state
);
154 return (serial_ttystate
) state
;
158 tcp_set_tty_state (scb
, ttystate
)
160 serial_ttystate ttystate
;
162 struct tcp_ttystate
*state
;
164 state
= (struct tcp_ttystate
*) ttystate
;
180 return; /* Always in raw mode */
183 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
184 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
186 For termio{s}, we actually just setup VTIME if necessary, and let the
187 timeout occur in the read() in tcp_read().
191 wait_for (scb
, timeout
)
197 fd_set readfds
, exceptfds
;
200 FD_ZERO (&exceptfds
);
205 FD_SET (scb
->fd
, &readfds
);
206 FD_SET (scb
->fd
, &exceptfds
);
211 numfds
= select (scb
->fd
+ 1, &readfds
, 0, &exceptfds
, &tv
);
213 numfds
= select (scb
->fd
+ 1, &readfds
, 0, &exceptfds
, 0);
218 return SERIAL_TIMEOUT
;
219 else if (errno
== EINTR
)
222 return SERIAL_ERROR
; /* Got an error from select or poll */
229 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
230 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
231 char if successful. Returns -2 if timeout expired, EOF if line dropped
232 dead, or -3 for any other error (see errno in that case). */
235 tcp_readchar (scb
, timeout
)
242 if (scb
->bufcnt
-- > 0)
245 /* We have to be able to keep the GUI alive here, so we break the original
246 timeout into steps of 1 second, running the "keep the GUI alive" hook
247 each time through the loop.
249 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
250 will only go through the loop once. */
252 delta
= (timeout
== 0 ? 0 : 1);
256 /* N.B. The UI may destroy our world (for instance by calling
257 remote_stop,) in which case we want to get out of here as
258 quickly as possible. It is not safe to touch scb, since
259 someone else might have freed it. The ui_loop_hook signals that
260 we should exit by returning 1. */
264 if (ui_loop_hook (0))
265 return SERIAL_TIMEOUT
;
268 status
= wait_for (scb
, delta
);
271 /* If we got a character or an error back from wait_for, then we can
272 break from the loop before the timeout is completed. */
274 if (status
!= SERIAL_TIMEOUT
)
279 /* If we have exhausted the original timeout, then generate
280 a SERIAL_TIMEOUT, and pass it out of the loop. */
282 else if (timeout
== 0)
284 status
== SERIAL_TIMEOUT
;
294 scb
->bufcnt
= read (scb
->fd
, scb
->buf
, BUFSIZ
);
295 if (scb
->bufcnt
!= -1 || errno
!= EINTR
)
299 if (scb
->bufcnt
<= 0)
301 if (scb
->bufcnt
== 0)
302 return SERIAL_TIMEOUT
; /* 0 chars means timeout [may need to
303 distinguish between EOF & timeouts
306 return SERIAL_ERROR
; /* Got an error from read */
310 scb
->bufp
= scb
->buf
;
315 tcp_noflush_set_tty_state (scb
, new_ttystate
, old_ttystate
)
317 serial_ttystate new_ttystate
;
318 serial_ttystate old_ttystate
;
324 tcp_print_tty_state (scb
, ttystate
)
326 serial_ttystate ttystate
;
328 /* Nothing to print. */
333 tcp_setbaudrate (scb
, rate
)
337 return 0; /* Never fails! */
341 tcp_setstopbits (scb
, num
)
345 return 0; /* Never fails! */
349 tcp_write (scb
, str
, len
)
358 cc
= write (scb
->fd
, str
, len
);
379 static struct serial_ops tcp_ops
=
387 tcp_return_0
, /* flush output */
388 tcp_return_0
, /* flush input */
389 tcp_return_0
, /* send break */
394 tcp_noflush_set_tty_state
,
397 tcp_return_0
, /* wait for output to drain */
401 _initialize_ser_tcp ()
403 serial_add_interface (&tcp_ops
);