2002-05-11 Daniel Jacobowitz <drow@mvista.com>
[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-unix.h"
25
26 #include <sys/types.h>
27
28 #ifdef HAVE_SYS_FILIO_H
29 #include <sys/filio.h> /* For FIONBIO. */
30 #endif
31 #ifdef HAVE_SYS_IOCTL_H
32 #include <sys/ioctl.h> /* For FIONBIO. */
33 #endif
34
35 #include <sys/time.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <netdb.h>
39 #include <sys/socket.h>
40 #include <netinet/tcp.h>
41
42 #include <signal.h>
43 #include "gdb_string.h"
44
45 static int tcp_open (struct serial *scb, const char *name);
46 static void tcp_close (struct serial *scb);
47 extern int (*ui_loop_hook) (int);
48 void _initialize_ser_tcp (void);
49
50 /* seconds to wait for connect */
51 #define TIMEOUT 15
52 /* how many times per second to poll ui_loop_hook */
53 #define POLL_INTERVAL 2
54
55 /* Open a tcp socket */
56
57 static int
58 tcp_open (struct serial *scb, const char *name)
59 {
60 char *port_str, hostname[100];
61 int n, port, tmp;
62 struct hostent *hostent;
63 struct sockaddr_in sockaddr;
64
65 port_str = strchr (name, ':');
66
67 if (!port_str)
68 error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */
69
70 tmp = min (port_str - name, (int) sizeof hostname - 1);
71 strncpy (hostname, name, tmp); /* Don't want colon */
72 hostname[tmp] = '\000'; /* Tie off host name */
73 port = atoi (port_str + 1);
74
75 /* default hostname is localhost */
76 if (!hostname[0])
77 strcpy (hostname, "localhost");
78
79 hostent = gethostbyname (hostname);
80 if (!hostent)
81 {
82 fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname);
83 errno = ENOENT;
84 return -1;
85 }
86
87 scb->fd = socket (PF_INET, SOCK_STREAM, 0);
88 if (scb->fd < 0)
89 return -1;
90
91 sockaddr.sin_family = PF_INET;
92 sockaddr.sin_port = htons (port);
93 memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
94 sizeof (struct in_addr));
95
96 /* set socket nonblocking */
97 tmp = 1;
98 ioctl (scb->fd, FIONBIO, &tmp);
99
100 /* Use Non-blocking connect. connect() will return 0 if connected already. */
101 n = connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr));
102
103 if (n < 0 && errno != EINPROGRESS)
104 {
105 tcp_close (scb);
106 return -1;
107 }
108
109 if (n)
110 {
111 /* looks like we need to wait for the connect */
112 struct timeval t;
113 fd_set rset, wset;
114 int polls = 0;
115 FD_ZERO (&rset);
116
117 do
118 {
119 /* While we wait for the connect to complete
120 poll the UI so it can update or the user can
121 interrupt. */
122 if (ui_loop_hook)
123 {
124 if (ui_loop_hook (0))
125 {
126 errno = EINTR;
127 tcp_close (scb);
128 return -1;
129 }
130 }
131
132 FD_SET (scb->fd, &rset);
133 wset = rset;
134 t.tv_sec = 0;
135 t.tv_usec = 1000000 / POLL_INTERVAL;
136
137 n = select (scb->fd + 1, &rset, &wset, NULL, &t);
138 polls++;
139 }
140 while (n == 0 && polls <= TIMEOUT * POLL_INTERVAL);
141 if (n < 0 || polls > TIMEOUT * POLL_INTERVAL)
142 {
143 if (polls > TIMEOUT * POLL_INTERVAL)
144 errno = ETIMEDOUT;
145 tcp_close (scb);
146 return -1;
147 }
148 }
149
150 /* Got something. Is it an error? */
151 {
152 int res, err, len;
153 len = sizeof(err);
154 res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, &err, &len);
155 if (res < 0 || err)
156 {
157 if (err)
158 errno = err;
159 tcp_close (scb);
160 return -1;
161 }
162 }
163
164 /* turn off nonblocking */
165 tmp = 0;
166 ioctl (scb->fd, FIONBIO, &tmp);
167
168 /* Disable Nagle algorithm. Needed in some cases. */
169 tmp = 1;
170 setsockopt (scb->fd, IPPROTO_TCP, TCP_NODELAY,
171 (char *)&tmp, sizeof (tmp));
172
173 /* If we don't do this, then GDB simply exits
174 when the remote side dies. */
175 signal (SIGPIPE, SIG_IGN);
176
177 return 0;
178 }
179
180 static void
181 tcp_close (struct serial *scb)
182 {
183 if (scb->fd < 0)
184 return;
185
186 close (scb->fd);
187 scb->fd = -1;
188 }
189
190 void
191 _initialize_ser_tcp (void)
192 {
193 struct serial_ops *ops = XMALLOC (struct serial_ops);
194 memset (ops, sizeof (struct serial_ops), 0);
195 ops->name = "tcp";
196 ops->next = 0;
197 ops->open = tcp_open;
198 ops->close = tcp_close;
199 ops->readchar = ser_unix_readchar;
200 ops->write = ser_unix_write;
201 ops->flush_output = ser_unix_nop_flush_output;
202 ops->flush_input = ser_unix_flush_input;
203 ops->send_break = ser_unix_nop_send_break;
204 ops->go_raw = ser_unix_nop_raw;
205 ops->get_tty_state = ser_unix_nop_get_tty_state;
206 ops->set_tty_state = ser_unix_nop_set_tty_state;
207 ops->print_tty_state = ser_unix_nop_print_tty_state;
208 ops->noflush_set_tty_state = ser_unix_nop_noflush_set_tty_state;
209 ops->setbaudrate = ser_unix_nop_setbaudrate;
210 ops->setstopbits = ser_unix_nop_setstopbits;
211 ops->drain_output = ser_unix_nop_drain_output;
212 ops->async = ser_unix_async;
213 serial_add_interface (ops);
214 }
This page took 0.034829 seconds and 4 git commands to generate.