1. Add libcurl debug code
2. Fix save failured register information when server return error 3. Fix DeviceName Changed when each run register device.
This commit is contained in:
parent
b95f4d3f26
commit
ffc9ac4285
|
@ -11,6 +11,7 @@
|
|||
#include "http.h"
|
||||
#include "log.h"
|
||||
|
||||
#define LIBCURL_DEBUG (0)
|
||||
#define HTTPS_RANDOM_LEN (14)
|
||||
|
||||
static size_t __writeDataCb(void *pData, size_t size, size_t nmemb, void *pParams)
|
||||
|
@ -38,18 +39,121 @@ const char* calc_register_checksum(char* pSecKey, char* pRandKey, int timeStamp,
|
|||
return hash_sha1((unsigned char*)utstring_body(pMsg), utstring_len(pMsg), pCheckSum);
|
||||
}
|
||||
|
||||
#if LIBCURL_DEBUG
|
||||
struct data {
|
||||
char trace_ascii; /* 1 or 0 */
|
||||
};
|
||||
|
||||
static
|
||||
void dump(const char *text,
|
||||
FILE *stream, unsigned char *ptr, size_t size,
|
||||
char nohex)
|
||||
{
|
||||
size_t i;
|
||||
size_t c;
|
||||
|
||||
unsigned int width = 0x10;
|
||||
|
||||
if(nohex)
|
||||
/* without the hex output, we can fit more on screen */
|
||||
width = 0x40;
|
||||
|
||||
fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n",
|
||||
text, (long)size, (long)size);
|
||||
|
||||
for(i = 0; i<size; i += width) {
|
||||
|
||||
fprintf(stream, "%4.4lx: ", (long)i);
|
||||
|
||||
if(!nohex) {
|
||||
/* hex not disabled, show it */
|
||||
for(c = 0; c < width; c++)
|
||||
if(i + c < size)
|
||||
fprintf(stream, "%02x ", ptr[i + c]);
|
||||
else
|
||||
fputs(" ", stream);
|
||||
}
|
||||
|
||||
for(c = 0; (c < width) && (i + c < size); c++) {
|
||||
/* check for 0D0A; if found, skip past and start a new line of output */
|
||||
if(nohex && (i + c + 1 < size) && ptr[i + c] == 0x0D &&
|
||||
ptr[i + c + 1] == 0x0A) {
|
||||
i += (c + 2 - width);
|
||||
break;
|
||||
}
|
||||
fprintf(stream, "%c",
|
||||
(ptr[i + c] >= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.');
|
||||
/* check again for 0D0A, to avoid an extra \n if it's at width */
|
||||
if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D &&
|
||||
ptr[i + c + 2] == 0x0A) {
|
||||
i += (c + 3 - width);
|
||||
break;
|
||||
}
|
||||
}
|
||||
fputc('\n', stream); /* newline */
|
||||
}
|
||||
fflush(stream);
|
||||
}
|
||||
|
||||
static
|
||||
int my_trace(CURL *handle, curl_infotype type,
|
||||
char *data, size_t size,
|
||||
void *userp)
|
||||
{
|
||||
struct data *config = (struct data *)userp;
|
||||
const char *text;
|
||||
(void)handle; /* prevent compiler warning */
|
||||
|
||||
switch(type) {
|
||||
case CURLINFO_TEXT:
|
||||
fprintf(stderr, "== Info: %s", data);
|
||||
/* FALLTHROUGH */
|
||||
default: /* in case a new one is introduced to shock us */
|
||||
return 0;
|
||||
|
||||
case CURLINFO_HEADER_OUT:
|
||||
text = "=> Send header";
|
||||
break;
|
||||
case CURLINFO_DATA_OUT:
|
||||
text = "=> Send data";
|
||||
break;
|
||||
case CURLINFO_SSL_DATA_OUT:
|
||||
text = "=> Send SSL data";
|
||||
break;
|
||||
case CURLINFO_HEADER_IN:
|
||||
text = "<= Recv header";
|
||||
break;
|
||||
case CURLINFO_DATA_IN:
|
||||
text = "<= Recv data";
|
||||
break;
|
||||
case CURLINFO_SSL_DATA_IN:
|
||||
text = "<= Recv SSL data";
|
||||
break;
|
||||
}
|
||||
|
||||
dump(text, stderr, (unsigned char *)data, size, config->trace_ascii);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
int device_register(void)
|
||||
{
|
||||
CURLcode ret;
|
||||
CURL* pCurl = NULL;
|
||||
struct curl_slist* pList = NULL;
|
||||
const char* pPostMsg = "";
|
||||
UT_string* pProduct, *pNonce, *pCurTime, *pChecksum;
|
||||
UT_string* pProduct, *pNonce, *pCurTime, *pChecksum, *pPostData;
|
||||
char randKey[HTTPS_RANDOM_LEN + 1];
|
||||
char checkSum[SHA1_STR_LEN + 1];
|
||||
UT_string* pResult = NULL;
|
||||
time_t timsStamp = time(NULL);
|
||||
|
||||
|
||||
#if LIBCURL_DEBUG
|
||||
struct data config;
|
||||
config.trace_ascii = 1; /* enable ascii tracing */
|
||||
#endif
|
||||
|
||||
hal_cleanup_device_register_info();
|
||||
|
||||
pCurl = curl_easy_init();
|
||||
|
||||
if(pCurl == NULL)
|
||||
|
@ -68,6 +172,7 @@ int device_register(void)
|
|||
utstring_new(pNonce);
|
||||
utstring_new(pCurTime);
|
||||
utstring_new(pChecksum);
|
||||
utstring_new(pPostData);
|
||||
|
||||
calc_register_checksum(NETEASE_PRODUCT_SEC, randKey, timsStamp, checkSum);
|
||||
|
||||
|
@ -75,12 +180,13 @@ int device_register(void)
|
|||
utstring_printf(pNonce, "Nonce: %s", randKey);
|
||||
utstring_printf(pCurTime, "CurTime: %d", timsStamp);
|
||||
utstring_printf(pChecksum, "CheckSum: %s", checkSum);
|
||||
utstring_printf(pPostData, "DeviceName=%s", hal_get_device_id());
|
||||
|
||||
pList = curl_slist_append(pList, utstring_body(pProduct));
|
||||
pList = curl_slist_append(pList, utstring_body(pNonce));
|
||||
pList = curl_slist_append(pList, utstring_body(pCurTime));
|
||||
pList = curl_slist_append(pList, utstring_body(pChecksum));
|
||||
pList = curl_slist_append(pList, "Content-Type:application/json;charset=UTF-8");
|
||||
pList = curl_slist_append(pList, "Content-Type:application/x-www-form-urlencoded");
|
||||
|
||||
curl_easy_setopt(pCurl, CURLOPT_URL, NETEASE_API_SERVER);
|
||||
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, __writeDataCb);
|
||||
|
@ -88,15 +194,20 @@ int device_register(void)
|
|||
curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 1L);
|
||||
curl_easy_setopt(pCurl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
|
||||
curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, pList);
|
||||
curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, NULL);
|
||||
curl_easy_setopt(pCurl, CURLOPT_POSTFIELDSIZE, (long)0);
|
||||
|
||||
curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, utstring_body(pPostData));
|
||||
curl_easy_setopt(pCurl, CURLOPT_POSTFIELDSIZE, (long)utstring_len(pPostData));
|
||||
#if LIBCURL_DEBUG
|
||||
curl_easy_setopt(pCurl, CURLOPT_DEBUGFUNCTION, my_trace);
|
||||
curl_easy_setopt(pCurl, CURLOPT_DEBUGDATA, &config);
|
||||
curl_easy_setopt(pCurl, CURLOPT_VERBOSE, 1L);
|
||||
#endif
|
||||
#if 0
|
||||
curl_easy_setopt(pCurl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt");
|
||||
curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 1L);
|
||||
#else
|
||||
curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
ret = curl_easy_perform(pCurl);
|
||||
|
||||
curl_slist_free_all(pList);
|
||||
|
@ -115,8 +226,12 @@ int device_register(void)
|
|||
|
||||
curl_easy_cleanup(pCurl);
|
||||
LOG_EX(LOG_Debug, "HTTP Post Finished:\n%s\n", utstring_body(pResult));
|
||||
hal_save_device_register_info(utstring_body(pResult), utstring_len(pResult));
|
||||
hal_get_regist_info(utstring_body(pResult));
|
||||
|
||||
if(hal_get_regist_info(utstring_body(pResult)) == 0)
|
||||
{
|
||||
hal_save_device_register_info(utstring_body(pResult), utstring_len(pResult));
|
||||
}
|
||||
|
||||
utstring_free(pResult);
|
||||
|
||||
return 0;
|
||||
|
|
Loading…
Reference in New Issue