X-Git-Url: http://drtracing.org/?a=blobdiff_plain;f=sim%2Fcommon%2Fdv-sockser.c;h=ae65585460aaff6c2fe5eccff0dd6a93feb6eeb8;hb=34b47c3828ca8e6d9dcddb2886744fb470e4276c;hp=d90c817751789accb61320988ce2f34b4ff046b5;hpb=8d6ed1b76807e48e3f3a1f9ae114943678764861;p=deliverable%2Fbinutils-gdb.git
diff --git a/sim/common/dv-sockser.c b/sim/common/dv-sockser.c
index d90c817751..ae65585460 100644
--- a/sim/common/dv-sockser.c
+++ b/sim/common/dv-sockser.c
@@ -1,20 +1,20 @@
/* Serial port emulation using sockets.
- Copyright (C) 1998 Free Software Foundation, Inc.
+ Copyright (C) 1998, 2007, 2008, 2009, 2010, 2011
+ Free Software Foundation, Inc.
Contributed by Cygnus Solutions.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
-any later version.
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
-You should have received a copy of the GNU General Public License along
-with this program; if not, write to the Free Software Foundation, Inc.,
-59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+You should have received a copy of the GNU General Public License
+along with this program. If not, see . */
/* FIXME: will obviously need to evolve.
- connectionless sockets might be more appropriate. */
@@ -35,6 +35,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifdef HAVE_FCNTL_H
#include
#endif
+#ifdef HAVE_UNISTD_H
+#include
+#endif
#include
#include
@@ -53,6 +56,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "dv-sockser.h"
+#ifndef HAVE_SOCKLEN_T
+typedef int socklen_t;
+#endif
+
/* Get definitions for both O_NONBLOCK and O_NDELAY. */
#ifndef O_NDELAY
@@ -71,12 +78,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#endif /* ! defined (FNBLOCK) */
#endif /* ! defined (O_NONBLOCK) */
-#define MIN(a,b) ((a) < (b) ? (a) : (b))
/* Compromise between eating cpu and properly busy-waiting.
One could have an option to set this but for now that seems
like featuritis. */
-#define DEFAULT_TIMEOUT 100 /* microseconds */
+#define DEFAULT_TIMEOUT 1000 /* microseconds */
/* FIXME: These should allocated at run time and kept with other simulator
state (duh...). Later. */
@@ -99,8 +105,8 @@ static const OPTION sockser_options[] =
{
{ { "sockser-addr", required_argument, NULL, OPTION_ADDR },
'\0', "SOCKET ADDRESS", "Set serial emulation socket address",
- sockser_option_handler },
- { { NULL, no_argument, NULL, 0 }, '\0', NULL, NULL, NULL }
+ sockser_option_handler, NULL },
+ { { NULL, no_argument, NULL, 0 }, '\0', NULL, NULL, NULL, NULL }
};
static SIM_RC
@@ -145,7 +151,9 @@ dv_sockser_init (SIM_DESC sd)
sockser_addr);
return SIM_RC_FAIL;
}
- tmp = MIN (port_str - sockser_addr, (int) sizeof hostname - 1);
+ tmp = port_str - sockser_addr;
+ if (tmp >= sizeof hostname)
+ tmp = sizeof (hostname) - 1;
strncpy (hostname, sockser_addr, tmp);
hostname[tmp] = '\000';
port = atoi (port_str + 1);
@@ -159,7 +167,7 @@ dv_sockser_init (SIM_DESC sd)
}
sockser_listen_fd = socket (PF_INET, SOCK_STREAM, 0);
- if (sockser_listen_fd < 0)
+ if (sockser_listen_fd == -1)
{
sim_io_eprintf (sd, "sockser init: unable to get socket: %s\n",
strerror (errno));
@@ -167,10 +175,16 @@ dv_sockser_init (SIM_DESC sd)
}
sockaddr.sin_family = PF_INET;
- sockaddr.sin_port = htons(port);
+ sockaddr.sin_port = htons (port);
memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
sizeof (struct in_addr));
+ tmp = 1;
+ if (setsockopt (sockser_listen_fd, SOL_SOCKET, SO_REUSEADDR, (void*)& tmp, sizeof (tmp)) < 0)
+ {
+ sim_io_eprintf (sd, "sockser init: unable to set SO_REUSEADDR: %s\n",
+ strerror (errno));
+ }
if (bind (sockser_listen_fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr)) < 0)
{
sim_io_eprintf (sd, "sockser init: unable to bind socket address: %s\n",
@@ -234,7 +248,7 @@ connected_p (SIM_DESC sd)
struct timeval tv;
fd_set readfds;
struct sockaddr sockaddr;
- int addrlen;
+ socklen_t addrlen;
if (sockser_listen_fd == -1)
return 0;
@@ -259,8 +273,9 @@ connected_p (SIM_DESC sd)
if (numfds <= 0)
return 0;
+ addrlen = sizeof (sockaddr);
sockser_fd = accept (sockser_listen_fd, &sockaddr, &addrlen);
- if (sockser_fd < 0)
+ if (sockser_fd == -1)
return 0;
/* Set non-blocking i/o. */
@@ -284,7 +299,8 @@ dv_sockser_status (SIM_DESC sd)
fd_set readfds,writefds;
/* status to return if the socket isn't set up, or select fails */
- status = DV_SOCKSER_INPUT_EMPTY | DV_SOCKSER_OUTPUT_EMPTY;
+ status = DV_SOCKSER_INPUT_EMPTY | DV_SOCKSER_OUTPUT_EMPTY |
+ DV_SOCKSER_DISCONNECTED;
if (! connected_p (sd))
return status;
@@ -331,13 +347,14 @@ dv_sockser_status (SIM_DESC sd)
}
int
-dv_sockser_write (SIM_DESC sd, unsigned char c)
+dv_sockser_write_buffer (SIM_DESC sd, const unsigned char *buffer,
+ unsigned nr_bytes)
{
int n;
if (! connected_p (sd))
return -1;
- n = write (sockser_fd, &c, 1);
+ n = write (sockser_fd, buffer, nr_bytes);
if (n == -1)
{
if (errno == EPIPE)
@@ -347,9 +364,15 @@ dv_sockser_write (SIM_DESC sd, unsigned char c)
}
return -1;
}
- if (n != 1)
+ if (n != nr_bytes)
return -1;
- return 1;
+ return nr_bytes;
+}
+
+int
+dv_sockser_write (SIM_DESC sd, unsigned char c)
+{
+ return dv_sockser_write_buffer (sd, &c, 1);
}
int