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>
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 /* Open up a raw tcp socket */
66 struct hostent
*hostent
;
67 struct sockaddr_in sockaddr
;
70 struct protoent
*protoent
;
73 port_str
= strchr (name
, ':');
76 error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */
78 tmp
= min (port_str
- name
, (int) sizeof hostname
- 1);
79 strncpy (hostname
, name
, tmp
); /* Don't want colon */
80 hostname
[tmp
] = '\000'; /* Tie off host name */
81 port
= atoi (port_str
+ 1);
83 hostent
= gethostbyname (hostname
);
87 fprintf_unfiltered (gdb_stderr
, "%s: unknown host\n", hostname
);
92 for (i
= 1; i
<= 15; i
++)
94 scb
->fd
= socket (PF_INET
, SOCK_STREAM
, 0);
98 /* Allow rapid reuse of this port. */
100 setsockopt (scb
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&tmp
, sizeof(tmp
));
102 /* Enable TCP keep alive process. */
104 setsockopt (scb
->fd
, SOL_SOCKET
, SO_KEEPALIVE
, (char *)&tmp
, sizeof(tmp
));
106 sockaddr
.sin_family
= PF_INET
;
107 sockaddr
.sin_port
= htons(port
);
108 memcpy (&sockaddr
.sin_addr
.s_addr
, hostent
->h_addr
,
109 sizeof (struct in_addr
));
111 if (!connect (scb
->fd
, (struct sockaddr
*) &sockaddr
, sizeof(sockaddr
)))
117 /* We retry for ECONNREFUSED because that is often a temporary condition, which
118 happens when the server is being restarted. */
120 if (errno
!= ECONNREFUSED
)
126 protoent
= getprotobyname ("tcp");
131 if (setsockopt (scb
->fd
, protoent
->p_proto
, TCP_NODELAY
,
132 (char *)&tmp
, sizeof(tmp
)))
135 signal(SIGPIPE
, SIG_IGN
); /* If we don't do this, then GDB simply exits
136 when the remote side dies. */
141 static serial_ttystate
142 tcp_get_tty_state(scb
)
145 struct tcp_ttystate
*state
;
147 state
= (struct tcp_ttystate
*)xmalloc(sizeof *state
);
149 return (serial_ttystate
)state
;
153 tcp_set_tty_state(scb
, ttystate
)
155 serial_ttystate ttystate
;
157 struct tcp_ttystate
*state
;
159 state
= (struct tcp_ttystate
*)ttystate
;
175 return; /* Always in raw mode */
178 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
179 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
181 For termio{s}, we actually just setup VTIME if necessary, and let the
182 timeout occur in the read() in tcp_read().
186 wait_for(scb
, timeout
)
192 fd_set readfds
, exceptfds
;
195 FD_ZERO (&exceptfds
);
200 FD_SET(scb
->fd
, &readfds
);
201 FD_SET(scb
->fd
, &exceptfds
);
206 numfds
= select(scb
->fd
+1, &readfds
, 0, &exceptfds
, &tv
);
208 numfds
= select(scb
->fd
+1, &readfds
, 0, &exceptfds
, 0);
212 return SERIAL_TIMEOUT
;
213 else if (errno
== EINTR
)
216 return SERIAL_ERROR
; /* Got an error from select or poll */
222 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
223 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
224 char if successful. Returns -2 if timeout expired, EOF if line dropped
225 dead, or -3 for any other error (see errno in that case). */
228 tcp_readchar(scb
, timeout
)
234 if (scb
->bufcnt
-- > 0)
237 status
= wait_for(scb
, timeout
);
244 scb
->bufcnt
= read(scb
->fd
, scb
->buf
, BUFSIZ
);
245 if (scb
->bufcnt
!= -1 || errno
!= EINTR
)
249 if (scb
->bufcnt
<= 0)
250 if (scb
->bufcnt
== 0)
251 return SERIAL_TIMEOUT
; /* 0 chars means timeout [may need to
252 distinguish between EOF & timeouts
255 return SERIAL_ERROR
; /* Got an error from read */
258 scb
->bufp
= scb
->buf
;
263 tcp_noflush_set_tty_state (scb
, new_ttystate
, old_ttystate
)
265 serial_ttystate new_ttystate
;
266 serial_ttystate old_ttystate
;
272 tcp_print_tty_state (scb
, ttystate
)
274 serial_ttystate ttystate
;
276 /* Nothing to print. */
281 tcp_setbaudrate(scb
, rate
)
285 return 0; /* Never fails! */
289 tcp_setstopbits(scb
, num
)
293 return 0; /* Never fails! */
297 tcp_write(scb
, str
, len
)
306 cc
= write(scb
->fd
, str
, len
);
327 static struct serial_ops tcp_ops
=
335 tcp_return_0
, /* flush output */
336 tcp_return_0
, /* flush input */
337 tcp_return_0
, /* send break */
342 tcp_noflush_set_tty_state
,
345 tcp_return_0
, /* wait for output to drain */
349 _initialize_ser_tcp ()
351 serial_add_interface (&tcp_ops
);