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