4f4fcdbc2c382a9b4fa08dfbe23ad0953ff19bed
[binutils-gdb.git] / gdb / ser-tcp.c
1 /* Serial interface for raw TCP connections on Un*x like systems
2 Copyright 1992, 1993 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
19
20 #include "defs.h"
21 #include "serial.h"
22 #include <sys/types.h>
23 #include <sys/time.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <netdb.h>
27 #include <sys/socket.h>
28 #include <netinet/tcp.h>
29 #include "signals.h"
30
31 struct tcp_ttystate
32 {
33 int bogus;
34 };
35
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));
46
47 /* Open up a raw tcp socket */
48
49 static int
50 tcp_open(scb, name)
51 serial_t scb;
52 const char *name;
53 {
54 char *port_str;
55 int port;
56 struct hostent *hostent;
57 struct sockaddr_in sockaddr;
58 int tmp;
59 char hostname[100];
60
61 port_str = strchr (name, ':');
62
63 if (!port_str)
64 error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */
65
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);
69
70 hostent = gethostbyname (hostname);
71
72 if (!hostent)
73 {
74 errno = ENOENT;
75 return -1;
76 }
77
78 scb->fd = socket (PF_INET, SOCK_STREAM, 0);
79 if (scb->fd < 0)
80 return -1;
81
82 /* Allow rapid reuse of this port. */
83 tmp = 1;
84 setsockopt (scb->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&tmp, sizeof(tmp));
85
86 /* Enable TCP keep alive process. */
87 tmp = 1;
88 setsockopt (scb->fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&tmp, sizeof(tmp));
89
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));
94
95 if (connect(scb->fd, &sockaddr, sizeof(sockaddr)))
96 {
97 close(scb->fd);
98 return -1;
99 }
100
101 tmp = 1;
102 if (setsockopt (scb->fd, 6, TCP_NODELAY, (char *)&tmp, sizeof(tmp)))
103 return -1;
104
105 signal(SIGPIPE, SIG_IGN); /* If we don't do this, then GDB simply exits
106 when the remote side dies. */
107
108 return 0;
109 }
110
111 static serial_ttystate
112 tcp_get_tty_state(scb)
113 serial_t scb;
114 {
115 struct tcp_ttystate *state;
116
117 state = (struct tcp_ttystate *)xmalloc(sizeof *state);
118
119 return (serial_ttystate)state;
120 }
121
122 static int
123 tcp_set_tty_state(scb, ttystate)
124 serial_t scb;
125 serial_ttystate ttystate;
126 {
127 struct tcp_ttystate *state;
128
129 state = (struct tcp_ttystate *)ttystate;
130
131 return 0;
132 }
133
134 static void
135 tcp_raw(scb)
136 serial_t scb;
137 {
138 return; /* Always in raw mode */
139 }
140
141 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
142 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
143
144 For termio{s}, we actually just setup VTIME if necessary, and let the
145 timeout occur in the read() in tcp_read().
146 */
147
148 static int
149 wait_for(scb, timeout)
150 serial_t scb;
151 int timeout;
152 {
153 int numfds;
154 struct timeval tv;
155 fd_set readfds;
156
157 FD_ZERO (&readfds);
158
159 tv.tv_sec = timeout;
160 tv.tv_usec = 0;
161
162 FD_SET(scb->fd, &readfds);
163
164 if (timeout >= 0)
165 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
166 else
167 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
168
169 if (numfds <= 0)
170 if (numfds == 0)
171 return SERIAL_TIMEOUT;
172 else
173 return SERIAL_ERROR; /* Got an error from select or poll */
174
175 return 0;
176 }
177
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). */
182
183 static int
184 tcp_readchar(scb, timeout)
185 serial_t scb;
186 int timeout;
187 {
188 int status;
189
190 if (scb->bufcnt-- > 0)
191 return *scb->bufp++;
192
193 status = wait_for(scb, timeout);
194
195 if (status < 0)
196 return status;
197
198 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
199
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
204 someday] */
205 else
206 return SERIAL_ERROR; /* Got an error from read */
207
208 scb->bufcnt--;
209 scb->bufp = scb->buf;
210 return *scb->bufp++;
211 }
212
213 static int
214 tcp_setbaudrate(scb, rate)
215 serial_t scb;
216 int rate;
217 {
218 return 0; /* Never fails! */
219 }
220
221 static int
222 tcp_write(scb, str, len)
223 serial_t scb;
224 const char *str;
225 int len;
226 {
227 int cc;
228
229 while (len > 0)
230 {
231 cc = write(scb->fd, str, len);
232
233 if (cc < 0)
234 return 1;
235 len -= cc;
236 str += cc;
237 }
238 return 0;
239 }
240
241 static void
242 tcp_close(scb)
243 serial_t scb;
244 {
245 if (scb->fd < 0)
246 return;
247
248 close(scb->fd);
249 scb->fd = -1;
250 }
251
252 static struct serial_ops tcp_ops =
253 {
254 "tcp",
255 0,
256 tcp_open,
257 tcp_close,
258 tcp_readchar,
259 tcp_write,
260 tcp_raw,
261 tcp_get_tty_state,
262 tcp_set_tty_state,
263 tcp_setbaudrate,
264 };
265
266 void
267 _initialize_ser_tcp ()
268 {
269 serial_add_interface (&tcp_ops);
270 }