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.
5 This file is part of GDB.
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.
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.
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. */
27 #include <sys/types.h>
29 #ifdef HAVE_SYS_FILIO_H
30 #include <sys/filio.h> /* For FIONBIO. */
32 #ifdef HAVE_SYS_IOCTL_H
33 #include <sys/ioctl.h> /* For FIONBIO. */
40 #define ETIMEDOUT WSAETIMEDOUT
41 #define close closesocket
42 #define ioctl ioctlsocket
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
47 #include <sys/socket.h>
48 #include <netinet/tcp.h>
52 #include "gdb_string.h"
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);
58 /* seconds to wait for connect */
60 /* how many times per second to poll deprecated_ui_loop_hook */
61 #define POLL_INTERVAL 2
63 /* Open a tcp socket */
66 net_open (struct serial
*scb
, const char *name
)
68 char *port_str
, hostname
[100];
71 struct hostent
*hostent
;
72 struct sockaddr_in sockaddr
;
80 if (strncmp (name
, "udp:", 4) == 0)
85 else if (strncmp (name
, "tcp:", 4) == 0)
88 port_str
= strchr (name
, ':');
91 error (_("net_open: No colon in host name!")); /* Shouldn't ever happen */
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);
98 /* default hostname is localhost */
100 strcpy (hostname
, "localhost");
102 hostent
= gethostbyname (hostname
);
105 fprintf_unfiltered (gdb_stderr
, "%s: unknown host\n", hostname
);
111 scb
->fd
= socket (PF_INET
, SOCK_DGRAM
, 0);
113 scb
->fd
= socket (PF_INET
, SOCK_STREAM
, 0);
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
));
123 /* set socket nonblocking */
125 ioctl (scb
->fd
, FIONBIO
, &ioarg
);
127 /* Use Non-blocking connect. connect() will return 0 if connected already. */
128 n
= connect (scb
->fd
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
));
132 /* Under Windows, calling "connect" with a non-blocking socket
133 results in WSAEWOULDBLOCK, not WSAEINPROGRESS. */
134 && WSAGetLastError() != WSAEWOULDBLOCK
136 && errno
!= EINPROGRESS
141 errno
= WSAGetLastError();
149 /* looks like we need to wait for the connect */
157 /* While we wait for the connect to complete,
158 poll the UI so it can update or the user can
160 if (deprecated_ui_loop_hook
)
162 if (deprecated_ui_loop_hook (0))
170 FD_SET (scb
->fd
, &rset
);
173 t
.tv_usec
= 1000000 / POLL_INTERVAL
;
175 n
= select (scb
->fd
+ 1, &rset
, &wset
, NULL
, &t
);
178 while (n
== 0 && polls
<= TIMEOUT
* POLL_INTERVAL
);
179 if (n
< 0 || polls
> TIMEOUT
* POLL_INTERVAL
)
181 if (polls
> TIMEOUT
* POLL_INTERVAL
)
188 /* Got something. Is it an error? */
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
);
206 /* turn off nonblocking */
208 ioctl (scb
->fd
, FIONBIO
, &ioarg
);
212 /* Disable Nagle algorithm. Needed in some cases. */
214 setsockopt (scb
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
215 (char *)&tmp
, sizeof (tmp
));
219 /* If we don't do this, then GDB simply exits
220 when the remote side dies. */
221 signal (SIGPIPE
, SIG_IGN
);
228 net_close (struct serial
*scb
)
238 net_read_prim (struct serial
*scb
, size_t count
)
240 return recv (scb
->fd
, scb
->buf
, count
, 0);
244 net_write_prim (struct serial
*scb
, const void *buf
, size_t count
)
246 return send (scb
->fd
, buf
, count
, 0);
250 _initialize_ser_tcp (void)
252 struct serial_ops
*ops
;
255 if (WSAStartup (MAKEWORD (1, 0), &wsa_data
) != 0)
256 /* WinSock is unavailable. */
259 ops
= XMALLOC (struct serial_ops
);
260 memset (ops
, 0, sizeof (struct serial_ops
));
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
);