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