Home > front end >  Socket not completely filling receive buffer
Socket not completely filling receive buffer

Time:10-05

I'm building my own netcat style listener in C for a project that I'm working on. The client (linux in this case) connects to the listener using netcat, and from the listener I'm able to send linux commands back. When running commands which give a small output (e.g. whoami, uname, pwd) the output comes back fine. However, when running a command that can give a substantial output (e.g. ls -la), I only get partial output until I send another command, which then means subsequent commands are executing the previous command. Screenshot:

Start of output

Fig 1: Start of output

Entering another command to finish output

Fig 2: Entering another command to force output to finish

One command behind

Fig 3: Now I'm one command behind, giving incorrect output.

Code as follows (includes/defines/other functions left out for brevity):

int main(int argc, char *argv[])
{
    char readbuff[262144];
    char user_input[1024] = "";
    struct sockaddr_in srv, cln;
    int bnd, checkrtr, len, lstn, new_sfd, rd, result, sfd, val;
    if(argc != 2)
    {
        fprintf(stderr, "[*]Usage: %s <target_router>\n", argv[0]);
        return -1;
    }
    check_router(argv[1]);

    // Start a listener on port 8888
    // Create the socket
    sfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sfd == -1)
    {
        fprintf(stderr, "\n[*]socket: %s (%d)", strerror(errno), errno);
        return -1;
    }
    else
    {
        fprintf(stdout, "\n[*]Socket created.");
    }

    val = 1;
    result = 0;
    result = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
    if(result == -1)
    {
        fprintf(stderr, "\n[*]setsockopt: %s (%d)", strerror(errno), errno);
    }
    else
    {
        fprintf(stdout, "\n[*]Address reuse set");
    }

    srv.sin_family = AF_INET;
    srv.sin_addr.s_addr = inet_addr(IP);
    srv.sin_port = htons(atoi(LPORT));
    len = sizeof(srv);

    bnd = bind(sfd, (struct sockaddr*)&srv, len);
    if(bnd != 0)
    {
        fprintf(stderr, "\n[*]bind: %s (%d)", strerror(errno), errno);
        return -1;
    }
    else
    {
        fprintf(stdout, "\n[*]Socket bound");
    }

    lstn = listen(sfd, 10);
    if(lstn != 0)
    {
        fprintf(stderr, "\n[*]listen: %s (%d)", strerror(errno), errno);
        return -1;
    }
    else
    {
        fprintf(stdout, "\n[*]Server listening on %s:%s", IP, LPORT);
    }
    socklen_t len_c = sizeof(cln);

    new_sfd = accept(sfd, (struct sockaddr*)&cln, &len_c);
    if(new_sfd == -1)
    {
        fprintf(stderr, "\n[*]accept: %s (%d)", strerror(errno), errno);
        return -1;
    }
    else
    {
        char *ip_c = inet_ntoa(cln.sin_addr);
        fprintf(stdout, "\n[*]New connection from client: %s:%d\n", ip_c, ntohs(cln.sin_port));
        while(1)
        {
            memset(readbuff, 0x00, sizeof(readbuff));
            memset(user_input, 0x00, sizeof(user_input));
            fgets(user_input, sizeof(user_input), stdin);
            if(user_input[0] == '\n')
            {
                continue;
            }
            int send_data = send(new_sfd, user_input, sizeof(user_input), 0);
            if(send_data == -1)
            {
                fprintf(stderr, "\n[*]send: %s (%d)", strerror(errno), errno);
                continue;
            }
            rd = read(new_sfd, readbuff, sizeof(readbuff));
            fprintf(stdout, "\n size of rd: %d", rd);
            fprintf(stdout, "\n size of readbuff: %ld", sizeof(readbuff));
            if(rd > 0)
            {
                fprintf(stdout, "\n%s", readbuff);
            }

            else if(rd == 0)
            {
                fprintf(stdout, "\n[*]Client connection closed.\n");
                continue;
                //break;
            }
            else
            {
                fprintf(stderr, "\n[*]recv: %s (%d)", strerror(errno), errno);
                continue;
            }
        }
    }
}

Is anyone able to give me a reason why the output stops partway through please?

CodePudding user response:

That's quite to be expected.

A single read can only read as much data as has been received so far (or otherwise it would need to bend time and space). The other end may also not have sent off everything they have to send by the time you issue read, so a single read won't be able to read everything either (because, well, it may not have been sent).

Since TCP is a stream protocol, you will need to devise a way to know how many bytes to expect for a single "message" from the other end, and read exactly that many bytes so you know you've gotten everything the other end has to say; I'd recommend some sort of Type-Length-Value scheme, even if you didn't need more than 1 type at present.

  • Related