gem5  v21.1.0.2
socket.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 The Regents of the University of California
3  * All rights reserved
4  *
5  * Copyright (c) 2002-2005 The Regents of The University of Michigan
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met: redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer;
12  * redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution;
15  * neither the name of the copyright holders nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "base/socket.hh"
33 
34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
36 #include <sys/socket.h>
37 #include <sys/types.h>
38 #include <unistd.h>
39 
40 #include <cerrno>
41 
42 #include "base/logging.hh"
43 #include "base/types.hh"
44 #include "sim/byteswap.hh"
45 
46 namespace gem5
47 {
48 
50 bool ListenSocket::anyListening = false;
51 
52 bool ListenSocket::bindToLoopback = false;
53 
54 void
56 {
57  listeningDisabled = false;
58  anyListening = false;
59  bindToLoopback = false;
60 }
61 
62 void
64 {
65  if (anyListening)
66  panic("Too late to disable all listeners, already have a listener");
67  listeningDisabled = true;
68 }
69 
70 bool
72 {
73  return listeningDisabled;
74 }
75 
76 void
78 {
79  if (anyListening)
80  panic("Too late to bind to loopback, already have a listener");
81  bindToLoopback = true;
82 }
83 
85 //
86 //
87 
89  : listening(false), fd(-1)
90 {}
91 
93 {
94  if (fd != -1)
95  close(fd);
96 }
97 
98 // Create a socket and configure it for listening
99 bool
100 ListenSocket::listen(int port, bool reuse)
101 {
102  if (listening)
103  panic("Socket already listening!");
104 
105  // only create socket if not already created by a previous call
106  if (fd == -1) {
107  fd = ::socket(PF_INET, SOCK_STREAM, 0);
108  if (fd < 0)
109  panic("Can't create socket:%s !", strerror(errno));
110  }
111 
112  if (reuse) {
113  int i = 1;
114  if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&i,
115  sizeof(i)) < 0)
116  panic("ListenSocket(listen): setsockopt() SO_REUSEADDR failed!");
117  }
118 
119  struct sockaddr_in sockaddr;
120  sockaddr.sin_family = PF_INET;
121  sockaddr.sin_addr.s_addr =
122  htobe<in_addr_t>(bindToLoopback ? INADDR_LOOPBACK : INADDR_ANY);
123  sockaddr.sin_port = htons(port);
124  // finally clear sin_zero
125  std::memset(&sockaddr.sin_zero, 0, sizeof(sockaddr.sin_zero));
126  int ret = ::bind(fd, (struct sockaddr *)&sockaddr, sizeof (sockaddr));
127  if (ret != 0) {
128  if (ret == -1 && errno != EADDRINUSE)
129  panic("ListenSocket(listen): bind() failed!");
130  return false;
131  }
132 
133  if (::listen(fd, 1) == -1) {
134  if (errno != EADDRINUSE)
135  panic("ListenSocket(listen): listen() failed!");
136 
137  return false;
138  }
139 
140  listening = true;
141  anyListening = true;
142  return true;
143 }
144 
145 
146 // Open a connection. Accept will block, so if you don't want it to,
147 // make sure a connection is ready before you call accept.
148 int
149 ListenSocket::accept(bool nodelay)
150 {
151  struct sockaddr_in sockaddr;
152  socklen_t slen = sizeof (sockaddr);
153  int sfd = ::accept(fd, (struct sockaddr *)&sockaddr, &slen);
154  if (sfd != -1 && nodelay) {
155  int i = 1;
156  if (::setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (char *)&i,
157  sizeof(i)) < 0)
158  warn("ListenSocket(accept): setsockopt() TCP_NODELAY failed!");
159  }
160 
161  return sfd;
162 }
163 
164 } // namespace gem5
gem5::ListenSocket::disableAll
static void disableAll()
Definition: socket.cc:63
gem5::ListenSocket::listening
bool listening
Definition: socket.hh:54
socket.hh
warn
#define warn(...)
Definition: logging.hh:245
gem5::ListenSocket::bindToLoopback
static bool bindToLoopback
Definition: socket.hh:45
gem5::ArmISA::fd
Bitfield< 14, 12 > fd
Definition: types.hh:150
gem5::ListenSocket::~ListenSocket
virtual ~ListenSocket()
Definition: socket.cc:92
gem5::ListenSocket::cleanup
static void cleanup()
Definition: socket.cc:55
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:66
gem5::ListenSocket::listeningDisabled
static bool listeningDisabled
The following variables are only used by socket unit tests: listeningDisabled, anyListening,...
Definition: socket.hh:42
gem5::ListenSocket::anyListening
static bool anyListening
Definition: socket.hh:43
gem5::ListenSocket::listen
virtual bool listen(int port, bool reuse=true)
Definition: socket.cc:100
gem5::ListenSocket::allDisabled
static bool allDisabled()
Definition: socket.cc:71
gem5::ListenSocket::fd
int fd
Definition: socket.hh:55
types.hh
logging.hh
gem5::ListenSocket::loopbackOnly
static void loopbackOnly()
Definition: socket.cc:77
gem5::ListenSocket::accept
virtual int accept(bool nodelay=false)
Definition: socket.cc:149
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::ListenSocket::ListenSocket
ListenSocket()
Definition: socket.cc:88
byteswap.hh
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:177

Generated on Tue Sep 21 2021 12:24:57 for gem5 by doxygen 1.8.17