Update/correct copyright notices.
[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 #include <sys/time.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <netdb.h>
31 #include <sys/socket.h>
32 #ifndef __CYGWIN32__
33 #include <netinet/tcp.h>
34 #endif
35
36 #include <signal.h>
37 #include "gdb_string.h"
38
39 static int tcp_open (serial_t scb, const char *name);
40 static void tcp_close (serial_t scb);
41
42 void _initialize_ser_tcp (void);
43
44 /* Open up a raw tcp socket */
45
46 static int
47 tcp_open (serial_t scb, const char *name)
48 {
49 char *port_str;
50 int port;
51 struct hostent *hostent;
52 struct sockaddr_in sockaddr;
53 int tmp;
54 char hostname[100];
55 struct protoent *protoent;
56 int i;
57
58 port_str = strchr (name, ':');
59
60 if (!port_str)
61 error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */
62
63 tmp = min (port_str - name, (int) sizeof hostname - 1);
64 strncpy (hostname, name, tmp); /* Don't want colon */
65 hostname[tmp] = '\000'; /* Tie off host name */
66 port = atoi (port_str + 1);
67
68 hostent = gethostbyname (hostname);
69
70 if (!hostent)
71 {
72 fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname);
73 errno = ENOENT;
74 return -1;
75 }
76
77 for (i = 1; i <= 15; i++)
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, (struct sockaddr *) &sockaddr, sizeof (sockaddr)))
97 break;
98
99 close (scb->fd);
100 scb->fd = -1;
101
102 /* We retry for ECONNREFUSED because that is often a temporary condition, which
103 happens when the server is being restarted. */
104
105 if (errno != ECONNREFUSED)
106 return -1;
107
108 sleep (1);
109 }
110
111 protoent = getprotobyname ("tcp");
112 if (!protoent)
113 return -1;
114
115 tmp = 1;
116 if (setsockopt (scb->fd, protoent->p_proto, TCP_NODELAY,
117 (char *) &tmp, sizeof (tmp)))
118 return -1;
119
120 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then GDB simply exits
121 when the remote side dies. */
122
123 return 0;
124 }
125
126 static void
127 tcp_close (serial_t scb)
128 {
129 if (scb->fd < 0)
130 return;
131
132 close (scb->fd);
133 scb->fd = -1;
134 }
135
136 void
137 _initialize_ser_tcp (void)
138 {
139 struct serial_ops *ops = XMALLOC (struct serial_ops);
140 memset (ops, sizeof (struct serial_ops), 0);
141 ops->name = "tcp";
142 ops->next = 0;
143 ops->open = tcp_open;
144 ops->close = tcp_close;
145 ops->readchar = ser_unix_readchar;
146 ops->write = ser_unix_write;
147 ops->flush_output = ser_unix_nop_flush_output;
148 ops->flush_input = ser_unix_flush_input;
149 ops->send_break = ser_unix_nop_send_break;
150 ops->go_raw = ser_unix_nop_raw;
151 ops->get_tty_state = ser_unix_nop_get_tty_state;
152 ops->set_tty_state = ser_unix_nop_set_tty_state;
153 ops->print_tty_state = ser_unix_nop_print_tty_state;
154 ops->noflush_set_tty_state = ser_unix_nop_noflush_set_tty_state;
155 ops->setbaudrate = ser_unix_nop_setbaudrate;
156 ops->setstopbits = ser_unix_nop_setstopbits;
157 ops->drain_output = ser_unix_nop_drain_output;
158 ops->async = ser_unix_async;
159 serial_add_interface (ops);
160 }
This page took 0.034827 seconds and 5 git commands to generate.