简体   繁体   中英

Extract numbers and characters from buffer as one whole string

I write a C program that listens to port 443 and receives SSL packets :

#include <errno.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <stdio.h>

#define MAX_SIZE 10000

void* message_processing(int sockfd)
{
    char* buff = calloc(MAX_SIZE + 1, sizeof(char));

    /*struct handshake *pkt;       
    pkt = calloc (1, sizeof (struct handshake));        */

    while (1) {

        int len_of_read_data = read(sockfd, buff, MAX_SIZE);

        if (len_of_read_data > 0) {

            FILE* file = fopen("logfile", "a");

            fprintf(file, "ssl type : %d\n", (int)buff[0]);
            fprintf(file, "ssl version : %d.%d\n", (int)buff[1], (int)buff[2]);
            fprintf(file, "ssl length : %d%d\n", (int)buff[3], (int)buff[4]);
            fprintf(file, "handshake type  : %d\n", (int)buff[5]);
            fprintf(file, "handshake len  : %d%d%d\n", (int)buff[6], (int)buff[7], (int)buff[8]);
            fprintf(file, "ssl version  : %d.%d\n", (int)buff[9], (int)buff[10]);

            fprintf(file, "random  : %c\n", buff[11]);
            fprintf(file, "random  : %c\n", buff[12]);
            fprintf(file, "random  : %c\n", buff[13]);
            fprintf(file, "random  : %c\n", buff[14]);
            fprintf(file, "random  : %c\n", buff[15]);
            fprintf(file, "random  : %c\n", buff[16]);
            fprintf(file, "random  : %c\n", buff[17]);
            fprintf(file, "random  : %c\n", buff[18]);

            fclose(file);
        }
    }
}

int main()
{
    struct sockaddr_in serv_addr;
    int sock_descriptor = socket(AF_INET, SOCK_STREAM, 0);

    int trueval = 1;
    setsockopt(sock_descriptor, SOL_SOCKET, SO_REUSEPORT | SO_REUSEADDR, (char*)&trueval, sizeof(trueval));

    bzero((char*)&serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(443);

    bind(sock_descriptor, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
    listen(sock_descriptor, 50);

    while (1) {

        struct sockaddr_in cli_addr;
        socklen_t clilen;
        clilen = sizeof(cli_addr);

        int client_socket = accept(sock_descriptor, (struct sockaddr*)&cli_addr, &clilen);

        message_processing(client_socket);

    }
}

As you can see, the program reads data from port 443 and stores them in buff variable. I extract SSL fields from buff according to SSL packet format and print them in logfile .

The problem is with buff[11] to buff[43] . This section is a random string that contains numbers and characters. I want to print buff[11] - buff[43] as one string. So I first print the bytes one by one in order to see contents (and then concatenate them into one string ), but some bytes are unreadable :

random  : �
random  : 
random  : )
random  : 
random  : M
random  : 
random  : #
random  : 

I'm new in C and I have not any idea about the solution. Can anybody guide me how can I extract this section of buff as one string that contains numbers and characters?

Your problem may have to with that you do not get the proper ints from the buffer, where the string follows these ints.

Instead of:

        fprintf(file, "ssl type : %d\n", (int)buff[0]);
        fprintf(file, "ssl version : %d.%d\n", (int)buff[1], (int)buff[2]);

Use:

        int *pInt=(int *)buff;
        fprintf(file, "ssl type : %d\n", pInt[0]);
        fprintf(file, "ssl version : %d.%d\n", pInt[1], pInt[2]);

Etc.

Following pInt[10] is the string. That would be:

        char *s= (char *)&pInt[11];

Note that (int)buff[1] makes an int from the second char of the buffer. But you already made (int)buff[0] an int, so to address the second int in the buffer, you need to add the size of an int, (int)buff[0+sizeof(int)] to get the second int form the buffer. The easier method is to use a pointer to an int, as I showed above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM