Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socket: fix: use MSG_DONTWAIT for send() and recv() #135

Merged
merged 1 commit into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/rpcclient/rpcclient/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from rpcclient.darwin.structs import timeval
from rpcclient.exceptions import BadReturnValueError
from rpcclient.structs.consts import AF_UNIX, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_RCVTIMEO, SO_SNDTIMEO, MSG_NOSIGNAL, \
EPIPE, F_GETFL, O_NONBLOCK, F_SETFL
EPIPE, F_GETFL, O_NONBLOCK, F_SETFL, MSG_DONTWAIT
from rpcclient.structs.generic import sockaddr_in, sockaddr_un, ifaddrs, sockaddr, hostent

Interface = namedtuple('Interface', 'name address netmask broadcast')
Expand Down Expand Up @@ -42,7 +42,7 @@ def send(self, buf: bytes, size: int = None) -> int:
"""
if size is None:
size = len(buf)
n = self._client.symbols.send(self.fd, buf, size, MSG_NOSIGNAL).c_int64
n = self._client.symbols.send(self.fd, buf, size, MSG_NOSIGNAL | MSG_DONTWAIT).c_int64
if n < 0:
if self._client.errno == EPIPE:
self.deallocate()
Expand All @@ -63,7 +63,7 @@ def recv(self, size: int = CHUNK_SIZE) -> bytes:
:return: received bytes
"""
with self._client.safe_malloc(size) as chunk:
err = self._client.symbols.recv(self.fd, chunk, size, 0).c_int64
err = self._client.symbols.recv(self.fd, chunk, size, MSG_DONTWAIT).c_int64
if err <= 0:
raise BadReturnValueError(f'recv() failed for fd: {self.fd} ({self._client.last_error})')
return chunk.peek(err)
Expand All @@ -73,9 +73,13 @@ def recvall(self, size: int) -> bytes:
buf = b''
with self._client.safe_malloc(size) as chunk:
while len(buf) < size:
err = self._client.symbols.recv(self.fd, chunk, size, 0).c_int64
if err <= 0:
err = self._client.symbols.recv(self.fd, chunk, size, MSG_DONTWAIT).c_int64

if err < 0:
raise BadReturnValueError(f'recv() failed for fd: {self.fd} ({self._client.last_error})')
elif err == 0:
raise BadReturnValueError(f'recv() failed for fd: {self.fd} (peer closed)')

buf += chunk.peek(err)
return buf

Expand Down
1 change: 1 addition & 0 deletions src/rpcclient/rpcclient/structs/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
SO_SNDTIMEO = 0x1005
SO_RCVTIMEO = 0x1006

MSG_DONTWAIT = 0x40
MSG_NOSIGNAL = 524288

AF_UNIX = 1
Expand Down