Skip to content

Commit

Permalink
lib.socket: added bind, listen, SHUT_RD, SHUT_WR, SHUT_RDWR &
Browse files Browse the repository at this point in the history
socket `SO_*` flags. Plus define test.
  • Loading branch information
YoSTEALTH committed Mar 10, 2024
1 parent 581fba8 commit 9845750
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/liburing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dynamic_import import importer


__version__ = '2024.3.8'
__version__ = '2024.3.10'

importer(cache=True, exclude_dir='lib')
# - `importer()` helps this project manage all import needs. It auto scans for
Expand Down
61 changes: 61 additions & 0 deletions src/liburing/lib/socket.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ cdef extern from '<sys/socket.h>' nogil:
sa_family_t sa_family
char sa_data[14] # protocol-specific address

int __bind 'bind'(int sockfd, const __sockaddr *addr, socklen_t addrlen)
int __listen 'listen'(int sockfd, int backlog)

int __setsockopt 'setsockopt'(int sockfd,
int level,
int optname,
Expand Down Expand Up @@ -134,3 +137,61 @@ cdef extern from '<arpa/inet.h>' nogil:
# converting network to host order,
uint16_t __ntohs 'ntohs'(uint16_t netshort)
uint32_t __ntohl 'ntohl'(uint32_t netlong)


cdef extern from * nogil: # <sys/socket.h>
enum:
__SHUT_RD 'SHUT_RD'
__SHUT_WR 'SHUT_WR'
__SHUT_RDWR 'SHUT_RDWR'

enum:
__SOL_SOCKET 'SOL_SOCKET'

__SO_DEBUG 'SO_DEBUG'
__SO_REUSEADDR 'SO_REUSEADDR'
__SO_TYPE 'SO_TYPE'
__SO_ERROR 'SO_ERROR'
__SO_DONTROUTE 'SO_DONTROUTE'
__SO_BROADCAST 'SO_BROADCAST'
__SO_SNDBUF 'SO_SNDBUF'
__SO_RCVBUF 'SO_RCVBUF'
__SO_SNDBUFFORCE 'SO_SNDBUFFORCE'
__SO_RCVBUFFORCE 'SO_RCVBUFFORCE'
__SO_KEEPALIVE 'SO_KEEPALIVE'
__SO_OOBINLINE 'SO_OOBINLINE'
__SO_NO_CHECK 'SO_NO_CHECK'
__SO_PRIORITY 'SO_PRIORITY'
__SO_LINGER 'SO_LINGER'
__SO_BSDCOMPAT 'SO_BSDCOMPAT'
__SO_REUSEPORT 'SO_REUSEPORT'

__SO_PASSCRED 'SO_PASSCRED'
__SO_PEERCRED 'SO_PEERCRED'
__SO_RCVLOWAT 'SO_RCVLOWAT'
__SO_SNDLOWAT 'SO_SNDLOWAT'

__SO_BINDTODEVICE 'SO_BINDTODEVICE'

# Socket filtering
__SO_ATTACH_FILTER 'SO_ATTACH_FILTER'
__SO_DETACH_FILTER 'SO_DETACH_FILTER'
__SO_GET_FILTER 'SO_GET_FILTER'
__SO_PEERNAME 'SO_PEERNAME'
__SO_ACCEPTCONN 'SO_ACCEPTCONN'
__SO_PEERSEC 'SO_PEERSEC'
__SO_PASSSEC 'SO_PASSSEC'
__SO_MARK 'SO_MARK'
__SO_PROTOCOL 'SO_PROTOCOL'
__SO_DOMAIN 'SO_DOMAIN'
__SO_RXQ_OVFL 'SO_RXQ_OVFL'
__SO_WIFI_STATUS 'SO_WIFI_STATUS'
__SCM_WIFI_STATUS 'SCM_WIFI_STATUS'
__SO_PEEK_OFF 'SO_PEEK_OFF'

# not tested
__SO_TIMESTAMP 'SO_TIMESTAMP'
__SO_TIMESTAMPNS 'SO_TIMESTAMPNS'
__SO_TIMESTAMPING 'SO_TIMESTAMPING'
__SO_RCVTIMEO 'SO_RCVTIMEO'
__SO_SNDTIMEO 'SO_SNDTIMEO'
45 changes: 45 additions & 0 deletions test/socket/socket_define_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,48 @@ def test_define():
assert liburing.SOCK_PACKET == 10
assert liburing.SOCK_CLOEXEC == 0o2000000
assert liburing.SOCK_NONBLOCK == 0o4000

assert liburing.SHUT_RD == 0
assert liburing.SHUT_WR == 1
assert liburing.SHUT_RDWR == 2

# setsockopt & getsockopt
assert liburing.SOL_SOCKET == 1
assert liburing.SO_DEBUG == 1
assert liburing.SO_REUSEADDR == 2
assert liburing.SO_TYPE == 3
assert liburing.SO_ERROR == 4
assert liburing.SO_DONTROUTE == 5
assert liburing.SO_BROADCAST == 6
assert liburing.SO_SNDBUF == 7
assert liburing.SO_RCVBUF == 8
assert liburing.SO_SNDBUFFORCE == 32
assert liburing.SO_RCVBUFFORCE == 33
assert liburing.SO_KEEPALIVE == 9
assert liburing.SO_OOBINLINE == 10
assert liburing.SO_NO_CHECK == 11
assert liburing.SO_PRIORITY == 12
assert liburing.SO_LINGER == 13
assert liburing.SO_BSDCOMPAT == 14
assert liburing.SO_REUSEPORT == 15
assert liburing.SO_PASSCRED == 16
assert liburing.SO_PEERCRED == 17
assert liburing.SO_RCVLOWAT == 18
assert liburing.SO_SNDLOWAT == 19
assert liburing.SO_BINDTODEVICE == 25

# Socket filtering
assert liburing.SO_ATTACH_FILTER == 26
assert liburing.SO_DETACH_FILTER == 27
assert liburing.SO_GET_FILTER == liburing.SO_ATTACH_FILTER
assert liburing.SO_PEERNAME == 28
assert liburing.SO_ACCEPTCONN == 30
assert liburing.SO_PEERSEC == 31
assert liburing.SO_PASSSEC == 34
assert liburing.SO_MARK == 36
assert liburing.SO_PROTOCOL == 38
assert liburing.SO_DOMAIN == 39
assert liburing.SO_RXQ_OVFL == 40
assert liburing.SO_WIFI_STATUS == 41
assert liburing.SCM_WIFI_STATUS == liburing.SO_WIFI_STATUS
assert liburing.SO_PEEK_OFF == 42

0 comments on commit 9845750

Please sign in to comment.