implement __cinit__, shutdown, close, __dealloc__ for socket, and also add a try-except to not leak client sock from accept()
from qmsk.net.socket.socket cimport *
from qmsk.net.socket.addr cimport sockaddr, build_sockaddr
cimport qmsk.net.socket.platform as platform
cimport qmsk.net.libc as libc, qmsk.net.py as py
from qmsk.net.py cimport raise_errno
cdef parse_sockaddr (platform.sockaddr **sa_ptr, platform.socklen_t *sa_len, sockaddr addr, int optional = 0) :
if addr is not None :
addr._get_sockaddr(sa_ptr, sa_len)
elif optional :
sa_ptr[0] = NULL
sa_len[0] = 0
else :
raise ValueError(addr)
cdef parse_buf (void **buf_ptr, size_t *buf_len, object buf, int optional = 0) :
cdef libc.ssize_t tmp_len
if buf is not None :
# XXX: test that except works right
py.PyObject_AsCharBuffer(buf, <char **> buf_ptr, &tmp_len)
# XXX: ensure that this is >= 0
buf_len[0] = tmp_len
elif optional :
buf_ptr[0] = NULL
buf_len[0] = 0
else :
raise ValueError(buf)
# XXX: do some GIL-releasin'
cdef class socket :
def __cinit__ (self) :
"""
Initialize the socket to set fd to -1, so that we dont't try and close stdin too often :)
"""
self.fd = -1
def __init__ (self, int family = platform.AF_INET, int socktype = platform.SOCK_STREAM, int protocol = 0, int fd = -1) :
"""
Create a new socket endpoint with the given family/domain, socktype and optionally, specific protocol,
unless the fd argument is given as >= 0, in which case it used directly.
family - one of AF_*
socktype - one of SOCK_*
protocol - one of IPPROTO_* or zero to select default
"""
if fd >= 0 :
# given fd
self.fd = fd
else :
# socket()
self.fd = platform.socket(family, socktype, protocol)
# trap
if self.fd < 0 :
raise_errno('socket')
def bind (self, sockaddr addr) :
"""
Bind this socket to the given local socket address. The given sockaddr should be of the same or a
compatible address family.
addr - the local address to bind to. The port may be zero to let the system choose an unused
ephemeral port.
"""
cdef platform.sockaddr *sa
cdef platform.socklen_t sa_len
# XXX: require non-NULL addr?
parse_sockaddr(&sa, &sa_len, addr, 1)
# bind()
if platform.bind(self.fd, sa, sa_len) :
raise_errno('bind')
def listen (self, int backlog) :
"""
Listen for connections, marking this socket as a passive socket, which can accept incoming connection
requests using sock.accept().
It is customary to call .bind() before .listen().
backlog - maximum number of pending connections (those not yet .accept()'d).
"""
# listen()
if platform.listen(self.fd, backlog) :
raise_errno('listen')
def connect (self, sockaddr addr) :
"""
Initiate a connection, connecting this socket to the remote endpoint specified by `addr`. The given sockaddr
should be of the same or a compatible address family.
If the socket is in non-blocking mode, this will presumeably return errno.EINPROGRESS.
If the socket has not yet been bound (using .bind()), the system will pick an appropriate local address and
ephemeral port.
addr - the remote address to connect to.
"""
cdef platform.sockaddr *sa
cdef platform.socklen_t sa_len
# XXX: require non-NULL addr?
parse_sockaddr(&sa, &sa_len, addr, 1)
# connect()
if platform.connect(self.fd, sa, sa_len) :
raise_errno('connect')
def accept (self) :
"""
Accept a connection, dequeueing the first pending connection and returning a new sock object for it. This
socket must be a connection-based socket (SOCK_STREAM/SOCK_SEQPACKET) and in the passive listening mode
(.listen()).
This returns a (sock, sockaddr) tuple:
sock - the newly created sock, corresponding to the incoming connection
sockaddr - the remote address of the incoming connection
"""
# prep the sockaddr that we will return
cdef platform.sockaddr_storage ss
cdef platform.socklen_t ss_len = sizeof(ss)
cdef socket_t sock_fd
# accept()
sock_fd = platform.accept(self.fd, <platform.sockaddr *> &ss, &ss_len)
if sock_fd < 0 :
raise_errno('accept')
try :
# prep the new socket
sock_obj = socket(sock_fd)
except :
# XXX: don't leak the socket fd? How does socket.__init__ handle this?
platform.close(sock_fd)
raise
# prep the new addr
sock_addr = build_sockaddr(<platform.sockaddr *> &ss, ss_len)
return sock_obj, sock_addr
def send (self, object buf, int flags = 0) :
"""
Transmit a message to the connected remote endpoint.
buf - the data to send
flags - (optional) MSG_* flags to send with
Returns the number of bytes sent, which may be less than the length of buf.
"""
cdef void *buf_ptr
cdef size_t buf_len
cdef libc.ssize_t ret
parse_buf(&buf_ptr, &buf_len, buf, 0)
# send()
ret = platform.send(self.fd, buf_ptr, buf_len, flags)
if ret < 0 :
raise_errno('send')
else :
return ret
def sendto (self, object buf, int flags = 0, sockaddr addr = None) :
"""
Transmit a message to the given remote endpoint. If this socket is connected, the addr must not be
specified, and this acts like send()
buf - the data to send
flags - (optional) MSG_* flags to send with
addr - (optional) target address
Returns the number of bytes sent, which may be less than the length of buf.
"""
cdef void *buf_ptr
cdef size_t buf_len
cdef libc.ssize_t ret
cdef platform.sockaddr *sa
cdef platform.socklen_t sa_len
parse_sockaddr(&sa, &sa_len, addr, 1)
parse_buf(&buf_ptr, &buf_len, buf, 0)
# send()
ret = platform.sendto(self.fd, buf_ptr, buf_len, flags, sa, sa_len)
if ret < 0 :
raise_errno('sendto')
else :
return ret
def sendmsg (self, sockaddr addr = None, iov = None, control = None, int flags = 0) :
"""
Transmit an extended message to the given remote endpoint (or default for connected sockets) with the given
extra parameters.
addr - (optional) destination address (struct msghdr::msg_name)
iov - (optional) sequence of read-buffers to transmit
control - (optional) control message to transmit
flags - (optional) MSG_* flags to send with
Returns the number of bytes sent, which may be less than the total length of iov.
"""
cdef libc.ssize_t ret
cdef libc.iovec *iovec
cdef platform.msghdr msg
libc.memset(&msg, 0, sizeof(msg))
parse_sockaddr(<platform.sockaddr **> &msg.msg_name, &msg.msg_namelen, addr, 1)
parse_buf(&msg.msg_control, &msg.msg_controllen, control, 1)
# iov
if iov :
iov = tuple(iov)
# numerb of bufs = number of iovecs
msg.msg_iovlen = len(iov)
# alloca the required number of iovec's
msg.msg_iov = <libc.iovec *> libc.alloca(msg.msg_iovlen * sizeof(libc.iovec))
# fill in the iovecs
for i, buf in enumerate(iov) :
iovec = &msg.msg_iov[i]
parse_buf(&iovec.iov_base, &iovec.iov_len, buf, 1)
# sendmsg()
ret = platform.sendmsg(self.fd, &msg, flags)
if ret < 0 :
raise_errno('sendmsg')
else :
return ret
def write (self, object buf) :
"""
Write data to socket, mostly equivalent to send() with flags=0.
buf - the data to send
Returns the number of bytes sent, which may be less than the length of buf.
"""
cdef void *buf_ptr
cdef size_t buf_len
cdef libc.ssize_t ret
parse_buf(&buf_ptr, &buf_len, buf, 0)
# send()
ret = libc.write(self.fd, buf_ptr, buf_len)
if ret < 0 :
raise_errno('write')
else :
return ret
def writev (self, iov) :
"""
Write data to a socket from multiple read-buffers.
iov - sequence of read-buffers to transmit
Returns the number of bytes sent, which may be less than the total length of iov.
"""
# iov
cdef libc.iovec *iov_list = NULL
cdef size_t iov_count = 0
cdef libc.iovec *iovec
iov = tuple(iov)
# numerb of bufs = number of iovecs
iov_count = len(iov)
# alloca the required number of iovec's
iov_list = <libc.iovec *> libc.alloca(iov_count * sizeof(libc.iovec))
# fill in the iovecs
for i, buf in enumerate(iov) :
iovec = &iov_list[i]
parse_buf(&iovec.iov_base, &iovec.iov_len, buf, 1)
# sendmsg()
ret = libc.writev(self.fd, iov_list, iov_count)
if ret < 0 :
raise_errno('writev')
else :
return ret
def shutdown (self, how) :
"""
Shutdown part of a full-duplex connection.
how - one of SHUT_*
This does not affect this socket's fd.
"""
# shutdown()
if platform.shutdown(self.fd, how) :
raise_errno('shutdown')
def close (self) :
"""
Close the socket fd if we have one, invalidating it if succesful.
Note that this will raise an error and keep the fd if the system close() returns an error.
Calling this again after a succesfull close() does nothing.
XXX: SO_LINGER/blocking?
>>> s = socket()
>>> s.fd >= 0
True
>>> s.close()
>>> s.fd >= 0
False
>>> s.close()
"""
# ignore if already closed
if self.fd < 0 :
return
# close()
if libc.close(self.fd) :
raise_errno('close')
# invalidate
self.fd = -1
def __dealloc__ (self) :
"""
Close the socket fd if one is set, ignoring any errors from close
"""
if self.fd >= 0 :
if libc.close(self.fd) :
# XXX: at least warn... ?
pass