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