]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ser-tcp.c
import gdb-1999-07-07 post reformat
[thirdparty/binutils-gdb.git] / gdb / ser-tcp.c
CommitLineData
c906108c 1/* Serial interface for raw TCP connections on Un*x like systems
9846de1b 2 Copyright 1992, 1993, 1998 Free Software Foundation, Inc.
c906108c 3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
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.
c906108c 10
c5aa993b
JM
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.
c906108c 15
c5aa993b
JM
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,
19 Boston, MA 02111-1307, USA. */
c906108c
SS
20
21#include "defs.h"
22#include "serial.h"
23#include <sys/types.h>
24#include <sys/time.h>
25#include <netinet/in.h>
26#include <arpa/inet.h>
27#include <netdb.h>
28#include <sys/socket.h>
7a292a7a
SS
29#ifdef HAVE_UNISTD_H
30#include <unistd.h>
31#endif
c906108c
SS
32
33#ifndef __CYGWIN32__
34#include <netinet/tcp.h>
35#endif
36
37#include "signals.h"
38#include "gdb_string.h"
39
7a292a7a
SS
40extern int (*ui_loop_hook) PARAMS ((int));
41
c906108c 42struct tcp_ttystate
c5aa993b
JM
43 {
44 int bogus;
45 };
c906108c
SS
46
47static int tcp_open PARAMS ((serial_t scb, const char *name));
48static void tcp_raw PARAMS ((serial_t scb));
49static int wait_for PARAMS ((serial_t scb, int timeout));
50static int tcp_readchar PARAMS ((serial_t scb, int timeout));
51static int tcp_setbaudrate PARAMS ((serial_t scb, int rate));
52static int tcp_setstopbits PARAMS ((serial_t scb, int num));
53static int tcp_write PARAMS ((serial_t scb, const char *str, int len));
54/* FIXME: static void tcp_restore PARAMS ((serial_t scb)); */
55static void tcp_close PARAMS ((serial_t scb));
56static serial_ttystate tcp_get_tty_state PARAMS ((serial_t scb));
57static int tcp_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
58static int tcp_return_0 PARAMS ((serial_t));
59static int tcp_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate,
60 serial_ttystate));
61static void tcp_print_tty_state PARAMS ((serial_t, serial_ttystate));
62
63void _initialize_ser_tcp PARAMS ((void));
64
65/* Open up a raw tcp socket */
66
67static int
c5aa993b 68tcp_open (scb, name)
c906108c
SS
69 serial_t scb;
70 const char *name;
71{
72 char *port_str;
73 int port;
74 struct hostent *hostent;
75 struct sockaddr_in sockaddr;
76 int tmp;
77 char hostname[100];
78 struct protoent *protoent;
79 int i;
80
81 port_str = strchr (name, ':');
82
83 if (!port_str)
c5aa993b 84 error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */
c906108c
SS
85
86 tmp = min (port_str - name, (int) sizeof hostname - 1);
c5aa993b 87 strncpy (hostname, name, tmp); /* Don't want colon */
c906108c
SS
88 hostname[tmp] = '\000'; /* Tie off host name */
89 port = atoi (port_str + 1);
90
91 hostent = gethostbyname (hostname);
92
93 if (!hostent)
94 {
95 fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname);
96 errno = ENOENT;
97 return -1;
98 }
99
100 for (i = 1; i <= 15; i++)
101 {
102 scb->fd = socket (PF_INET, SOCK_STREAM, 0);
103 if (scb->fd < 0)
104 return -1;
105
106 /* Allow rapid reuse of this port. */
107 tmp = 1;
c5aa993b 108 setsockopt (scb->fd, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, sizeof (tmp));
c906108c
SS
109
110 /* Enable TCP keep alive process. */
111 tmp = 1;
c5aa993b 112 setsockopt (scb->fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
c906108c
SS
113
114 sockaddr.sin_family = PF_INET;
c5aa993b 115 sockaddr.sin_port = htons (port);
c906108c
SS
116 memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
117 sizeof (struct in_addr));
118
c5aa993b 119 if (!connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr)))
c906108c
SS
120 break;
121
122 close (scb->fd);
123 scb->fd = -1;
124
125/* We retry for ECONNREFUSED because that is often a temporary condition, which
126 happens when the server is being restarted. */
127
128 if (errno != ECONNREFUSED)
129 return -1;
130
131 sleep (1);
132 }
133
134 protoent = getprotobyname ("tcp");
135 if (!protoent)
136 return -1;
137
138 tmp = 1;
139 if (setsockopt (scb->fd, protoent->p_proto, TCP_NODELAY,
c5aa993b 140 (char *) &tmp, sizeof (tmp)))
c906108c
SS
141 return -1;
142
c5aa993b 143 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then GDB simply exits
c906108c
SS
144 when the remote side dies. */
145
146 return 0;
147}
148
149static serial_ttystate
c5aa993b 150tcp_get_tty_state (scb)
c906108c
SS
151 serial_t scb;
152{
153 struct tcp_ttystate *state;
154
c5aa993b 155 state = (struct tcp_ttystate *) xmalloc (sizeof *state);
c906108c 156
c5aa993b 157 return (serial_ttystate) state;
c906108c
SS
158}
159
160static int
c5aa993b 161tcp_set_tty_state (scb, ttystate)
c906108c
SS
162 serial_t scb;
163 serial_ttystate ttystate;
164{
165 struct tcp_ttystate *state;
166
c5aa993b 167 state = (struct tcp_ttystate *) ttystate;
c906108c
SS
168
169 return 0;
170}
171
172static int
173tcp_return_0 (scb)
174 serial_t scb;
175{
176 return 0;
177}
178
179static void
c5aa993b 180tcp_raw (scb)
c906108c
SS
181 serial_t scb;
182{
183 return; /* Always in raw mode */
184}
185
186/* Wait for input on scb, with timeout seconds. Returns 0 on success,
187 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
188
189 For termio{s}, we actually just setup VTIME if necessary, and let the
190 timeout occur in the read() in tcp_read().
191 */
192
193static int
194wait_for (scb, timeout)
195 serial_t scb;
196 int timeout;
197{
198 int numfds;
199 struct timeval tv;
200 fd_set readfds, exceptfds;
201
202 FD_ZERO (&readfds);
203 FD_ZERO (&exceptfds);
204
205 tv.tv_sec = timeout;
206 tv.tv_usec = 0;
207
c5aa993b
JM
208 FD_SET (scb->fd, &readfds);
209 FD_SET (scb->fd, &exceptfds);
c906108c
SS
210
211 while (1)
212 {
213 if (timeout >= 0)
c5aa993b 214 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
c906108c 215 else
c5aa993b 216 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
c906108c
SS
217
218 if (numfds <= 0)
c5aa993b 219 {
c906108c
SS
220 if (numfds == 0)
221 return SERIAL_TIMEOUT;
222 else if (errno == EINTR)
223 continue;
224 else
225 return SERIAL_ERROR; /* Got an error from select or poll */
c5aa993b 226 }
c906108c
SS
227
228 return 0;
229 }
230}
231
232/* Read a character with user-specified timeout. TIMEOUT is number of seconds
233 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
234 char if successful. Returns -2 if timeout expired, EOF if line dropped
235 dead, or -3 for any other error (see errno in that case). */
236
237static int
238tcp_readchar (scb, timeout)
239 serial_t scb;
240 int timeout;
241{
242 int status;
7a292a7a 243 int delta;
c906108c
SS
244
245 if (scb->bufcnt-- > 0)
246 return *scb->bufp++;
247
7a292a7a
SS
248 /* We have to be able to keep the GUI alive here, so we break the original
249 timeout into steps of 1 second, running the "keep the GUI alive" hook
250 each time through the loop.
251
252 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
253 will only go through the loop once. */
c5aa993b 254
7a292a7a
SS
255 delta = (timeout == 0 ? 0 : 1);
256 while (1)
257 {
258
259 /* N.B. The UI may destroy our world (for instance by calling
c5aa993b
JM
260 remote_stop,) in which case we want to get out of here as
261 quickly as possible. It is not safe to touch scb, since
262 someone else might have freed it. The ui_loop_hook signals that
263 we should exit by returning 1. */
7a292a7a
SS
264
265 if (ui_loop_hook)
266 {
267 if (ui_loop_hook (0))
268 return SERIAL_TIMEOUT;
269 }
270
271 status = wait_for (scb, delta);
272 timeout -= delta;
273
274 /* If we got a character or an error back from wait_for, then we can
275 break from the loop before the timeout is completed. */
c5aa993b 276
7a292a7a
SS
277 if (status != SERIAL_TIMEOUT)
278 {
279 break;
280 }
281
282 /* If we have exhausted the original timeout, then generate
c5aa993b
JM
283 a SERIAL_TIMEOUT, and pass it out of the loop. */
284
7a292a7a
SS
285 else if (timeout == 0)
286 {
287 status == SERIAL_TIMEOUT;
288 break;
289 }
290 }
c906108c
SS
291
292 if (status < 0)
293 return status;
294
295 while (1)
296 {
c5aa993b 297 scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ);
c906108c
SS
298 if (scb->bufcnt != -1 || errno != EINTR)
299 break;
300 }
301
302 if (scb->bufcnt <= 0)
303 {
304 if (scb->bufcnt == 0)
c5aa993b
JM
305 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
306 distinguish between EOF & timeouts
307 someday] */
c906108c 308 else
c5aa993b 309 return SERIAL_ERROR; /* Got an error from read */
c906108c
SS
310 }
311
312 scb->bufcnt--;
313 scb->bufp = scb->buf;
314 return *scb->bufp++;
315}
316
317static int
318tcp_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
319 serial_t scb;
320 serial_ttystate new_ttystate;
321 serial_ttystate old_ttystate;
322{
323 return 0;
324}
325
326static void
327tcp_print_tty_state (scb, ttystate)
328 serial_t scb;
329 serial_ttystate ttystate;
330{
331 /* Nothing to print. */
332 return;
333}
334
335static int
c5aa993b 336tcp_setbaudrate (scb, rate)
c906108c
SS
337 serial_t scb;
338 int rate;
339{
340 return 0; /* Never fails! */
341}
342
343static int
c5aa993b 344tcp_setstopbits (scb, num)
c906108c
SS
345 serial_t scb;
346 int num;
347{
348 return 0; /* Never fails! */
349}
350
351static int
c5aa993b 352tcp_write (scb, str, len)
c906108c
SS
353 serial_t scb;
354 const char *str;
355 int len;
356{
357 int cc;
358
359 while (len > 0)
360 {
c5aa993b 361 cc = write (scb->fd, str, len);
c906108c
SS
362
363 if (cc < 0)
364 return 1;
365 len -= cc;
366 str += cc;
367 }
368 return 0;
369}
370
371static void
c5aa993b 372tcp_close (scb)
c906108c
SS
373 serial_t scb;
374{
375 if (scb->fd < 0)
376 return;
377
c5aa993b 378 close (scb->fd);
c906108c
SS
379 scb->fd = -1;
380}
381
382static struct serial_ops tcp_ops =
383{
384 "tcp",
385 0,
386 tcp_open,
387 tcp_close,
388 tcp_readchar,
389 tcp_write,
c5aa993b
JM
390 tcp_return_0, /* flush output */
391 tcp_return_0, /* flush input */
392 tcp_return_0, /* send break */
c906108c
SS
393 tcp_raw,
394 tcp_get_tty_state,
395 tcp_set_tty_state,
396 tcp_print_tty_state,
397 tcp_noflush_set_tty_state,
398 tcp_setbaudrate,
399 tcp_setstopbits,
c5aa993b 400 tcp_return_0, /* wait for output to drain */
c906108c
SS
401};
402
403void
404_initialize_ser_tcp ()
405{
406 serial_add_interface (&tcp_ops);
407}