gnunet-gns-proxy.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012-2018 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @author Martin Schanzenbach
  18. * @author Christian Grothoff
  19. * @file src/gns/gnunet-gns-proxy.c
  20. * @brief HTTP(S) proxy that rewrites URIs and fakes certificates to make GNS work
  21. * with legacy browsers
  22. *
  23. * TODO:
  24. * - double-check queueing logic
  25. */
  26. #include "platform.h"
  27. #include <microhttpd.h>
  28. /* Just included for the right curl.h */
  29. #include "gnunet_curl_lib.h"
  30. #include <gnutls/gnutls.h>
  31. #include <gnutls/x509.h>
  32. #include <gnutls/abstract.h>
  33. #include <gnutls/crypto.h>
  34. #if HAVE_GNUTLS_DANE
  35. #include <gnutls/dane.h>
  36. #endif
  37. #include <regex.h>
  38. #include "gnunet_util_lib.h"
  39. #include "gnunet_gns_service.h"
  40. #include "gnunet_identity_service.h"
  41. #include "gns.h"
  42. #include "gnunet_mhd_compat.h"
  43. /**
  44. * Default Socks5 listen port.
  45. */
  46. #define GNUNET_GNS_PROXY_PORT 7777
  47. /**
  48. * Maximum supported length for a URI.
  49. * Should die. @deprecated
  50. */
  51. #define MAX_HTTP_URI_LENGTH 2048
  52. /**
  53. * Maximum number of DANE records we support
  54. * per domain name (and port and protocol).
  55. */
  56. #define MAX_DANES 32
  57. /**
  58. * Size of the buffer for the data upload / download. Must be
  59. * enough for curl, thus CURL_MAX_WRITE_SIZE is needed here (16k).
  60. */
  61. #define IO_BUFFERSIZE CURL_MAX_WRITE_SIZE
  62. /**
  63. * Size of the read/write buffers for Socks. Uses
  64. * 256 bytes for the hostname (at most), plus a few
  65. * bytes overhead for the messages.
  66. */
  67. #define SOCKS_BUFFERSIZE (256 + 32)
  68. /**
  69. * Port for plaintext HTTP.
  70. */
  71. #define HTTP_PORT 80
  72. /**
  73. * Port for HTTPS.
  74. */
  75. #define HTTPS_PORT 443
  76. /**
  77. * Largest allowed size for a PEM certificate.
  78. */
  79. #define MAX_PEM_SIZE (10 * 1024)
  80. /**
  81. * After how long do we clean up unused MHD TLS instances?
  82. */
  83. #define MHD_CACHE_TIMEOUT GNUNET_TIME_relative_multiply ( \
  84. GNUNET_TIME_UNIT_MINUTES, 5)
  85. /**
  86. * After how long do we clean up Socks5 handles that failed to show any activity
  87. * with their respective MHD instance?
  88. */
  89. #define HTTP_HANDSHAKE_TIMEOUT GNUNET_TIME_relative_multiply ( \
  90. GNUNET_TIME_UNIT_SECONDS, 15)
  91. /**
  92. * Log curl error.
  93. *
  94. * @param level log level
  95. * @param fun name of curl_easy-function that gave the error
  96. * @param rc return code from curl
  97. */
  98. #define LOG_CURL_EASY(level, fun, rc) \
  99. GNUNET_log (level, \
  100. _ ("%s failed at %s:%d: `%s'\n"), \
  101. fun, \
  102. __FILE__, \
  103. __LINE__, \
  104. curl_easy_strerror (rc))
  105. /* *************** Socks protocol definitions (move to TUN?) ****************** */
  106. /**
  107. * Which SOCKS version do we speak?
  108. */
  109. #define SOCKS_VERSION_5 0x05
  110. /**
  111. * Flag to set for 'no authentication'.
  112. */
  113. #define SOCKS_AUTH_NONE 0
  114. /**
  115. * Commands in Socks5.
  116. */
  117. enum Socks5Commands
  118. {
  119. /**
  120. * Establish TCP/IP stream.
  121. */
  122. SOCKS5_CMD_TCP_STREAM = 1,
  123. /**
  124. * Establish TCP port binding.
  125. */
  126. SOCKS5_CMD_TCP_PORT = 2,
  127. /**
  128. * Establish UDP port binding.
  129. */
  130. SOCKS5_CMD_UDP_PORT = 3
  131. };
  132. /**
  133. * Address types in Socks5.
  134. */
  135. enum Socks5AddressType
  136. {
  137. /**
  138. * IPv4 address.
  139. */
  140. SOCKS5_AT_IPV4 = 1,
  141. /**
  142. * IPv4 address.
  143. */
  144. SOCKS5_AT_DOMAINNAME = 3,
  145. /**
  146. * IPv6 address.
  147. */
  148. SOCKS5_AT_IPV6 = 4
  149. };
  150. /**
  151. * Status codes in Socks5 response.
  152. */
  153. enum Socks5StatusCode
  154. {
  155. SOCKS5_STATUS_REQUEST_GRANTED = 0,
  156. SOCKS5_STATUS_GENERAL_FAILURE = 1,
  157. SOCKS5_STATUS_CONNECTION_NOT_ALLOWED_BY_RULE = 2,
  158. SOCKS5_STATUS_NETWORK_UNREACHABLE = 3,
  159. SOCKS5_STATUS_HOST_UNREACHABLE = 4,
  160. SOCKS5_STATUS_CONNECTION_REFUSED_BY_HOST = 5,
  161. SOCKS5_STATUS_TTL_EXPIRED = 6,
  162. SOCKS5_STATUS_COMMAND_NOT_SUPPORTED = 7,
  163. SOCKS5_STATUS_ADDRESS_TYPE_NOT_SUPPORTED = 8
  164. };
  165. /**
  166. * Client hello in Socks5 protocol.
  167. */
  168. struct Socks5ClientHelloMessage
  169. {
  170. /**
  171. * Should be #SOCKS_VERSION_5.
  172. */
  173. uint8_t version;
  174. /**
  175. * How many authentication methods does the client support.
  176. */
  177. uint8_t num_auth_methods;
  178. /* followed by supported authentication methods, 1 byte per method */
  179. };
  180. /**
  181. * Server hello in Socks5 protocol.
  182. */
  183. struct Socks5ServerHelloMessage
  184. {
  185. /**
  186. * Should be #SOCKS_VERSION_5.
  187. */
  188. uint8_t version;
  189. /**
  190. * Chosen authentication method, for us always #SOCKS_AUTH_NONE,
  191. * which skips the authentication step.
  192. */
  193. uint8_t auth_method;
  194. };
  195. /**
  196. * Client socks request in Socks5 protocol.
  197. */
  198. struct Socks5ClientRequestMessage
  199. {
  200. /**
  201. * Should be #SOCKS_VERSION_5.
  202. */
  203. uint8_t version;
  204. /**
  205. * Command code, we only uspport #SOCKS5_CMD_TCP_STREAM.
  206. */
  207. uint8_t command;
  208. /**
  209. * Reserved, always zero.
  210. */
  211. uint8_t resvd;
  212. /**
  213. * Address type, an `enum Socks5AddressType`.
  214. */
  215. uint8_t addr_type;
  216. /*
  217. * Followed by either an ip4/ipv6 address or a domain name with a
  218. * length field (uint8_t) in front (depending on @e addr_type).
  219. * followed by port number in network byte order (uint16_t).
  220. */
  221. };
  222. /**
  223. * Server response to client requests in Socks5 protocol.
  224. */
  225. struct Socks5ServerResponseMessage
  226. {
  227. /**
  228. * Should be #SOCKS_VERSION_5.
  229. */
  230. uint8_t version;
  231. /**
  232. * Status code, an `enum Socks5StatusCode`
  233. */
  234. uint8_t reply;
  235. /**
  236. * Always zero.
  237. */
  238. uint8_t reserved;
  239. /**
  240. * Address type, an `enum Socks5AddressType`.
  241. */
  242. uint8_t addr_type;
  243. /*
  244. * Followed by either an ip4/ipv6 address or a domain name with a
  245. * length field (uint8_t) in front (depending on @e addr_type).
  246. * followed by port number in network byte order (uint16_t).
  247. */
  248. };
  249. /* *********************** Datastructures for HTTP handling ****************** */
  250. /**
  251. * A structure for CA cert/key
  252. */
  253. struct ProxyCA
  254. {
  255. /**
  256. * The certificate
  257. */
  258. gnutls_x509_crt_t cert;
  259. /**
  260. * The private key
  261. */
  262. gnutls_x509_privkey_t key;
  263. };
  264. /**
  265. * Structure for GNS certificates
  266. */
  267. struct ProxyGNSCertificate
  268. {
  269. /**
  270. * The certificate as PEM
  271. */
  272. char cert[MAX_PEM_SIZE];
  273. /**
  274. * The private key as PEM
  275. */
  276. char key[MAX_PEM_SIZE];
  277. };
  278. /**
  279. * A structure for all running Httpds
  280. */
  281. struct MhdHttpList
  282. {
  283. /**
  284. * DLL for httpds
  285. */
  286. struct MhdHttpList *prev;
  287. /**
  288. * DLL for httpds
  289. */
  290. struct MhdHttpList *next;
  291. /**
  292. * the domain name to server (only important for TLS)
  293. */
  294. char *domain;
  295. /**
  296. * The daemon handle
  297. */
  298. struct MHD_Daemon *daemon;
  299. /**
  300. * Optional proxy certificate used
  301. */
  302. struct ProxyGNSCertificate *proxy_cert;
  303. /**
  304. * The task ID
  305. */
  306. struct GNUNET_SCHEDULER_Task *httpd_task;
  307. /**
  308. * is this an ssl daemon?
  309. */
  310. int is_ssl;
  311. };
  312. /* ***************** Datastructures for Socks handling **************** */
  313. /**
  314. * The socks phases.
  315. */
  316. enum SocksPhase
  317. {
  318. /**
  319. * We're waiting to get the client hello.
  320. */
  321. SOCKS5_INIT,
  322. /**
  323. * We're waiting to get the initial request.
  324. */
  325. SOCKS5_REQUEST,
  326. /**
  327. * We are currently resolving the destination.
  328. */
  329. SOCKS5_RESOLVING,
  330. /**
  331. * We're in transfer mode.
  332. */
  333. SOCKS5_DATA_TRANSFER,
  334. /**
  335. * Finish writing the write buffer, then clean up.
  336. */
  337. SOCKS5_WRITE_THEN_CLEANUP,
  338. /**
  339. * Socket has been passed to MHD, do not close it anymore.
  340. */
  341. SOCKS5_SOCKET_WITH_MHD,
  342. /**
  343. * We've started receiving upload data from MHD.
  344. */
  345. SOCKS5_SOCKET_UPLOAD_STARTED,
  346. /**
  347. * We've finished receiving upload data from MHD.
  348. */
  349. SOCKS5_SOCKET_UPLOAD_DONE,
  350. /**
  351. * We've finished uploading data via CURL and can now download.
  352. */
  353. SOCKS5_SOCKET_DOWNLOAD_STARTED,
  354. /**
  355. * We've finished receiving download data from cURL.
  356. */
  357. SOCKS5_SOCKET_DOWNLOAD_DONE
  358. };
  359. /**
  360. * A header list
  361. */
  362. struct HttpResponseHeader
  363. {
  364. /**
  365. * DLL
  366. */
  367. struct HttpResponseHeader *next;
  368. /**
  369. * DLL
  370. */
  371. struct HttpResponseHeader *prev;
  372. /**
  373. * Header type
  374. */
  375. char *type;
  376. /**
  377. * Header value
  378. */
  379. char *value;
  380. };
  381. /**
  382. * A structure for socks requests
  383. */
  384. struct Socks5Request
  385. {
  386. /**
  387. * DLL.
  388. */
  389. struct Socks5Request *next;
  390. /**
  391. * DLL.
  392. */
  393. struct Socks5Request *prev;
  394. /**
  395. * The client socket
  396. */
  397. struct GNUNET_NETWORK_Handle *sock;
  398. /**
  399. * Handle to GNS lookup, during #SOCKS5_RESOLVING phase.
  400. */
  401. struct GNUNET_GNS_LookupWithTldRequest *gns_lookup;
  402. /**
  403. * Client socket read task
  404. */
  405. struct GNUNET_SCHEDULER_Task *rtask;
  406. /**
  407. * Client socket write task
  408. */
  409. struct GNUNET_SCHEDULER_Task *wtask;
  410. /**
  411. * Timeout task
  412. */
  413. struct GNUNET_SCHEDULER_Task *timeout_task;
  414. /**
  415. * Read buffer
  416. */
  417. char rbuf[SOCKS_BUFFERSIZE];
  418. /**
  419. * Write buffer
  420. */
  421. char wbuf[SOCKS_BUFFERSIZE];
  422. /**
  423. * Buffer we use for moving data between MHD and curl (in both directions).
  424. */
  425. char io_buf[IO_BUFFERSIZE];
  426. /**
  427. * MHD HTTP instance handling this request, NULL for none.
  428. */
  429. struct MhdHttpList *hd;
  430. /**
  431. * MHD connection for this request.
  432. */
  433. struct MHD_Connection *con;
  434. /**
  435. * MHD response object for this request.
  436. */
  437. struct MHD_Response *response;
  438. /**
  439. * the domain name to server (only important for TLS)
  440. */
  441. char *domain;
  442. /**
  443. * DNS Legacy Host Name as given by GNS, NULL if not given.
  444. */
  445. char *leho;
  446. /**
  447. * Payload of the DANE records encountered.
  448. */
  449. char *dane_data[MAX_DANES + 1];
  450. /**
  451. * The URL to fetch
  452. */
  453. char *url;
  454. /**
  455. * Handle to cURL
  456. */
  457. CURL *curl;
  458. /**
  459. * HTTP request headers for the curl request.
  460. */
  461. struct curl_slist *headers;
  462. /**
  463. * DNS->IP mappings resolved through GNS
  464. */
  465. struct curl_slist *hosts;
  466. /**
  467. * HTTP response code to give to MHD for the response.
  468. */
  469. unsigned int response_code;
  470. /**
  471. * Number of bytes in @e dane_data.
  472. */
  473. int dane_data_len[MAX_DANES + 1];
  474. /**
  475. * Number of entries used in @e dane_data_len
  476. * and @e dane_data.
  477. */
  478. unsigned int num_danes;
  479. /**
  480. * Number of bytes already in read buffer
  481. */
  482. size_t rbuf_len;
  483. /**
  484. * Number of bytes already in write buffer
  485. */
  486. size_t wbuf_len;
  487. /**
  488. * Number of bytes already in the IO buffer.
  489. */
  490. size_t io_len;
  491. /**
  492. * Once known, what's the target address for the connection?
  493. */
  494. struct sockaddr_storage destination_address;
  495. /**
  496. * The socks state
  497. */
  498. enum SocksPhase state;
  499. /**
  500. * Desired destination port.
  501. */
  502. uint16_t port;
  503. /**
  504. * Headers from response
  505. */
  506. struct HttpResponseHeader *header_head;
  507. /**
  508. * Headers from response
  509. */
  510. struct HttpResponseHeader *header_tail;
  511. /**
  512. * X.509 Certificate status
  513. */
  514. int ssl_checked;
  515. /**
  516. * Was the hostname resolved via GNS?
  517. */
  518. int is_gns;
  519. /**
  520. * This is (probably) a TLS connection
  521. */
  522. int is_tls;
  523. /**
  524. * Did we suspend MHD processing?
  525. */
  526. int suspended;
  527. /**
  528. * Did we pause CURL processing?
  529. */
  530. int curl_paused;
  531. };
  532. /* *********************** Globals **************************** */
  533. /**
  534. * The address to bind to
  535. */
  536. static in_addr_t address;
  537. /**
  538. * The IPv6 address to bind to
  539. */
  540. static struct in6_addr address6;
  541. /**
  542. * The port the proxy is running on (default 7777)
  543. */
  544. static uint16_t port = GNUNET_GNS_PROXY_PORT;
  545. /**
  546. * The CA file (pem) to use for the proxy CA
  547. */
  548. static char *cafile_opt;
  549. /**
  550. * The listen socket of the proxy for IPv4
  551. */
  552. static struct GNUNET_NETWORK_Handle *lsock4;
  553. /**
  554. * The listen socket of the proxy for IPv6
  555. */
  556. static struct GNUNET_NETWORK_Handle *lsock6;
  557. /**
  558. * The listen task ID for IPv4
  559. */
  560. static struct GNUNET_SCHEDULER_Task *ltask4;
  561. /**
  562. * The listen task ID for IPv6
  563. */
  564. static struct GNUNET_SCHEDULER_Task *ltask6;
  565. /**
  566. * The cURL download task (curl multi API).
  567. */
  568. static struct GNUNET_SCHEDULER_Task *curl_download_task;
  569. /**
  570. * The cURL multi handle
  571. */
  572. static CURLM *curl_multi;
  573. /**
  574. * Handle to the GNS service
  575. */
  576. static struct GNUNET_GNS_Handle *gns_handle;
  577. /**
  578. * Disable IPv6.
  579. */
  580. static int disable_v6;
  581. /**
  582. * DLL for http/https daemons
  583. */
  584. static struct MhdHttpList *mhd_httpd_head;
  585. /**
  586. * DLL for http/https daemons
  587. */
  588. static struct MhdHttpList *mhd_httpd_tail;
  589. /**
  590. * Daemon for HTTP (we have one per X.509 certificate, and then one for
  591. * all HTTP connections; this is the one for HTTP, not HTTPS).
  592. */
  593. static struct MhdHttpList *httpd;
  594. /**
  595. * DLL of active socks requests.
  596. */
  597. static struct Socks5Request *s5r_head;
  598. /**
  599. * DLL of active socks requests.
  600. */
  601. static struct Socks5Request *s5r_tail;
  602. /**
  603. * The CA for X.509 certificate generation
  604. */
  605. static struct ProxyCA proxy_ca;
  606. /**
  607. * Response we return on cURL failures.
  608. */
  609. static struct MHD_Response *curl_failure_response;
  610. /**
  611. * Our configuration.
  612. */
  613. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  614. /* ************************* Global helpers ********************* */
  615. /**
  616. * Run MHD now, we have extra data ready for the callback.
  617. *
  618. * @param hd the daemon to run now.
  619. */
  620. static void
  621. run_mhd_now (struct MhdHttpList *hd);
  622. /**
  623. * Clean up s5r handles.
  624. *
  625. * @param s5r the handle to destroy
  626. */
  627. static void
  628. cleanup_s5r (struct Socks5Request *s5r)
  629. {
  630. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  631. "Cleaning up socks request\n");
  632. if (NULL != s5r->curl)
  633. {
  634. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  635. "Cleaning up cURL handle\n");
  636. curl_multi_remove_handle (curl_multi,
  637. s5r->curl);
  638. curl_easy_cleanup (s5r->curl);
  639. s5r->curl = NULL;
  640. }
  641. if (s5r->suspended)
  642. {
  643. s5r->suspended = GNUNET_NO;
  644. MHD_resume_connection (s5r->con);
  645. }
  646. curl_slist_free_all (s5r->headers);
  647. if (NULL != s5r->hosts)
  648. {
  649. curl_slist_free_all (s5r->hosts);
  650. }
  651. if ((NULL != s5r->response) &&
  652. (curl_failure_response != s5r->response))
  653. {
  654. MHD_destroy_response (s5r->response);
  655. s5r->response = NULL;
  656. }
  657. if (NULL != s5r->rtask)
  658. {
  659. GNUNET_SCHEDULER_cancel (s5r->rtask);
  660. s5r->rtask = NULL;
  661. }
  662. if (NULL != s5r->timeout_task)
  663. {
  664. GNUNET_SCHEDULER_cancel (s5r->timeout_task);
  665. s5r->timeout_task = NULL;
  666. }
  667. if (NULL != s5r->wtask)
  668. {
  669. GNUNET_SCHEDULER_cancel (s5r->wtask);
  670. s5r->wtask = NULL;
  671. }
  672. if (NULL != s5r->gns_lookup)
  673. {
  674. GNUNET_GNS_lookup_with_tld_cancel (s5r->gns_lookup);
  675. s5r->gns_lookup = NULL;
  676. }
  677. if (NULL != s5r->sock)
  678. {
  679. if (SOCKS5_SOCKET_WITH_MHD <= s5r->state)
  680. GNUNET_NETWORK_socket_free_memory_only_ (s5r->sock);
  681. else
  682. GNUNET_NETWORK_socket_close (s5r->sock);
  683. s5r->sock = NULL;
  684. }
  685. GNUNET_CONTAINER_DLL_remove (s5r_head,
  686. s5r_tail,
  687. s5r);
  688. GNUNET_free (s5r->domain);
  689. GNUNET_free (s5r->leho);
  690. GNUNET_free (s5r->url);
  691. for (unsigned int i = 0; i < s5r->num_danes; i++)
  692. GNUNET_free (s5r->dane_data[i]);
  693. GNUNET_free (s5r);
  694. }
  695. /* ************************* HTTP handling with cURL *********************** */
  696. static void
  697. curl_download_prepare ();
  698. /**
  699. * Callback for MHD response generation. This function is called from
  700. * MHD whenever MHD expects to get data back. Copies data from the
  701. * io_buf, if available.
  702. *
  703. * @param cls closure with our `struct Socks5Request`
  704. * @param pos in buffer
  705. * @param buf where to copy data
  706. * @param max available space in @a buf
  707. * @return number of bytes written to @a buf
  708. */
  709. static ssize_t
  710. mhd_content_cb (void *cls,
  711. uint64_t pos,
  712. char*buf,
  713. size_t max)
  714. {
  715. struct Socks5Request *s5r = cls;
  716. size_t bytes_to_copy;
  717. if ((SOCKS5_SOCKET_UPLOAD_STARTED == s5r->state) ||
  718. (SOCKS5_SOCKET_UPLOAD_DONE == s5r->state))
  719. {
  720. /* we're still not done with the upload, do not yet
  721. start the download, the IO buffer is still full
  722. with upload data. */
  723. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  724. "Pausing MHD download %s%s, not yet ready for download\n",
  725. s5r->domain,
  726. s5r->url);
  727. return 0; /* not yet ready for data download */
  728. }
  729. bytes_to_copy = GNUNET_MIN (max,
  730. s5r->io_len);
  731. if ((0 == bytes_to_copy) &&
  732. (SOCKS5_SOCKET_DOWNLOAD_DONE != s5r->state))
  733. {
  734. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  735. "Pausing MHD download %s%s, no data available\n",
  736. s5r->domain,
  737. s5r->url);
  738. if (NULL != s5r->curl)
  739. {
  740. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  741. "Continuing CURL interaction for %s%s\n",
  742. s5r->domain,
  743. s5r->url);
  744. if (GNUNET_YES == s5r->curl_paused)
  745. {
  746. s5r->curl_paused = GNUNET_NO;
  747. curl_easy_pause (s5r->curl,
  748. CURLPAUSE_CONT);
  749. }
  750. curl_download_prepare ();
  751. }
  752. if (GNUNET_NO == s5r->suspended)
  753. {
  754. MHD_suspend_connection (s5r->con);
  755. s5r->suspended = GNUNET_YES;
  756. }
  757. return 0; /* more data later */
  758. }
  759. if ((0 == bytes_to_copy) &&
  760. (SOCKS5_SOCKET_DOWNLOAD_DONE == s5r->state))
  761. {
  762. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  763. "Completed MHD download %s%s\n",
  764. s5r->domain,
  765. s5r->url);
  766. return MHD_CONTENT_READER_END_OF_STREAM;
  767. }
  768. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  769. "Writing %llu/%llu bytes to %s%s\n",
  770. (unsigned long long) bytes_to_copy,
  771. (unsigned long long) s5r->io_len,
  772. s5r->domain,
  773. s5r->url);
  774. GNUNET_memcpy (buf,
  775. s5r->io_buf,
  776. bytes_to_copy);
  777. memmove (s5r->io_buf,
  778. &s5r->io_buf[bytes_to_copy],
  779. s5r->io_len - bytes_to_copy);
  780. s5r->io_len -= bytes_to_copy;
  781. if ((NULL != s5r->curl) &&
  782. (GNUNET_YES == s5r->curl_paused))
  783. {
  784. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  785. "Continuing CURL interaction for %s%s\n",
  786. s5r->domain,
  787. s5r->url);
  788. s5r->curl_paused = GNUNET_NO;
  789. curl_easy_pause (s5r->curl,
  790. CURLPAUSE_CONT);
  791. }
  792. return bytes_to_copy;
  793. }
  794. /**
  795. * Check that the website has presented us with a valid X.509 certificate.
  796. * The certificate must either match the domain name or the LEHO name
  797. * (or, if available, the TLSA record).
  798. *
  799. * @param s5r request to check for.
  800. * @return #GNUNET_OK if the certificate is valid
  801. */
  802. static int
  803. check_ssl_certificate (struct Socks5Request *s5r)
  804. {
  805. unsigned int cert_list_size;
  806. const gnutls_datum_t *chainp;
  807. const struct curl_tlssessioninfo *tlsinfo;
  808. char certdn[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 3];
  809. size_t size;
  810. gnutls_x509_crt_t x509_cert;
  811. int rc;
  812. const char *name;
  813. s5r->ssl_checked = GNUNET_YES;
  814. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  815. "Checking X.509 certificate\n");
  816. if (CURLE_OK !=
  817. curl_easy_getinfo (s5r->curl,
  818. CURLINFO_TLS_SESSION,
  819. &tlsinfo))
  820. return GNUNET_SYSERR;
  821. if (CURLSSLBACKEND_GNUTLS != tlsinfo->backend)
  822. {
  823. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  824. _ ("Unsupported CURL TLS backend %d\n"),
  825. tlsinfo->backend);
  826. return GNUNET_SYSERR;
  827. }
  828. chainp = gnutls_certificate_get_peers (tlsinfo->internals,
  829. &cert_list_size);
  830. if ((! chainp) ||
  831. (0 == cert_list_size))
  832. return GNUNET_SYSERR;
  833. size = sizeof(certdn);
  834. /* initialize an X.509 certificate structure. */
  835. gnutls_x509_crt_init (&x509_cert);
  836. gnutls_x509_crt_import (x509_cert,
  837. chainp,
  838. GNUTLS_X509_FMT_DER);
  839. if (0 != (rc = gnutls_x509_crt_get_dn_by_oid (x509_cert,
  840. GNUTLS_OID_X520_COMMON_NAME,
  841. 0, /* the first and only one */
  842. 0 /* no DER encoding */,
  843. certdn,
  844. &size)))
  845. {
  846. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  847. _ ("Failed to fetch CN from cert: %s\n"),
  848. gnutls_strerror (rc));
  849. gnutls_x509_crt_deinit (x509_cert);
  850. return GNUNET_SYSERR;
  851. }
  852. /* check for TLSA/DANE records */
  853. #if HAVE_GNUTLS_DANE
  854. if (0 != s5r->num_danes)
  855. {
  856. dane_state_t dane_state;
  857. dane_query_t dane_query;
  858. unsigned int verify;
  859. /* FIXME: add flags to gnutls to NOT read UNBOUND_ROOT_KEY_FILE here! */
  860. if (0 != (rc = dane_state_init (&dane_state,
  861. #ifdef DANE_F_IGNORE_DNSSEC
  862. DANE_F_IGNORE_DNSSEC |
  863. #endif
  864. DANE_F_IGNORE_LOCAL_RESOLVER)))
  865. {
  866. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  867. _ ("Failed to initialize DANE: %s\n"),
  868. dane_strerror (rc));
  869. gnutls_x509_crt_deinit (x509_cert);
  870. return GNUNET_SYSERR;
  871. }
  872. s5r->dane_data[s5r->num_danes] = NULL;
  873. s5r->dane_data_len[s5r->num_danes] = 0;
  874. if (0 != (rc = dane_raw_tlsa (dane_state,
  875. &dane_query,
  876. s5r->dane_data,
  877. s5r->dane_data_len,
  878. GNUNET_YES,
  879. GNUNET_NO)))
  880. {
  881. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  882. _ ("Failed to parse DANE record: %s\n"),
  883. dane_strerror (rc));
  884. dane_state_deinit (dane_state);
  885. gnutls_x509_crt_deinit (x509_cert);
  886. return GNUNET_SYSERR;
  887. }
  888. if (0 != (rc = dane_verify_crt_raw (dane_state,
  889. chainp,
  890. cert_list_size,
  891. gnutls_certificate_type_get (
  892. tlsinfo->internals),
  893. dane_query,
  894. 0, 0,
  895. &verify)))
  896. {
  897. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  898. _ ("Failed to verify TLS connection using DANE: %s\n"),
  899. dane_strerror (rc));
  900. dane_query_deinit (dane_query);
  901. dane_state_deinit (dane_state);
  902. gnutls_x509_crt_deinit (x509_cert);
  903. return GNUNET_SYSERR;
  904. }
  905. if (0 != verify)
  906. {
  907. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  908. _ (
  909. "Failed DANE verification failed with GnuTLS verify status code: %u\n"),
  910. verify);
  911. dane_query_deinit (dane_query);
  912. dane_state_deinit (dane_state);
  913. gnutls_x509_crt_deinit (x509_cert);
  914. return GNUNET_SYSERR;
  915. }
  916. dane_query_deinit (dane_query);
  917. dane_state_deinit (dane_state);
  918. /* success! */
  919. }
  920. else
  921. #endif
  922. {
  923. /* try LEHO or ordinary domain name X509 verification */
  924. name = s5r->domain;
  925. if (NULL != s5r->leho)
  926. name = s5r->leho;
  927. if (NULL != name)
  928. {
  929. if (0 == (rc = gnutls_x509_crt_check_hostname (x509_cert,
  930. name)))
  931. {
  932. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  933. _ (
  934. "TLS certificate subject name (%s) does not match `%s': %d\n"),
  935. certdn,
  936. name,
  937. rc);
  938. gnutls_x509_crt_deinit (x509_cert);
  939. return GNUNET_SYSERR;
  940. }
  941. }
  942. else
  943. {
  944. /* we did not even have the domain name!? */
  945. GNUNET_break (0);
  946. return GNUNET_SYSERR;
  947. }
  948. }
  949. gnutls_x509_crt_deinit (x509_cert);
  950. return GNUNET_OK;
  951. }
  952. /**
  953. * We're getting an HTTP response header from cURL. Convert it to the
  954. * MHD response headers. Mostly copies the headers, but makes special
  955. * adjustments to "Set-Cookie" and "Location" headers as those may need
  956. * to be changed from the LEHO to the domain the browser expects.
  957. *
  958. * @param buffer curl buffer with a single line of header data; not 0-terminated!
  959. * @param size curl blocksize
  960. * @param nmemb curl blocknumber
  961. * @param cls our `struct Socks5Request *`
  962. * @return size of processed bytes
  963. */
  964. static size_t
  965. curl_check_hdr (void *buffer,
  966. size_t size,
  967. size_t nmemb,
  968. void *cls)
  969. {
  970. struct Socks5Request *s5r = cls;
  971. struct HttpResponseHeader *header;
  972. size_t bytes = size * nmemb;
  973. char *ndup;
  974. const char *hdr_type;
  975. const char *cookie_domain;
  976. char *hdr_val;
  977. char *new_cookie_hdr;
  978. char *new_location;
  979. size_t offset;
  980. size_t delta_cdomain;
  981. int domain_matched;
  982. char *tok;
  983. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  984. "Receiving HTTP response header from CURL\n");
  985. /* first, check TLS certificate */
  986. if ((GNUNET_YES != s5r->ssl_checked) &&
  987. (GNUNET_YES == s5r->is_tls))
  988. // (HTTPS_PORT == s5r->port))
  989. {
  990. if (GNUNET_OK != check_ssl_certificate (s5r))
  991. return 0;
  992. }
  993. ndup = GNUNET_strndup (buffer,
  994. bytes);
  995. hdr_type = strtok (ndup,
  996. ":");
  997. if (NULL == hdr_type)
  998. {
  999. GNUNET_free (ndup);
  1000. return bytes;
  1001. }
  1002. hdr_val = strtok (NULL,
  1003. "");
  1004. if (NULL == hdr_val)
  1005. {
  1006. GNUNET_free (ndup);
  1007. return bytes;
  1008. }
  1009. if (' ' == *hdr_val)
  1010. hdr_val++;
  1011. /* custom logic for certain header types */
  1012. new_cookie_hdr = NULL;
  1013. if ((NULL != s5r->leho) &&
  1014. (0 == strcasecmp (hdr_type,
  1015. MHD_HTTP_HEADER_SET_COOKIE)))
  1016. {
  1017. new_cookie_hdr = GNUNET_malloc (strlen (hdr_val)
  1018. + strlen (s5r->domain) + 1);
  1019. offset = 0;
  1020. domain_matched = GNUNET_NO; /* make sure we match domain at most once */
  1021. for (tok = strtok (hdr_val, ";"); NULL != tok; tok = strtok (NULL, ";"))
  1022. {
  1023. if ((0 == strncasecmp (tok,
  1024. " domain",
  1025. strlen (" domain"))) &&
  1026. (GNUNET_NO == domain_matched))
  1027. {
  1028. domain_matched = GNUNET_YES;
  1029. cookie_domain = tok + strlen (" domain") + 1;
  1030. if (strlen (cookie_domain) < strlen (s5r->leho))
  1031. {
  1032. delta_cdomain = strlen (s5r->leho) - strlen (cookie_domain);
  1033. if (0 == strcasecmp (cookie_domain,
  1034. s5r->leho + delta_cdomain))
  1035. {
  1036. offset += sprintf (new_cookie_hdr + offset,
  1037. " domain=%s;",
  1038. s5r->domain);
  1039. continue;
  1040. }
  1041. }
  1042. else if (0 == strcmp (cookie_domain,
  1043. s5r->leho))
  1044. {
  1045. offset += sprintf (new_cookie_hdr + offset,
  1046. " domain=%s;",
  1047. s5r->domain);
  1048. continue;
  1049. }
  1050. else if (('.' == cookie_domain[0]) &&
  1051. (0 == strcmp (&cookie_domain[1],
  1052. s5r->leho)))
  1053. {
  1054. offset += sprintf (new_cookie_hdr + offset,
  1055. " domain=.%s;",
  1056. s5r->domain);
  1057. continue;
  1058. }
  1059. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1060. _ ("Cookie domain `%s' supplied by server is invalid\n"),
  1061. tok);
  1062. }
  1063. GNUNET_memcpy (new_cookie_hdr + offset,
  1064. tok,
  1065. strlen (tok));
  1066. offset += strlen (tok);
  1067. new_cookie_hdr[offset++] = ';';
  1068. }
  1069. hdr_val = new_cookie_hdr;
  1070. }
  1071. new_location = NULL;
  1072. if (0 == strcasecmp (MHD_HTTP_HEADER_TRANSFER_ENCODING,
  1073. hdr_type))
  1074. {
  1075. /* Ignore transfer encoding, set automatically by MHD if required */
  1076. goto cleanup;
  1077. }
  1078. if ((0 == strcasecmp (MHD_HTTP_HEADER_LOCATION,
  1079. hdr_type)))
  1080. {
  1081. char *leho_host;
  1082. GNUNET_asprintf (&leho_host,
  1083. (GNUNET_YES != s5r->is_tls) // (HTTPS_PORT != s5r->port)
  1084. ? "http://%s"
  1085. : "https://%s",
  1086. s5r->leho);
  1087. if (0 == strncmp (leho_host,
  1088. hdr_val,
  1089. strlen (leho_host)))
  1090. {
  1091. GNUNET_asprintf (&new_location,
  1092. "%s%s%s",
  1093. (GNUNET_YES != s5r->is_tls) // (HTTPS_PORT != s5r->port)
  1094. ? "http://"
  1095. : "https://",
  1096. s5r->domain,
  1097. hdr_val + strlen (leho_host));
  1098. hdr_val = new_location;
  1099. }
  1100. GNUNET_free (leho_host);
  1101. }
  1102. if (0 == strcasecmp (MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN,
  1103. hdr_type))
  1104. {
  1105. char *leho_host;
  1106. GNUNET_asprintf (&leho_host,
  1107. (GNUNET_YES != s5r->is_tls) // (HTTPS_PORT != s5r->port)
  1108. ? "http://%s"
  1109. : "https://%s",
  1110. s5r->leho);
  1111. if (0 == strncmp (leho_host,
  1112. hdr_val,
  1113. strlen (leho_host)))
  1114. {
  1115. GNUNET_asprintf (&new_location,
  1116. "%s%s",
  1117. (GNUNET_YES != s5r->is_tls) // (HTTPS_PORT != s5r->port)
  1118. ? "http://"
  1119. : "https://",
  1120. s5r->domain);
  1121. hdr_val = new_location;
  1122. }
  1123. GNUNET_free (leho_host);
  1124. }
  1125. /* MHD does not allow certain characters in values, remove those */
  1126. if (NULL != (tok = strchr (hdr_val, '\n')))
  1127. *tok = '\0';
  1128. if (NULL != (tok = strchr (hdr_val, '\r')))
  1129. *tok = '\0';
  1130. if (NULL != (tok = strchr (hdr_val, '\t')))
  1131. *tok = '\0';
  1132. if (0 != strlen (hdr_val)) /* Rely in MHD to set those */
  1133. {
  1134. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1135. "Adding header %s: %s to MHD response\n",
  1136. hdr_type,
  1137. hdr_val);
  1138. header = GNUNET_new (struct HttpResponseHeader);
  1139. header->type = GNUNET_strdup (hdr_type);
  1140. header->value = GNUNET_strdup (hdr_val);
  1141. GNUNET_CONTAINER_DLL_insert (s5r->header_head,
  1142. s5r->header_tail,
  1143. header);
  1144. }
  1145. cleanup:
  1146. GNUNET_free (ndup);
  1147. GNUNET_free (new_cookie_hdr);
  1148. GNUNET_free (new_location);
  1149. return bytes;
  1150. }
  1151. /**
  1152. * Create an MHD response object in @a s5r matching the
  1153. * information we got from curl.
  1154. *
  1155. * @param s5r the request for which we convert the response
  1156. * @return #GNUNET_OK on success, #GNUNET_SYSERR if response was
  1157. * already initialized before
  1158. */
  1159. static int
  1160. create_mhd_response_from_s5r (struct Socks5Request *s5r)
  1161. {
  1162. long resp_code;
  1163. double content_length;
  1164. if (NULL != s5r->response)
  1165. {
  1166. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1167. "Response already set!\n");
  1168. return GNUNET_SYSERR;
  1169. }
  1170. GNUNET_break (CURLE_OK ==
  1171. curl_easy_getinfo (s5r->curl,
  1172. CURLINFO_RESPONSE_CODE,
  1173. &resp_code));
  1174. GNUNET_break (CURLE_OK ==
  1175. curl_easy_getinfo (s5r->curl,
  1176. CURLINFO_CONTENT_LENGTH_DOWNLOAD,
  1177. &content_length));
  1178. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1179. "Creating MHD response with code %d and size %d for %s%s\n",
  1180. (int) resp_code,
  1181. (int) content_length,
  1182. s5r->domain,
  1183. s5r->url);
  1184. s5r->response_code = resp_code;
  1185. s5r->response = MHD_create_response_from_callback ((-1 == content_length)
  1186. ? MHD_SIZE_UNKNOWN
  1187. : content_length,
  1188. IO_BUFFERSIZE,
  1189. &mhd_content_cb,
  1190. s5r,
  1191. NULL);
  1192. for (struct HttpResponseHeader *header = s5r->header_head;
  1193. NULL != header;
  1194. header = header->next)
  1195. {
  1196. if (0 == strcasecmp (header->type,
  1197. MHD_HTTP_HEADER_CONTENT_LENGTH))
  1198. continue; /* MHD won't let us mess with those, for good reason */
  1199. if ((0 == strcasecmp (header->type,
  1200. MHD_HTTP_HEADER_TRANSFER_ENCODING)) &&
  1201. ((0 == strcasecmp (header->value,
  1202. "identity")) ||
  1203. (0 == strcasecmp (header->value,
  1204. "chunked"))))
  1205. continue; /* MHD won't let us mess with those, for good reason */
  1206. if (MHD_YES !=
  1207. MHD_add_response_header (s5r->response,
  1208. header->type,
  1209. header->value))
  1210. {
  1211. GNUNET_break (0);
  1212. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1213. "Failed to add header `%s:%s'\n",
  1214. header->type,
  1215. header->value);
  1216. }
  1217. }
  1218. /* force connection to be closed after each request, as we
  1219. do not support HTTP pipelining (yet, FIXME!) */
  1220. /*GNUNET_break (MHD_YES ==
  1221. MHD_add_response_header (s5r->response,
  1222. MHD_HTTP_HEADER_CONNECTION,
  1223. "close"));*/
  1224. MHD_resume_connection (s5r->con);
  1225. s5r->suspended = GNUNET_NO;
  1226. return GNUNET_OK;
  1227. }
  1228. /**
  1229. * Handle response payload data from cURL. Copies it into our `io_buf` to make
  1230. * it available to MHD.
  1231. *
  1232. * @param ptr pointer to the data
  1233. * @param size number of blocks of data
  1234. * @param nmemb blocksize
  1235. * @param ctx our `struct Socks5Request *`
  1236. * @return number of bytes handled
  1237. */
  1238. static size_t
  1239. curl_download_cb (void *ptr,
  1240. size_t size,
  1241. size_t nmemb,
  1242. void*ctx)
  1243. {
  1244. struct Socks5Request *s5r = ctx;
  1245. size_t total = size * nmemb;
  1246. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1247. "Receiving %ux%u bytes for `%s%s' from cURL to download\n",
  1248. (unsigned int) size,
  1249. (unsigned int) nmemb,
  1250. s5r->domain,
  1251. s5r->url);
  1252. if (NULL == s5r->response)
  1253. GNUNET_assert (GNUNET_OK ==
  1254. create_mhd_response_from_s5r (s5r));
  1255. if ((SOCKS5_SOCKET_UPLOAD_DONE == s5r->state) &&
  1256. (0 == s5r->io_len))
  1257. {
  1258. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1259. "Previous upload finished... starting DOWNLOAD.\n");
  1260. s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED;
  1261. }
  1262. if ((SOCKS5_SOCKET_UPLOAD_STARTED == s5r->state) ||
  1263. (SOCKS5_SOCKET_UPLOAD_DONE == s5r->state))
  1264. {
  1265. /* we're still not done with the upload, do not yet
  1266. start the download, the IO buffer is still full
  1267. with upload data. */
  1268. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1269. "Pausing CURL download `%s%s', waiting for UPLOAD to finish\n",
  1270. s5r->domain,
  1271. s5r->url);
  1272. s5r->curl_paused = GNUNET_YES;
  1273. return CURL_WRITEFUNC_PAUSE; /* not yet ready for data download */
  1274. }
  1275. if (sizeof(s5r->io_buf) - s5r->io_len < total)
  1276. {
  1277. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1278. "Pausing CURL `%s%s' download, not enough space %llu %llu %llu\n",
  1279. s5r->domain,
  1280. s5r->url,
  1281. (unsigned long long) sizeof(s5r->io_buf),
  1282. (unsigned long long) s5r->io_len,
  1283. (unsigned long long) total);
  1284. s5r->curl_paused = GNUNET_YES;
  1285. return CURL_WRITEFUNC_PAUSE; /* not enough space */
  1286. }
  1287. GNUNET_memcpy (&s5r->io_buf[s5r->io_len],
  1288. ptr,
  1289. total);
  1290. s5r->io_len += total;
  1291. if (GNUNET_YES == s5r->suspended)
  1292. {
  1293. MHD_resume_connection (s5r->con);
  1294. s5r->suspended = GNUNET_NO;
  1295. }
  1296. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1297. "Received %llu bytes of payload via cURL from %s\n",
  1298. (unsigned long long) total,
  1299. s5r->domain);
  1300. if (s5r->io_len == total)
  1301. run_mhd_now (s5r->hd);
  1302. return total;
  1303. }
  1304. /**
  1305. * cURL callback for uploaded (PUT/POST) data. Copies it into our `io_buf`
  1306. * to make it available to MHD.
  1307. *
  1308. * @param buf where to write the data
  1309. * @param size number of bytes per member
  1310. * @param nmemb number of members available in @a buf
  1311. * @param cls our `struct Socks5Request` that generated the data
  1312. * @return number of bytes copied to @a buf
  1313. */
  1314. static size_t
  1315. curl_upload_cb (void *buf,
  1316. size_t size,
  1317. size_t nmemb,
  1318. void *cls)
  1319. {
  1320. struct Socks5Request *s5r = cls;
  1321. size_t len = size * nmemb;
  1322. size_t to_copy;
  1323. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1324. "Receiving %ux%u bytes for `%s%s' from cURL to upload\n",
  1325. (unsigned int) size,
  1326. (unsigned int) nmemb,
  1327. s5r->domain,
  1328. s5r->url);
  1329. if ((0 == s5r->io_len) &&
  1330. (SOCKS5_SOCKET_UPLOAD_DONE != s5r->state))
  1331. {
  1332. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1333. "Pausing CURL UPLOAD %s%s, need more data\n",
  1334. s5r->domain,
  1335. s5r->url);
  1336. return CURL_READFUNC_PAUSE;
  1337. }
  1338. if ((0 == s5r->io_len) &&
  1339. (SOCKS5_SOCKET_UPLOAD_DONE == s5r->state))
  1340. {
  1341. s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED;
  1342. if (GNUNET_YES == s5r->curl_paused)
  1343. {
  1344. s5r->curl_paused = GNUNET_NO;
  1345. curl_easy_pause (s5r->curl,
  1346. CURLPAUSE_CONT);
  1347. }
  1348. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1349. "Completed CURL UPLOAD %s%s\n",
  1350. s5r->domain,
  1351. s5r->url);
  1352. return 0; /* upload finished, can now download */
  1353. }
  1354. if ((SOCKS5_SOCKET_UPLOAD_STARTED != s5r->state) &&
  1355. (SOCKS5_SOCKET_UPLOAD_DONE != s5r->state))
  1356. {
  1357. GNUNET_break (0);
  1358. return CURL_READFUNC_ABORT;
  1359. }
  1360. to_copy = GNUNET_MIN (s5r->io_len,
  1361. len);
  1362. GNUNET_memcpy (buf,
  1363. s5r->io_buf,
  1364. to_copy);
  1365. memmove (s5r->io_buf,
  1366. &s5r->io_buf[to_copy],
  1367. s5r->io_len - to_copy);
  1368. s5r->io_len -= to_copy;
  1369. if (s5r->io_len + to_copy == sizeof(s5r->io_buf))
  1370. run_mhd_now (s5r->hd); /* got more space for upload now */
  1371. return to_copy;
  1372. }
  1373. /* ************************** main loop of cURL interaction ****************** */
  1374. /**
  1375. * Task that is run when we are ready to receive more data
  1376. * from curl
  1377. *
  1378. * @param cls closure
  1379. */
  1380. static void
  1381. curl_task_download (void *cls);
  1382. /**
  1383. * Ask cURL for the select() sets and schedule cURL operations.
  1384. */
  1385. static void
  1386. curl_download_prepare ()
  1387. {
  1388. CURLMcode mret;
  1389. fd_set rs;
  1390. fd_set ws;
  1391. fd_set es;
  1392. int max;
  1393. struct GNUNET_NETWORK_FDSet *grs;
  1394. struct GNUNET_NETWORK_FDSet *gws;
  1395. long to;
  1396. struct GNUNET_TIME_Relative rtime;
  1397. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1398. "Scheduling CURL interaction\n");
  1399. if (NULL != curl_download_task)
  1400. {
  1401. GNUNET_SCHEDULER_cancel (curl_download_task);
  1402. curl_download_task = NULL;
  1403. }
  1404. max = -1;
  1405. FD_ZERO (&rs);
  1406. FD_ZERO (&ws);
  1407. FD_ZERO (&es);
  1408. if (CURLM_OK != (mret = curl_multi_fdset (curl_multi,
  1409. &rs,
  1410. &ws,
  1411. &es,
  1412. &max)))
  1413. {
  1414. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1415. "%s failed at %s:%d: `%s'\n",
  1416. "curl_multi_fdset", __FILE__, __LINE__,
  1417. curl_multi_strerror (mret));
  1418. return;
  1419. }
  1420. to = -1;
  1421. GNUNET_break (CURLM_OK ==
  1422. curl_multi_timeout (curl_multi,
  1423. &to));
  1424. if (-1 == to)
  1425. rtime = GNUNET_TIME_UNIT_FOREVER_REL;
  1426. else
  1427. rtime = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
  1428. to);
  1429. if (-1 != max)
  1430. {
  1431. grs = GNUNET_NETWORK_fdset_create ();
  1432. gws = GNUNET_NETWORK_fdset_create ();
  1433. GNUNET_NETWORK_fdset_copy_native (grs,
  1434. &rs,
  1435. max + 1);
  1436. GNUNET_NETWORK_fdset_copy_native (gws,
  1437. &ws,
  1438. max + 1);
  1439. curl_download_task = GNUNET_SCHEDULER_add_select (
  1440. GNUNET_SCHEDULER_PRIORITY_DEFAULT,
  1441. rtime,
  1442. grs,
  1443. gws,
  1444. &curl_task_download,
  1445. curl_multi);
  1446. GNUNET_NETWORK_fdset_destroy (gws);
  1447. GNUNET_NETWORK_fdset_destroy (grs);
  1448. }
  1449. else
  1450. {
  1451. curl_download_task = GNUNET_SCHEDULER_add_delayed (rtime,
  1452. &curl_task_download,
  1453. curl_multi);
  1454. }
  1455. }
  1456. /**
  1457. * Task that is run when we are ready to receive more data from curl.
  1458. *
  1459. * @param cls closure, NULL
  1460. */
  1461. static void
  1462. curl_task_download (void *cls)
  1463. {
  1464. int running;
  1465. int msgnum;
  1466. struct CURLMsg *msg;
  1467. CURLMcode mret;
  1468. struct Socks5Request *s5r;
  1469. curl_download_task = NULL;
  1470. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1471. "Running CURL interaction\n");
  1472. do
  1473. {
  1474. running = 0;
  1475. mret = curl_multi_perform (curl_multi,
  1476. &running);
  1477. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1478. "Checking CURL multi status: %d\n",
  1479. mret);
  1480. while (NULL != (msg = curl_multi_info_read (curl_multi,
  1481. &msgnum)))
  1482. {
  1483. GNUNET_break (CURLE_OK ==
  1484. curl_easy_getinfo (msg->easy_handle,
  1485. CURLINFO_PRIVATE,
  1486. (char **) &s5r));
  1487. if (NULL == s5r)
  1488. {
  1489. GNUNET_break (0);
  1490. continue;
  1491. }
  1492. switch (msg->msg)
  1493. {
  1494. case CURLMSG_NONE:
  1495. /* documentation says this is not used */
  1496. GNUNET_break (0);
  1497. break;
  1498. case CURLMSG_DONE:
  1499. switch (msg->data.result)
  1500. {
  1501. case CURLE_OK:
  1502. case CURLE_GOT_NOTHING:
  1503. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1504. "CURL download %s%s completed.\n",
  1505. s5r->domain,
  1506. s5r->url);
  1507. if (NULL == s5r->response)
  1508. {
  1509. GNUNET_assert (GNUNET_OK ==
  1510. create_mhd_response_from_s5r (s5r));
  1511. }
  1512. s5r->state = SOCKS5_SOCKET_DOWNLOAD_DONE;
  1513. if (GNUNET_YES == s5r->suspended)
  1514. {
  1515. MHD_resume_connection (s5r->con);
  1516. s5r->suspended = GNUNET_NO;
  1517. }
  1518. run_mhd_now (s5r->hd);
  1519. break;
  1520. default:
  1521. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1522. "Download curl %s%s failed: %s\n",
  1523. s5r->domain,
  1524. s5r->url,
  1525. curl_easy_strerror (msg->data.result));
  1526. /* FIXME: indicate error somehow? close MHD connection badly as well? */
  1527. s5r->state = SOCKS5_SOCKET_DOWNLOAD_DONE;
  1528. if (GNUNET_YES == s5r->suspended)
  1529. {
  1530. MHD_resume_connection (s5r->con);
  1531. s5r->suspended = GNUNET_NO;
  1532. }
  1533. run_mhd_now (s5r->hd);
  1534. break;
  1535. }
  1536. if (NULL == s5r->response)
  1537. s5r->response = curl_failure_response;
  1538. break;
  1539. case CURLMSG_LAST:
  1540. /* documentation says this is not used */
  1541. GNUNET_break (0);
  1542. break;
  1543. default:
  1544. /* unexpected status code */
  1545. GNUNET_break (0);
  1546. break;
  1547. }
  1548. }
  1549. ;
  1550. }
  1551. while (mret == CURLM_CALL_MULTI_PERFORM);
  1552. if (CURLM_OK != mret)
  1553. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1554. "%s failed at %s:%d: `%s'\n",
  1555. "curl_multi_perform", __FILE__, __LINE__,
  1556. curl_multi_strerror (mret));
  1557. if (0 == running)
  1558. {
  1559. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1560. "Suspending cURL multi loop, no more events pending\n");
  1561. if (NULL != curl_download_task)
  1562. {
  1563. GNUNET_SCHEDULER_cancel (curl_download_task);
  1564. curl_download_task = NULL;
  1565. }
  1566. return; /* nothing more in progress */
  1567. }
  1568. curl_download_prepare ();
  1569. }
  1570. /* ********************************* MHD response generation ******************* */
  1571. /**
  1572. * Read HTTP request header field from the request. Copies the fields
  1573. * over to the 'headers' that will be given to curl. However, 'Host'
  1574. * is substituted with the LEHO if present. We also change the
  1575. * 'Connection' header value to "close" as the proxy does not support
  1576. * pipelining.
  1577. *
  1578. * @param cls our `struct Socks5Request`
  1579. * @param kind value kind
  1580. * @param key field key
  1581. * @param value field value
  1582. * @return #MHD_YES to continue to iterate
  1583. */
  1584. static int
  1585. con_val_iter (void *cls,
  1586. enum MHD_ValueKind kind,
  1587. const char *key,
  1588. const char *value)
  1589. {
  1590. struct Socks5Request *s5r = cls;
  1591. char *hdr;
  1592. if ((0 == strcasecmp (MHD_HTTP_HEADER_HOST,
  1593. key)) &&
  1594. (NULL != s5r->leho))
  1595. value = s5r->leho;
  1596. GNUNET_asprintf (&hdr,
  1597. "%s: %s",
  1598. key,
  1599. value);
  1600. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1601. "Adding HEADER `%s' to HTTP request\n",
  1602. hdr);
  1603. s5r->headers = curl_slist_append (s5r->headers,
  1604. hdr);
  1605. GNUNET_free (hdr);
  1606. return MHD_YES;
  1607. }
  1608. /**
  1609. * Main MHD callback for handling requests.
  1610. *
  1611. * @param cls unused
  1612. * @param con MHD connection handle
  1613. * @param url the url in the request
  1614. * @param meth the HTTP method used ("GET", "PUT", etc.)
  1615. * @param ver the HTTP version string ("HTTP/1.1" for version 1.1, etc.)
  1616. * @param upload_data the data being uploaded (excluding HEADERS,
  1617. * for a POST that fits into memory and that is encoded
  1618. * with a supported encoding, the POST data will NOT be
  1619. * given in upload_data and is instead available as
  1620. * part of MHD_get_connection_values; very large POST
  1621. * data *will* be made available incrementally in
  1622. * upload_data)
  1623. * @param upload_data_size set initially to the size of the
  1624. * @a upload_data provided; the method must update this
  1625. * value to the number of bytes NOT processed;
  1626. * @param con_cls pointer to location where we store the `struct Request`
  1627. * @return #MHD_YES if the connection was handled successfully,
  1628. * #MHD_NO if the socket must be closed due to a serious
  1629. * error while handling the request
  1630. */
  1631. static MHD_RESULT
  1632. create_response (void *cls,
  1633. struct MHD_Connection *con,
  1634. const char *url,
  1635. const char *meth,
  1636. const char *ver,
  1637. const char *upload_data,
  1638. size_t *upload_data_size,
  1639. void **con_cls)
  1640. {
  1641. struct Socks5Request *s5r = *con_cls;
  1642. char *curlurl;
  1643. char ipstring[INET6_ADDRSTRLEN];
  1644. char ipaddr[INET6_ADDRSTRLEN + 2];
  1645. char *curl_hosts;
  1646. const struct sockaddr *sa;
  1647. const struct sockaddr_in *s4;
  1648. const struct sockaddr_in6 *s6;
  1649. uint16_t port;
  1650. size_t left;
  1651. if (NULL == s5r)
  1652. {
  1653. GNUNET_break (0);
  1654. return MHD_NO;
  1655. }
  1656. s5r->con = con;
  1657. /* Fresh connection. */
  1658. if (SOCKS5_SOCKET_WITH_MHD == s5r->state)
  1659. {
  1660. /* first time here, initialize curl handle */
  1661. if (s5r->is_gns)
  1662. {
  1663. sa = (const struct sockaddr *) &s5r->destination_address;
  1664. switch (sa->sa_family)
  1665. {
  1666. case AF_INET:
  1667. s4 = (const struct sockaddr_in *) &s5r->destination_address;
  1668. if (NULL == inet_ntop (AF_INET,
  1669. &s4->sin_addr,
  1670. ipstring,
  1671. sizeof(ipstring)))
  1672. {
  1673. GNUNET_break (0);
  1674. return MHD_NO;
  1675. }
  1676. GNUNET_snprintf (ipaddr,
  1677. sizeof(ipaddr),
  1678. "%s",
  1679. ipstring);
  1680. port = ntohs (s4->sin_port);
  1681. break;
  1682. case AF_INET6:
  1683. s6 = (const struct sockaddr_in6 *) &s5r->destination_address;
  1684. if (NULL == inet_ntop (AF_INET6,
  1685. &s6->sin6_addr,
  1686. ipstring,
  1687. sizeof(ipstring)))
  1688. {
  1689. GNUNET_break (0);
  1690. return MHD_NO;
  1691. }
  1692. GNUNET_snprintf (ipaddr,
  1693. sizeof(ipaddr),
  1694. "%s",
  1695. ipstring);
  1696. port = ntohs (s6->sin6_port);
  1697. break;
  1698. default:
  1699. GNUNET_break (0);
  1700. return MHD_NO;
  1701. }
  1702. GNUNET_asprintf (&curl_hosts,
  1703. "%s:%d:%s",
  1704. s5r->leho,
  1705. port,
  1706. ipaddr);
  1707. s5r->hosts = curl_slist_append (NULL,
  1708. curl_hosts);
  1709. GNUNET_free (curl_hosts);
  1710. }
  1711. else
  1712. {
  1713. port = s5r->port;
  1714. }
  1715. if (NULL == s5r->curl)
  1716. s5r->curl = curl_easy_init ();
  1717. if (NULL == s5r->curl)
  1718. return MHD_queue_response (con,
  1719. MHD_HTTP_INTERNAL_SERVER_ERROR,
  1720. curl_failure_response);
  1721. curl_easy_setopt (s5r->curl,
  1722. CURLOPT_HEADERFUNCTION,
  1723. &curl_check_hdr);
  1724. curl_easy_setopt (s5r->curl,
  1725. CURLOPT_HEADERDATA,
  1726. s5r);
  1727. curl_easy_setopt (s5r->curl,
  1728. CURLOPT_FOLLOWLOCATION,
  1729. 0);
  1730. if (s5r->is_gns)
  1731. curl_easy_setopt (s5r->curl,
  1732. CURLOPT_IPRESOLVE,
  1733. CURL_IPRESOLVE_V4);
  1734. curl_easy_setopt (s5r->curl,
  1735. CURLOPT_CONNECTTIMEOUT,
  1736. 600L);
  1737. curl_easy_setopt (s5r->curl,
  1738. CURLOPT_TIMEOUT,
  1739. 600L);
  1740. curl_easy_setopt (s5r->curl,
  1741. CURLOPT_NOSIGNAL,
  1742. 1L);
  1743. curl_easy_setopt (s5r->curl,
  1744. CURLOPT_HTTP_CONTENT_DECODING,
  1745. 0);
  1746. curl_easy_setopt (s5r->curl,
  1747. CURLOPT_NOSIGNAL,
  1748. 1L);
  1749. curl_easy_setopt (s5r->curl,
  1750. CURLOPT_PRIVATE,
  1751. s5r);
  1752. curl_easy_setopt (s5r->curl,
  1753. CURLOPT_VERBOSE,
  1754. 0L);
  1755. /**
  1756. * Pre-populate cache to resolve Hostname.
  1757. * This is necessary as the DNS name in the CURLOPT_URL is used
  1758. * for SNI http://de.wikipedia.org/wiki/Server_Name_Indication
  1759. */
  1760. if ((NULL != s5r->leho) &&
  1761. (NULL != s5r->hosts))
  1762. {
  1763. curl_easy_setopt (s5r->curl,
  1764. CURLOPT_RESOLVE,
  1765. s5r->hosts);
  1766. }
  1767. if (s5r->is_gns)
  1768. {
  1769. GNUNET_asprintf (&curlurl,
  1770. (GNUNET_YES != s5r->is_tls) // (HTTPS_PORT != s5r->port)
  1771. ? "http://%s:%d%s"
  1772. : "https://%s:%d%s",
  1773. (NULL != s5r->leho)
  1774. ? s5r->leho
  1775. : ipaddr,
  1776. port,
  1777. s5r->url);
  1778. }
  1779. else
  1780. {
  1781. GNUNET_asprintf (&curlurl,
  1782. (GNUNET_YES != s5r->is_tls) // (HTTPS_PORT != s5r->port)
  1783. ? "http://%s:%d%s"
  1784. : "https://%s:%d%s",
  1785. s5r->domain,
  1786. port,
  1787. s5r->url);
  1788. }
  1789. curl_easy_setopt (s5r->curl,
  1790. CURLOPT_URL,
  1791. curlurl);
  1792. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1793. "Launching %s CURL interaction, fetching `%s'\n",
  1794. (s5r->is_gns) ? "GNS" : "DNS",
  1795. curlurl);
  1796. GNUNET_free (curlurl);
  1797. if (0 == strcasecmp (meth,
  1798. MHD_HTTP_METHOD_PUT))
  1799. {
  1800. s5r->state = SOCKS5_SOCKET_UPLOAD_STARTED;
  1801. curl_easy_setopt (s5r->curl,
  1802. CURLOPT_UPLOAD,
  1803. 1L);
  1804. curl_easy_setopt (s5r->curl,
  1805. CURLOPT_WRITEFUNCTION,
  1806. &curl_download_cb);
  1807. curl_easy_setopt (s5r->curl,
  1808. CURLOPT_WRITEDATA,
  1809. s5r);
  1810. GNUNET_assert (CURLE_OK ==
  1811. curl_easy_setopt (s5r->curl,
  1812. CURLOPT_READFUNCTION,
  1813. &curl_upload_cb));
  1814. curl_easy_setopt (s5r->curl,
  1815. CURLOPT_READDATA,
  1816. s5r);
  1817. {
  1818. const char *us;
  1819. long upload_size = 0;
  1820. us = MHD_lookup_connection_value (con,
  1821. MHD_HEADER_KIND,
  1822. MHD_HTTP_HEADER_CONTENT_LENGTH);
  1823. if ((1 == sscanf (us,
  1824. "%ld",
  1825. &upload_size)) &&
  1826. (upload_size >= 0))
  1827. {
  1828. curl_easy_setopt (s5r->curl,
  1829. CURLOPT_INFILESIZE,
  1830. upload_size);
  1831. }
  1832. }
  1833. }
  1834. else if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
  1835. {
  1836. s5r->state = SOCKS5_SOCKET_UPLOAD_STARTED;
  1837. curl_easy_setopt (s5r->curl,
  1838. CURLOPT_POST,
  1839. 1L);
  1840. curl_easy_setopt (s5r->curl,
  1841. CURLOPT_WRITEFUNCTION,
  1842. &curl_download_cb);
  1843. curl_easy_setopt (s5r->curl,
  1844. CURLOPT_WRITEDATA,
  1845. s5r);
  1846. curl_easy_setopt (s5r->curl,
  1847. CURLOPT_READFUNCTION,
  1848. &curl_upload_cb);
  1849. curl_easy_setopt (s5r->curl,
  1850. CURLOPT_READDATA,
  1851. s5r);
  1852. {
  1853. const char *us;
  1854. long upload_size;
  1855. upload_size = 0;
  1856. us = MHD_lookup_connection_value (con,
  1857. MHD_HEADER_KIND,
  1858. MHD_HTTP_HEADER_CONTENT_LENGTH);
  1859. if ((NULL != us) &&
  1860. (1 == sscanf (us,
  1861. "%ld",
  1862. &upload_size)) &&
  1863. (upload_size >= 0))
  1864. {
  1865. curl_easy_setopt (s5r->curl,
  1866. CURLOPT_INFILESIZE,
  1867. upload_size);
  1868. }
  1869. else
  1870. {
  1871. curl_easy_setopt (s5r->curl,
  1872. CURLOPT_INFILESIZE,
  1873. upload_size);
  1874. }
  1875. }
  1876. }
  1877. else if (0 == strcasecmp (meth,
  1878. MHD_HTTP_METHOD_HEAD))
  1879. {
  1880. s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED;
  1881. curl_easy_setopt (s5r->curl,
  1882. CURLOPT_NOBODY,
  1883. 1L);
  1884. }
  1885. else if (0 == strcasecmp (meth,
  1886. MHD_HTTP_METHOD_OPTIONS))
  1887. {
  1888. s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED;
  1889. curl_easy_setopt (s5r->curl,
  1890. CURLOPT_CUSTOMREQUEST,
  1891. "OPTIONS");
  1892. curl_easy_setopt (s5r->curl,
  1893. CURLOPT_WRITEFUNCTION,
  1894. &curl_download_cb);
  1895. curl_easy_setopt (s5r->curl,
  1896. CURLOPT_WRITEDATA,
  1897. s5r);
  1898. }
  1899. else if (0 == strcasecmp (meth,
  1900. MHD_HTTP_METHOD_GET))
  1901. {
  1902. s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED;
  1903. curl_easy_setopt (s5r->curl,
  1904. CURLOPT_HTTPGET,
  1905. 1L);
  1906. curl_easy_setopt (s5r->curl,
  1907. CURLOPT_WRITEFUNCTION,
  1908. &curl_download_cb);
  1909. curl_easy_setopt (s5r->curl,
  1910. CURLOPT_WRITEDATA,
  1911. s5r);
  1912. }
  1913. else if (0 == strcasecmp (meth,
  1914. MHD_HTTP_METHOD_DELETE))
  1915. {
  1916. s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED;
  1917. curl_easy_setopt (s5r->curl,
  1918. CURLOPT_CUSTOMREQUEST,
  1919. "DELETE");
  1920. curl_easy_setopt (s5r->curl,
  1921. CURLOPT_WRITEFUNCTION,
  1922. &curl_download_cb);
  1923. curl_easy_setopt (s5r->curl,
  1924. CURLOPT_WRITEDATA,
  1925. s5r);
  1926. }
  1927. else
  1928. {
  1929. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1930. _ ("Unsupported HTTP method `%s'\n"),
  1931. meth);
  1932. curl_easy_cleanup (s5r->curl);
  1933. s5r->curl = NULL;
  1934. return MHD_NO;
  1935. }
  1936. if (0 == strcasecmp (ver, MHD_HTTP_VERSION_1_0))
  1937. {
  1938. curl_easy_setopt (s5r->curl,
  1939. CURLOPT_HTTP_VERSION,
  1940. CURL_HTTP_VERSION_1_0);
  1941. }
  1942. else if (0 == strcasecmp (ver, MHD_HTTP_VERSION_1_1))
  1943. {
  1944. curl_easy_setopt (s5r->curl,
  1945. CURLOPT_HTTP_VERSION,
  1946. CURL_HTTP_VERSION_1_1);
  1947. }
  1948. else
  1949. {
  1950. curl_easy_setopt (s5r->curl,
  1951. CURLOPT_HTTP_VERSION,
  1952. CURL_HTTP_VERSION_NONE);
  1953. }
  1954. if (GNUNET_YES == s5r->is_tls) // (HTTPS_PORT == s5r->port)
  1955. {
  1956. curl_easy_setopt (s5r->curl,
  1957. CURLOPT_USE_SSL,
  1958. CURLUSESSL_ALL);
  1959. if (0 < s5r->num_danes)
  1960. curl_easy_setopt (s5r->curl,
  1961. CURLOPT_SSL_VERIFYPEER,
  1962. 0L);
  1963. else
  1964. curl_easy_setopt (s5r->curl,
  1965. CURLOPT_SSL_VERIFYPEER,
  1966. 1L);
  1967. /* Disable cURL checking the hostname, as we will check ourselves
  1968. as only we have the domain name or the LEHO or the DANE record */
  1969. curl_easy_setopt (s5r->curl,
  1970. CURLOPT_SSL_VERIFYHOST,
  1971. 0L);
  1972. }
  1973. else
  1974. {
  1975. curl_easy_setopt (s5r->curl,
  1976. CURLOPT_USE_SSL,
  1977. CURLUSESSL_NONE);
  1978. }
  1979. if (CURLM_OK !=
  1980. curl_multi_add_handle (curl_multi,
  1981. s5r->curl))
  1982. {
  1983. GNUNET_break (0);
  1984. curl_easy_cleanup (s5r->curl);
  1985. s5r->curl = NULL;
  1986. return MHD_NO;
  1987. }
  1988. MHD_get_connection_values (con,
  1989. MHD_HEADER_KIND,
  1990. (MHD_KeyValueIterator) & con_val_iter,
  1991. s5r);
  1992. curl_easy_setopt (s5r->curl,
  1993. CURLOPT_HTTPHEADER,
  1994. s5r->headers);
  1995. curl_download_prepare ();
  1996. return MHD_YES;
  1997. }
  1998. /* continuing to process request */
  1999. if (0 != *upload_data_size)
  2000. {
  2001. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2002. "Processing %u bytes UPLOAD\n",
  2003. (unsigned int) *upload_data_size);
  2004. /* FIXME: This must be set or a header with Transfer-Encoding: chunked. Else
  2005. * upload callback is not called!
  2006. */
  2007. curl_easy_setopt (s5r->curl,
  2008. CURLOPT_POSTFIELDSIZE,
  2009. *upload_data_size);
  2010. left = GNUNET_MIN (*upload_data_size,
  2011. sizeof(s5r->io_buf) - s5r->io_len);
  2012. GNUNET_memcpy (&s5r->io_buf[s5r->io_len],
  2013. upload_data,
  2014. left);
  2015. s5r->io_len += left;
  2016. *upload_data_size -= left;
  2017. GNUNET_assert (NULL != s5r->curl);
  2018. if (GNUNET_YES == s5r->curl_paused)
  2019. {
  2020. s5r->curl_paused = GNUNET_NO;
  2021. curl_easy_pause (s5r->curl,
  2022. CURLPAUSE_CONT);
  2023. }
  2024. return MHD_YES;
  2025. }
  2026. if (SOCKS5_SOCKET_UPLOAD_STARTED == s5r->state)
  2027. {
  2028. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2029. "Finished processing UPLOAD\n");
  2030. s5r->state = SOCKS5_SOCKET_UPLOAD_DONE;
  2031. }
  2032. if (NULL == s5r->response)
  2033. {
  2034. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2035. "Waiting for HTTP response for %s%s...\n",
  2036. s5r->domain,
  2037. s5r->url);
  2038. MHD_suspend_connection (con);
  2039. s5r->suspended = GNUNET_YES;
  2040. return MHD_YES;
  2041. }
  2042. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2043. "Queueing response for %s%s with MHD\n",
  2044. s5r->domain,
  2045. s5r->url);
  2046. run_mhd_now (s5r->hd);
  2047. return MHD_queue_response (con,
  2048. s5r->response_code,
  2049. s5r->response);
  2050. }
  2051. /* ******************** MHD HTTP setup and event loop ******************** */
  2052. /**
  2053. * Function called when MHD decides that we are done with a request.
  2054. *
  2055. * @param cls NULL
  2056. * @param connection connection handle
  2057. * @param con_cls value as set by the last call to
  2058. * the MHD_AccessHandlerCallback, should be our `struct Socks5Request *`
  2059. * @param toe reason for request termination (ignored)
  2060. */
  2061. static void
  2062. mhd_completed_cb (void *cls,
  2063. struct MHD_Connection *connection,
  2064. void **con_cls,
  2065. enum MHD_RequestTerminationCode toe)
  2066. {
  2067. struct Socks5Request *s5r = *con_cls;
  2068. if (NULL == s5r)
  2069. return;
  2070. if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe)
  2071. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2072. "MHD encountered error handling request: %d\n",
  2073. toe);
  2074. if (NULL != s5r->curl)
  2075. {
  2076. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2077. "Removing cURL handle (MHD interaction complete)\n");
  2078. curl_multi_remove_handle (curl_multi,
  2079. s5r->curl);
  2080. curl_slist_free_all (s5r->headers);
  2081. s5r->headers = NULL;
  2082. curl_easy_reset (s5r->curl);
  2083. s5r->rbuf_len = 0;
  2084. s5r->wbuf_len = 0;
  2085. s5r->io_len = 0;
  2086. curl_download_prepare ();
  2087. }
  2088. if ((NULL != s5r->response) &&
  2089. (curl_failure_response != s5r->response))
  2090. MHD_destroy_response (s5r->response);
  2091. for (struct HttpResponseHeader *header = s5r->header_head;
  2092. NULL != header;
  2093. header = s5r->header_head)
  2094. {
  2095. GNUNET_CONTAINER_DLL_remove (s5r->header_head,
  2096. s5r->header_tail,
  2097. header);
  2098. GNUNET_free (header->type);
  2099. GNUNET_free (header->value);
  2100. GNUNET_free (header);
  2101. }
  2102. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2103. "Finished request for %s\n",
  2104. s5r->url);
  2105. GNUNET_free (s5r->url);
  2106. s5r->state = SOCKS5_SOCKET_WITH_MHD;
  2107. s5r->url = NULL;
  2108. s5r->response = NULL;
  2109. *con_cls = NULL;
  2110. }
  2111. /**
  2112. * Function called when MHD connection is opened or closed.
  2113. *
  2114. * @param cls NULL
  2115. * @param connection connection handle
  2116. * @param con_cls value as set by the last call to
  2117. * the MHD_AccessHandlerCallback, should be our `struct Socks5Request *`
  2118. * @param toe connection notification type
  2119. */
  2120. static void
  2121. mhd_connection_cb (void *cls,
  2122. struct MHD_Connection *connection,
  2123. void **con_cls,
  2124. enum MHD_ConnectionNotificationCode cnc)
  2125. {
  2126. struct Socks5Request *s5r;
  2127. const union MHD_ConnectionInfo *ci;
  2128. int sock;
  2129. switch (cnc)
  2130. {
  2131. case MHD_CONNECTION_NOTIFY_STARTED:
  2132. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection started...\n");
  2133. ci = MHD_get_connection_info (connection,
  2134. MHD_CONNECTION_INFO_CONNECTION_FD);
  2135. if (NULL == ci)
  2136. {
  2137. GNUNET_break (0);
  2138. return;
  2139. }
  2140. sock = ci->connect_fd;
  2141. for (s5r = s5r_head; NULL != s5r; s5r = s5r->next)
  2142. {
  2143. if (GNUNET_NETWORK_get_fd (s5r->sock) == sock)
  2144. {
  2145. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2146. "Context set...\n");
  2147. s5r->ssl_checked = GNUNET_NO;
  2148. *con_cls = s5r;
  2149. break;
  2150. }
  2151. }
  2152. break;
  2153. case MHD_CONNECTION_NOTIFY_CLOSED:
  2154. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2155. "Connection closed... cleaning up\n");
  2156. s5r = *con_cls;
  2157. if (NULL == s5r)
  2158. {
  2159. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2160. "Connection stale!\n");
  2161. return;
  2162. }
  2163. cleanup_s5r (s5r);
  2164. curl_download_prepare ();
  2165. *con_cls = NULL;
  2166. break;
  2167. default:
  2168. GNUNET_break (0);
  2169. }
  2170. }
  2171. /**
  2172. * Function called when MHD first processes an incoming connection.
  2173. * Gives us the respective URI information.
  2174. *
  2175. * We use this to associate the `struct MHD_Connection` with our
  2176. * internal `struct Socks5Request` data structure (by checking
  2177. * for matching sockets).
  2178. *
  2179. * @param cls the HTTP server handle (a `struct MhdHttpList`)
  2180. * @param url the URL that is being requested
  2181. * @param connection MHD connection object for the request
  2182. * @return the `struct Socks5Request` that this @a connection is for
  2183. */
  2184. static void *
  2185. mhd_log_callback (void *cls,
  2186. const char *url,
  2187. struct MHD_Connection *connection)
  2188. {
  2189. struct Socks5Request *s5r;
  2190. const union MHD_ConnectionInfo *ci;
  2191. ci = MHD_get_connection_info (connection,
  2192. MHD_CONNECTION_INFO_SOCKET_CONTEXT);
  2193. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing %s\n", url);
  2194. if (NULL == ci)
  2195. {
  2196. GNUNET_break (0);
  2197. return NULL;
  2198. }
  2199. s5r = ci->socket_context;
  2200. if (NULL != s5r->url)
  2201. {
  2202. GNUNET_break (0);
  2203. return NULL;
  2204. }
  2205. s5r->url = GNUNET_strdup (url);
  2206. if (NULL != s5r->timeout_task)
  2207. {
  2208. GNUNET_SCHEDULER_cancel (s5r->timeout_task);
  2209. s5r->timeout_task = NULL;
  2210. }
  2211. GNUNET_assert (s5r->state == SOCKS5_SOCKET_WITH_MHD);
  2212. return s5r;
  2213. }
  2214. /**
  2215. * Kill the given MHD daemon.
  2216. *
  2217. * @param hd daemon to stop
  2218. */
  2219. static void
  2220. kill_httpd (struct MhdHttpList *hd)
  2221. {
  2222. GNUNET_CONTAINER_DLL_remove (mhd_httpd_head,
  2223. mhd_httpd_tail,
  2224. hd);
  2225. GNUNET_free (hd->domain);
  2226. MHD_stop_daemon (hd->daemon);
  2227. if (NULL != hd->httpd_task)
  2228. {
  2229. GNUNET_SCHEDULER_cancel (hd->httpd_task);
  2230. hd->httpd_task = NULL;
  2231. }
  2232. GNUNET_free (hd->proxy_cert);
  2233. if (hd == httpd)
  2234. httpd = NULL;
  2235. GNUNET_free (hd);
  2236. }
  2237. /**
  2238. * Task run whenever HTTP server is idle for too long. Kill it.
  2239. *
  2240. * @param cls the `struct MhdHttpList *`
  2241. */
  2242. static void
  2243. kill_httpd_task (void *cls)
  2244. {
  2245. struct MhdHttpList *hd = cls;
  2246. hd->httpd_task = NULL;
  2247. kill_httpd (hd);
  2248. }
  2249. /**
  2250. * Task run whenever HTTP server operations are pending.
  2251. *
  2252. * @param cls the `struct MhdHttpList *` of the daemon that is being run
  2253. */
  2254. static void
  2255. do_httpd (void *cls);
  2256. /**
  2257. * Schedule MHD. This function should be called initially when an
  2258. * MHD is first getting its client socket, and will then automatically
  2259. * always be called later whenever there is work to be done.
  2260. *
  2261. * @param hd the daemon to schedule
  2262. */
  2263. static void
  2264. schedule_httpd (struct MhdHttpList *hd)
  2265. {
  2266. fd_set rs;
  2267. fd_set ws;
  2268. fd_set es;
  2269. struct GNUNET_NETWORK_FDSet *wrs;
  2270. struct GNUNET_NETWORK_FDSet *wws;
  2271. int max;
  2272. int haveto;
  2273. MHD_UNSIGNED_LONG_LONG timeout;
  2274. struct GNUNET_TIME_Relative tv;
  2275. FD_ZERO (&rs);
  2276. FD_ZERO (&ws);
  2277. FD_ZERO (&es);
  2278. max = -1;
  2279. if (MHD_YES !=
  2280. MHD_get_fdset (hd->daemon,
  2281. &rs,
  2282. &ws,
  2283. &es,
  2284. &max))
  2285. {
  2286. kill_httpd (hd);
  2287. return;
  2288. }
  2289. haveto = MHD_get_timeout (hd->daemon,
  2290. &timeout);
  2291. if (MHD_YES == haveto)
  2292. tv.rel_value_us = (uint64_t) timeout * 1000LL;
  2293. else
  2294. tv = GNUNET_TIME_UNIT_FOREVER_REL;
  2295. if (-1 != max)
  2296. {
  2297. wrs = GNUNET_NETWORK_fdset_create ();
  2298. wws = GNUNET_NETWORK_fdset_create ();
  2299. GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
  2300. GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
  2301. }
  2302. else
  2303. {
  2304. wrs = NULL;
  2305. wws = NULL;
  2306. }
  2307. if (NULL != hd->httpd_task)
  2308. {
  2309. GNUNET_SCHEDULER_cancel (hd->httpd_task);
  2310. hd->httpd_task = NULL;
  2311. }
  2312. if ((MHD_YES != haveto) &&
  2313. (-1 == max) &&
  2314. (hd != httpd))
  2315. {
  2316. /* daemon is idle, kill after timeout */
  2317. hd->httpd_task = GNUNET_SCHEDULER_add_delayed (MHD_CACHE_TIMEOUT,
  2318. &kill_httpd_task,
  2319. hd);
  2320. }
  2321. else
  2322. {
  2323. hd->httpd_task =
  2324. GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
  2325. tv, wrs, wws,
  2326. &do_httpd, hd);
  2327. }
  2328. if (NULL != wrs)
  2329. GNUNET_NETWORK_fdset_destroy (wrs);
  2330. if (NULL != wws)
  2331. GNUNET_NETWORK_fdset_destroy (wws);
  2332. }
  2333. /**
  2334. * Task run whenever HTTP server operations are pending.
  2335. *
  2336. * @param cls the `struct MhdHttpList` of the daemon that is being run
  2337. */
  2338. static void
  2339. do_httpd (void *cls)
  2340. {
  2341. struct MhdHttpList *hd = cls;
  2342. hd->httpd_task = NULL;
  2343. MHD_run (hd->daemon);
  2344. schedule_httpd (hd);
  2345. }
  2346. /**
  2347. * Run MHD now, we have extra data ready for the callback.
  2348. *
  2349. * @param hd the daemon to run now.
  2350. */
  2351. static void
  2352. run_mhd_now (struct MhdHttpList *hd)
  2353. {
  2354. if (NULL != hd->httpd_task)
  2355. GNUNET_SCHEDULER_cancel (hd->httpd_task);
  2356. hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd,
  2357. hd);
  2358. }
  2359. /**
  2360. * Read file in filename
  2361. *
  2362. * @param filename file to read
  2363. * @param size pointer where filesize is stored
  2364. * @return NULL on error
  2365. */
  2366. static void*
  2367. load_file (const char*filename,
  2368. unsigned int*size)
  2369. {
  2370. void *buffer;
  2371. uint64_t fsize;
  2372. if (GNUNET_OK !=
  2373. GNUNET_DISK_file_size (filename,
  2374. &fsize,
  2375. GNUNET_YES,
  2376. GNUNET_YES))
  2377. return NULL;
  2378. if (fsize > MAX_PEM_SIZE)
  2379. return NULL;
  2380. *size = (unsigned int) fsize;
  2381. buffer = GNUNET_malloc (*size);
  2382. if (fsize !=
  2383. GNUNET_DISK_fn_read (filename,
  2384. buffer,
  2385. (size_t) fsize))
  2386. {
  2387. GNUNET_free (buffer);
  2388. return NULL;
  2389. }
  2390. return buffer;
  2391. }
  2392. /**
  2393. * Load PEM key from file
  2394. *
  2395. * @param key where to store the data
  2396. * @param keyfile path to the PEM file
  2397. * @return #GNUNET_OK on success
  2398. */
  2399. static int
  2400. load_key_from_file (gnutls_x509_privkey_t key,
  2401. const char*keyfile)
  2402. {
  2403. gnutls_datum_t key_data;
  2404. int ret;
  2405. key_data.data = load_file (keyfile,
  2406. &key_data.size);
  2407. if (NULL == key_data.data)
  2408. return GNUNET_SYSERR;
  2409. ret = gnutls_x509_privkey_import (key, &key_data,
  2410. GNUTLS_X509_FMT_PEM);
  2411. if (GNUTLS_E_SUCCESS != ret)
  2412. {
  2413. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2414. _ ("Unable to import private key from file `%s'\n"),
  2415. keyfile);
  2416. }
  2417. GNUNET_free (key_data.data);
  2418. return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
  2419. }
  2420. /**
  2421. * Load cert from file
  2422. *
  2423. * @param crt struct to store data in
  2424. * @param certfile path to pem file
  2425. * @return #GNUNET_OK on success
  2426. */
  2427. static int
  2428. load_cert_from_file (gnutls_x509_crt_t crt,
  2429. const char*certfile)
  2430. {
  2431. gnutls_datum_t cert_data;
  2432. int ret;
  2433. cert_data.data = load_file (certfile,
  2434. &cert_data.size);
  2435. if (NULL == cert_data.data)
  2436. return GNUNET_SYSERR;
  2437. ret = gnutls_x509_crt_import (crt,
  2438. &cert_data,
  2439. GNUTLS_X509_FMT_PEM);
  2440. if (GNUTLS_E_SUCCESS != ret)
  2441. {
  2442. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2443. _ ("Unable to import certificate from `%s'\n"),
  2444. certfile);
  2445. }
  2446. GNUNET_free (cert_data.data);
  2447. return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
  2448. }
  2449. /**
  2450. * Generate new certificate for specific name
  2451. *
  2452. * @param name the subject name to generate a cert for
  2453. * @return a struct holding the PEM data, NULL on error
  2454. */
  2455. static struct ProxyGNSCertificate *
  2456. generate_gns_certificate (const char *name)
  2457. {
  2458. unsigned int serial;
  2459. size_t key_buf_size;
  2460. size_t cert_buf_size;
  2461. gnutls_x509_crt_t request;
  2462. time_t etime;
  2463. struct tm *tm_data;
  2464. struct ProxyGNSCertificate *pgc;
  2465. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2466. "Generating x.509 certificate for `%s'\n",
  2467. name);
  2468. GNUNET_break (GNUTLS_E_SUCCESS == gnutls_x509_crt_init (&request));
  2469. GNUNET_break (GNUTLS_E_SUCCESS == gnutls_x509_crt_set_key (request,
  2470. proxy_ca.key));
  2471. pgc = GNUNET_new (struct ProxyGNSCertificate);
  2472. gnutls_x509_crt_set_dn_by_oid (request,
  2473. GNUTLS_OID_X520_COUNTRY_NAME,
  2474. 0,
  2475. "ZZ",
  2476. strlen ("ZZ"));
  2477. gnutls_x509_crt_set_dn_by_oid (request,
  2478. GNUTLS_OID_X520_ORGANIZATION_NAME,
  2479. 0,
  2480. "GNU Name System",
  2481. strlen ("GNU Name System"));
  2482. gnutls_x509_crt_set_dn_by_oid (request,
  2483. GNUTLS_OID_X520_COMMON_NAME,
  2484. 0,
  2485. name,
  2486. strlen (name));
  2487. gnutls_x509_crt_set_subject_alternative_name (request,
  2488. GNUTLS_SAN_DNSNAME,
  2489. name);
  2490. GNUNET_break (GNUTLS_E_SUCCESS ==
  2491. gnutls_x509_crt_set_version (request,
  2492. 3));
  2493. gnutls_rnd (GNUTLS_RND_NONCE,
  2494. &serial,
  2495. sizeof(serial));
  2496. gnutls_x509_crt_set_serial (request,
  2497. &serial,
  2498. sizeof(serial));
  2499. etime = time (NULL);
  2500. tm_data = localtime (&etime);
  2501. tm_data->tm_hour--;
  2502. etime = mktime (tm_data);
  2503. gnutls_x509_crt_set_activation_time (request,
  2504. etime);
  2505. tm_data->tm_year++;
  2506. etime = mktime (tm_data);
  2507. gnutls_x509_crt_set_expiration_time (request,
  2508. etime);
  2509. gnutls_x509_crt_sign2 (request,
  2510. proxy_ca.cert,
  2511. proxy_ca.key,
  2512. GNUTLS_DIG_SHA512,
  2513. 0);
  2514. key_buf_size = sizeof(pgc->key);
  2515. cert_buf_size = sizeof(pgc->cert);
  2516. gnutls_x509_crt_export (request,
  2517. GNUTLS_X509_FMT_PEM,
  2518. pgc->cert,
  2519. &cert_buf_size);
  2520. gnutls_x509_privkey_export (proxy_ca.key,
  2521. GNUTLS_X509_FMT_PEM,
  2522. pgc->key,
  2523. &key_buf_size);
  2524. gnutls_x509_crt_deinit (request);
  2525. return pgc;
  2526. }
  2527. /**
  2528. * Function called by MHD with errors, suppresses them all.
  2529. *
  2530. * @param cls closure
  2531. * @param fm format string (`printf()`-style)
  2532. * @param ap arguments to @a fm
  2533. */
  2534. static void
  2535. mhd_error_log_callback (void *cls,
  2536. const char *fm,
  2537. va_list ap)
  2538. {
  2539. /* do nothing */
  2540. }
  2541. /**
  2542. * Lookup (or create) an TLS MHD instance for a particular domain.
  2543. *
  2544. * @param domain the domain the TLS daemon has to serve
  2545. * @return NULL on error
  2546. */
  2547. static struct MhdHttpList *
  2548. lookup_ssl_httpd (const char*domain)
  2549. {
  2550. struct MhdHttpList *hd;
  2551. struct ProxyGNSCertificate *pgc;
  2552. if (NULL == domain)
  2553. {
  2554. GNUNET_break (0);
  2555. return NULL;
  2556. }
  2557. for (hd = mhd_httpd_head; NULL != hd; hd = hd->next)
  2558. if ((NULL != hd->domain) &&
  2559. (0 == strcmp (hd->domain, domain)))
  2560. return hd;
  2561. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2562. "Starting fresh MHD HTTPS instance for domain `%s'\n",
  2563. domain);
  2564. pgc = generate_gns_certificate (domain);
  2565. hd = GNUNET_new (struct MhdHttpList);
  2566. hd->is_ssl = GNUNET_YES;
  2567. hd->domain = GNUNET_strdup (domain);
  2568. hd->proxy_cert = pgc;
  2569. hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL
  2570. | MHD_USE_NO_LISTEN_SOCKET
  2571. | MHD_ALLOW_SUSPEND_RESUME,
  2572. 0,
  2573. NULL, NULL,
  2574. &create_response, hd,
  2575. MHD_OPTION_CONNECTION_TIMEOUT, (unsigned
  2576. int) 16,
  2577. MHD_OPTION_NOTIFY_COMPLETED, &mhd_completed_cb,
  2578. NULL,
  2579. MHD_OPTION_NOTIFY_CONNECTION,
  2580. &mhd_connection_cb, NULL,
  2581. MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback,
  2582. NULL,
  2583. MHD_OPTION_EXTERNAL_LOGGER,
  2584. &mhd_error_log_callback, NULL,
  2585. MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
  2586. MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
  2587. MHD_OPTION_END);
  2588. if (NULL == hd->daemon)
  2589. {
  2590. GNUNET_free (pgc);
  2591. GNUNET_free (hd);
  2592. return NULL;
  2593. }
  2594. GNUNET_CONTAINER_DLL_insert (mhd_httpd_head,
  2595. mhd_httpd_tail,
  2596. hd);
  2597. return hd;
  2598. }
  2599. /**
  2600. * Task run when a Socks5Request somehow fails to be associated with
  2601. * an MHD connection (e.g. because the client never speaks HTTP after
  2602. * the SOCKS5 handshake). Clean up.
  2603. *
  2604. * @param cls the `struct Socks5Request *`
  2605. */
  2606. static void
  2607. timeout_s5r_handshake (void *cls)
  2608. {
  2609. struct Socks5Request *s5r = cls;
  2610. s5r->timeout_task = NULL;
  2611. cleanup_s5r (s5r);
  2612. }
  2613. /**
  2614. * We're done with the Socks5 protocol, now we need to pass the
  2615. * connection data through to the final destination, either
  2616. * direct (if the protocol might not be HTTP), or via MHD
  2617. * (if the port looks like it should be HTTP).
  2618. *
  2619. * @param s5r socks request that has reached the final stage
  2620. */
  2621. static void
  2622. setup_data_transfer (struct Socks5Request *s5r)
  2623. {
  2624. struct MhdHttpList *hd;
  2625. int fd;
  2626. const struct sockaddr *addr;
  2627. socklen_t len;
  2628. char *domain;
  2629. if (GNUNET_YES == s5r->is_tls)
  2630. {
  2631. GNUNET_asprintf (&domain,
  2632. "%s",
  2633. s5r->domain);
  2634. hd = lookup_ssl_httpd (domain);
  2635. if (NULL == hd)
  2636. {
  2637. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2638. _ ("Failed to start HTTPS server for `%s'\n"),
  2639. s5r->domain);
  2640. cleanup_s5r (s5r);
  2641. GNUNET_free (domain);
  2642. return;
  2643. }
  2644. }
  2645. else
  2646. {
  2647. domain = NULL;
  2648. GNUNET_assert (NULL != httpd);
  2649. hd = httpd;
  2650. }
  2651. fd = GNUNET_NETWORK_get_fd (s5r->sock);
  2652. addr = GNUNET_NETWORK_get_addr (s5r->sock);
  2653. len = GNUNET_NETWORK_get_addrlen (s5r->sock);
  2654. s5r->state = SOCKS5_SOCKET_WITH_MHD;
  2655. if (MHD_YES !=
  2656. MHD_add_connection (hd->daemon,
  2657. fd,
  2658. addr,
  2659. len))
  2660. {
  2661. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2662. _ ("Failed to pass client to MHD\n"));
  2663. cleanup_s5r (s5r);
  2664. GNUNET_free (domain);
  2665. return;
  2666. }
  2667. s5r->hd = hd;
  2668. schedule_httpd (hd);
  2669. s5r->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_HANDSHAKE_TIMEOUT,
  2670. &timeout_s5r_handshake,
  2671. s5r);
  2672. GNUNET_free (domain);
  2673. }
  2674. /* ********************* SOCKS handling ************************* */
  2675. /**
  2676. * Write data from buffer to socks5 client, then continue with state machine.
  2677. *
  2678. * @param cls the closure with the `struct Socks5Request`
  2679. */
  2680. static void
  2681. do_write (void *cls)
  2682. {
  2683. struct Socks5Request *s5r = cls;
  2684. ssize_t len;
  2685. s5r->wtask = NULL;
  2686. len = GNUNET_NETWORK_socket_send (s5r->sock,
  2687. s5r->wbuf,
  2688. s5r->wbuf_len);
  2689. if (len <= 0)
  2690. {
  2691. /* write error: connection closed, shutdown, etc.; just clean up */
  2692. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2693. "Write Error\n");
  2694. cleanup_s5r (s5r);
  2695. return;
  2696. }
  2697. memmove (s5r->wbuf,
  2698. &s5r->wbuf[len],
  2699. s5r->wbuf_len - len);
  2700. s5r->wbuf_len -= len;
  2701. if (s5r->wbuf_len > 0)
  2702. {
  2703. /* not done writing */
  2704. s5r->wtask =
  2705. GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
  2706. s5r->sock,
  2707. &do_write, s5r);
  2708. return;
  2709. }
  2710. /* we're done writing, continue with state machine! */
  2711. switch (s5r->state)
  2712. {
  2713. case SOCKS5_INIT:
  2714. GNUNET_assert (0);
  2715. break;
  2716. case SOCKS5_REQUEST:
  2717. GNUNET_assert (NULL != s5r->rtask);
  2718. break;
  2719. case SOCKS5_DATA_TRANSFER:
  2720. setup_data_transfer (s5r);
  2721. return;
  2722. case SOCKS5_WRITE_THEN_CLEANUP:
  2723. cleanup_s5r (s5r);
  2724. return;
  2725. default:
  2726. GNUNET_break (0);
  2727. break;
  2728. }
  2729. }
  2730. /**
  2731. * Return a server response message indicating a failure to the client.
  2732. *
  2733. * @param s5r request to return failure code for
  2734. * @param sc status code to return
  2735. */
  2736. static void
  2737. signal_socks_failure (struct Socks5Request *s5r,
  2738. enum Socks5StatusCode sc)
  2739. {
  2740. struct Socks5ServerResponseMessage *s_resp;
  2741. GNUNET_break (0 == s5r->wbuf_len); /* Should happen first in any transmission, right? */
  2742. GNUNET_assert (SOCKS_BUFFERSIZE - s5r->wbuf_len >=
  2743. sizeof(struct Socks5ServerResponseMessage));
  2744. s_resp = (struct Socks5ServerResponseMessage *) &s5r->wbuf[s5r->wbuf_len];
  2745. memset (s_resp, 0, sizeof(struct Socks5ServerResponseMessage));
  2746. s_resp->version = SOCKS_VERSION_5;
  2747. s_resp->reply = sc;
  2748. s5r->state = SOCKS5_WRITE_THEN_CLEANUP;
  2749. if (NULL != s5r->wtask)
  2750. s5r->wtask =
  2751. GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
  2752. s5r->sock,
  2753. &do_write, s5r);
  2754. }
  2755. /**
  2756. * Return a server response message indicating success.
  2757. *
  2758. * @param s5r request to return success status message for
  2759. */
  2760. static void
  2761. signal_socks_success (struct Socks5Request *s5r)
  2762. {
  2763. struct Socks5ServerResponseMessage *s_resp;
  2764. s_resp = (struct Socks5ServerResponseMessage *) &s5r->wbuf[s5r->wbuf_len];
  2765. s_resp->version = SOCKS_VERSION_5;
  2766. s_resp->reply = SOCKS5_STATUS_REQUEST_GRANTED;
  2767. s_resp->reserved = 0;
  2768. s_resp->addr_type = SOCKS5_AT_IPV4;
  2769. /* zero out IPv4 address and port */
  2770. memset (&s_resp[1],
  2771. 0,
  2772. sizeof(struct in_addr) + sizeof(uint16_t));
  2773. s5r->wbuf_len += sizeof(struct Socks5ServerResponseMessage)
  2774. + sizeof(struct in_addr) + sizeof(uint16_t);
  2775. if (NULL == s5r->wtask)
  2776. s5r->wtask =
  2777. GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
  2778. s5r->sock,
  2779. &do_write, s5r);
  2780. }
  2781. /**
  2782. * Process GNS results for target domain.
  2783. *
  2784. * @param cls the `struct Socks5Request *`
  2785. * @param tld #GNUNET_YES if this was a GNS TLD.
  2786. * @param rd_count number of records returned
  2787. * @param rd record data
  2788. */
  2789. static void
  2790. handle_gns_result (void *cls,
  2791. int tld,
  2792. uint32_t rd_count,
  2793. const struct GNUNET_GNSRECORD_Data *rd)
  2794. {
  2795. struct Socks5Request *s5r = cls;
  2796. const struct GNUNET_GNSRECORD_Data *r;
  2797. int got_ip;
  2798. s5r->gns_lookup = NULL;
  2799. s5r->is_gns = tld;
  2800. got_ip = GNUNET_NO;
  2801. for (uint32_t i = 0; i < rd_count; i++)
  2802. {
  2803. r = &rd[i];
  2804. switch (r->record_type)
  2805. {
  2806. case GNUNET_DNSPARSER_TYPE_A:
  2807. {
  2808. struct sockaddr_in *in;
  2809. if (sizeof(struct in_addr) != r->data_size)
  2810. {
  2811. GNUNET_break_op (0);
  2812. break;
  2813. }
  2814. if (GNUNET_YES == got_ip)
  2815. break;
  2816. if (GNUNET_OK !=
  2817. GNUNET_NETWORK_test_pf (PF_INET))
  2818. break;
  2819. got_ip = GNUNET_YES;
  2820. in = (struct sockaddr_in *) &s5r->destination_address;
  2821. in->sin_family = AF_INET;
  2822. GNUNET_memcpy (&in->sin_addr,
  2823. r->data,
  2824. r->data_size);
  2825. in->sin_port = htons (s5r->port);
  2826. #if HAVE_SOCKADDR_IN_SIN_LEN
  2827. in->sin_len = sizeof(*in);
  2828. #endif
  2829. }
  2830. break;
  2831. case GNUNET_DNSPARSER_TYPE_AAAA:
  2832. {
  2833. struct sockaddr_in6 *in;
  2834. if (sizeof(struct in6_addr) != r->data_size)
  2835. {
  2836. GNUNET_break_op (0);
  2837. break;
  2838. }
  2839. if (GNUNET_YES == got_ip)
  2840. break;
  2841. if (GNUNET_YES == disable_v6)
  2842. break;
  2843. if (GNUNET_OK !=
  2844. GNUNET_NETWORK_test_pf (PF_INET6))
  2845. break;
  2846. /* FIXME: allow user to disable IPv6 per configuration option... */
  2847. got_ip = GNUNET_YES;
  2848. in = (struct sockaddr_in6 *) &s5r->destination_address;
  2849. in->sin6_family = AF_INET6;
  2850. GNUNET_memcpy (&in->sin6_addr,
  2851. r->data,
  2852. r->data_size);
  2853. in->sin6_port = htons (s5r->port);
  2854. #if HAVE_SOCKADDR_IN_SIN_LEN
  2855. in->sin6_len = sizeof(*in);
  2856. #endif
  2857. }
  2858. break;
  2859. case GNUNET_GNSRECORD_TYPE_VPN:
  2860. GNUNET_break (0); /* should have been translated within GNS */
  2861. break;
  2862. case GNUNET_GNSRECORD_TYPE_LEHO:
  2863. GNUNET_free (s5r->leho);
  2864. s5r->leho = GNUNET_strndup (r->data,
  2865. r->data_size);
  2866. break;
  2867. case GNUNET_GNSRECORD_TYPE_BOX:
  2868. {
  2869. const struct GNUNET_GNSRECORD_BoxRecord *box;
  2870. if (r->data_size < sizeof(struct GNUNET_GNSRECORD_BoxRecord))
  2871. {
  2872. GNUNET_break_op (0);
  2873. break;
  2874. }
  2875. box = r->data;
  2876. if ((ntohl (box->record_type) != GNUNET_DNSPARSER_TYPE_TLSA) ||
  2877. (ntohs (box->protocol) != IPPROTO_TCP) ||
  2878. (ntohs (box->service) != s5r->port))
  2879. break; /* BOX record does not apply */
  2880. if (s5r->num_danes >= MAX_DANES)
  2881. {
  2882. GNUNET_break (0); /* MAX_DANES too small */
  2883. break;
  2884. }
  2885. s5r->is_tls = GNUNET_YES; /* This should be TLS */
  2886. s5r->dane_data_len[s5r->num_danes]
  2887. = r->data_size - sizeof(struct GNUNET_GNSRECORD_BoxRecord);
  2888. s5r->dane_data[s5r->num_danes]
  2889. = GNUNET_memdup (&box[1],
  2890. s5r->dane_data_len[s5r->num_danes]);
  2891. s5r->num_danes++;
  2892. break;
  2893. }
  2894. default:
  2895. /* don't care */
  2896. break;
  2897. }
  2898. }
  2899. if ((GNUNET_YES != got_ip) &&
  2900. (GNUNET_YES == tld))
  2901. {
  2902. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2903. "Name resolution failed to yield useful IP address.\n");
  2904. signal_socks_failure (s5r,
  2905. SOCKS5_STATUS_GENERAL_FAILURE);
  2906. return;
  2907. }
  2908. s5r->state = SOCKS5_DATA_TRANSFER;
  2909. signal_socks_success (s5r);
  2910. }
  2911. /**
  2912. * Remove the first @a len bytes from the beginning of the read buffer.
  2913. *
  2914. * @param s5r the handle clear the read buffer for
  2915. * @param len number of bytes in read buffer to advance
  2916. */
  2917. static void
  2918. clear_from_s5r_rbuf (struct Socks5Request *s5r,
  2919. size_t len)
  2920. {
  2921. GNUNET_assert (len <= s5r->rbuf_len);
  2922. memmove (s5r->rbuf,
  2923. &s5r->rbuf[len],
  2924. s5r->rbuf_len - len);
  2925. s5r->rbuf_len -= len;
  2926. }
  2927. /**
  2928. * Read data from incoming Socks5 connection
  2929. *
  2930. * @param cls the closure with the `struct Socks5Request`
  2931. */
  2932. static void
  2933. do_s5r_read (void *cls)
  2934. {
  2935. struct Socks5Request *s5r = cls;
  2936. const struct Socks5ClientHelloMessage *c_hello;
  2937. struct Socks5ServerHelloMessage *s_hello;
  2938. const struct Socks5ClientRequestMessage *c_req;
  2939. ssize_t rlen;
  2940. size_t alen;
  2941. const struct GNUNET_SCHEDULER_TaskContext *tc;
  2942. s5r->rtask = NULL;
  2943. tc = GNUNET_SCHEDULER_get_task_context ();
  2944. if ((NULL != tc->read_ready) &&
  2945. (GNUNET_NETWORK_fdset_isset (tc->read_ready,
  2946. s5r->sock)))
  2947. {
  2948. rlen = GNUNET_NETWORK_socket_recv (s5r->sock,
  2949. &s5r->rbuf[s5r->rbuf_len],
  2950. sizeof(s5r->rbuf) - s5r->rbuf_len);
  2951. if (rlen <= 0)
  2952. {
  2953. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2954. "socks5 client disconnected.\n");
  2955. cleanup_s5r (s5r);
  2956. return;
  2957. }
  2958. s5r->rbuf_len += rlen;
  2959. }
  2960. s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  2961. s5r->sock,
  2962. &do_s5r_read, s5r);
  2963. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2964. "Processing %zu bytes of socks data in state %d\n",
  2965. s5r->rbuf_len,
  2966. s5r->state);
  2967. switch (s5r->state)
  2968. {
  2969. case SOCKS5_INIT:
  2970. c_hello = (const struct Socks5ClientHelloMessage*) &s5r->rbuf;
  2971. if ((s5r->rbuf_len < sizeof(struct Socks5ClientHelloMessage)) ||
  2972. (s5r->rbuf_len < sizeof(struct Socks5ClientHelloMessage)
  2973. + c_hello->num_auth_methods))
  2974. return; /* need more data */
  2975. if (SOCKS_VERSION_5 != c_hello->version)
  2976. {
  2977. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2978. _ ("Unsupported socks version %d\n"),
  2979. (int) c_hello->version);
  2980. cleanup_s5r (s5r);
  2981. return;
  2982. }
  2983. clear_from_s5r_rbuf (s5r,
  2984. sizeof(struct Socks5ClientHelloMessage)
  2985. + c_hello->num_auth_methods);
  2986. GNUNET_assert (0 == s5r->wbuf_len);
  2987. s_hello = (struct Socks5ServerHelloMessage *) &s5r->wbuf;
  2988. s5r->wbuf_len = sizeof(struct Socks5ServerHelloMessage);
  2989. s_hello->version = SOCKS_VERSION_5;
  2990. s_hello->auth_method = SOCKS_AUTH_NONE;
  2991. GNUNET_assert (NULL == s5r->wtask);
  2992. s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
  2993. s5r->sock,
  2994. &do_write, s5r);
  2995. s5r->state = SOCKS5_REQUEST;
  2996. return;
  2997. case SOCKS5_REQUEST:
  2998. c_req = (const struct Socks5ClientRequestMessage *) &s5r->rbuf;
  2999. if (s5r->rbuf_len < sizeof(struct Socks5ClientRequestMessage))
  3000. return;
  3001. switch (c_req->command)
  3002. {
  3003. case SOCKS5_CMD_TCP_STREAM:
  3004. /* handled below */
  3005. break;
  3006. default:
  3007. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3008. _ ("Unsupported socks command %d\n"),
  3009. (int) c_req->command);
  3010. signal_socks_failure (s5r,
  3011. SOCKS5_STATUS_COMMAND_NOT_SUPPORTED);
  3012. return;
  3013. }
  3014. switch (c_req->addr_type)
  3015. {
  3016. case SOCKS5_AT_IPV4:
  3017. {
  3018. const struct in_addr *v4 = (const struct in_addr *) &c_req[1];
  3019. const uint16_t *port = (const uint16_t *) &v4[1];
  3020. struct sockaddr_in *in;
  3021. s5r->port = ntohs (*port);
  3022. alen = sizeof(struct in_addr);
  3023. if (s5r->rbuf_len < sizeof(struct Socks5ClientRequestMessage)
  3024. + alen + sizeof(uint16_t))
  3025. return; /* need more data */
  3026. in = (struct sockaddr_in *) &s5r->destination_address;
  3027. in->sin_family = AF_INET;
  3028. in->sin_addr = *v4;
  3029. in->sin_port = *port;
  3030. #if HAVE_SOCKADDR_IN_SIN_LEN
  3031. in->sin_len = sizeof(*in);
  3032. #endif
  3033. s5r->state = SOCKS5_DATA_TRANSFER;
  3034. }
  3035. break;
  3036. case SOCKS5_AT_IPV6:
  3037. {
  3038. const struct in6_addr *v6 = (const struct in6_addr *) &c_req[1];
  3039. const uint16_t *port = (const uint16_t *) &v6[1];
  3040. struct sockaddr_in6 *in;
  3041. s5r->port = ntohs (*port);
  3042. alen = sizeof(struct in6_addr);
  3043. if (s5r->rbuf_len < sizeof(struct Socks5ClientRequestMessage)
  3044. + alen + sizeof(uint16_t))
  3045. return; /* need more data */
  3046. in = (struct sockaddr_in6 *) &s5r->destination_address;
  3047. in->sin6_family = AF_INET6;
  3048. in->sin6_addr = *v6;
  3049. in->sin6_port = *port;
  3050. #if HAVE_SOCKADDR_IN_SIN_LEN
  3051. in->sin6_len = sizeof(*in);
  3052. #endif
  3053. s5r->state = SOCKS5_DATA_TRANSFER;
  3054. }
  3055. break;
  3056. case SOCKS5_AT_DOMAINNAME:
  3057. {
  3058. const uint8_t *dom_len;
  3059. const char *dom_name;
  3060. const uint16_t *port;
  3061. dom_len = (const uint8_t *) &c_req[1];
  3062. alen = *dom_len + 1;
  3063. if (s5r->rbuf_len < sizeof(struct Socks5ClientRequestMessage)
  3064. + alen + sizeof(uint16_t))
  3065. return; /* need more data */
  3066. dom_name = (const char *) &dom_len[1];
  3067. port = (const uint16_t *) &dom_name[*dom_len];
  3068. s5r->domain = GNUNET_strndup (dom_name,
  3069. *dom_len);
  3070. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  3071. "Requested connection is to %s:%d\n",
  3072. // (HTTPS_PORT == s5r->port) ? "s" : "",
  3073. s5r->domain,
  3074. ntohs (*port));
  3075. s5r->state = SOCKS5_RESOLVING;
  3076. s5r->port = ntohs (*port);
  3077. s5r->is_tls = (HTTPS_PORT == s5r->port) ? GNUNET_YES : GNUNET_NO;
  3078. s5r->gns_lookup = GNUNET_GNS_lookup_with_tld (gns_handle,
  3079. s5r->domain,
  3080. GNUNET_DNSPARSER_TYPE_A,
  3081. GNUNET_GNS_LO_LOCAL_MASTER /* only cached */,
  3082. &handle_gns_result,
  3083. s5r);
  3084. break;
  3085. }
  3086. default:
  3087. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3088. _ ("Unsupported socks address type %d\n"),
  3089. (int) c_req->addr_type);
  3090. signal_socks_failure (s5r,
  3091. SOCKS5_STATUS_ADDRESS_TYPE_NOT_SUPPORTED);
  3092. return;
  3093. }
  3094. clear_from_s5r_rbuf (s5r,
  3095. sizeof(struct Socks5ClientRequestMessage)
  3096. + alen + sizeof(uint16_t));
  3097. if (0 != s5r->rbuf_len)
  3098. {
  3099. /* read more bytes than healthy, why did the client send more!? */
  3100. GNUNET_break_op (0);
  3101. signal_socks_failure (s5r,
  3102. SOCKS5_STATUS_GENERAL_FAILURE);
  3103. return;
  3104. }
  3105. if (SOCKS5_DATA_TRANSFER == s5r->state)
  3106. {
  3107. /* if we are not waiting for GNS resolution, signal success */
  3108. signal_socks_success (s5r);
  3109. }
  3110. /* We are done reading right now */
  3111. GNUNET_SCHEDULER_cancel (s5r->rtask);
  3112. s5r->rtask = NULL;
  3113. return;
  3114. case SOCKS5_RESOLVING:
  3115. GNUNET_assert (0);
  3116. return;
  3117. case SOCKS5_DATA_TRANSFER:
  3118. GNUNET_assert (0);
  3119. return;
  3120. default:
  3121. GNUNET_assert (0);
  3122. return;
  3123. }
  3124. }
  3125. /**
  3126. * Accept new incoming connections
  3127. *
  3128. * @param cls the closure with the lsock4 or lsock6
  3129. * @param tc the scheduler context
  3130. */
  3131. static void
  3132. do_accept (void *cls)
  3133. {
  3134. struct GNUNET_NETWORK_Handle *lsock = cls;
  3135. struct GNUNET_NETWORK_Handle *s;
  3136. struct Socks5Request *s5r;
  3137. GNUNET_assert (NULL != lsock);
  3138. if (lsock == lsock4)
  3139. ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  3140. lsock,
  3141. &do_accept,
  3142. lsock);
  3143. else if (lsock == lsock6)
  3144. ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  3145. lsock,
  3146. &do_accept,
  3147. lsock);
  3148. else
  3149. GNUNET_assert (0);
  3150. s = GNUNET_NETWORK_socket_accept (lsock,
  3151. NULL,
  3152. NULL);
  3153. if (NULL == s)
  3154. {
  3155. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
  3156. "accept");
  3157. return;
  3158. }
  3159. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  3160. "Got an inbound connection, waiting for data\n");
  3161. s5r = GNUNET_new (struct Socks5Request);
  3162. GNUNET_CONTAINER_DLL_insert (s5r_head,
  3163. s5r_tail,
  3164. s5r);
  3165. s5r->sock = s;
  3166. s5r->state = SOCKS5_INIT;
  3167. s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  3168. s5r->sock,
  3169. &do_s5r_read,
  3170. s5r);
  3171. }
  3172. /* ******************* General / main code ********************* */
  3173. /**
  3174. * Task run on shutdown
  3175. *
  3176. * @param cls closure
  3177. */
  3178. static void
  3179. do_shutdown (void *cls)
  3180. {
  3181. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  3182. "Shutting down...\n");
  3183. /* MHD requires resuming before destroying the daemons */
  3184. for (struct Socks5Request *s5r = s5r_head;
  3185. NULL != s5r;
  3186. s5r = s5r->next)
  3187. {
  3188. if (s5r->suspended)
  3189. {
  3190. s5r->suspended = GNUNET_NO;
  3191. MHD_resume_connection (s5r->con);
  3192. }
  3193. }
  3194. while (NULL != mhd_httpd_head)
  3195. kill_httpd (mhd_httpd_head);
  3196. while (NULL != s5r_head)
  3197. cleanup_s5r (s5r_head);
  3198. if (NULL != lsock4)
  3199. {
  3200. GNUNET_NETWORK_socket_close (lsock4);
  3201. lsock4 = NULL;
  3202. }
  3203. if (NULL != lsock6)
  3204. {
  3205. GNUNET_NETWORK_socket_close (lsock6);
  3206. lsock6 = NULL;
  3207. }
  3208. if (NULL != curl_multi)
  3209. {
  3210. curl_multi_cleanup (curl_multi);
  3211. curl_multi = NULL;
  3212. }
  3213. if (NULL != gns_handle)
  3214. {
  3215. GNUNET_GNS_disconnect (gns_handle);
  3216. gns_handle = NULL;
  3217. }
  3218. if (NULL != curl_download_task)
  3219. {
  3220. GNUNET_SCHEDULER_cancel (curl_download_task);
  3221. curl_download_task = NULL;
  3222. }
  3223. if (NULL != ltask4)
  3224. {
  3225. GNUNET_SCHEDULER_cancel (ltask4);
  3226. ltask4 = NULL;
  3227. }
  3228. if (NULL != ltask6)
  3229. {
  3230. GNUNET_SCHEDULER_cancel (ltask6);
  3231. ltask6 = NULL;
  3232. }
  3233. gnutls_x509_crt_deinit (proxy_ca.cert);
  3234. gnutls_x509_privkey_deinit (proxy_ca.key);
  3235. gnutls_global_deinit ();
  3236. }
  3237. /**
  3238. * Create an IPv4 listen socket bound to our port.
  3239. *
  3240. * @return NULL on error
  3241. */
  3242. static struct GNUNET_NETWORK_Handle *
  3243. bind_v4 ()
  3244. {
  3245. struct GNUNET_NETWORK_Handle *ls;
  3246. struct sockaddr_in sa4;
  3247. int eno;
  3248. memset (&sa4, 0, sizeof(sa4));
  3249. sa4.sin_family = AF_INET;
  3250. sa4.sin_port = htons (port);
  3251. sa4.sin_addr.s_addr = address;
  3252. #if HAVE_SOCKADDR_IN_SIN_LEN
  3253. sa4.sin_len = sizeof(sa4);
  3254. #endif
  3255. ls = GNUNET_NETWORK_socket_create (AF_INET,
  3256. SOCK_STREAM,
  3257. 0);
  3258. if (NULL == ls)
  3259. return NULL;
  3260. if (GNUNET_OK !=
  3261. GNUNET_NETWORK_socket_bind (ls,
  3262. (const struct sockaddr *) &sa4,
  3263. sizeof(sa4)))
  3264. {
  3265. eno = errno;
  3266. GNUNET_NETWORK_socket_close (ls);
  3267. errno = eno;
  3268. return NULL;
  3269. }
  3270. return ls;
  3271. }
  3272. /**
  3273. * Create an IPv6 listen socket bound to our port.
  3274. *
  3275. * @return NULL on error
  3276. */
  3277. static struct GNUNET_NETWORK_Handle *
  3278. bind_v6 ()
  3279. {
  3280. struct GNUNET_NETWORK_Handle *ls;
  3281. struct sockaddr_in6 sa6;
  3282. int eno;
  3283. memset (&sa6, 0, sizeof(sa6));
  3284. sa6.sin6_family = AF_INET6;
  3285. sa6.sin6_port = htons (port);
  3286. sa6.sin6_addr = address6;
  3287. #if HAVE_SOCKADDR_IN_SIN_LEN
  3288. sa6.sin6_len = sizeof(sa6);
  3289. #endif
  3290. ls = GNUNET_NETWORK_socket_create (AF_INET6,
  3291. SOCK_STREAM,
  3292. 0);
  3293. if (NULL == ls)
  3294. return NULL;
  3295. if (GNUNET_OK !=
  3296. GNUNET_NETWORK_socket_bind (ls,
  3297. (const struct sockaddr *) &sa6,
  3298. sizeof(sa6)))
  3299. {
  3300. eno = errno;
  3301. GNUNET_NETWORK_socket_close (ls);
  3302. errno = eno;
  3303. return NULL;
  3304. }
  3305. return ls;
  3306. }
  3307. /**
  3308. * Main function that will be run
  3309. *
  3310. * @param cls closure
  3311. * @param args remaining command-line arguments
  3312. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  3313. * @param c configuration
  3314. */
  3315. static void
  3316. run (void *cls,
  3317. char *const *args,
  3318. const char *cfgfile,
  3319. const struct GNUNET_CONFIGURATION_Handle *c)
  3320. {
  3321. char*cafile_cfg = NULL;
  3322. char*cafile;
  3323. char*addr_str;
  3324. struct MhdHttpList *hd;
  3325. cfg = c;
  3326. /* Get address to bind to */
  3327. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "gns-proxy",
  3328. "BIND_TO",
  3329. &addr_str))
  3330. {
  3331. // No address specified
  3332. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3333. "Don't know what to bind to...\n");
  3334. GNUNET_free (addr_str);
  3335. GNUNET_SCHEDULER_shutdown ();
  3336. return;
  3337. }
  3338. if (1 != inet_pton (AF_INET, addr_str, &address))
  3339. {
  3340. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3341. "Unable to parse address %s\n",
  3342. addr_str);
  3343. GNUNET_free (addr_str);
  3344. GNUNET_SCHEDULER_shutdown ();
  3345. return;
  3346. }
  3347. GNUNET_free (addr_str);
  3348. /* Get address to bind to */
  3349. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "gns-proxy",
  3350. "BIND_TO6",
  3351. &addr_str))
  3352. {
  3353. // No address specified
  3354. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3355. "Don't know what to bind6 to...\n");
  3356. GNUNET_free (addr_str);
  3357. GNUNET_SCHEDULER_shutdown ();
  3358. return;
  3359. }
  3360. if (1 != inet_pton (AF_INET6, addr_str, &address6))
  3361. {
  3362. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3363. "Unable to parse IPv6 address %s\n",
  3364. addr_str);
  3365. GNUNET_free (addr_str);
  3366. GNUNET_SCHEDULER_shutdown ();
  3367. return;
  3368. }
  3369. GNUNET_free (addr_str);
  3370. if (NULL == (curl_multi = curl_multi_init ()))
  3371. {
  3372. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3373. "Failed to create cURL multi handle!\n");
  3374. return;
  3375. }
  3376. cafile = cafile_opt;
  3377. if (NULL == cafile)
  3378. {
  3379. if (GNUNET_OK !=
  3380. GNUNET_CONFIGURATION_get_value_filename (cfg,
  3381. "gns-proxy",
  3382. "PROXY_CACERT",
  3383. &cafile_cfg))
  3384. {
  3385. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  3386. "gns-proxy",
  3387. "PROXY_CACERT");
  3388. return;
  3389. }
  3390. cafile = cafile_cfg;
  3391. }
  3392. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  3393. "Using `%s' as CA\n",
  3394. cafile);
  3395. gnutls_global_init ();
  3396. gnutls_x509_crt_init (&proxy_ca.cert);
  3397. gnutls_x509_privkey_init (&proxy_ca.key);
  3398. if ((GNUNET_OK !=
  3399. load_cert_from_file (proxy_ca.cert,
  3400. cafile)) ||
  3401. (GNUNET_OK !=
  3402. load_key_from_file (proxy_ca.key,
  3403. cafile)))
  3404. {
  3405. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3406. _ ("Failed to load X.509 key and certificate from `%s'\n"),
  3407. cafile);
  3408. gnutls_x509_crt_deinit (proxy_ca.cert);
  3409. gnutls_x509_privkey_deinit (proxy_ca.key);
  3410. gnutls_global_deinit ();
  3411. GNUNET_free (cafile_cfg);
  3412. return;
  3413. }
  3414. GNUNET_free (cafile_cfg);
  3415. if (NULL == (gns_handle = GNUNET_GNS_connect (cfg)))
  3416. {
  3417. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3418. "Unable to connect to GNS!\n");
  3419. gnutls_x509_crt_deinit (proxy_ca.cert);
  3420. gnutls_x509_privkey_deinit (proxy_ca.key);
  3421. gnutls_global_deinit ();
  3422. return;
  3423. }
  3424. GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
  3425. NULL);
  3426. /* Open listen socket for socks proxy */
  3427. lsock6 = bind_v6 ();
  3428. if (NULL == lsock6)
  3429. {
  3430. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
  3431. "bind");
  3432. }
  3433. else
  3434. {
  3435. if (GNUNET_OK !=
  3436. GNUNET_NETWORK_socket_listen (lsock6,
  3437. 5))
  3438. {
  3439. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
  3440. "listen");
  3441. GNUNET_NETWORK_socket_close (lsock6);
  3442. lsock6 = NULL;
  3443. }
  3444. else
  3445. {
  3446. ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  3447. lsock6,
  3448. &do_accept,
  3449. lsock6);
  3450. }
  3451. }
  3452. lsock4 = bind_v4 ();
  3453. if (NULL == lsock4)
  3454. {
  3455. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
  3456. "bind");
  3457. }
  3458. else
  3459. {
  3460. if (GNUNET_OK !=
  3461. GNUNET_NETWORK_socket_listen (lsock4,
  3462. 5))
  3463. {
  3464. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
  3465. "listen");
  3466. GNUNET_NETWORK_socket_close (lsock4);
  3467. lsock4 = NULL;
  3468. }
  3469. else
  3470. {
  3471. ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  3472. lsock4,
  3473. &do_accept,
  3474. lsock4);
  3475. }
  3476. }
  3477. if ((NULL == lsock4) &&
  3478. (NULL == lsock6))
  3479. {
  3480. GNUNET_SCHEDULER_shutdown ();
  3481. return;
  3482. }
  3483. if (0 != curl_global_init (CURL_GLOBAL_WIN32))
  3484. {
  3485. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3486. "cURL global init failed!\n");
  3487. GNUNET_SCHEDULER_shutdown ();
  3488. return;
  3489. }
  3490. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  3491. "Proxy listens on port %u\n",
  3492. (unsigned int) port);
  3493. /* start MHD daemon for HTTP */
  3494. hd = GNUNET_new (struct MhdHttpList);
  3495. hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET
  3496. | MHD_ALLOW_SUSPEND_RESUME,
  3497. 0,
  3498. NULL, NULL,
  3499. &create_response, hd,
  3500. MHD_OPTION_CONNECTION_TIMEOUT, (unsigned
  3501. int) 16,
  3502. MHD_OPTION_NOTIFY_COMPLETED, &mhd_completed_cb,
  3503. NULL,
  3504. MHD_OPTION_NOTIFY_CONNECTION,
  3505. &mhd_connection_cb, NULL,
  3506. MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback,
  3507. NULL,
  3508. MHD_OPTION_END);
  3509. if (NULL == hd->daemon)
  3510. {
  3511. GNUNET_free (hd);
  3512. GNUNET_SCHEDULER_shutdown ();
  3513. return;
  3514. }
  3515. httpd = hd;
  3516. GNUNET_CONTAINER_DLL_insert (mhd_httpd_head,
  3517. mhd_httpd_tail,
  3518. hd);
  3519. }
  3520. /**
  3521. * The main function for gnunet-gns-proxy.
  3522. *
  3523. * @param argc number of arguments from the command line
  3524. * @param argv command line arguments
  3525. * @return 0 ok, 1 on error
  3526. */
  3527. int
  3528. main (int argc,
  3529. char *const *argv)
  3530. {
  3531. struct GNUNET_GETOPT_CommandLineOption options[] = {
  3532. GNUNET_GETOPT_option_uint16 ('p',
  3533. "port",
  3534. NULL,
  3535. gettext_noop (
  3536. "listen on specified port (default: 7777)"),
  3537. &port),
  3538. GNUNET_GETOPT_option_string ('a',
  3539. "authority",
  3540. NULL,
  3541. gettext_noop ("pem file to use as CA"),
  3542. &cafile_opt),
  3543. GNUNET_GETOPT_option_flag ('6',
  3544. "disable-ivp6",
  3545. gettext_noop ("disable use of IPv6"),
  3546. &disable_v6),
  3547. GNUNET_GETOPT_OPTION_END
  3548. };
  3549. static const char*page =
  3550. "<html><head><title>gnunet-gns-proxy</title>"
  3551. "</head><body>cURL fail</body></html>";
  3552. int ret;
  3553. if (GNUNET_OK !=
  3554. GNUNET_STRINGS_get_utf8_args (argc, argv,
  3555. &argc, &argv))
  3556. return 2;
  3557. GNUNET_log_setup ("gnunet-gns-proxy",
  3558. "WARNING",
  3559. NULL);
  3560. curl_failure_response
  3561. = MHD_create_response_from_buffer (strlen (page),
  3562. (void *) page,
  3563. MHD_RESPMEM_PERSISTENT);
  3564. ret =
  3565. (GNUNET_OK ==
  3566. GNUNET_PROGRAM_run (argc, argv,
  3567. "gnunet-gns-proxy",
  3568. _ ("GNUnet GNS proxy"),
  3569. options,
  3570. &run, NULL)) ? 0 : 1;
  3571. MHD_destroy_response (curl_failure_response);
  3572. GNUNET_free_nz ((char *) argv);
  3573. return ret;
  3574. }
  3575. /* end of gnunet-gns-proxy.c */