From 16b8e66c64ee8acd6ea2ce34372e52406a990d2a Mon Sep 17 00:00:00 2001 From: Erik Prabowo Kamal Date: Fri, 30 Aug 2019 18:52:32 +0800 Subject: Managed to post login credentials to the RT server For now, it's just to get a response --- rtclient.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ rtclient.h | 1 + 2 files changed, 45 insertions(+) diff --git a/rtclient.c b/rtclient.c index 1be0579..c489398 100644 --- a/rtclient.c +++ b/rtclient.c @@ -25,6 +25,50 @@ bool rtclient_init() return (bool)handle; } +static size_t handle_login(const char *response, size_t size, size_t nmemb, void *writedata) +{ +#ifdef DEBUG +#ifdef ANDROID + __android_log_print(ANDROID_LOG_DEBUG, "libkelakon.so", "Login response:\n%s", response); +#else + fprintf(stderr, "Login response:\n%s\n", response); +#endif // ANDROID +#endif // DEBUG + return size * nmemb; +} + +void rtclient_login(const char *name, const char *password) +{ + struct curl_httppost *post, *last = NULL; + curl_formadd(&post, &last, + CURLFORM_COPYNAME, "user", + CURLFORM_PTRCONTENTS, name, + CURLFORM_END); + curl_formadd(&post, &last, + CURLFORM_COPYNAME, "pass", + CURLFORM_PTRCONTENTS, password, + CURLFORM_END); + last = NULL; + + curl_easy_setopt(handle, CURLOPT_URL, "https://darapsa.co.id/rt"); + curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, handle_login); + curl_easy_setopt(handle, CURLOPT_HTTPPOST, post); +#ifdef DEBUG + CURLcode res = +#endif // DEBUG + curl_easy_perform(handle); +#ifdef DEBUG + if (res != CURLE_OK) { + const char *error = curl_easy_strerror(res); +#ifdef ANDROID + __android_log_print(ANDROID_LOG_ERROR, "libkelakon.so", "cURL perform error: %s", error); +#else + fprintf(stderr, "cURL perform error: %s\n", error); +#endif // ANDROID + } +#endif // DEBUG +} + void rtclient_cleanup() { if (handle) diff --git a/rtclient.h b/rtclient.h index 53b8a51..dfeea5b 100644 --- a/rtclient.h +++ b/rtclient.h @@ -6,6 +6,7 @@ extern "C" { #endif bool rtclient_init(); + void rtclient_login(const char *name, const char *password); void rtclient_cleanup(); #ifdef __cplusplus -- cgit v1.2.3