AF_RDS support for getsockopt

Proposal:

The buffer length used in the getsockopt() api of the python socket library is limited to the size 1024. Hence, it is not possible to get socket option values that span more than 1024 bytes in length. In the C implementation, getsockopt() has no such buflen limit.

Also, the C implementation of getsockopt() can be used to determine the correct buflen required as the API sets buffer provided with the required buflen value. In the python implementation, this parameter value is ignored.

static PyObject *
sock_getsockopt(PySocketSockObject *s, PyObject *args)
{
        //...

        if (!PyArg_ParseTuple(args, "ii|i:getsockopt",
                          &level, &optname, &buflen))
        return NULL;

        //...
        if (buflen <= 0 || buflen > 1024) {
            PyErr_SetString(PyExc_OSError,
                            "getsockopt buflen out of range");
            return NULL;
        }
        //...
}

Is there a reason why the buflen is restricted to 1024? It would be greatly helpful if this restriction is removed or atleast bypass this check for the AF_RDS type sockets.
The value returned in the in-out param buflen can also be very useful while decoding/parsing the data. In my opinion, this should also be added.

In the same sock_getsockopt API, the return value of getsockopt() is ignored. But this return value(of type integer) is very useful in parsing the buffer that is being returned.

static PyObject *
sock_getsockopt(PySocketSockObject *s, PyObject *args)
{
        //...
        buf = PyBytes_FromStringAndSize((char *)NULL, buflen);
        if (buf == NULL)
            return NULL;
        res = getsockopt(s->sock_fd, level, optname,
                         (void *)PyBytes_AS_STRING(buf), &buflen);
        if (res < 0) {
            Py_DECREF(buf);
            return s->errorhandler();
        }
        _PyBytes_Resize(&buf, buflen);
        return buf;
}

In my opinion, this return value from the getsockopt needs to be utilized/appended in the buffer that is being returned now.

I have proposed a PR for the same

Looks like this was added by Guido himself, over 30 years ago! See this commit. My initial guess would be that there’s either a better way around this (considering nobody has had the problem for 32 years, but it might just be that nobody uses RDS :slight_smile: – I could not find anyone ever encountering this problem across the entire internet), or that there was a very good reason to add the buffer limit.