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