* hppa-tdep.c (hppa_push_arguments): Allocate enough space for
[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));
41static int tcp_write PARAMS ((serial_t scb, const char *str, int len));
42static void tcp_restore PARAMS ((serial_t scb));
43static void tcp_close PARAMS ((serial_t scb));
44static serial_ttystate tcp_get_tty_state PARAMS ((serial_t scb));
45static int tcp_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
46
47/* Open up a raw tcp socket */
48
49static int
50tcp_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];
69b2f0fe 60 struct protoent *protoent;
38dc5e12
SG
61
62 port_str = strchr (name, ':');
63
64 if (!port_str)
65 error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */
66
a037b21e
SG
67 tmp = min (port_str - name, sizeof hostname - 1);
68 strncpy (hostname, name, tmp); /* Don't want colon */
69 hostname[tmp] = '\000'; /* Tie off host name */
38dc5e12
SG
70 port = atoi (port_str + 1);
71
72 hostent = gethostbyname (hostname);
73
74 if (!hostent)
75 {
a037b21e 76 fprintf (stderr, "%s: unknown host\n", hostname);
38dc5e12
SG
77 errno = ENOENT;
78 return -1;
79 }
80
81 scb->fd = socket (PF_INET, SOCK_STREAM, 0);
82 if (scb->fd < 0)
83 return -1;
84
85 /* Allow rapid reuse of this port. */
86 tmp = 1;
87 setsockopt (scb->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&tmp, sizeof(tmp));
88
89 /* Enable TCP keep alive process. */
90 tmp = 1;
91 setsockopt (scb->fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&tmp, sizeof(tmp));
92
93 sockaddr.sin_family = PF_INET;
94 sockaddr.sin_port = htons(port);
95 memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
96 sizeof (struct in_addr));
97
a037b21e 98 if (connect (scb->fd, &sockaddr, sizeof(sockaddr)))
38dc5e12
SG
99 {
100 close(scb->fd);
a037b21e 101 scb->fd = -1;
38dc5e12
SG
102 return -1;
103 }
104
69b2f0fe
SG
105 protoent = getprotobyname ("tcp");
106 if (!protoent)
107 return -1;
108
38dc5e12 109 tmp = 1;
69b2f0fe
SG
110 if (setsockopt (scb->fd, protoent->p_proto, TCP_NODELAY,
111 (char *)&tmp, sizeof(tmp)))
38dc5e12
SG
112 return -1;
113
114 signal(SIGPIPE, SIG_IGN); /* If we don't do this, then GDB simply exits
115 when the remote side dies. */
116
117 return 0;
118}
119
120static serial_ttystate
121tcp_get_tty_state(scb)
122 serial_t scb;
123{
124 struct tcp_ttystate *state;
125
126 state = (struct tcp_ttystate *)xmalloc(sizeof *state);
127
128 return (serial_ttystate)state;
129}
130
131static int
132tcp_set_tty_state(scb, ttystate)
133 serial_t scb;
134 serial_ttystate ttystate;
135{
136 struct tcp_ttystate *state;
137
138 state = (struct tcp_ttystate *)ttystate;
139
140 return 0;
141}
142
c2e247c4
JK
143static int
144tcp_flush_output (scb)
145 serial_t scb;
146{
147 /* This is only used by utils.c on stdout, so it doesn't need to work
148 for tcp. */
149 return 0;
150}
151
38dc5e12
SG
152static void
153tcp_raw(scb)
154 serial_t scb;
155{
156 return; /* Always in raw mode */
157}
158
159/* Wait for input on scb, with timeout seconds. Returns 0 on success,
160 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
161
162 For termio{s}, we actually just setup VTIME if necessary, and let the
163 timeout occur in the read() in tcp_read().
164 */
165
166static int
167wait_for(scb, timeout)
168 serial_t scb;
169 int timeout;
170{
171 int numfds;
172 struct timeval tv;
a037b21e 173 fd_set readfds, exceptfds;
38dc5e12
SG
174
175 FD_ZERO (&readfds);
a037b21e 176 FD_ZERO (&exceptfds);
38dc5e12
SG
177
178 tv.tv_sec = timeout;
179 tv.tv_usec = 0;
180
181 FD_SET(scb->fd, &readfds);
a037b21e 182 FD_SET(scb->fd, &exceptfds);
38dc5e12 183
a037b21e
SG
184 while (1)
185 {
186 if (timeout >= 0)
187 numfds = select(scb->fd+1, &readfds, 0, &exceptfds, &tv);
188 else
189 numfds = select(scb->fd+1, &readfds, 0, &exceptfds, 0);
190
191 if (numfds <= 0)
192 if (numfds == 0)
193 return SERIAL_TIMEOUT;
194 else if (errno == EINTR)
195 continue;
196 else
197 return SERIAL_ERROR; /* Got an error from select or poll */
198
199 return 0;
200 }
38dc5e12
SG
201}
202
203/* Read a character with user-specified timeout. TIMEOUT is number of seconds
204 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
205 char if successful. Returns -2 if timeout expired, EOF if line dropped
206 dead, or -3 for any other error (see errno in that case). */
207
208static int
209tcp_readchar(scb, timeout)
210 serial_t scb;
211 int timeout;
212{
213 int status;
214
215 if (scb->bufcnt-- > 0)
216 return *scb->bufp++;
217
218 status = wait_for(scb, timeout);
219
220 if (status < 0)
221 return status;
222
223 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
224
225 if (scb->bufcnt <= 0)
226 if (scb->bufcnt == 0)
227 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
228 distinguish between EOF & timeouts
229 someday] */
230 else
231 return SERIAL_ERROR; /* Got an error from read */
232
233 scb->bufcnt--;
234 scb->bufp = scb->buf;
235 return *scb->bufp++;
236}
237
c2e247c4
JK
238static int
239tcp_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
240 serial_t scb;
241 serial_ttystate new_ttystate;
242 serial_ttystate old_ttystate;
243{
244 return 0;
245}
246
247static void
248tcp_print_tty_state (scb, ttystate)
249 serial_t scb;
250 serial_ttystate ttystate;
251{
252 /* Nothing to print. */
253 return;
254}
255
38dc5e12
SG
256static int
257tcp_setbaudrate(scb, rate)
258 serial_t scb;
259 int rate;
260{
261 return 0; /* Never fails! */
262}
263
c2e247c4
JK
264static int
265tcp_set_process_group (scb, ttystate, group)
266 serial_t scb;
267 serial_ttystate ttystate;
268 int group;
269{
270 return 0;
271}
272
38dc5e12
SG
273static int
274tcp_write(scb, str, len)
275 serial_t scb;
276 const char *str;
277 int len;
278{
279 int cc;
280
281 while (len > 0)
282 {
283 cc = write(scb->fd, str, len);
284
285 if (cc < 0)
286 return 1;
287 len -= cc;
288 str += cc;
289 }
290 return 0;
291}
292
293static void
294tcp_close(scb)
295 serial_t scb;
296{
297 if (scb->fd < 0)
298 return;
299
300 close(scb->fd);
301 scb->fd = -1;
302}
303
304static struct serial_ops tcp_ops =
305{
306 "tcp",
307 0,
308 tcp_open,
309 tcp_close,
310 tcp_readchar,
311 tcp_write,
c2e247c4 312 tcp_flush_output,
38dc5e12
SG
313 tcp_raw,
314 tcp_get_tty_state,
315 tcp_set_tty_state,
c2e247c4
JK
316 tcp_print_tty_state,
317 tcp_noflush_set_tty_state,
38dc5e12 318 tcp_setbaudrate,
c2e247c4 319 tcp_set_process_group
38dc5e12
SG
320};
321
322void
323_initialize_ser_tcp ()
324{
325 serial_add_interface (&tcp_ops);
326}
This page took 0.045151 seconds and 4 git commands to generate.