Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Serial interface for raw TCP connections on Un*x like systems |
c2c6d25f | 2 | Copyright 1992, 1993, 1998-1999 Free Software Foundation, Inc. |
c906108c | 3 | |
c5aa993b | 4 | This file is part of GDB. |
c906108c | 5 | |
c5aa993b JM |
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. | |
c906108c | 10 | |
c5aa993b JM |
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. | |
c906108c | 15 | |
c5aa993b JM |
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, | |
19 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
20 | |
21 | #include "defs.h" | |
22 | #include "serial.h" | |
c2c6d25f JM |
23 | #include "ser-unix.h" |
24 | ||
c906108c SS |
25 | #include <sys/types.h> |
26 | #include <sys/time.h> | |
27 | #include <netinet/in.h> | |
28 | #include <arpa/inet.h> | |
29 | #include <netdb.h> | |
30 | #include <sys/socket.h> | |
c906108c SS |
31 | #ifndef __CYGWIN32__ |
32 | #include <netinet/tcp.h> | |
33 | #endif | |
34 | ||
35 | #include "signals.h" | |
36 | #include "gdb_string.h" | |
37 | ||
c2c6d25f JM |
38 | static int tcp_open (serial_t scb, const char *name); |
39 | static void tcp_close (serial_t scb); | |
40 | ||
41 | void _initialize_ser_tcp (void); | |
c906108c SS |
42 | |
43 | /* Open up a raw tcp socket */ | |
44 | ||
45 | static int | |
c2c6d25f | 46 | tcp_open (serial_t scb, const char *name) |
c906108c SS |
47 | { |
48 | char *port_str; | |
49 | int port; | |
50 | struct hostent *hostent; | |
51 | struct sockaddr_in sockaddr; | |
52 | int tmp; | |
53 | char hostname[100]; | |
54 | struct protoent *protoent; | |
55 | int i; | |
56 | ||
57 | port_str = strchr (name, ':'); | |
58 | ||
59 | if (!port_str) | |
c5aa993b | 60 | error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */ |
c906108c SS |
61 | |
62 | tmp = min (port_str - name, (int) sizeof hostname - 1); | |
c5aa993b | 63 | strncpy (hostname, name, tmp); /* Don't want colon */ |
c906108c SS |
64 | hostname[tmp] = '\000'; /* Tie off host name */ |
65 | port = atoi (port_str + 1); | |
66 | ||
67 | hostent = gethostbyname (hostname); | |
68 | ||
69 | if (!hostent) | |
70 | { | |
71 | fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname); | |
72 | errno = ENOENT; | |
73 | return -1; | |
74 | } | |
75 | ||
76 | for (i = 1; i <= 15; i++) | |
77 | { | |
78 | scb->fd = socket (PF_INET, SOCK_STREAM, 0); | |
79 | if (scb->fd < 0) | |
80 | return -1; | |
81 | ||
82 | /* Allow rapid reuse of this port. */ | |
83 | tmp = 1; | |
c5aa993b | 84 | setsockopt (scb->fd, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, sizeof (tmp)); |
c906108c SS |
85 | |
86 | /* Enable TCP keep alive process. */ | |
87 | tmp = 1; | |
c5aa993b | 88 | setsockopt (scb->fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp)); |
c906108c SS |
89 | |
90 | sockaddr.sin_family = PF_INET; | |
c5aa993b | 91 | sockaddr.sin_port = htons (port); |
c906108c SS |
92 | memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr, |
93 | sizeof (struct in_addr)); | |
94 | ||
c5aa993b | 95 | if (!connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr))) |
c906108c SS |
96 | break; |
97 | ||
98 | close (scb->fd); | |
99 | scb->fd = -1; | |
100 | ||
101 | /* We retry for ECONNREFUSED because that is often a temporary condition, which | |
102 | happens when the server is being restarted. */ | |
103 | ||
104 | if (errno != ECONNREFUSED) | |
105 | return -1; | |
106 | ||
107 | sleep (1); | |
108 | } | |
109 | ||
110 | protoent = getprotobyname ("tcp"); | |
111 | if (!protoent) | |
112 | return -1; | |
113 | ||
114 | tmp = 1; | |
115 | if (setsockopt (scb->fd, protoent->p_proto, TCP_NODELAY, | |
c5aa993b | 116 | (char *) &tmp, sizeof (tmp))) |
c906108c SS |
117 | return -1; |
118 | ||
c5aa993b | 119 | signal (SIGPIPE, SIG_IGN); /* If we don't do this, then GDB simply exits |
c906108c SS |
120 | when the remote side dies. */ |
121 | ||
122 | return 0; | |
123 | } | |
124 | ||
c906108c | 125 | static void |
c2c6d25f | 126 | tcp_close (serial_t scb) |
c906108c SS |
127 | { |
128 | if (scb->fd < 0) | |
129 | return; | |
130 | ||
c5aa993b | 131 | close (scb->fd); |
c906108c SS |
132 | scb->fd = -1; |
133 | } | |
134 | ||
c906108c | 135 | void |
c2c6d25f | 136 | _initialize_ser_tcp (void) |
c906108c | 137 | { |
c2c6d25f JM |
138 | struct serial_ops *ops = XMALLOC (struct serial_ops); |
139 | memset (ops, sizeof (struct serial_ops), 0); | |
140 | ops->name = "tcp"; | |
141 | ops->next = 0; | |
142 | ops->open = tcp_open; | |
143 | ops->close = tcp_close; | |
144 | ops->readchar = ser_unix_readchar; | |
145 | ops->write = ser_unix_write; | |
146 | ops->flush_output = ser_unix_nop_flush_output; | |
2acceee2 | 147 | ops->flush_input = ser_unix_flush_input; |
c2c6d25f JM |
148 | ops->send_break = ser_unix_nop_send_break; |
149 | ops->go_raw = ser_unix_nop_raw; | |
150 | ops->get_tty_state = ser_unix_nop_get_tty_state; | |
151 | ops->set_tty_state = ser_unix_nop_set_tty_state; | |
152 | ops->print_tty_state = ser_unix_nop_print_tty_state; | |
153 | ops->noflush_set_tty_state = ser_unix_nop_noflush_set_tty_state; | |
154 | ops->setbaudrate = ser_unix_nop_setbaudrate; | |
155 | ops->setstopbits = ser_unix_nop_setstopbits; | |
156 | ops->drain_output = ser_unix_nop_drain_output; | |
157 | ops->async = ser_unix_async; | |
158 | serial_add_interface (ops); | |
c906108c | 159 | } |