1 /* Serial interface for raw TCP connections on Un*x like systems
2 Copyright 1992, 1993 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>
28 #include <netinet/tcp.h>
30 #include "gdb_string.h"
37 static int tcp_open
PARAMS ((serial_t scb
, const char *name
));
38 static void tcp_raw
PARAMS ((serial_t scb
));
39 static int wait_for
PARAMS ((serial_t scb
, int timeout
));
40 static int tcp_readchar
PARAMS ((serial_t scb
, int timeout
));
41 static int tcp_setbaudrate
PARAMS ((serial_t scb
, int rate
));
42 static int tcp_setstopbits
PARAMS ((serial_t scb
, int num
));
43 static int tcp_write
PARAMS ((serial_t scb
, const char *str
, int len
));
44 /* FIXME: static void tcp_restore PARAMS ((serial_t scb)); */
45 static void tcp_close
PARAMS ((serial_t scb
));
46 static serial_ttystate tcp_get_tty_state
PARAMS ((serial_t scb
));
47 static int tcp_set_tty_state
PARAMS ((serial_t scb
, serial_ttystate state
));
49 /* Open up a raw tcp socket */
58 struct hostent
*hostent
;
59 struct sockaddr_in sockaddr
;
62 struct protoent
*protoent
;
65 port_str
= strchr (name
, ':');
68 error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */
70 tmp
= min (port_str
- name
, (int) sizeof hostname
- 1);
71 strncpy (hostname
, name
, tmp
); /* Don't want colon */
72 hostname
[tmp
] = '\000'; /* Tie off host name */
73 port
= atoi (port_str
+ 1);
75 hostent
= gethostbyname (hostname
);
79 fprintf_unfiltered (gdb_stderr
, "%s: unknown host\n", hostname
);
84 for (i
= 1; i
<= 15; i
++)
86 scb
->fd
= socket (PF_INET
, SOCK_STREAM
, 0);
90 /* Allow rapid reuse of this port. */
92 setsockopt (scb
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&tmp
, sizeof(tmp
));
94 /* Enable TCP keep alive process. */
96 setsockopt (scb
->fd
, SOL_SOCKET
, SO_KEEPALIVE
, (char *)&tmp
, sizeof(tmp
));
98 sockaddr
.sin_family
= PF_INET
;
99 sockaddr
.sin_port
= htons(port
);
100 memcpy (&sockaddr
.sin_addr
.s_addr
, hostent
->h_addr
,
101 sizeof (struct in_addr
));
103 if (!connect (scb
->fd
, (struct sockaddr
*) &sockaddr
, sizeof(sockaddr
)))
109 /* We retry for ECONNREFUSED because that is often a temporary condition, which
110 happens when the server is being restarted. */
112 if (errno
!= ECONNREFUSED
)
118 protoent
= getprotobyname ("tcp");
123 if (setsockopt (scb
->fd
, protoent
->p_proto
, TCP_NODELAY
,
124 (char *)&tmp
, sizeof(tmp
)))
127 signal(SIGPIPE
, SIG_IGN
); /* If we don't do this, then GDB simply exits
128 when the remote side dies. */
133 static serial_ttystate
134 tcp_get_tty_state(scb
)
137 struct tcp_ttystate
*state
;
139 state
= (struct tcp_ttystate
*)xmalloc(sizeof *state
);
141 return (serial_ttystate
)state
;
145 tcp_set_tty_state(scb
, ttystate
)
147 serial_ttystate ttystate
;
149 struct tcp_ttystate
*state
;
151 state
= (struct tcp_ttystate
*)ttystate
;
167 return; /* Always in raw mode */
170 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
171 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
173 For termio{s}, we actually just setup VTIME if necessary, and let the
174 timeout occur in the read() in tcp_read().
178 wait_for(scb
, timeout
)
184 fd_set readfds
, exceptfds
;
187 FD_ZERO (&exceptfds
);
192 FD_SET(scb
->fd
, &readfds
);
193 FD_SET(scb
->fd
, &exceptfds
);
198 numfds
= select(scb
->fd
+1, &readfds
, 0, &exceptfds
, &tv
);
200 numfds
= select(scb
->fd
+1, &readfds
, 0, &exceptfds
, 0);
204 return SERIAL_TIMEOUT
;
205 else if (errno
== EINTR
)
208 return SERIAL_ERROR
; /* Got an error from select or poll */
214 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
215 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
216 char if successful. Returns -2 if timeout expired, EOF if line dropped
217 dead, or -3 for any other error (see errno in that case). */
220 tcp_readchar(scb
, timeout
)
226 if (scb
->bufcnt
-- > 0)
229 status
= wait_for(scb
, timeout
);
236 scb
->bufcnt
= read(scb
->fd
, scb
->buf
, BUFSIZ
);
237 if (scb
->bufcnt
!= -1 || errno
!= EINTR
)
241 if (scb
->bufcnt
<= 0)
242 if (scb
->bufcnt
== 0)
243 return SERIAL_TIMEOUT
; /* 0 chars means timeout [may need to
244 distinguish between EOF & timeouts
247 return SERIAL_ERROR
; /* Got an error from read */
250 scb
->bufp
= scb
->buf
;
255 tcp_noflush_set_tty_state (scb
, new_ttystate
, old_ttystate
)
257 serial_ttystate new_ttystate
;
258 serial_ttystate old_ttystate
;
264 tcp_print_tty_state (scb
, ttystate
)
266 serial_ttystate ttystate
;
268 /* Nothing to print. */
273 tcp_setbaudrate(scb
, rate
)
277 return 0; /* Never fails! */
281 tcp_setstopbits(scb
, num
)
285 return 0; /* Never fails! */
289 tcp_write(scb
, str
, len
)
298 cc
= write(scb
->fd
, str
, len
);
319 static struct serial_ops tcp_ops
=
327 tcp_return_0
, /* flush output */
328 tcp_return_0
, /* flush input */
329 tcp_return_0
, /* send break */
334 tcp_noflush_set_tty_state
,
340 _initialize_ser_tcp ()
342 serial_add_interface (&tcp_ops
);