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