#include "defs.h"
#include "serial.h"
#include "ser-unix.h"
#include <sys/types.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#ifndef __CYGWIN32__
#include <netinet/tcp.h>
#endif
#include "signals.h"
#include "gdb_string.h"
static int tcp_open (serial_t scb, const char *name);
static void tcp_close (serial_t scb);
void _initialize_ser_tcp (void);
static int
tcp_open (serial_t scb, const char *name)
{
char *port_str;
int port;
struct hostent *hostent;
struct sockaddr_in sockaddr;
int tmp;
char hostname[100];
struct protoent *protoent;
int i;
port_str = strchr (name, ':');
if (!port_str)
error ("tcp_open: No colon in host name!");
tmp = min (port_str - name, (int) sizeof hostname - 1);
strncpy (hostname, name, tmp);
hostname[tmp] = '\000';
port = atoi (port_str + 1);
hostent = gethostbyname (hostname);
if (!hostent)
{
fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname);
errno = ENOENT;
return -1;
}
for (i = 1; i <= 15; i++)
{
scb->fd = socket (PF_INET, SOCK_STREAM, 0);
if (scb->fd < 0)
return -1;
tmp = 1;
setsockopt (scb->fd, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, sizeof (tmp));
tmp = 1;
setsockopt (scb->fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
sockaddr.sin_family = PF_INET;
sockaddr.sin_port = htons (port);
memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
sizeof (struct in_addr));
if (!connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr)))
break;
close (scb->fd);
scb->fd = -1;
if (errno != ECONNREFUSED)
return -1;
sleep (1);
}
protoent = getprotobyname ("tcp");
if (!protoent)
return -1;
tmp = 1;
if (setsockopt (scb->fd, protoent->p_proto, TCP_NODELAY,
(char *) &tmp, sizeof (tmp)))
return -1;
signal (SIGPIPE, SIG_IGN);
return 0;
}
static void
tcp_close (serial_t scb)
{
if (scb->fd < 0)
return;
close (scb->fd);
scb->fd = -1;
}
void
_initialize_ser_tcp (void)
{
struct serial_ops *ops = XMALLOC (struct serial_ops);
memset (ops, sizeof (struct serial_ops), 0);
ops->name = "tcp";
ops->next = 0;
ops->sopen = tcp_open;
ops->sclose = tcp_close;
ops->readchar = ser_unix_readchar;
ops->swrite = ser_unix_write;
ops->flush_output = ser_unix_nop_flush_output;
ops->flush_input = ser_unix_flush_input;
ops->send_break = ser_unix_nop_send_break;
ops->go_raw = ser_unix_nop_raw;
ops->get_tty_state = ser_unix_nop_get_tty_state;
ops->set_tty_state = ser_unix_nop_set_tty_state;
ops->print_tty_state = ser_unix_nop_print_tty_state;
ops->noflush_set_tty_state = ser_unix_nop_noflush_set_tty_state;
ops->setbaudrate = ser_unix_nop_setbaudrate;
ops->setstopbits = ser_unix_nop_setstopbits;
ops->drain_output = ser_unix_nop_drain_output;
ops->async = ser_unix_async;
serial_add_interface (ops);
}