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_write
PARAMS ((serial_t scb
, const char *str
, int len
));
42 static void tcp_restore
PARAMS ((serial_t scb
));
43 static void tcp_close
PARAMS ((serial_t scb
));
44 static serial_ttystate tcp_get_tty_state
PARAMS ((serial_t scb
));
45 static int tcp_set_tty_state
PARAMS ((serial_t scb
, serial_ttystate state
));
47 /* Open up a raw tcp socket */
56 struct hostent
*hostent
;
57 struct sockaddr_in sockaddr
;
61 port_str
= strchr (name
, ':');
64 error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */
66 tmp
= min(port_str
- name
+ 1, sizeof hostname
);
67 strncpy (hostname
, name
, tmp
- 1); /* Don't want colon */
68 port
= atoi (port_str
+ 1);
70 hostent
= gethostbyname (hostname
);
78 scb
->fd
= socket (PF_INET
, SOCK_STREAM
, 0);
82 /* Allow rapid reuse of this port. */
84 setsockopt (scb
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&tmp
, sizeof(tmp
));
86 /* Enable TCP keep alive process. */
88 setsockopt (scb
->fd
, SOL_SOCKET
, SO_KEEPALIVE
, (char *)&tmp
, sizeof(tmp
));
90 sockaddr
.sin_family
= PF_INET
;
91 sockaddr
.sin_port
= htons(port
);
92 memcpy (&sockaddr
.sin_addr
.s_addr
, hostent
->h_addr
,
93 sizeof (struct in_addr
));
95 if (connect(scb
->fd
, &sockaddr
, sizeof(sockaddr
)))
102 if (setsockopt (scb
->fd
, 6, TCP_NODELAY
, (char *)&tmp
, sizeof(tmp
)))
105 signal(SIGPIPE
, SIG_IGN
); /* If we don't do this, then GDB simply exits
106 when the remote side dies. */
111 static serial_ttystate
112 tcp_get_tty_state(scb
)
115 struct tcp_ttystate
*state
;
117 state
= (struct tcp_ttystate
*)xmalloc(sizeof *state
);
119 return (serial_ttystate
)state
;
123 tcp_set_tty_state(scb
, ttystate
)
125 serial_ttystate ttystate
;
127 struct tcp_ttystate
*state
;
129 state
= (struct tcp_ttystate
*)ttystate
;
138 return; /* Always in raw mode */
141 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
142 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
144 For termio{s}, we actually just setup VTIME if necessary, and let the
145 timeout occur in the read() in tcp_read().
149 wait_for(scb
, timeout
)
162 FD_SET(scb
->fd
, &readfds
);
165 numfds
= select(scb
->fd
+1, &readfds
, 0, 0, &tv
);
167 numfds
= select(scb
->fd
+1, &readfds
, 0, 0, 0);
171 return SERIAL_TIMEOUT
;
173 return SERIAL_ERROR
; /* Got an error from select or poll */
178 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
179 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
180 char if successful. Returns -2 if timeout expired, EOF if line dropped
181 dead, or -3 for any other error (see errno in that case). */
184 tcp_readchar(scb
, timeout
)
190 if (scb
->bufcnt
-- > 0)
193 status
= wait_for(scb
, timeout
);
198 scb
->bufcnt
= read(scb
->fd
, scb
->buf
, BUFSIZ
);
200 if (scb
->bufcnt
<= 0)
201 if (scb
->bufcnt
== 0)
202 return SERIAL_TIMEOUT
; /* 0 chars means timeout [may need to
203 distinguish between EOF & timeouts
206 return SERIAL_ERROR
; /* Got an error from read */
209 scb
->bufp
= scb
->buf
;
214 tcp_setbaudrate(scb
, rate
)
218 return 0; /* Never fails! */
222 tcp_write(scb
, str
, len
)
231 cc
= write(scb
->fd
, str
, len
);
252 static struct serial_ops tcp_ops
=
267 _initialize_ser_tcp ()
269 serial_add_interface (&tcp_ops
);