Skip to content

Commit

Permalink
Revert "socketfromfd: fix cross platform usage"
Browse files Browse the repository at this point in the history
This reverts commit e69e802.
  • Loading branch information
benoitc committed Nov 26, 2019
1 parent 8c759dd commit ab25bae
Showing 1 changed file with 8 additions and 29 deletions.
37 changes: 8 additions & 29 deletions gunicorn/socketfromfd.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

__all__ = ('fromfd',)

SO_DOMAIN = getattr(socket, 'SO_DOMAIN', 39)
SO_TYPE = getattr(socket, 'SO_TYPE', 3)
SO_PROTOCOL = getattr(socket, 'SO_PROTOCOL', 38)

_libc_name = find_library('c')
if _libc_name is not None:
if sys.platform.startswith("aix"):
Expand Down Expand Up @@ -43,12 +47,8 @@ def _errcheck_errno(result, func, arguments):

if platform.system() == 'SunOS':
_libc_getsockopt = libc._so_getsockopt
_libc_getsockname = libc._so_getsockname
else:
_libc_getsockopt = libc.getsockopt
_libc_getsockname = libc.getsockname


_libc_getsockopt.argtypes = [
ctypes.c_int, # int sockfd
ctypes.c_int, # int level
Expand All @@ -59,17 +59,6 @@ def _errcheck_errno(result, func, arguments):
_libc_getsockopt.restype = ctypes.c_int # 0: ok, -1: err
_libc_getsockopt.errcheck = _errcheck_errno

class SockAddr(ctypes.Structure):
_fields_ = [
('sa_len', ctypes.c_uint8),
('sa_family', ctypes.c_uint8),
('sa_data', ctypes.c_char * 14)
]
_libc_getsockname.argtypes = [
ctypes.c_int,
ctypes.POINTER(SockAddr),
ctypes.POINTER(ctypes.c_int)
]

def _raw_getsockopt(fd, level, optname):
"""Make raw getsockopt() call for int32 optval
Expand All @@ -85,11 +74,6 @@ def _raw_getsockopt(fd, level, optname):
ctypes.byref(optval), ctypes.byref(optlen))
return optval.value

def _raw_getsockname(fd):
sockaddr = SockAddr()
len = ctypes.c_int(ctypes.sizeof(sockaddr))
_libc_getsockname(fd, sockaddr, len)
return sockaddr.sa_family

def fromfd(fd, keep_fd=True):
"""Create a socket from a file descriptor
Expand All @@ -108,15 +92,10 @@ def fromfd(fd, keep_fd=True):
:return: socket.socket instance
:raises OSError: for invalid socket fd
"""
family = _raw_getsockname(fd)
if hasattr(socket, 'SO_TYPE'):
typ = _raw_getsockopt(fd, socket.SOL_SOCKET, getattr(socket, 'SO_TYPE'))
else:
typ = socket.SOCK_STREAM
if hasattr(socket, 'SO_PROTOCOL'):
proto = _raw_getsockopt(fd, socket.SOL_SOCKET, getattr(socket, 'SO_PROTOCOL'))
else:
proto = 0
family = _raw_getsockopt(fd, socket.SOL_SOCKET, SO_DOMAIN)
typ = _raw_getsockopt(fd, socket.SOL_SOCKET, SO_TYPE)
proto = _raw_getsockopt(fd, socket.SOL_SOCKET, SO_PROTOCOL)
s
if keep_fd:
return socket.fromfd(fd, family, typ, proto)
else:
Expand Down

0 comments on commit ab25bae

Please sign in to comment.