base: Teach the socket listeners how to bind to the loopback.
authorGabe Black <gabeblack@google.com>
Fri, 5 May 2017 23:50:22 +0000 (16:50 -0700)
committerGabe Black <gabeblack@google.com>
Tue, 9 May 2017 16:44:30 +0000 (16:44 +0000)
The loopback device will allow access to various services like remote GDB
debugging, connecting to the terminal, etc., without letting external
agents like port scanners connect and disrupting the simulation.

Change-Id: I76dccbf152fa278ae9f342b25f7e345a1329fbe4
Reviewed-on: https://gem5-review.googlesource.com/3080
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
Maintainer: Andreas Sandberg <andreas.sandberg@arm.com>

src/base/socket.cc
src/base/socket.hh

index 7a444373bfa5d25cfc70260946919fa71f3f25e2..5ef0f4b9eddcbd7e416c023786d1a2e9797bbe4a 100644 (file)
 
 #include "base/misc.hh"
 #include "base/types.hh"
+#include "sim/byteswap.hh"
 
 using namespace std;
 
 bool ListenSocket::listeningDisabled = false;
 bool ListenSocket::anyListening = false;
 
+bool ListenSocket::bindToLoopback = false;
+
 void
 ListenSocket::disableAll()
 {
@@ -60,6 +63,14 @@ ListenSocket::allDisabled()
     return listeningDisabled;
 }
 
+void
+ListenSocket::loopbackOnly()
+{
+    if (anyListening)
+        panic("Too late to bind to loopback, already have a listener");
+    bindToLoopback = true;
+}
+
 ////////////////////////////////////////////////////////////////////////
 //
 //
@@ -94,7 +105,8 @@ ListenSocket::listen(int port, bool reuse)
 
     struct sockaddr_in sockaddr;
     sockaddr.sin_family = PF_INET;
-    sockaddr.sin_addr.s_addr = INADDR_ANY;
+    sockaddr.sin_addr.s_addr =
+        htobe<unsigned long>(bindToLoopback ? INADDR_LOOPBACK : INADDR_ANY);
     sockaddr.sin_port = htons(port);
     // finally clear sin_zero
     memset(&sockaddr.sin_zero, 0, sizeof(sockaddr.sin_zero));
index 942318e45a8d2b70098678536abf8f4fd4aaa9ff..fab7f7576e25013f406cd684a3eb2b365c00b869 100644 (file)
@@ -37,10 +37,14 @@ class ListenSocket
     static bool listeningDisabled;
     static bool anyListening;
 
+    static bool bindToLoopback;
+
   public:
     static void disableAll();
     static bool allDisabled();
 
+    static void loopbackOnly();
+
   protected:
     bool listening;
     int fd;