2005-05-22 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, len;
191 len = sizeof(err);
192 /* On Windows, the fourth parameter to getsockopt is a "char *";
193 on UNIX systems it is generally "void *". The cast to "void *"
194 is OK everywhere, since in C "void *" can be implicitly
195 converted to any pointer type. */
196 res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len);
197 if (res < 0 || err)
198 {
199 if (err)
200 errno = err;
201 net_close (scb);
202 return -1;
203 }
204 }
205
206 /* turn off nonblocking */
207 ioarg = 0;
208 ioctl (scb->fd, FIONBIO, &ioarg);
209
210 if (use_udp == 0)
211 {
212 /* Disable Nagle algorithm. Needed in some cases. */
213 tmp = 1;
214 setsockopt (scb->fd, IPPROTO_TCP, TCP_NODELAY,
215 (char *)&tmp, sizeof (tmp));
216 }
217
218 #ifdef SIGPIPE
219 /* If we don't do this, then GDB simply exits
220 when the remote side dies. */
221 signal (SIGPIPE, SIG_IGN);
222 #endif
223
224 return 0;
225 }
226
227 static void
228 net_close (struct serial *scb)
229 {
230 if (scb->fd < 0)
231 return;
232
233 close (scb->fd);
234 scb->fd = -1;
235 }
236
237 static int
238 net_read_prim (struct serial *scb, size_t count)
239 {
240 return recv (scb->fd, scb->buf, count, 0);
241 }
242
243 static int
244 net_write_prim (struct serial *scb, const void *buf, size_t count)
245 {
246 return send (scb->fd, buf, count, 0);
247 }
248
249 void
250 _initialize_ser_tcp (void)
251 {
252 struct serial_ops *ops;
253 #ifdef USE_WIN32API
254 WSADATA wsa_data;
255 if (WSAStartup (MAKEWORD (1, 0), &wsa_data) != 0)
256 /* WinSock is unavailable. */
257 return;
258 #endif
259 ops = XMALLOC (struct serial_ops);
260 memset (ops, 0, sizeof (struct serial_ops));
261 ops->name = "tcp";
262 ops->next = 0;
263 ops->open = net_open;
264 ops->close = net_close;
265 ops->readchar = ser_base_readchar;
266 ops->write = ser_base_write;
267 ops->flush_output = ser_base_flush_output;
268 ops->flush_input = ser_base_flush_input;
269 ops->send_break = ser_base_send_break;
270 ops->go_raw = ser_base_raw;
271 ops->get_tty_state = ser_base_get_tty_state;
272 ops->set_tty_state = ser_base_set_tty_state;
273 ops->print_tty_state = ser_base_print_tty_state;
274 ops->noflush_set_tty_state = ser_base_noflush_set_tty_state;
275 ops->setbaudrate = ser_base_setbaudrate;
276 ops->setstopbits = ser_base_setstopbits;
277 ops->drain_output = ser_base_drain_output;
278 ops->async = ser_base_async;
279 ops->read_prim = net_read_prim;
280 ops->write_prim = net_write_prim;
281 serial_add_interface (ops);
282 }
This page took 0.038961 seconds and 4 git commands to generate.