SOCK_SEQPACKET behavior on small buffer - Unix

This is a discussion on SOCK_SEQPACKET behavior on small buffer - Unix ; Hi, What does one do if the buffer passed in for read()/recv()/recvmsg() is smaller than the message that is going to be returned from a SOCK_SEQPACKET-type socket? Usually it just returns the head of the particular message and discards the ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: SOCK_SEQPACKET behavior on small buffer

  1. SOCK_SEQPACKET behavior on small buffer

    Hi,


    What does one do if the buffer passed in for read()/recv()/recvmsg() is
    smaller than the message that is going to be returned from a
    SOCK_SEQPACKET-type socket?
    Usually it just returns the head of the particular message and discards
    the rest. The test program below will store "He" and " W" in buf. I
    tried toying with MSG_EOR in the hope that repeatedly calling recvmsg()
    and checking for it would return me what has been discarded but this is
    not the case.
    How would one go about grabbing the part of the message that would be
    discarded without enlarging the buffer? (A buffer size of 2 is just to
    show the issue; the message may be bigger anytime than the buffer.)

    ---
    #include
    #include
    #include

    int main(void)
    {
    int sp[2], n;
    char buf[2], cbuf[2000];
    struct iovec iov = {buf, sizeof(buf)};
    struct msghdr mh = {
    .msg_iov = &iov,
    .msg_iovlen = 1,
    .msg_control = cbuf,
    .msg_controllen = sizeof(cbuf),
    };

    socketpair(PF_LOCAL, SOCK_DGRAM, 0, sp);
    send(sp[0], "Hello ", 6, MSG_EOR);
    send(sp[0], " World", 6, MSG_EOR);
    while ((n = recvmsg(sp[1], &mh, 0)) >= 0)
    printf("%d %u\n", n, mh.msg_flags);
    return 0;
    }
    ---

    Jan
    --

  2. Re: SOCK_SEQPACKET behavior on small buffer

    On Sep 23, 3:11*pm, Jan Engelhardt wrote:

    > What does one do if the buffer passed in for read()/recv()/recvmsg() is
    > smaller than the message that is going to be returned from a
    > SOCK_SEQPACKET-type socket?


    If you do this, you lose part of the message.

    > Usually it just returns the head of the particular message and discards
    > the rest.


    Always.

    > The test program below will store "He" and " W" in buf. I
    > tried toying with MSG_EOR in the hope that repeatedly calling recvmsg()
    > and checking for it would return me what has been discarded but this is
    > not the case.
    > How would one go about grabbing the part of the message that would be
    > discarded without enlarging the buffer? (A buffer size of 2 is just to
    > show the issue; the message may be bigger anytime than the buffer.)


    You can't. As the spec says, "for message-based sockets such as
    SOCK_DGRAM and SOCK_SEQPACKET, the entire message must be read in a
    single operation."
    http://www.opengroup.org/onlinepubs/.../xns/recv.html

    Though this makes sense for datagrams, IMO it makes no sense for
    sequenced packets. But that's life.

    DS

  3. Re: SOCK_SEQPACKET behavior on small buffer


    On Sep 24 2008 10:26, David Schwartz wrote:
    >
    >> The test program below will store "He" and " W" in buf. I
    >> tried toying with MSG_EOR in the hope that repeatedly calling recvmsg()
    >> and checking for it would return me what has been discarded but this is
    >> not the case.
    >> How would one go about grabbing the part of the message that would be
    >> discarded without enlarging the buffer? (A buffer size of 2 is just to
    >> show the issue; the message may be bigger anytime than the buffer.)

    >
    > You can't. As the spec says, "for message-based sockets such as
    > SOCK_DGRAM and SOCK_SEQPACKET, the entire message must be read in a
    > single operation."
    > http://www.opengroup.org/onlinepubs/.../xns/recv.html


    By enlarging I actually meant upping the hardcoded value.
    Is there perhaps a way to obtain the size of the message, to pass
    that to malloc()?

    Unfortunately, according to the manpage of recvmsg(2), it is not possible to
    pass in both MSG_PEEK and MSG_TRUNC as flags to recv.


    Jan
    --

  4. Re: SOCK_SEQPACKET behavior on small buffer

    On Sep 24, 1:54*pm, Jan Engelhardt wrote:

    > By enlarging I actually meant upping the hardcoded value.
    > Is there perhaps a way to obtain the size of the message, to pass
    > that to malloc()?


    Allocate a buffer that's the largest your protocol allows. If you need
    to keep the data around, and the buffer is way too big, copy the data
    into a correctly-sized buffer.

    DS

+ Reply to Thread