2 * Copyright (c) 2020 The Regents of the University of California
5 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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.
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.
31 * Authors: Nathan Binkert
35 #include "base/socket.hh"
37 #include <netinet/in.h>
38 #include <netinet/tcp.h>
39 #include <sys/socket.h>
40 #include <sys/types.h>
45 #include "base/logging.hh"
46 #include "base/types.hh"
47 #include "sim/byteswap.hh"
51 bool ListenSocket::listeningDisabled
= false;
52 bool ListenSocket::anyListening
= false;
54 bool ListenSocket::bindToLoopback
= false;
57 ListenSocket::cleanup()
59 listeningDisabled
= false;
61 bindToLoopback
= false;
65 ListenSocket::disableAll()
68 panic("Too late to disable all listeners, already have a listener");
69 listeningDisabled
= true;
73 ListenSocket::allDisabled()
75 return listeningDisabled
;
79 ListenSocket::loopbackOnly()
82 panic("Too late to bind to loopback, already have a listener");
83 bindToLoopback
= true;
86 ////////////////////////////////////////////////////////////////////////
90 ListenSocket::ListenSocket()
91 : listening(false), fd(-1)
94 ListenSocket::~ListenSocket()
100 // Create a socket and configure it for listening
102 ListenSocket::listen(int port
, bool reuse
)
105 panic("Socket already listening!");
107 fd
= ::socket(PF_INET
, SOCK_STREAM
, 0);
109 panic("Can't create socket:%s !", strerror(errno
));
113 if (::setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&i
,
115 panic("ListenSocket(listen): setsockopt() SO_REUSEADDR failed!");
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
));
127 if (ret
== -1 && errno
!= EADDRINUSE
)
128 panic("ListenSocket(listen): bind() failed!");
132 if (::listen(fd
, 1) == -1) {
133 if (errno
!= EADDRINUSE
)
134 panic("ListenSocket(listen): listen() failed!");
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.
148 ListenSocket::accept(bool nodelay
)
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
) {
155 if (::setsockopt(sfd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&i
,
157 warn("ListenSocket(accept): setsockopt() TCP_NODELAY failed!");