C / C ++程序在Raspberry Pi上无法正常工作

时间:2014-08-12 12:13:12

标签: c++ raspberry-pi raspbian


我写了程序,通过我的gmail地址发送5封电子邮件,暂停1分钟。这个程序适用于我的笔记本电脑,但不适用于Raspberry Pi。在Raspberry Pi上它只发送一封电子邮件。

main.cpp中:

#include <string>
#include <thread>
#include <chrono>

#include "email.c"

void threadFunction()
{
    for (int i = 0; i < 5; i++)
    {
        std::string message = /*"Hello my name is Dmitry\r\nTest1\r\nTest2\r\n" + */std::to_string(i) + "\r\n";
        int status = sent_email("*********", "*********", "Test message", message.c_str(), "smtps://smtp.gmail.com", "*********", "*********");
        std::this_thread::sleep_for(std::chrono::minutes(1));
        status++;
    }
}

int main()
{
     /*std::thread thr(threadFunction);
     thr.join();*/
     threadFunction();
     return 0;
}

email.c:

#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

struct upload_information {
    const char *data;
    size_t data_length;
    size_t sent;
};

size_t read_callback(char *buffer, size_t size, size_t nitems, void *instream);

int sent_email(const char *FROM, const char *TO, const char *SUBJECT, const char *message, const char *server, const char *login, const char *password)
{
    CURL *curl;
    CURLcode res = CURLE_OK;
    struct curl_slist *recipients = NULL;

    size_t length = strlen(message) + strlen(FROM) + strlen(TO) + strlen(SUBJECT) + 32; //32 is FROM: <>/n/r, TO: <>/n/r length and SUBJECT + new line(after header - SMTP standart) + /0

    char *data;
    data = (char*)malloc(sizeof(char) * length);

    strcat(data, "FROM: <");
    strcat(data, FROM);
    strcat(data, ">\r\n");
    strcat(data, "TO: <");
    strcat(data, TO);
    strcat(data, ">\r\n");
    strcat(data, "SUBJECT: ");
    strcat(data, SUBJECT);
    strcat(data, "\r\n\r\n");
    strcat(data, message);

    struct upload_information upload_info;
    upload_info.data = data;
    upload_info.data_length = length;
    upload_info.sent = 0;

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_USERNAME, login);
        curl_easy_setopt(curl, CURLOPT_PASSWORD, password);

        curl_easy_setopt(curl, CURLOPT_URL, server);

        curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
        recipients = curl_slist_append(recipients, TO);
        //recipients = curl_slist_append(recipients, CC);
        curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
        curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
        curl_easy_setopt(curl, CURLOPT_READDATA, &upload_info);
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
        //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        res = curl_easy_perform(curl);

        if(res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));

        curl_slist_free_all(recipients);
        curl_easy_cleanup(curl);
    }

    free(data);
    return (int)res;
}

size_t read_callback(char *buffer, size_t size, size_t nitems, void *instream)
{
    struct upload_information *upload_info = (struct upload_information *)instream;

    if ((size == 0) || (nitems == 0) || (size * nitems < 1))
        return 0;

    if (upload_info->sent < upload_info->data_length)
    {
        size_t length = size * nitems;

        if (length > upload_info->data_length - upload_info->sent)
            length = upload_info->data_length - upload_info->sent;

        memcpy(buffer, upload_info->data + upload_info->sent, length);

        upload_info->sent += length;

        return length;
    }

    return 0;
}

Raspberry Pi上的错误消息:
*** glibc detected *** ./Mailer: double free or corruption (!prev): 0x01d0da38 ***

P.S。树莓派节目发送1条正确的信息和第二条信息的一部分 P.S.S Post更新了

2 个答案:

答案 0 :(得分:3)

您为消息分配的缓冲区是1个字节到短。您要添加的所有常量字符串的总长度为29,但您还需要为'\0'字符添加一个字节strcat将终止该字符串。

如果运行以下代码,则会显示所有字符串文字的连接长度为30

#include <stdio.h>

int
main() {
    printf("%d\n", (int) sizeof("FROM: <" ">\r\n"
                                "TO: <" ">\r\n"
                                "SUBJECT: " "\r\n"));
}

该消息还有另外两个问题。一个是你没有根据需要在标题和消息之间加一个空行。另一个是您没有使用\r\n终止邮件的最后一行。这可能会混淆curl库,因为它需要添加行结束符本身。如果没有,则无法正确通过SMTP发送邮件。

答案 1 :(得分:0)

我已经解决了我的问题。问题是因为当我释放内存时我没有重置内存。

我为strcat添加*data = '\0';