File: ecurl.cpp

package info (click to toggle)
spacebar 1%3A6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,448 kB
  • sloc: cpp: 4,708; xml: 293; sql: 22; makefile: 3; sh: 1
file content (183 lines) | stat: -rw-r--r-- 6,293 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// SPDX-FileCopyrightText: 2022 Michael Lang <criticaltemp@protonmail.com>
//
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL

#include "ecurl.h"
#include "modemcontroller.h"
#include "settingsmanager.h"

#include <netdb.h>

ECurl::ECurl()
{
    curl_global_init(CURL_GLOBAL_DEFAULT);
    ares_library_init(ARES_LIB_INIT_ALL);
}

ECurl::~ECurl()
{
    ares_library_cleanup();
    curl_global_cleanup();
}

QByteArray ECurl::networkRequest(const QString &url, const QByteArray &data) const
{
    CURL *curl = curl_easy_init();
    if (!curl) {
        return QByteArray();
    }
    struct curl_slist *host = NULL;

    // provide a buffer to store errors in
    char errbuf[CURL_ERROR_SIZE];
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);

    QString ifaceName = ModemController::instance().ifaceName;
    curl_easy_setopt(curl, CURLOPT_INTERFACE, (SL("if!") + ifaceName).toUtf8().constData());

    // only attempt to resolve if not using proxy
    if (SettingsManager::self()->mmsProxy().isEmpty()) {
        QString dnsServers = ModemController::instance().dnsServers;
        CURLcode supported = curl_easy_setopt(curl, CURLOPT_DNS_INTERFACE, ifaceName.toUtf8().constData());

        // use c-ares if curl was built without dns resolver support
        if (supported == CURLE_UNKNOWN_OPTION || supported == CURLE_NOT_BUILT_IN) {
            ares_channel channel;

            int aresReturn = ares_init(&channel);

            if (aresReturn != ARES_SUCCESS) {
                qDebug() << "Ares init failed:" << ares_strerror(aresReturn);
            } else {
                ares_set_local_dev(channel, ifaceName.toUtf8().constData());
                ares_set_servers_csv(channel, dnsServers.toUtf8().constData());

                CURLU *curlUrl = curl_url();
                curl_url_set(curlUrl, CURLUPART_URL, url.toUtf8().constData(), 0);
                char *hostname = url.toUtf8().data();
                curl_url_get(curlUrl, CURLUPART_HOST, &hostname, 0);
                curl_url_cleanup(curlUrl);

                char *hostIp = NULL;
                ares_gethostbyname(channel, hostname, AF_UNSPEC, aresResolveCallback, (void *)&hostIp);
                aresResolveWait(channel);

                if (hostIp == NULL) {
                    qDebug() << "Failed to resolve:" << hostname;
                }

                const char *resolve = QByteArray("+").append(hostname).append(":80:[").append(hostIp).append("]").constData();
                host = curl_slist_append(NULL, resolve);
                curl_easy_setopt(curl, CURLOPT_RESOLVE, host);

                curl_free(hostname);
                ares_destroy(channel);
            }
        } else {
            curl_easy_setopt(curl, CURLOPT_DNS_SERVERS, dnsServers.toUtf8().constData());
        }
    }

    curl_easy_setopt(curl, CURLOPT_URL, url.toUtf8().constData());
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
    curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 60L);
    curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 30L);

    // Fake user agent for carrier network compatibility
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "Android MmsLib/1.0");

    if (!SettingsManager::self()->mmsProxy().isEmpty()) {
        QString proxy = SettingsManager::self()->mmsProxy() + SL(":") + QString::number(SettingsManager::self()->mmsPort());
        curl_easy_setopt(curl, CURLOPT_PROXY, proxy.toUtf8().constData());
    }

    if (!data.isEmpty()) {
        curl_easy_setopt(curl, CURLOPT_POST, 1L);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.constData());
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data.length());

        QString len = QString::number(data.length());
        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Content-Type: application/vnd.wap.mms-message");
        headers = curl_slist_append(headers, (SL("Content-Length: ") + len).toUtf8().constData());
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    }

    QByteArray response;
    const QByteArray *pointer = &response;
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteFunction);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, pointer);

    CURLcode res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    curl_slist_free_all(host);

    if (res != CURLE_OK) {
        qDebug() << "Network Error:" << errbuf;
        return QByteArray();
    } else {
        return response;
    }
}

size_t ECurl::curlWriteFunction(char *chunk, size_t size, size_t len, QByteArray *response)
{
    response->append(chunk, static_cast<int>(size * len));
    return size * len;
}

void ECurl::aresResolveCallback(void *arg, int status, int timeouts, struct hostent *host)
{
    char **hostIp = (char **)arg;

    // when the ares handle is getting destroyed, the 'arg' pointer may not
    // be valid so only defer it when we know the 'status' says its fine!
    if (!host || status != ARES_SUCCESS) {
        return;
    }

    char ip[INET6_ADDRSTRLEN];

    for (int i = 0; host->h_addr_list[i]; ++i) {
        ares_inet_ntop(host->h_addrtype, host->h_addr_list[i], ip, sizeof(ip));
        if (i > 0) {
            strcat(*hostIp, ",");
            strcat(*hostIp, strdup(ip));
        } else {
            *hostIp = strdup(ip);
        }

        qDebug() << "Found IP for" << host->h_name << *hostIp;
    }

    (void)timeouts; // ignored
}

void ECurl::aresResolveWait(ares_channel channel)
{
    qint64 start = QTime::currentTime().msecsSinceStartOfDay();

    for (;;) {
        struct timeval *tvp, tv;
        fd_set read_fds, write_fds;
        int nfds;

        FD_ZERO(&read_fds);
        FD_ZERO(&write_fds);
        nfds = ares_fds(channel, &read_fds, &write_fds);
        if (nfds == 0) {
            break;
        }
        tvp = ares_timeout(channel, NULL, &tv);
        int count = select(nfds, &read_fds, &write_fds, NULL, tvp);
        if (count == -1) {
            qDebug() << "Error waiting for c-ares read/write descriptors";
            break;
        }
        if (QTime::currentTime().msecsSinceStartOfDay() - start > 15 * 1000) {
            qDebug() << "Resovler timeout";
            break;
        }
        ares_process(channel, &read_fds, &write_fds);
    }
}