gem5  v20.1.0.0
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 using namespace std;
47 
49 bool ListenSocket::anyListening = false;
50 
51 bool ListenSocket::bindToLoopback = false;
52 
53 void
55 {
56  listeningDisabled = false;
57  anyListening = false;
58  bindToLoopback = false;
59 }
60 
61 void
63 {
64  if (anyListening)
65  panic("Too late to disable all listeners, already have a listener");
66  listeningDisabled = true;
67 }
68 
69 bool
71 {
72  return listeningDisabled;
73 }
74 
75 void
77 {
78  if (anyListening)
79  panic("Too late to bind to loopback, already have a listener");
80  bindToLoopback = true;
81 }
82 
84 //
85 //
86 
88  : listening(false), fd(-1)
89 {}
90 
92 {
93  if (fd != -1)
94  close(fd);
95 }
96 
97 // Create a socket and configure it for listening
98 bool
99 ListenSocket::listen(int port, bool reuse)
100 {
101  if (listening)
102  panic("Socket already listening!");
103 
104  // only create socket if not already created by a previous call
105  if (fd == -1) {
106  fd = ::socket(PF_INET, SOCK_STREAM, 0);
107  if (fd < 0)
108  panic("Can't create socket:%s !", strerror(errno));
109  }
110 
111  if (reuse) {
112  int i = 1;
113  if (::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&i,
114  sizeof(i)) < 0)
115  panic("ListenSocket(listen): setsockopt() SO_REUSEADDR failed!");
116  }
117 
118  struct sockaddr_in sockaddr;
119  sockaddr.sin_family = PF_INET;
120  sockaddr.sin_addr.s_addr =
121  htobe<in_addr_t>(bindToLoopback ? INADDR_LOOPBACK : INADDR_ANY);
122  sockaddr.sin_port = htons(port);
123  // finally clear sin_zero
124  memset(&sockaddr.sin_zero, 0, sizeof(sockaddr.sin_zero));
125  int ret = ::bind(fd, (struct sockaddr *)&sockaddr, sizeof (sockaddr));
126  if (ret != 0) {
127  if (ret == -1 && errno != EADDRINUSE)
128  panic("ListenSocket(listen): bind() failed!");
129  return false;
130  }
131 
132  if (::listen(fd, 1) == -1) {
133  if (errno != EADDRINUSE)
134  panic("ListenSocket(listen): listen() failed!");
135 
136  return false;
137  }
138 
139  listening = true;
140  anyListening = true;
141  return true;
142 }
143 
144 
145 // Open a connection. Accept will block, so if you don't want it to,
146 // make sure a connection is ready before you call accept.
147 int
148 ListenSocket::accept(bool nodelay)
149 {
150  struct sockaddr_in sockaddr;
151  socklen_t slen = sizeof (sockaddr);
152  int sfd = ::accept(fd, (struct sockaddr *)&sockaddr, &slen);
153  if (sfd != -1 && nodelay) {
154  int i = 1;
155  if (::setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (char *)&i,
156  sizeof(i)) < 0)
157  warn("ListenSocket(accept): setsockopt() TCP_NODELAY failed!");
158  }
159 
160  return sfd;
161 }
socket.hh
warn
#define warn(...)
Definition: logging.hh:239
ListenSocket::listen
virtual bool listen(int port, bool reuse=true)
Definition: socket.cc:99
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
ArmISA::fd
Bitfield< 14, 12 > fd
Definition: types.hh:159
ListenSocket::accept
virtual int accept(bool nodelay=false)
Definition: socket.cc:148
ListenSocket::~ListenSocket
virtual ~ListenSocket()
Definition: socket.cc:91
ListenSocket::listeningDisabled
static bool listeningDisabled
The following variables are only used by socket unit tests: listeningDisabled, anyListening,...
Definition: socket.hh:39
ListenSocket::bindToLoopback
static bool bindToLoopback
Definition: socket.hh:42
ListenSocket::ListenSocket
ListenSocket()
Definition: socket.cc:87
ListenSocket::fd
int fd
Definition: socket.hh:52
ListenSocket::listening
bool listening
Definition: socket.hh:51
ListenSocket::cleanup
static void cleanup()
Definition: socket.cc:54
ListenSocket::allDisabled
static bool allDisabled()
Definition: socket.cc:70
std
Overload hash function for BasicBlockRange type.
Definition: vec_reg.hh:587
types.hh
ListenSocket::loopbackOnly
static void loopbackOnly()
Definition: socket.cc:76
logging.hh
ListenSocket::disableAll
static void disableAll()
Definition: socket.cc:62
ListenSocket::anyListening
static bool anyListening
Definition: socket.hh:40
byteswap.hh
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171

Generated on Wed Sep 30 2020 14:02:07 for gem5 by doxygen 1.8.17