]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - sim/common/dv-sockser.c
sim: formally assume unistd.h always exists (via gnulib)
[thirdparty/binutils-gdb.git] / sim / common / dv-sockser.c
index c95288ca775621ace2237323e3ec1d55eff3d770..698cab839209db5d7977a8963f084d20a7d8d1f4 100644 (file)
@@ -1,80 +1,52 @@
 /* Serial port emulation using sockets.
-   Copyright (C) 1998 Free Software Foundation, Inc.
+   Copyright (C) 1998-2023 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 <http://www.gnu.org/licenses/>.  */
 
 /* FIXME: will obviously need to evolve.
    - connectionless sockets might be more appropriate.  */
 
-#include "sim-main.h"
+/* This must come before any other includes.  */
+#include "defs.h"
 
-#ifdef HAVE_STRING_H
-#include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
-#include <signal.h>
-#ifdef HAVE_STDLIB_H
-#include <stdlib.h>
-#endif
+#include <errno.h>
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
-#ifdef HAVE_UNISTD_H
+#include <netdb.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
-#endif
-
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/time.h>
-#include <netinet/in.h>
 #include <arpa/inet.h>
-#include <netdb.h>
+#include <netinet/in.h>
+#include <sys/select.h>
 #include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/types.h>
 
-#ifndef __CYGWIN32__
-#include <netinet/tcp.h>
-#endif
-
+#include "sim-main.h"
 #include "sim-assert.h"
 #include "sim-options.h"
 
 #include "dv-sockser.h"
 \f
-/* Get definitions for both O_NONBLOCK and O_NDELAY.  */
-
-#ifndef O_NDELAY
-#ifdef FNDELAY
-#define O_NDELAY FNDELAY
-#else /* ! defined (FNDELAY) */
-#define O_NDELAY 0
-#endif /* ! defined (FNDELAY) */
-#endif /* ! defined (O_NDELAY) */
-
-#ifndef O_NONBLOCK
-#ifdef FNBLOCK
-#define O_NONBLOCK FNBLOCK
-#else /* ! defined (FNBLOCK) */
-#define O_NONBLOCK 0
-#endif /* ! defined (FNBLOCK) */
-#endif /* ! defined (O_NONBLOCK) */
+#ifndef HAVE_SOCKLEN_T
+typedef int socklen_t;
+#endif
 \f
-#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
@@ -102,8 +74,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
@@ -148,7 +120,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);
@@ -162,7 +136,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));
@@ -170,12 +144,12 @@ 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)
+  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));
@@ -199,6 +173,7 @@ dv_sockser_init (SIM_DESC sd)
 
   /* Handle writes to missing client -> SIGPIPE.
      ??? Need a central signal management module.  */
+#ifdef SIGPIPE
   {
     RETSIGTYPE (*orig) ();
     orig = signal (SIGPIPE, SIG_IGN);
@@ -206,6 +181,7 @@ dv_sockser_init (SIM_DESC sd)
     if (orig != SIG_DFL && orig != SIG_IGN)
       signal (SIGPIPE, orig);
   }
+#endif
 
   return SIM_RC_OK;
 }
@@ -225,8 +201,11 @@ dv_sockser_uninstall (SIM_DESC sd)
     }
 }
 
+/* Provide a prototype to silence -Wmissing-prototypes.  */
+extern MODULE_INIT_FN sim_install_dv_sockser;
+
 SIM_RC
-dv_sockser_install (SIM_DESC sd)
+sim_install_dv_sockser (SIM_DESC sd)
 {
   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
   if (sim_add_option_table (sd, NULL, sockser_options) != SIM_RC_OK)
@@ -243,7 +222,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;
@@ -268,13 +247,15 @@ 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.  */
+#if defined(F_GETFL) && defined(O_NONBLOCK)
   flags = fcntl (sockser_fd, F_GETFL);
-  flags |= O_NONBLOCK | O_NDELAY;
+  flags |= O_NONBLOCK;
   if (fcntl (sockser_fd, F_SETFL, flags) == -1)
     {
       sim_io_eprintf (sd, "unable to set nonblocking i/o");
@@ -282,6 +263,7 @@ connected_p (SIM_DESC sd)
       sockser_fd = -1;
       return 0;
     }
+#endif
   return 1;
 }
 
@@ -293,7 +275,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;
@@ -340,13 +323,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)
@@ -356,9 +340,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