Use getprotobyname instead of hardwired number (in tcp_open).
[deliverable/binutils-gdb.git] / gdb / ser-tcp.c
1 /* Serial interface for raw TCP connections on Un*x like systems
2 Copyright 1992, 1993 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include "serial.h"
22 #include <sys/types.h>
23 #include <sys/time.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <netdb.h>
27 #include <sys/socket.h>
28 #include <netinet/tcp.h>
29 #include "signals.h"
30
31 struct tcp_ttystate
32 {
33 int bogus;
34 };
35
36 static int tcp_open PARAMS ((serial_t scb, const char *name));
37 static void tcp_raw PARAMS ((serial_t scb));
38 static int wait_for PARAMS ((serial_t scb, int timeout));
39 static int tcp_readchar PARAMS ((serial_t scb, int timeout));
40 static int tcp_setbaudrate PARAMS ((serial_t scb, int rate));
41 static int tcp_write PARAMS ((serial_t scb, const char *str, int len));
42 static void tcp_restore PARAMS ((serial_t scb));
43 static void tcp_close PARAMS ((serial_t scb));
44 static serial_ttystate tcp_get_tty_state PARAMS ((serial_t scb));
45 static int tcp_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
46
47 /* Open up a raw tcp socket */
48
49 static int
50 tcp_open(scb, name)
51 serial_t scb;
52 const char *name;
53 {
54 char *port_str;
55 int port;
56 struct hostent *hostent;
57 struct sockaddr_in sockaddr;
58 int tmp;
59 char hostname[100];
60 struct protoent *protoent;
61
62 port_str = strchr (name, ':');
63
64 if (!port_str)
65 error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */
66
67 tmp = min(port_str - name + 1, sizeof hostname);
68 strncpy (hostname, name, tmp - 1); /* Don't want colon */
69 port = atoi (port_str + 1);
70
71 hostent = gethostbyname (hostname);
72
73 if (!hostent)
74 {
75 errno = ENOENT;
76 return -1;
77 }
78
79 scb->fd = socket (PF_INET, SOCK_STREAM, 0);
80 if (scb->fd < 0)
81 return -1;
82
83 /* Allow rapid reuse of this port. */
84 tmp = 1;
85 setsockopt (scb->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&tmp, sizeof(tmp));
86
87 /* Enable TCP keep alive process. */
88 tmp = 1;
89 setsockopt (scb->fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&tmp, sizeof(tmp));
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 if (connect(scb->fd, &sockaddr, sizeof(sockaddr)))
97 {
98 close(scb->fd);
99 return -1;
100 }
101
102 protoent = getprotobyname ("tcp");
103 if (!protoent)
104 return -1;
105
106 tmp = 1;
107 if (setsockopt (scb->fd, protoent->p_proto, TCP_NODELAY,
108 (char *)&tmp, sizeof(tmp)))
109 return -1;
110
111 signal(SIGPIPE, SIG_IGN); /* If we don't do this, then GDB simply exits
112 when the remote side dies. */
113
114 return 0;
115 }
116
117 static serial_ttystate
118 tcp_get_tty_state(scb)
119 serial_t scb;
120 {
121 struct tcp_ttystate *state;
122
123 state = (struct tcp_ttystate *)xmalloc(sizeof *state);
124
125 return (serial_ttystate)state;
126 }
127
128 static int
129 tcp_set_tty_state(scb, ttystate)
130 serial_t scb;
131 serial_ttystate ttystate;
132 {
133 struct tcp_ttystate *state;
134
135 state = (struct tcp_ttystate *)ttystate;
136
137 return 0;
138 }
139
140 static void
141 tcp_raw(scb)
142 serial_t scb;
143 {
144 return; /* Always in raw mode */
145 }
146
147 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
148 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
149
150 For termio{s}, we actually just setup VTIME if necessary, and let the
151 timeout occur in the read() in tcp_read().
152 */
153
154 static int
155 wait_for(scb, timeout)
156 serial_t scb;
157 int timeout;
158 {
159 int numfds;
160 struct timeval tv;
161 fd_set readfds;
162
163 FD_ZERO (&readfds);
164
165 tv.tv_sec = timeout;
166 tv.tv_usec = 0;
167
168 FD_SET(scb->fd, &readfds);
169
170 if (timeout >= 0)
171 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
172 else
173 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
174
175 if (numfds <= 0)
176 if (numfds == 0)
177 return SERIAL_TIMEOUT;
178 else
179 return SERIAL_ERROR; /* Got an error from select or poll */
180
181 return 0;
182 }
183
184 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
185 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
186 char if successful. Returns -2 if timeout expired, EOF if line dropped
187 dead, or -3 for any other error (see errno in that case). */
188
189 static int
190 tcp_readchar(scb, timeout)
191 serial_t scb;
192 int timeout;
193 {
194 int status;
195
196 if (scb->bufcnt-- > 0)
197 return *scb->bufp++;
198
199 status = wait_for(scb, timeout);
200
201 if (status < 0)
202 return status;
203
204 scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ);
205
206 if (scb->bufcnt <= 0)
207 if (scb->bufcnt == 0)
208 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
209 distinguish between EOF & timeouts
210 someday] */
211 else
212 return SERIAL_ERROR; /* Got an error from read */
213
214 scb->bufcnt--;
215 scb->bufp = scb->buf;
216 return *scb->bufp++;
217 }
218
219 static int
220 tcp_setbaudrate(scb, rate)
221 serial_t scb;
222 int rate;
223 {
224 return 0; /* Never fails! */
225 }
226
227 static int
228 tcp_write(scb, str, len)
229 serial_t scb;
230 const char *str;
231 int len;
232 {
233 int cc;
234
235 while (len > 0)
236 {
237 cc = write(scb->fd, str, len);
238
239 if (cc < 0)
240 return 1;
241 len -= cc;
242 str += cc;
243 }
244 return 0;
245 }
246
247 static void
248 tcp_close(scb)
249 serial_t scb;
250 {
251 if (scb->fd < 0)
252 return;
253
254 close(scb->fd);
255 scb->fd = -1;
256 }
257
258 static struct serial_ops tcp_ops =
259 {
260 "tcp",
261 0,
262 tcp_open,
263 tcp_close,
264 tcp_readchar,
265 tcp_write,
266 tcp_raw,
267 tcp_get_tty_state,
268 tcp_set_tty_state,
269 tcp_setbaudrate,
270 };
271
272 void
273 _initialize_ser_tcp ()
274 {
275 serial_add_interface (&tcp_ops);
276 }
This page took 0.034867 seconds and 5 git commands to generate.