Welcome, guest | Sign In | My Account | Store | Cart

Utility functions to create a single server socket which able to listen on both IPv4 and IPv6. Inspired by: http://bugs.python.org/issue17561

Expected usage:

>>> sock = create_server_sock(("", 8000))
>>> if not has_dual_stack(sock):
...     sock.close()
...     sock = MultipleSocketsListener([("0.0.0.0", 8000), ("::", 8000)])
>>>

From here on you have a socket which listens on port 8000, all interfaces, serving both IPv4 and IPv6. You can start accepting new connections as usual:

>>> while True:
...     conn, addr = sock.accept()
...     # handle new connection

Supports UNIX, Windows, non-blocking sockets and socket timeouts. Works with Python >= 2.6 and 3.X.

Python, 428 lines
  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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""
Utility functions to create server sockets able to listen on both
IPv4 and IPv6.  Inspired by: http://bugs.python.org/issue17561

Expected usage:

>>> sock = create_server_sock(("", 8000))
>>> if not has_dual_stack(sock):
...     sock.close()
...     sock = MultipleSocketsListener([("0.0.0.0", 8000), ("::", 8000)])
>>>

From here on you have a socket which listens on port 8000,
all interfaces, serving both IPv4 and IPv6.
You can start accepting new connections as usual:

>>> while True:
...     conn, addr = sock.accept()
...     # handle new connection

Supports UNIX, Windows, non-blocking sockets and socket timeouts.
Works with Python >= 2.6 and 3.X.
"""


import os
import sys
import socket
import select
import contextlib


__author__ = "Giampaolo Rodola' <g.rodola [AT] gmail [DOT] com>"
__license__ = "MIT"


def has_dual_stack(sock=None):
    """Return True if kernel allows creating a socket which is able to
    listen for both IPv4 and IPv6 connections.
    If *sock* is provided the check is made against it.
    """
    try:
        socket.AF_INET6
        socket.IPPROTO_IPV6
        socket.IPV6_V6ONLY
    except AttributeError:
        return False
    try:
        if sock is not None:
            return not sock.getsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY)
        else:
            sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
            with contextlib.closing(sock):
                sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False)
                return True
    except socket.error:
        return False


def create_server_sock(address, family=None, reuse_addr=None, queue_size=5,
                       dual_stack=has_dual_stack()):
    """Convenience function which creates a TCP server bound to
    *address* and return the socket object.

    Internally it takes care of choosing the right address family
    (IPv4 or IPv6) depending on the host specified in *address*
    (a (host, port) tuple.
    If host is an empty string or None all interfaces are assumed
    and if dual stack is supported by kernel the socket will be
    able to listen for both IPv4 and IPv6 connections.

    *family* can be set to either AF_INET or AF_INET6 to force the
    socket to use IPv4 or IPv6. If not set it will be determined
    from host.

    *reuse_addr* tells the kernel to reuse a local socket in TIME_WAIT
    state, without waiting for its natural timeout to expire.
    If not set will default to True on POSIX.

    *queue_size* is the maximum number of queued connections passed to
    listen() (defaults to 5).

    If *dual_stack* if True it will force the socket to listen on both
    IPv4 and IPv6 connections (defaults to True on all platforms
    natively supporting this functionality).

    The returned socket can be used to accept() new connections as in:

    >>> server = create_server_sock((None, 8000))
    >>> while True:
    ...     sock, addr = server.accept()
    ...     # handle new sock connection
    """
    AF_INET6 = getattr(socket, 'AF_INET6', 0)
    host, port = address
    if host == "":
        # http://mail.python.org/pipermail/python-ideas/2013-March/019937.html
        host = None
    if host is None and dual_stack:
        host = "::"
    if family is None:
        family = socket.AF_UNSPEC
    if reuse_addr is None:
        reuse_addr = os.name == 'posix' and sys.platform != 'cygwin'
    err = None
    info = socket.getaddrinfo(host, port, family, socket.SOCK_STREAM,
                              0, socket.AI_PASSIVE)
    if not dual_stack:
        # in case dual stack is not supported we want IPv4 to be
        # preferred over IPv6
        info.sort(key=lambda x: x[0] == socket.AF_INET, reverse=True)
    for res in info:
        af, socktype, proto, canonname, sa = res
        sock = None
        try:
            sock = socket.socket(af, socktype, proto)
            if reuse_addr:
                sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            if af == AF_INET6:
                if dual_stack:
                    # enable
                    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
                elif has_dual_stack(sock):
                    # disable
                    sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
            sock.bind(sa)
            sock.listen(queue_size)
            return sock
        except socket.error as _:
            err = _
            if sock is not None:
                sock.close()
    if err is not None:
        raise err
    else:
        raise socket.error("getaddrinfo returns an empty list")


class MultipleSocketsListener:
    """Listen on multiple addresses specified as a list of
    (host, port) tuples.
    Useful to listen on both IPv4 and IPv6 on those systems where
    a dual stack is not supported natively (Windows and many UNIXes).

    The returned instance is a socket-like object which can be used to
    accept() new connections, as with a common socket.
    Calls like settimeout() and setsockopt() will be applied to all
    sockets.
    Calls like gettimeout() or getsockopt() will refer to the first
    socket in the list.
    """

    def __init__(self, addresses, family=None, reuse_addr=None, queue_size=5):
        self._socks = []
        self._sockmap = {}
        if hasattr(select, 'poll'):
            self._pollster = select.poll()
        else:
            self._pollster = None
        completed = False
        try:
            for addr in addresses:
                sock = create_server_sock(
                    addr, family=family, reuse_addr=reuse_addr,
                    queue_size=queue_size, dual_stack=False)
                self._socks.append(sock)
                fd = sock.fileno()
                if self._pollster is not None:
                    self._pollster.register(fd, select.POLLIN)
                self._sockmap[fd] = sock
            completed = True
        finally:
            if not completed:
                self.close()

    def __enter__(self):
        return self

    def __exit__(self, *args):
        self.close()

    def __repr__(self):
        addrs = []
        for sock in self._socks:
            try:
                addrs.append(sock.getsockname())
            except socket.error:
                addrs.append(())
        return '<%s (%r) at %#x>' % (self.__class__.__name__, addrs, id(self))

    def _poll(self):
        """Return the first readable fd."""
        timeout = self.gettimeout()
        if self._pollster is None:
            fds = select.select(self._sockmap.keys(), [], [], timeout)
            if timeout and fds == ([], [], []):
                raise socket.timeout('timed out')
        else:
            if timeout is not None:
                timeout *= 1000
            fds = self._pollster.poll(timeout)
            if timeout and fds == []:
                raise socket.timeout('timed out')
        try:
            return fds[0][0]
        except IndexError:
            pass  # non-blocking socket

    def _multicall(self, name, *args, **kwargs):
        for sock in self._socks:
            meth = getattr(sock, name)
            meth(*args, **kwargs)

    def accept(self):
        """Accept a connection from the first socket which is ready
        to do so.
        """
        fd = self._poll()
        sock = self._sockmap[fd] if fd else self._socks[0]
        return sock.accept()

    def filenos(self):
        """Return sockets' file descriptors as a list of integers.
        This is useful with select().
        """
        return list(self._sockmap.keys())

    def getsockname(self):
        """Return first registered socket's own address."""
        return self._socks[0].getsockname()

    def getsockopt(self, level, optname, buflen=0):
        """Return first registered socket's options."""
        return self._socks[0].getsockopt(level, optname, buflen)

    def gettimeout(self):
        """Return first registered socket's timeout."""
        return self._socks[0].gettimeout()

    def settimeout(self, timeout):
        """Set timeout for all registered sockets."""
        self._multicall('settimeout', timeout)

    def setblocking(self, flag):
        """Set non/blocking mode for all registered sockets."""
        self._multicall('setblocking', flag)

    def setsockopt(self, level, optname, value):
        """Set option for all registered sockets."""
        self._multicall('setsockopt', level, optname, value)

    def shutdown(self, how):
        """Shut down all registered sockets."""
        self._multicall('shutdown', how)

    def close(self):
        """Close all registered sockets."""
        self._multicall('close')
        self._socks = []
        self._sockmap.clear()


# ===================================================================
# --- tests
# ===================================================================


if __name__ == '__main__':
    import unittest
    import threading
    import errno
    import time
    try:
        from test.support import find_unused_port  # PY3
    except ImportError:
        from test.test_support import find_unused_port  # PY2

    class TestCase(unittest.TestCase):

        def echo_server(self, sock):
            def run():
                with contextlib.closing(sock):
                    conn, _ = sock.accept()
                    with contextlib.closing(conn) as conn:
                        msg = conn.recv(1024)
                        if not msg:
                            return
                        conn.sendall(msg)

            t = threading.Thread(target=run)
            t.start()
            time.sleep(.1)

        def test_create_server_sock(self):
            port = find_unused_port()
            sock = create_server_sock((None, port))
            with contextlib.closing(sock):
                self.assertEqual(sock.getsockname()[1], port)
                self.assertEqual(sock.type, socket.SOCK_STREAM)
                if has_dual_stack():
                    self.assertEqual(sock.family, socket.AF_INET6)
                else:
                    self.assertEqual(sock.family, socket.AF_INET)
                self.echo_server(sock)
                cl = socket.create_connection(('localhost', port), timeout=2)
                with contextlib.closing(cl):
                    cl.sendall(b'foo')
                    self.assertEqual(cl.recv(1024), b'foo')

        def test_has_dual_stack(self):
            # IPv4 sockets are not supposed to support dual stack
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            with contextlib.closing(sock):
                sock.bind(("", 0))
                self.assertFalse(has_dual_stack(sock=sock))

        def test_dual_stack(self):
            sock = create_server_sock((None, 0))
            with contextlib.closing(sock):
                self.echo_server(sock)
                port = sock.getsockname()[1]
                cl = socket.create_connection(("127.0.0.1", port), timeout=2)
                with contextlib.closing(cl):
                    cl.sendall(b'foo')
                    self.assertEqual(cl.recv(1024), b'foo')

            sock = create_server_sock((None, 0))
            with contextlib.closing(sock):
                self.echo_server(sock)
                port = sock.getsockname()[1]
                if has_dual_stack():
                    self.assertTrue(has_dual_stack(sock=sock))
                    cl = socket.create_connection(("::1", port), timeout=2)
                    with contextlib.closing(cl):
                        cl.sendall(b'foo')
                        self.assertEqual(cl.recv(1024), b'foo')
                else:
                    self.assertFalse(has_dual_stack(sock=sock))
                    try:
                        socket.create_connection(("::1", port))
                    except socket.error as err:
                        if os.name == 'nt':
                            code = errno.WSAECONNREFUSED
                        else:
                            code = errno.ECONNREFUSED
                        self.assertEqual(err.errno, code)
                    else:
                        self.fail('exception not raised')

                    # just stop server
                    cl = socket.create_connection(("127.0.0.1", port), timeout=2)
                    with contextlib.closing(sock):
                        cl.sendall(b'foo')
                        cl.recv(1024)
                    if hasattr(unittest, 'skip'):  # PY >= 2.7
                        unittest.skip('dual stack cannot be tested as not '
                                      'supported')

        # --- multiple listener tests

        def test_mlistener(self):
            port = find_unused_port()
            # v4
            sock = MultipleSocketsListener(
                [('127.0.0.1', port), ('::1', port)])
            with contextlib.closing(sock):
                self.echo_server(sock)
                port = sock.getsockname()[1]
                cl = socket.create_connection(("127.0.0.1", port), timeout=2)
                with contextlib.closing(cl):
                    cl.sendall(b'foo')
                    self.assertEqual(cl.recv(1024), b'foo')
            # v6
            sock = MultipleSocketsListener(
                [('127.0.0.1', port), ('::1', port)])
            with contextlib.closing(sock):
                self.echo_server(sock)
                port = sock.getsockname()[1]
                cl = socket.create_connection(("::1", port), timeout=2)
                with contextlib.closing(cl):
                    cl.sendall(b'foo')
                    self.assertEqual(cl.recv(1024), b'foo')

        def test_mlistener_timeout(self):
            sock = MultipleSocketsListener([('127.0.0.1', 0), ('::1', 0)])
            sock.settimeout(.01)
            self.assertRaises(socket.timeout, sock.accept)

        def test_mlistener_nonblocking(self):
            sock = MultipleSocketsListener([('127.0.0.1', 0), ('::1', 0)])
            sock.setblocking(False)
            try:
                sock.accept()
            except socket.error as err:
                if os.name == 'nt':
                    code = errno.WSAEWOULDBLOCK
                else:
                    code = errno.EAGAIN
                self.assertEqual(err.errno, code)
            else:
                self.fail('exception not raised')

        def test_mlistener_ctx_manager(self):
            with MultipleSocketsListener([("0.0.0.0", 0), ("::", 0)]) as msl:
                pass
            self.assertEqual(msl._socks, [])
            self.assertEqual(msl._sockmap, {})

        def test_mlistener_overridden_meths(self):
            with MultipleSocketsListener([("0.0.0.0", 0), ("::", 0)]) as msl:
                self.assertEqual(
                    bool(msl.getsockopt(
                        socket.SOL_SOCKET, socket.SO_REUSEADDR)),
                    os.name == 'posix')
                self.assertEqual(msl.getsockname()[0], "0.0.0.0")
                self.assertTrue(msl.filenos())
                msl.setblocking(True)
                msl.settimeout(2)
                self.assertEqual(msl.gettimeout(), 2)
                try:
                    msl.setsockopt(
                        socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
                except socket.error:
                    pass

    test_suite = unittest.TestSuite()
    test_suite.addTest(unittest.makeSuite(TestCase))
    unittest.TextTestRunner(verbosity=2).run(test_suite)

1 comment

Mara 10 years, 10 months ago  # | flag

I saw this and was very surprised this wasn't something just masked behind scapy. I like this one and will likely be keeping this one around for some testing and logging. :)