1 /* Serial interface for raw TCP connections on Un*x like systems.
3 Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2001, 2005
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
28 #include <sys/types.h>
30 #ifdef HAVE_SYS_FILIO_H
31 #include <sys/filio.h> /* For FIONBIO. */
33 #ifdef HAVE_SYS_IOCTL_H
34 #include <sys/ioctl.h> /* For FIONBIO. */
41 #define ETIMEDOUT WSAETIMEDOUT
42 #define close(fd) closesocket (fd)
43 #define ioctl ioctlsocket
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
48 #include <sys/socket.h>
49 #include <netinet/tcp.h>
53 #include "gdb_string.h"
55 #ifndef HAVE_SOCKLEN_T
56 typedef int socklen_t
;
59 static int net_open (struct serial
*scb
, const char *name
);
60 static void net_close (struct serial
*scb
);
61 void _initialize_ser_tcp (void);
63 /* seconds to wait for connect */
65 /* how many times per second to poll deprecated_ui_loop_hook */
66 #define POLL_INTERVAL 2
68 /* Open a tcp socket */
71 net_open (struct serial
*scb
, const char *name
)
73 char *port_str
, hostname
[100];
76 struct hostent
*hostent
;
77 struct sockaddr_in sockaddr
;
85 if (strncmp (name
, "udp:", 4) == 0)
90 else if (strncmp (name
, "tcp:", 4) == 0)
93 port_str
= strchr (name
, ':');
96 error (_("net_open: No colon in host name!")); /* Shouldn't ever happen */
98 tmp
= min (port_str
- name
, (int) sizeof hostname
- 1);
99 strncpy (hostname
, name
, tmp
); /* Don't want colon */
100 hostname
[tmp
] = '\000'; /* Tie off host name */
101 port
= atoi (port_str
+ 1);
103 /* default hostname is localhost */
105 strcpy (hostname
, "localhost");
107 hostent
= gethostbyname (hostname
);
110 fprintf_unfiltered (gdb_stderr
, "%s: unknown host\n", hostname
);
116 scb
->fd
= socket (PF_INET
, SOCK_DGRAM
, 0);
118 scb
->fd
= socket (PF_INET
, SOCK_STREAM
, 0);
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
));
128 /* set socket nonblocking */
130 ioctl (scb
->fd
, FIONBIO
, &ioarg
);
132 /* Use Non-blocking connect. connect() will return 0 if connected already. */
133 n
= connect (scb
->fd
, (struct sockaddr
*) &sockaddr
, sizeof (sockaddr
));
137 /* Under Windows, calling "connect" with a non-blocking socket
138 results in WSAEWOULDBLOCK, not WSAEINPROGRESS. */
139 && WSAGetLastError() != WSAEWOULDBLOCK
141 && errno
!= EINPROGRESS
146 errno
= WSAGetLastError();
154 /* looks like we need to wait for the connect */
162 /* While we wait for the connect to complete,
163 poll the UI so it can update or the user can
165 if (deprecated_ui_loop_hook
)
167 if (deprecated_ui_loop_hook (0))
175 FD_SET (scb
->fd
, &rset
);
178 t
.tv_usec
= 1000000 / POLL_INTERVAL
;
180 n
= select (scb
->fd
+ 1, &rset
, &wset
, NULL
, &t
);
183 while (n
== 0 && polls
<= TIMEOUT
* POLL_INTERVAL
);
184 if (n
< 0 || polls
> TIMEOUT
* POLL_INTERVAL
)
186 if (polls
> TIMEOUT
* POLL_INTERVAL
)
193 /* Got something. Is it an error? */
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
);
212 /* turn off nonblocking */
214 ioctl (scb
->fd
, FIONBIO
, &ioarg
);
218 /* Disable Nagle algorithm. Needed in some cases. */
220 setsockopt (scb
->fd
, IPPROTO_TCP
, TCP_NODELAY
,
221 (char *)&tmp
, sizeof (tmp
));
225 /* If we don't do this, then GDB simply exits
226 when the remote side dies. */
227 signal (SIGPIPE
, SIG_IGN
);
234 net_close (struct serial
*scb
)
244 net_read_prim (struct serial
*scb
, size_t count
)
246 return recv (scb
->fd
, scb
->buf
, count
, 0);
250 net_write_prim (struct serial
*scb
, const void *buf
, size_t count
)
252 return send (scb
->fd
, buf
, count
, 0);
256 _initialize_ser_tcp (void)
258 struct serial_ops
*ops
;
261 if (WSAStartup (MAKEWORD (1, 0), &wsa_data
) != 0)
262 /* WinSock is unavailable. */
265 ops
= XMALLOC (struct serial_ops
);
266 memset (ops
, 0, sizeof (struct serial_ops
));
269 ops
->open
= net_open
;
270 ops
->close
= net_close
;
271 ops
->readchar
= ser_base_readchar
;
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
;
285 ops
->read_prim
= net_read_prim
;
286 ops
->write_prim
= net_write_prim
;
287 serial_add_interface (ops
);