@@ -40,6 +40,24 @@ include "includes/stdlib.pxi"
40
40
include " errors.pyx"
41
41
42
42
43
+ cdef _is_sock_ip(sock_family):
44
+ return sock_family == uv.AF_INET or sock_family == uv.AF_INET6
45
+
46
+
47
+ cdef _is_sock_stream(sock_type):
48
+ # Linux's socket.type is a bitmask that can include extra info
49
+ # about socket, therefore we can't do simple
50
+ # `sock_type == socket.SOCK_STREAM`.
51
+ return (sock_type & uv.SOCK_STREAM) == uv.SOCK_STREAM
52
+
53
+
54
+ cdef _is_sock_dgram(sock_type):
55
+ # Linux's socket.type is a bitmask that can include extra info
56
+ # about socket, therefore we can't do simple
57
+ # `sock_type == socket.SOCK_DGRAM`.
58
+ return (sock_type & uv.SOCK_DGRAM) == uv.SOCK_DGRAM
59
+
60
+
43
61
cdef isfuture(obj):
44
62
if aio_isfuture is None :
45
63
return isinstance (obj, aio_Future)
@@ -1322,6 +1340,10 @@ cdef class Loop:
1322
1340
else :
1323
1341
if sock is None :
1324
1342
raise ValueError (' Neither host/port nor sock were specified' )
1343
+ if (not _is_sock_stream(sock.type) or
1344
+ not _is_sock_ip(sock.family)):
1345
+ raise ValueError (
1346
+ ' A TCP Stream Socket was expected, got {!r}' .format(sock))
1325
1347
tcp = TCPServer.new(self , protocol_factory, server, ssl,
1326
1348
uv.AF_UNSPEC)
1327
1349
@@ -1505,6 +1527,10 @@ cdef class Loop:
1505
1527
if sock is None :
1506
1528
raise ValueError (
1507
1529
' host and port was not specified and no sock specified' )
1530
+ if (not _is_sock_stream(sock.type) or
1531
+ not _is_sock_ip(sock.family)):
1532
+ raise ValueError (
1533
+ ' A TCP Stream Socket was expected, got {!r}' .format(sock))
1508
1534
1509
1535
waiter = self ._new_future()
1510
1536
tr = TCPTransport.new(self , protocol, None , waiter)
@@ -1578,8 +1604,6 @@ cdef class Loop:
1578
1604
if ssl is not None and not isinstance (ssl, ssl_SSLContext):
1579
1605
raise TypeError (' ssl argument must be an SSLContext or None' )
1580
1606
1581
- pipe = UnixServer.new(self , protocol_factory, server, ssl)
1582
-
1583
1607
if path is not None :
1584
1608
if sock is not None :
1585
1609
raise ValueError (
@@ -1594,7 +1618,6 @@ cdef class Loop:
1594
1618
try :
1595
1619
sock.bind(path)
1596
1620
except OSError as exc:
1597
- pipe._close()
1598
1621
sock.close()
1599
1622
if exc.errno == errno.EADDRINUSE:
1600
1623
# Let's improve the error message by adding
@@ -1604,7 +1627,6 @@ cdef class Loop:
1604
1627
else :
1605
1628
raise
1606
1629
except :
1607
- pipe._close()
1608
1630
sock.close()
1609
1631
raise
1610
1632
@@ -1613,11 +1635,13 @@ cdef class Loop:
1613
1635
raise ValueError (
1614
1636
' path was not specified, and no sock specified' )
1615
1637
1616
- if sock.family != uv.AF_UNIX or sock.type ! = uv.SOCK_STREAM :
1638
+ if sock.family != uv.AF_UNIX or not _is_sock_stream( sock.type) :
1617
1639
raise ValueError (
1618
1640
' A UNIX Domain Stream Socket was expected, got {!r}'
1619
1641
.format(sock))
1620
1642
1643
+ pipe = UnixServer.new(self , protocol_factory, server, ssl)
1644
+
1621
1645
try :
1622
1646
# See a comment on os_dup in create_connection
1623
1647
fileno = os_dup(sock.fileno())
@@ -1686,7 +1710,7 @@ cdef class Loop:
1686
1710
if sock is None :
1687
1711
raise ValueError (' no path and sock were specified' )
1688
1712
1689
- if sock.family != uv.AF_UNIX or sock.type ! = uv.SOCK_STREAM :
1713
+ if sock.family != uv.AF_UNIX or not _is_sock_stream( sock.type) :
1690
1714
raise ValueError (
1691
1715
' A UNIX Domain Stream Socket was expected, got {!r}'
1692
1716
.format(sock))
@@ -1989,9 +2013,9 @@ cdef class Loop:
1989
2013
1990
2014
if ssl is not None and not isinstance (ssl, ssl_SSLContext):
1991
2015
raise TypeError (' ssl argument must be an SSLContext or None' )
1992
-
1993
- if sock.type ! = uv.SOCK_STREAM:
1994
- raise ValueError ( ' invalid socket type, SOCK_STREAM expected' )
2016
+ if not _is_sock_stream(sock.type):
2017
+ raise ValueError (
2018
+ ' A Stream Socket was expected, got {!r} ' .format(sock) )
1995
2019
1996
2020
# See a comment on os_dup in create_connection
1997
2021
fileno = os_dup(sock.fileno())
@@ -2296,6 +2320,9 @@ cdef class Loop:
2296
2320
system.addrinfo * rai
2297
2321
2298
2322
if sock is not None :
2323
+ if not _is_sock_dgram(sock.type):
2324
+ raise ValueError (
2325
+ ' A UDP Socket was expected, got {!r}' .format(sock))
2299
2326
if (local_addr or remote_addr or
2300
2327
family or proto or flags or
2301
2328
reuse_address or reuse_port or allow_broadcast):
0 commit comments