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>
30 #include <netinet/tcp.h>
34 #include "gdb_string.h"
41 static int tcp_open
PARAMS ((serial_t scb
, const char *name
));
42 static void tcp_raw
PARAMS ((serial_t scb
));
43 static int wait_for
PARAMS ((serial_t scb
, int timeout
));
44 static int tcp_readchar
PARAMS ((serial_t scb
, int timeout
));
45 static int tcp_setbaudrate
PARAMS ((serial_t scb
, int rate
));
46 static int tcp_setstopbits
PARAMS ((serial_t scb
, int num
));
47 static int tcp_write
PARAMS ((serial_t scb
, const char *str
, int len
));
48 /* FIXME: static void tcp_restore PARAMS ((serial_t scb)); */
49 static void tcp_close
PARAMS ((serial_t scb
));
50 static serial_ttystate tcp_get_tty_state
PARAMS ((serial_t scb
));
51 static int tcp_set_tty_state
PARAMS ((serial_t scb
, serial_ttystate state
));
52 static int tcp_return_0
PARAMS ((serial_t
));
53 static int tcp_noflush_set_tty_state
PARAMS ((serial_t
, serial_ttystate
,
55 static void tcp_print_tty_state
PARAMS ((serial_t
, serial_ttystate
));
57 void _initialize_ser_tcp
PARAMS ((void));
59 /* Open up a raw tcp socket */
68 struct hostent
*hostent
;
69 struct sockaddr_in sockaddr
;
72 struct protoent
*protoent
;
75 port_str
= strchr (name
, ':');
78 error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */
80 tmp
= min (port_str
- name
, (int) sizeof hostname
- 1);
81 strncpy (hostname
, name
, tmp
); /* Don't want colon */
82 hostname
[tmp
] = '\000'; /* Tie off host name */
83 port
= atoi (port_str
+ 1);
85 hostent
= gethostbyname (hostname
);
89 fprintf_unfiltered (gdb_stderr
, "%s: unknown host\n", hostname
);
94 for (i
= 1; i
<= 15; i
++)
96 scb
->fd
= socket (PF_INET
, SOCK_STREAM
, 0);
100 /* Allow rapid reuse of this port. */
102 setsockopt (scb
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&tmp
, sizeof(tmp
));
104 /* Enable TCP keep alive process. */
106 setsockopt (scb
->fd
, SOL_SOCKET
, SO_KEEPALIVE
, (char *)&tmp
, sizeof(tmp
));
108 sockaddr
.sin_family
= PF_INET
;
109 sockaddr
.sin_port
= htons(port
);
110 memcpy (&sockaddr
.sin_addr
.s_addr
, hostent
->h_addr
,
111 sizeof (struct in_addr
));
113 if (!connect (scb
->fd
, (struct sockaddr
*) &sockaddr
, sizeof(sockaddr
)))
119 /* We retry for ECONNREFUSED because that is often a temporary condition, which
120 happens when the server is being restarted. */
122 if (errno
!= ECONNREFUSED
)
128 protoent
= getprotobyname ("tcp");
133 if (setsockopt (scb
->fd
, protoent
->p_proto
, TCP_NODELAY
,
134 (char *)&tmp
, sizeof(tmp
)))
137 signal(SIGPIPE
, SIG_IGN
); /* If we don't do this, then GDB simply exits
138 when the remote side dies. */
143 static serial_ttystate
144 tcp_get_tty_state(scb
)
147 struct tcp_ttystate
*state
;
149 state
= (struct tcp_ttystate
*)xmalloc(sizeof *state
);
151 return (serial_ttystate
)state
;
155 tcp_set_tty_state(scb
, ttystate
)
157 serial_ttystate ttystate
;
159 struct tcp_ttystate
*state
;
161 state
= (struct tcp_ttystate
*)ttystate
;
177 return; /* Always in raw mode */
180 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
181 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
183 For termio{s}, we actually just setup VTIME if necessary, and let the
184 timeout occur in the read() in tcp_read().
188 wait_for (scb
, timeout
)
194 fd_set readfds
, exceptfds
;
197 FD_ZERO (&exceptfds
);
202 FD_SET(scb
->fd
, &readfds
);
203 FD_SET(scb
->fd
, &exceptfds
);
208 numfds
= select(scb
->fd
+1, &readfds
, 0, &exceptfds
, &tv
);
210 numfds
= select(scb
->fd
+1, &readfds
, 0, &exceptfds
, 0);
215 return SERIAL_TIMEOUT
;
216 else if (errno
== EINTR
)
219 return SERIAL_ERROR
; /* Got an error from select or poll */
226 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
227 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
228 char if successful. Returns -2 if timeout expired, EOF if line dropped
229 dead, or -3 for any other error (see errno in that case). */
232 tcp_readchar (scb
, timeout
)
238 if (scb
->bufcnt
-- > 0)
241 status
= wait_for(scb
, timeout
);
248 scb
->bufcnt
= read(scb
->fd
, scb
->buf
, BUFSIZ
);
249 if (scb
->bufcnt
!= -1 || errno
!= EINTR
)
253 if (scb
->bufcnt
<= 0)
255 if (scb
->bufcnt
== 0)
256 return SERIAL_TIMEOUT
; /* 0 chars means timeout [may need to
257 distinguish between EOF & timeouts
260 return SERIAL_ERROR
; /* Got an error from read */
264 scb
->bufp
= scb
->buf
;
269 tcp_noflush_set_tty_state (scb
, new_ttystate
, old_ttystate
)
271 serial_ttystate new_ttystate
;
272 serial_ttystate old_ttystate
;
278 tcp_print_tty_state (scb
, ttystate
)
280 serial_ttystate ttystate
;
282 /* Nothing to print. */
287 tcp_setbaudrate(scb
, rate
)
291 return 0; /* Never fails! */
295 tcp_setstopbits(scb
, num
)
299 return 0; /* Never fails! */
303 tcp_write(scb
, str
, len
)
312 cc
= write(scb
->fd
, str
, len
);
333 static struct serial_ops tcp_ops
=
341 tcp_return_0
, /* flush output */
342 tcp_return_0
, /* flush input */
343 tcp_return_0
, /* send break */
348 tcp_noflush_set_tty_state
,
351 tcp_return_0
, /* wait for output to drain */
355 _initialize_ser_tcp ()
357 serial_add_interface (&tcp_ops
);