Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Serial interface for raw TCP connections on Un*x like systems |
b6ba6518 KB |
2 | Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2001 |
3 | Free Software Foundation, Inc. | |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
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. | |
c906108c | 11 | |
c5aa993b JM |
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. | |
c906108c | 16 | |
c5aa993b JM |
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. */ | |
c906108c SS |
21 | |
22 | #include "defs.h" | |
23 | #include "serial.h" | |
c2c6d25f JM |
24 | #include "ser-unix.h" |
25 | ||
c906108c SS |
26 | #include <sys/types.h> |
27 | #include <sys/time.h> | |
28 | #include <netinet/in.h> | |
29 | #include <arpa/inet.h> | |
30 | #include <netdb.h> | |
31 | #include <sys/socket.h> | |
c906108c | 32 | #include <netinet/tcp.h> |
c906108c | 33 | |
042be3a9 | 34 | #include <signal.h> |
c906108c SS |
35 | #include "gdb_string.h" |
36 | ||
819cc324 AC |
37 | static int tcp_open (struct serial *scb, const char *name); |
38 | static void tcp_close (struct serial *scb); | |
c2c6d25f JM |
39 | |
40 | void _initialize_ser_tcp (void); | |
c906108c SS |
41 | |
42 | /* Open up a raw tcp socket */ | |
43 | ||
44 | static int | |
819cc324 | 45 | tcp_open (struct serial *scb, const char *name) |
c906108c SS |
46 | { |
47 | char *port_str; | |
48 | int port; | |
49 | struct hostent *hostent; | |
50 | struct sockaddr_in sockaddr; | |
51 | int tmp; | |
52 | char hostname[100]; | |
53 | struct protoent *protoent; | |
54 | int i; | |
55 | ||
56 | port_str = strchr (name, ':'); | |
57 | ||
58 | if (!port_str) | |
c5aa993b | 59 | error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */ |
c906108c SS |
60 | |
61 | tmp = min (port_str - name, (int) sizeof hostname - 1); | |
c5aa993b | 62 | strncpy (hostname, name, tmp); /* Don't want colon */ |
c906108c SS |
63 | hostname[tmp] = '\000'; /* Tie off host name */ |
64 | port = atoi (port_str + 1); | |
65 | ||
ad4571f3 CV |
66 | if (!hostname[0]) |
67 | strcpy (hostname, "localhost"); | |
68 | ||
c906108c SS |
69 | hostent = gethostbyname (hostname); |
70 | ||
71 | if (!hostent) | |
72 | { | |
73 | fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname); | |
74 | errno = ENOENT; | |
75 | return -1; | |
76 | } | |
77 | ||
78 | for (i = 1; i <= 15; i++) | |
79 | { | |
80 | scb->fd = socket (PF_INET, SOCK_STREAM, 0); | |
81 | if (scb->fd < 0) | |
82 | return -1; | |
83 | ||
84 | /* Allow rapid reuse of this port. */ | |
85 | tmp = 1; | |
c5aa993b | 86 | setsockopt (scb->fd, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, sizeof (tmp)); |
c906108c SS |
87 | |
88 | /* Enable TCP keep alive process. */ | |
89 | tmp = 1; | |
c5aa993b | 90 | setsockopt (scb->fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp)); |
c906108c SS |
91 | |
92 | sockaddr.sin_family = PF_INET; | |
c5aa993b | 93 | sockaddr.sin_port = htons (port); |
c906108c SS |
94 | memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr, |
95 | sizeof (struct in_addr)); | |
96 | ||
c5aa993b | 97 | if (!connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr))) |
c906108c SS |
98 | break; |
99 | ||
100 | close (scb->fd); | |
101 | scb->fd = -1; | |
102 | ||
103 | /* We retry for ECONNREFUSED because that is often a temporary condition, which | |
104 | happens when the server is being restarted. */ | |
105 | ||
106 | if (errno != ECONNREFUSED) | |
107 | return -1; | |
108 | ||
109 | sleep (1); | |
110 | } | |
111 | ||
112 | protoent = getprotobyname ("tcp"); | |
113 | if (!protoent) | |
114 | return -1; | |
115 | ||
116 | tmp = 1; | |
117 | if (setsockopt (scb->fd, protoent->p_proto, TCP_NODELAY, | |
c5aa993b | 118 | (char *) &tmp, sizeof (tmp))) |
c906108c SS |
119 | return -1; |
120 | ||
c5aa993b | 121 | signal (SIGPIPE, SIG_IGN); /* If we don't do this, then GDB simply exits |
c906108c SS |
122 | when the remote side dies. */ |
123 | ||
124 | return 0; | |
125 | } | |
126 | ||
c906108c | 127 | static void |
819cc324 | 128 | tcp_close (struct serial *scb) |
c906108c SS |
129 | { |
130 | if (scb->fd < 0) | |
131 | return; | |
132 | ||
c5aa993b | 133 | close (scb->fd); |
c906108c SS |
134 | scb->fd = -1; |
135 | } | |
136 | ||
c906108c | 137 | void |
c2c6d25f | 138 | _initialize_ser_tcp (void) |
c906108c | 139 | { |
c2c6d25f JM |
140 | struct serial_ops *ops = XMALLOC (struct serial_ops); |
141 | memset (ops, sizeof (struct serial_ops), 0); | |
142 | ops->name = "tcp"; | |
143 | ops->next = 0; | |
144 | ops->open = tcp_open; | |
145 | ops->close = tcp_close; | |
146 | ops->readchar = ser_unix_readchar; | |
147 | ops->write = ser_unix_write; | |
148 | ops->flush_output = ser_unix_nop_flush_output; | |
2acceee2 | 149 | ops->flush_input = ser_unix_flush_input; |
c2c6d25f JM |
150 | ops->send_break = ser_unix_nop_send_break; |
151 | ops->go_raw = ser_unix_nop_raw; | |
152 | ops->get_tty_state = ser_unix_nop_get_tty_state; | |
153 | ops->set_tty_state = ser_unix_nop_set_tty_state; | |
154 | ops->print_tty_state = ser_unix_nop_print_tty_state; | |
155 | ops->noflush_set_tty_state = ser_unix_nop_noflush_set_tty_state; | |
156 | ops->setbaudrate = ser_unix_nop_setbaudrate; | |
157 | ops->setstopbits = ser_unix_nop_setstopbits; | |
158 | ops->drain_output = ser_unix_nop_drain_output; | |
159 | ops->async = ser_unix_async; | |
160 | serial_add_interface (ops); | |
c906108c | 161 | } |