darkhttpd
darkhttpd copied to clipboard
`maxconn` option does not limit simultaneous connections
The --maxconn N
option does not limit the number of simultaneous connections as described.
This value is being used as the backlog
parameter to listen
. At least on Linux, this does not limit the number of concurrent connections, because once a connection is accepted, it's no long part of the pending connection queue.
One way to fix this would be to count the number of open connections, and avoid adding the listening socket to the file descriptor set if the count reaches the maximum.
Here's a Python script to reproduce the issue:
import socket
PORT = 8080
def main():
request = b'GET /darkhttpd.c HTTP/1.1\r\n\r\n'
sockets = []
while True:
s = socket.socket()
s.settimeout(1)
s.connect(("", PORT))
s.send(request)
data = s.recv(1024)
sockets.append(s)
print('{} connections open...'.format(len(sockets)))
if __name__ == '__main__':
main()
Yep, you're right.