0

I'm working on a project for an embedded ARM system, and on my HTTP client code I've run into an odd problem: recv() seems to only read about half of the HTTP server's response before returning...

Code:

#define MAX_CONTENT_SIZE 2048

int generate_body(char* method, char* params, int id, char* outbuf) {
    return sprintf(outbuf, "{\"method\": \"%s\", \"params\": [%s], \"id\": %d}",
        method, params, id);
}

// Generates header struct at the beginning of program,
// Should only be called ONCE!
void create_headers(getwork_headers *headers) {
    // Note: C99 struct initializers don't seem to work
    // TODO: Investigate why
    headers->post_request = "POST / HTTP/1.1\r\n";
    headers->host_details = "Host: ";
    headers->colon = ":";
    headers->user_agent = "User-Agent: Miner\r\n";
    headers->content_type = "Content-type: application/json\r\n";
    headers->header_auth = "Authorization: Basic ";
    headers->newline = "\r\n";
    headers->mining_ext = "X-Mining-Extensions: MUCHLOGIC\r\n";
    headers->content_length = "Content-Length: ";
    headers->header_end = "\r\n\r\n";
}

void update_headers(char content[MAX_CONTENT_SIZE], getwork_headers *headers,
    miner_configuration *config, char b64_auth[256], size_t bodysize) {
    memset(content, 0, MAX_CONTENT_SIZE);

    strcat(content, headers->post_request);
    strcat(content, headers->host_details);
    strcat(content, config->host);
    strcat(content, headers->colon);

    // Port in HTTP request must be readable string
    char portbuf[9];
    memset(portbuf, 0, 9);
    snprintf(portbuf, 9, "%d", config->port);

    strcat(content, portbuf);
    strcat(content, headers->newline);
    strcat(content, headers->user_agent);
    strcat(content, headers->content_type);

    // Add the authentication headers
    strcat(content, headers->header_auth);
    strcat(content, b64_auth);
    strcat(content, headers->newline);

    strcat(content, headers->mining_ext);
    strcat(content, headers->content_length);

    // Dynamically change the content length
    char bodybuf[5];
    memset(bodybuf, 0, 5);
    snprintf(bodybuf, 5, "%d", bodysize);
    strcat(content, bodybuf);

    strcat(content, headers->header_end);
}

int getwork(miner_configuration *config, char auth_str[256]) {
    // Store the HTTP request for later
    char content[MAX_CONTENT_SIZE];

    getwork_headers hdr;
    char body_text[256];
    memset(body_text, 0, 256); // Testing
    generate_body("getwork", "", 0, body_text); // Testing

    create_headers(&hdr);
    update_headers(content, &hdr, config, auth_str, strlen(body_text));

    // Add body to content
    strcat(content, body_text);

    printf(content); // Testing
    sleep(5); // Testing

    // Find the IP address of the server, with gethostbyname
    struct hostent* myhost = gethostbyname(config->host);

    // Create a TCP socket
    int rpc_socket;
    rpc_socket = socket(AF_INET, SOCK_STREAM, 0);

    // Tell the socket to connect to the IP address we found
    struct sockaddr_in sain;
    sain.sin_family = AF_INET;
    sain.sin_port = htons(config->port);
    sain.sin_addr.s_addr = *((unsigned long *)(myhost->h_addr_list[0]));
    connect(rpc_socket, (struct sockaddr *)&sain, sizeof(sain));

    // Send our request
    send(rpc_socket, content, strlen(content), 0);

    int recvd_len;
    char incoming_buffer[MAX_CONTENT_SIZE];

    memset(incoming_buffer, 0, sizeof(incoming_buffer));

    // TODO: Check if recv() is actually sucessful
    recvd_len = recv(rpc_socket, incoming_buffer, (MAX_CONTENT_SIZE - 1), 0);

    if (recvd_len <= 0) {
        return 1; // Error in recv()
    }

    incoming_buffer[recvd_len] = 0; // Null-terminate (J.I.C.)
    iprintf(incoming_buffer);

    printf("\nOther side closed connection!\n");

    // Shutdown
    shutdown(rpc_socket, 0);

    closesocket(rpc_socket);
    return 0;
}

The problem seems to be with recv(rpc_socket, incoming_buffer, (MAX_CONTENT_SIZE - 1), 0); as the request seems to be sent out just fine

Request:

POST / HTTP/1.1
Host: XXXXXXXX:8332
User-Agent: Miner
Content-type: application/json
Authorization: Basic XXXXXXX
Content-Length: 43

{"method": "getwork", "params": [], "id":0}

Any help on what the problem might would be greatly appreciated as trying to debug the embedded device is rather frustrating.

joshumax
  • 212
  • 2
  • 10

1 Answers1

1

recv() is permitted to return partial data. To get the full response, you need to call recv() in a loop.

For more details, see Handling partial return from recv() TCP in C

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012