]>
Commit | Line | Data |
---|---|---|
f9f87d2c MK |
1 | /* Serial interface for raw TCP connections on Un*x like systems. |
2 | ||
197e01b6 | 3 | Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2001, 2005 |
b6ba6518 | 4 | Free Software Foundation, Inc. |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
8 | This program is free software; you can redistribute it and/or modify |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
c906108c | 12 | |
c5aa993b JM |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
c906108c | 17 | |
c5aa993b JM |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software | |
197e01b6 EZ |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | Boston, MA 02110-1301, USA. */ | |
c906108c SS |
22 | |
23 | #include "defs.h" | |
24 | #include "serial.h" | |
3eb25fda | 25 | #include "ser-base.h" |
c2c6d25f JM |
26 | #include "ser-unix.h" |
27 | ||
c906108c | 28 | #include <sys/types.h> |
0cf3e697 MH |
29 | |
30 | #ifdef HAVE_SYS_FILIO_H | |
31 | #include <sys/filio.h> /* For FIONBIO. */ | |
32 | #endif | |
33 | #ifdef HAVE_SYS_IOCTL_H | |
34 | #include <sys/ioctl.h> /* For FIONBIO. */ | |
35 | #endif | |
36 | ||
c906108c | 37 | #include <sys/time.h> |
b4505029 MM |
38 | |
39 | #ifdef USE_WIN32API | |
40 | #include <winsock2.h> | |
41 | #define ETIMEDOUT WSAETIMEDOUT | |
056d7646 | 42 | #define close(fd) closesocket (fd) |
b4505029 MM |
43 | #define ioctl ioctlsocket |
44 | #else | |
c906108c SS |
45 | #include <netinet/in.h> |
46 | #include <arpa/inet.h> | |
47 | #include <netdb.h> | |
48 | #include <sys/socket.h> | |
c906108c | 49 | #include <netinet/tcp.h> |
b4505029 | 50 | #endif |
c906108c | 51 | |
042be3a9 | 52 | #include <signal.h> |
c906108c SS |
53 | #include "gdb_string.h" |
54 | ||
f9f87d2c MK |
55 | #ifndef HAVE_SOCKLEN_T |
56 | typedef int socklen_t; | |
57 | #endif | |
58 | ||
9db8d71f DJ |
59 | static int net_open (struct serial *scb, const char *name); |
60 | static void net_close (struct serial *scb); | |
c2c6d25f | 61 | void _initialize_ser_tcp (void); |
c906108c | 62 | |
7c7a201a MH |
63 | /* seconds to wait for connect */ |
64 | #define TIMEOUT 15 | |
98bbd631 | 65 | /* how many times per second to poll deprecated_ui_loop_hook */ |
7c7a201a MH |
66 | #define POLL_INTERVAL 2 |
67 | ||
68 | /* Open a tcp socket */ | |
c906108c SS |
69 | |
70 | static int | |
9db8d71f | 71 | net_open (struct serial *scb, const char *name) |
c906108c | 72 | { |
7c7a201a MH |
73 | char *port_str, hostname[100]; |
74 | int n, port, tmp; | |
9db8d71f | 75 | int use_udp; |
c906108c SS |
76 | struct hostent *hostent; |
77 | struct sockaddr_in sockaddr; | |
b4505029 MM |
78 | #ifdef USE_WIN32API |
79 | u_long ioarg; | |
80 | #else | |
81 | int ioarg; | |
82 | #endif | |
c906108c | 83 | |
9db8d71f DJ |
84 | use_udp = 0; |
85 | if (strncmp (name, "udp:", 4) == 0) | |
86 | { | |
87 | use_udp = 1; | |
88 | name = name + 4; | |
89 | } | |
90 | else if (strncmp (name, "tcp:", 4) == 0) | |
91 | name = name + 4; | |
92 | ||
c906108c SS |
93 | port_str = strchr (name, ':'); |
94 | ||
95 | if (!port_str) | |
8a3fe4f8 | 96 | error (_("net_open: No colon in host name!")); /* Shouldn't ever happen */ |
c906108c SS |
97 | |
98 | tmp = min (port_str - name, (int) sizeof hostname - 1); | |
c5aa993b | 99 | strncpy (hostname, name, tmp); /* Don't want colon */ |
c906108c SS |
100 | hostname[tmp] = '\000'; /* Tie off host name */ |
101 | port = atoi (port_str + 1); | |
102 | ||
7c7a201a | 103 | /* default hostname is localhost */ |
ad4571f3 CV |
104 | if (!hostname[0]) |
105 | strcpy (hostname, "localhost"); | |
106 | ||
c906108c | 107 | hostent = gethostbyname (hostname); |
c906108c SS |
108 | if (!hostent) |
109 | { | |
110 | fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname); | |
111 | errno = ENOENT; | |
112 | return -1; | |
113 | } | |
114 | ||
9db8d71f DJ |
115 | if (use_udp) |
116 | scb->fd = socket (PF_INET, SOCK_DGRAM, 0); | |
117 | else | |
118 | scb->fd = socket (PF_INET, SOCK_STREAM, 0); | |
119 | ||
7c7a201a MH |
120 | if (scb->fd < 0) |
121 | return -1; | |
122 | ||
123 | sockaddr.sin_family = PF_INET; | |
124 | sockaddr.sin_port = htons (port); | |
125 | memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr, | |
126 | sizeof (struct in_addr)); | |
c906108c | 127 | |
7c7a201a | 128 | /* set socket nonblocking */ |
b4505029 MM |
129 | ioarg = 1; |
130 | ioctl (scb->fd, FIONBIO, &ioarg); | |
c906108c | 131 | |
7c7a201a MH |
132 | /* Use Non-blocking connect. connect() will return 0 if connected already. */ |
133 | n = connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr)); | |
c906108c | 134 | |
b4505029 MM |
135 | if (n < 0 |
136 | #ifdef USE_WIN32API | |
137 | /* Under Windows, calling "connect" with a non-blocking socket | |
138 | results in WSAEWOULDBLOCK, not WSAEINPROGRESS. */ | |
139 | && WSAGetLastError() != WSAEWOULDBLOCK | |
140 | #else | |
141 | && errno != EINPROGRESS | |
142 | #endif | |
143 | ) | |
7c7a201a | 144 | { |
b4505029 MM |
145 | #ifdef USE_WIN32API |
146 | errno = WSAGetLastError(); | |
147 | #endif | |
9db8d71f | 148 | net_close (scb); |
7c7a201a | 149 | return -1; |
c906108c SS |
150 | } |
151 | ||
7c7a201a MH |
152 | if (n) |
153 | { | |
154 | /* looks like we need to wait for the connect */ | |
155 | struct timeval t; | |
156 | fd_set rset, wset; | |
157 | int polls = 0; | |
158 | FD_ZERO (&rset); | |
159 | ||
160 | do | |
161 | { | |
2c1ab592 | 162 | /* While we wait for the connect to complete, |
7c7a201a | 163 | poll the UI so it can update or the user can |
2c1ab592 | 164 | interrupt. */ |
98bbd631 | 165 | if (deprecated_ui_loop_hook) |
7c7a201a | 166 | { |
98bbd631 | 167 | if (deprecated_ui_loop_hook (0)) |
7c7a201a MH |
168 | { |
169 | errno = EINTR; | |
9db8d71f | 170 | net_close (scb); |
7c7a201a MH |
171 | return -1; |
172 | } | |
173 | } | |
174 | ||
175 | FD_SET (scb->fd, &rset); | |
176 | wset = rset; | |
177 | t.tv_sec = 0; | |
178 | t.tv_usec = 1000000 / POLL_INTERVAL; | |
179 | ||
180 | n = select (scb->fd + 1, &rset, &wset, NULL, &t); | |
181 | polls++; | |
182 | } | |
183 | while (n == 0 && polls <= TIMEOUT * POLL_INTERVAL); | |
184 | if (n < 0 || polls > TIMEOUT * POLL_INTERVAL) | |
185 | { | |
186 | if (polls > TIMEOUT * POLL_INTERVAL) | |
187 | errno = ETIMEDOUT; | |
9db8d71f | 188 | net_close (scb); |
7c7a201a MH |
189 | return -1; |
190 | } | |
191 | } | |
c906108c | 192 | |
7c7a201a MH |
193 | /* Got something. Is it an error? */ |
194 | { | |
47b667de AC |
195 | int res, err; |
196 | socklen_t len; | |
7c7a201a | 197 | len = sizeof(err); |
b4505029 MM |
198 | /* On Windows, the fourth parameter to getsockopt is a "char *"; |
199 | on UNIX systems it is generally "void *". The cast to "void *" | |
200 | is OK everywhere, since in C "void *" can be implicitly | |
201 | converted to any pointer type. */ | |
202 | res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len); | |
7c7a201a MH |
203 | if (res < 0 || err) |
204 | { | |
205 | if (err) | |
206 | errno = err; | |
9db8d71f | 207 | net_close (scb); |
7c7a201a MH |
208 | return -1; |
209 | } | |
210 | } | |
9db8d71f | 211 | |
7c7a201a | 212 | /* turn off nonblocking */ |
b4505029 MM |
213 | ioarg = 0; |
214 | ioctl (scb->fd, FIONBIO, &ioarg); | |
7c7a201a | 215 | |
9db8d71f DJ |
216 | if (use_udp == 0) |
217 | { | |
218 | /* Disable Nagle algorithm. Needed in some cases. */ | |
219 | tmp = 1; | |
220 | setsockopt (scb->fd, IPPROTO_TCP, TCP_NODELAY, | |
221 | (char *)&tmp, sizeof (tmp)); | |
222 | } | |
223 | ||
6d318c73 | 224 | #ifdef SIGPIPE |
7c7a201a MH |
225 | /* If we don't do this, then GDB simply exits |
226 | when the remote side dies. */ | |
227 | signal (SIGPIPE, SIG_IGN); | |
6d318c73 | 228 | #endif |
c906108c SS |
229 | |
230 | return 0; | |
231 | } | |
232 | ||
c906108c | 233 | static void |
9db8d71f | 234 | net_close (struct serial *scb) |
c906108c SS |
235 | { |
236 | if (scb->fd < 0) | |
237 | return; | |
238 | ||
c5aa993b | 239 | close (scb->fd); |
c906108c SS |
240 | scb->fd = -1; |
241 | } | |
242 | ||
b4505029 MM |
243 | static int |
244 | net_read_prim (struct serial *scb, size_t count) | |
245 | { | |
246 | return recv (scb->fd, scb->buf, count, 0); | |
247 | } | |
248 | ||
249 | static int | |
250 | net_write_prim (struct serial *scb, const void *buf, size_t count) | |
251 | { | |
252 | return send (scb->fd, buf, count, 0); | |
253 | } | |
254 | ||
c906108c | 255 | void |
c2c6d25f | 256 | _initialize_ser_tcp (void) |
c906108c | 257 | { |
b4505029 MM |
258 | struct serial_ops *ops; |
259 | #ifdef USE_WIN32API | |
260 | WSADATA wsa_data; | |
261 | if (WSAStartup (MAKEWORD (1, 0), &wsa_data) != 0) | |
262 | /* WinSock is unavailable. */ | |
263 | return; | |
264 | #endif | |
265 | ops = XMALLOC (struct serial_ops); | |
2fdbdd39 | 266 | memset (ops, 0, sizeof (struct serial_ops)); |
c2c6d25f JM |
267 | ops->name = "tcp"; |
268 | ops->next = 0; | |
9db8d71f DJ |
269 | ops->open = net_open; |
270 | ops->close = net_close; | |
b4505029 | 271 | ops->readchar = ser_base_readchar; |
dd5da072 MM |
272 | ops->write = ser_base_write; |
273 | ops->flush_output = ser_base_flush_output; | |
274 | ops->flush_input = ser_base_flush_input; | |
275 | ops->send_break = ser_base_send_break; | |
276 | ops->go_raw = ser_base_raw; | |
277 | ops->get_tty_state = ser_base_get_tty_state; | |
278 | ops->set_tty_state = ser_base_set_tty_state; | |
279 | ops->print_tty_state = ser_base_print_tty_state; | |
280 | ops->noflush_set_tty_state = ser_base_noflush_set_tty_state; | |
281 | ops->setbaudrate = ser_base_setbaudrate; | |
282 | ops->setstopbits = ser_base_setstopbits; | |
283 | ops->drain_output = ser_base_drain_output; | |
284 | ops->async = ser_base_async; | |
b4505029 MM |
285 | ops->read_prim = net_read_prim; |
286 | ops->write_prim = net_write_prim; | |
c2c6d25f | 287 | serial_add_interface (ops); |
c906108c | 288 | } |