gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
socket.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2005 The Regents of The University of Michigan
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef __SOCKET_HH__
30 #define __SOCKET_HH__
31 
32 #include <sys/socket.h>
33 #include <sys/types.h>
34 #include <sys/un.h>
35 
36 #include <cassert>
37 #include <functional>
38 #include <memory>
39 #include <string>
40 
41 #include "base/named.hh"
42 
43 namespace gem5
44 {
45 
46 class ListenSocket : public Named
47 {
48  protected:
53  static bool listeningDisabled;
54  static bool anyListening;
55 
56  static bool bindToLoopback;
57 
58  public:
59  static void disableAll();
60  static bool allDisabled();
61 
62  static void loopbackOnly();
63 
64  protected:
65  bool listening = false;
66  int fd = -1;
67 
68  void
70  {
71  listening = true;
72  anyListening = true;
73  }
74 
75  /*
76  * cleanup resets the static variables back to their default values.
77  */
78  static void cleanup();
79 
80  ListenSocket(const std::string &_name);
81 
82  public:
87  virtual ~ListenSocket();
88 
89  virtual int accept();
90  virtual void listen() = 0;
91 
92  virtual void output(std::ostream &os) const = 0;
93 
94  int getfd() const { return fd; }
95  bool islistening() const { return listening; }
96 
97  /* Create a socket, adding SOCK_CLOEXEC if available. */
98  static int socketCloexec(int domain, int type, int protocol);
99  /* Accept a connection, adding SOCK_CLOEXEC if available. */
100  static int acceptCloexec(int sockfd, struct sockaddr *addr,
101  socklen_t *addrlen); // end of api_socket
103 };
104 
105 inline static std::ostream &
106 operator << (std::ostream &os, const ListenSocket &socket)
107 {
108  socket.output(os);
109  return os;
110 }
111 
112 using ListenSocketPtr = std::unique_ptr<ListenSocket>;
113 
115 {
116  public:
117  using Builder = std::function<ListenSocketPtr(const std::string &name)>;
118 
120  ListenSocketConfig(Builder _builder) : builder(_builder) {}
121 
123  build(const std::string &name) const
124  {
125  assert(builder);
126  return builder(name);
127  }
128 
129  operator bool() const { return (bool)builder; }
130 
131  static bool parseIni(const std::string &value, ListenSocketConfig &retval);
132 
133  private:
135 };
136 
137 static inline ListenSocketConfig listenSocketEmptyConfig() { return {}; }
138 
139 // AF_INET based sockets.
140 
142 {
143  protected:
144  int _port;
145 
146  virtual bool listen(int port);
147 
148  public:
149  ListenSocketInet(const std::string &_name, int port);
150 
151  int accept() override;
152  void listen() override;
153  void output(std::ostream &os) const override;
154 };
155 
157 
158 // AF_UNIX based sockets.
159 
161 {
162  protected:
163  virtual size_t prepSockaddrUn(sockaddr_un &addr) const = 0;
164 
165  void checkPathLength(const std::string &original, size_t max_len);
166 
167  ListenSocketUnix(const std::string &_name) : ListenSocket(_name) {}
168 
169  public:
170  void listen() override;
171 };
172 
174 {
175  protected:
176  std::string dir;
177  std::string resolvedDir;
178  std::string fname;
179 
180  bool unlink() const;
181 
182  size_t prepSockaddrUn(sockaddr_un &addr) const override;
183 
184  public:
185  ListenSocketUnixFile(const std::string &_name, const std::string &_dir,
186  const std::string &_fname);
188 
189  void listen() override;
190  void output(std::ostream &os) const override;
191 };
192 
194  std::string dir, std::string fname);
195 
197 {
198  protected:
199  std::string path;
200 
201  size_t prepSockaddrUn(sockaddr_un &addr) const override;
202 
203  public:
205  const std::string &_name, const std::string &_path);
206 
207  void output(std::ostream &os) const override;
208 };
209 
211 
212 } // namespace gem5
213 
214 #endif //__SOCKET_HH__
gem5::ListenSocketUnixFile::listen
void listen() override
Definition: socket.cc:336
gem5::ListenSocket::disableAll
static void disableAll()
Definition: socket.cc:82
gem5::ListenSocketUnixFile::resolvedDir
std::string resolvedDir
Definition: socket.hh:177
gem5::ListenSocketConfig::parseIni
static bool parseIni(const std::string &value, ListenSocketConfig &retval)
Definition: socket.cc:152
gem5::ListenSocket::listening
bool listening
Definition: socket.hh:65
gem5::ListenSocketUnixFile::output
void output(std::ostream &os) const override
Definition: socket.cc:364
gem5::ListenSocket::bindToLoopback
static bool bindToLoopback
Definition: socket.hh:56
gem5::ListenSocket::~ListenSocket
virtual ~ListenSocket()
Definition: socket.cc:131
gem5::ListenSocketUnix::checkPathLength
void checkPathLength(const std::string &original, size_t max_len)
Definition: socket.cc:268
gem5::ListenSocketUnixFile::ListenSocketUnixFile
ListenSocketUnixFile(const std::string &_name, const std::string &_dir, const std::string &_fname)
Definition: socket.cc:304
gem5::ListenSocketInet::listen
void listen() override
Definition: socket.cc:243
gem5::ListenSocketUnix::prepSockaddrUn
virtual size_t prepSockaddrUn(sockaddr_un &addr) const =0
gem5::ArmISA::domain
Bitfield< 7, 4 > domain
Definition: misc_types.hh:481
named.hh
gem5::ListenSocketConfig
Definition: socket.hh:114
gem5::ListenSocketUnix::listen
void listen() override
Definition: socket.cc:276
gem5::ListenSocket
Definition: socket.hh:46
gem5::ListenSocketUnixFile::dir
std::string dir
Definition: socket.hh:176
gem5::operator<<
static std::ostream & operator<<(std::ostream &os, const DummyMatRegContainer &d)
Definition: matrix.hh:564
gem5::ListenSocket::cleanup
static void cleanup()
Definition: socket.cc:74
gem5::ListenSocketConfig::builder
Builder builder
Definition: socket.hh:134
gem5::listenSocketUnixFileConfig
ListenSocketConfig listenSocketUnixFileConfig(std::string dir, std::string fname)
Definition: socket.cc:370
gem5::ListenSocketUnixFile::unlink
bool unlink() const
Definition: socket.cc:321
gem5::ListenSocketConfig::build
ListenSocketPtr build(const std::string &name) const
Definition: socket.hh:123
gem5::ListenSocketInet::accept
int accept() override
Definition: socket.cc:182
gem5::Named
Interface for things with names.
Definition: named.hh:38
gem5::ListenSocketUnix::ListenSocketUnix
ListenSocketUnix(const std::string &_name)
Definition: socket.hh:167
gem5::ListenSocket::getfd
int getfd() const
Definition: socket.hh:94
gem5::listenSocketInetConfig
ListenSocketConfig listenSocketInetConfig(int port)
Definition: socket.cc:260
gem5::ListenSocketUnixAbstract::output
void output(std::ostream &os) const override
Definition: socket.cc:394
gem5::ListenSocket::listeningDisabled
static bool listeningDisabled
The following variables are only used by socket unit tests: listeningDisabled, anyListening,...
Definition: socket.hh:53
gem5::ListenSocket::acceptCloexec
static int acceptCloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
Definition: socket.cc:115
gem5::ListenSocket::anyListening
static bool anyListening
Definition: socket.hh:54
gem5::ListenSocketConfig::ListenSocketConfig
ListenSocketConfig(Builder _builder)
Definition: socket.hh:120
gem5::ListenSocketInet
Definition: socket.hh:141
gem5::ListenSocketUnix
Definition: socket.hh:160
gem5::X86ISA::type
type
Definition: misc.hh:734
gem5::ListenSocketInet::ListenSocketInet
ListenSocketInet(const std::string &_name, int port)
Definition: socket.cc:177
gem5::ListenSocket::ListenSocket
ListenSocket(const std::string &_name)
Definition: socket.cc:129
gem5::ListenSocketUnixFile
Definition: socket.hh:173
gem5::ListenSocketUnixAbstract::ListenSocketUnixAbstract
ListenSocketUnixAbstract(const std::string &_name, const std::string &_path)
Definition: socket.cc:386
gem5::ListenSocket::accept
virtual int accept()
Definition: socket.cc:140
gem5::ListenSocket::allDisabled
static bool allDisabled()
Definition: socket.cc:90
gem5::ListenSocketConfig::ListenSocketConfig
ListenSocketConfig()
Definition: socket.hh:119
gem5::ListenSocket::output
virtual void output(std::ostream &os) const =0
gem5::listenSocketUnixAbstractConfig
ListenSocketConfig listenSocketUnixAbstractConfig(std::string path)
Definition: socket.cc:400
gem5::ListenSocket::fd
int fd
Definition: socket.hh:66
name
const std::string & name()
Definition: trace.cc:48
gem5::ListenSocketUnixFile::fname
std::string fname
Definition: socket.hh:178
gem5::ListenSocket::listen
virtual void listen()=0
gem5::X86ISA::os
Bitfield< 17 > os
Definition: misc.hh:810
gem5::ListenSocket::socketCloexec
static int socketCloexec(int domain, int type, int protocol)
Definition: socket.cc:106
gem5::ListenSocketInet::_port
int _port
Definition: socket.hh:144
gem5::ListenSocket::loopbackOnly
static void loopbackOnly()
Definition: socket.cc:96
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::listenSocketEmptyConfig
static ListenSocketConfig listenSocketEmptyConfig()
Definition: socket.hh:137
gem5::ListenSocketConfig::Builder
std::function< ListenSocketPtr(const std::string &name)> Builder
Definition: socket.hh:117
gem5::ListenSocketUnixAbstract::prepSockaddrUn
size_t prepSockaddrUn(sockaddr_un &addr) const override
Definition: socket.cc:378
gem5::ListenSocketUnixAbstract
Definition: socket.hh:196
gem5::ListenSocket::setListening
void setListening()
Definition: socket.hh:69
gem5::Named::_name
const std::string _name
Definition: named.hh:41
gem5::ListenSocketInet::output
void output(std::ostream &os) const override
Definition: socket.cc:254
gem5::ListenSocketUnixFile::prepSockaddrUn
size_t prepSockaddrUn(sockaddr_un &addr) const override
Definition: socket.cc:328
gem5::ListenSocketUnixAbstract::path
std::string path
Definition: socket.hh:199
gem5::ListenSocketPtr
std::unique_ptr< ListenSocket > ListenSocketPtr
Definition: socket.hh:112
gem5::ListenSocketUnixFile::~ListenSocketUnixFile
~ListenSocketUnixFile()
Definition: socket.cc:311
gem5::ListenSocket::islistening
bool islistening() const
Definition: socket.hh:95
gem5::X86ISA::addr
Bitfield< 3 > addr
Definition: types.hh:84

Generated on Sun Jul 30 2023 01:56:51 for gem5 by doxygen 1.8.17