plugin_transport_http_server.c 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470
  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_server.c
  19. * @brief HTTP/S server transport plugin
  20. * @author Matthias Wachs
  21. * @author David Barksdale
  22. * @author Christian Grothoff
  23. */
  24. #include "platform.h"
  25. #include "gnunet_util_lib.h"
  26. #include "gnunet_statistics_service.h"
  27. #include "gnunet_transport_plugin.h"
  28. #include "gnunet_nat_lib.h"
  29. #include "plugin_transport_http_common.h"
  30. #include <microhttpd.h>
  31. #include <regex.h>
  32. #if BUILD_HTTPS
  33. #define PLUGIN_NAME "https_server"
  34. #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_https_server_init
  35. #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_https_server_done
  36. #else
  37. #define PLUGIN_NAME "http_server"
  38. #define LIBGNUNET_PLUGIN_TRANSPORT_INIT libgnunet_plugin_transport_http_server_init
  39. #define LIBGNUNET_PLUGIN_TRANSPORT_DONE libgnunet_plugin_transport_http_server_done
  40. #endif
  41. #define HTTP_ERROR_RESPONSE "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD><BODY><H1>Not Found</H1>The requested URL was not found on this server.<P><HR><ADDRESS></ADDRESS></BODY></HTML>"
  42. #define _RECEIVE 0
  43. #define _SEND 1
  44. #define LOG(kind,...) GNUNET_log_from (kind, "transport-" PLUGIN_NAME,__VA_ARGS__)
  45. /**
  46. * Information we keep with MHD for an HTTP request.
  47. */
  48. struct ServerRequest
  49. {
  50. /**
  51. * The session this server request belongs to
  52. * Can be NULL, when session was disconnected and freed
  53. */
  54. struct Session *session;
  55. /**
  56. * The MHD connection
  57. */
  58. struct MHD_Connection *mhd_conn;
  59. /**
  60. * The MHD daemon
  61. */
  62. struct MHD_Daemon *mhd_daemon;
  63. /**
  64. * Options requested by peer
  65. */
  66. uint32_t options;
  67. #define OPTION_LONG_POLL 1 /* GET request wants long-poll semantics */
  68. /**
  69. * _RECV or _SEND
  70. */
  71. int direction;
  72. /**
  73. * For PUT requests: Is this the first or last callback with size 0
  74. * For GET requests: Have we sent a message
  75. */
  76. int connected;
  77. };
  78. /**
  79. * Wrapper to manage addresses
  80. */
  81. struct HttpAddressWrapper
  82. {
  83. /**
  84. * Linked list next
  85. */
  86. struct HttpAddressWrapper *next;
  87. /**
  88. * Linked list previous
  89. */
  90. struct HttpAddressWrapper *prev;
  91. /**
  92. * An address we are using.
  93. */
  94. struct HttpAddress *address;
  95. /**
  96. * Length of the address.
  97. */
  98. size_t addrlen;
  99. };
  100. /**
  101. * Message to send using http
  102. */
  103. struct HTTP_Message
  104. {
  105. /**
  106. * next pointer for double linked list
  107. */
  108. struct HTTP_Message *next;
  109. /**
  110. * previous pointer for double linked list
  111. */
  112. struct HTTP_Message *prev;
  113. /**
  114. * buffer containing data to send
  115. */
  116. char *buf;
  117. /**
  118. * amount of data already sent
  119. */
  120. size_t pos;
  121. /**
  122. * buffer length
  123. */
  124. size_t size;
  125. /**
  126. * HTTP/S specific overhead
  127. */
  128. size_t overhead;
  129. /**
  130. * Continuation function to call once the transmission buffer
  131. * has again space available. NULL if there is no
  132. * continuation to call.
  133. */
  134. GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
  135. /**
  136. * Closure for transmit_cont.
  137. */
  138. void *transmit_cont_cls;
  139. };
  140. /**
  141. * Session handle for connections.
  142. */
  143. struct Session
  144. {
  145. /**
  146. * To whom are we talking to (set to our identity
  147. * if we are still waiting for the welcome message)
  148. */
  149. struct GNUNET_PeerIdentity target;
  150. /**
  151. * Pointer to the global plugin struct.
  152. */
  153. struct HTTP_Server_Plugin *plugin;
  154. /**
  155. * next pointer for double linked list
  156. */
  157. struct HTTP_Message *msg_head;
  158. /**
  159. * previous pointer for double linked list
  160. */
  161. struct HTTP_Message *msg_tail;
  162. /**
  163. * Message stream tokenizer for incoming data
  164. */
  165. struct GNUNET_SERVER_MessageStreamTokenizer *msg_tk;
  166. /**
  167. * Client recv handle
  168. */
  169. struct ServerRequest *server_recv;
  170. /**
  171. * Client send handle
  172. */
  173. struct ServerRequest *server_send;
  174. /**
  175. * Address
  176. */
  177. struct GNUNET_HELLO_Address *address;
  178. /**
  179. * Absolute time when to receive data again
  180. * Used for receive throttling
  181. */
  182. struct GNUNET_TIME_Absolute next_receive;
  183. /**
  184. * Absolute time when this connection will time out.
  185. */
  186. struct GNUNET_TIME_Absolute timeout;
  187. /**
  188. * Session timeout task
  189. */
  190. struct GNUNET_SCHEDULER_Task * timeout_task;
  191. /**
  192. * Task to resume MHD handling when receiving is allowed again
  193. */
  194. struct GNUNET_SCHEDULER_Task * recv_wakeup_task;
  195. /**
  196. * Number of bytes waiting for transmission to this peer.
  197. */
  198. unsigned long long bytes_in_queue;
  199. /**
  200. * Number of messages waiting for transmission to this peer.
  201. */
  202. unsigned int msgs_in_queue;
  203. /**
  204. * Unique HTTP/S connection tag for this connection
  205. */
  206. uint32_t tag;
  207. /**
  208. * ATS network type.
  209. */
  210. enum GNUNET_ATS_Network_Type scope;
  211. /**
  212. * #GNUNET_YES if this session is known to the service.
  213. */
  214. int known_to_service;
  215. };
  216. /**
  217. * Encapsulation of all of the state of the plugin.
  218. */
  219. struct HTTP_Server_Plugin
  220. {
  221. /**
  222. * Our environment.
  223. */
  224. struct GNUNET_TRANSPORT_PluginEnvironment *env;
  225. /**
  226. * Hash map of open sessions.
  227. */
  228. struct GNUNET_CONTAINER_MultiPeerMap *sessions;
  229. /**
  230. * Function to call about session status changes.
  231. */
  232. GNUNET_TRANSPORT_SessionInfoCallback sic;
  233. /**
  234. * Closure for @e sic.
  235. */
  236. void *sic_cls;
  237. /**
  238. * Plugin name
  239. */
  240. char *name;
  241. /**
  242. * Protocol
  243. */
  244. char *protocol;
  245. /**
  246. * External address
  247. */
  248. char *external_hostname;
  249. /**
  250. * External hostname the plugin can be connected to, can be different to
  251. * the host's FQDN, used e.g. for reverse proxying
  252. */
  253. struct GNUNET_HELLO_Address *ext_addr;
  254. /**
  255. * NAT handle & address management
  256. */
  257. struct GNUNET_NAT_Handle *nat;
  258. /**
  259. * IPv4 addresses DLL head
  260. */
  261. struct HttpAddressWrapper *addr_head;
  262. /**
  263. * IPv4 addresses DLL tail
  264. */
  265. struct HttpAddressWrapper *addr_tail;
  266. /**
  267. * IPv4 server socket to bind to
  268. */
  269. struct sockaddr_in *server_addr_v4;
  270. /**
  271. * IPv6 server socket to bind to
  272. */
  273. struct sockaddr_in6 *server_addr_v6;
  274. /**
  275. * MHD IPv4 daemon
  276. */
  277. struct MHD_Daemon *server_v4;
  278. /**
  279. * MHD IPv4 daemon
  280. */
  281. struct MHD_Daemon *server_v6;
  282. #if BUILD_HTTPS
  283. /**
  284. * Crypto related
  285. *
  286. * Example:
  287. *
  288. * Use RC4-128 instead of AES:
  289. * NONE:+VERS-TLS1.0:+ARCFOUR-128:+SHA1:+RSA:+COMP-NULL
  290. *
  291. */
  292. char *crypto_init;
  293. /**
  294. * TLS key
  295. */
  296. char *key;
  297. /**
  298. * TLS certificate
  299. */
  300. char *cert;
  301. #endif
  302. /**
  303. * MHD IPv4 task
  304. */
  305. struct GNUNET_SCHEDULER_Task * server_v4_task;
  306. /**
  307. * MHD IPv6 task
  308. */
  309. struct GNUNET_SCHEDULER_Task * server_v6_task;
  310. /**
  311. * Task calling transport service about external address
  312. */
  313. struct GNUNET_SCHEDULER_Task * notify_ext_task;
  314. /**
  315. * Notify transport only about external address
  316. */
  317. unsigned int external_only;
  318. /**
  319. * The IPv4 server is scheduled to run asap
  320. */
  321. int server_v4_immediately;
  322. /**
  323. * The IPv6 server is scheduled to run asap
  324. */
  325. int server_v6_immediately;
  326. /**
  327. * Verify external address
  328. */
  329. int verify_external_hostname;
  330. /**
  331. * Maximum number of sockets the plugin can use
  332. * Each http request /request connections are two connections
  333. */
  334. unsigned int max_request;
  335. /**
  336. * Current number of sockets the plugin can use
  337. * Each http connection are two requests
  338. */
  339. unsigned int cur_request;
  340. /**
  341. * Did we immediately end the session in disconnect_cb
  342. */
  343. int in_shutdown;
  344. /**
  345. * Length of peer id
  346. */
  347. int peer_id_length;
  348. /**
  349. * My options to be included in the address
  350. */
  351. uint32_t options;
  352. /**
  353. * use IPv6
  354. */
  355. uint16_t use_ipv6;
  356. /**
  357. * use IPv4
  358. */
  359. uint16_t use_ipv4;
  360. /**
  361. * Port used
  362. */
  363. uint16_t port;
  364. /**
  365. * Regex for parsing URLs. FIXME: this seems overkill.
  366. */
  367. regex_t url_regex;
  368. };
  369. /**
  370. * If a session monitor is attached, notify it about the new
  371. * session state.
  372. *
  373. * @param plugin our plugin
  374. * @param session session that changed state
  375. * @param state new state of the session
  376. */
  377. static void
  378. notify_session_monitor (struct HTTP_Server_Plugin *plugin,
  379. struct Session *session,
  380. enum GNUNET_TRANSPORT_SessionState state)
  381. {
  382. struct GNUNET_TRANSPORT_SessionInfo info;
  383. if (NULL == plugin->sic)
  384. return;
  385. memset (&info, 0, sizeof (info));
  386. info.state = state;
  387. info.is_inbound = GNUNET_YES;
  388. info.num_msg_pending = session->msgs_in_queue;
  389. info.num_bytes_pending = session->bytes_in_queue;
  390. info.receive_delay = session->next_receive;
  391. info.session_timeout = session->timeout;
  392. info.address = session->address;
  393. plugin->sic (plugin->sic_cls,
  394. session,
  395. &info);
  396. }
  397. /**
  398. * Wake up an MHD connection which was suspended
  399. *
  400. * @param cls the session
  401. * @param tc task context
  402. */
  403. static void
  404. server_wake_up (void *cls,
  405. const struct GNUNET_SCHEDULER_TaskContext *tc)
  406. {
  407. struct Session *s = cls;
  408. s->recv_wakeup_task = NULL;
  409. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
  410. return;
  411. LOG (GNUNET_ERROR_TYPE_DEBUG,
  412. "Session %p: Waking up PUT handle\n",
  413. s);
  414. MHD_resume_connection (s->server_recv->mhd_conn);
  415. }
  416. /**
  417. * Reschedule the execution of both IPv4 and IPv6 server.
  418. *
  419. * @param plugin the plugin
  420. * @param server which server to schedule v4 or v6?
  421. * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
  422. * until timeout
  423. */
  424. static void
  425. server_reschedule (struct HTTP_Server_Plugin *plugin,
  426. struct MHD_Daemon *server,
  427. int now);
  428. /**
  429. * Deletes the session. Must not be used afterwards.
  430. *
  431. * @param s the session to delete
  432. */
  433. static void
  434. server_delete_session (struct Session *s)
  435. {
  436. struct HTTP_Server_Plugin *plugin = s->plugin;
  437. struct HTTP_Message *msg;
  438. if (NULL != s->timeout_task)
  439. {
  440. GNUNET_SCHEDULER_cancel (s->timeout_task);
  441. s->timeout_task = NULL;
  442. s->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
  443. }
  444. if (NULL != s->recv_wakeup_task)
  445. {
  446. GNUNET_SCHEDULER_cancel (s->recv_wakeup_task);
  447. s->recv_wakeup_task = NULL;
  448. if (NULL != s->server_recv)
  449. MHD_resume_connection (s->server_recv->mhd_conn);
  450. }
  451. GNUNET_assert (GNUNET_OK ==
  452. GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
  453. &s->target,
  454. s));
  455. while (NULL != (msg = s->msg_head))
  456. {
  457. GNUNET_CONTAINER_DLL_remove (s->msg_head,
  458. s->msg_tail,
  459. msg);
  460. if (NULL != msg->transmit_cont)
  461. msg->transmit_cont (msg->transmit_cont_cls,
  462. &s->target,
  463. GNUNET_SYSERR,
  464. msg->size,
  465. msg->pos + msg->overhead);
  466. GNUNET_assert (s->msgs_in_queue > 0);
  467. s->msgs_in_queue--;
  468. GNUNET_assert (s->bytes_in_queue >= msg->size);
  469. s->bytes_in_queue -= msg->size;
  470. GNUNET_free (msg);
  471. }
  472. GNUNET_assert (0 == s->msgs_in_queue);
  473. GNUNET_assert (0 == s->bytes_in_queue);
  474. if (NULL != s->server_send)
  475. {
  476. LOG (GNUNET_ERROR_TYPE_DEBUG,
  477. "Server: %p / %p Terminating inbound PUT session to peer `%s'\n",
  478. s, s->server_send,
  479. GNUNET_i2s (&s->target));
  480. s->server_send->session = NULL;
  481. MHD_set_connection_option (s->server_send->mhd_conn,
  482. MHD_CONNECTION_OPTION_TIMEOUT,
  483. 1 /* 0 = no timeout, so this is MIN */);
  484. server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_YES);
  485. }
  486. if (NULL != s->server_recv)
  487. {
  488. LOG (GNUNET_ERROR_TYPE_DEBUG,
  489. "Server: %p / %p Terminating inbound GET session to peer `%s'\n",
  490. s, s->server_recv, GNUNET_i2s (&s->target));
  491. s->server_recv->session = NULL;
  492. MHD_set_connection_option (s->server_recv->mhd_conn,
  493. MHD_CONNECTION_OPTION_TIMEOUT,
  494. 1 /* 0 = no timeout, so this is MIN */);
  495. server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_YES);
  496. }
  497. notify_session_monitor (plugin,
  498. s,
  499. GNUNET_TRANSPORT_SS_DONE);
  500. if (GNUNET_YES == s->known_to_service)
  501. {
  502. plugin->env->session_end (plugin->env->cls,
  503. s->address,
  504. s);
  505. s->known_to_service = GNUNET_NO;
  506. }
  507. if (NULL != s->msg_tk)
  508. {
  509. GNUNET_SERVER_mst_destroy (s->msg_tk);
  510. s->msg_tk = NULL;
  511. }
  512. GNUNET_HELLO_address_free (s->address);
  513. LOG (GNUNET_ERROR_TYPE_DEBUG,
  514. "Session %p destroyed\n",
  515. s);
  516. GNUNET_free (s);
  517. }
  518. /**
  519. * Disconnect session @a s by telling MHD to close the
  520. * connections (reducing timeout, etc.).
  521. *
  522. * @param cls closure with the `struct HTTP_Server_Plugin`
  523. * @param s the session
  524. * @return #GNUNET_OK on success
  525. */
  526. static int
  527. http_server_plugin_disconnect_session (void *cls,
  528. struct Session *s)
  529. {
  530. server_delete_session (s);
  531. return GNUNET_OK;
  532. }
  533. /**
  534. * Session was idle, so disconnect it
  535. *
  536. * @param cls the session
  537. * @param tc task context
  538. */
  539. static void
  540. server_session_timeout (void *cls,
  541. const struct GNUNET_SCHEDULER_TaskContext *tc)
  542. {
  543. struct Session *s = cls;
  544. struct GNUNET_TIME_Relative left;
  545. s->timeout_task = NULL;
  546. left = GNUNET_TIME_absolute_get_remaining (s->timeout);
  547. if (0 != left.rel_value_us)
  548. {
  549. /* not actually our turn yet, but let's at least update
  550. the monitor, it may think we're about to die ... */
  551. notify_session_monitor (s->plugin,
  552. s,
  553. GNUNET_TRANSPORT_SS_UP);
  554. s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
  555. &server_session_timeout,
  556. s);
  557. return;
  558. }
  559. GNUNET_log (TIMEOUT_LOG,
  560. "Session %p was idle for %s, disconnecting\n",
  561. s,
  562. GNUNET_STRINGS_relative_time_to_string (HTTP_SERVER_SESSION_TIMEOUT,
  563. GNUNET_YES));
  564. server_delete_session (s);
  565. }
  566. /**
  567. * Increment session timeout due to activity session @a s
  568. *
  569. * @param s the session
  570. */
  571. static void
  572. server_reschedule_session_timeout (struct Session *s)
  573. {
  574. GNUNET_assert (NULL != s->timeout_task);
  575. s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
  576. }
  577. /**
  578. * Function that can be used by the transport service to transmit
  579. * a message using the plugin. Note that in the case of a
  580. * peer disconnecting, the continuation MUST be called
  581. * prior to the disconnect notification itself. This function
  582. * will be called with this peer's HELLO message to initiate
  583. * a fresh connection to another peer.
  584. *
  585. * @param cls closure
  586. * @param session which session must be used
  587. * @param msgbuf the message to transmit
  588. * @param msgbuf_size number of bytes in @a msgbuf
  589. * @param priority how important is the message (most plugins will
  590. * ignore message priority and just FIFO)
  591. * @param to how long to wait at most for the transmission (does not
  592. * require plugins to discard the message after the timeout,
  593. * just advisory for the desired delay; most plugins will ignore
  594. * this as well)
  595. * @param cont continuation to call once the message has
  596. * been transmitted (or if the transport is ready
  597. * for the next transmission call; or if the
  598. * peer disconnected...); can be NULL
  599. * @param cont_cls closure for @a cont
  600. * @return number of bytes used (on the physical network, with overheads);
  601. * -1 on hard errors (i.e. address invalid); 0 is a legal value
  602. * and does NOT mean that the message was not transmitted (DV)
  603. */
  604. static ssize_t
  605. http_server_plugin_send (void *cls,
  606. struct Session *session,
  607. const char *msgbuf,
  608. size_t msgbuf_size,
  609. unsigned int priority,
  610. struct GNUNET_TIME_Relative to,
  611. GNUNET_TRANSPORT_TransmitContinuation cont,
  612. void *cont_cls)
  613. {
  614. struct HTTP_Server_Plugin *plugin = cls;
  615. struct HTTP_Message *msg;
  616. ssize_t bytes_sent = 0;
  617. char *stat_txt;
  618. LOG (GNUNET_ERROR_TYPE_DEBUG,
  619. "Session %p/request %p: Sending message with %u to peer `%s'\n",
  620. session,
  621. session->server_send,
  622. msgbuf_size,
  623. GNUNET_i2s (&session->target));
  624. /* create new message and schedule */
  625. bytes_sent = sizeof (struct HTTP_Message) + msgbuf_size;
  626. msg = GNUNET_malloc (bytes_sent);
  627. msg->next = NULL;
  628. msg->size = msgbuf_size;
  629. msg->pos = 0;
  630. msg->buf = (char *) &msg[1];
  631. msg->transmit_cont = cont;
  632. msg->transmit_cont_cls = cont_cls;
  633. memcpy (msg->buf,
  634. msgbuf,
  635. msgbuf_size);
  636. GNUNET_CONTAINER_DLL_insert_tail (session->msg_head,
  637. session->msg_tail,
  638. msg);
  639. session->msgs_in_queue++;
  640. session->bytes_in_queue += msg->size;
  641. notify_session_monitor (plugin,
  642. session,
  643. GNUNET_TRANSPORT_SS_UP);
  644. GNUNET_asprintf (&stat_txt,
  645. "# bytes currently in %s_server buffers",
  646. plugin->protocol);
  647. GNUNET_STATISTICS_update (plugin->env->stats,
  648. stat_txt, msgbuf_size, GNUNET_NO);
  649. GNUNET_free (stat_txt);
  650. if (NULL != session->server_send)
  651. server_reschedule (session->plugin,
  652. session->server_send->mhd_daemon,
  653. GNUNET_YES);
  654. return bytes_sent;
  655. }
  656. /**
  657. * Terminate session during shutdown.
  658. *
  659. * @param cls the `struct HTTP_Server_Plugin *`
  660. * @param peer for which this is a session
  661. * @param value the `struct Session` to clean up
  662. * @return #GNUNET_OK (continue to iterate)
  663. */
  664. static int
  665. destroy_session_shutdown_cb (void *cls,
  666. const struct GNUNET_PeerIdentity *peer,
  667. void *value)
  668. {
  669. struct Session *s = value;
  670. struct ServerRequest *sc_send;
  671. struct ServerRequest *sc_recv;
  672. sc_send = s->server_send;
  673. sc_recv = s->server_recv;
  674. server_delete_session (s);
  675. GNUNET_free_non_null (sc_send);
  676. GNUNET_free_non_null (sc_recv);
  677. return GNUNET_OK;
  678. }
  679. /**
  680. * Terminate session.
  681. *
  682. * @param cls the `struct HTTP_Server_Plugin *`
  683. * @param peer for which this is a session
  684. * @param value the `struct Session` to clean up
  685. * @return #GNUNET_OK (continue to iterate)
  686. */
  687. static int
  688. destroy_session_cb (void *cls,
  689. const struct GNUNET_PeerIdentity *peer,
  690. void *value)
  691. {
  692. struct Session *s = value;
  693. server_delete_session (s);
  694. return GNUNET_OK;
  695. }
  696. /**
  697. * Function that can be used to force the plugin to disconnect
  698. * from the given peer and cancel all previous transmissions
  699. * (and their continuationc).
  700. *
  701. * @param cls closure
  702. * @param target peer from which to disconnect
  703. */
  704. static void
  705. http_server_plugin_disconnect_peer (void *cls,
  706. const struct GNUNET_PeerIdentity *target)
  707. {
  708. struct HTTP_Server_Plugin *plugin = cls;
  709. LOG (GNUNET_ERROR_TYPE_DEBUG,
  710. "Transport tells me to disconnect `%s'\n",
  711. GNUNET_i2s (target));
  712. GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
  713. target,
  714. &destroy_session_cb,
  715. plugin);
  716. }
  717. /**
  718. * Another peer has suggested an address for this
  719. * peer and transport plugin. Check that this could be a valid
  720. * address. If so, consider adding it to the list
  721. * of addresses.
  722. *
  723. * @param cls closure
  724. * @param addr pointer to the address
  725. * @param addrlen length of @a addr
  726. * @return #GNUNET_OK if this is a plausible address for this peer
  727. * and transport
  728. */
  729. static int
  730. http_server_plugin_address_suggested (void *cls,
  731. const void *addr,
  732. size_t addrlen)
  733. {
  734. struct HTTP_Server_Plugin *plugin = cls;
  735. struct HttpAddressWrapper *next;
  736. struct HttpAddressWrapper *pos;
  737. const struct HttpAddress *haddr = addr;
  738. if ((NULL != plugin->ext_addr) &&
  739. GNUNET_YES == (http_common_cmp_addresses (addr, addrlen,
  740. plugin->ext_addr->address,
  741. plugin->ext_addr->address_length)))
  742. {
  743. /* Checking HTTP_OPTIONS_VERIFY_CERTIFICATE option for external hostname */
  744. if ((ntohl (haddr->options) & HTTP_OPTIONS_VERIFY_CERTIFICATE) !=
  745. (plugin->options & HTTP_OPTIONS_VERIFY_CERTIFICATE))
  746. return GNUNET_NO; /* VERIFY option not set as required! */
  747. return GNUNET_OK;
  748. }
  749. next = plugin->addr_head;
  750. while (NULL != (pos = next))
  751. {
  752. next = pos->next;
  753. if (GNUNET_YES == (http_common_cmp_addresses(addr,
  754. addrlen,
  755. pos->address,
  756. pos->addrlen)))
  757. return GNUNET_OK;
  758. }
  759. return GNUNET_NO;
  760. }
  761. /**
  762. * Creates a new outbound session the transport
  763. * service will use to send data to the peer.
  764. *
  765. * Since HTTP/S server cannot create sessions, always returns NULL.
  766. *
  767. * @param cls the plugin
  768. * @param address the address
  769. * @return always NULL
  770. */
  771. static struct Session *
  772. http_server_plugin_get_session (void *cls,
  773. const struct GNUNET_HELLO_Address *address)
  774. {
  775. return NULL;
  776. }
  777. /**
  778. * Call MHD IPv4 to process pending requests and then go back
  779. * and schedule the next run.
  780. *
  781. * @param cls plugin as closure
  782. * @param tc task context
  783. */
  784. static void
  785. server_v4_run (void *cls,
  786. const struct GNUNET_SCHEDULER_TaskContext *tc)
  787. {
  788. struct HTTP_Server_Plugin *plugin = cls;
  789. plugin->server_v4_task = NULL;
  790. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
  791. return;
  792. plugin->server_v4_immediately = GNUNET_NO;
  793. GNUNET_assert (MHD_YES == MHD_run (plugin->server_v4));
  794. server_reschedule (plugin, plugin->server_v4, GNUNET_NO);
  795. }
  796. /**
  797. * Call MHD IPv6 to process pending requests and then go back
  798. * and schedule the next run.
  799. *
  800. * @param cls plugin as closure
  801. * @param tc task context
  802. */
  803. static void
  804. server_v6_run (void *cls,
  805. const struct GNUNET_SCHEDULER_TaskContext *tc)
  806. {
  807. struct HTTP_Server_Plugin *plugin = cls;
  808. plugin->server_v6_task = NULL;
  809. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
  810. return;
  811. plugin->server_v6_immediately = GNUNET_NO;
  812. GNUNET_assert (MHD_YES == MHD_run (plugin->server_v6));
  813. server_reschedule (plugin, plugin->server_v6, GNUNET_NO);
  814. }
  815. /**
  816. * Function that queries MHD's select sets and
  817. * starts the task waiting for them.
  818. *
  819. * @param plugin plugin
  820. * @param daemon_handle the MHD daemon handle
  821. * @param now schedule now
  822. * @return gnunet task identifier
  823. */
  824. static struct GNUNET_SCHEDULER_Task *
  825. server_schedule (struct HTTP_Server_Plugin *plugin,
  826. struct MHD_Daemon *daemon_handle,
  827. int now)
  828. {
  829. struct GNUNET_SCHEDULER_Task * ret;
  830. fd_set rs;
  831. fd_set ws;
  832. fd_set es;
  833. struct GNUNET_NETWORK_FDSet *wrs;
  834. struct GNUNET_NETWORK_FDSet *wws;
  835. int max;
  836. MHD_UNSIGNED_LONG_LONG timeout;
  837. static unsigned long long last_timeout = 0;
  838. int haveto;
  839. struct GNUNET_TIME_Relative tv;
  840. if (GNUNET_YES == plugin->in_shutdown)
  841. return NULL;
  842. ret = NULL;
  843. FD_ZERO (&rs);
  844. FD_ZERO (&ws);
  845. FD_ZERO (&es);
  846. wrs = GNUNET_NETWORK_fdset_create ();
  847. wws = GNUNET_NETWORK_fdset_create ();
  848. max = -1;
  849. GNUNET_assert (MHD_YES ==
  850. MHD_get_fdset (daemon_handle,
  851. &rs,
  852. &ws,
  853. &es,
  854. &max));
  855. haveto = MHD_get_timeout (daemon_handle, &timeout);
  856. if (haveto == MHD_YES)
  857. {
  858. if (timeout != last_timeout)
  859. {
  860. LOG (GNUNET_ERROR_TYPE_DEBUG,
  861. "SELECT Timeout changed from %llu to %llu (ms)\n",
  862. last_timeout, timeout);
  863. last_timeout = timeout;
  864. }
  865. if (timeout <= GNUNET_TIME_UNIT_SECONDS.rel_value_us / 1000LL)
  866. tv.rel_value_us = (uint64_t) timeout * 1000LL;
  867. else
  868. tv = GNUNET_TIME_UNIT_SECONDS;
  869. }
  870. else
  871. tv = GNUNET_TIME_UNIT_SECONDS;
  872. /* Force immediate run, since we have outbound data to send */
  873. if (now == GNUNET_YES)
  874. tv = GNUNET_TIME_UNIT_MILLISECONDS;
  875. GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
  876. GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
  877. if (daemon_handle == plugin->server_v4)
  878. {
  879. if (plugin->server_v4_task != NULL)
  880. {
  881. GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
  882. plugin->server_v4_task = NULL;
  883. }
  884. #if 0
  885. LOG (GNUNET_ERROR_TYPE_DEBUG,
  886. "Scheduling IPv4 server task in %llu ms\n",
  887. tv);
  888. #endif
  889. ret =
  890. GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
  891. tv, wrs, wws,
  892. &server_v4_run, plugin);
  893. }
  894. if (daemon_handle == plugin->server_v6)
  895. {
  896. if (plugin->server_v6_task != NULL)
  897. {
  898. GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
  899. plugin->server_v6_task = NULL;
  900. }
  901. #if 0
  902. LOG (GNUNET_ERROR_TYPE_DEBUG,
  903. "Scheduling IPv6 server task in %llu ms\n", tv);
  904. #endif
  905. ret =
  906. GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
  907. tv, wrs, wws,
  908. &server_v6_run, plugin);
  909. }
  910. GNUNET_NETWORK_fdset_destroy (wrs);
  911. GNUNET_NETWORK_fdset_destroy (wws);
  912. return ret;
  913. }
  914. /**
  915. * Reschedule the execution of both IPv4 and IPv6 server
  916. *
  917. * @param plugin the plugin
  918. * @param server which server to schedule v4 or v6?
  919. * @param now #GNUNET_YES to schedule execution immediately, #GNUNET_NO to wait
  920. * until timeout
  921. */
  922. static void
  923. server_reschedule (struct HTTP_Server_Plugin *plugin,
  924. struct MHD_Daemon *server,
  925. int now)
  926. {
  927. if ((server == plugin->server_v4) && (plugin->server_v4 != NULL))
  928. {
  929. if (GNUNET_YES == plugin->server_v4_immediately)
  930. return; /* No rescheduling, server will run asap */
  931. if (GNUNET_YES == now)
  932. plugin->server_v4_immediately = GNUNET_YES;
  933. if (plugin->server_v4_task != NULL)
  934. {
  935. GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
  936. plugin->server_v4_task = NULL;
  937. }
  938. plugin->server_v4_task = server_schedule (plugin, plugin->server_v4, now);
  939. }
  940. if ((server == plugin->server_v6) && (plugin->server_v6 != NULL))
  941. {
  942. if (GNUNET_YES == plugin->server_v6_immediately)
  943. return; /* No rescheduling, server will run asap */
  944. if (GNUNET_YES == now)
  945. plugin->server_v6_immediately = GNUNET_YES;
  946. if (plugin->server_v6_task != NULL)
  947. {
  948. GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
  949. plugin->server_v6_task = NULL;
  950. }
  951. plugin->server_v6_task = server_schedule (plugin, plugin->server_v6, now);
  952. }
  953. }
  954. /**
  955. * Function that is called to get the keepalive factor.
  956. * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
  957. * calculate the interval between keepalive packets.
  958. *
  959. * @param cls closure with the `struct HTTP_Server_Plugin`
  960. * @return keepalive factor
  961. */
  962. static unsigned int
  963. http_server_query_keepalive_factor (void *cls)
  964. {
  965. return 3;
  966. }
  967. /**
  968. * Function that will be called whenever the transport service wants to
  969. * notify the plugin that a session is still active and in use and
  970. * therefore the session timeout for this session has to be updated
  971. *
  972. * @param cls closure
  973. * @param peer which peer was the session for
  974. * @param session which session is being updated
  975. */
  976. static void
  977. http_server_plugin_update_session_timeout (void *cls,
  978. const struct GNUNET_PeerIdentity *peer,
  979. struct Session *session)
  980. {
  981. server_reschedule_session_timeout (session);
  982. }
  983. /**
  984. * Tell MHD that the connection should timeout after @a to seconds.
  985. *
  986. * @param plugin our plugin
  987. * @param s session for which the timeout changes
  988. * @param to timeout in seconds
  989. */
  990. static void
  991. server_mhd_connection_timeout (struct HTTP_Server_Plugin *plugin,
  992. struct Session *s,
  993. unsigned int to)
  994. {
  995. /* Setting timeouts for other connections */
  996. if (NULL != s->server_recv)
  997. {
  998. LOG (GNUNET_ERROR_TYPE_DEBUG,
  999. "Setting timeout for %p to %u sec.\n",
  1000. s->server_recv, to);
  1001. MHD_set_connection_option (s->server_recv->mhd_conn,
  1002. MHD_CONNECTION_OPTION_TIMEOUT,
  1003. to);
  1004. server_reschedule (plugin, s->server_recv->mhd_daemon, GNUNET_NO);
  1005. }
  1006. if (NULL != s->server_send)
  1007. {
  1008. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1009. "Setting timeout for %p to %u sec.\n",
  1010. s->server_send, to);
  1011. MHD_set_connection_option (s->server_send->mhd_conn,
  1012. MHD_CONNECTION_OPTION_TIMEOUT,
  1013. to);
  1014. server_reschedule (plugin, s->server_send->mhd_daemon, GNUNET_NO);
  1015. }
  1016. }
  1017. /**
  1018. * Parse incoming URL for tag and target
  1019. *
  1020. * @param plugin plugin
  1021. * @param url incoming url
  1022. * @param target where to store the target
  1023. * @param tag where to store the tag
  1024. * @param options where to store the options
  1025. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  1026. */
  1027. static int
  1028. server_parse_url (struct HTTP_Server_Plugin *plugin,
  1029. const char *url,
  1030. struct GNUNET_PeerIdentity *target,
  1031. uint32_t *tag,
  1032. uint32_t *options)
  1033. {
  1034. regmatch_t matches[4];
  1035. const char *tag_start;
  1036. const char *target_start;
  1037. char *tag_end;
  1038. char *options_end;
  1039. size_t hash_length;
  1040. unsigned long int rc;
  1041. /* URL parsing */
  1042. #define URL_REGEX \
  1043. ("^.*/([0-9A-Z]+);([0-9]+)(,[0-9]+)?$")
  1044. if (NULL == url)
  1045. {
  1046. GNUNET_break (0);
  1047. return GNUNET_SYSERR;
  1048. }
  1049. if (regexec(&plugin->url_regex, url, 4, matches, 0))
  1050. {
  1051. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1052. "URL `%s' did not match regex\n", url);
  1053. return GNUNET_SYSERR;
  1054. }
  1055. target_start = &url[matches[1].rm_so];
  1056. tag_start = &url[matches[2].rm_so];
  1057. /* convert tag */
  1058. rc = strtoul (tag_start, &tag_end, 10);
  1059. if (&url[matches[2].rm_eo] != tag_end)
  1060. {
  1061. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1062. "URL tag did not line up with submatch\n");
  1063. return GNUNET_SYSERR;
  1064. }
  1065. if (rc == 0)
  1066. {
  1067. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1068. "URL tag is zero\n");
  1069. return GNUNET_SYSERR;
  1070. }
  1071. if ((rc == ULONG_MAX) && (ERANGE == errno))
  1072. {
  1073. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1074. "URL tag > ULONG_MAX\n");
  1075. return GNUNET_SYSERR;
  1076. }
  1077. if (rc > UINT32_MAX)
  1078. {
  1079. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1080. "URL tag > UINT32_MAX\n");
  1081. return GNUNET_SYSERR;
  1082. }
  1083. (*tag) = (uint32_t)rc;
  1084. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1085. "Found tag `%u' in url\n",
  1086. *tag);
  1087. /* convert peer id */
  1088. hash_length = matches[1].rm_eo - matches[1].rm_so;
  1089. if (hash_length != plugin->peer_id_length)
  1090. {
  1091. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1092. "URL target is %u bytes, expecting %u\n",
  1093. hash_length, plugin->peer_id_length);
  1094. return GNUNET_SYSERR;
  1095. }
  1096. if (GNUNET_OK !=
  1097. GNUNET_CRYPTO_eddsa_public_key_from_string (target_start,
  1098. hash_length,
  1099. &target->public_key))
  1100. {
  1101. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1102. "URL target conversion failed\n");
  1103. return GNUNET_SYSERR;
  1104. }
  1105. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1106. "Found target `%s' in URL\n",
  1107. GNUNET_i2s_full (target));
  1108. /* convert options */
  1109. if (-1 == matches[3].rm_so)
  1110. {
  1111. *options = 0;
  1112. }
  1113. else
  1114. {
  1115. rc = strtoul (&url[matches[3].rm_so + 1], &options_end, 10);
  1116. if (&url[matches[3].rm_eo] != options_end)
  1117. {
  1118. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1119. "URL options did not line up with submatch\n");
  1120. return GNUNET_SYSERR;
  1121. }
  1122. if ((rc == ULONG_MAX) && (ERANGE == errno))
  1123. {
  1124. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1125. "URL options > ULONG_MAX\n");
  1126. return GNUNET_SYSERR;
  1127. }
  1128. if (rc > UINT32_MAX)
  1129. {
  1130. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1131. "URL options > UINT32_MAX\n");
  1132. return GNUNET_SYSERR;
  1133. }
  1134. (*options) = (uint32_t) rc;
  1135. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1136. "Found options `%u' in url\n",
  1137. *options);
  1138. }
  1139. return GNUNET_OK;
  1140. }
  1141. /**
  1142. * Closure for #session_tag_it().
  1143. */
  1144. struct SessionTagContext
  1145. {
  1146. /**
  1147. * Set to session matching the tag.
  1148. */
  1149. struct Session *res;
  1150. /**
  1151. * Tag we are looking for.
  1152. */
  1153. uint32_t tag;
  1154. };
  1155. /**
  1156. * Find a session with a matching tag.
  1157. *
  1158. * @param cls the `struct SessionTagContext *`
  1159. * @param key peer identity (unused)
  1160. * @param value the `struct Session *`
  1161. * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
  1162. */
  1163. static int
  1164. session_tag_it (void *cls,
  1165. const struct GNUNET_PeerIdentity *key,
  1166. void *value)
  1167. {
  1168. struct SessionTagContext *stc = cls;
  1169. struct Session *s = value;
  1170. if (s->tag == stc->tag)
  1171. {
  1172. stc->res = s;
  1173. return GNUNET_NO;
  1174. }
  1175. return GNUNET_YES;
  1176. }
  1177. /**
  1178. * Lookup a mhd connection and create one if none is found
  1179. *
  1180. * @param plugin the plugin handle
  1181. * @param mhd_connection the incoming mhd_connection
  1182. * @param url incoming requested URL
  1183. * @param method PUT or GET
  1184. * @return the server connecetion
  1185. */
  1186. static struct ServerRequest *
  1187. server_lookup_connection (struct HTTP_Server_Plugin *plugin,
  1188. struct MHD_Connection *mhd_connection,
  1189. const char *url,
  1190. const char *method)
  1191. {
  1192. struct Session *s = NULL;
  1193. struct ServerRequest *sc = NULL;
  1194. const union MHD_ConnectionInfo *conn_info;
  1195. struct HttpAddress *addr;
  1196. struct GNUNET_PeerIdentity target;
  1197. size_t addr_len;
  1198. struct SessionTagContext stc;
  1199. uint32_t options;
  1200. int direction = GNUNET_SYSERR;
  1201. unsigned int to;
  1202. enum GNUNET_ATS_Network_Type scope;
  1203. conn_info = MHD_get_connection_info (mhd_connection,
  1204. MHD_CONNECTION_INFO_CLIENT_ADDRESS);
  1205. if ((conn_info->client_addr->sa_family != AF_INET) &&
  1206. (conn_info->client_addr->sa_family != AF_INET6))
  1207. return NULL;
  1208. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1209. "New %s request from %s\n",
  1210. method,
  1211. url);
  1212. stc.tag = 0;
  1213. if (GNUNET_SYSERR ==
  1214. server_parse_url (plugin, url, &target, &stc.tag, &options))
  1215. {
  1216. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1217. "Invalid url %s\n", url);
  1218. return NULL;
  1219. }
  1220. if (0 == strcmp (MHD_HTTP_METHOD_PUT, method))
  1221. direction = _RECEIVE;
  1222. else if (0 == strcmp (MHD_HTTP_METHOD_GET, method))
  1223. direction = _SEND;
  1224. else
  1225. {
  1226. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1227. "Invalid method %s for request from %s\n",
  1228. method, url);
  1229. return NULL;
  1230. }
  1231. plugin->cur_request++;
  1232. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1233. "New %s request from %s with tag %u (%u of %u)\n",
  1234. method,
  1235. GNUNET_i2s (&target),
  1236. stc.tag,
  1237. plugin->cur_request, plugin->max_request);
  1238. /* find existing session */
  1239. stc.res = NULL;
  1240. GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
  1241. &target,
  1242. &session_tag_it,
  1243. &stc);
  1244. if (NULL == (s = stc.res))
  1245. {
  1246. /* create new session */
  1247. addr = NULL;
  1248. switch (conn_info->client_addr->sa_family)
  1249. {
  1250. case (AF_INET):
  1251. addr = http_common_address_from_socket (plugin->protocol,
  1252. conn_info->client_addr,
  1253. sizeof (struct sockaddr_in));
  1254. addr_len = http_common_address_get_size (addr);
  1255. scope = plugin->env->get_address_type (plugin->env->cls,
  1256. conn_info->client_addr,
  1257. sizeof (struct sockaddr_in));
  1258. break;
  1259. case (AF_INET6):
  1260. addr = http_common_address_from_socket (plugin->protocol,
  1261. conn_info->client_addr,
  1262. sizeof (struct sockaddr_in6));
  1263. addr_len = http_common_address_get_size (addr);
  1264. scope = plugin->env->get_address_type (plugin->env->cls,
  1265. conn_info->client_addr,
  1266. sizeof (struct sockaddr_in6));
  1267. break;
  1268. default:
  1269. /* external host name */
  1270. return NULL;
  1271. }
  1272. s = GNUNET_new (struct Session);
  1273. s->target = target;
  1274. s->plugin = plugin;
  1275. s->scope = scope;
  1276. s->address = GNUNET_HELLO_address_allocate (&s->target,
  1277. PLUGIN_NAME,
  1278. addr,
  1279. addr_len,
  1280. GNUNET_HELLO_ADDRESS_INFO_INBOUND);
  1281. s->next_receive = GNUNET_TIME_UNIT_ZERO_ABS;
  1282. s->tag = stc.tag;
  1283. s->timeout = GNUNET_TIME_relative_to_absolute (HTTP_SERVER_SESSION_TIMEOUT);
  1284. s->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_SERVER_SESSION_TIMEOUT,
  1285. &server_session_timeout,
  1286. s);
  1287. (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
  1288. &s->target,
  1289. s,
  1290. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  1291. notify_session_monitor (plugin,
  1292. s,
  1293. GNUNET_TRANSPORT_SS_INIT);
  1294. notify_session_monitor (plugin,
  1295. s,
  1296. GNUNET_TRANSPORT_SS_HANDSHAKE);
  1297. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1298. "Creating new session %p for peer `%s' connecting from `%s'\n",
  1299. s, GNUNET_i2s (&target),
  1300. http_common_plugin_address_to_string (plugin->protocol,
  1301. addr,
  1302. addr_len));
  1303. GNUNET_free_non_null (addr);
  1304. }
  1305. if ( (_RECEIVE == direction) &&
  1306. (NULL != s->server_recv) )
  1307. {
  1308. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1309. "Duplicate PUT request from `%s' tag %u, dismissing new request\n",
  1310. GNUNET_i2s (&target),
  1311. stc.tag);
  1312. return NULL;
  1313. }
  1314. if ((_SEND == direction) && (NULL != s->server_send))
  1315. {
  1316. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1317. "Duplicate GET request from `%s' tag %u, dismissing new request\n",
  1318. GNUNET_i2s (&target),
  1319. stc.tag);
  1320. return NULL;
  1321. }
  1322. sc = GNUNET_new (struct ServerRequest);
  1323. if (conn_info->client_addr->sa_family == AF_INET)
  1324. sc->mhd_daemon = plugin->server_v4;
  1325. if (conn_info->client_addr->sa_family == AF_INET6)
  1326. sc->mhd_daemon = plugin->server_v6;
  1327. sc->mhd_conn = mhd_connection;
  1328. sc->direction = direction;
  1329. sc->connected = GNUNET_NO;
  1330. sc->session = s;
  1331. sc->options = options;
  1332. if (direction == _SEND)
  1333. {
  1334. s->server_send = sc;
  1335. }
  1336. if (direction == _RECEIVE)
  1337. {
  1338. s->server_recv = sc;
  1339. }
  1340. if ((GNUNET_NO == s->known_to_service) &&
  1341. (NULL != s->server_send) &&
  1342. (NULL != s->server_recv) )
  1343. {
  1344. s->known_to_service = GNUNET_YES;
  1345. notify_session_monitor (plugin,
  1346. s,
  1347. GNUNET_TRANSPORT_SS_UP);
  1348. plugin->env->session_start (plugin->env->cls,
  1349. s->address,
  1350. s,
  1351. s->scope);
  1352. }
  1353. if ( (NULL == s->server_recv) ||
  1354. (NULL == s->server_send) )
  1355. {
  1356. to = (HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL);
  1357. MHD_set_connection_option (mhd_connection,
  1358. MHD_CONNECTION_OPTION_TIMEOUT, to);
  1359. server_reschedule (plugin, sc->mhd_daemon, GNUNET_NO);
  1360. }
  1361. else
  1362. {
  1363. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1364. "Session %p for peer `%s' fully connected\n",
  1365. s, GNUNET_i2s (&target));
  1366. to = (HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL);
  1367. server_mhd_connection_timeout (plugin, s, to);
  1368. }
  1369. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1370. "Setting timeout for %p to %u sec.\n", sc, to);
  1371. return sc;
  1372. }
  1373. /**
  1374. * Callback called by MHD when it needs data to send
  1375. *
  1376. * @param cls current session
  1377. * @param pos position in buffer
  1378. * @param buf the buffer to write data to
  1379. * @param max max number of bytes available in @a buf
  1380. * @return bytes written to @a buf
  1381. */
  1382. static ssize_t
  1383. server_send_callback (void *cls,
  1384. uint64_t pos,
  1385. char *buf,
  1386. size_t max)
  1387. {
  1388. struct ServerRequest *sc = cls;
  1389. struct Session *s = sc->session;
  1390. ssize_t bytes_read = 0;
  1391. struct HTTP_Message *msg;
  1392. char *stat_txt;
  1393. if (NULL == s)
  1394. {
  1395. /* session is disconnecting */
  1396. return 0;
  1397. }
  1398. sc = s->server_send;
  1399. if (NULL == sc)
  1400. return 0;
  1401. msg = s->msg_head;
  1402. if (NULL != msg)
  1403. {
  1404. /* sending */
  1405. bytes_read = GNUNET_MIN (msg->size - msg->pos,
  1406. max);
  1407. memcpy (buf, &msg->buf[msg->pos], bytes_read);
  1408. msg->pos += bytes_read;
  1409. /* removing message */
  1410. if (msg->pos == msg->size)
  1411. {
  1412. GNUNET_CONTAINER_DLL_remove (s->msg_head,
  1413. s->msg_tail,
  1414. msg);
  1415. if (NULL != msg->transmit_cont)
  1416. msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_OK,
  1417. msg->size, msg->size + msg->overhead);
  1418. GNUNET_assert (s->msgs_in_queue > 0);
  1419. s->msgs_in_queue--;
  1420. GNUNET_assert (s->bytes_in_queue >= msg->size);
  1421. s->bytes_in_queue -= msg->size;
  1422. GNUNET_free (msg);
  1423. notify_session_monitor (s->plugin,
  1424. s,
  1425. GNUNET_TRANSPORT_SS_UPDATE);
  1426. }
  1427. }
  1428. if (0 < bytes_read)
  1429. {
  1430. sc->connected = GNUNET_YES;
  1431. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1432. "Sent %u bytes to peer `%s' with session %p \n",
  1433. bytes_read,
  1434. GNUNET_i2s (&s->target),
  1435. s);
  1436. GNUNET_asprintf (&stat_txt,
  1437. "# bytes currently in %s_server buffers",
  1438. s->plugin->protocol);
  1439. GNUNET_STATISTICS_update (s->plugin->env->stats,
  1440. stat_txt,
  1441. - bytes_read,
  1442. GNUNET_NO);
  1443. GNUNET_free (stat_txt);
  1444. GNUNET_asprintf (&stat_txt,
  1445. "# bytes transmitted via %s_server",
  1446. s->plugin->protocol);
  1447. GNUNET_STATISTICS_update (s->plugin->env->stats,
  1448. stat_txt, bytes_read, GNUNET_NO);
  1449. GNUNET_free (stat_txt);
  1450. }
  1451. else if ((sc->options & OPTION_LONG_POLL) && sc->connected)
  1452. {
  1453. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1454. "Completing GET response to peer `%s' with session %p\n",
  1455. GNUNET_i2s (&s->target),
  1456. s);
  1457. return MHD_CONTENT_READER_END_OF_STREAM;
  1458. }
  1459. return bytes_read;
  1460. }
  1461. /**
  1462. * Callback called by MessageStreamTokenizer when a message has arrived
  1463. *
  1464. * @param cls current session as closure
  1465. * @param client client
  1466. * @param message the message to be forwarded to transport service
  1467. * @return #GNUNET_OK
  1468. */
  1469. static int
  1470. server_receive_mst_cb (void *cls,
  1471. void *client,
  1472. const struct GNUNET_MessageHeader *message)
  1473. {
  1474. struct Session *s = cls;
  1475. struct HTTP_Server_Plugin *plugin = s->plugin;
  1476. struct GNUNET_TIME_Relative delay;
  1477. char *stat_txt;
  1478. if (GNUNET_NO == s->known_to_service)
  1479. {
  1480. s->known_to_service = GNUNET_YES;
  1481. plugin->env->session_start (plugin->env->cls,
  1482. s->address,
  1483. s,
  1484. s->scope);
  1485. notify_session_monitor (plugin,
  1486. s,
  1487. GNUNET_TRANSPORT_SS_UP);
  1488. }
  1489. delay = plugin->env->receive (plugin->env->cls,
  1490. s->address,
  1491. s,
  1492. message);
  1493. GNUNET_asprintf (&stat_txt,
  1494. "# bytes received via %s_server",
  1495. plugin->protocol);
  1496. GNUNET_STATISTICS_update (plugin->env->stats,
  1497. stat_txt, ntohs (message->size), GNUNET_NO);
  1498. GNUNET_free (stat_txt);
  1499. s->next_receive = GNUNET_TIME_relative_to_absolute (delay);
  1500. if (delay.rel_value_us > 0)
  1501. {
  1502. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1503. "Peer `%s' address `%s' next read delayed for %s\n",
  1504. GNUNET_i2s (&s->target),
  1505. http_common_plugin_address_to_string (plugin->protocol,
  1506. s->address->address,
  1507. s->address->address_length),
  1508. GNUNET_STRINGS_relative_time_to_string (delay,
  1509. GNUNET_YES));
  1510. }
  1511. server_reschedule_session_timeout (s);
  1512. return GNUNET_OK;
  1513. }
  1514. /**
  1515. * Add headers to a request indicating that we allow Cross-Origin Resource
  1516. * Sharing.
  1517. *
  1518. * @param response response object to modify
  1519. */
  1520. static void
  1521. add_cors_headers(struct MHD_Response *response)
  1522. {
  1523. MHD_add_response_header (response,
  1524. "Access-Control-Allow-Origin",
  1525. "*");
  1526. MHD_add_response_header (response,
  1527. "Access-Control-Allow-Methods",
  1528. "GET, PUT, OPTIONS");
  1529. MHD_add_response_header (response,
  1530. "Access-Control-Max-Age",
  1531. "86400");
  1532. }
  1533. /**
  1534. * MHD callback for a new incoming connection
  1535. *
  1536. * @param cls the plugin handle
  1537. * @param mhd_connection the mhd connection
  1538. * @param url the requested URL
  1539. * @param method GET or PUT
  1540. * @param version HTTP version
  1541. * @param upload_data upload data
  1542. * @param upload_data_size size of @a upload_data
  1543. * @param httpSessionCache the session cache to remember the connection
  1544. * @return MHD_YES if connection is accepted, MHD_NO on reject
  1545. */
  1546. static int
  1547. server_access_cb (void *cls,
  1548. struct MHD_Connection *mhd_connection,
  1549. const char *url,
  1550. const char *method,
  1551. const char *version,
  1552. const char *upload_data,
  1553. size_t *upload_data_size,
  1554. void **httpSessionCache)
  1555. {
  1556. struct HTTP_Server_Plugin *plugin = cls;
  1557. struct ServerRequest *sc = *httpSessionCache;
  1558. struct Session *s;
  1559. struct MHD_Response *response;
  1560. int res = MHD_YES;
  1561. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1562. _("Access from connection %p (%u of %u) for `%s' `%s' url `%s' with upload data size %u\n"),
  1563. sc,
  1564. plugin->cur_request,
  1565. plugin->max_request,
  1566. method,
  1567. version,
  1568. url,
  1569. (*upload_data_size));
  1570. if (NULL == sc)
  1571. {
  1572. /* CORS pre-flight request */
  1573. if (0 == strcmp (MHD_HTTP_METHOD_OPTIONS, method))
  1574. {
  1575. response = MHD_create_response_from_buffer (0, NULL,
  1576. MHD_RESPMEM_PERSISTENT);
  1577. add_cors_headers(response);
  1578. res = MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
  1579. MHD_destroy_response (response);
  1580. return res;
  1581. }
  1582. /* new connection */
  1583. sc = server_lookup_connection (plugin, mhd_connection, url, method);
  1584. if (NULL != sc)
  1585. {
  1586. /* attach to new / existing session */
  1587. (*httpSessionCache) = sc;
  1588. }
  1589. else
  1590. {
  1591. /* existing session already has matching connection, refuse */
  1592. response = MHD_create_response_from_data (strlen (HTTP_ERROR_RESPONSE),
  1593. HTTP_ERROR_RESPONSE,
  1594. MHD_NO,
  1595. MHD_NO);
  1596. MHD_add_response_header (response,
  1597. MHD_HTTP_HEADER_CONTENT_TYPE,
  1598. "text/html");
  1599. add_cors_headers(response);
  1600. res = MHD_queue_response (mhd_connection, MHD_HTTP_NOT_FOUND, response);
  1601. MHD_destroy_response (response);
  1602. return res;
  1603. }
  1604. }
  1605. /* 'old' connection */
  1606. if (NULL == (s = sc->session))
  1607. {
  1608. /* Session was already disconnected;
  1609. sent HTTP/1.1: 200 OK as response */
  1610. response = MHD_create_response_from_data (strlen ("Thank you!"),
  1611. "Thank you!", MHD_NO, MHD_NO);
  1612. add_cors_headers(response);
  1613. MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
  1614. MHD_destroy_response (response);
  1615. return MHD_YES;
  1616. }
  1617. if (sc->direction == _SEND)
  1618. {
  1619. response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 32 * 1024,
  1620. &server_send_callback, sc, NULL);
  1621. add_cors_headers(response);
  1622. MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
  1623. MHD_destroy_response (response);
  1624. return MHD_YES;
  1625. }
  1626. if (sc->direction == _RECEIVE)
  1627. {
  1628. if ((*upload_data_size == 0) && (sc->connected == GNUNET_NO))
  1629. {
  1630. /* (*upload_data_size == 0) first callback when header are passed */
  1631. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1632. "Session %p / Connection %p: Peer `%s' PUT on address `%s' connected\n",
  1633. s, sc,
  1634. GNUNET_i2s (&s->target),
  1635. http_common_plugin_address_to_string (plugin->protocol,
  1636. s->address->address,
  1637. s->address->address_length));
  1638. sc->connected = GNUNET_YES;
  1639. return MHD_YES;
  1640. }
  1641. else if ((*upload_data_size == 0) && (sc->connected == GNUNET_YES))
  1642. {
  1643. /* (*upload_data_size == 0) when upload is complete */
  1644. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1645. "Session %p / Connection %p: Peer `%s' PUT on address `%s' finished upload\n",
  1646. s, sc,
  1647. GNUNET_i2s (&s->target),
  1648. http_common_plugin_address_to_string (plugin->protocol,
  1649. s->address->address,
  1650. s->address->address_length));
  1651. sc->connected = GNUNET_NO;
  1652. /* Sent HTTP/1.1: 200 OK as PUT Response\ */
  1653. response = MHD_create_response_from_data (strlen ("Thank you!"),
  1654. "Thank you!", MHD_NO, MHD_NO);
  1655. add_cors_headers(response);
  1656. MHD_queue_response (mhd_connection, MHD_HTTP_OK, response);
  1657. MHD_destroy_response (response);
  1658. return MHD_YES;
  1659. }
  1660. else if ((*upload_data_size > 0) && (sc->connected == GNUNET_YES))
  1661. {
  1662. struct GNUNET_TIME_Relative delay;
  1663. /* (*upload_data_size > 0) for every segment received */
  1664. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1665. "Session %p / Connection %p: Peer `%s' PUT on address `%s' received %u bytes\n",
  1666. s, sc,
  1667. GNUNET_i2s (&s->target),
  1668. http_common_plugin_address_to_string (plugin->protocol,
  1669. s->address->address,
  1670. s->address->address_length),
  1671. *upload_data_size);
  1672. delay = GNUNET_TIME_absolute_get_remaining (s->next_receive);
  1673. if (0 == delay.rel_value_us)
  1674. {
  1675. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1676. "PUT with %u bytes forwarded to MST\n",
  1677. *upload_data_size);
  1678. if (s->msg_tk == NULL)
  1679. {
  1680. s->msg_tk = GNUNET_SERVER_mst_create (&server_receive_mst_cb, s);
  1681. }
  1682. GNUNET_SERVER_mst_receive (s->msg_tk, s, upload_data, *upload_data_size,
  1683. GNUNET_NO, GNUNET_NO);
  1684. server_mhd_connection_timeout (plugin, s,
  1685. GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value_us / 1000LL
  1686. / 1000LL);
  1687. (*upload_data_size) = 0;
  1688. }
  1689. else
  1690. {
  1691. /* delay processing */
  1692. GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
  1693. "Session %p / Connection %p: no inbound bandwidth available! Next read was delayed by %s\n",
  1694. s, sc, GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
  1695. GNUNET_assert(s->server_recv->mhd_conn == mhd_connection);
  1696. MHD_suspend_connection (s->server_recv->mhd_conn);
  1697. if (NULL == s->recv_wakeup_task)
  1698. s->recv_wakeup_task = GNUNET_SCHEDULER_add_delayed (delay,
  1699. &server_wake_up, s);
  1700. }
  1701. return MHD_YES;
  1702. }
  1703. else
  1704. {
  1705. GNUNET_break (0);
  1706. return MHD_NO;
  1707. }
  1708. }
  1709. return res;
  1710. }
  1711. /**
  1712. * Callback from MHD when a connection disconnects
  1713. *
  1714. * @param cls closure with the `struct HTTP_Server_Plugin *`
  1715. * @param connection the disconnected MHD connection
  1716. * @param httpSessionCache the pointer to distinguish
  1717. */
  1718. static void
  1719. server_disconnect_cb (void *cls,
  1720. struct MHD_Connection *connection,
  1721. void **httpSessionCache)
  1722. {
  1723. struct HTTP_Server_Plugin *plugin = cls;
  1724. struct ServerRequest *sc = *httpSessionCache;
  1725. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1726. "Disconnect for connection %p\n",
  1727. sc);
  1728. if (NULL == sc)
  1729. {
  1730. /* CORS pre-flight request finished */
  1731. return;
  1732. }
  1733. if (NULL != sc->session)
  1734. {
  1735. if (sc->direction == _SEND)
  1736. {
  1737. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1738. "Peer `%s' connection %p, GET on address `%s' disconnected\n",
  1739. GNUNET_i2s (&sc->session->target),
  1740. sc->session->server_send,
  1741. http_common_plugin_address_to_string (plugin->protocol,
  1742. sc->session->address->address,
  1743. sc->session->address->address_length));
  1744. sc->session->server_send = NULL;
  1745. }
  1746. else if (sc->direction == _RECEIVE)
  1747. {
  1748. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1749. "Peer `%s' connection %p PUT on address `%s' disconnected\n",
  1750. GNUNET_i2s (&sc->session->target),
  1751. sc->session->server_recv,
  1752. http_common_plugin_address_to_string (plugin->protocol,
  1753. sc->session->address->address,
  1754. sc->session->address->address_length));
  1755. sc->session->server_recv = NULL;
  1756. if (NULL != sc->session->msg_tk)
  1757. {
  1758. GNUNET_SERVER_mst_destroy (sc->session->msg_tk);
  1759. sc->session->msg_tk = NULL;
  1760. }
  1761. }
  1762. }
  1763. GNUNET_free (sc);
  1764. plugin->cur_request--;
  1765. }
  1766. /**
  1767. * Check if incoming connection is accepted.
  1768. *
  1769. * @param cls plugin as closure
  1770. * @param addr address of incoming connection
  1771. * @param addr_len number of bytes in @a addr
  1772. * @return MHD_YES if connection is accepted, MHD_NO if connection is rejected
  1773. */
  1774. static int
  1775. server_accept_cb (void *cls,
  1776. const struct sockaddr *addr,
  1777. socklen_t addr_len)
  1778. {
  1779. struct HTTP_Server_Plugin *plugin = cls;
  1780. if (plugin->cur_request <= plugin->max_request)
  1781. {
  1782. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1783. _("Accepting connection (%u of %u) from `%s'\n"),
  1784. plugin->cur_request, plugin->max_request,
  1785. GNUNET_a2s (addr, addr_len));
  1786. return MHD_YES;
  1787. }
  1788. else
  1789. {
  1790. LOG (GNUNET_ERROR_TYPE_WARNING,
  1791. _("Server reached maximum number connections (%u), rejecting new connection\n"),
  1792. plugin->max_request);
  1793. return MHD_NO;
  1794. }
  1795. }
  1796. /**
  1797. * Log function called by MHD.
  1798. *
  1799. * @param arg NULL
  1800. * @param fmt format string
  1801. * @param ap arguments for the format string (va_start() and va_end()
  1802. * will be called by MHD)
  1803. */
  1804. static void
  1805. server_log (void *arg,
  1806. const char *fmt,
  1807. va_list ap)
  1808. {
  1809. char text[1024];
  1810. vsnprintf (text,
  1811. sizeof (text),
  1812. fmt,
  1813. ap);
  1814. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1815. "Server: %s\n",
  1816. text);
  1817. }
  1818. #if BUILD_HTTPS
  1819. /**
  1820. * Load ssl certificate from file
  1821. *
  1822. * @param file filename
  1823. * @return content of the file
  1824. */
  1825. static char *
  1826. server_load_file (const char *file)
  1827. {
  1828. struct GNUNET_DISK_FileHandle *gn_file;
  1829. uint64_t fsize;
  1830. char *text = NULL;
  1831. if (GNUNET_OK != GNUNET_DISK_file_size (file,
  1832. &fsize, GNUNET_NO, GNUNET_YES))
  1833. return NULL;
  1834. text = GNUNET_malloc (fsize + 1);
  1835. gn_file =
  1836. GNUNET_DISK_file_open (file, GNUNET_DISK_OPEN_READ,
  1837. GNUNET_DISK_PERM_USER_READ);
  1838. if (NULL == gn_file)
  1839. {
  1840. GNUNET_free (text);
  1841. return NULL;
  1842. }
  1843. if (GNUNET_SYSERR == GNUNET_DISK_file_read (gn_file, text, fsize))
  1844. {
  1845. GNUNET_free (text);
  1846. GNUNET_DISK_file_close (gn_file);
  1847. return NULL;
  1848. }
  1849. text[fsize] = '\0';
  1850. GNUNET_DISK_file_close (gn_file);
  1851. return text;
  1852. }
  1853. #endif
  1854. #if BUILD_HTTPS
  1855. /**
  1856. * Load ssl certificate
  1857. *
  1858. * @param plugin the plugin
  1859. * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  1860. */
  1861. static int
  1862. server_load_certificate (struct HTTP_Server_Plugin *plugin)
  1863. {
  1864. int res = GNUNET_OK;
  1865. char *key_file;
  1866. char *cert_file;
  1867. if (GNUNET_OK !=
  1868. GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
  1869. plugin->name,
  1870. "KEY_FILE", &key_file))
  1871. {
  1872. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  1873. plugin->name, "CERT_FILE");
  1874. return GNUNET_SYSERR;
  1875. }
  1876. if (GNUNET_OK !=
  1877. GNUNET_CONFIGURATION_get_value_filename (plugin->env->cfg,
  1878. plugin->name,
  1879. "CERT_FILE", &cert_file))
  1880. {
  1881. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  1882. plugin->name, "CERT_FILE");
  1883. GNUNET_free (key_file);
  1884. return GNUNET_SYSERR;
  1885. }
  1886. /* Get crypto init string from config. If not present, use
  1887. * default values */
  1888. if (GNUNET_OK ==
  1889. GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
  1890. plugin->name,
  1891. "CRYPTO_INIT",
  1892. &plugin->crypto_init))
  1893. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1894. "Using crypto init string `%s'\n",
  1895. plugin->crypto_init);
  1896. else
  1897. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1898. "Using default crypto init string \n");
  1899. /* read key & certificates from file */
  1900. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1901. "Trying to loading TLS certificate from key-file `%s' cert-file`%s'\n",
  1902. key_file, cert_file);
  1903. plugin->key = server_load_file (key_file);
  1904. plugin->cert = server_load_file (cert_file);
  1905. if ((plugin->key == NULL) || (plugin->cert == NULL))
  1906. {
  1907. struct GNUNET_OS_Process *cert_creation;
  1908. GNUNET_free_non_null (plugin->key);
  1909. plugin->key = NULL;
  1910. GNUNET_free_non_null (plugin->cert);
  1911. plugin->cert = NULL;
  1912. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1913. "No usable TLS certificate found, creating certificate\n");
  1914. errno = 0;
  1915. cert_creation =
  1916. GNUNET_OS_start_process (GNUNET_NO, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
  1917. NULL, NULL, NULL,
  1918. "gnunet-transport-certificate-creation",
  1919. "gnunet-transport-certificate-creation",
  1920. key_file,
  1921. cert_file,
  1922. NULL);
  1923. if (NULL == cert_creation)
  1924. {
  1925. LOG (GNUNET_ERROR_TYPE_ERROR,
  1926. _("Could not create a new TLS certificate, program `gnunet-transport-certificate-creation' could not be started!\n"));
  1927. GNUNET_free (key_file);
  1928. GNUNET_free (cert_file);
  1929. GNUNET_free_non_null (plugin->key);
  1930. plugin->key = NULL;
  1931. GNUNET_free_non_null (plugin->cert);
  1932. plugin->cert = NULL;
  1933. GNUNET_free_non_null (plugin->crypto_init);
  1934. plugin->crypto_init = NULL;
  1935. return GNUNET_SYSERR;
  1936. }
  1937. GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (cert_creation));
  1938. GNUNET_OS_process_destroy (cert_creation);
  1939. plugin->key = server_load_file (key_file);
  1940. plugin->cert = server_load_file (cert_file);
  1941. }
  1942. if ((plugin->key == NULL) || (plugin->cert == NULL))
  1943. {
  1944. LOG (GNUNET_ERROR_TYPE_ERROR,
  1945. _("No usable TLS certificate found and creating one at `%s/%s' failed!\n"),
  1946. key_file, cert_file);
  1947. GNUNET_free (key_file);
  1948. GNUNET_free (cert_file);
  1949. GNUNET_free_non_null (plugin->key);
  1950. plugin->key = NULL;
  1951. GNUNET_free_non_null (plugin->cert);
  1952. plugin->cert = NULL;
  1953. GNUNET_free_non_null (plugin->crypto_init);
  1954. plugin->crypto_init = NULL;
  1955. return GNUNET_SYSERR;
  1956. }
  1957. GNUNET_free (key_file);
  1958. GNUNET_free (cert_file);
  1959. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1960. "TLS certificate loaded\n");
  1961. return res;
  1962. }
  1963. #endif
  1964. /**
  1965. * Invoke `MHD_start_daemon` with the various options we need to
  1966. * setup the HTTP server with the given listen address.
  1967. *
  1968. * @param plugin our plugin
  1969. * @param addr listen address to use
  1970. * @param v6 MHD_NO_FLAG or MHD_USE_IPv6, depending on context
  1971. * @return NULL on error
  1972. */
  1973. static struct MHD_Daemon *
  1974. run_mhd_start_daemon (struct HTTP_Server_Plugin *plugin,
  1975. const struct sockaddr_in *addr,
  1976. int v6)
  1977. {
  1978. struct MHD_Daemon *server;
  1979. unsigned int timeout;
  1980. #if MHD_VERSION >= 0x00090E00
  1981. timeout = HTTP_SERVER_NOT_VALIDATED_TIMEOUT.rel_value_us / 1000LL / 1000LL;
  1982. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1983. "MHD can set timeout per connection! Default time out %u sec.\n",
  1984. timeout);
  1985. #else
  1986. timeout = HTTP_SERVER_SESSION_TIMEOUT.rel_value_us / 1000LL / 1000LL;
  1987. LOG (GNUNET_ERROR_TYPE_WARNING,
  1988. "MHD cannot set timeout per connection! Default time out %u sec.\n",
  1989. timeout);
  1990. #endif
  1991. server = MHD_start_daemon (
  1992. #if VERBOSE_SERVER
  1993. MHD_USE_DEBUG |
  1994. #endif
  1995. #if BUILD_HTTPS
  1996. MHD_USE_SSL |
  1997. #endif
  1998. MHD_USE_SUSPEND_RESUME |
  1999. v6,
  2000. plugin->port,
  2001. &server_accept_cb, plugin,
  2002. &server_access_cb, plugin,
  2003. MHD_OPTION_SOCK_ADDR,
  2004. addr,
  2005. MHD_OPTION_CONNECTION_LIMIT,
  2006. (unsigned int) plugin->max_request,
  2007. #if BUILD_HTTPS
  2008. MHD_OPTION_HTTPS_PRIORITIES,
  2009. plugin->crypto_init,
  2010. MHD_OPTION_HTTPS_MEM_KEY,
  2011. plugin->key,
  2012. MHD_OPTION_HTTPS_MEM_CERT,
  2013. plugin->cert,
  2014. #endif
  2015. MHD_OPTION_CONNECTION_TIMEOUT,
  2016. timeout,
  2017. MHD_OPTION_CONNECTION_MEMORY_LIMIT,
  2018. (size_t) (2 *
  2019. GNUNET_SERVER_MAX_MESSAGE_SIZE),
  2020. MHD_OPTION_NOTIFY_COMPLETED,
  2021. &server_disconnect_cb, plugin,
  2022. MHD_OPTION_EXTERNAL_LOGGER,
  2023. &server_log, NULL,
  2024. MHD_OPTION_END);
  2025. #ifdef TCP_STEALTH
  2026. if ( (NULL != server) &&
  2027. (0 != (plugin->options & HTTP_OPTIONS_TCP_STEALTH)) )
  2028. {
  2029. const union MHD_DaemonInfo *di;
  2030. di = MHD_get_daemon_info (server,
  2031. MHD_DAEMON_INFO_LISTEN_FD,
  2032. NULL);
  2033. if ( (0 != setsockopt ((int) di->listen_fd,
  2034. IPPROTO_TCP,
  2035. TCP_STEALTH,
  2036. plugin->env->my_identity,
  2037. sizeof (struct GNUNET_PeerIdentity))) )
  2038. {
  2039. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2040. _("TCP_STEALTH not supported on this platform.\n"));
  2041. MHD_stop_daemon (server);
  2042. server = NULL;
  2043. }
  2044. }
  2045. #endif
  2046. return server;
  2047. }
  2048. /**
  2049. * Start the HTTP server
  2050. *
  2051. * @param plugin the plugin handle
  2052. * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  2053. */
  2054. static int
  2055. server_start (struct HTTP_Server_Plugin *plugin)
  2056. {
  2057. const char *msg;
  2058. GNUNET_assert (NULL != plugin);
  2059. #if BUILD_HTTPS
  2060. if (GNUNET_SYSERR == server_load_certificate (plugin))
  2061. {
  2062. LOG (GNUNET_ERROR_TYPE_ERROR,
  2063. _("Could not load or create server certificate! Loading plugin failed!\n"));
  2064. return GNUNET_SYSERR;
  2065. }
  2066. #endif
  2067. plugin->server_v4 = NULL;
  2068. if (GNUNET_YES == plugin->use_ipv4)
  2069. {
  2070. plugin->server_v4
  2071. = run_mhd_start_daemon (plugin,
  2072. (const struct sockaddr_in *) plugin->server_addr_v4,
  2073. MHD_NO_FLAG);
  2074. if (NULL == plugin->server_v4)
  2075. {
  2076. LOG (GNUNET_ERROR_TYPE_ERROR,
  2077. "Failed to start %s IPv4 server component on port %u\n",
  2078. plugin->name,
  2079. plugin->port);
  2080. }
  2081. else
  2082. server_reschedule (plugin,
  2083. plugin->server_v4,
  2084. GNUNET_NO);
  2085. }
  2086. plugin->server_v6 = NULL;
  2087. if (GNUNET_YES == plugin->use_ipv6)
  2088. {
  2089. plugin->server_v6
  2090. = run_mhd_start_daemon (plugin,
  2091. (const struct sockaddr_in *) plugin->server_addr_v6,
  2092. MHD_USE_IPv6);
  2093. if (NULL == plugin->server_v6)
  2094. {
  2095. LOG (GNUNET_ERROR_TYPE_ERROR,
  2096. "Failed to start %s IPv6 server component on port %u\n",
  2097. plugin->name,
  2098. plugin->port);
  2099. }
  2100. else
  2101. {
  2102. server_reschedule (plugin,
  2103. plugin->server_v6,
  2104. GNUNET_NO);
  2105. }
  2106. }
  2107. msg = "No";
  2108. if ( (NULL == plugin->server_v6) &&
  2109. (NULL == plugin->server_v4) )
  2110. {
  2111. LOG (GNUNET_ERROR_TYPE_ERROR,
  2112. "%s %s server component started on port %u\n",
  2113. msg,
  2114. plugin->name,
  2115. plugin->port);
  2116. return GNUNET_SYSERR;
  2117. }
  2118. if ((NULL != plugin->server_v6) &&
  2119. (NULL != plugin->server_v4))
  2120. msg = "IPv4 and IPv6";
  2121. else if (NULL != plugin->server_v6)
  2122. msg = "IPv6";
  2123. else if (NULL != plugin->server_v4)
  2124. msg = "IPv4";
  2125. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2126. "%s %s server component started on port %u\n",
  2127. msg,
  2128. plugin->name,
  2129. plugin->port);
  2130. return GNUNET_OK;
  2131. }
  2132. /**
  2133. * Add an address to the server's set of addresses and notify transport
  2134. *
  2135. * @param cls the plugin handle
  2136. * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
  2137. * @param addr the address
  2138. * @param addrlen address length
  2139. */
  2140. static void
  2141. server_add_address (void *cls,
  2142. int add_remove,
  2143. const struct sockaddr *addr,
  2144. socklen_t addrlen)
  2145. {
  2146. struct HTTP_Server_Plugin *plugin = cls;
  2147. struct GNUNET_HELLO_Address *address;
  2148. struct HttpAddressWrapper *w = NULL;
  2149. w = GNUNET_new (struct HttpAddressWrapper);
  2150. w->address = http_common_address_from_socket (plugin->protocol,
  2151. addr,
  2152. addrlen);
  2153. if (NULL == w->address)
  2154. {
  2155. GNUNET_free (w);
  2156. return;
  2157. }
  2158. w->addrlen = http_common_address_get_size (w->address);
  2159. GNUNET_CONTAINER_DLL_insert (plugin->addr_head,
  2160. plugin->addr_tail,
  2161. w);
  2162. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2163. "Notifying transport to add address `%s'\n",
  2164. http_common_plugin_address_to_string (plugin->protocol,
  2165. w->address,
  2166. w->addrlen));
  2167. /* modify our published address list */
  2168. #if BUILD_HTTPS
  2169. address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
  2170. "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
  2171. #else
  2172. address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
  2173. "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
  2174. #endif
  2175. plugin->env->notify_address (plugin->env->cls,
  2176. add_remove,
  2177. address);
  2178. GNUNET_HELLO_address_free (address);
  2179. }
  2180. /**
  2181. * Remove an address from the server's set of addresses and notify transport
  2182. *
  2183. * @param cls the plugin handle
  2184. * @param add_remove #GNUNET_YES on add, #GNUNET_NO on remove
  2185. * @param addr the address
  2186. * @param addrlen address length
  2187. */
  2188. static void
  2189. server_remove_address (void *cls,
  2190. int add_remove,
  2191. const struct sockaddr *addr,
  2192. socklen_t addrlen)
  2193. {
  2194. struct HTTP_Server_Plugin *plugin = cls;
  2195. struct GNUNET_HELLO_Address *address;
  2196. struct HttpAddressWrapper *w = plugin->addr_head;
  2197. size_t saddr_len;
  2198. void * saddr;
  2199. saddr = http_common_address_from_socket (plugin->protocol,
  2200. addr,
  2201. addrlen);
  2202. if (NULL == saddr)
  2203. return;
  2204. saddr_len = http_common_address_get_size (saddr);
  2205. while (NULL != w)
  2206. {
  2207. if (GNUNET_YES ==
  2208. http_common_cmp_addresses (w->address,
  2209. w->addrlen,
  2210. saddr,
  2211. saddr_len))
  2212. break;
  2213. w = w->next;
  2214. }
  2215. GNUNET_free (saddr);
  2216. if (NULL == w)
  2217. return;
  2218. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2219. "Notifying transport to remove address `%s'\n",
  2220. http_common_plugin_address_to_string (plugin->protocol,
  2221. w->address,
  2222. w->addrlen));
  2223. GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
  2224. plugin->addr_tail,
  2225. w);
  2226. /* modify our published address list */
  2227. #if BUILD_HTTPS
  2228. address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
  2229. "https_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
  2230. #else
  2231. address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
  2232. "http_client", w->address, w->addrlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
  2233. #endif
  2234. plugin->env->notify_address (plugin->env->cls, add_remove, address);
  2235. GNUNET_HELLO_address_free (address);
  2236. GNUNET_free (w->address);
  2237. GNUNET_free (w);
  2238. }
  2239. /**
  2240. * Our external IP address/port mapping has changed.
  2241. *
  2242. * @param cls closure, the 'struct LocalAddrList'
  2243. * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
  2244. * the previous (now invalid) one
  2245. * @param addr either the previous or the new public IP address
  2246. * @param addrlen actual lenght of the address
  2247. */
  2248. static void
  2249. server_nat_port_map_callback (void *cls,
  2250. int add_remove,
  2251. const struct sockaddr *addr,
  2252. socklen_t addrlen)
  2253. {
  2254. struct HTTP_Server_Plugin *plugin = cls;
  2255. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2256. "NAT called to %s address `%s'\n",
  2257. (add_remove == GNUNET_NO) ? "remove" : "add",
  2258. GNUNET_a2s (addr, addrlen));
  2259. if (AF_INET == addr->sa_family)
  2260. {
  2261. struct sockaddr_in *s4 = (struct sockaddr_in *) addr;
  2262. if (GNUNET_NO == plugin->use_ipv4)
  2263. return;
  2264. if ((NULL != plugin->server_addr_v4) &&
  2265. (0 != memcmp (&plugin->server_addr_v4->sin_addr,
  2266. &s4->sin_addr, sizeof (struct in_addr))))
  2267. {
  2268. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2269. "Skipping address `%s' (not bindto address)\n",
  2270. GNUNET_a2s (addr, addrlen));
  2271. return;
  2272. }
  2273. }
  2274. if (AF_INET6 == addr->sa_family)
  2275. {
  2276. struct sockaddr_in6 *s6 = (struct sockaddr_in6 *) addr;
  2277. if (GNUNET_NO == plugin->use_ipv6)
  2278. return;
  2279. if ((NULL != plugin->server_addr_v6) &&
  2280. (0 != memcmp (&plugin->server_addr_v6->sin6_addr,
  2281. &s6->sin6_addr, sizeof (struct in6_addr))))
  2282. {
  2283. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2284. "Skipping address `%s' (not bindto address)\n",
  2285. GNUNET_a2s (addr, addrlen));
  2286. return;
  2287. }
  2288. }
  2289. switch (add_remove)
  2290. {
  2291. case GNUNET_YES:
  2292. server_add_address (cls, add_remove, addr, addrlen);
  2293. break;
  2294. case GNUNET_NO:
  2295. server_remove_address (cls, add_remove, addr, addrlen);
  2296. break;
  2297. }
  2298. }
  2299. /**
  2300. * Get valid server addresses
  2301. *
  2302. * @param plugin the plugin handle
  2303. * @param service_name the servicename
  2304. * @param cfg configuration handle
  2305. * @param addrs addresses
  2306. * @param addr_lens address length
  2307. * @return number of addresses
  2308. */
  2309. static int
  2310. server_get_addresses (struct HTTP_Server_Plugin *plugin,
  2311. const char *service_name,
  2312. const struct GNUNET_CONFIGURATION_Handle *cfg,
  2313. struct sockaddr ***addrs,
  2314. socklen_t ** addr_lens)
  2315. {
  2316. int disablev6;
  2317. unsigned long long port;
  2318. struct addrinfo hints;
  2319. struct addrinfo *res;
  2320. struct addrinfo *pos;
  2321. struct addrinfo *next;
  2322. unsigned int i;
  2323. int resi;
  2324. int ret;
  2325. struct sockaddr **saddrs;
  2326. socklen_t *saddrlens;
  2327. char *hostname;
  2328. *addrs = NULL;
  2329. *addr_lens = NULL;
  2330. disablev6 = !plugin->use_ipv6;
  2331. port = 0;
  2332. if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
  2333. {
  2334. GNUNET_break (GNUNET_OK ==
  2335. GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
  2336. "PORT", &port));
  2337. if (port > 65535)
  2338. {
  2339. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2340. _("Require valid port number for service in configuration!\n"));
  2341. return GNUNET_SYSERR;
  2342. }
  2343. }
  2344. if (0 == port)
  2345. {
  2346. LOG (GNUNET_ERROR_TYPE_INFO,
  2347. "Starting in listen only mode\n");
  2348. return -1; /* listen only */
  2349. }
  2350. if (GNUNET_CONFIGURATION_have_value (cfg, service_name,
  2351. "BINDTO"))
  2352. {
  2353. GNUNET_break (GNUNET_OK ==
  2354. GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
  2355. "BINDTO", &hostname));
  2356. }
  2357. else
  2358. hostname = NULL;
  2359. if (NULL != hostname)
  2360. {
  2361. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2362. "Resolving `%s' since that is where `%s' will bind to.\n",
  2363. hostname, service_name);
  2364. memset (&hints, 0, sizeof (struct addrinfo));
  2365. if (disablev6)
  2366. hints.ai_family = AF_INET;
  2367. if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
  2368. (NULL == res))
  2369. {
  2370. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2371. _("Failed to resolve `%s': %s\n"),
  2372. hostname,
  2373. gai_strerror (ret));
  2374. GNUNET_free (hostname);
  2375. return GNUNET_SYSERR;
  2376. }
  2377. next = res;
  2378. i = 0;
  2379. while (NULL != (pos = next))
  2380. {
  2381. next = pos->ai_next;
  2382. if ((disablev6) && (pos->ai_family == AF_INET6))
  2383. continue;
  2384. i++;
  2385. }
  2386. if (0 == i)
  2387. {
  2388. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2389. _("Failed to find %saddress for `%s'.\n"),
  2390. disablev6 ? "IPv4 " : "", hostname);
  2391. freeaddrinfo (res);
  2392. GNUNET_free (hostname);
  2393. return GNUNET_SYSERR;
  2394. }
  2395. resi = i;
  2396. saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
  2397. saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
  2398. i = 0;
  2399. next = res;
  2400. while (NULL != (pos = next))
  2401. {
  2402. next = pos->ai_next;
  2403. if ((disablev6) && (pos->ai_family == AF_INET6))
  2404. continue;
  2405. if ((pos->ai_protocol != IPPROTO_TCP) && (0 != pos->ai_protocol))
  2406. continue; /* not TCP */
  2407. if ((pos->ai_socktype != SOCK_STREAM) && (0 != pos->ai_socktype))
  2408. continue; /* huh? */
  2409. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2410. "Service will bind to `%s'\n",
  2411. GNUNET_a2s (pos->ai_addr,
  2412. pos->ai_addrlen));
  2413. if (pos->ai_family == AF_INET)
  2414. {
  2415. GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
  2416. saddrlens[i] = pos->ai_addrlen;
  2417. saddrs[i] = GNUNET_malloc (saddrlens[i]);
  2418. memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
  2419. ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
  2420. }
  2421. else
  2422. {
  2423. GNUNET_assert (pos->ai_family == AF_INET6);
  2424. GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in6));
  2425. saddrlens[i] = pos->ai_addrlen;
  2426. saddrs[i] = GNUNET_malloc (saddrlens[i]);
  2427. memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
  2428. ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
  2429. }
  2430. i++;
  2431. }
  2432. GNUNET_free (hostname);
  2433. freeaddrinfo (res);
  2434. resi = i;
  2435. }
  2436. else
  2437. {
  2438. /* will bind against everything, just set port */
  2439. if (disablev6)
  2440. {
  2441. /* V4-only */
  2442. resi = 1;
  2443. i = 0;
  2444. saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
  2445. saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
  2446. saddrlens[i] = sizeof (struct sockaddr_in);
  2447. saddrs[i] = GNUNET_malloc (saddrlens[i]);
  2448. #if HAVE_SOCKADDR_IN_SIN_LEN
  2449. ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
  2450. #endif
  2451. ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
  2452. ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
  2453. }
  2454. else
  2455. {
  2456. /* dual stack */
  2457. resi = 2;
  2458. saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
  2459. saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
  2460. i = 0;
  2461. saddrlens[i] = sizeof (struct sockaddr_in6);
  2462. saddrs[i] = GNUNET_malloc (saddrlens[i]);
  2463. #if HAVE_SOCKADDR_IN_SIN_LEN
  2464. ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
  2465. #endif
  2466. ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
  2467. ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
  2468. i++;
  2469. saddrlens[i] = sizeof (struct sockaddr_in);
  2470. saddrs[i] = GNUNET_malloc (saddrlens[i]);
  2471. #if HAVE_SOCKADDR_IN_SIN_LEN
  2472. ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
  2473. #endif
  2474. ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
  2475. ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
  2476. }
  2477. }
  2478. *addrs = saddrs;
  2479. *addr_lens = saddrlens;
  2480. return resi;
  2481. }
  2482. /**
  2483. * Ask NAT for addresses
  2484. *
  2485. * @param plugin the plugin handle
  2486. */
  2487. static void
  2488. server_start_report_addresses (struct HTTP_Server_Plugin *plugin)
  2489. {
  2490. int res = GNUNET_OK;
  2491. struct sockaddr **addrs;
  2492. socklen_t *addrlens;
  2493. res = server_get_addresses (plugin,
  2494. plugin->name,
  2495. plugin->env->cfg,
  2496. &addrs, &addrlens);
  2497. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2498. _("Found %u addresses to report to NAT service\n"),
  2499. res);
  2500. if (GNUNET_SYSERR == res)
  2501. {
  2502. plugin->nat = NULL;
  2503. return;
  2504. }
  2505. plugin->nat =
  2506. GNUNET_NAT_register (plugin->env->cfg,
  2507. GNUNET_YES,
  2508. plugin->port,
  2509. (unsigned int) res,
  2510. (const struct sockaddr **) addrs, addrlens,
  2511. &server_nat_port_map_callback, NULL, plugin);
  2512. while (res > 0)
  2513. {
  2514. res--;
  2515. GNUNET_assert (NULL != addrs[res]);
  2516. GNUNET_free (addrs[res]);
  2517. }
  2518. GNUNET_free_non_null (addrs);
  2519. GNUNET_free_non_null (addrlens);
  2520. }
  2521. /**
  2522. * Stop NAT for addresses
  2523. *
  2524. * @param plugin the plugin handle
  2525. */
  2526. static void
  2527. server_stop_report_addresses (struct HTTP_Server_Plugin *plugin)
  2528. {
  2529. struct HttpAddressWrapper *w;
  2530. /* Stop NAT handle */
  2531. if (NULL != plugin->nat)
  2532. {
  2533. GNUNET_NAT_unregister (plugin->nat);
  2534. plugin->nat = NULL;
  2535. }
  2536. /* Clean up addresses */
  2537. while (NULL != plugin->addr_head)
  2538. {
  2539. w = plugin->addr_head;
  2540. GNUNET_CONTAINER_DLL_remove (plugin->addr_head,
  2541. plugin->addr_tail,
  2542. w);
  2543. GNUNET_free (w->address);
  2544. GNUNET_free (w);
  2545. }
  2546. }
  2547. /**
  2548. * Check if IPv6 supported on this system
  2549. *
  2550. * @param plugin the plugin handle
  2551. * @return #GNUNET_YES on success, else #GNUNET_NO
  2552. */
  2553. static int
  2554. server_check_ipv6_support (struct HTTP_Server_Plugin *plugin)
  2555. {
  2556. struct GNUNET_NETWORK_Handle *desc = NULL;
  2557. int res = GNUNET_NO;
  2558. /* Probe IPv6 support */
  2559. desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
  2560. if (NULL == desc)
  2561. {
  2562. if ((errno == ENOBUFS) || (errno == ENOMEM) || (errno == ENFILE) ||
  2563. (errno == EACCES))
  2564. {
  2565. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
  2566. }
  2567. LOG (GNUNET_ERROR_TYPE_WARNING,
  2568. _("Disabling IPv6 since it is not supported on this system!\n"));
  2569. res = GNUNET_NO;
  2570. }
  2571. else
  2572. {
  2573. GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
  2574. desc = NULL;
  2575. res = GNUNET_YES;
  2576. }
  2577. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2578. "Testing IPv6 on this system: %s\n",
  2579. (res == GNUNET_YES) ? "successful" : "failed");
  2580. return res;
  2581. }
  2582. /**
  2583. * Notify server about our external hostname
  2584. *
  2585. * @param cls plugin
  2586. * @param tc task context (unused)
  2587. */
  2588. static void
  2589. server_notify_external_hostname (void *cls,
  2590. const struct GNUNET_SCHEDULER_TaskContext *tc)
  2591. {
  2592. struct HTTP_Server_Plugin *plugin = cls;
  2593. struct HttpAddress *ext_addr;
  2594. size_t ext_addr_len;
  2595. unsigned int urlen;
  2596. char *url;
  2597. plugin->notify_ext_task = NULL;
  2598. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
  2599. return;
  2600. GNUNET_asprintf(&url,
  2601. "%s://%s",
  2602. plugin->protocol,
  2603. plugin->external_hostname);
  2604. urlen = strlen (url) + 1;
  2605. ext_addr = GNUNET_malloc (sizeof (struct HttpAddress) + urlen);
  2606. ext_addr->options = htonl (plugin->options);
  2607. ext_addr->urlen = htonl (urlen);
  2608. ext_addr_len = sizeof (struct HttpAddress) + urlen;
  2609. memcpy (&ext_addr[1], url, urlen);
  2610. GNUNET_free (url);
  2611. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2612. "Notifying transport about external hostname address `%s'\n",
  2613. plugin->external_hostname);
  2614. #if BUILD_HTTPS
  2615. if (GNUNET_YES == plugin->verify_external_hostname)
  2616. LOG (GNUNET_ERROR_TYPE_INFO,
  2617. "Enabling SSL verification for external hostname address `%s'\n",
  2618. plugin->external_hostname);
  2619. plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
  2620. "https_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
  2621. plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
  2622. GNUNET_free (ext_addr);
  2623. #else
  2624. plugin->ext_addr = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
  2625. "http_client", ext_addr, ext_addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE );
  2626. plugin->env->notify_address (plugin->env->cls, GNUNET_YES, plugin->ext_addr);
  2627. GNUNET_free (ext_addr);
  2628. #endif
  2629. }
  2630. /**
  2631. * Configure the plugin
  2632. *
  2633. * @param plugin plugin handle
  2634. * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  2635. */
  2636. static int
  2637. server_configure_plugin (struct HTTP_Server_Plugin *plugin)
  2638. {
  2639. unsigned long long port;
  2640. unsigned long long max_connections;
  2641. char *bind4_address = NULL;
  2642. char *bind6_address = NULL;
  2643. char *eh_tmp = NULL;
  2644. int external_hostname_use_port;
  2645. /* Use IPv4? */
  2646. if (GNUNET_CONFIGURATION_have_value
  2647. (plugin->env->cfg, plugin->name, "USE_IPv4"))
  2648. {
  2649. plugin->use_ipv4 =
  2650. GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
  2651. plugin->name,
  2652. "USE_IPv4");
  2653. }
  2654. else
  2655. plugin->use_ipv4 = GNUNET_YES;
  2656. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2657. _("IPv4 support is %s\n"),
  2658. (plugin->use_ipv4 == GNUNET_YES) ? "enabled" : "disabled");
  2659. /* Use IPv6? */
  2660. if (GNUNET_CONFIGURATION_have_value
  2661. (plugin->env->cfg, plugin->name, "USE_IPv6"))
  2662. {
  2663. plugin->use_ipv6 =
  2664. GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
  2665. plugin->name,
  2666. "USE_IPv6");
  2667. }
  2668. else
  2669. plugin->use_ipv6 = GNUNET_YES;
  2670. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2671. _("IPv6 support is %s\n"),
  2672. (plugin->use_ipv6 == GNUNET_YES) ? "enabled" : "disabled");
  2673. if ((plugin->use_ipv4 == GNUNET_NO) && (plugin->use_ipv6 == GNUNET_NO))
  2674. {
  2675. LOG (GNUNET_ERROR_TYPE_ERROR,
  2676. _("Neither IPv4 nor IPv6 are enabled! Fix in configuration\n"));
  2677. return GNUNET_SYSERR;
  2678. }
  2679. /* Reading port number from config file */
  2680. if ((GNUNET_OK !=
  2681. GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
  2682. plugin->name,
  2683. "PORT", &port)) || (port > 65535))
  2684. {
  2685. LOG (GNUNET_ERROR_TYPE_ERROR,
  2686. _("Port is required! Fix in configuration\n"));
  2687. return GNUNET_SYSERR;
  2688. }
  2689. plugin->port = port;
  2690. LOG (GNUNET_ERROR_TYPE_INFO,
  2691. _("Using port %u\n"), plugin->port);
  2692. if ((plugin->use_ipv4 == GNUNET_YES) &&
  2693. (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
  2694. plugin->name, "BINDTO", &bind4_address)))
  2695. {
  2696. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2697. "Binding %s plugin to specific IPv4 address: `%s'\n",
  2698. plugin->protocol, bind4_address);
  2699. plugin->server_addr_v4 = GNUNET_new (struct sockaddr_in);
  2700. if (1 != inet_pton (AF_INET, bind4_address,
  2701. &plugin->server_addr_v4->sin_addr))
  2702. {
  2703. LOG (GNUNET_ERROR_TYPE_ERROR,
  2704. _("Specific IPv4 address `%s' in configuration file is invalid!\n"),
  2705. bind4_address);
  2706. GNUNET_free (bind4_address);
  2707. GNUNET_free (plugin->server_addr_v4);
  2708. plugin->server_addr_v4 = NULL;
  2709. return GNUNET_SYSERR;
  2710. }
  2711. else
  2712. {
  2713. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2714. _("Binding to IPv4 address %s\n"),
  2715. bind4_address);
  2716. plugin->server_addr_v4->sin_family = AF_INET;
  2717. plugin->server_addr_v4->sin_port = htons (plugin->port);
  2718. }
  2719. GNUNET_free (bind4_address);
  2720. }
  2721. if ((plugin->use_ipv6 == GNUNET_YES) &&
  2722. (GNUNET_YES ==
  2723. GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
  2724. plugin->name,
  2725. "BINDTO6", &bind6_address)))
  2726. {
  2727. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2728. "Binding %s plugin to specific IPv6 address: `%s'\n",
  2729. plugin->protocol, bind6_address);
  2730. plugin->server_addr_v6 = GNUNET_new (struct sockaddr_in6);
  2731. if (1 !=
  2732. inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
  2733. {
  2734. LOG (GNUNET_ERROR_TYPE_ERROR,
  2735. _("Specific IPv6 address `%s' in configuration file is invalid!\n"),
  2736. bind6_address);
  2737. GNUNET_free (bind6_address);
  2738. GNUNET_free (plugin->server_addr_v6);
  2739. plugin->server_addr_v6 = NULL;
  2740. return GNUNET_SYSERR;
  2741. }
  2742. else
  2743. {
  2744. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2745. _("Binding to IPv6 address %s\n"),
  2746. bind6_address);
  2747. plugin->server_addr_v6->sin6_family = AF_INET6;
  2748. plugin->server_addr_v6->sin6_port = htons (plugin->port);
  2749. }
  2750. GNUNET_free (bind6_address);
  2751. }
  2752. plugin->verify_external_hostname = GNUNET_NO;
  2753. #if BUILD_HTTPS
  2754. plugin->verify_external_hostname = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
  2755. plugin->name,
  2756. "VERIFY_EXTERNAL_HOSTNAME");
  2757. if (GNUNET_SYSERR == plugin->verify_external_hostname)
  2758. plugin->verify_external_hostname = GNUNET_NO;
  2759. if (GNUNET_YES == plugin->verify_external_hostname)
  2760. plugin->options |= HTTP_OPTIONS_VERIFY_CERTIFICATE;
  2761. #endif
  2762. external_hostname_use_port = GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
  2763. plugin->name,
  2764. "EXTERNAL_HOSTNAME_USE_PORT");
  2765. if (GNUNET_SYSERR == external_hostname_use_port)
  2766. external_hostname_use_port = GNUNET_NO;
  2767. if (GNUNET_YES ==
  2768. GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
  2769. plugin->name,
  2770. "EXTERNAL_HOSTNAME",
  2771. &eh_tmp))
  2772. {
  2773. char *tmp;
  2774. char *pos = NULL;
  2775. char *pos_url = NULL;
  2776. if (NULL != strstr(eh_tmp, "://"))
  2777. tmp = &strstr(eh_tmp, "://")[3];
  2778. else
  2779. tmp = eh_tmp;
  2780. if (GNUNET_YES == external_hostname_use_port)
  2781. {
  2782. if ( (strlen (tmp) > 1) && (NULL != (pos = strchr(tmp, '/'))) )
  2783. {
  2784. pos_url = pos + 1;
  2785. pos[0] = '\0';
  2786. GNUNET_asprintf (&plugin->external_hostname,
  2787. "%s:%u/%s",
  2788. tmp,
  2789. (uint16_t) port,
  2790. (NULL == pos_url) ? "" : pos_url);
  2791. }
  2792. else
  2793. GNUNET_asprintf (&plugin->external_hostname,
  2794. "%s:%u",
  2795. tmp,
  2796. (uint16_t) port);
  2797. }
  2798. else
  2799. plugin->external_hostname = GNUNET_strdup (tmp);
  2800. GNUNET_free (eh_tmp);
  2801. LOG (GNUNET_ERROR_TYPE_INFO,
  2802. _("Using external hostname `%s'\n"),
  2803. plugin->external_hostname);
  2804. plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&server_notify_external_hostname,
  2805. plugin);
  2806. /* Use only configured external hostname */
  2807. if (GNUNET_CONFIGURATION_have_value
  2808. (plugin->env->cfg,
  2809. plugin->name,
  2810. "EXTERNAL_HOSTNAME_ONLY"))
  2811. {
  2812. plugin->external_only =
  2813. GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg,
  2814. plugin->name,
  2815. "EXTERNAL_HOSTNAME_ONLY");
  2816. }
  2817. else
  2818. plugin->external_only = GNUNET_NO;
  2819. if (GNUNET_YES == plugin->external_only)
  2820. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2821. _("Notifying transport only about hostname `%s'\n"),
  2822. plugin->external_hostname);
  2823. }
  2824. else
  2825. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2826. "No external hostname configured\n");
  2827. /* Optional parameters */
  2828. if (GNUNET_OK !=
  2829. GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
  2830. plugin->name,
  2831. "MAX_CONNECTIONS",
  2832. &max_connections))
  2833. max_connections = 128;
  2834. plugin->max_request = max_connections;
  2835. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2836. _("Maximum number of connections is %u\n"),
  2837. plugin->max_request);
  2838. plugin->peer_id_length = strlen (GNUNET_i2s_full (plugin->env->my_identity));
  2839. return GNUNET_OK;
  2840. }
  2841. /**
  2842. * Exit point from the plugin.
  2843. *
  2844. * @param cls api
  2845. * @return NULL
  2846. */
  2847. void *
  2848. LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
  2849. {
  2850. struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
  2851. struct HTTP_Server_Plugin *plugin = api->cls;
  2852. if (NULL == api->cls)
  2853. {
  2854. /* Free for stub mode */
  2855. GNUNET_free (api);
  2856. return NULL;
  2857. }
  2858. plugin->in_shutdown = GNUNET_YES;
  2859. LOG (GNUNET_ERROR_TYPE_INFO,
  2860. _("Shutting down plugin `%s'\n"),
  2861. plugin->name);
  2862. if (NULL != plugin->notify_ext_task)
  2863. {
  2864. GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
  2865. plugin->notify_ext_task = NULL;
  2866. }
  2867. if (NULL != plugin->ext_addr)
  2868. {
  2869. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2870. "Notifying transport to remove address `%s'\n",
  2871. http_common_plugin_address_to_string (plugin->protocol,
  2872. plugin->ext_addr->address,
  2873. plugin->ext_addr->address_length));
  2874. #if BUILD_HTTPS
  2875. plugin->env->notify_address (plugin->env->cls,
  2876. GNUNET_NO,
  2877. plugin->ext_addr);
  2878. #else
  2879. plugin->env->notify_address (plugin->env->cls,
  2880. GNUNET_NO,
  2881. plugin->ext_addr);
  2882. #endif
  2883. GNUNET_HELLO_address_free (plugin->ext_addr);
  2884. plugin->ext_addr = NULL;
  2885. }
  2886. /* Stop to report addresses to transport service */
  2887. server_stop_report_addresses (plugin);
  2888. if (NULL != plugin->server_v4)
  2889. {
  2890. MHD_stop_daemon (plugin->server_v4);
  2891. plugin->server_v4 = NULL;
  2892. }
  2893. if (NULL != plugin->server_v6)
  2894. {
  2895. MHD_stop_daemon (plugin->server_v6);
  2896. plugin->server_v6 = NULL;
  2897. }
  2898. if (NULL != plugin->server_v4_task)
  2899. {
  2900. GNUNET_SCHEDULER_cancel (plugin->server_v4_task);
  2901. plugin->server_v4_task = NULL;
  2902. }
  2903. if (NULL != plugin->server_v6_task)
  2904. {
  2905. GNUNET_SCHEDULER_cancel (plugin->server_v6_task);
  2906. plugin->server_v6_task = NULL;
  2907. }
  2908. #if BUILD_HTTPS
  2909. GNUNET_free_non_null (plugin->crypto_init);
  2910. GNUNET_free_non_null (plugin->cert);
  2911. GNUNET_free_non_null (plugin->key);
  2912. #endif
  2913. GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
  2914. &destroy_session_shutdown_cb,
  2915. plugin);
  2916. GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
  2917. plugin->sessions = NULL;
  2918. /* Clean up */
  2919. GNUNET_free_non_null (plugin->external_hostname);
  2920. GNUNET_free_non_null (plugin->ext_addr);
  2921. GNUNET_free_non_null (plugin->server_addr_v4);
  2922. GNUNET_free_non_null (plugin->server_addr_v6);
  2923. regfree (&plugin->url_regex);
  2924. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2925. _("Shutdown for plugin `%s' complete\n"),
  2926. plugin->name);
  2927. GNUNET_free (plugin);
  2928. GNUNET_free (api);
  2929. return NULL;
  2930. }
  2931. /**
  2932. * Function called for a quick conversion of the binary address to
  2933. * a numeric address. Note that the caller must not free the
  2934. * address and that the next call to this function is allowed
  2935. * to override the address again.
  2936. *
  2937. * @param cls unused
  2938. * @param addr binary address
  2939. * @param addrlen length of the address
  2940. * @return string representing the same address
  2941. */
  2942. static const char *
  2943. http_server_plugin_address_to_string (void *cls,
  2944. const void *addr,
  2945. size_t addrlen)
  2946. {
  2947. return http_common_plugin_address_to_string (PLUGIN_NAME,
  2948. addr,
  2949. addrlen);
  2950. }
  2951. /**
  2952. * Function obtain the network type for a session
  2953. *
  2954. * @param cls closure ('struct HTTP_Server_Plugin*')
  2955. * @param session the session
  2956. * @return the network type in HBO or #GNUNET_SYSERR
  2957. */
  2958. static enum GNUNET_ATS_Network_Type
  2959. http_server_plugin_get_network (void *cls,
  2960. struct Session *session)
  2961. {
  2962. return session->scope;
  2963. }
  2964. /**
  2965. * Function that will be called whenever the transport service wants to
  2966. * notify the plugin that the inbound quota changed and that the plugin
  2967. * should update it's delay for the next receive value
  2968. *
  2969. * @param cls closure
  2970. * @param peer which peer was the session for
  2971. * @param session which session is being updated
  2972. * @param delay new delay to use for receiving
  2973. */
  2974. static void
  2975. http_server_plugin_update_inbound_delay (void *cls,
  2976. const struct GNUNET_PeerIdentity *peer,
  2977. struct Session *session,
  2978. struct GNUNET_TIME_Relative delay)
  2979. {
  2980. session->next_receive = GNUNET_TIME_relative_to_absolute (delay);
  2981. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2982. "New inbound delay %s\n",
  2983. GNUNET_STRINGS_relative_time_to_string (delay,
  2984. GNUNET_NO));
  2985. if (NULL != session->recv_wakeup_task)
  2986. {
  2987. GNUNET_SCHEDULER_cancel (session->recv_wakeup_task);
  2988. session->recv_wakeup_task
  2989. = GNUNET_SCHEDULER_add_delayed (delay,
  2990. &server_wake_up,
  2991. session);
  2992. }
  2993. }
  2994. /**
  2995. * Return information about the given session to the
  2996. * monitor callback.
  2997. *
  2998. * @param cls the `struct Plugin` with the monitor callback (`sic`)
  2999. * @param peer peer we send information about
  3000. * @param value our `struct Session` to send information about
  3001. * @return #GNUNET_OK (continue to iterate)
  3002. */
  3003. static int
  3004. send_session_info_iter (void *cls,
  3005. const struct GNUNET_PeerIdentity *peer,
  3006. void *value)
  3007. {
  3008. struct HTTP_Server_Plugin *plugin = cls;
  3009. struct Session *session = value;
  3010. notify_session_monitor (plugin,
  3011. session,
  3012. GNUNET_TRANSPORT_SS_INIT);
  3013. return GNUNET_OK;
  3014. }
  3015. /**
  3016. * Begin monitoring sessions of a plugin. There can only
  3017. * be one active monitor per plugin (i.e. if there are
  3018. * multiple monitors, the transport service needs to
  3019. * multiplex the generated events over all of them).
  3020. *
  3021. * @param cls closure of the plugin
  3022. * @param sic callback to invoke, NULL to disable monitor;
  3023. * plugin will being by iterating over all active
  3024. * sessions immediately and then enter monitor mode
  3025. * @param sic_cls closure for @a sic
  3026. */
  3027. static void
  3028. http_server_plugin_setup_monitor (void *cls,
  3029. GNUNET_TRANSPORT_SessionInfoCallback sic,
  3030. void *sic_cls)
  3031. {
  3032. struct HTTP_Server_Plugin *plugin = cls;
  3033. plugin->sic = sic;
  3034. plugin->sic_cls = sic_cls;
  3035. if (NULL != sic)
  3036. {
  3037. GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
  3038. &send_session_info_iter,
  3039. plugin);
  3040. /* signal end of first iteration */
  3041. sic (sic_cls, NULL, NULL);
  3042. }
  3043. }
  3044. /**
  3045. * Entry point for the plugin.
  3046. *
  3047. * @param cls env
  3048. * @return api
  3049. */
  3050. void *
  3051. LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
  3052. {
  3053. struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
  3054. struct GNUNET_TRANSPORT_PluginFunctions *api;
  3055. struct HTTP_Server_Plugin *plugin;
  3056. if (NULL == env->receive)
  3057. {
  3058. /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
  3059. initialze the plugin or the API */
  3060. api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
  3061. api->cls = NULL;
  3062. api->address_to_string = &http_server_plugin_address_to_string;
  3063. api->string_to_address = &http_common_plugin_string_to_address;
  3064. api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
  3065. return api;
  3066. }
  3067. plugin = GNUNET_new (struct HTTP_Server_Plugin);
  3068. plugin->env = env;
  3069. plugin->sessions = GNUNET_CONTAINER_multipeermap_create (128,
  3070. GNUNET_YES);
  3071. api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
  3072. api->cls = plugin;
  3073. api->send = &http_server_plugin_send;
  3074. api->disconnect_peer = &http_server_plugin_disconnect_peer;
  3075. api->disconnect_session = &http_server_plugin_disconnect_session;
  3076. api->query_keepalive_factor = &http_server_query_keepalive_factor;
  3077. api->check_address = &http_server_plugin_address_suggested;
  3078. api->get_session = &http_server_plugin_get_session;
  3079. api->address_to_string = &http_server_plugin_address_to_string;
  3080. api->string_to_address = &http_common_plugin_string_to_address;
  3081. api->address_pretty_printer = &http_common_plugin_address_pretty_printer;
  3082. api->get_network = &http_server_plugin_get_network;
  3083. api->update_session_timeout = &http_server_plugin_update_session_timeout;
  3084. api->update_inbound_delay = &http_server_plugin_update_inbound_delay;
  3085. api->setup_monitor = &http_server_plugin_setup_monitor;
  3086. #if BUILD_HTTPS
  3087. plugin->name = "transport-https_server";
  3088. plugin->protocol = "https";
  3089. #else
  3090. plugin->name = "transport-http_server";
  3091. plugin->protocol = "http";
  3092. #endif
  3093. if (GNUNET_YES ==
  3094. GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
  3095. plugin->name,
  3096. "TCP_STEALTH"))
  3097. {
  3098. #ifdef TCP_STEALTH
  3099. plugin->options |= HTTP_OPTIONS_TCP_STEALTH;
  3100. #else
  3101. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3102. _("TCP_STEALTH not supported on this platform.\n"));
  3103. LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
  3104. return NULL;
  3105. #endif
  3106. }
  3107. /* Compile URL regex */
  3108. if (regcomp(&plugin->url_regex,
  3109. URL_REGEX,
  3110. REG_EXTENDED))
  3111. {
  3112. LOG (GNUNET_ERROR_TYPE_ERROR,
  3113. _("Unable to compile URL regex\n"));
  3114. LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
  3115. return NULL;
  3116. }
  3117. /* Configure plugin */
  3118. if (GNUNET_SYSERR == server_configure_plugin (plugin))
  3119. {
  3120. LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
  3121. return NULL;
  3122. }
  3123. /* Check IPv6 support */
  3124. if (GNUNET_YES == plugin->use_ipv6)
  3125. plugin->use_ipv6 = server_check_ipv6_support (plugin);
  3126. /* Report addresses to transport service */
  3127. if (GNUNET_NO == plugin->external_only)
  3128. server_start_report_addresses (plugin);
  3129. if (GNUNET_SYSERR == server_start (plugin))
  3130. {
  3131. LIBGNUNET_PLUGIN_TRANSPORT_DONE (api);
  3132. return NULL;
  3133. }
  3134. return api;
  3135. }
  3136. /* end of plugin_transport_http_server.c */