plugin_transport_http_client.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2002-2014 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file transport/plugin_transport_http_client.c
  19. * @brief HTTP/S client transport plugin
  20. * @author Matthias Wachs
  21. * @author Christian Grothoff
  22. */
  23. #if BUILD_HTTPS
  24. #define PLUGIN_NAME "https_client"
  25. #define HTTP_STAT_STR_CONNECTIONS "# HTTPS client connections"
  26. #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_client_init
  27. #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_client_done
  28. #else
  29. #define PLUGIN_NAME "http_client"
  30. #define HTTP_STAT_STR_CONNECTIONS "# HTTP client connections"
  31. #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_client_init
  32. #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_client_done
  33. #endif
  34. #define VERBOSE_CURL GNUNET_NO
  35. #define PUT_DISCONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
  36. #define ENABLE_PUT GNUNET_YES
  37. #define ENABLE_GET GNUNET_YES
  38. #include "platform.h"
  39. #include "gnunet_util_lib.h"
  40. #include "gnunet_protocols.h"
  41. #include "gnunet_transport_plugin.h"
  42. #include "plugin_transport_http_common.h"
  43. #include <curl/curl.h>
  44. #define LOG(kind,...) GNUNET_log_from(kind, PLUGIN_NAME, __VA_ARGS__)
  45. /**
  46. * Encapsulation of all of the state of the plugin.
  47. */
  48. struct HTTP_Client_Plugin;
  49. /**
  50. * State of a HTTP PUT request
  51. */
  52. enum HTTP_PUT_REQUEST_STATE
  53. {
  54. /**
  55. * Just created, not yet connected
  56. */
  57. H_NOT_CONNECTED,
  58. /**
  59. * Connected
  60. */
  61. H_CONNECTED,
  62. /**
  63. * Paused, nothing to send
  64. */
  65. H_PAUSED,
  66. /**
  67. * Temporary disconnect in progress due to inactivity
  68. */
  69. H_TMP_DISCONNECTING,
  70. /**
  71. * Send request while temporary disconnect, reconnect
  72. */
  73. H_TMP_RECONNECT_REQUIRED,
  74. /**
  75. * Temporarily disconnected
  76. */
  77. H_TMP_DISCONNECTED,
  78. /**
  79. * Disconnected
  80. */
  81. H_DISCONNECTED
  82. };
  83. /**
  84. * Message to send using http
  85. */
  86. struct HTTP_Message
  87. {
  88. /**
  89. * next pointer for double linked list
  90. */
  91. struct HTTP_Message *next;
  92. /**
  93. * previous pointer for double linked list
  94. */
  95. struct HTTP_Message *prev;
  96. /**
  97. * buffer containing data to send
  98. */
  99. char *buf;
  100. /**
  101. * Continuation function to call once the transmission buffer
  102. * has again space available. NULL if there is no
  103. * continuation to call.
  104. */
  105. GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
  106. /**
  107. * Closure for @e transmit_cont.
  108. */
  109. void *transmit_cont_cls;
  110. /**
  111. * amount of data already sent
  112. */
  113. size_t pos;
  114. /**
  115. * buffer length
  116. */
  117. size_t size;
  118. };
  119. /**
  120. * Session handle for HTTP(S) connections.
  121. */
  122. struct Session;
  123. /**
  124. * A request handle
  125. *
  126. */
  127. struct RequestHandle
  128. {
  129. /**
  130. * Current state of this request
  131. */
  132. enum HTTP_PUT_REQUEST_STATE state;
  133. /**
  134. * The curl easy handle
  135. */
  136. CURL *easyhandle;
  137. /**
  138. * The related session
  139. */
  140. struct Session *s;
  141. };
  142. /**
  143. * Session handle for connections.
  144. */
  145. struct Session
  146. {
  147. /**
  148. * The URL to connect to
  149. */
  150. char *url;
  151. /**
  152. * Address
  153. */
  154. struct GNUNET_HELLO_Address *address;
  155. /**
  156. * Pointer to the global plugin struct.
  157. */
  158. struct HTTP_Client_Plugin *plugin;
  159. /**
  160. * Handle for the HTTP PUT request.
  161. */
  162. struct RequestHandle put;
  163. /**
  164. * Handle for the HTTP GET request.
  165. */
  166. struct RequestHandle get;
  167. /**
  168. * next pointer for double linked list
  169. */
  170. struct HTTP_Message *msg_head;
  171. /**
  172. * previous pointer for double linked list
  173. */
  174. struct HTTP_Message *msg_tail;
  175. /**
  176. * Message stream tokenizer for incoming data
  177. */
  178. struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
  179. /**
  180. * Session timeout task
  181. */
  182. struct GNUNET_SCHEDULER_Task * put_disconnect_task;
  183. /**
  184. * Session timeout task
  185. */
  186. struct GNUNET_SCHEDULER_Task * timeout_task;
  187. /**
  188. * Task to wake up client receive handle when receiving is allowed again
  189. */
  190. struct GNUNET_SCHEDULER_Task * recv_wakeup_task;
  191. /**
  192. * Absolute time when to receive data again.
  193. * Used for receive throttling.
  194. */
  195. struct GNUNET_TIME_Absolute next_receive;
  196. /**
  197. * When does this session time out.
  198. */
  199. struct GNUNET_TIME_Absolute timeout;
  200. /**
  201. * Number of bytes waiting for transmission to this peer.
  202. */
  203. unsigned long long bytes_in_queue;
  204. /**
  205. * Outbound overhead due to HTTP connection
  206. * Add to next message of this session when calling callback
  207. */
  208. size_t overhead;
  209. /**
  210. * Number of messages waiting for transmission to this peer.
  211. */
  212. unsigned int msgs_in_queue;
  213. /**
  214. * ATS network type.
  215. */
  216. enum GNUNET_ATS_Network_Type scope;
  217. };
  218. /**
  219. * Encapsulation of all of the state of the plugin.
  220. */
  221. struct HTTP_Client_Plugin
  222. {
  223. /**
  224. * Our environment.
  225. */
  226. struct GNUNET_TRANSPORT_PluginEnvironment *env;
  227. /**
  228. * Open sessions.
  229. */
  230. struct GNUNET_CONTAINER_MultiPeerMap *sessions;
  231. /**
  232. * Function to call about session status changes.
  233. */
  234. GNUNET_TRANSPORT_SessionInfoCallback sic;
  235. /**
  236. * Closure for @e sic.
  237. */
  238. void *sic_cls;
  239. /**
  240. * Plugin name
  241. */
  242. char *name;
  243. /**
  244. * Protocol
  245. */
  246. char *protocol;
  247. /**
  248. * Proxy configuration: hostname or ip of the proxy server
  249. */
  250. char *proxy_hostname;
  251. /**
  252. * Username for the proxy server
  253. */
  254. char *proxy_username;
  255. /**
  256. * Password for the proxy server
  257. */
  258. char *proxy_password;
  259. /**
  260. * cURL Multihandle
  261. */
  262. CURLM *curl_multi_handle;
  263. /**
  264. * curl perform task
  265. */
  266. struct GNUNET_SCHEDULER_Task * client_perform_task;
  267. /**
  268. * Type of proxy server:
  269. *
  270. * Valid values as supported by curl:
  271. * CURLPROXY_HTTP, CURLPROXY_HTTP_1_0 CURLPROXY_SOCKS4, CURLPROXY_SOCKS5,
  272. * CURLPROXY_SOCKS4A, CURLPROXY_SOCKS5_HOSTNAME
  273. */
  274. curl_proxytype proxytype;
  275. /**
  276. * Use proxy tunneling:
  277. * Tunnel all operations through a given HTTP instead of have the proxy
  278. * evaluate the HTTP request
  279. *
  280. * Default: #GNUNET_NO, #GNUNET_YES experimental
  281. */
  282. int proxy_use_httpproxytunnel;
  283. /**
  284. * My options to be included in the address
  285. */
  286. uint32_t options;
  287. /**
  288. * Maximum number of sockets the plugin can use
  289. * Each http connections are two requests
  290. */
  291. unsigned int max_requests;
  292. /**
  293. * Current number of sockets the plugin can use
  294. * Each http connections are two requests
  295. */
  296. unsigned int cur_requests;
  297. /**
  298. * Last used unique HTTP connection tag
  299. */
  300. uint32_t last_tag;
  301. /**
  302. * use IPv6
  303. */
  304. uint16_t use_ipv6;
  305. /**
  306. * use IPv4
  307. */
  308. uint16_t use_ipv4;
  309. /**
  310. * Should we emulate an XHR client for testing?
  311. */
  312. int emulate_xhr;
  313. };
  314. /**
  315. * Disconnect a session
  316. *
  317. * @param cls the `struct HTTP_Client_Plugin *`
  318. * @param s session
  319. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  320. */
  321. static int
  322. http_client_plugin_session_disconnect (void *cls, struct Session *s);
  323. /**
  324. * If a session monitor is attached, notify it about the new
  325. * session state.
  326. *
  327. * @param plugin our plugin
  328. * @param session session that changed state
  329. * @param state new state of the session
  330. */
  331. static void
  332. notify_session_monitor (struct HTTP_Client_Plugin *plugin,
  333. struct Session *session,
  334. enum GNUNET_TRANSPORT_SessionState state)
  335. {
  336. struct GNUNET_TRANSPORT_SessionInfo info;
  337. if (NULL == plugin->sic)
  338. return;
  339. memset (&info, 0, sizeof (info));
  340. info.state = state;
  341. info.is_inbound = GNUNET_NO;
  342. info.num_msg_pending = session->msgs_in_queue;
  343. info.num_bytes_pending = session->bytes_in_queue;
  344. info.receive_delay = session->next_receive;
  345. info.session_timeout = session->timeout;
  346. info.address = session->address;
  347. plugin->sic (plugin->sic_cls,
  348. session,
  349. &info);
  350. }
  351. /**
  352. * Delete session @a s.
  353. *
  354. * @param s the session to delete
  355. */
  356. static void
  357. client_delete_session (struct Session *s)
  358. {
  359. struct HTTP_Client_Plugin *plugin = s->plugin;
  360. struct HTTP_Message *pos;
  361. struct HTTP_Message *next;
  362. CURLMcode mret;
  363. if (NULL != s->timeout_task)
  364. {
  365. GNUNET_SCHEDULER_cancel (s->timeout_task);
  366. s->timeout_task = NULL;
  367. s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
  368. }
  369. if (NULL != s->put_disconnect_task)
  370. {
  371. GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
  372. s->put_disconnect_task = NULL;
  373. }
  374. if (NULL != s->recv_wakeup_task)
  375. {
  376. GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
  377. s->recv_wakeup_task = NULL;
  378. }
  379. GNUNET_assert (GNUNET_OK ==
  380. GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
  381. &s->address->peer,
  382. s));
  383. if (NULL != s->put.easyhandle)
  384. {
  385. LOG (GNUNET_ERROR_TYPE_DEBUG,
  386. "Session %p/request %p: disconnecting PUT request to peer `%s'\n",
  387. s,
  388. s->put.easyhandle,
  389. GNUNET_i2s (&s->address->peer));
  390. /* remove curl handle from multi handle */
  391. mret = curl_multi_remove_handle (plugin->curl_multi_handle,
  392. s->put.easyhandle);
  393. GNUNET_break (CURLM_OK == mret);
  394. curl_easy_cleanup (s->put.easyhandle);
  395. GNUNET_assert (plugin->cur_requests > 0);
  396. plugin->cur_requests--;
  397. s->put.easyhandle = NULL;
  398. }
  399. if (NULL != s->get.easyhandle)
  400. {
  401. LOG (GNUNET_ERROR_TYPE_DEBUG,
  402. "Session %p/request %p: disconnecting GET request to peer `%s'\n",
  403. s, s->get.easyhandle,
  404. GNUNET_i2s (&s->address->peer));
  405. /* remove curl handle from multi handle */
  406. mret = curl_multi_remove_handle (plugin->curl_multi_handle,
  407. s->get.easyhandle);
  408. GNUNET_break (CURLM_OK == mret);
  409. curl_easy_cleanup (s->get.easyhandle);
  410. GNUNET_assert (plugin->cur_requests > 0);
  411. plugin->cur_requests--;
  412. s->get.easyhandle = NULL;
  413. }
  414. GNUNET_STATISTICS_set (plugin->env->stats,
  415. HTTP_STAT_STR_CONNECTIONS,
  416. plugin->cur_requests,
  417. GNUNET_NO);
  418. next = s->msg_head;
  419. while (NULL != (pos = next))
  420. {
  421. next = pos->next;
  422. GNUNET_CONTAINER_DLL_remove (s->msg_head,
  423. s->msg_tail,
  424. pos);
  425. GNUNET_assert (0 < s->msgs_in_queue);
  426. s->msgs_in_queue--;
  427. GNUNET_assert (pos->size <= s->bytes_in_queue);
  428. s->bytes_in_queue -= pos->size;
  429. if (NULL != pos->transmit_cont)
  430. pos->transmit_cont (pos->transmit_cont_cls,
  431. &s->address->peer,
  432. GNUNET_SYSERR,
  433. pos->size,
  434. pos->pos + s->overhead);
  435. s->overhead = 0;
  436. GNUNET_free (pos);
  437. }
  438. GNUNET_assert (0 == s->msgs_in_queue);
  439. GNUNET_assert (0 == s->bytes_in_queue);
  440. notify_session_monitor (plugin,
  441. s,
  442. GNUNET_TRANSPORT_SS_DONE);
  443. if (NULL != s->msg_tk)
  444. {
  445. GNUNET_SERVER_mst_destroy (s->msg_tk);
  446. s->msg_tk = NULL;
  447. }
  448. GNUNET_HELLO_address_free (s->address);
  449. GNUNET_free (s->url);
  450. GNUNET_free (s);
  451. }
  452. /**
  453. * Increment session timeout due to activity for session @a s.
  454. *
  455. * @param s the session
  456. */
  457. static void
  458. client_reschedule_session_timeout (struct Session *s)
  459. {
  460. GNUNET_assert (NULL != s->timeout_task);
  461. s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
  462. }
  463. /**
  464. * Task performing curl operations
  465. *
  466. * @param cls plugin as closure
  467. * @param tc gnunet scheduler task context
  468. */
  469. static void
  470. client_run (void *cls,
  471. const struct GNUNET_SCHEDULER_TaskContext *tc);
  472. /**
  473. * Function setting up file descriptors and scheduling task to run
  474. *
  475. * @param plugin the plugin as closure
  476. * @param now schedule task in 1ms, regardless of what curl may say
  477. * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
  478. */
  479. static int
  480. client_schedule (struct HTTP_Client_Plugin *plugin,
  481. int now)
  482. {
  483. fd_set rs;
  484. fd_set ws;
  485. fd_set es;
  486. int max;
  487. struct GNUNET_NETWORK_FDSet *grs;
  488. struct GNUNET_NETWORK_FDSet *gws;
  489. long to;
  490. CURLMcode mret;
  491. struct GNUNET_TIME_Relative timeout;
  492. /* Cancel previous scheduled task */
  493. if (plugin->client_perform_task != NULL)
  494. {
  495. GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
  496. plugin->client_perform_task = NULL;
  497. }
  498. max = -1;
  499. FD_ZERO (&rs);
  500. FD_ZERO (&ws);
  501. FD_ZERO (&es);
  502. mret = curl_multi_fdset (plugin->curl_multi_handle, &rs, &ws, &es, &max);
  503. if (mret != CURLM_OK)
  504. {
  505. LOG (GNUNET_ERROR_TYPE_ERROR,
  506. _("%s failed at %s:%d: `%s'\n"),
  507. "curl_multi_fdset",
  508. __FILE__,
  509. __LINE__,
  510. curl_multi_strerror (mret));
  511. return GNUNET_SYSERR;
  512. }
  513. mret = curl_multi_timeout (plugin->curl_multi_handle, &to);
  514. if (-1 == to)
  515. timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
  516. else
  517. timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, to);
  518. if (now == GNUNET_YES)
  519. timeout = GNUNET_TIME_UNIT_MILLISECONDS;
  520. if (CURLM_OK != mret)
  521. {
  522. LOG (GNUNET_ERROR_TYPE_ERROR,
  523. _("%s failed at %s:%d: `%s'\n"),
  524. "curl_multi_timeout", __FILE__, __LINE__,
  525. curl_multi_strerror (mret));
  526. return GNUNET_SYSERR;
  527. }
  528. grs = GNUNET_NETWORK_fdset_create ();
  529. gws = GNUNET_NETWORK_fdset_create ();
  530. GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
  531. GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
  532. /* Schedule task to run when select is ready to read or write */
  533. plugin->client_perform_task =
  534. GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
  535. timeout, grs, gws,
  536. &client_run, plugin);
  537. GNUNET_NETWORK_fdset_destroy (gws);
  538. GNUNET_NETWORK_fdset_destroy (grs);
  539. return GNUNET_OK;
  540. }
  541. #if VERBOSE_CURL
  542. /**
  543. * Loggging function
  544. *
  545. * @param curl the curl easy handle
  546. * @param type message type
  547. * @param data data to log, NOT a 0-terminated string
  548. * @param size data length
  549. * @param cls the closure
  550. * @return always 0
  551. */
  552. static int
  553. client_log (CURL *curl,
  554. curl_infotype type,
  555. const char *data,
  556. size_t size,
  557. void *cls)
  558. {
  559. struct RequestHandle *ch = cls;
  560. const char *ttype = "UNSPECIFIED";
  561. char text[size + 2];
  562. if (! ((CURLINFO_TEXT == type) ||
  563. (CURLINFO_HEADER_IN == type) ||
  564. (CURLINFO_HEADER_OUT == type)))
  565. return 0;
  566. switch (type)
  567. {
  568. case CURLINFO_TEXT:
  569. ttype = "TEXT";
  570. break;
  571. case CURLINFO_HEADER_IN:
  572. ttype = "HEADER_IN";
  573. break;
  574. case CURLINFO_HEADER_OUT:
  575. ttype = "HEADER_OUT";
  576. /* Overhead*/
  577. GNUNET_assert (NULL != ch);
  578. GNUNET_assert (NULL != ch->easyhandle);
  579. GNUNET_assert (NULL != ch->s);
  580. ch->s->overhead += size;
  581. break;
  582. default:
  583. ttype = "UNSPECIFIED";
  584. break;
  585. }
  586. memcpy (text, data, size);
  587. if (text[size - 1] == '\n')
  588. {
  589. text[size] = '\0';
  590. }
  591. else
  592. {
  593. text[size] = '\n';
  594. text[size + 1] = '\0';
  595. }
  596. LOG (GNUNET_ERROR_TYPE_DEBUG,
  597. "Request %p %s: %s",
  598. ch->easyhandle,
  599. ttype,
  600. text);
  601. return 0;
  602. }
  603. #endif
  604. /**
  605. * Connect GET request
  606. *
  607. * @param s the session to connect
  608. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  609. */
  610. static int
  611. client_connect_get (struct Session *s);
  612. /**
  613. * Connect a HTTP put request
  614. *
  615. * @param s the session to connect
  616. * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for success
  617. */
  618. static int
  619. client_connect_put (struct Session *s);
  620. /**
  621. * Function that can be used by the transport service to transmit
  622. * a message using the plugin. Note that in the case of a
  623. * peer disconnecting, the continuation MUST be called
  624. * prior to the disconnect notification itself. This function
  625. * will be called with this peer's HELLO message to initiate
  626. * a fresh connection to another peer.
  627. *
  628. * @param cls closure
  629. * @param s which session must be used
  630. * @param msgbuf the message to transmit
  631. * @param msgbuf_size number of bytes in @a msgbuf
  632. * @param priority how important is the message (most plugins will
  633. * ignore message priority and just FIFO)
  634. * @param to how long to wait at most for the transmission (does not
  635. * require plugins to discard the message after the timeout,
  636. * just advisory for the desired delay; most plugins will ignore
  637. * this as well)
  638. * @param cont continuation to call once the message has
  639. * been transmitted (or if the transport is ready
  640. * for the next transmission call; or if the
  641. * peer disconnected...); can be NULL
  642. * @param cont_cls closure for @a cont
  643. * @return number of bytes used (on the physical network, with overheads);
  644. * -1 on hard errors (i.e. address invalid); 0 is a legal value
  645. * and does NOT mean that the message was not transmitted (DV)
  646. */
  647. static ssize_t
  648. http_client_plugin_send (void *cls,
  649. struct Session *s,
  650. const char *msgbuf,
  651. size_t msgbuf_size,
  652. unsigned int priority,
  653. struct GNUNET_TIME_Relative to,
  654. GNUNET_TRANSPORT_TransmitContinuation cont,
  655. void *cont_cls)
  656. {
  657. struct HTTP_Client_Plugin *plugin = cls;
  658. struct HTTP_Message *msg;
  659. char *stat_txt;
  660. LOG (GNUNET_ERROR_TYPE_DEBUG,
  661. "Session %p/request %p: Sending message with %u to peer `%s' \n",
  662. s,
  663. s->put.easyhandle,
  664. msgbuf_size,
  665. GNUNET_i2s (&s->address->peer));
  666. /* create new message and schedule */
  667. msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
  668. msg->size = msgbuf_size;
  669. msg->buf = (char *) &msg[1];
  670. msg->transmit_cont = cont;
  671. msg->transmit_cont_cls = cont_cls;
  672. memcpy (msg->buf,
  673. msgbuf,
  674. msgbuf_size);
  675. GNUNET_CONTAINER_DLL_insert_tail (s->msg_head,
  676. s->msg_tail,
  677. msg);
  678. s->msgs_in_queue++;
  679. s->bytes_in_queue += msg->size;
  680. GNUNET_asprintf (&stat_txt,
  681. "# bytes currently in %s_client buffers",
  682. plugin->protocol);
  683. GNUNET_STATISTICS_update (plugin->env->stats,
  684. stat_txt, msgbuf_size, GNUNET_NO);
  685. GNUNET_free (stat_txt);
  686. notify_session_monitor (plugin,
  687. s,
  688. GNUNET_TRANSPORT_SS_UPDATE);
  689. if (H_TMP_DISCONNECTING == s->put.state)
  690. {
  691. /* PUT request is currently getting disconnected */
  692. s->put.state = H_TMP_RECONNECT_REQUIRED;
  693. LOG (GNUNET_ERROR_TYPE_DEBUG,
  694. "Session %p/request %p: currently disconnecting, reconnecting immediately\n",
  695. s,
  696. s->put.easyhandle);
  697. return msgbuf_size;
  698. }
  699. if (H_PAUSED == s->put.state)
  700. {
  701. /* PUT request was paused, unpause */
  702. GNUNET_assert (s->put_disconnect_task != NULL);
  703. GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
  704. s->put_disconnect_task = NULL;
  705. LOG (GNUNET_ERROR_TYPE_DEBUG,
  706. "Session %p/request %p: unpausing request\n",
  707. s, s->put.easyhandle);
  708. s->put.state = H_CONNECTED;
  709. if (NULL != s->put.easyhandle)
  710. curl_easy_pause (s->put.easyhandle, CURLPAUSE_CONT);
  711. }
  712. else if (H_TMP_DISCONNECTED == s->put.state)
  713. {
  714. /* PUT request was disconnected, reconnect */
  715. LOG (GNUNET_ERROR_TYPE_DEBUG, "Session %p: Reconnecting PUT request\n", s);
  716. GNUNET_break (NULL == s->put.easyhandle);
  717. if (GNUNET_SYSERR == client_connect_put (s))
  718. {
  719. /* Could not reconnect */
  720. http_client_plugin_session_disconnect (plugin, s);
  721. return GNUNET_SYSERR;
  722. }
  723. }
  724. client_schedule (s->plugin, GNUNET_YES);
  725. return msgbuf_size;
  726. }
  727. /**
  728. * Disconnect a session
  729. *
  730. * @param cls the `struct HTTP_Client_Plugin *`
  731. * @param s session
  732. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  733. */
  734. static int
  735. http_client_plugin_session_disconnect (void *cls,
  736. struct Session *s)
  737. {
  738. struct HTTP_Client_Plugin *plugin = cls;
  739. LOG (GNUNET_ERROR_TYPE_DEBUG,
  740. "Session %p: notifying transport about ending session\n",
  741. s);
  742. plugin->env->session_end (plugin->env->cls,
  743. s->address,
  744. s);
  745. client_delete_session (s);
  746. /* Re-schedule since handles have changed */
  747. if (plugin->client_perform_task != NULL)
  748. {
  749. GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
  750. plugin->client_perform_task = NULL;
  751. }
  752. client_schedule (plugin, GNUNET_YES);
  753. return GNUNET_OK;
  754. }
  755. /**
  756. * Function that is called to get the keepalive factor.
  757. * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
  758. * calculate the interval between keepalive packets.
  759. *
  760. * @param cls closure with the `struct Plugin`
  761. * @return keepalive factor
  762. */
  763. static unsigned int
  764. http_client_query_keepalive_factor (void *cls)
  765. {
  766. return 3;
  767. }
  768. /**
  769. * Callback to destroys all sessions on exit.
  770. *
  771. * @param cls the `struct HTTP_Client_Plugin *`
  772. * @param peer identity of the peer
  773. * @param value the `struct Session *`
  774. * @return #GNUNET_OK (continue iterating)
  775. */
  776. static int
  777. destroy_session_cb (void *cls,
  778. const struct GNUNET_PeerIdentity *peer,
  779. void *value)
  780. {
  781. struct HTTP_Client_Plugin *plugin = cls;
  782. struct Session *session = value;
  783. http_client_plugin_session_disconnect (plugin, session);
  784. return GNUNET_OK;
  785. }
  786. /**
  787. * Function that can be used to force the plugin to disconnect
  788. * from the given peer and cancel all previous transmissions
  789. * (and their continuationc).
  790. *
  791. * @param cls closure
  792. * @param target peer from which to disconnect
  793. */
  794. static void
  795. http_client_plugin_peer_disconnect (void *cls,
  796. const struct GNUNET_PeerIdentity *target)
  797. {
  798. struct HTTP_Client_Plugin *plugin = cls;
  799. LOG (GNUNET_ERROR_TYPE_DEBUG,
  800. "Transport tells me to disconnect `%s'\n",
  801. GNUNET_i2s (target));
  802. GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
  803. target,
  804. &destroy_session_cb,
  805. plugin);
  806. }
  807. /**
  808. * Closure for #session_lookup_client_by_address().
  809. */
  810. struct SessionClientCtx
  811. {
  812. /**
  813. * Address we are looking for.
  814. */
  815. const struct GNUNET_HELLO_Address *address;
  816. /**
  817. * Session that was found.
  818. */
  819. struct Session *ret;
  820. };
  821. /**
  822. * Locate the seession object for a given address.
  823. *
  824. * @param cls the `struct SessionClientCtx *`
  825. * @param key peer identity
  826. * @param value the `struct Session` to check
  827. * @return #GNUNET_NO if found, #GNUNET_OK if not
  828. */
  829. static int
  830. session_lookup_client_by_address (void *cls,
  831. const struct GNUNET_PeerIdentity *key,
  832. void *value)
  833. {
  834. struct SessionClientCtx *sc_ctx = cls;
  835. struct Session *s = value;
  836. if (0 == GNUNET_HELLO_address_cmp (sc_ctx->address,
  837. s->address))
  838. {
  839. sc_ctx->ret = s;
  840. return GNUNET_NO;
  841. }
  842. return GNUNET_YES;
  843. }
  844. /**
  845. * Check if a sessions exists for an specific address
  846. *
  847. * @param plugin the plugin
  848. * @param address the address
  849. * @return the session or NULL
  850. */
  851. static struct Session *
  852. client_lookup_session (struct HTTP_Client_Plugin *plugin,
  853. const struct GNUNET_HELLO_Address *address)
  854. {
  855. struct SessionClientCtx sc_ctx;
  856. sc_ctx.address = address;
  857. sc_ctx.ret = NULL;
  858. GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
  859. &session_lookup_client_by_address,
  860. &sc_ctx);
  861. return sc_ctx.ret;
  862. }
  863. /**
  864. * When we have nothing to transmit, we pause the HTTP PUT
  865. * after a while (so that gnurl stops asking). This task
  866. * is the delayed task that actually disconnects the PUT.
  867. *
  868. * @param cls the `struct Session *` with the put
  869. * @param tc scheduler context
  870. */
  871. static void
  872. client_put_disconnect (void *cls,
  873. const struct GNUNET_SCHEDULER_TaskContext *tc)
  874. {
  875. struct Session *s = cls;
  876. s->put_disconnect_task = NULL;
  877. LOG (GNUNET_ERROR_TYPE_DEBUG,
  878. "Session %p/request %p: will be disconnected due to no activity\n",
  879. s, s->put.easyhandle);
  880. s->put.state = H_TMP_DISCONNECTING;
  881. if (NULL != s->put.easyhandle)
  882. curl_easy_pause (s->put.easyhandle,
  883. CURLPAUSE_CONT);
  884. client_schedule (s->plugin, GNUNET_YES);
  885. }
  886. /**
  887. * Callback method used with libcurl
  888. * Method is called when libcurl needs to read data during sending
  889. *
  890. * @param stream pointer where to write data
  891. * @param size size of an individual element
  892. * @param nmemb count of elements that can be written to the buffer
  893. * @param cls our `struct Session`
  894. * @return bytes written to stream, returning 0 will terminate request!
  895. */
  896. static size_t
  897. client_send_cb (void *stream,
  898. size_t size,
  899. size_t nmemb,
  900. void *cls)
  901. {
  902. struct Session *s = cls;
  903. struct HTTP_Client_Plugin *plugin = s->plugin;
  904. struct HTTP_Message *msg = s->msg_head;
  905. size_t len;
  906. char *stat_txt;
  907. if (H_TMP_DISCONNECTING == s->put.state)
  908. {
  909. LOG (GNUNET_ERROR_TYPE_DEBUG,
  910. "Session %p/request %p: disconnect due to inactivity\n",
  911. s, s->put.easyhandle);
  912. return 0;
  913. }
  914. if (NULL == msg)
  915. {
  916. if (GNUNET_YES == plugin->emulate_xhr)
  917. {
  918. LOG (GNUNET_ERROR_TYPE_DEBUG,
  919. "Session %p/request %p: PUT request finished\n",
  920. s,
  921. s->put.easyhandle);
  922. s->put.state = H_TMP_DISCONNECTING;
  923. return 0;
  924. }
  925. /* We have nothing to send, so pause PUT request */
  926. LOG (GNUNET_ERROR_TYPE_DEBUG,
  927. "Session %p/request %p: nothing to send, suspending\n",
  928. s,
  929. s->put.easyhandle);
  930. s->put_disconnect_task = GNUNET_SCHEDULER_add_delayed (PUT_DISCONNECT_TIMEOUT,
  931. &client_put_disconnect, s);
  932. s->put.state = H_PAUSED;
  933. return CURL_READFUNC_PAUSE;
  934. }
  935. /* data to send */
  936. GNUNET_assert (msg->pos < msg->size);
  937. /* calculate how much fits in buffer */
  938. len = GNUNET_MIN (msg->size - msg->pos,
  939. size * nmemb);
  940. memcpy (stream, &msg->buf[msg->pos], len);
  941. msg->pos += len;
  942. if (msg->pos == msg->size)
  943. {
  944. LOG (GNUNET_ERROR_TYPE_DEBUG,
  945. "Session %p/request %p: sent message with %u bytes sent, removing message from queue\n",
  946. s,
  947. s->put.easyhandle,
  948. msg->size,
  949. msg->pos);
  950. /* Calling transmit continuation */
  951. GNUNET_CONTAINER_DLL_remove (s->msg_head,
  952. s->msg_tail,
  953. msg);
  954. GNUNET_assert (0 < s->msgs_in_queue);
  955. s->msgs_in_queue--;
  956. GNUNET_assert (msg->size <= s->bytes_in_queue);
  957. s->bytes_in_queue -= msg->size;
  958. if (NULL != msg->transmit_cont)
  959. msg->transmit_cont (msg->transmit_cont_cls,
  960. &s->address->peer,
  961. GNUNET_OK,
  962. msg->size,
  963. msg->size + s->overhead);
  964. s->overhead = 0;
  965. GNUNET_free (msg);
  966. }
  967. notify_session_monitor (plugin,
  968. s,
  969. GNUNET_TRANSPORT_SS_UPDATE);
  970. GNUNET_asprintf (&stat_txt,
  971. "# bytes currently in %s_client buffers",
  972. plugin->protocol);
  973. GNUNET_STATISTICS_update (plugin->env->stats,
  974. stat_txt,
  975. - len,
  976. GNUNET_NO);
  977. GNUNET_free (stat_txt);
  978. GNUNET_asprintf (&stat_txt,
  979. "# bytes transmitted via %s_client",
  980. plugin->protocol);
  981. GNUNET_STATISTICS_update (plugin->env->stats,
  982. stat_txt,
  983. len,
  984. GNUNET_NO);
  985. GNUNET_free (stat_txt);
  986. return len;
  987. }
  988. /**
  989. * Wake up a curl handle which was suspended
  990. *
  991. * @param cls the session
  992. * @param tc task context
  993. */
  994. static void
  995. client_wake_up (void *cls,
  996. const struct GNUNET_SCHEDULER_TaskContext *tc)
  997. {
  998. struct Session *s = cls;
  999. s->recv_wakeup_task = NULL;
  1000. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
  1001. return;
  1002. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1003. "Session %p/request %p: Waking up GET handle\n",
  1004. s, s->get.easyhandle);
  1005. if (H_PAUSED == s->put.state)
  1006. {
  1007. /* PUT request was paused, unpause */
  1008. GNUNET_assert (s->put_disconnect_task != NULL);
  1009. GNUNET_SCHEDULER_cancel (s->put_disconnect_task);
  1010. s->put_disconnect_task = NULL;
  1011. s->put.state = H_CONNECTED;
  1012. if (NULL != s->put.easyhandle)
  1013. curl_easy_pause (s->put.easyhandle, CURLPAUSE_CONT);
  1014. }
  1015. if (NULL != s->get.easyhandle)
  1016. curl_easy_pause (s->get.easyhandle, CURLPAUSE_CONT);
  1017. }
  1018. /**
  1019. * Callback for message stream tokenizer
  1020. *
  1021. * @param cls the session
  1022. * @param client not used
  1023. * @param message the message received
  1024. * @return always #GNUNET_OK
  1025. */
  1026. static int
  1027. client_receive_mst_cb (void *cls,
  1028. void *client,
  1029. const struct GNUNET_MessageHeader *message)
  1030. {
  1031. struct Session *s = cls;
  1032. struct HTTP_Client_Plugin *plugin;
  1033. struct GNUNET_TIME_Relative delay;
  1034. char *stat_txt;
  1035. plugin = s->plugin;
  1036. delay = s->plugin->env->receive (plugin->env->cls,
  1037. s->address,
  1038. s,
  1039. message);
  1040. GNUNET_asprintf (&stat_txt,
  1041. "# bytes received via %s_client",
  1042. plugin->protocol);
  1043. GNUNET_STATISTICS_update (plugin->env->stats,
  1044. stat_txt,
  1045. ntohs (message->size),
  1046. GNUNET_NO);
  1047. GNUNET_free (stat_txt);
  1048. s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
  1049. if (GNUNET_TIME_absolute_get ().abs_value_us < s->next_receive.abs_value_us)
  1050. {
  1051. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1052. "Client: peer `%s' address `%s' next read delayed for %s\n",
  1053. GNUNET_i2s (&s->address->peer),
  1054. http_common_plugin_address_to_string (s->plugin->protocol,
  1055. s->address->address,
  1056. s->address->address_length),
  1057. GNUNET_STRINGS_relative_time_to_string (delay,
  1058. GNUNET_YES));
  1059. }
  1060. client_reschedule_session_timeout (s);
  1061. return GNUNET_OK;
  1062. }
  1063. /**
  1064. * Callback method used with libcurl when data for a PUT request are
  1065. * received. We do not expect data here, so we just discard it.
  1066. *
  1067. * @param stream pointer where to write data
  1068. * @param size size of an individual element
  1069. * @param nmemb count of elements that can be written to the buffer
  1070. * @param cls destination pointer, passed to the libcurl handle
  1071. * @return bytes read from stream
  1072. */
  1073. static size_t
  1074. client_receive_put (void *stream,
  1075. size_t size,
  1076. size_t nmemb,
  1077. void *cls)
  1078. {
  1079. return size * nmemb;
  1080. }
  1081. /**
  1082. * Callback method used with libcurl when data for a GET request are
  1083. * received. Forward to MST
  1084. *
  1085. * @param stream pointer where to write data
  1086. * @param size size of an individual element
  1087. * @param nmemb count of elements that can be written to the buffer
  1088. * @param cls destination pointer, passed to the libcurl handle
  1089. * @return bytes read from stream
  1090. */
  1091. static size_t
  1092. client_receive (void *stream,
  1093. size_t size,
  1094. size_t nmemb,
  1095. void *cls)
  1096. {
  1097. struct Session *s = cls;
  1098. struct GNUNET_TIME_Absolute now;
  1099. size_t len = size * nmemb;
  1100. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1101. "Session %p / request %p: Received %u bytes from peer `%s'\n",
  1102. s,
  1103. s->get.easyhandle,
  1104. len,
  1105. GNUNET_i2s (&s->address->peer));
  1106. now = GNUNET_TIME_absolute_get ();
  1107. if (now.abs_value_us < s->next_receive.abs_value_us)
  1108. {
  1109. struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
  1110. struct GNUNET_TIME_Relative delta
  1111. = GNUNET_TIME_absolute_get_difference (now, s->next_receive);
  1112. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1113. "Session %p / request %p: No inbound bandwidth available! Next read was delayed for %s\n",
  1114. s,
  1115. s->get.easyhandle,
  1116. GNUNET_STRINGS_relative_time_to_string (delta,
  1117. GNUNET_YES));
  1118. if (s->recv_wakeup_task != NULL)
  1119. {
  1120. GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
  1121. s->recv_wakeup_task = NULL;
  1122. }
  1123. s->recv_wakeup_task
  1124. = GNUNET_SCHEDULER_add_delayed (delta,
  1125. &client_wake_up,
  1126. s);
  1127. return CURL_WRITEFUNC_PAUSE;
  1128. }
  1129. if (NULL == s->msg_tk)
  1130. s->msg_tk = GNUNET_SERVER_mst_create (&client_receive_mst_cb,
  1131. s);
  1132. GNUNET_SERVER_mst_receive (s->msg_tk,
  1133. s,
  1134. stream,
  1135. len,
  1136. GNUNET_NO,
  1137. GNUNET_NO);
  1138. return len;
  1139. }
  1140. /**
  1141. * Task performing curl operations
  1142. *
  1143. * @param cls plugin as closure
  1144. * @param tc scheduler task context
  1145. */
  1146. static void
  1147. client_run (void *cls,
  1148. const struct GNUNET_SCHEDULER_TaskContext *tc)
  1149. {
  1150. struct HTTP_Client_Plugin *plugin = cls;
  1151. int running;
  1152. long http_statuscode;
  1153. CURLMcode mret;
  1154. CURLMsg *msg;
  1155. int put_request; /* GNUNET_YES if easy handle is put, GNUNET_NO for get */
  1156. int msgs_left;
  1157. plugin->client_perform_task = NULL;
  1158. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
  1159. return;
  1160. /* While data are available or timeouts occured */
  1161. do
  1162. {
  1163. running = 0;
  1164. /* Perform operations for all handles */
  1165. mret = curl_multi_perform (plugin->curl_multi_handle, &running);
  1166. /* Get additional information for all handles */
  1167. while (NULL != (msg = curl_multi_info_read (plugin->curl_multi_handle, &msgs_left)))
  1168. {
  1169. CURL *easy_h = msg->easy_handle;
  1170. struct Session *s = NULL;
  1171. char *d = NULL; /* curl requires 'd' to be a 'char *' */
  1172. GNUNET_assert (NULL != easy_h);
  1173. /* Obtain session from easy handle */
  1174. GNUNET_assert (CURLE_OK == curl_easy_getinfo (easy_h, CURLINFO_PRIVATE, &d));
  1175. s = (struct Session *) d;
  1176. GNUNET_assert (NULL != s);
  1177. if (msg->msg != CURLMSG_DONE)
  1178. continue; /* This should not happen */
  1179. /* Get HTTP response code */
  1180. GNUNET_break (CURLE_OK == curl_easy_getinfo (easy_h,
  1181. CURLINFO_RESPONSE_CODE, &http_statuscode));
  1182. if (easy_h == s->put.easyhandle)
  1183. put_request = GNUNET_YES;
  1184. else
  1185. put_request = GNUNET_NO;
  1186. /* Log status of terminated request */
  1187. if ((0 != msg->data.result) || (http_statuscode != 200))
  1188. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1189. "Session %p/request %p: %s request to `%s' ended with status %i reason %i: `%s'\n",
  1190. s, msg->easy_handle,
  1191. (GNUNET_YES == put_request) ? "PUT" : "GET",
  1192. GNUNET_i2s (&s->address->peer),
  1193. http_statuscode,
  1194. msg->data.result,
  1195. curl_easy_strerror (msg->data.result));
  1196. else
  1197. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1198. "Session %p/request %p: %s request to `%s' ended normal\n",
  1199. s, msg->easy_handle,
  1200. (GNUNET_YES == put_request) ? "PUT" : "GET",
  1201. GNUNET_i2s (&s->address->peer));
  1202. /* Remove easy handle from multi handle */
  1203. curl_multi_remove_handle (plugin->curl_multi_handle, easy_h);
  1204. /* Clean up easy handle */
  1205. curl_easy_cleanup (easy_h);
  1206. /* Remove information */
  1207. GNUNET_assert (plugin->cur_requests > 0);
  1208. plugin->cur_requests--;
  1209. LOG (GNUNET_ERROR_TYPE_INFO,
  1210. "%s request to %s done, number of requests decreased to %u\n",
  1211. (GNUNET_YES == put_request) ? "PUT" : "GET",
  1212. s->url,
  1213. plugin->cur_requests);
  1214. if (GNUNET_YES == put_request)
  1215. {
  1216. /* Clean up a PUT request */
  1217. s->put.easyhandle = NULL;
  1218. s->put.s = NULL;
  1219. switch (s->put.state) {
  1220. case H_NOT_CONNECTED:
  1221. case H_DISCONNECTED:
  1222. case H_TMP_DISCONNECTED:
  1223. /* This must not happen */
  1224. GNUNET_break (0);
  1225. break;
  1226. case H_TMP_RECONNECT_REQUIRED:
  1227. /* Transport called send while disconnect in progess, reconnect */
  1228. if (GNUNET_SYSERR == client_connect_put (s))
  1229. {
  1230. /* Reconnect failed, disconnect session */
  1231. http_client_plugin_session_disconnect (plugin, s);
  1232. }
  1233. break;
  1234. case H_TMP_DISCONNECTING:
  1235. /* PUT gets temporarily disconnected */
  1236. s->put.state = H_TMP_DISCONNECTED;
  1237. break;
  1238. case H_PAUSED:
  1239. case H_CONNECTED:
  1240. /* PUT gets permanently disconnected */
  1241. s->put.state = H_DISCONNECTED;
  1242. http_client_plugin_session_disconnect (plugin, s);
  1243. break;
  1244. default:
  1245. GNUNET_break (0);
  1246. break;
  1247. }
  1248. }
  1249. else if (GNUNET_NO == put_request)
  1250. {
  1251. /* Clean up a GET request */
  1252. s->get.easyhandle = NULL;
  1253. s->get.s = NULL;
  1254. /* If we are emulating an XHR client we need to make another GET
  1255. * request.
  1256. */
  1257. if (GNUNET_YES == plugin->emulate_xhr)
  1258. {
  1259. if (GNUNET_SYSERR == client_connect_get (s))
  1260. http_client_plugin_session_disconnect (plugin, s);
  1261. }
  1262. else
  1263. {
  1264. /* GET request was terminated, so disconnect session */
  1265. http_client_plugin_session_disconnect (plugin, s);
  1266. }
  1267. }
  1268. else
  1269. GNUNET_break (0); /* Must not happen */
  1270. GNUNET_STATISTICS_set (plugin->env->stats,
  1271. HTTP_STAT_STR_CONNECTIONS,
  1272. plugin->cur_requests,
  1273. GNUNET_NO);
  1274. }
  1275. }
  1276. while (mret == CURLM_CALL_MULTI_PERFORM);
  1277. client_schedule (plugin, GNUNET_NO);
  1278. }
  1279. #ifdef TCP_STEALTH
  1280. /**
  1281. * Open TCP socket with TCP STEALTH enabled.
  1282. *
  1283. * @param clientp our `struct Session *`
  1284. * @param purpose why does curl want to open a socket
  1285. * @param address what kind of socket does curl want to have opened?
  1286. * @return opened socket
  1287. */
  1288. static curl_socket_t
  1289. open_tcp_stealth_socket_cb (void *clientp,
  1290. curlsocktype purpose,
  1291. struct curl_sockaddr *address)
  1292. {
  1293. struct Session *s = clientp;
  1294. int ret;
  1295. switch (purpose)
  1296. {
  1297. case CURLSOCKTYPE_IPCXN:
  1298. ret = socket (address->family,
  1299. address->socktype,
  1300. address->protocol);
  1301. if (-1 == ret)
  1302. return CURL_SOCKET_BAD;
  1303. if ( ( (SOCK_STREAM != address->socktype) ||
  1304. ( (0 != address->protocol) &&
  1305. (IPPROTO_TCP != address->protocol))) )
  1306. return (curl_socket_t) ret;
  1307. if ( (0 != setsockopt (ret,
  1308. IPPROTO_TCP,
  1309. TCP_STEALTH,
  1310. &s->address->peer,
  1311. sizeof (struct GNUNET_PeerIdentity))) )
  1312. {
  1313. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1314. _("TCP_STEALTH not supported on this platform.\n"));
  1315. (void) close (ret);
  1316. return CURL_SOCKET_BAD;
  1317. }
  1318. return (curl_socket_t) ret;
  1319. case CURLSOCKTYPE_ACCEPT:
  1320. GNUNET_break (0);
  1321. return CURL_SOCKET_BAD;
  1322. break;
  1323. case CURLSOCKTYPE_LAST:
  1324. GNUNET_break (0);
  1325. return CURL_SOCKET_BAD;
  1326. default:
  1327. GNUNET_break (0);
  1328. return CURL_SOCKET_BAD;
  1329. }
  1330. }
  1331. #endif
  1332. /**
  1333. * Connect GET request for a session
  1334. *
  1335. * @param s the session to connect
  1336. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  1337. */
  1338. static int
  1339. client_connect_get (struct Session *s)
  1340. {
  1341. CURLMcode mret;
  1342. struct HttpAddress *ha;
  1343. uint32_t options;
  1344. ha = (struct HttpAddress *) s->address->address;
  1345. options = ntohl (ha->options);
  1346. /* create get request */
  1347. s->get.easyhandle = curl_easy_init ();
  1348. s->get.s = s;
  1349. if (0 != (options & HTTP_OPTIONS_TCP_STEALTH))
  1350. {
  1351. #ifdef TCP_STEALTH
  1352. curl_easy_setopt (s->get.easyhandle,
  1353. CURLOPT_OPENSOCKETFUNCTION,
  1354. &open_tcp_stealth_socket_cb);
  1355. curl_easy_setopt (s->get.easyhandle,
  1356. CURLOPT_OPENSOCKETDATA,
  1357. s);
  1358. #else
  1359. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1360. "Cannot connect, TCP STEALTH needed and not supported by kernel.\n");
  1361. curl_easy_cleanup (s->get.easyhandle);
  1362. s->get.easyhandle = NULL;
  1363. s->get.s = NULL;
  1364. return GNUNET_SYSERR;
  1365. #endif
  1366. }
  1367. #if VERBOSE_CURL
  1368. curl_easy_setopt (s->get.easyhandle,
  1369. CURLOPT_VERBOSE,
  1370. 1L);
  1371. curl_easy_setopt (s->get.easyhandle,
  1372. CURLOPT_DEBUGFUNCTION,
  1373. &client_log);
  1374. curl_easy_setopt (s->get.easyhandle,
  1375. CURLOPT_DEBUGDATA,
  1376. &s->get);
  1377. #endif
  1378. #if BUILD_HTTPS
  1379. curl_easy_setopt (s->get.easyhandle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
  1380. {
  1381. if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
  1382. (options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
  1383. {
  1384. curl_easy_setopt (s->get.easyhandle,
  1385. CURLOPT_SSL_VERIFYPEER, 1L);
  1386. curl_easy_setopt (s->get.easyhandle,
  1387. CURLOPT_SSL_VERIFYHOST,
  1388. 2L);
  1389. }
  1390. else
  1391. {
  1392. curl_easy_setopt (s->get.easyhandle,
  1393. CURLOPT_SSL_VERIFYPEER,
  1394. 0L);
  1395. curl_easy_setopt (s->get.easyhandle,
  1396. CURLOPT_SSL_VERIFYHOST,
  1397. 0L);
  1398. }
  1399. }
  1400. curl_easy_setopt (s->get.easyhandle,
  1401. CURLOPT_PROTOCOLS,
  1402. CURLPROTO_HTTPS);
  1403. curl_easy_setopt (s->get.easyhandle,
  1404. CURLOPT_REDIR_PROTOCOLS,
  1405. CURLPROTO_HTTPS);
  1406. #else
  1407. curl_easy_setopt (s->get.easyhandle,
  1408. CURLOPT_PROTOCOLS,
  1409. CURLPROTO_HTTP);
  1410. curl_easy_setopt (s->get.easyhandle,
  1411. CURLOPT_REDIR_PROTOCOLS,
  1412. CURLPROTO_HTTP);
  1413. #endif
  1414. if (NULL != s->plugin->proxy_hostname)
  1415. {
  1416. curl_easy_setopt (s->get.easyhandle,
  1417. CURLOPT_PROXY,
  1418. s->plugin->proxy_hostname);
  1419. curl_easy_setopt (s->get.easyhandle,
  1420. CURLOPT_PROXYTYPE,
  1421. s->plugin->proxytype);
  1422. if (NULL != s->plugin->proxy_username)
  1423. curl_easy_setopt (s->get.easyhandle,
  1424. CURLOPT_PROXYUSERNAME,
  1425. s->plugin->proxy_username);
  1426. if (NULL != s->plugin->proxy_password)
  1427. curl_easy_setopt (s->get.easyhandle,
  1428. CURLOPT_PROXYPASSWORD,
  1429. s->plugin->proxy_password);
  1430. if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
  1431. curl_easy_setopt (s->get.easyhandle,
  1432. CURLOPT_HTTPPROXYTUNNEL,
  1433. s->plugin->proxy_use_httpproxytunnel);
  1434. }
  1435. if (GNUNET_YES == s->plugin->emulate_xhr)
  1436. {
  1437. char *url;
  1438. GNUNET_asprintf (&url,
  1439. "%s,1",
  1440. s->url);
  1441. curl_easy_setopt (s->get.easyhandle,
  1442. CURLOPT_URL,
  1443. url);
  1444. GNUNET_free(url);
  1445. }
  1446. else
  1447. {
  1448. curl_easy_setopt (s->get.easyhandle,
  1449. CURLOPT_URL,
  1450. s->url);
  1451. }
  1452. curl_easy_setopt (s->get.easyhandle,
  1453. CURLOPT_READFUNCTION,
  1454. &client_send_cb);
  1455. curl_easy_setopt (s->get.easyhandle,
  1456. CURLOPT_READDATA,
  1457. s);
  1458. curl_easy_setopt (s->get.easyhandle,
  1459. CURLOPT_WRITEFUNCTION,
  1460. &client_receive);
  1461. curl_easy_setopt (s->get.easyhandle,
  1462. CURLOPT_WRITEDATA,
  1463. s);
  1464. /* No timeout by default, timeout done with session timeout */
  1465. curl_easy_setopt (s->get.easyhandle,
  1466. CURLOPT_TIMEOUT,
  1467. 0L);
  1468. curl_easy_setopt (s->get.easyhandle,
  1469. CURLOPT_PRIVATE, s);
  1470. curl_easy_setopt (s->get.easyhandle,
  1471. CURLOPT_CONNECTTIMEOUT_MS,
  1472. (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
  1473. curl_easy_setopt (s->get.easyhandle, CURLOPT_BUFFERSIZE,
  1474. 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
  1475. #if CURL_TCP_NODELAY
  1476. curl_easy_setopt (ps->recv_endpoint,
  1477. CURLOPT_TCP_NODELAY,
  1478. 1L);
  1479. #endif
  1480. curl_easy_setopt (s->get.easyhandle,
  1481. CURLOPT_FOLLOWLOCATION,
  1482. 0L);
  1483. mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
  1484. s->get.easyhandle);
  1485. if (CURLM_OK != mret)
  1486. {
  1487. LOG (GNUNET_ERROR_TYPE_ERROR,
  1488. "Session %p : Failed to add GET handle to multihandle: `%s'\n",
  1489. s,
  1490. curl_multi_strerror (mret));
  1491. curl_easy_cleanup (s->get.easyhandle);
  1492. s->get.easyhandle = NULL;
  1493. s->get.s = NULL;
  1494. GNUNET_break (0);
  1495. return GNUNET_SYSERR;
  1496. }
  1497. s->plugin->cur_requests++;
  1498. LOG (GNUNET_ERROR_TYPE_INFO,
  1499. "GET request `%s' established, number of requests increased to %u\n",
  1500. s->url,
  1501. s->plugin->cur_requests);
  1502. return GNUNET_OK;
  1503. }
  1504. /**
  1505. * Connect a HTTP put request
  1506. *
  1507. * @param s the session to connect
  1508. * @return #GNUNET_SYSERR for hard failure, #GNUNET_OK for ok
  1509. */
  1510. static int
  1511. client_connect_put (struct Session *s)
  1512. {
  1513. CURLMcode mret;
  1514. struct HttpAddress *ha;
  1515. uint32_t options;
  1516. ha = (struct HttpAddress *) s->address->address;
  1517. options = ntohl (ha->options);
  1518. /* create put request */
  1519. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1520. "Session %p: Init PUT handle\n",
  1521. s);
  1522. s->put.easyhandle = curl_easy_init ();
  1523. s->put.s = s;
  1524. #if VERBOSE_CURL
  1525. curl_easy_setopt (s->put.easyhandle,
  1526. CURLOPT_VERBOSE,
  1527. 1L);
  1528. curl_easy_setopt (s->put.easyhandle,
  1529. CURLOPT_DEBUGFUNCTION,
  1530. &client_log);
  1531. curl_easy_setopt (s->put.easyhandle,
  1532. CURLOPT_DEBUGDATA,
  1533. &s->put);
  1534. #endif
  1535. if (0 != (options & HTTP_OPTIONS_TCP_STEALTH))
  1536. {
  1537. #ifdef TCP_STEALTH
  1538. curl_easy_setopt (s->put.easyhandle,
  1539. CURLOPT_OPENSOCKETFUNCTION,
  1540. &open_tcp_stealth_socket_cb);
  1541. curl_easy_setopt (s->put.easyhandle,
  1542. CURLOPT_OPENSOCKETDATA,
  1543. s);
  1544. #else
  1545. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1546. "Cannot connect, TCP STEALTH needed and not supported by kernel.\n");
  1547. curl_easy_cleanup (s->put.easyhandle);
  1548. s->put.easyhandle = NULL;
  1549. s->put.s = NULL;
  1550. s->put.state = H_DISCONNECTED;
  1551. return GNUNET_SYSERR;
  1552. #endif
  1553. }
  1554. #if BUILD_HTTPS
  1555. curl_easy_setopt (s->put.easyhandle,
  1556. CURLOPT_SSLVERSION,
  1557. CURL_SSLVERSION_TLSv1);
  1558. {
  1559. struct HttpAddress *ha;
  1560. ha = (struct HttpAddress *) s->address->address;
  1561. if (HTTP_OPTIONS_VERIFY_CERTIFICATE ==
  1562. (ntohl (ha->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE))
  1563. {
  1564. curl_easy_setopt (s->put.easyhandle,
  1565. CURLOPT_SSL_VERIFYPEER,
  1566. 1L);
  1567. curl_easy_setopt (s->put.easyhandle,
  1568. CURLOPT_SSL_VERIFYHOST,
  1569. 2L);
  1570. }
  1571. else
  1572. {
  1573. curl_easy_setopt (s->put.easyhandle,
  1574. CURLOPT_SSL_VERIFYPEER,
  1575. 0L);
  1576. curl_easy_setopt (s->put.easyhandle,
  1577. CURLOPT_SSL_VERIFYHOST,
  1578. 0L);
  1579. }
  1580. }
  1581. curl_easy_setopt (s->put.easyhandle,
  1582. CURLOPT_PROTOCOLS,
  1583. CURLPROTO_HTTPS);
  1584. curl_easy_setopt (s->put.easyhandle,
  1585. CURLOPT_REDIR_PROTOCOLS,
  1586. CURLPROTO_HTTPS);
  1587. #else
  1588. curl_easy_setopt (s->put.easyhandle,
  1589. CURLOPT_PROTOCOLS,
  1590. CURLPROTO_HTTP);
  1591. curl_easy_setopt (s->put.easyhandle,
  1592. CURLOPT_REDIR_PROTOCOLS,
  1593. CURLPROTO_HTTP);
  1594. #endif
  1595. if (NULL != s->plugin->proxy_hostname)
  1596. {
  1597. curl_easy_setopt (s->put.easyhandle,
  1598. CURLOPT_PROXY,
  1599. s->plugin->proxy_hostname);
  1600. curl_easy_setopt (s->put.easyhandle,
  1601. CURLOPT_PROXYTYPE,
  1602. s->plugin->proxytype);
  1603. if (NULL != s->plugin->proxy_username)
  1604. curl_easy_setopt (s->put.easyhandle,
  1605. CURLOPT_PROXYUSERNAME,
  1606. s->plugin->proxy_username);
  1607. if (NULL != s->plugin->proxy_password)
  1608. curl_easy_setopt (s->put.easyhandle,
  1609. CURLOPT_PROXYPASSWORD,
  1610. s->plugin->proxy_password);
  1611. if (GNUNET_YES == s->plugin->proxy_use_httpproxytunnel)
  1612. curl_easy_setopt (s->put.easyhandle,
  1613. CURLOPT_HTTPPROXYTUNNEL,
  1614. s->plugin->proxy_use_httpproxytunnel);
  1615. }
  1616. curl_easy_setopt (s->put.easyhandle,
  1617. CURLOPT_URL,
  1618. s->url);
  1619. curl_easy_setopt (s->put.easyhandle,
  1620. CURLOPT_UPLOAD,
  1621. 1L);
  1622. curl_easy_setopt (s->put.easyhandle,
  1623. CURLOPT_READFUNCTION,
  1624. &client_send_cb);
  1625. curl_easy_setopt (s->put.easyhandle,
  1626. CURLOPT_READDATA,
  1627. s);
  1628. curl_easy_setopt (s->put.easyhandle,
  1629. CURLOPT_WRITEFUNCTION,
  1630. &client_receive_put);
  1631. curl_easy_setopt (s->put.easyhandle,
  1632. CURLOPT_WRITEDATA,
  1633. s);
  1634. /* No timeout by default, timeout done with session timeout */
  1635. curl_easy_setopt (s->put.easyhandle,
  1636. CURLOPT_TIMEOUT,
  1637. 0L);
  1638. curl_easy_setopt (s->put.easyhandle,
  1639. CURLOPT_PRIVATE,
  1640. s);
  1641. curl_easy_setopt (s->put.easyhandle,
  1642. CURLOPT_CONNECTTIMEOUT_MS,
  1643. (long) (HTTP_CLIENT_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL));
  1644. curl_easy_setopt (s->put.easyhandle, CURLOPT_BUFFERSIZE,
  1645. 2 * GNUNET_SERVER_MAX_MESSAGE_SIZE);
  1646. #if CURL_TCP_NODELAY
  1647. curl_easy_setopt (s->put.easyhandle, CURLOPT_TCP_NODELAY, 1);
  1648. #endif
  1649. mret = curl_multi_add_handle (s->plugin->curl_multi_handle,
  1650. s->put.easyhandle);
  1651. if (CURLM_OK != mret)
  1652. {
  1653. LOG (GNUNET_ERROR_TYPE_ERROR,
  1654. "Session %p : Failed to add PUT handle to multihandle: `%s'\n",
  1655. s, curl_multi_strerror (mret));
  1656. curl_easy_cleanup (s->put.easyhandle);
  1657. s->put.easyhandle = NULL;
  1658. s->put.s = NULL;
  1659. s->put.state = H_DISCONNECTED;
  1660. return GNUNET_SYSERR;
  1661. }
  1662. s->put.state = H_CONNECTED;
  1663. s->plugin->cur_requests++;
  1664. LOG (GNUNET_ERROR_TYPE_INFO,
  1665. "PUT request `%s' established, number of requests increased to %u\n",
  1666. s->url, s->plugin->cur_requests);
  1667. return GNUNET_OK;
  1668. }
  1669. /**
  1670. * Connect both PUT and GET request for a session
  1671. *
  1672. * @param s the session to connect
  1673. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  1674. */
  1675. static int
  1676. client_connect (struct Session *s)
  1677. {
  1678. struct HTTP_Client_Plugin *plugin = s->plugin;
  1679. int res = GNUNET_OK;
  1680. /* create url */
  1681. if (NULL ==
  1682. http_common_plugin_address_to_string(plugin->protocol,
  1683. s->address->address,
  1684. s->address->address_length))
  1685. {
  1686. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1687. "Invalid address peer `%s'\n",
  1688. GNUNET_i2s(&s->address->peer));
  1689. return GNUNET_SYSERR;
  1690. }
  1691. GNUNET_asprintf (&s->url,
  1692. "%s/%s;%u",
  1693. http_common_plugin_address_to_url (NULL,
  1694. s->address->address,
  1695. s->address->address_length),
  1696. GNUNET_i2s_full (plugin->env->my_identity),
  1697. plugin->last_tag);
  1698. plugin->last_tag++;
  1699. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1700. "Initiating outbound session peer `%s' using address `%s'\n",
  1701. GNUNET_i2s (&s->address->peer), s->url);
  1702. if (GNUNET_SYSERR == client_connect_get (s))
  1703. return GNUNET_SYSERR;
  1704. /* If we are emulating an XHR client then delay sending a PUT request until
  1705. * there is something to send.
  1706. */
  1707. if (GNUNET_YES == plugin->emulate_xhr)
  1708. {
  1709. s->put.state = H_TMP_DISCONNECTED;
  1710. }
  1711. else if (GNUNET_SYSERR == client_connect_put (s))
  1712. return GNUNET_SYSERR;
  1713. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1714. "Session %p: connected with GET %p and PUT %p\n",
  1715. s, s->get.easyhandle,
  1716. s->put.easyhandle);
  1717. /* Perform connect */
  1718. GNUNET_STATISTICS_set (plugin->env->stats,
  1719. HTTP_STAT_STR_CONNECTIONS,
  1720. plugin->cur_requests,
  1721. GNUNET_NO);
  1722. /* Re-schedule since handles have changed */
  1723. if (NULL != plugin->client_perform_task)
  1724. {
  1725. GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
  1726. plugin->client_perform_task = NULL;
  1727. }
  1728. /* Schedule task to run immediately */
  1729. plugin->client_perform_task = GNUNET_SCHEDULER_add_now (client_run,
  1730. plugin);
  1731. return res;
  1732. }
  1733. /**
  1734. * Function obtain the network type for a session
  1735. *
  1736. * @param cls closure (`struct Plugin*`)
  1737. * @param session the session
  1738. * @return the network type
  1739. */
  1740. static enum GNUNET_ATS_Network_Type
  1741. http_client_plugin_get_network (void *cls,
  1742. struct Session *session)
  1743. {
  1744. return session->scope;
  1745. }
  1746. /**
  1747. * Session was idle, so disconnect it
  1748. *
  1749. * @param cls the `struct Session` of the idle session
  1750. * @param tc scheduler context
  1751. */
  1752. static void
  1753. client_session_timeout (void *cls,
  1754. const struct GNUNET_SCHEDULER_TaskContext *tc)
  1755. {
  1756. struct Session *s = cls;
  1757. struct GNUNET_TIME_Relative left;
  1758. s->timeout_task = NULL;
  1759. left = GNUNET_TIME_absolute_get_remaining (s->timeout);
  1760. if (0 != left.rel_value_us)
  1761. {
  1762. /* not actually our turn yet, but let's at least update
  1763. the monitor, it may think we're about to die ... */
  1764. notify_session_monitor (s->plugin,
  1765. s,
  1766. GNUNET_TRANSPORT_SS_UPDATE);
  1767. s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
  1768. &client_session_timeout,
  1769. s);
  1770. return;
  1771. }
  1772. LOG (TIMEOUT_LOG,
  1773. "Session %p was idle for %s, disconnecting\n",
  1774. s,
  1775. GNUNET_STRINGS_relative_time_to_string (HTTP_CLIENT_SESSION_TIMEOUT,
  1776. GNUNET_YES));
  1777. GNUNET_assert (GNUNET_OK ==
  1778. http_client_plugin_session_disconnect (s->plugin,
  1779. s));
  1780. }
  1781. /**
  1782. * Creates a new outbound session the transport service will use to
  1783. * send data to the peer
  1784. *
  1785. * @param cls the plugin
  1786. * @param address the address
  1787. * @return the session or NULL of max connections exceeded
  1788. */
  1789. static struct Session *
  1790. http_client_plugin_get_session (void *cls,
  1791. const struct GNUNET_HELLO_Address *address)
  1792. {
  1793. struct HTTP_Client_Plugin *plugin = cls;
  1794. struct Session *s;
  1795. struct sockaddr *sa;
  1796. enum GNUNET_ATS_Network_Type net_type;
  1797. size_t salen = 0;
  1798. int res;
  1799. GNUNET_assert (NULL != address->address);
  1800. /* find existing session */
  1801. s = client_lookup_session (plugin, address);
  1802. if (NULL != s)
  1803. return s;
  1804. /* create a new session */
  1805. if (plugin->max_requests <= plugin->cur_requests)
  1806. {
  1807. LOG (GNUNET_ERROR_TYPE_WARNING,
  1808. "Maximum number of requests (%u) reached: "
  1809. "cannot connect to peer `%s'\n",
  1810. plugin->max_requests,
  1811. GNUNET_i2s (&address->peer));
  1812. return NULL;
  1813. }
  1814. /* Determine network location */
  1815. net_type = GNUNET_ATS_NET_UNSPECIFIED;
  1816. sa = http_common_socket_from_address (address->address,
  1817. address->address_length,
  1818. &res);
  1819. if (GNUNET_SYSERR == res)
  1820. return NULL;
  1821. if (GNUNET_YES == res)
  1822. {
  1823. GNUNET_assert (NULL != sa);
  1824. if (AF_INET == sa->sa_family)
  1825. {
  1826. salen = sizeof (struct sockaddr_in);
  1827. }
  1828. else if (AF_INET6 == sa->sa_family)
  1829. {
  1830. salen = sizeof (struct sockaddr_in6);
  1831. }
  1832. net_type = plugin->env->get_address_type (plugin->env->cls, sa, salen);
  1833. GNUNET_free (sa);
  1834. }
  1835. else if (GNUNET_NO == res)
  1836. {
  1837. /* Cannot convert to sockaddr -> is external hostname */
  1838. net_type = GNUNET_ATS_NET_WAN;
  1839. }
  1840. if (GNUNET_ATS_NET_UNSPECIFIED == net_type)
  1841. {
  1842. GNUNET_break (0);
  1843. return NULL;
  1844. }
  1845. s = GNUNET_new (struct Session);
  1846. s->plugin = plugin;
  1847. s->address = GNUNET_HELLO_address_copy (address);
  1848. s->scope = net_type;
  1849. s->put.state = H_NOT_CONNECTED;
  1850. s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_CLIENT_SESSION_TIMEOUT);
  1851. s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_CLIENT_SESSION_TIMEOUT,
  1852. &client_session_timeout,
  1853. s);
  1854. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1855. "Created new session %p for `%s' address `%s''\n",
  1856. s,
  1857. http_common_plugin_address_to_string (plugin->protocol,
  1858. s->address->address,
  1859. s->address->address_length),
  1860. GNUNET_i2s (&s->address->peer));
  1861. /* add new session */
  1862. (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
  1863. &s->address->peer,
  1864. s,
  1865. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  1866. /* initiate new connection */
  1867. if (GNUNET_SYSERR == client_connect (s))
  1868. {
  1869. LOG (GNUNET_ERROR_TYPE_ERROR,
  1870. "Cannot connect to peer `%s' address `%s''\n",
  1871. http_common_plugin_address_to_string (plugin->protocol,
  1872. s->address->address, s->address->address_length),
  1873. GNUNET_i2s (&s->address->peer));
  1874. client_delete_session (s);
  1875. return NULL;
  1876. }
  1877. notify_session_monitor (plugin,
  1878. s,
  1879. GNUNET_TRANSPORT_SS_INIT);
  1880. notify_session_monitor (plugin,
  1881. s,
  1882. GNUNET_TRANSPORT_SS_UP); /* or handshake? */
  1883. return s;
  1884. }
  1885. /**
  1886. * Setup http_client plugin
  1887. *
  1888. * @param plugin the plugin handle
  1889. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  1890. */
  1891. static int
  1892. client_start (struct HTTP_Client_Plugin *plugin)
  1893. {
  1894. curl_global_init (CURL_GLOBAL_ALL);
  1895. plugin->curl_multi_handle = curl_multi_init ();
  1896. if (NULL == plugin->curl_multi_handle)
  1897. {
  1898. LOG (GNUNET_ERROR_TYPE_ERROR,
  1899. _("Could not initialize curl multi handle, failed to start %s plugin!\n"),
  1900. plugin->name);
  1901. return GNUNET_SYSERR;
  1902. }
  1903. return GNUNET_OK;
  1904. }
  1905. /**
  1906. * Another peer has suggested an address for this
  1907. * peer and transport plugin. Check that this could be a valid
  1908. * address. If so, consider adding it to the list
  1909. * of addresses.
  1910. *
  1911. * @param cls closure with the `struct Plugin`
  1912. * @param addr pointer to the address
  1913. * @param addrlen length of @a addr
  1914. * @return #GNUNET_OK if this is a plausible address for this peer
  1915. * and transport; always returns #GNUNET_NO (this is the client!)
  1916. */
  1917. static int
  1918. http_client_plugin_address_suggested (void *cls,
  1919. const void *addr,
  1920. size_t addrlen)
  1921. {
  1922. /* A HTTP/S client does not have any valid address so:*/
  1923. return GNUNET_NO;
  1924. }
  1925. /**
  1926. * Exit point from the plugin.
  1927. *
  1928. * @param cls api as closure
  1929. * @return NULL
  1930. */
  1931. void *
  1932. LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
  1933. {
  1934. struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
  1935. struct HTTP_Client_Plugin *plugin = api->cls;
  1936. if (NULL == api->cls)
  1937. {
  1938. /* Stub shutdown */
  1939. GNUNET_free (api);
  1940. return NULL;
  1941. }
  1942. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1943. _("Shutting down plugin `%s'\n"),
  1944. plugin->name);
  1945. GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
  1946. &destroy_session_cb,
  1947. plugin);
  1948. if (NULL != plugin->client_perform_task)
  1949. {
  1950. GNUNET_SCHEDULER_cancel (plugin->client_perform_task);
  1951. plugin->client_perform_task = NULL;
  1952. }
  1953. if (NULL != plugin->curl_multi_handle)
  1954. {
  1955. curl_multi_cleanup (plugin->curl_multi_handle);
  1956. plugin->curl_multi_handle = NULL;
  1957. }
  1958. curl_global_cleanup ();
  1959. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1960. _("Shutdown for plugin `%s' complete\n"),
  1961. plugin->name);
  1962. GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
  1963. GNUNET_free_non_null (plugin->proxy_hostname);
  1964. GNUNET_free_non_null (plugin->proxy_username);
  1965. GNUNET_free_non_null (plugin->proxy_password);
  1966. GNUNET_free (plugin);
  1967. GNUNET_free (api);
  1968. return NULL;
  1969. }
  1970. /**
  1971. * Configure plugin
  1972. *
  1973. * @param plugin the plugin handle
  1974. * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  1975. */
  1976. static int
  1977. client_configure_plugin (struct HTTP_Client_Plugin *plugin)
  1978. {
  1979. unsigned long long max_requests;
  1980. char *proxy_type;
  1981. /* Optional parameters */
  1982. if (GNUNET_OK !=
  1983. GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
  1984. plugin->name,
  1985. "MAX_CONNECTIONS",
  1986. &max_requests))
  1987. max_requests = 128;
  1988. plugin->max_requests = max_requests;
  1989. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1990. _("Maximum number of requests is %u\n"),
  1991. plugin->max_requests);
  1992. /* Read proxy configuration */
  1993. if (GNUNET_OK ==
  1994. GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
  1995. plugin->name,
  1996. "PROXY",
  1997. &plugin->proxy_hostname))
  1998. {
  1999. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2000. "Found proxy host: `%s'\n",
  2001. plugin->proxy_hostname);
  2002. /* proxy username */
  2003. if (GNUNET_OK ==
  2004. GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
  2005. plugin->name,
  2006. "PROXY_USERNAME",
  2007. &plugin->proxy_username))
  2008. {
  2009. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2010. "Found proxy username name: `%s'\n",
  2011. plugin->proxy_username);
  2012. }
  2013. /* proxy password */
  2014. if (GNUNET_OK ==
  2015. GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
  2016. plugin->name,
  2017. "PROXY_PASSWORD",
  2018. &plugin->proxy_password))
  2019. {
  2020. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2021. "Found proxy password name: `%s'\n",
  2022. plugin->proxy_password);
  2023. }
  2024. /* proxy type */
  2025. if (GNUNET_OK ==
  2026. GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
  2027. plugin->name,
  2028. "PROXY_TYPE",
  2029. &proxy_type))
  2030. {
  2031. GNUNET_STRINGS_utf8_toupper (proxy_type, proxy_type);
  2032. if (0 == strcmp(proxy_type, "HTTP"))
  2033. plugin->proxytype = CURLPROXY_HTTP;
  2034. else if (0 == strcmp(proxy_type, "SOCKS4"))
  2035. plugin->proxytype = CURLPROXY_SOCKS4;
  2036. else if (0 == strcmp(proxy_type, "SOCKS5"))
  2037. plugin->proxytype = CURLPROXY_SOCKS5;
  2038. else if (0 == strcmp(proxy_type, "SOCKS4A"))
  2039. plugin->proxytype = CURLPROXY_SOCKS4A;
  2040. else if (0 == strcmp(proxy_type, "SOCKS5_HOSTNAME "))
  2041. plugin->proxytype = CURLPROXY_SOCKS5_HOSTNAME ;
  2042. else
  2043. {
  2044. LOG (GNUNET_ERROR_TYPE_ERROR,
  2045. _("Invalid proxy type: `%s', disabling proxy! Check configuration!\n"),
  2046. proxy_type);
  2047. GNUNET_free (proxy_type);
  2048. GNUNET_free (plugin->proxy_hostname);
  2049. plugin->proxy_hostname = NULL;
  2050. GNUNET_free_non_null (plugin->proxy_username);
  2051. plugin->proxy_username = NULL;
  2052. GNUNET_free_non_null (plugin->proxy_password);
  2053. plugin->proxy_password = NULL;
  2054. return GNUNET_SYSERR;
  2055. }
  2056. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2057. "Found proxy type: `%s'\n",
  2058. proxy_type);
  2059. }
  2060. /* proxy http tunneling */
  2061. plugin->proxy_use_httpproxytunnel
  2062. = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
  2063. plugin->name,
  2064. "PROXY_HTTP_TUNNELING");
  2065. if (GNUNET_SYSERR == plugin->proxy_use_httpproxytunnel)
  2066. plugin->proxy_use_httpproxytunnel = GNUNET_NO;
  2067. GNUNET_free_non_null (proxy_type);
  2068. }
  2069. /* Should we emulate an XHR client for testing? */
  2070. plugin->emulate_xhr
  2071. = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
  2072. plugin->name,
  2073. "EMULATE_XHR");
  2074. return GNUNET_OK;
  2075. }
  2076. /**
  2077. * Function to convert an address to a human-readable string.
  2078. *
  2079. * @param cls closure
  2080. * @param addr address to convert
  2081. * @param addrlen address length
  2082. * @return res string if conversion was successful, NULL otherwise
  2083. */
  2084. static const char *
  2085. http_client_plugin_address_to_string (void *cls,
  2086. const void *addr,
  2087. size_t addrlen)
  2088. {
  2089. return http_common_plugin_address_to_string (PLUGIN_NAME,
  2090. addr,
  2091. addrlen);
  2092. }
  2093. /**
  2094. * Function that will be called whenever the transport service wants to
  2095. * notify the plugin that a session is still active and in use and
  2096. * therefore the session timeout for this session has to be updated
  2097. *
  2098. * @param cls closure
  2099. * @param peer which peer was the session for
  2100. * @param session which session is being updated
  2101. */
  2102. static void
  2103. http_client_plugin_update_session_timeout (void *cls,
  2104. const struct GNUNET_PeerIdentity *peer,
  2105. struct Session *session)
  2106. {
  2107. client_reschedule_session_timeout (session);
  2108. }
  2109. /**
  2110. * Function that will be called whenever the transport service wants to
  2111. * notify the plugin that the inbound quota changed and that the plugin
  2112. * should update it's delay for the next receive value
  2113. *
  2114. * @param cls closure
  2115. * @param peer which peer was the session for
  2116. * @param s which session is being updated
  2117. * @param delay new delay to use for receiving
  2118. */
  2119. static void
  2120. http_client_plugin_update_inbound_delay (void *cls,
  2121. const struct GNUNET_PeerIdentity *peer,
  2122. struct Session *s,
  2123. struct GNUNET_TIME_Relative delay)
  2124. {
  2125. s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
  2126. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2127. "New inbound delay %s\n",
  2128. GNUNET_STRINGS_relative_time_to_string (delay,
  2129. GNUNET_NO));
  2130. if (s->recv_wakeup_task != NULL)
  2131. {
  2132. GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
  2133. s->recv_wakeup_task = GNUNET_SCHEDULER_add_delayed (delay,
  2134. &client_wake_up, s);
  2135. }
  2136. }
  2137. /**
  2138. * Return information about the given session to the
  2139. * monitor callback.
  2140. *
  2141. * @param cls the `struct Plugin` with the monitor callback (`sic`)
  2142. * @param peer peer we send information about
  2143. * @param value our `struct Session` to send information about
  2144. * @return #GNUNET_OK (continue to iterate)
  2145. */
  2146. static int
  2147. send_session_info_iter (void *cls,
  2148. const struct GNUNET_PeerIdentity *peer,
  2149. void *value)
  2150. {
  2151. struct HTTP_Client_Plugin *plugin = cls;
  2152. struct Session *session = value;
  2153. notify_session_monitor (plugin,
  2154. session,
  2155. GNUNET_TRANSPORT_SS_INIT);
  2156. notify_session_monitor (plugin,
  2157. session,
  2158. GNUNET_TRANSPORT_SS_UP); /* FIXME: or handshake? */
  2159. return GNUNET_OK;
  2160. }
  2161. /**
  2162. * Begin monitoring sessions of a plugin. There can only
  2163. * be one active monitor per plugin (i.e. if there are
  2164. * multiple monitors, the transport service needs to
  2165. * multiplex the generated events over all of them).
  2166. *
  2167. * @param cls closure of the plugin
  2168. * @param sic callback to invoke, NULL to disable monitor;
  2169. * plugin will being by iterating over all active
  2170. * sessions immediately and then enter monitor mode
  2171. * @param sic_cls closure for @a sic
  2172. */
  2173. static void
  2174. http_client_plugin_setup_monitor (void *cls,
  2175. GNUNET_TRANSPORT_SessionInfoCallback sic,
  2176. void *sic_cls)
  2177. {
  2178. struct HTTP_Client_Plugin *plugin = cls;
  2179. plugin->sic = sic;
  2180. plugin->sic_cls = sic_cls;
  2181. if (NULL != sic)
  2182. {
  2183. GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
  2184. &send_session_info_iter,
  2185. plugin);
  2186. /* signal end of first iteration */
  2187. sic (sic_cls, NULL, NULL);
  2188. }
  2189. }
  2190. /**
  2191. * Entry point for the plugin.
  2192. */
  2193. void *
  2194. LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
  2195. {
  2196. struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
  2197. struct GNUNET_TRANSPORT_PluginFunctions *api;
  2198. struct HTTP_Client_Plugin *plugin;
  2199. if (NULL == env->receive)
  2200. {
  2201. /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
  2202. initialze the plugin or the API */
  2203. api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
  2204. api->cls = NULL;
  2205. api->address_to_string = &http_client_plugin_address_to_string;
  2206. api->string_to_address = &http_common_plugin_string_to_address;
  2207. api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
  2208. return api;
  2209. }
  2210. plugin = GNUNET_new (struct HTTP_Client_Plugin);
  2211. plugin->env = env;
  2212. plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
  2213. GNUNET_YES);
  2214. api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
  2215. api->cls = plugin;
  2216. api->send = &http_client_plugin_send;
  2217. api->disconnect_session = &http_client_plugin_session_disconnect;
  2218. api->query_keepalive_factor = &http_client_query_keepalive_factor;
  2219. api->disconnect_peer = &http_client_plugin_peer_disconnect;
  2220. api->check_address = &http_client_plugin_address_suggested;
  2221. api->get_session = &http_client_plugin_get_session;
  2222. api->address_to_string = &http_client_plugin_address_to_string;
  2223. api->string_to_address = &http_common_plugin_string_to_address;
  2224. api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
  2225. api->get_network = &http_client_plugin_get_network;
  2226. api->update_session_timeout = &http_client_plugin_update_session_timeout;
  2227. api->update_inbound_delay = &http_client_plugin_update_inbound_delay;
  2228. api->setup_monitor = &http_client_plugin_setup_monitor;
  2229. #if BUILD_HTTPS
  2230. plugin->name = "transport-https_client";
  2231. plugin->protocol = "https";
  2232. #else
  2233. plugin->name = "transport-http_client";
  2234. plugin->protocol = "http";
  2235. #endif
  2236. plugin->last_tag = 1;
  2237. if (GNUNET_SYSERR == client_configure_plugin (plugin))
  2238. {
  2239. LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
  2240. return NULL;
  2241. }
  2242. /* Start client */
  2243. if (GNUNET_SYSERR == client_start (plugin))
  2244. {
  2245. LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
  2246. return NULL;
  2247. }
  2248. return api;
  2249. }
  2250. /* end of plugin_transport_http_client.c */