Skip to content

Commit

Permalink
Fix races in server tube from Gallopsled#1067 and apply Gallopsled#1569
Browse files Browse the repository at this point in the history
  • Loading branch information
Arusekk committed Oct 20, 2020
1 parent 447aa55 commit 755b041
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 18 deletions.
2 changes: 0 additions & 2 deletions pwnlib/tubes/listen.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ def __init__(self, port=0, bindaddr='::',
super(listen, self).__init__(*args, **kwargs)

port = int(port)
fam = {socket.AF_INET: 'ipv4',
socket.AF_INET6: 'ipv6'}.get(fam, fam)

fam = self._get_family(fam)
typ = self._get_type(typ)
Expand Down
23 changes: 7 additions & 16 deletions pwnlib/tubes/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pwnlib.log import getLogger
from pwnlib.tubes.sock import sock
from pwnlib.tubes.remote import remote
from six.moves.queue import Queue

log = getLogger(__name__)

Expand Down Expand Up @@ -67,19 +68,17 @@ class server(sock):

_accepter = None

def __init__(self, port=0, bindaddr = "0.0.0.0", fam = "any", typ = "tcp",
def __init__(self, port=0, bindaddr = "::", fam = "any", typ = "tcp",
callback = None, blocking = False, *args, **kwargs):
super(server, self).__init__(*args, **kwargs)

port = int(port)
fam = {socket.AF_INET: 'ipv4',
socket.AF_INET6: 'ipv6'}.get(fam, fam)

fam = self._get_family(fam)
typ = self._get_type(typ)

if fam == socket.AF_INET6 and bindaddr == '0.0.0.0':
bindaddr = '::'
if fam == socket.AF_INET and bindaddr == '::':
bindaddr = '0.0.0.0'

h = self.waitfor('Trying to bind to %s on port %d' % (bindaddr, port))

Expand All @@ -104,8 +103,7 @@ def __init__(self, port=0, bindaddr = "0.0.0.0", fam = "any", typ = "tcp",
h.success()

self.sock = listen_sock
self.connections_waiting = threading.Event()
self.connections = []
self.connections = Queue()
def accepter():
while True:
h = self.waitfor('Waiting for connections on %s:%s' % (self.lhost, self.lport))
Expand Down Expand Up @@ -139,21 +137,14 @@ def accepter():
else:
callback(r)
else:
self.connections.append(r)
if not self.connections_waiting.is_set():
self.connections_waiting.set()
self.connections.put(r)

self._accepter = context.Thread(target = accepter)
self._accepter.daemon = True
self._accepter.start()

def next_connection(self):
if not self.connections_waiting.is_set():
self.connections_waiting.wait()
conn = self.connections.pop(0)
if not self.connections:
self.connections_waiting.clear()
return conn
return self.connections.get()

def close(self):
# since `close` is scheduled to run on exit we must check that we got
Expand Down

0 comments on commit 755b041

Please sign in to comment.