-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisten.cc
89 lines (78 loc) · 2.65 KB
/
listen.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// -*- C++ -*-
//
// Listen class implementation.
//
// Copyright 1992-2021 Deven T. Corzine <[email protected]>
//
// SPDX-License-Identifier: MIT
//
#include "phoenix.h"
boolean Listen::PortBusy(int port)
{
struct sockaddr_in saddr; // socket address
int fd; // listening socket fd
int option = 1; // option to set for setsockopt()
// Initialize listening socket.
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = INADDR_ANY;
saddr.sin_port = htons((u_short) port);
if ((fd = socket(PF_INET, SOCK_STREAM, 0)) == -1) return false;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option))) {
close(fd);
return false;
}
if (bind(fd, (struct sockaddr *) &saddr, sizeof(saddr))) {
close(fd);
return errno == EADDRINUSE;
}
close(fd);
return false;
}
void Listen::Open(int port)
{
fdtable.OpenListen(port);
}
Listen::Listen(int port) // Listen on a port.
{
const int Backlog = 8; // backlog on socket (for listen())
struct sockaddr_in saddr; // socket address
int tries = 0; // number of tries so far
int option = 1; // option to set for setsockopt()
type = ListenFD; // Identify as a Listen FD.
// Initialize listening socket.
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = htonl(INADDR_ANY);
saddr.sin_port = htons((u_short) port);
if ((fd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
error("Listen::Listen(): socket()");
}
if (fcntl(fd, F_SETFD, 0) == -1) error("Listen::Listen(): fcntl()");
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option))) {
error("Listen::Listen(): setsockopt()");
}
// Try to bind to the port. Try real hard.
while (bind(fd, (struct sockaddr *) &saddr, sizeof(saddr))) {
if (errno == EADDRINUSE) {
if (!tries++) fprintf(stderr, "Waiting for port %d.\n", port);
sleep(1);
} else {
error("Listen::Listen(): bind(port = %d)", port);
}
}
if (listen(fd, Backlog)) error("Listen::Listen(): listen()");
}
Listen::~Listen() // Listen destructor.
{
Closed();
}
void Listen::Closed() // Connection is closed.
{
if (fd == -1) return; // Skip the rest if already closed.
fdtable.Closed(fd); // Remove from FDTable.
close(fd); // Close connection.
NoReadSelect(); // Don't select closed connections!
NoWriteSelect();
fd = -1; // Connection is closed.
}