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., 675 Mass Ave, Cambridge, MA 02139, 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>
36 static int tcp_open
PARAMS ((serial_t scb
, const char *name
));
37 static void tcp_raw
PARAMS ((serial_t scb
));
38 static int wait_for
PARAMS ((serial_t scb
, int timeout
));
39 static int tcp_readchar
PARAMS ((serial_t scb
, int timeout
));
40 static int tcp_setbaudrate
PARAMS ((serial_t scb
, int rate
));
41 static int tcp_setstopbits
PARAMS ((serial_t scb
, int num
));
42 static int tcp_write
PARAMS ((serial_t scb
, const char *str
, int len
));
43 /* FIXME: static void tcp_restore PARAMS ((serial_t scb)); */
44 static void tcp_close
PARAMS ((serial_t scb
));
45 static serial_ttystate tcp_get_tty_state
PARAMS ((serial_t scb
));
46 static int tcp_set_tty_state
PARAMS ((serial_t scb
, serial_ttystate state
));
48 /* Open up a raw tcp socket */
57 struct hostent
*hostent
;
58 struct sockaddr_in sockaddr
;
61 struct protoent
*protoent
;
63 port_str
= strchr (name
, ':');
66 error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */
68 tmp
= min (port_str
- name
, sizeof hostname
- 1);
69 strncpy (hostname
, name
, tmp
); /* Don't want colon */
70 hostname
[tmp
] = '\000'; /* Tie off host name */
71 port
= atoi (port_str
+ 1);
73 hostent
= gethostbyname (hostname
);
77 fprintf_unfiltered (gdb_stderr
, "%s: unknown host\n", hostname
);
82 for (i
= 1; i
<= 15; i
++)
84 scb
->fd
= socket (PF_INET
, SOCK_STREAM
, 0);
88 /* Allow rapid reuse of this port. */
90 setsockopt (scb
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&tmp
, sizeof(tmp
));
92 /* Enable TCP keep alive process. */
94 setsockopt (scb
->fd
, SOL_SOCKET
, SO_KEEPALIVE
, (char *)&tmp
, sizeof(tmp
));
96 sockaddr
.sin_family
= PF_INET
;
97 sockaddr
.sin_port
= htons(port
);
98 memcpy (&sockaddr
.sin_addr
.s_addr
, hostent
->h_addr
,
99 sizeof (struct in_addr
));
101 if (!connect (scb
->fd
, (struct sockaddr
*) &sockaddr
, sizeof(sockaddr
)))
107 /* We retry for ECONNREFUSED because that is often a temporary condition, which
108 happens when the server is being restarted. */
110 if (errno
!= ECONNREFUSED
)
116 protoent
= getprotobyname ("tcp");
121 if (setsockopt (scb
->fd
, protoent
->p_proto
, TCP_NODELAY
,
122 (char *)&tmp
, sizeof(tmp
)))
125 signal(SIGPIPE
, SIG_IGN
); /* If we don't do this, then GDB simply exits
126 when the remote side dies. */
131 static serial_ttystate
132 tcp_get_tty_state(scb
)
135 struct tcp_ttystate
*state
;
137 state
= (struct tcp_ttystate
*)xmalloc(sizeof *state
);
139 return (serial_ttystate
)state
;
143 tcp_set_tty_state(scb
, ttystate
)
145 serial_ttystate ttystate
;
147 struct tcp_ttystate
*state
;
149 state
= (struct tcp_ttystate
*)ttystate
;
165 return; /* Always in raw mode */
168 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
169 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
171 For termio{s}, we actually just setup VTIME if necessary, and let the
172 timeout occur in the read() in tcp_read().
176 wait_for(scb
, timeout
)
182 fd_set readfds
, exceptfds
;
185 FD_ZERO (&exceptfds
);
190 FD_SET(scb
->fd
, &readfds
);
191 FD_SET(scb
->fd
, &exceptfds
);
196 numfds
= select(scb
->fd
+1, &readfds
, 0, &exceptfds
, &tv
);
198 numfds
= select(scb
->fd
+1, &readfds
, 0, &exceptfds
, 0);
202 return SERIAL_TIMEOUT
;
203 else if (errno
== EINTR
)
206 return SERIAL_ERROR
; /* Got an error from select or poll */
212 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
213 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
214 char if successful. Returns -2 if timeout expired, EOF if line dropped
215 dead, or -3 for any other error (see errno in that case). */
218 tcp_readchar(scb
, timeout
)
224 if (scb
->bufcnt
-- > 0)
227 status
= wait_for(scb
, timeout
);
234 scb
->bufcnt
= read(scb
->fd
, scb
->buf
, BUFSIZ
);
235 if (scb
->bufcnt
!= -1 || errno
!= EINTR
)
239 if (scb
->bufcnt
<= 0)
240 if (scb
->bufcnt
== 0)
241 return SERIAL_TIMEOUT
; /* 0 chars means timeout [may need to
242 distinguish between EOF & timeouts
245 return SERIAL_ERROR
; /* Got an error from read */
248 scb
->bufp
= scb
->buf
;
253 tcp_noflush_set_tty_state (scb
, new_ttystate
, old_ttystate
)
255 serial_ttystate new_ttystate
;
256 serial_ttystate old_ttystate
;
262 tcp_print_tty_state (scb
, ttystate
)
264 serial_ttystate ttystate
;
266 /* Nothing to print. */
271 tcp_setbaudrate(scb
, rate
)
275 return 0; /* Never fails! */
279 tcp_setstopbits(scb
, num
)
283 return 0; /* Never fails! */
287 tcp_write(scb
, str
, len
)
296 cc
= write(scb
->fd
, str
, len
);
317 static struct serial_ops tcp_ops
=
325 tcp_return_0
, /* flush output */
326 tcp_return_0
, /* flush input */
327 tcp_return_0
, /* send break */
332 tcp_noflush_set_tty_state
,
338 _initialize_ser_tcp ()
340 serial_add_interface (&tcp_ops
);