34 #include <netinet/in.h> 35 #include <netinet/tcp.h> 36 #include <sys/socket.h> 37 #include <sys/types.h> 56 listeningDisabled =
false;
58 bindToLoopback =
false;
65 panic(
"Too late to disable all listeners, already have a listener");
66 listeningDisabled =
true;
72 return listeningDisabled;
79 panic(
"Too late to bind to loopback, already have a listener");
80 bindToLoopback =
true;
88 : listening(false),
fd(-1)
102 panic(
"Socket already listening!");
104 fd = ::socket(PF_INET, SOCK_STREAM, 0);
106 panic(
"Can't create socket:%s !", strerror(errno));
110 if (::setsockopt(
fd, SOL_SOCKET, SO_REUSEADDR, (
char *)&i,
112 panic(
"ListenSocket(listen): setsockopt() SO_REUSEADDR failed!");
115 struct sockaddr_in sockaddr;
116 sockaddr.sin_family = PF_INET;
117 sockaddr.sin_addr.s_addr =
119 sockaddr.sin_port = htons(port);
121 memset(&sockaddr.sin_zero, 0,
sizeof(sockaddr.sin_zero));
122 int ret = ::bind(
fd, (
struct sockaddr *)&sockaddr,
sizeof (sockaddr));
124 if (ret == -1 && errno != EADDRINUSE)
125 panic(
"ListenSocket(listen): bind() failed!");
130 if (errno != EADDRINUSE)
131 panic(
"ListenSocket(listen): listen() failed!");
147 struct sockaddr_in sockaddr;
148 socklen_t slen =
sizeof (sockaddr);
149 int sfd =
::accept(
fd, (
struct sockaddr *)&sockaddr, &slen);
150 if (sfd != -1 && nodelay) {
152 if (::setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, (
char *)&i,
154 warn(
"ListenSocket(accept): setsockopt() TCP_NODELAY failed!");
#define panic(...)
This implements a cprintf based panic() function.
virtual bool listen(int port, bool reuse=true)
Overload hash function for BasicBlockRange type.
static bool bindToLoopback
static bool allDisabled()
static void loopbackOnly()
virtual int accept(bool nodelay=false)
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,16,32,64}_t.
static bool listeningDisabled