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