| 1 | /* Serial interface for raw TCP connections on Un*x like systems |
| 2 | Copyright 1992, 1993, 1998 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 | #ifdef HAVE_UNISTD_H |
| 29 | #include <unistd.h> |
| 30 | #endif |
| 31 | |
| 32 | #ifndef __CYGWIN32__ |
| 33 | #include <netinet/tcp.h> |
| 34 | #endif |
| 35 | |
| 36 | #include "signals.h" |
| 37 | #include "gdb_string.h" |
| 38 | |
| 39 | extern int (*ui_loop_hook) PARAMS ((int)); |
| 40 | |
| 41 | struct tcp_ttystate |
| 42 | { |
| 43 | int bogus; |
| 44 | }; |
| 45 | |
| 46 | static int tcp_open PARAMS ((serial_t scb, const char *name)); |
| 47 | static void tcp_raw PARAMS ((serial_t scb)); |
| 48 | static int wait_for PARAMS ((serial_t scb, int timeout)); |
| 49 | static int tcp_readchar PARAMS ((serial_t scb, int timeout)); |
| 50 | static int tcp_setbaudrate PARAMS ((serial_t scb, int rate)); |
| 51 | static int tcp_setstopbits PARAMS ((serial_t scb, int num)); |
| 52 | static int tcp_write PARAMS ((serial_t scb, const char *str, int len)); |
| 53 | /* FIXME: static void tcp_restore PARAMS ((serial_t scb)); */ |
| 54 | static void tcp_close PARAMS ((serial_t scb)); |
| 55 | static serial_ttystate tcp_get_tty_state PARAMS ((serial_t scb)); |
| 56 | static int tcp_set_tty_state PARAMS ((serial_t scb, serial_ttystate state)); |
| 57 | static int tcp_return_0 PARAMS ((serial_t)); |
| 58 | static int tcp_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate, |
| 59 | serial_ttystate)); |
| 60 | static void tcp_print_tty_state PARAMS ((serial_t, serial_ttystate)); |
| 61 | |
| 62 | void _initialize_ser_tcp PARAMS ((void)); |
| 63 | |
| 64 | /* Open up a raw tcp socket */ |
| 65 | |
| 66 | static int |
| 67 | tcp_open(scb, name) |
| 68 | serial_t scb; |
| 69 | const char *name; |
| 70 | { |
| 71 | char *port_str; |
| 72 | int port; |
| 73 | struct hostent *hostent; |
| 74 | struct sockaddr_in sockaddr; |
| 75 | int tmp; |
| 76 | char hostname[100]; |
| 77 | struct protoent *protoent; |
| 78 | int i; |
| 79 | |
| 80 | port_str = strchr (name, ':'); |
| 81 | |
| 82 | if (!port_str) |
| 83 | error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */ |
| 84 | |
| 85 | tmp = min (port_str - name, (int) sizeof hostname - 1); |
| 86 | strncpy (hostname, name, tmp); /* Don't want colon */ |
| 87 | hostname[tmp] = '\000'; /* Tie off host name */ |
| 88 | port = atoi (port_str + 1); |
| 89 | |
| 90 | hostent = gethostbyname (hostname); |
| 91 | |
| 92 | if (!hostent) |
| 93 | { |
| 94 | fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname); |
| 95 | errno = ENOENT; |
| 96 | return -1; |
| 97 | } |
| 98 | |
| 99 | for (i = 1; i <= 15; i++) |
| 100 | { |
| 101 | scb->fd = socket (PF_INET, SOCK_STREAM, 0); |
| 102 | if (scb->fd < 0) |
| 103 | return -1; |
| 104 | |
| 105 | /* Allow rapid reuse of this port. */ |
| 106 | tmp = 1; |
| 107 | setsockopt (scb->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&tmp, sizeof(tmp)); |
| 108 | |
| 109 | /* Enable TCP keep alive process. */ |
| 110 | tmp = 1; |
| 111 | setsockopt (scb->fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&tmp, sizeof(tmp)); |
| 112 | |
| 113 | sockaddr.sin_family = PF_INET; |
| 114 | sockaddr.sin_port = htons(port); |
| 115 | memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr, |
| 116 | sizeof (struct in_addr)); |
| 117 | |
| 118 | if (!connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr))) |
| 119 | break; |
| 120 | |
| 121 | close (scb->fd); |
| 122 | scb->fd = -1; |
| 123 | |
| 124 | /* We retry for ECONNREFUSED because that is often a temporary condition, which |
| 125 | happens when the server is being restarted. */ |
| 126 | |
| 127 | if (errno != ECONNREFUSED) |
| 128 | return -1; |
| 129 | |
| 130 | sleep (1); |
| 131 | } |
| 132 | |
| 133 | protoent = getprotobyname ("tcp"); |
| 134 | if (!protoent) |
| 135 | return -1; |
| 136 | |
| 137 | tmp = 1; |
| 138 | if (setsockopt (scb->fd, protoent->p_proto, TCP_NODELAY, |
| 139 | (char *)&tmp, sizeof(tmp))) |
| 140 | return -1; |
| 141 | |
| 142 | signal(SIGPIPE, SIG_IGN); /* If we don't do this, then GDB simply exits |
| 143 | when the remote side dies. */ |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static serial_ttystate |
| 149 | tcp_get_tty_state(scb) |
| 150 | serial_t scb; |
| 151 | { |
| 152 | struct tcp_ttystate *state; |
| 153 | |
| 154 | state = (struct tcp_ttystate *)xmalloc(sizeof *state); |
| 155 | |
| 156 | return (serial_ttystate)state; |
| 157 | } |
| 158 | |
| 159 | static int |
| 160 | tcp_set_tty_state(scb, ttystate) |
| 161 | serial_t scb; |
| 162 | serial_ttystate ttystate; |
| 163 | { |
| 164 | struct tcp_ttystate *state; |
| 165 | |
| 166 | state = (struct tcp_ttystate *)ttystate; |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static int |
| 172 | tcp_return_0 (scb) |
| 173 | serial_t scb; |
| 174 | { |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static void |
| 179 | tcp_raw(scb) |
| 180 | serial_t scb; |
| 181 | { |
| 182 | return; /* Always in raw mode */ |
| 183 | } |
| 184 | |
| 185 | /* Wait for input on scb, with timeout seconds. Returns 0 on success, |
| 186 | otherwise SERIAL_TIMEOUT or SERIAL_ERROR. |
| 187 | |
| 188 | For termio{s}, we actually just setup VTIME if necessary, and let the |
| 189 | timeout occur in the read() in tcp_read(). |
| 190 | */ |
| 191 | |
| 192 | static int |
| 193 | wait_for (scb, timeout) |
| 194 | serial_t scb; |
| 195 | int timeout; |
| 196 | { |
| 197 | int numfds; |
| 198 | struct timeval tv; |
| 199 | fd_set readfds, exceptfds; |
| 200 | |
| 201 | FD_ZERO (&readfds); |
| 202 | FD_ZERO (&exceptfds); |
| 203 | |
| 204 | tv.tv_sec = timeout; |
| 205 | tv.tv_usec = 0; |
| 206 | |
| 207 | FD_SET(scb->fd, &readfds); |
| 208 | FD_SET(scb->fd, &exceptfds); |
| 209 | |
| 210 | while (1) |
| 211 | { |
| 212 | if (timeout >= 0) |
| 213 | numfds = select(scb->fd+1, &readfds, 0, &exceptfds, &tv); |
| 214 | else |
| 215 | numfds = select(scb->fd+1, &readfds, 0, &exceptfds, 0); |
| 216 | |
| 217 | if (numfds <= 0) |
| 218 | { |
| 219 | if (numfds == 0) |
| 220 | return SERIAL_TIMEOUT; |
| 221 | else if (errno == EINTR) |
| 222 | continue; |
| 223 | else |
| 224 | return SERIAL_ERROR; /* Got an error from select or poll */ |
| 225 | } |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /* Read a character with user-specified timeout. TIMEOUT is number of seconds |
| 232 | to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns |
| 233 | char if successful. Returns -2 if timeout expired, EOF if line dropped |
| 234 | dead, or -3 for any other error (see errno in that case). */ |
| 235 | |
| 236 | static int |
| 237 | tcp_readchar (scb, timeout) |
| 238 | serial_t scb; |
| 239 | int timeout; |
| 240 | { |
| 241 | int status; |
| 242 | int delta; |
| 243 | |
| 244 | if (scb->bufcnt-- > 0) |
| 245 | return *scb->bufp++; |
| 246 | |
| 247 | /* We have to be able to keep the GUI alive here, so we break the original |
| 248 | timeout into steps of 1 second, running the "keep the GUI alive" hook |
| 249 | each time through the loop. |
| 250 | |
| 251 | Also, timeout = 0 means to poll, so we just set the delta to 0, so we |
| 252 | will only go through the loop once. */ |
| 253 | |
| 254 | delta = (timeout == 0 ? 0 : 1); |
| 255 | while (1) |
| 256 | { |
| 257 | |
| 258 | /* N.B. The UI may destroy our world (for instance by calling |
| 259 | remote_stop,) in which case we want to get out of here as |
| 260 | quickly as possible. It is not safe to touch scb, since |
| 261 | someone else might have freed it. The ui_loop_hook signals that |
| 262 | we should exit by returning 1. */ |
| 263 | |
| 264 | if (ui_loop_hook) |
| 265 | { |
| 266 | if (ui_loop_hook (0)) |
| 267 | return SERIAL_TIMEOUT; |
| 268 | } |
| 269 | |
| 270 | status = wait_for (scb, delta); |
| 271 | timeout -= delta; |
| 272 | |
| 273 | /* If we got a character or an error back from wait_for, then we can |
| 274 | break from the loop before the timeout is completed. */ |
| 275 | |
| 276 | if (status != SERIAL_TIMEOUT) |
| 277 | { |
| 278 | break; |
| 279 | } |
| 280 | |
| 281 | /* If we have exhausted the original timeout, then generate |
| 282 | a SERIAL_TIMEOUT, and pass it out of the loop. */ |
| 283 | |
| 284 | else if (timeout == 0) |
| 285 | { |
| 286 | status == SERIAL_TIMEOUT; |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | if (status < 0) |
| 292 | return status; |
| 293 | |
| 294 | while (1) |
| 295 | { |
| 296 | scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ); |
| 297 | if (scb->bufcnt != -1 || errno != EINTR) |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | if (scb->bufcnt <= 0) |
| 302 | { |
| 303 | if (scb->bufcnt == 0) |
| 304 | return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to |
| 305 | distinguish between EOF & timeouts |
| 306 | someday] */ |
| 307 | else |
| 308 | return SERIAL_ERROR; /* Got an error from read */ |
| 309 | } |
| 310 | |
| 311 | scb->bufcnt--; |
| 312 | scb->bufp = scb->buf; |
| 313 | return *scb->bufp++; |
| 314 | } |
| 315 | |
| 316 | static int |
| 317 | tcp_noflush_set_tty_state (scb, new_ttystate, old_ttystate) |
| 318 | serial_t scb; |
| 319 | serial_ttystate new_ttystate; |
| 320 | serial_ttystate old_ttystate; |
| 321 | { |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static void |
| 326 | tcp_print_tty_state (scb, ttystate) |
| 327 | serial_t scb; |
| 328 | serial_ttystate ttystate; |
| 329 | { |
| 330 | /* Nothing to print. */ |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | static int |
| 335 | tcp_setbaudrate(scb, rate) |
| 336 | serial_t scb; |
| 337 | int rate; |
| 338 | { |
| 339 | return 0; /* Never fails! */ |
| 340 | } |
| 341 | |
| 342 | static int |
| 343 | tcp_setstopbits(scb, num) |
| 344 | serial_t scb; |
| 345 | int num; |
| 346 | { |
| 347 | return 0; /* Never fails! */ |
| 348 | } |
| 349 | |
| 350 | static int |
| 351 | tcp_write(scb, str, len) |
| 352 | serial_t scb; |
| 353 | const char *str; |
| 354 | int len; |
| 355 | { |
| 356 | int cc; |
| 357 | |
| 358 | while (len > 0) |
| 359 | { |
| 360 | cc = write(scb->fd, str, len); |
| 361 | |
| 362 | if (cc < 0) |
| 363 | return 1; |
| 364 | len -= cc; |
| 365 | str += cc; |
| 366 | } |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static void |
| 371 | tcp_close(scb) |
| 372 | serial_t scb; |
| 373 | { |
| 374 | if (scb->fd < 0) |
| 375 | return; |
| 376 | |
| 377 | close(scb->fd); |
| 378 | scb->fd = -1; |
| 379 | } |
| 380 | |
| 381 | static struct serial_ops tcp_ops = |
| 382 | { |
| 383 | "tcp", |
| 384 | 0, |
| 385 | tcp_open, |
| 386 | tcp_close, |
| 387 | tcp_readchar, |
| 388 | tcp_write, |
| 389 | tcp_return_0, /* flush output */ |
| 390 | tcp_return_0, /* flush input */ |
| 391 | tcp_return_0, /* send break */ |
| 392 | tcp_raw, |
| 393 | tcp_get_tty_state, |
| 394 | tcp_set_tty_state, |
| 395 | tcp_print_tty_state, |
| 396 | tcp_noflush_set_tty_state, |
| 397 | tcp_setbaudrate, |
| 398 | tcp_setstopbits, |
| 399 | tcp_return_0, /* wait for output to drain */ |
| 400 | }; |
| 401 | |
| 402 | void |
| 403 | _initialize_ser_tcp () |
| 404 | { |
| 405 | serial_add_interface (&tcp_ops); |
| 406 | } |