首页 > 解决方案 > 用于在检索响应时与 POP3 本地服务器不一致进行交互的 Openssl 库

问题描述

我正在使用 openSSL 库处理 pop3 clinet。但是我遇到了这个不一致的问题,当从 BIO 套接字读取时,5 次中有 4 次在末尾有“\r\n.\r\n”,表示来自服务器的多行响应结束。但有时由于某种原因只返回“\r\n”,其余的“.\r\n”出现在下一个 BIO_read 中。缓冲区没有溢出,它设置为 1024 长度,即使在 604 个字符长的响应中也会发生这种情况。

例子:

+OK 555 octets
Return-Path: test1@mail.local
Received: from [127.0.0.1] (mail.local [127.0.0.1])
    by LAPTOP-R30GIL79 with ESMTPA
    ; Wed, 20 Oct 2021 11:55:24 +0200
Message-ID: <dad24da8-dd91-62ed-3338-95fe46890c32@mail.local>
Date: Wed, 20 Oct 2021 11:55:24 +0200
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
 Thunderbird/91.2.0
Content-Language: sk
To: test1@mail.local
From: test1 <test1@mail.local>
Subject: testing
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Text testovacej spravy.

.

之后调用它完全相同会导致:

+OK 555 octets
Return-Path: test1@mail.local
Received: from [127.0.0.1] (mail.local [127.0.0.1])
    by LAPTOP-R30GIL79 with ESMTPA
    ; Wed, 20 Oct 2021 11:55:24 +0200
Message-ID: <dad24da8-dd91-62ed-3338-95fe46890c32@mail.local>
Date: Wed, 20 Oct 2021 11:55:24 +0200
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
 Thunderbird/91.2.0
Content-Language: sk
To: test1@mail.local
From: test1 <test1@mail.local>
Subject: testing
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Text testovacej spravy.
\r\n

如您所见,由于某种原因缺少“.\r\n”。八位字节确实匹配,但它以某种方式消失了。没有内存泄漏。这是我用来读取 BIO 套接字的函数

int readBIO(){
    memset(buf, 0, strlen(buf));
    int x = BIO_read(bio,buf,BUFF_SIZE);
    if (x == 0 )
    {
        fprintf(stderr, "Error: Trying to read from closed connection.\n");
        freeAll();
        exit(1);
    }else if(x < 0){
        if (! BIO_should_retry(bio))
        {
            fprintf(stderr, "Error: Reading from bio socked has failed.\n");
            freeAll();
            exit(1);
        }
    }
    if (checkResponse() != 0)
    {
        fprintf(stderr, "Error: Incorrect command / typo in command detected.\n Or wrong username / password has been entered.\n Server responded with -ERR.\n");
        freeAll();
        exit(1);
    }
    
    return 0;
}

任何人都可能知道为什么会这样?

标签: copensslpop3

解决方案


推荐阅读