From a3d0c4bff2ebc6cd10f94725b230546c591d86e4 Mon Sep 17 00:00:00 2001 From: power47 Date: Thu, 25 Aug 2022 14:45:32 +0300 Subject: [PATCH] Implement set/get for SO_RCVBUF --- _sctp.c | 39 +++++++++++++++++++++++++++++++++++++-- sctp.py | 15 ++++++++++++++- setup.py | 4 ++-- 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/_sctp.c b/_sctp.c index 8d6d3cc..aa0dca7 100644 --- a/_sctp.c +++ b/_sctp.c @@ -82,6 +82,8 @@ static PyObject* get_adaptation(PyObject* dummy, PyObject* args); static PyObject* set_adaptation(PyObject* dummy, PyObject* args); static PyObject* get_sndbuf(PyObject* dummy, PyObject* args); static PyObject* set_sndbuf(PyObject* dummy, PyObject* args); +static PyObject* get_rcvbuf(PyObject* dummy, PyObject* args); +static PyObject* set_rcvbuf(PyObject* dummy, PyObject* args); static PyObject* set_peer_primary(PyObject* dummy, PyObject* args); static PyObject* set_primary(PyObject* dummy, PyObject* args); static PyObject* bindx(PyObject* dummy, PyObject* args); @@ -134,6 +136,8 @@ static PyMethodDef _sctp_methods[] = {"set_adaptation", set_adaptation, METH_VARARGS, ""}, {"get_sndbuf", get_sndbuf, METH_VARARGS, ""}, {"set_sndbuf", set_sndbuf, METH_VARARGS, ""}, + {"get_rcvbuf", get_rcvbuf, METH_VARARGS, ""}, + {"set_rcvbuf", set_rcvbuf, METH_VARARGS, ""}, {"get_disable_fragments", get_disable_fragments, METH_VARARGS, ""}, {"set_disable_fragments", set_disable_fragments, METH_VARARGS, ""}, {"get_events", get_events, METH_VARARGS, ""}, @@ -1175,7 +1179,7 @@ static PyObject* get_sndbuf(PyObject* dummy, PyObject* args) PyObject* ret = 0; int fd, v; socklen_t lv = sizeof(v); - + if (PyArg_ParseTuple(args, "i", &fd)) { if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &v, &lv)) { PyErr_SetFromErrno(PyExc_IOError); @@ -1198,7 +1202,38 @@ static PyObject* set_sndbuf(PyObject* dummy, PyObject* args) ret = Py_None; Py_INCREF(ret); } } - return ret; + return ret; +} + +static PyObject* get_rcvbuf(PyObject* dummy, PyObject* args) +{ + PyObject* ret = 0; + int fd, v; + socklen_t lv = sizeof(v); + + if (PyArg_ParseTuple(args, "i", &fd)) { + if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &v, &lv)) { + PyErr_SetFromErrno(PyExc_IOError); + } else { + ret = Py23_PyLong_FromLong(v); + } + } + return ret; +} + +static PyObject* set_rcvbuf(PyObject* dummy, PyObject* args) +{ + PyObject* ret = 0; + int fd, v; + + if (PyArg_ParseTuple(args, "ii", &fd, &v)) { + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &v, sizeof(v))) { + PyErr_SetFromErrno(PyExc_IOError); + } else { + ret = Py_None; Py_INCREF(ret); + } + } + return ret; } static int to_sockaddr(const char *caddr, int port, struct sockaddr* saddr, int* slen) diff --git a/sctp.py b/sctp.py index 87bcd2b..f9af39e 100644 --- a/sctp.py +++ b/sctp.py @@ -1382,7 +1382,7 @@ def get_sndbuf(self): Gets the send buffer size from the kernel for this socket. """ return _sctp.get_sndbuf(self._sk.fileno()) - + def set_sndbuf(self, rvalue): """ Sets the send buffer size in the kernel for this socket. @@ -1390,6 +1390,19 @@ def set_sndbuf(self, rvalue): # Linux doubles the size, hence we divise it by 2 _sctp.set_sndbuf(self._sk.fileno(), rvalue//2) + def get_rcvbuf(self): + """ + Gets the receive buffer size from the kernel for this socket. + """ + return _sctp.get_rcvbuf(self._sk.fileno()) + + def set_rcvbuf(self, rvalue): + """ + Sets the receive buffer size in the kernel for this socket. + """ + # Linux doubles the size, hence we divise it by 2 + _sctp.set_rcvbuf(self._sk.fileno(), rvalue//2) + def get_disable_fragments(self): """ Queries kernel and returns True if message fragmenting is disable for diff --git a/setup.py b/setup.py index 10488e1..6664091 100644 --- a/setup.py +++ b/setup.py @@ -23,13 +23,13 @@ from distutils.core import setup, Extension setup(name='pysctp', - version='0.7.1', + version='0.7.2', license = "LGPL", description = 'pysctp is a python module for the SCTP protocol stack and library', long_description = 'pysctp is a python wrapper for the SCTP protocol stack and library. On Mac OS X you will need the SCTP NKE (Kernel Extensions). On Linux systems, you need an SCTP-aware kernel (most are) and install the following packages (debian): apt install libsctp-dev libsctp1 lksctp-tools', url = "https://github.com/p1sec/pysctp", keywords = "SCTP SIGTRAN", - platforms = ["Linux", "Debian", "Ubuntu", "Mac OS X (untested)"], + platforms = ["Linux", "Debian", "Ubuntu", "Fedora", "Mac OS X (untested)"], classifiers = ['Development Status :: 5 - Production/Stable', 'Intended Audience :: Developers', 'License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)',