* mdebugread.c (parse_symbol): Use new variable
[deliverable/binutils-gdb.git] / gdb / ser-tcp.c
CommitLineData
38dc5e12
SG
1/* Serial interface for raw TCP connections on Un*x like systems
2 Copyright 1992, 1993 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, 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
31struct tcp_ttystate
32{
33 int bogus;
34};
35
36static int tcp_open PARAMS ((serial_t scb, const char *name));
37static void tcp_raw PARAMS ((serial_t scb));
38static int wait_for PARAMS ((serial_t scb, int timeout));
39static int tcp_readchar PARAMS ((serial_t scb, int timeout));
40static int tcp_setbaudrate PARAMS ((serial_t scb, int rate));
85c8b135 41static int tcp_setstopbits PARAMS ((serial_t scb, int num));
38dc5e12 42static int tcp_write PARAMS ((serial_t scb, const char *str, int len));
0ac0a9f6 43/* FIXME: static void tcp_restore PARAMS ((serial_t scb)); */
38dc5e12
SG
44static void tcp_close PARAMS ((serial_t scb));
45static serial_ttystate tcp_get_tty_state PARAMS ((serial_t scb));
46static int tcp_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
47
48/* Open up a raw tcp socket */
49
50static int
51tcp_open(scb, name)
52 serial_t scb;
53 const char *name;
54{
55 char *port_str;
56 int port;
57 struct hostent *hostent;
58 struct sockaddr_in sockaddr;
59 int tmp;
60 char hostname[100];
69b2f0fe 61 struct protoent *protoent;
38dc5e12
SG
62
63 port_str = strchr (name, ':');
64
65 if (!port_str)
66 error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */
67
a037b21e
SG
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 */
38dc5e12
SG
71 port = atoi (port_str + 1);
72
73 hostent = gethostbyname (hostname);
74
75 if (!hostent)
76 {
199b2450 77 fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname);
38dc5e12
SG
78 errno = ENOENT;
79 return -1;
80 }
81
82 scb->fd = socket (PF_INET, SOCK_STREAM, 0);
83 if (scb->fd < 0)
84 return -1;
85
86 /* Allow rapid reuse of this port. */
87 tmp = 1;
88 setsockopt (scb->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&tmp, sizeof(tmp));
89
90 /* Enable TCP keep alive process. */
91 tmp = 1;
92 setsockopt (scb->fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&tmp, sizeof(tmp));
93
94 sockaddr.sin_family = PF_INET;
95 sockaddr.sin_port = htons(port);
96 memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
97 sizeof (struct in_addr));
98
ee6e2087 99 if (connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)))
38dc5e12
SG
100 {
101 close(scb->fd);
a037b21e 102 scb->fd = -1;
38dc5e12
SG
103 return -1;
104 }
105
69b2f0fe
SG
106 protoent = getprotobyname ("tcp");
107 if (!protoent)
108 return -1;
109
38dc5e12 110 tmp = 1;
69b2f0fe
SG
111 if (setsockopt (scb->fd, protoent->p_proto, TCP_NODELAY,
112 (char *)&tmp, sizeof(tmp)))
38dc5e12
SG
113 return -1;
114
115 signal(SIGPIPE, SIG_IGN); /* If we don't do this, then GDB simply exits
116 when the remote side dies. */
117
118 return 0;
119}
120
121static serial_ttystate
122tcp_get_tty_state(scb)
123 serial_t scb;
124{
125 struct tcp_ttystate *state;
126
127 state = (struct tcp_ttystate *)xmalloc(sizeof *state);
128
129 return (serial_ttystate)state;
130}
131
132static int
133tcp_set_tty_state(scb, ttystate)
134 serial_t scb;
135 serial_ttystate ttystate;
136{
137 struct tcp_ttystate *state;
138
139 state = (struct tcp_ttystate *)ttystate;
140
141 return 0;
142}
143
c2e247c4 144static int
704deef2 145tcp_return_0 (scb)
c2e247c4
JK
146 serial_t scb;
147{
c2e247c4
JK
148 return 0;
149}
150
38dc5e12
SG
151static void
152tcp_raw(scb)
153 serial_t scb;
154{
155 return; /* Always in raw mode */
156}
157
158/* Wait for input on scb, with timeout seconds. Returns 0 on success,
159 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
160
161 For termio{s}, we actually just setup VTIME if necessary, and let the
162 timeout occur in the read() in tcp_read().
163 */
164
165static int
166wait_for(scb, timeout)
167 serial_t scb;
168 int timeout;
169{
170 int numfds;
171 struct timeval tv;
a037b21e 172 fd_set readfds, exceptfds;
38dc5e12
SG
173
174 FD_ZERO (&readfds);
a037b21e 175 FD_ZERO (&exceptfds);
38dc5e12
SG
176
177 tv.tv_sec = timeout;
178 tv.tv_usec = 0;
179
180 FD_SET(scb->fd, &readfds);
a037b21e 181 FD_SET(scb->fd, &exceptfds);
38dc5e12 182
a037b21e
SG
183 while (1)
184 {
185 if (timeout >= 0)
186 numfds = select(scb->fd+1, &readfds, 0, &exceptfds, &tv);
187 else
188 numfds = select(scb->fd+1, &readfds, 0, &exceptfds, 0);
189
190 if (numfds <= 0)
191 if (numfds == 0)
192 return SERIAL_TIMEOUT;
193 else if (errno == EINTR)
194 continue;
195 else
196 return SERIAL_ERROR; /* Got an error from select or poll */
197
198 return 0;
199 }
38dc5e12
SG
200}
201
202/* Read a character with user-specified timeout. TIMEOUT is number of seconds
203 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
204 char if successful. Returns -2 if timeout expired, EOF if line dropped
205 dead, or -3 for any other error (see errno in that case). */
206
207static int
208tcp_readchar(scb, timeout)
209 serial_t scb;
210 int timeout;
211{
212 int status;
213
214 if (scb->bufcnt-- > 0)
215 return *scb->bufp++;
216
217 status = wait_for(scb, timeout);
218
219 if (status < 0)
220 return status;
221
2e6784a8
SG
222 while (1)
223 {
224 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
225 if (scb->bufcnt != -1 || errno != EINTR)
226 break;
227 }
38dc5e12
SG
228
229 if (scb->bufcnt <= 0)
230 if (scb->bufcnt == 0)
231 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
232 distinguish between EOF & timeouts
233 someday] */
234 else
235 return SERIAL_ERROR; /* Got an error from read */
236
237 scb->bufcnt--;
238 scb->bufp = scb->buf;
239 return *scb->bufp++;
240}
241
c2e247c4
JK
242static int
243tcp_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
244 serial_t scb;
245 serial_ttystate new_ttystate;
246 serial_ttystate old_ttystate;
247{
248 return 0;
249}
250
251static void
252tcp_print_tty_state (scb, ttystate)
253 serial_t scb;
254 serial_ttystate ttystate;
255{
256 /* Nothing to print. */
257 return;
258}
259
38dc5e12
SG
260static int
261tcp_setbaudrate(scb, rate)
262 serial_t scb;
263 int rate;
264{
265 return 0; /* Never fails! */
266}
267
85c8b135
SG
268static int
269tcp_setstopbits(scb, num)
270 serial_t scb;
271 int num;
272{
273 return 0; /* Never fails! */
274}
275
38dc5e12
SG
276static int
277tcp_write(scb, str, len)
278 serial_t scb;
279 const char *str;
280 int len;
281{
282 int cc;
283
284 while (len > 0)
285 {
286 cc = write(scb->fd, str, len);
287
288 if (cc < 0)
289 return 1;
290 len -= cc;
291 str += cc;
292 }
293 return 0;
294}
295
296static void
297tcp_close(scb)
298 serial_t scb;
299{
300 if (scb->fd < 0)
301 return;
302
303 close(scb->fd);
304 scb->fd = -1;
305}
306
307static struct serial_ops tcp_ops =
308{
309 "tcp",
310 0,
311 tcp_open,
312 tcp_close,
313 tcp_readchar,
314 tcp_write,
704deef2
JK
315 tcp_return_0, /* flush output */
316 tcp_return_0, /* flush input */
317 tcp_return_0, /* send break */
38dc5e12
SG
318 tcp_raw,
319 tcp_get_tty_state,
320 tcp_set_tty_state,
c2e247c4
JK
321 tcp_print_tty_state,
322 tcp_noflush_set_tty_state,
38dc5e12 323 tcp_setbaudrate,
85c8b135 324 tcp_setstopbits,
38dc5e12
SG
325};
326
327void
328_initialize_ser_tcp ()
329{
330 serial_add_interface (&tcp_ops);
331}
This page took 0.18502 seconds and 4 git commands to generate.