如何通过 ssl 和套接字发送目标文件 .o

发布于 2024-12-12 01:45:14 字数 1214 浏览 0 评论 0原文

我需要将目标文件从客户端发送到服务器程序。 我尝试读取该文件,将其存储到缓冲区中,通过 ssl 发送缓冲区并写入服务器程序中的文件。 这不适用于 .o 文件。这就是 ELF 的输出。

这是我的一些代码

读取文件

void readFile(char filename[])  
{
    FILE        *input_file;
    char        line[BUFSIZ];

    input_file = fopen(filename, "r");

    if(input_file == NULL){
        printf("cannot open input_file '%s'\n", filename );
        exit(EXIT_FAILURE);
    }

while (fgets(line,sizeof line, input_file) != NULL) {

    for(int i = 0; i<strlen(line); i++){
        current_file[i] = line[i];
    }   
}}

客户端发送文件

    readFile(filename);
    ctx = InitCTX();  
    server = OpenConnection(hostname, atoi(portnum));  
    ssl = SSL_new(ctx);      /* create new SSL connection state */  
    SSL_set_fd(ssl, server);    /* attach the socket descriptor */  
    if ( SSL_connect(ssl) == FAIL )   /* perform the connection */  
        ERR_print_errors_fp(stderr);  
    else  
    {   

        printf("Connected with %s encryption\n", SSL_get_cipher(ssl));  
        ShowCerts(ssl);        /* get any certs */  
        SSL_write(ssl, current_file, strlen(current_file));

是否可以读取目标文件,然后将其存储在缓冲区中?

还有其他方法可以发送这些文件吗?

I need to send an object file from my client to the server program.
I tried reading the file, storing it into a buffer, sent the buffer over the ssl and wrote to a file in the server program.
This did not work for .o files. This was the out put ELF.

This is some of my code

Read file

void readFile(char filename[])  
{
    FILE        *input_file;
    char        line[BUFSIZ];

    input_file = fopen(filename, "r");

    if(input_file == NULL){
        printf("cannot open input_file '%s'\n", filename );
        exit(EXIT_FAILURE);
    }

while (fgets(line,sizeof line, input_file) != NULL) {

    for(int i = 0; i<strlen(line); i++){
        current_file[i] = line[i];
    }   
}}

Client sends the file

    readFile(filename);
    ctx = InitCTX();  
    server = OpenConnection(hostname, atoi(portnum));  
    ssl = SSL_new(ctx);      /* create new SSL connection state */  
    SSL_set_fd(ssl, server);    /* attach the socket descriptor */  
    if ( SSL_connect(ssl) == FAIL )   /* perform the connection */  
        ERR_print_errors_fp(stderr);  
    else  
    {   

        printf("Connected with %s encryption\n", SSL_get_cipher(ssl));  
        ShowCerts(ssl);        /* get any certs */  
        SSL_write(ssl, current_file, strlen(current_file));

Is it possible to read object file, and then store it in a buffer?

Is there other methods to send these file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

来日方长 2024-12-19 01:45:15

这是用于缓冲二进制文件(如“对象”文件)的示例代码。为了简洁起见,不进行检查和指针维护。

 FILE *file;
 char *buffer;
 unsigned long len;
 unsigned char buffer*;
 //open in binary mode
 file = fopen(name, "rb");
 //check file size
 fseek(file, 0, SEEK_END);
 len = ftell(file);
 fseek(file, 0, SEEK_SET);
 //allocate buffer accordingly
 buffer = (unsigned char *) calloc (1, fileLen + 1); //1 byte for the terminator
 //Store into buffer
 fread(buffer, len, 1, file);

This is a sample code for buffering binary files like the "object" ones. Checks and pointers maintenance are not there for brevity sake.

 FILE *file;
 char *buffer;
 unsigned long len;
 unsigned char buffer*;
 //open in binary mode
 file = fopen(name, "rb");
 //check file size
 fseek(file, 0, SEEK_END);
 len = ftell(file);
 fseek(file, 0, SEEK_SET);
 //allocate buffer accordingly
 buffer = (unsigned char *) calloc (1, fileLen + 1); //1 byte for the terminator
 //Store into buffer
 fread(buffer, len, 1, file);
沫离伤花 2024-12-19 01:45:14

.o 文件没有什么“特殊”之处,它只是存储在光盘上的一系列字节,并为其分配了文件名和扩展名。

我的猜测是,您需要以不同于现有文件的方式处理该文件,因为它将是二进制数据。它不会有您当前正在阅读的行的“通常”概念。

我会看看以下内容:
http://www. linuxquestions.org/questions/programming-9/c-howto-read-binary-file-into-buffer-172985/

尝试先在本地读取然后写入文件,以检查它是否正在被读取在传输过程中或本地 I/O 例程中是否损坏?

There's nothing 'special' about a .o file, it's simply a series of bytes stored on disc, with a filename and extension assigned to it.

My guess would be that you need to handle the file differently from what you have there, as it's going to be binary data. It won't have the 'usual' concept of lines that you're currently reading in.

I'd have a look at the following:
http://www.linuxquestions.org/questions/programming-9/c-howto-read-binary-file-into-buffer-172985/

Try reading then writing your file locally first to check whether it's being corrupted in transit or in the local I/O routines?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文