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