util.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. * Copyright 2011 Con Kolivas
  3. * Copyright 2010 Jeff Garzik
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 2 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #define _GNU_SOURCE
  11. #include "config.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <ctype.h>
  15. #include <stdarg.h>
  16. #include <string.h>
  17. #include <jansson.h>
  18. #include <curl/curl.h>
  19. #include <time.h>
  20. #include <curses.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <sys/types.h>
  24. #ifndef WIN32
  25. # include <sys/socket.h>
  26. # include <netinet/in.h>
  27. # include <netinet/tcp.h>
  28. #else
  29. # include <winsock2.h>
  30. # include <mstcpip.h>
  31. #endif
  32. #include "miner.h"
  33. #include "elist.h"
  34. #if JANSSON_MAJOR_VERSION >= 2
  35. #define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
  36. #else
  37. #define JSON_LOADS(str, err_ptr) json_loads((str), (err_ptr))
  38. #endif
  39. bool successful_connect = false;
  40. struct data_buffer {
  41. void *buf;
  42. size_t len;
  43. };
  44. struct upload_buffer {
  45. const void *buf;
  46. size_t len;
  47. };
  48. struct header_info {
  49. char *lp_path;
  50. bool has_rolltime;
  51. };
  52. struct tq_ent {
  53. void *data;
  54. struct list_head q_node;
  55. };
  56. void vapplog(int prio, const char *fmt, va_list ap)
  57. {
  58. extern bool use_curses;
  59. #ifdef HAVE_SYSLOG_H
  60. if (use_syslog) {
  61. vsyslog(prio, fmt, ap);
  62. }
  63. #else
  64. if (0) {}
  65. #endif
  66. else if (opt_log_output || prio <= LOG_NOTICE) {
  67. char *f;
  68. int len;
  69. struct timeval tv = { };
  70. struct tm tm;
  71. gettimeofday(&tv, NULL);
  72. localtime_r(&tv.tv_sec, &tm);
  73. len = 40 + strlen(fmt) + 22;
  74. f = alloca(len);
  75. sprintf(f, "[%d-%02d-%02d %02d:%02d:%02d] %s\n",
  76. tm.tm_year + 1900,
  77. tm.tm_mon + 1,
  78. tm.tm_mday,
  79. tm.tm_hour,
  80. tm.tm_min,
  81. tm.tm_sec,
  82. fmt);
  83. /* Only output to stderr if it's not going to the screen as well */
  84. if (!isatty(fileno((FILE *)stderr))) {
  85. va_list apc;
  86. va_copy(apc, ap);
  87. vfprintf(stderr, f, apc); /* atomic write to stderr */
  88. fflush(stderr);
  89. }
  90. if (use_curses)
  91. log_curses(prio, f, ap);
  92. else {
  93. int len = strlen(f);
  94. strcpy(f + len - 1, " \n");
  95. log_curses(prio, f, ap);
  96. }
  97. }
  98. }
  99. void applog(int prio, const char *fmt, ...)
  100. {
  101. va_list ap;
  102. va_start(ap, fmt);
  103. vapplog(prio, fmt, ap);
  104. va_end(ap);
  105. }
  106. static void databuf_free(struct data_buffer *db)
  107. {
  108. if (!db)
  109. return;
  110. free(db->buf);
  111. memset(db, 0, sizeof(*db));
  112. }
  113. static size_t all_data_cb(const void *ptr, size_t size, size_t nmemb,
  114. void *user_data)
  115. {
  116. struct data_buffer *db = user_data;
  117. size_t len = size * nmemb;
  118. size_t oldlen, newlen;
  119. void *newmem;
  120. static const unsigned char zero = 0;
  121. oldlen = db->len;
  122. newlen = oldlen + len;
  123. newmem = realloc(db->buf, newlen + 1);
  124. if (!newmem)
  125. return 0;
  126. db->buf = newmem;
  127. db->len = newlen;
  128. memcpy(db->buf + oldlen, ptr, len);
  129. memcpy(db->buf + newlen, &zero, 1); /* null terminate */
  130. return len;
  131. }
  132. static size_t upload_data_cb(void *ptr, size_t size, size_t nmemb,
  133. void *user_data)
  134. {
  135. struct upload_buffer *ub = user_data;
  136. int len = size * nmemb;
  137. if (len > ub->len)
  138. len = ub->len;
  139. if (len) {
  140. memcpy(ptr, ub->buf, len);
  141. ub->buf += len;
  142. ub->len -= len;
  143. }
  144. return len;
  145. }
  146. static size_t resp_hdr_cb(void *ptr, size_t size, size_t nmemb, void *user_data)
  147. {
  148. struct header_info *hi = user_data;
  149. size_t remlen, slen, ptrlen = size * nmemb;
  150. char *rem, *val = NULL, *key = NULL;
  151. void *tmp;
  152. val = calloc(1, ptrlen);
  153. key = calloc(1, ptrlen);
  154. if (!key || !val)
  155. goto out;
  156. tmp = memchr(ptr, ':', ptrlen);
  157. if (!tmp || (tmp == ptr)) /* skip empty keys / blanks */
  158. goto out;
  159. slen = tmp - ptr;
  160. if ((slen + 1) == ptrlen) /* skip key w/ no value */
  161. goto out;
  162. memcpy(key, ptr, slen); /* store & nul term key */
  163. key[slen] = 0;
  164. rem = ptr + slen + 1; /* trim value's leading whitespace */
  165. remlen = ptrlen - slen - 1;
  166. while ((remlen > 0) && (isspace(*rem))) {
  167. remlen--;
  168. rem++;
  169. }
  170. memcpy(val, rem, remlen); /* store value, trim trailing ws */
  171. val[remlen] = 0;
  172. while ((*val) && (isspace(val[strlen(val) - 1]))) {
  173. val[strlen(val) - 1] = 0;
  174. }
  175. if (!*val) /* skip blank value */
  176. goto out;
  177. if (opt_protocol)
  178. applog(LOG_DEBUG, "HTTP hdr(%s): %s", key, val);
  179. if (!strcasecmp("X-Roll-Ntime", key)) {
  180. if (!strncasecmp("N", val, 1)) {
  181. if (opt_debug)
  182. applog(LOG_DEBUG, "X-Roll-Ntime: N found");
  183. } else {
  184. if (opt_debug)
  185. applog(LOG_DEBUG, "X-Roll-Ntime found");
  186. hi->has_rolltime = true;
  187. }
  188. }
  189. if (!strcasecmp("X-Long-Polling", key)) {
  190. hi->lp_path = val; /* steal memory reference */
  191. val = NULL;
  192. }
  193. out:
  194. free(key);
  195. free(val);
  196. return ptrlen;
  197. }
  198. #ifdef CURL_HAS_SOCKOPT
  199. int json_rpc_call_sockopt_cb(void *userdata, curl_socket_t fd, curlsocktype purpose)
  200. {
  201. int keepalive = 1;
  202. int tcp_keepcnt = 5;
  203. int tcp_keepidle = 120;
  204. int tcp_keepintvl = 120;
  205. #ifndef WIN32
  206. if (unlikely(setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive))))
  207. return 1;
  208. # ifdef __linux
  209. if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &tcp_keepcnt, sizeof(tcp_keepcnt))))
  210. return 1;
  211. if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &tcp_keepidle, sizeof(tcp_keepidle))))
  212. return 1;
  213. if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &tcp_keepintvl, sizeof(tcp_keepintvl))))
  214. return 1;
  215. # endif /* __linux */
  216. # ifdef __APPLE_CC__
  217. if (unlikely(setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &tcp_keepintvl, sizeof(tcp_keepintvl))))
  218. return 1;
  219. # endif /* __APPLE_CC__ */
  220. #else /* WIN32 */
  221. struct tcp_keepalive vals;
  222. vals.onoff = 1;
  223. vals.keepalivetime = tcp_keepidle * 1000;
  224. vals.keepaliveinterval = tcp_keepintvl * 1000;
  225. DWORD outputBytes;
  226. if (unlikely(WSAIoctl(fd, SIO_KEEPALIVE_VALS, &vals, sizeof(vals), NULL, 0, &outputBytes, NULL, NULL)))
  227. return 1;
  228. #endif /* WIN32 */
  229. return 0;
  230. }
  231. #endif
  232. json_t *json_rpc_call(CURL *curl, const char *url,
  233. const char *userpass, const char *rpc_req,
  234. bool probe, bool longpoll, bool *rolltime,
  235. struct pool *pool)
  236. {
  237. json_t *val, *err_val, *res_val;
  238. int rc;
  239. struct data_buffer all_data = { };
  240. struct upload_buffer upload_data;
  241. json_error_t err = { };
  242. struct curl_slist *headers = NULL;
  243. char len_hdr[64], user_agent_hdr[128];
  244. char curl_err_str[CURL_ERROR_SIZE];
  245. long timeout = longpoll ? (60 * 60) : 60;
  246. struct header_info hi = { };
  247. bool probing = false;
  248. /* it is assumed that 'curl' is freshly [re]initialized at this pt */
  249. if (probe) {
  250. probing = !pool->probed;
  251. /* Probe for only 15 seconds */
  252. timeout = 15;
  253. }
  254. curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
  255. #if 0 /* Disable curl debugging since it spews to stderr */
  256. if (opt_protocol)
  257. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  258. #endif
  259. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  260. curl_easy_setopt(curl, CURLOPT_URL, url);
  261. curl_easy_setopt(curl, CURLOPT_ENCODING, "");
  262. curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  263. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, all_data_cb);
  264. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &all_data);
  265. curl_easy_setopt(curl, CURLOPT_READFUNCTION, upload_data_cb);
  266. curl_easy_setopt(curl, CURLOPT_READDATA, &upload_data);
  267. curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_err_str);
  268. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  269. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, resp_hdr_cb);
  270. curl_easy_setopt(curl, CURLOPT_HEADERDATA, &hi);
  271. curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_TRY);
  272. if (userpass) {
  273. curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
  274. curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  275. }
  276. #ifdef CURL_HAS_SOCKOPT
  277. if (longpoll)
  278. curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, json_rpc_call_sockopt_cb);
  279. #endif
  280. curl_easy_setopt(curl, CURLOPT_POST, 1);
  281. if (opt_protocol)
  282. applog(LOG_DEBUG, "JSON protocol request:\n%s", rpc_req);
  283. upload_data.buf = rpc_req;
  284. upload_data.len = strlen(rpc_req);
  285. sprintf(len_hdr, "Content-Length: %lu",
  286. (unsigned long) upload_data.len);
  287. sprintf(user_agent_hdr, "User-Agent: %s", PACKAGE_STRING);
  288. headers = curl_slist_append(headers,
  289. "Content-type: application/json");
  290. headers = curl_slist_append(headers,
  291. "X-Mining-Extensions: midstate rollntime");
  292. headers = curl_slist_append(headers, len_hdr);
  293. headers = curl_slist_append(headers, user_agent_hdr);
  294. headers = curl_slist_append(headers, "Expect:"); /* disable Expect hdr*/
  295. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  296. rc = curl_easy_perform(curl);
  297. if (rc) {
  298. applog(LOG_INFO, "HTTP request failed: %s", curl_err_str);
  299. goto err_out;
  300. }
  301. if (!all_data.buf) {
  302. if (opt_debug)
  303. applog(LOG_DEBUG, "Empty data received in json_rpc_call.");
  304. goto err_out;
  305. }
  306. if (probing) {
  307. pool->probed = true;
  308. /* If X-Long-Polling was found, activate long polling */
  309. if (hi.lp_path)
  310. pool->hdr_path = hi.lp_path;
  311. else
  312. pool->hdr_path = NULL;
  313. }
  314. *rolltime = hi.has_rolltime;
  315. val = JSON_LOADS(all_data.buf, &err);
  316. if (!val) {
  317. applog(LOG_INFO, "JSON decode failed(%d): %s", err.line, err.text);
  318. if (opt_protocol)
  319. applog(LOG_DEBUG, "JSON protocol response:\n%s", all_data.buf);
  320. goto err_out;
  321. }
  322. if (opt_protocol) {
  323. char *s = json_dumps(val, JSON_INDENT(3));
  324. applog(LOG_DEBUG, "JSON protocol response:\n%s", s);
  325. free(s);
  326. }
  327. /* JSON-RPC valid response returns a non-null 'result',
  328. * and a null 'error'.
  329. */
  330. res_val = json_object_get(val, "result");
  331. err_val = json_object_get(val, "error");
  332. if (!res_val || json_is_null(res_val) ||
  333. (err_val && !json_is_null(err_val))) {
  334. char *s;
  335. if (err_val)
  336. s = json_dumps(err_val, JSON_INDENT(3));
  337. else
  338. s = strdup("(unknown reason)");
  339. applog(LOG_INFO, "JSON-RPC call failed: %s", s);
  340. free(s);
  341. goto err_out;
  342. }
  343. successful_connect = true;
  344. databuf_free(&all_data);
  345. curl_slist_free_all(headers);
  346. curl_easy_reset(curl);
  347. return val;
  348. err_out:
  349. databuf_free(&all_data);
  350. curl_slist_free_all(headers);
  351. curl_easy_reset(curl);
  352. if (!successful_connect)
  353. applog(LOG_DEBUG, "Failed to connect in json_rpc_call");
  354. curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1);
  355. return NULL;
  356. }
  357. char *bin2hex(const unsigned char *p, size_t len)
  358. {
  359. int i;
  360. char *s = malloc((len * 2) + 1);
  361. if (!s)
  362. return NULL;
  363. for (i = 0; i < len; i++)
  364. sprintf(s + (i * 2), "%02x", (unsigned int) p[i]);
  365. return s;
  366. }
  367. bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
  368. {
  369. while (*hexstr && len) {
  370. char hex_byte[3];
  371. unsigned int v;
  372. if (!hexstr[1]) {
  373. applog(LOG_ERR, "hex2bin str truncated");
  374. return false;
  375. }
  376. hex_byte[0] = hexstr[0];
  377. hex_byte[1] = hexstr[1];
  378. hex_byte[2] = 0;
  379. if (sscanf(hex_byte, "%x", &v) != 1) {
  380. applog(LOG_ERR, "hex2bin sscanf '%s' failed", hex_byte);
  381. return false;
  382. }
  383. *p = (unsigned char) v;
  384. p++;
  385. hexstr += 2;
  386. len--;
  387. }
  388. return (len == 0 && *hexstr == 0) ? true : false;
  389. }
  390. /* Subtract the `struct timeval' values X and Y,
  391. storing the result in RESULT.
  392. Return 1 if the difference is negative, otherwise 0. */
  393. int
  394. timeval_subtract (
  395. struct timeval *result, struct timeval *x, struct timeval *y)
  396. {
  397. /* Perform the carry for the later subtraction by updating Y. */
  398. if (x->tv_usec < y->tv_usec) {
  399. int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
  400. y->tv_usec -= 1000000 * nsec;
  401. y->tv_sec += nsec;
  402. }
  403. if (x->tv_usec - y->tv_usec > 1000000) {
  404. int nsec = (x->tv_usec - y->tv_usec) / 1000000;
  405. y->tv_usec += 1000000 * nsec;
  406. y->tv_sec -= nsec;
  407. }
  408. /* Compute the time remaining to wait.
  409. `tv_usec' is certainly positive. */
  410. result->tv_sec = x->tv_sec - y->tv_sec;
  411. result->tv_usec = x->tv_usec - y->tv_usec;
  412. /* Return 1 if result is negative. */
  413. return x->tv_sec < y->tv_sec;
  414. }
  415. bool fulltest(const unsigned char *hash, const unsigned char *target)
  416. {
  417. unsigned char hash_swap[32], target_swap[32];
  418. uint32_t *hash32 = (uint32_t *) hash_swap;
  419. uint32_t *target32 = (uint32_t *) target_swap;
  420. int i;
  421. bool rc = true;
  422. char *hash_str, *target_str;
  423. swap256(hash_swap, hash);
  424. swap256(target_swap, target);
  425. for (i = 0; i < 32/4; i++) {
  426. uint32_t h32tmp = swab32(hash32[i]);
  427. uint32_t t32tmp = target32[i];
  428. target32[i] = swab32(target32[i]); /* for printing */
  429. if (h32tmp > t32tmp) {
  430. rc = false;
  431. break;
  432. }
  433. if (h32tmp < t32tmp) {
  434. rc = true;
  435. break;
  436. }
  437. }
  438. if (opt_debug) {
  439. hash_str = bin2hex(hash_swap, 32);
  440. target_str = bin2hex(target_swap, 32);
  441. applog(LOG_DEBUG, " Proof: %s\nTarget: %s\nTrgVal? %s",
  442. hash_str,
  443. target_str,
  444. rc ? "YES (hash < target)" :
  445. "no (false positive; hash > target)");
  446. free(hash_str);
  447. free(target_str);
  448. }
  449. return rc;
  450. }
  451. struct thread_q *tq_new(void)
  452. {
  453. struct thread_q *tq;
  454. tq = calloc(1, sizeof(*tq));
  455. if (!tq)
  456. return NULL;
  457. INIT_LIST_HEAD(&tq->q);
  458. pthread_mutex_init(&tq->mutex, NULL);
  459. pthread_cond_init(&tq->cond, NULL);
  460. return tq;
  461. }
  462. void tq_free(struct thread_q *tq)
  463. {
  464. struct tq_ent *ent, *iter;
  465. if (!tq)
  466. return;
  467. list_for_each_entry_safe(ent, iter, &tq->q, q_node) {
  468. list_del(&ent->q_node);
  469. free(ent);
  470. }
  471. pthread_cond_destroy(&tq->cond);
  472. pthread_mutex_destroy(&tq->mutex);
  473. memset(tq, 0, sizeof(*tq)); /* poison */
  474. free(tq);
  475. }
  476. static void tq_freezethaw(struct thread_q *tq, bool frozen)
  477. {
  478. mutex_lock(&tq->mutex);
  479. tq->frozen = frozen;
  480. pthread_cond_signal(&tq->cond);
  481. mutex_unlock(&tq->mutex);
  482. }
  483. void tq_freeze(struct thread_q *tq)
  484. {
  485. tq_freezethaw(tq, true);
  486. }
  487. void tq_thaw(struct thread_q *tq)
  488. {
  489. tq_freezethaw(tq, false);
  490. }
  491. bool tq_push(struct thread_q *tq, void *data)
  492. {
  493. struct tq_ent *ent;
  494. bool rc = true;
  495. ent = calloc(1, sizeof(*ent));
  496. if (!ent)
  497. return false;
  498. ent->data = data;
  499. INIT_LIST_HEAD(&ent->q_node);
  500. mutex_lock(&tq->mutex);
  501. if (!tq->frozen) {
  502. list_add_tail(&ent->q_node, &tq->q);
  503. } else {
  504. free(ent);
  505. rc = false;
  506. }
  507. pthread_cond_signal(&tq->cond);
  508. mutex_unlock(&tq->mutex);
  509. return rc;
  510. }
  511. void *tq_pop(struct thread_q *tq, const struct timespec *abstime)
  512. {
  513. struct tq_ent *ent;
  514. void *rval = NULL;
  515. int rc;
  516. mutex_lock(&tq->mutex);
  517. if (!list_empty(&tq->q))
  518. goto pop;
  519. if (abstime)
  520. rc = pthread_cond_timedwait(&tq->cond, &tq->mutex, abstime);
  521. else
  522. rc = pthread_cond_wait(&tq->cond, &tq->mutex);
  523. if (rc)
  524. goto out;
  525. if (list_empty(&tq->q))
  526. goto out;
  527. pop:
  528. ent = list_entry(tq->q.next, struct tq_ent, q_node);
  529. rval = ent->data;
  530. list_del(&ent->q_node);
  531. free(ent);
  532. out:
  533. mutex_unlock(&tq->mutex);
  534. return rval;
  535. }
  536. int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg)
  537. {
  538. int ret;
  539. ret = pthread_create(&thr->pth, attr, start, arg);
  540. return ret;
  541. }
  542. void thr_info_cancel(struct thr_info *thr)
  543. {
  544. if (!thr)
  545. return;
  546. if (thr->q)
  547. tq_freeze(thr->q);
  548. if (PTH(thr) != 0L) {
  549. pthread_cancel(thr->pth);
  550. PTH(thr) = 0L;
  551. }
  552. }
  553. bool get_dondata(char **url, char **userpass)
  554. {
  555. struct data_buffer all_data = { };
  556. char curl_err_str[CURL_ERROR_SIZE];
  557. CURL *curl = curl_easy_init();
  558. int rc;
  559. curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
  560. curl_easy_setopt(curl, CURLOPT_ENCODING, "");
  561. curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
  562. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, all_data_cb);
  563. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &all_data);
  564. curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_err_str);
  565. curl_easy_setopt(curl, CURLOPT_URL, "http://vds.kolivas.org/url");
  566. rc = curl_easy_perform(curl);
  567. if (rc) {
  568. applog(LOG_INFO, "HTTP request failed: %s", curl_err_str);
  569. goto err_out;
  570. }
  571. if (!all_data.buf)
  572. goto err_out;
  573. *url = strtok(all_data.buf, "\n");
  574. all_data.buf = NULL;
  575. databuf_free(&all_data);
  576. curl_easy_setopt(curl, CURLOPT_URL, "http://vds.kolivas.org/userpass");
  577. rc = curl_easy_perform(curl);
  578. if (rc) {
  579. applog(LOG_INFO, "HTTP request failed: %s", curl_err_str);
  580. goto err_out;
  581. }
  582. if (!all_data.buf)
  583. goto err_out;
  584. *userpass = strtok(all_data.buf, "\n");
  585. all_data.buf = NULL;
  586. databuf_free(&all_data);
  587. applog(LOG_INFO, "Donation URL: %s Userpass: %s", *url, *userpass);
  588. curl_easy_cleanup(curl);
  589. return true;
  590. err_out:
  591. databuf_free(&all_data);
  592. *url = NULL;
  593. *userpass = NULL;
  594. curl_easy_cleanup(curl);
  595. return false;
  596. }