Removed superflous code.
[deliverable/binutils-gdb.git] / gdb / ser-tcp.c
CommitLineData
38dc5e12 1/* Serial interface for raw TCP connections on Un*x like systems
24418cfb 2 Copyright 1992, 1993, 1998 Free Software Foundation, Inc.
38dc5e12
SG
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
6c9638b4 18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
38dc5e12
SG
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>
94a42c63
GN
28
29#ifndef __CYGWIN32__
38dc5e12 30#include <netinet/tcp.h>
94a42c63
GN
31#endif
32
38dc5e12 33#include "signals.h"
b52cac6b 34#include "gdb_string.h"
38dc5e12
SG
35
36struct tcp_ttystate
37{
38 int bogus;
39};
40
41static int tcp_open PARAMS ((serial_t scb, const char *name));
42static void tcp_raw PARAMS ((serial_t scb));
43static int wait_for PARAMS ((serial_t scb, int timeout));
44static int tcp_readchar PARAMS ((serial_t scb, int timeout));
45static int tcp_setbaudrate PARAMS ((serial_t scb, int rate));
85c8b135 46static int tcp_setstopbits PARAMS ((serial_t scb, int num));
38dc5e12 47static int tcp_write PARAMS ((serial_t scb, const char *str, int len));
0ac0a9f6 48/* FIXME: static void tcp_restore PARAMS ((serial_t scb)); */
38dc5e12
SG
49static void tcp_close PARAMS ((serial_t scb));
50static serial_ttystate tcp_get_tty_state PARAMS ((serial_t scb));
51static int tcp_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
b607efe7
FF
52static int tcp_return_0 PARAMS ((serial_t));
53static int tcp_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate,
54 serial_ttystate));
55static void tcp_print_tty_state PARAMS ((serial_t, serial_ttystate));
38dc5e12 56
24418cfb
JM
57void _initialize_ser_tcp PARAMS ((void));
58
38dc5e12
SG
59/* Open up a raw tcp socket */
60
61static int
62tcp_open(scb, name)
63 serial_t scb;
64 const char *name;
65{
66 char *port_str;
67 int port;
68 struct hostent *hostent;
69 struct sockaddr_in sockaddr;
70 int tmp;
71 char hostname[100];
69b2f0fe 72 struct protoent *protoent;
5bdf05c7 73 int i;
38dc5e12
SG
74
75 port_str = strchr (name, ':');
76
77 if (!port_str)
78 error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */
79
b52cac6b 80 tmp = min (port_str - name, (int) sizeof hostname - 1);
a037b21e
SG
81 strncpy (hostname, name, tmp); /* Don't want colon */
82 hostname[tmp] = '\000'; /* Tie off host name */
38dc5e12
SG
83 port = atoi (port_str + 1);
84
85 hostent = gethostbyname (hostname);
86
87 if (!hostent)
88 {
199b2450 89 fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname);
38dc5e12
SG
90 errno = ENOENT;
91 return -1;
92 }
93
4887063b
SG
94 for (i = 1; i <= 15; i++)
95 {
96 scb->fd = socket (PF_INET, SOCK_STREAM, 0);
97 if (scb->fd < 0)
98 return -1;
38dc5e12 99
4887063b
SG
100 /* Allow rapid reuse of this port. */
101 tmp = 1;
102 setsockopt (scb->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&tmp, sizeof(tmp));
38dc5e12 103
4887063b
SG
104 /* Enable TCP keep alive process. */
105 tmp = 1;
106 setsockopt (scb->fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&tmp, sizeof(tmp));
38dc5e12 107
4887063b
SG
108 sockaddr.sin_family = PF_INET;
109 sockaddr.sin_port = htons(port);
110 memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
111 sizeof (struct in_addr));
38dc5e12 112
4887063b
SG
113 if (!connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)))
114 break;
115
116 close (scb->fd);
a037b21e 117 scb->fd = -1;
4887063b
SG
118
119/* We retry for ECONNREFUSED because that is often a temporary condition, which
120 happens when the server is being restarted. */
121
122 if (errno != ECONNREFUSED)
123 return -1;
124
125 sleep (1);
38dc5e12
SG
126 }
127
69b2f0fe
SG
128 protoent = getprotobyname ("tcp");
129 if (!protoent)
130 return -1;
131
38dc5e12 132 tmp = 1;
69b2f0fe
SG
133 if (setsockopt (scb->fd, protoent->p_proto, TCP_NODELAY,
134 (char *)&tmp, sizeof(tmp)))
38dc5e12
SG
135 return -1;
136
137 signal(SIGPIPE, SIG_IGN); /* If we don't do this, then GDB simply exits
138 when the remote side dies. */
139
140 return 0;
141}
142
143static serial_ttystate
144tcp_get_tty_state(scb)
145 serial_t scb;
146{
147 struct tcp_ttystate *state;
148
149 state = (struct tcp_ttystate *)xmalloc(sizeof *state);
150
151 return (serial_ttystate)state;
152}
153
154static int
155tcp_set_tty_state(scb, ttystate)
156 serial_t scb;
157 serial_ttystate ttystate;
158{
159 struct tcp_ttystate *state;
160
161 state = (struct tcp_ttystate *)ttystate;
162
163 return 0;
164}
165
c2e247c4 166static int
704deef2 167tcp_return_0 (scb)
c2e247c4
JK
168 serial_t scb;
169{
c2e247c4
JK
170 return 0;
171}
172
38dc5e12
SG
173static void
174tcp_raw(scb)
175 serial_t scb;
176{
177 return; /* Always in raw mode */
178}
179
180/* Wait for input on scb, with timeout seconds. Returns 0 on success,
181 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
182
183 For termio{s}, we actually just setup VTIME if necessary, and let the
184 timeout occur in the read() in tcp_read().
185 */
186
187static int
24418cfb 188wait_for (scb, timeout)
38dc5e12
SG
189 serial_t scb;
190 int timeout;
191{
192 int numfds;
193 struct timeval tv;
a037b21e 194 fd_set readfds, exceptfds;
38dc5e12
SG
195
196 FD_ZERO (&readfds);
a037b21e 197 FD_ZERO (&exceptfds);
38dc5e12
SG
198
199 tv.tv_sec = timeout;
200 tv.tv_usec = 0;
201
202 FD_SET(scb->fd, &readfds);
a037b21e 203 FD_SET(scb->fd, &exceptfds);
38dc5e12 204
a037b21e
SG
205 while (1)
206 {
207 if (timeout >= 0)
208 numfds = select(scb->fd+1, &readfds, 0, &exceptfds, &tv);
209 else
210 numfds = select(scb->fd+1, &readfds, 0, &exceptfds, 0);
211
212 if (numfds <= 0)
24418cfb
JM
213 {
214 if (numfds == 0)
215 return SERIAL_TIMEOUT;
216 else if (errno == EINTR)
217 continue;
218 else
219 return SERIAL_ERROR; /* Got an error from select or poll */
220 }
a037b21e
SG
221
222 return 0;
223 }
38dc5e12
SG
224}
225
226/* Read a character with user-specified timeout. TIMEOUT is number of seconds
227 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
228 char if successful. Returns -2 if timeout expired, EOF if line dropped
229 dead, or -3 for any other error (see errno in that case). */
230
231static int
24418cfb 232tcp_readchar (scb, timeout)
38dc5e12
SG
233 serial_t scb;
234 int timeout;
235{
236 int status;
237
238 if (scb->bufcnt-- > 0)
239 return *scb->bufp++;
240
241 status = wait_for(scb, timeout);
242
243 if (status < 0)
244 return status;
245
2e6784a8
SG
246 while (1)
247 {
248 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
249 if (scb->bufcnt != -1 || errno != EINTR)
250 break;
251 }
38dc5e12
SG
252
253 if (scb->bufcnt <= 0)
24418cfb
JM
254 {
255 if (scb->bufcnt == 0)
256 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
257 distinguish between EOF & timeouts
258 someday] */
259 else
260 return SERIAL_ERROR; /* Got an error from read */
261 }
38dc5e12
SG
262
263 scb->bufcnt--;
264 scb->bufp = scb->buf;
265 return *scb->bufp++;
266}
267
c2e247c4
JK
268static int
269tcp_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
270 serial_t scb;
271 serial_ttystate new_ttystate;
272 serial_ttystate old_ttystate;
273{
274 return 0;
275}
276
277static void
278tcp_print_tty_state (scb, ttystate)
279 serial_t scb;
280 serial_ttystate ttystate;
281{
282 /* Nothing to print. */
283 return;
284}
285
38dc5e12
SG
286static int
287tcp_setbaudrate(scb, rate)
288 serial_t scb;
289 int rate;
290{
291 return 0; /* Never fails! */
292}
293
85c8b135
SG
294static int
295tcp_setstopbits(scb, num)
296 serial_t scb;
297 int num;
298{
299 return 0; /* Never fails! */
300}
301
38dc5e12
SG
302static int
303tcp_write(scb, str, len)
304 serial_t scb;
305 const char *str;
306 int len;
307{
308 int cc;
309
310 while (len > 0)
311 {
312 cc = write(scb->fd, str, len);
313
314 if (cc < 0)
315 return 1;
316 len -= cc;
317 str += cc;
318 }
319 return 0;
320}
321
322static void
323tcp_close(scb)
324 serial_t scb;
325{
326 if (scb->fd < 0)
327 return;
328
329 close(scb->fd);
330 scb->fd = -1;
331}
332
333static struct serial_ops tcp_ops =
334{
335 "tcp",
336 0,
337 tcp_open,
338 tcp_close,
339 tcp_readchar,
340 tcp_write,
704deef2
JK
341 tcp_return_0, /* flush output */
342 tcp_return_0, /* flush input */
343 tcp_return_0, /* send break */
38dc5e12
SG
344 tcp_raw,
345 tcp_get_tty_state,
346 tcp_set_tty_state,
c2e247c4
JK
347 tcp_print_tty_state,
348 tcp_noflush_set_tty_state,
38dc5e12 349 tcp_setbaudrate,
85c8b135 350 tcp_setstopbits,
3ffbdf15 351 tcp_return_0, /* wait for output to drain */
38dc5e12
SG
352};
353
354void
355_initialize_ser_tcp ()
356{
357 serial_add_interface (&tcp_ops);
358}
This page took 0.318088 seconds and 4 git commands to generate.