* gnu-regex.c: Eliminate obsolete check for _MSC_VER.
[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 #include <netinet/tcp.h>
33
34 #include <signal.h>
35 #include "gdb_string.h"
36
37 static int tcp_open (serial_t scb, const char *name);
38 static void tcp_close (serial_t scb);
39
40 void _initialize_ser_tcp (void);
41
42 /* Open up a raw tcp socket */
43
44 static int
45 tcp_open (serial_t scb, const char *name)
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)
59 error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */
60
61 tmp = min (port_str - name, (int) sizeof hostname - 1);
62 strncpy (hostname, name, tmp); /* Don't want colon */
63 hostname[tmp] = '\000'; /* Tie off host name */
64 port = atoi (port_str + 1);
65
66 hostent = gethostbyname (hostname);
67
68 if (!hostent)
69 {
70 fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname);
71 errno = ENOENT;
72 return -1;
73 }
74
75 for (i = 1; i <= 15; i++)
76 {
77 scb->fd = socket (PF_INET, SOCK_STREAM, 0);
78 if (scb->fd < 0)
79 return -1;
80
81 /* Allow rapid reuse of this port. */
82 tmp = 1;
83 setsockopt (scb->fd, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, sizeof (tmp));
84
85 /* Enable TCP keep alive process. */
86 tmp = 1;
87 setsockopt (scb->fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
88
89 sockaddr.sin_family = PF_INET;
90 sockaddr.sin_port = htons (port);
91 memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
92 sizeof (struct in_addr));
93
94 if (!connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr)))
95 break;
96
97 close (scb->fd);
98 scb->fd = -1;
99
100 /* We retry for ECONNREFUSED because that is often a temporary condition, which
101 happens when the server is being restarted. */
102
103 if (errno != ECONNREFUSED)
104 return -1;
105
106 sleep (1);
107 }
108
109 protoent = getprotobyname ("tcp");
110 if (!protoent)
111 return -1;
112
113 tmp = 1;
114 if (setsockopt (scb->fd, protoent->p_proto, TCP_NODELAY,
115 (char *) &tmp, sizeof (tmp)))
116 return -1;
117
118 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then GDB simply exits
119 when the remote side dies. */
120
121 return 0;
122 }
123
124 static void
125 tcp_close (serial_t scb)
126 {
127 if (scb->fd < 0)
128 return;
129
130 close (scb->fd);
131 scb->fd = -1;
132 }
133
134 void
135 _initialize_ser_tcp (void)
136 {
137 struct serial_ops *ops = XMALLOC (struct serial_ops);
138 memset (ops, sizeof (struct serial_ops), 0);
139 ops->name = "tcp";
140 ops->next = 0;
141 ops->open = tcp_open;
142 ops->close = tcp_close;
143 ops->readchar = ser_unix_readchar;
144 ops->write = ser_unix_write;
145 ops->flush_output = ser_unix_nop_flush_output;
146 ops->flush_input = ser_unix_flush_input;
147 ops->send_break = ser_unix_nop_send_break;
148 ops->go_raw = ser_unix_nop_raw;
149 ops->get_tty_state = ser_unix_nop_get_tty_state;
150 ops->set_tty_state = ser_unix_nop_set_tty_state;
151 ops->print_tty_state = ser_unix_nop_print_tty_state;
152 ops->noflush_set_tty_state = ser_unix_nop_noflush_set_tty_state;
153 ops->setbaudrate = ser_unix_nop_setbaudrate;
154 ops->setstopbits = ser_unix_nop_setstopbits;
155 ops->drain_output = ser_unix_nop_drain_output;
156 ops->async = ser_unix_async;
157 serial_add_interface (ops);
158 }
This page took 0.042337 seconds and 5 git commands to generate.