gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
43namespace gem5
44{
45
46class 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);
102 // end of api_socket
103};
104
105inline static std::ostream &
106operator << (std::ostream &os, const ListenSocket &socket)
107{
108 socket.output(os);
109 return os;
110}
111
112using 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
137static 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__
ListenSocketPtr build(const std::string &name) const
Definition socket.hh:123
std::function< ListenSocketPtr(const std::string &name)> Builder
Definition socket.hh:117
static bool parseIni(const std::string &value, ListenSocketConfig &retval)
Definition socket.cc:152
ListenSocketConfig(Builder _builder)
Definition socket.hh:120
ListenSocketInet(const std::string &_name, int port)
Definition socket.cc:177
int accept() override
Definition socket.cc:182
void listen() override
Definition socket.cc:243
void output(std::ostream &os) const override
Definition socket.cc:254
size_t prepSockaddrUn(sockaddr_un &addr) const override
Definition socket.cc:378
ListenSocketUnixAbstract(const std::string &_name, const std::string &_path)
Definition socket.cc:386
void output(std::ostream &os) const override
Definition socket.cc:394
size_t prepSockaddrUn(sockaddr_un &addr) const override
Definition socket.cc:328
void output(std::ostream &os) const override
Definition socket.cc:364
void listen() override
Definition socket.cc:336
ListenSocketUnixFile(const std::string &_name, const std::string &_dir, const std::string &_fname)
Definition socket.cc:304
ListenSocketUnix(const std::string &_name)
Definition socket.hh:167
void listen() override
Definition socket.cc:276
virtual size_t prepSockaddrUn(sockaddr_un &addr) const =0
void checkPathLength(const std::string &original, size_t max_len)
Definition socket.cc:268
static bool anyListening
Definition socket.hh:54
ListenSocket(const std::string &_name)
Definition socket.cc:129
virtual void output(std::ostream &os) const =0
static bool bindToLoopback
Definition socket.hh:56
static bool listeningDisabled
The following variables are only used by socket unit tests: listeningDisabled, anyListening,...
Definition socket.hh:53
static void loopbackOnly()
Definition socket.cc:96
virtual int accept()
Definition socket.cc:140
virtual void listen()=0
static int socketCloexec(int domain, int type, int protocol)
Definition socket.cc:106
static void cleanup()
Definition socket.cc:74
static bool allDisabled()
Definition socket.cc:90
static void disableAll()
Definition socket.cc:82
void setListening()
Definition socket.hh:69
bool islistening() const
Definition socket.hh:95
int getfd() const
Definition socket.hh:94
static int acceptCloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
Definition socket.cc:115
Interface for things with names.
Definition named.hh:39
const std::string _name
Definition named.hh:41
virtual ~ListenSocket()
Definition socket.cc:131
Bitfield< 7, 4 > domain
Bitfield< 17 > os
Definition misc.hh:838
Bitfield< 3 > addr
Definition types.hh:84
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
ListenSocketConfig listenSocketUnixAbstractConfig(std::string path)
Definition socket.cc:400
ListenSocketConfig listenSocketInetConfig(int port)
Definition socket.cc:260
static std::ostream & operator<<(std::ostream &os, const DummyMatRegContainer &d)
Definition matrix.hh:564
ListenSocketConfig listenSocketUnixFileConfig(std::string dir, std::string fname)
Definition socket.cc:370
static ListenSocketConfig listenSocketEmptyConfig()
Definition socket.hh:137
std::unique_ptr< ListenSocket > ListenSocketPtr
Definition socket.hh:112
const std::string & name()
Definition trace.cc:48

Generated on Tue Jun 18 2024 16:24:01 for gem5 by doxygen 1.11.0