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