gnunet-gns-proxy.c 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908
  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 certificats 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_non_null (s5r->domain);
  689. GNUNET_free_non_null (s5r->leho);
  690. GNUNET_free_non_null (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_non_null (new_cookie_hdr);
  1148. GNUNET_free_non_null (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 (i.e. "HTTP/1.1")
  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. const struct sockaddr *sa;
  1646. const struct sockaddr_in *s4;
  1647. const struct sockaddr_in6 *s6;
  1648. uint16_t port;
  1649. size_t left;
  1650. if (NULL == s5r)
  1651. {
  1652. GNUNET_break (0);
  1653. return MHD_NO;
  1654. }
  1655. s5r->con = con;
  1656. /* Fresh connection. */
  1657. if (SOCKS5_SOCKET_WITH_MHD == s5r->state)
  1658. {
  1659. /* first time here, initialize curl handle */
  1660. if (s5r->is_gns)
  1661. {
  1662. sa = (const struct sockaddr *) &s5r->destination_address;
  1663. switch (sa->sa_family)
  1664. {
  1665. case AF_INET:
  1666. s4 = (const struct sockaddr_in *) &s5r->destination_address;
  1667. if (NULL == inet_ntop (AF_INET,
  1668. &s4->sin_addr,
  1669. ipstring,
  1670. sizeof(ipstring)))
  1671. {
  1672. GNUNET_break (0);
  1673. return MHD_NO;
  1674. }
  1675. GNUNET_snprintf (ipaddr,
  1676. sizeof(ipaddr),
  1677. "%s",
  1678. ipstring);
  1679. port = ntohs (s4->sin_port);
  1680. break;
  1681. case AF_INET6:
  1682. s6 = (const struct sockaddr_in6 *) &s5r->destination_address;
  1683. if (NULL == inet_ntop (AF_INET6,
  1684. &s6->sin6_addr,
  1685. ipstring,
  1686. sizeof(ipstring)))
  1687. {
  1688. GNUNET_break (0);
  1689. return MHD_NO;
  1690. }
  1691. GNUNET_snprintf (ipaddr,
  1692. sizeof(ipaddr),
  1693. "%s",
  1694. ipstring);
  1695. port = ntohs (s6->sin6_port);
  1696. break;
  1697. default:
  1698. GNUNET_break (0);
  1699. return MHD_NO;
  1700. }
  1701. }
  1702. else
  1703. {
  1704. port = s5r->port;
  1705. }
  1706. if (NULL == s5r->curl)
  1707. s5r->curl = curl_easy_init ();
  1708. if (NULL == s5r->curl)
  1709. return MHD_queue_response (con,
  1710. MHD_HTTP_INTERNAL_SERVER_ERROR,
  1711. curl_failure_response);
  1712. curl_easy_setopt (s5r->curl,
  1713. CURLOPT_HEADERFUNCTION,
  1714. &curl_check_hdr);
  1715. curl_easy_setopt (s5r->curl,
  1716. CURLOPT_HEADERDATA,
  1717. s5r);
  1718. curl_easy_setopt (s5r->curl,
  1719. CURLOPT_FOLLOWLOCATION,
  1720. 0);
  1721. if (s5r->is_gns)
  1722. curl_easy_setopt (s5r->curl,
  1723. CURLOPT_IPRESOLVE,
  1724. CURL_IPRESOLVE_V4);
  1725. curl_easy_setopt (s5r->curl,
  1726. CURLOPT_CONNECTTIMEOUT,
  1727. 600L);
  1728. curl_easy_setopt (s5r->curl,
  1729. CURLOPT_TIMEOUT,
  1730. 600L);
  1731. curl_easy_setopt (s5r->curl,
  1732. CURLOPT_NOSIGNAL,
  1733. 1L);
  1734. curl_easy_setopt (s5r->curl,
  1735. CURLOPT_HTTP_CONTENT_DECODING,
  1736. 0);
  1737. curl_easy_setopt (s5r->curl,
  1738. CURLOPT_NOSIGNAL,
  1739. 1L);
  1740. curl_easy_setopt (s5r->curl,
  1741. CURLOPT_PRIVATE,
  1742. s5r);
  1743. curl_easy_setopt (s5r->curl,
  1744. CURLOPT_VERBOSE,
  1745. 0L);
  1746. /**
  1747. * Pre-populate cache to resolve Hostname.
  1748. * This is necessary as the DNS name in the CURLOPT_URL is used
  1749. * for SNI http://de.wikipedia.org/wiki/Server_Name_Indication
  1750. */if (NULL != s5r->leho)
  1751. {
  1752. char *curl_hosts;
  1753. GNUNET_asprintf (&curl_hosts,
  1754. "%s:%d:%s",
  1755. s5r->leho,
  1756. port,
  1757. ipaddr);
  1758. s5r->hosts = curl_slist_append (NULL,
  1759. curl_hosts);
  1760. curl_easy_setopt (s5r->curl,
  1761. CURLOPT_RESOLVE,
  1762. s5r->hosts);
  1763. GNUNET_free (curl_hosts);
  1764. }
  1765. if (s5r->is_gns)
  1766. {
  1767. GNUNET_asprintf (&curlurl,
  1768. (GNUNET_YES != s5r->is_tls) // (HTTPS_PORT != s5r->port)
  1769. ? "http://%s:%d%s"
  1770. : "https://%s:%d%s",
  1771. (NULL != s5r->leho)
  1772. ? s5r->leho
  1773. : ipaddr,
  1774. port,
  1775. s5r->url);
  1776. }
  1777. else
  1778. {
  1779. GNUNET_asprintf (&curlurl,
  1780. (GNUNET_YES != s5r->is_tls) // (HTTPS_PORT != s5r->port)
  1781. ? "http://%s:%d%s"
  1782. : "https://%s:%d%s",
  1783. s5r->domain,
  1784. port,
  1785. s5r->url);
  1786. }
  1787. curl_easy_setopt (s5r->curl,
  1788. CURLOPT_URL,
  1789. curlurl);
  1790. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1791. "Launching %s CURL interaction, fetching `%s'\n",
  1792. (s5r->is_gns) ? "GNS" : "DNS",
  1793. curlurl);
  1794. GNUNET_free (curlurl);
  1795. if (0 == strcasecmp (meth,
  1796. MHD_HTTP_METHOD_PUT))
  1797. {
  1798. s5r->state = SOCKS5_SOCKET_UPLOAD_STARTED;
  1799. curl_easy_setopt (s5r->curl,
  1800. CURLOPT_UPLOAD,
  1801. 1L);
  1802. curl_easy_setopt (s5r->curl,
  1803. CURLOPT_WRITEFUNCTION,
  1804. &curl_download_cb);
  1805. curl_easy_setopt (s5r->curl,
  1806. CURLOPT_WRITEDATA,
  1807. s5r);
  1808. GNUNET_assert (CURLE_OK ==
  1809. curl_easy_setopt (s5r->curl,
  1810. CURLOPT_READFUNCTION,
  1811. &curl_upload_cb));
  1812. curl_easy_setopt (s5r->curl,
  1813. CURLOPT_READDATA,
  1814. s5r);
  1815. {
  1816. const char *us;
  1817. long upload_size = 0;
  1818. us = MHD_lookup_connection_value (con,
  1819. MHD_HEADER_KIND,
  1820. MHD_HTTP_HEADER_CONTENT_LENGTH);
  1821. if ((1 == sscanf (us,
  1822. "%ld",
  1823. &upload_size)) &&
  1824. (upload_size >= 0))
  1825. {
  1826. curl_easy_setopt (s5r->curl,
  1827. CURLOPT_INFILESIZE,
  1828. upload_size);
  1829. }
  1830. }
  1831. }
  1832. else if (0 == strcasecmp (meth, MHD_HTTP_METHOD_POST))
  1833. {
  1834. s5r->state = SOCKS5_SOCKET_UPLOAD_STARTED;
  1835. curl_easy_setopt (s5r->curl,
  1836. CURLOPT_POST,
  1837. 1L);
  1838. curl_easy_setopt (s5r->curl,
  1839. CURLOPT_WRITEFUNCTION,
  1840. &curl_download_cb);
  1841. curl_easy_setopt (s5r->curl,
  1842. CURLOPT_WRITEDATA,
  1843. s5r);
  1844. curl_easy_setopt (s5r->curl,
  1845. CURLOPT_READFUNCTION,
  1846. &curl_upload_cb);
  1847. curl_easy_setopt (s5r->curl,
  1848. CURLOPT_READDATA,
  1849. s5r);
  1850. {
  1851. const char *us;
  1852. long upload_size;
  1853. upload_size = 0;
  1854. us = MHD_lookup_connection_value (con,
  1855. MHD_HEADER_KIND,
  1856. MHD_HTTP_HEADER_CONTENT_LENGTH);
  1857. if ((NULL != us) &&
  1858. (1 == sscanf (us,
  1859. "%ld",
  1860. &upload_size)) &&
  1861. (upload_size >= 0))
  1862. {
  1863. curl_easy_setopt (s5r->curl,
  1864. CURLOPT_INFILESIZE,
  1865. upload_size);
  1866. }
  1867. else
  1868. {
  1869. curl_easy_setopt (s5r->curl,
  1870. CURLOPT_INFILESIZE,
  1871. upload_size);
  1872. }
  1873. }
  1874. }
  1875. else if (0 == strcasecmp (meth,
  1876. MHD_HTTP_METHOD_HEAD))
  1877. {
  1878. s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED;
  1879. curl_easy_setopt (s5r->curl,
  1880. CURLOPT_NOBODY,
  1881. 1L);
  1882. }
  1883. else if (0 == strcasecmp (meth,
  1884. MHD_HTTP_METHOD_OPTIONS))
  1885. {
  1886. s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED;
  1887. curl_easy_setopt (s5r->curl,
  1888. CURLOPT_CUSTOMREQUEST,
  1889. "OPTIONS");
  1890. curl_easy_setopt (s5r->curl,
  1891. CURLOPT_WRITEFUNCTION,
  1892. &curl_download_cb);
  1893. curl_easy_setopt (s5r->curl,
  1894. CURLOPT_WRITEDATA,
  1895. s5r);
  1896. }
  1897. else if (0 == strcasecmp (meth,
  1898. MHD_HTTP_METHOD_GET))
  1899. {
  1900. s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED;
  1901. curl_easy_setopt (s5r->curl,
  1902. CURLOPT_HTTPGET,
  1903. 1L);
  1904. curl_easy_setopt (s5r->curl,
  1905. CURLOPT_WRITEFUNCTION,
  1906. &curl_download_cb);
  1907. curl_easy_setopt (s5r->curl,
  1908. CURLOPT_WRITEDATA,
  1909. s5r);
  1910. }
  1911. else if (0 == strcasecmp (meth,
  1912. MHD_HTTP_METHOD_DELETE))
  1913. {
  1914. s5r->state = SOCKS5_SOCKET_DOWNLOAD_STARTED;
  1915. curl_easy_setopt (s5r->curl,
  1916. CURLOPT_CUSTOMREQUEST,
  1917. "DELETE");
  1918. curl_easy_setopt (s5r->curl,
  1919. CURLOPT_WRITEFUNCTION,
  1920. &curl_download_cb);
  1921. curl_easy_setopt (s5r->curl,
  1922. CURLOPT_WRITEDATA,
  1923. s5r);
  1924. }
  1925. else
  1926. {
  1927. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1928. _ ("Unsupported HTTP method `%s'\n"),
  1929. meth);
  1930. curl_easy_cleanup (s5r->curl);
  1931. s5r->curl = NULL;
  1932. return MHD_NO;
  1933. }
  1934. if (0 == strcasecmp (ver, MHD_HTTP_VERSION_1_0))
  1935. {
  1936. curl_easy_setopt (s5r->curl,
  1937. CURLOPT_HTTP_VERSION,
  1938. CURL_HTTP_VERSION_1_0);
  1939. }
  1940. else if (0 == strcasecmp (ver, MHD_HTTP_VERSION_1_1))
  1941. {
  1942. curl_easy_setopt (s5r->curl,
  1943. CURLOPT_HTTP_VERSION,
  1944. CURL_HTTP_VERSION_1_1);
  1945. }
  1946. else
  1947. {
  1948. curl_easy_setopt (s5r->curl,
  1949. CURLOPT_HTTP_VERSION,
  1950. CURL_HTTP_VERSION_NONE);
  1951. }
  1952. if (GNUNET_YES == s5r->is_tls) // (HTTPS_PORT == s5r->port)
  1953. {
  1954. curl_easy_setopt (s5r->curl,
  1955. CURLOPT_USE_SSL,
  1956. CURLUSESSL_ALL);
  1957. if (0 < s5r->num_danes)
  1958. curl_easy_setopt (s5r->curl,
  1959. CURLOPT_SSL_VERIFYPEER,
  1960. 0L);
  1961. else
  1962. curl_easy_setopt (s5r->curl,
  1963. CURLOPT_SSL_VERIFYPEER,
  1964. 1L);
  1965. /* Disable cURL checking the hostname, as we will check ourselves
  1966. as only we have the domain name or the LEHO or the DANE record */
  1967. curl_easy_setopt (s5r->curl,
  1968. CURLOPT_SSL_VERIFYHOST,
  1969. 0L);
  1970. }
  1971. else
  1972. {
  1973. curl_easy_setopt (s5r->curl,
  1974. CURLOPT_USE_SSL,
  1975. CURLUSESSL_NONE);
  1976. }
  1977. if (CURLM_OK !=
  1978. curl_multi_add_handle (curl_multi,
  1979. s5r->curl))
  1980. {
  1981. GNUNET_break (0);
  1982. curl_easy_cleanup (s5r->curl);
  1983. s5r->curl = NULL;
  1984. return MHD_NO;
  1985. }
  1986. MHD_get_connection_values (con,
  1987. MHD_HEADER_KIND,
  1988. (MHD_KeyValueIterator) & con_val_iter,
  1989. s5r);
  1990. curl_easy_setopt (s5r->curl,
  1991. CURLOPT_HTTPHEADER,
  1992. s5r->headers);
  1993. curl_download_prepare ();
  1994. return MHD_YES;
  1995. }
  1996. /* continuing to process request */
  1997. if (0 != *upload_data_size)
  1998. {
  1999. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2000. "Processing %u bytes UPLOAD\n",
  2001. (unsigned int) *upload_data_size);
  2002. /* FIXME: This must be set or a header with Transfer-Encoding: chunked. Else
  2003. * upload callback is not called!
  2004. */
  2005. curl_easy_setopt (s5r->curl,
  2006. CURLOPT_POSTFIELDSIZE,
  2007. *upload_data_size);
  2008. left = GNUNET_MIN (*upload_data_size,
  2009. sizeof(s5r->io_buf) - s5r->io_len);
  2010. GNUNET_memcpy (&s5r->io_buf[s5r->io_len],
  2011. upload_data,
  2012. left);
  2013. s5r->io_len += left;
  2014. *upload_data_size -= left;
  2015. GNUNET_assert (NULL != s5r->curl);
  2016. if (GNUNET_YES == s5r->curl_paused)
  2017. {
  2018. s5r->curl_paused = GNUNET_NO;
  2019. curl_easy_pause (s5r->curl,
  2020. CURLPAUSE_CONT);
  2021. }
  2022. return MHD_YES;
  2023. }
  2024. if (SOCKS5_SOCKET_UPLOAD_STARTED == s5r->state)
  2025. {
  2026. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2027. "Finished processing UPLOAD\n");
  2028. s5r->state = SOCKS5_SOCKET_UPLOAD_DONE;
  2029. }
  2030. if (NULL == s5r->response)
  2031. {
  2032. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2033. "Waiting for HTTP response for %s%s...\n",
  2034. s5r->domain,
  2035. s5r->url);
  2036. MHD_suspend_connection (con);
  2037. s5r->suspended = GNUNET_YES;
  2038. return MHD_YES;
  2039. }
  2040. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2041. "Queueing response for %s%s with MHD\n",
  2042. s5r->domain,
  2043. s5r->url);
  2044. run_mhd_now (s5r->hd);
  2045. return MHD_queue_response (con,
  2046. s5r->response_code,
  2047. s5r->response);
  2048. }
  2049. /* ******************** MHD HTTP setup and event loop ******************** */
  2050. /**
  2051. * Function called when MHD decides that we are done with a request.
  2052. *
  2053. * @param cls NULL
  2054. * @param connection connection handle
  2055. * @param con_cls value as set by the last call to
  2056. * the MHD_AccessHandlerCallback, should be our `struct Socks5Request *`
  2057. * @param toe reason for request termination (ignored)
  2058. */
  2059. static void
  2060. mhd_completed_cb (void *cls,
  2061. struct MHD_Connection *connection,
  2062. void **con_cls,
  2063. enum MHD_RequestTerminationCode toe)
  2064. {
  2065. struct Socks5Request *s5r = *con_cls;
  2066. if (NULL == s5r)
  2067. return;
  2068. if (MHD_REQUEST_TERMINATED_COMPLETED_OK != toe)
  2069. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2070. "MHD encountered error handling request: %d\n",
  2071. toe);
  2072. if (NULL != s5r->curl)
  2073. {
  2074. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2075. "Removing cURL handle (MHD interaction complete)\n");
  2076. curl_multi_remove_handle (curl_multi,
  2077. s5r->curl);
  2078. curl_slist_free_all (s5r->headers);
  2079. s5r->headers = NULL;
  2080. curl_easy_reset (s5r->curl);
  2081. s5r->rbuf_len = 0;
  2082. s5r->wbuf_len = 0;
  2083. s5r->io_len = 0;
  2084. curl_download_prepare ();
  2085. }
  2086. if ((NULL != s5r->response) &&
  2087. (curl_failure_response != s5r->response))
  2088. MHD_destroy_response (s5r->response);
  2089. for (struct HttpResponseHeader *header = s5r->header_head;
  2090. NULL != header;
  2091. header = s5r->header_head)
  2092. {
  2093. GNUNET_CONTAINER_DLL_remove (s5r->header_head,
  2094. s5r->header_tail,
  2095. header);
  2096. GNUNET_free (header->type);
  2097. GNUNET_free (header->value);
  2098. GNUNET_free (header);
  2099. }
  2100. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2101. "Finished request for %s\n",
  2102. s5r->url);
  2103. GNUNET_free (s5r->url);
  2104. s5r->state = SOCKS5_SOCKET_WITH_MHD;
  2105. s5r->url = NULL;
  2106. s5r->response = NULL;
  2107. *con_cls = NULL;
  2108. }
  2109. /**
  2110. * Function called when MHD connection is opened or closed.
  2111. *
  2112. * @param cls NULL
  2113. * @param connection connection handle
  2114. * @param con_cls value as set by the last call to
  2115. * the MHD_AccessHandlerCallback, should be our `struct Socks5Request *`
  2116. * @param toe connection notification type
  2117. */
  2118. static void
  2119. mhd_connection_cb (void *cls,
  2120. struct MHD_Connection *connection,
  2121. void **con_cls,
  2122. enum MHD_ConnectionNotificationCode cnc)
  2123. {
  2124. struct Socks5Request *s5r;
  2125. const union MHD_ConnectionInfo *ci;
  2126. int sock;
  2127. switch (cnc)
  2128. {
  2129. case MHD_CONNECTION_NOTIFY_STARTED:
  2130. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connection started...\n");
  2131. ci = MHD_get_connection_info (connection,
  2132. MHD_CONNECTION_INFO_CONNECTION_FD);
  2133. if (NULL == ci)
  2134. {
  2135. GNUNET_break (0);
  2136. return;
  2137. }
  2138. sock = ci->connect_fd;
  2139. for (s5r = s5r_head; NULL != s5r; s5r = s5r->next)
  2140. {
  2141. if (GNUNET_NETWORK_get_fd (s5r->sock) == sock)
  2142. {
  2143. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2144. "Context set...\n");
  2145. s5r->ssl_checked = GNUNET_NO;
  2146. *con_cls = s5r;
  2147. break;
  2148. }
  2149. }
  2150. break;
  2151. case MHD_CONNECTION_NOTIFY_CLOSED:
  2152. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2153. "Connection closed... cleaning up\n");
  2154. s5r = *con_cls;
  2155. if (NULL == s5r)
  2156. {
  2157. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2158. "Connection stale!\n");
  2159. return;
  2160. }
  2161. cleanup_s5r (s5r);
  2162. curl_download_prepare ();
  2163. *con_cls = NULL;
  2164. break;
  2165. default:
  2166. GNUNET_break (0);
  2167. }
  2168. }
  2169. /**
  2170. * Function called when MHD first processes an incoming connection.
  2171. * Gives us the respective URI information.
  2172. *
  2173. * We use this to associate the `struct MHD_Connection` with our
  2174. * internal `struct Socks5Request` data structure (by checking
  2175. * for matching sockets).
  2176. *
  2177. * @param cls the HTTP server handle (a `struct MhdHttpList`)
  2178. * @param url the URL that is being requested
  2179. * @param connection MHD connection object for the request
  2180. * @return the `struct Socks5Request` that this @a connection is for
  2181. */
  2182. static void *
  2183. mhd_log_callback (void *cls,
  2184. const char *url,
  2185. struct MHD_Connection *connection)
  2186. {
  2187. struct Socks5Request *s5r;
  2188. const union MHD_ConnectionInfo *ci;
  2189. ci = MHD_get_connection_info (connection,
  2190. MHD_CONNECTION_INFO_SOCKET_CONTEXT);
  2191. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Processing %s\n", url);
  2192. if (NULL == ci)
  2193. {
  2194. GNUNET_break (0);
  2195. return NULL;
  2196. }
  2197. s5r = ci->socket_context;
  2198. if (NULL != s5r->url)
  2199. {
  2200. GNUNET_break (0);
  2201. return NULL;
  2202. }
  2203. s5r->url = GNUNET_strdup (url);
  2204. if (NULL != s5r->timeout_task)
  2205. {
  2206. GNUNET_SCHEDULER_cancel (s5r->timeout_task);
  2207. s5r->timeout_task = NULL;
  2208. }
  2209. GNUNET_assert (s5r->state == SOCKS5_SOCKET_WITH_MHD);
  2210. return s5r;
  2211. }
  2212. /**
  2213. * Kill the given MHD daemon.
  2214. *
  2215. * @param hd daemon to stop
  2216. */
  2217. static void
  2218. kill_httpd (struct MhdHttpList *hd)
  2219. {
  2220. GNUNET_CONTAINER_DLL_remove (mhd_httpd_head,
  2221. mhd_httpd_tail,
  2222. hd);
  2223. GNUNET_free_non_null (hd->domain);
  2224. MHD_stop_daemon (hd->daemon);
  2225. if (NULL != hd->httpd_task)
  2226. {
  2227. GNUNET_SCHEDULER_cancel (hd->httpd_task);
  2228. hd->httpd_task = NULL;
  2229. }
  2230. GNUNET_free_non_null (hd->proxy_cert);
  2231. if (hd == httpd)
  2232. httpd = NULL;
  2233. GNUNET_free (hd);
  2234. }
  2235. /**
  2236. * Task run whenever HTTP server is idle for too long. Kill it.
  2237. *
  2238. * @param cls the `struct MhdHttpList *`
  2239. */
  2240. static void
  2241. kill_httpd_task (void *cls)
  2242. {
  2243. struct MhdHttpList *hd = cls;
  2244. hd->httpd_task = NULL;
  2245. kill_httpd (hd);
  2246. }
  2247. /**
  2248. * Task run whenever HTTP server operations are pending.
  2249. *
  2250. * @param cls the `struct MhdHttpList *` of the daemon that is being run
  2251. */
  2252. static void
  2253. do_httpd (void *cls);
  2254. /**
  2255. * Schedule MHD. This function should be called initially when an
  2256. * MHD is first getting its client socket, and will then automatically
  2257. * always be called later whenever there is work to be done.
  2258. *
  2259. * @param hd the daemon to schedule
  2260. */
  2261. static void
  2262. schedule_httpd (struct MhdHttpList *hd)
  2263. {
  2264. fd_set rs;
  2265. fd_set ws;
  2266. fd_set es;
  2267. struct GNUNET_NETWORK_FDSet *wrs;
  2268. struct GNUNET_NETWORK_FDSet *wws;
  2269. int max;
  2270. int haveto;
  2271. MHD_UNSIGNED_LONG_LONG timeout;
  2272. struct GNUNET_TIME_Relative tv;
  2273. FD_ZERO (&rs);
  2274. FD_ZERO (&ws);
  2275. FD_ZERO (&es);
  2276. max = -1;
  2277. if (MHD_YES !=
  2278. MHD_get_fdset (hd->daemon,
  2279. &rs,
  2280. &ws,
  2281. &es,
  2282. &max))
  2283. {
  2284. kill_httpd (hd);
  2285. return;
  2286. }
  2287. haveto = MHD_get_timeout (hd->daemon,
  2288. &timeout);
  2289. if (MHD_YES == haveto)
  2290. tv.rel_value_us = (uint64_t) timeout * 1000LL;
  2291. else
  2292. tv = GNUNET_TIME_UNIT_FOREVER_REL;
  2293. if (-1 != max)
  2294. {
  2295. wrs = GNUNET_NETWORK_fdset_create ();
  2296. wws = GNUNET_NETWORK_fdset_create ();
  2297. GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1);
  2298. GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1);
  2299. }
  2300. else
  2301. {
  2302. wrs = NULL;
  2303. wws = NULL;
  2304. }
  2305. if (NULL != hd->httpd_task)
  2306. {
  2307. GNUNET_SCHEDULER_cancel (hd->httpd_task);
  2308. hd->httpd_task = NULL;
  2309. }
  2310. if ((MHD_YES != haveto) &&
  2311. (-1 == max) &&
  2312. (hd != httpd))
  2313. {
  2314. /* daemon is idle, kill after timeout */
  2315. hd->httpd_task = GNUNET_SCHEDULER_add_delayed (MHD_CACHE_TIMEOUT,
  2316. &kill_httpd_task,
  2317. hd);
  2318. }
  2319. else
  2320. {
  2321. hd->httpd_task =
  2322. GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
  2323. tv, wrs, wws,
  2324. &do_httpd, hd);
  2325. }
  2326. if (NULL != wrs)
  2327. GNUNET_NETWORK_fdset_destroy (wrs);
  2328. if (NULL != wws)
  2329. GNUNET_NETWORK_fdset_destroy (wws);
  2330. }
  2331. /**
  2332. * Task run whenever HTTP server operations are pending.
  2333. *
  2334. * @param cls the `struct MhdHttpList` of the daemon that is being run
  2335. */
  2336. static void
  2337. do_httpd (void *cls)
  2338. {
  2339. struct MhdHttpList *hd = cls;
  2340. hd->httpd_task = NULL;
  2341. MHD_run (hd->daemon);
  2342. schedule_httpd (hd);
  2343. }
  2344. /**
  2345. * Run MHD now, we have extra data ready for the callback.
  2346. *
  2347. * @param hd the daemon to run now.
  2348. */
  2349. static void
  2350. run_mhd_now (struct MhdHttpList *hd)
  2351. {
  2352. if (NULL != hd->httpd_task)
  2353. GNUNET_SCHEDULER_cancel (hd->httpd_task);
  2354. hd->httpd_task = GNUNET_SCHEDULER_add_now (&do_httpd,
  2355. hd);
  2356. }
  2357. /**
  2358. * Read file in filename
  2359. *
  2360. * @param filename file to read
  2361. * @param size pointer where filesize is stored
  2362. * @return NULL on error
  2363. */
  2364. static void*
  2365. load_file (const char*filename,
  2366. unsigned int*size)
  2367. {
  2368. void *buffer;
  2369. uint64_t fsize;
  2370. if (GNUNET_OK !=
  2371. GNUNET_DISK_file_size (filename,
  2372. &fsize,
  2373. GNUNET_YES,
  2374. GNUNET_YES))
  2375. return NULL;
  2376. if (fsize > MAX_PEM_SIZE)
  2377. return NULL;
  2378. *size = (unsigned int) fsize;
  2379. buffer = GNUNET_malloc (*size);
  2380. if (fsize !=
  2381. GNUNET_DISK_fn_read (filename,
  2382. buffer,
  2383. (size_t) fsize))
  2384. {
  2385. GNUNET_free (buffer);
  2386. return NULL;
  2387. }
  2388. return buffer;
  2389. }
  2390. /**
  2391. * Load PEM key from file
  2392. *
  2393. * @param key where to store the data
  2394. * @param keyfile path to the PEM file
  2395. * @return #GNUNET_OK on success
  2396. */
  2397. static int
  2398. load_key_from_file (gnutls_x509_privkey_t key,
  2399. const char*keyfile)
  2400. {
  2401. gnutls_datum_t key_data;
  2402. int ret;
  2403. key_data.data = load_file (keyfile,
  2404. &key_data.size);
  2405. if (NULL == key_data.data)
  2406. return GNUNET_SYSERR;
  2407. ret = gnutls_x509_privkey_import (key, &key_data,
  2408. GNUTLS_X509_FMT_PEM);
  2409. if (GNUTLS_E_SUCCESS != ret)
  2410. {
  2411. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2412. _ ("Unable to import private key from file `%s'\n"),
  2413. keyfile);
  2414. }
  2415. GNUNET_free_non_null (key_data.data);
  2416. return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
  2417. }
  2418. /**
  2419. * Load cert from file
  2420. *
  2421. * @param crt struct to store data in
  2422. * @param certfile path to pem file
  2423. * @return #GNUNET_OK on success
  2424. */
  2425. static int
  2426. load_cert_from_file (gnutls_x509_crt_t crt,
  2427. const char*certfile)
  2428. {
  2429. gnutls_datum_t cert_data;
  2430. int ret;
  2431. cert_data.data = load_file (certfile,
  2432. &cert_data.size);
  2433. if (NULL == cert_data.data)
  2434. return GNUNET_SYSERR;
  2435. ret = gnutls_x509_crt_import (crt,
  2436. &cert_data,
  2437. GNUTLS_X509_FMT_PEM);
  2438. if (GNUTLS_E_SUCCESS != ret)
  2439. {
  2440. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2441. _ ("Unable to import certificate from `%s'\n"),
  2442. certfile);
  2443. }
  2444. GNUNET_free_non_null (cert_data.data);
  2445. return (GNUTLS_E_SUCCESS != ret) ? GNUNET_SYSERR : GNUNET_OK;
  2446. }
  2447. /**
  2448. * Generate new certificate for specific name
  2449. *
  2450. * @param name the subject name to generate a cert for
  2451. * @return a struct holding the PEM data, NULL on error
  2452. */
  2453. static struct ProxyGNSCertificate *
  2454. generate_gns_certificate (const char *name)
  2455. {
  2456. unsigned int serial;
  2457. size_t key_buf_size;
  2458. size_t cert_buf_size;
  2459. gnutls_x509_crt_t request;
  2460. time_t etime;
  2461. struct tm *tm_data;
  2462. struct ProxyGNSCertificate *pgc;
  2463. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2464. "Generating x.509 certificate for `%s'\n",
  2465. name);
  2466. GNUNET_break (GNUTLS_E_SUCCESS == gnutls_x509_crt_init (&request));
  2467. GNUNET_break (GNUTLS_E_SUCCESS == gnutls_x509_crt_set_key (request,
  2468. proxy_ca.key));
  2469. pgc = GNUNET_new (struct ProxyGNSCertificate);
  2470. gnutls_x509_crt_set_dn_by_oid (request,
  2471. GNUTLS_OID_X520_COUNTRY_NAME,
  2472. 0,
  2473. "ZZ",
  2474. strlen ("ZZ"));
  2475. gnutls_x509_crt_set_dn_by_oid (request,
  2476. GNUTLS_OID_X520_ORGANIZATION_NAME,
  2477. 0,
  2478. "GNU Name System",
  2479. strlen ("GNU Name System"));
  2480. gnutls_x509_crt_set_dn_by_oid (request,
  2481. GNUTLS_OID_X520_COMMON_NAME,
  2482. 0,
  2483. name,
  2484. strlen (name));
  2485. gnutls_x509_crt_set_subject_alternative_name (request,
  2486. GNUTLS_SAN_DNSNAME,
  2487. name);
  2488. GNUNET_break (GNUTLS_E_SUCCESS ==
  2489. gnutls_x509_crt_set_version (request,
  2490. 3));
  2491. gnutls_rnd (GNUTLS_RND_NONCE,
  2492. &serial,
  2493. sizeof(serial));
  2494. gnutls_x509_crt_set_serial (request,
  2495. &serial,
  2496. sizeof(serial));
  2497. etime = time (NULL);
  2498. tm_data = localtime (&etime);
  2499. tm_data->tm_hour--;
  2500. etime = mktime (tm_data);
  2501. gnutls_x509_crt_set_activation_time (request,
  2502. etime);
  2503. tm_data->tm_year++;
  2504. etime = mktime (tm_data);
  2505. gnutls_x509_crt_set_expiration_time (request,
  2506. etime);
  2507. gnutls_x509_crt_sign2 (request,
  2508. proxy_ca.cert,
  2509. proxy_ca.key,
  2510. GNUTLS_DIG_SHA512,
  2511. 0);
  2512. key_buf_size = sizeof(pgc->key);
  2513. cert_buf_size = sizeof(pgc->cert);
  2514. gnutls_x509_crt_export (request,
  2515. GNUTLS_X509_FMT_PEM,
  2516. pgc->cert,
  2517. &cert_buf_size);
  2518. gnutls_x509_privkey_export (proxy_ca.key,
  2519. GNUTLS_X509_FMT_PEM,
  2520. pgc->key,
  2521. &key_buf_size);
  2522. gnutls_x509_crt_deinit (request);
  2523. return pgc;
  2524. }
  2525. /**
  2526. * Function called by MHD with errors, suppresses them all.
  2527. *
  2528. * @param cls closure
  2529. * @param fm format string (`printf()`-style)
  2530. * @param ap arguments to @a fm
  2531. */
  2532. static void
  2533. mhd_error_log_callback (void *cls,
  2534. const char *fm,
  2535. va_list ap)
  2536. {
  2537. /* do nothing */
  2538. }
  2539. /**
  2540. * Lookup (or create) an TLS MHD instance for a particular domain.
  2541. *
  2542. * @param domain the domain the TLS daemon has to serve
  2543. * @return NULL on error
  2544. */
  2545. static struct MhdHttpList *
  2546. lookup_ssl_httpd (const char*domain)
  2547. {
  2548. struct MhdHttpList *hd;
  2549. struct ProxyGNSCertificate *pgc;
  2550. if (NULL == domain)
  2551. {
  2552. GNUNET_break (0);
  2553. return NULL;
  2554. }
  2555. for (hd = mhd_httpd_head; NULL != hd; hd = hd->next)
  2556. if ((NULL != hd->domain) &&
  2557. (0 == strcmp (hd->domain, domain)))
  2558. return hd;
  2559. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2560. "Starting fresh MHD HTTPS instance for domain `%s'\n",
  2561. domain);
  2562. pgc = generate_gns_certificate (domain);
  2563. hd = GNUNET_new (struct MhdHttpList);
  2564. hd->is_ssl = GNUNET_YES;
  2565. hd->domain = GNUNET_strdup (domain);
  2566. hd->proxy_cert = pgc;
  2567. hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_SSL
  2568. | MHD_USE_NO_LISTEN_SOCKET
  2569. | MHD_ALLOW_SUSPEND_RESUME,
  2570. 0,
  2571. NULL, NULL,
  2572. &create_response, hd,
  2573. MHD_OPTION_CONNECTION_TIMEOUT, (unsigned
  2574. int) 16,
  2575. MHD_OPTION_NOTIFY_COMPLETED, &mhd_completed_cb,
  2576. NULL,
  2577. MHD_OPTION_NOTIFY_CONNECTION,
  2578. &mhd_connection_cb, NULL,
  2579. MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback,
  2580. NULL,
  2581. MHD_OPTION_EXTERNAL_LOGGER,
  2582. &mhd_error_log_callback, NULL,
  2583. MHD_OPTION_HTTPS_MEM_KEY, pgc->key,
  2584. MHD_OPTION_HTTPS_MEM_CERT, pgc->cert,
  2585. MHD_OPTION_END);
  2586. if (NULL == hd->daemon)
  2587. {
  2588. GNUNET_free (pgc);
  2589. GNUNET_free (hd);
  2590. return NULL;
  2591. }
  2592. GNUNET_CONTAINER_DLL_insert (mhd_httpd_head,
  2593. mhd_httpd_tail,
  2594. hd);
  2595. return hd;
  2596. }
  2597. /**
  2598. * Task run when a Socks5Request somehow fails to be associated with
  2599. * an MHD connection (i.e. because the client never speaks HTTP after
  2600. * the SOCKS5 handshake). Clean up.
  2601. *
  2602. * @param cls the `struct Socks5Request *`
  2603. */
  2604. static void
  2605. timeout_s5r_handshake (void *cls)
  2606. {
  2607. struct Socks5Request *s5r = cls;
  2608. s5r->timeout_task = NULL;
  2609. cleanup_s5r (s5r);
  2610. }
  2611. /**
  2612. * We're done with the Socks5 protocol, now we need to pass the
  2613. * connection data through to the final destination, either
  2614. * direct (if the protocol might not be HTTP), or via MHD
  2615. * (if the port looks like it should be HTTP).
  2616. *
  2617. * @param s5r socks request that has reached the final stage
  2618. */
  2619. static void
  2620. setup_data_transfer (struct Socks5Request *s5r)
  2621. {
  2622. struct MhdHttpList *hd;
  2623. int fd;
  2624. const struct sockaddr *addr;
  2625. socklen_t len;
  2626. char *domain;
  2627. if (GNUNET_YES == s5r->is_tls)
  2628. {
  2629. GNUNET_asprintf (&domain,
  2630. "%s",
  2631. s5r->domain);
  2632. hd = lookup_ssl_httpd (domain);
  2633. if (NULL == hd)
  2634. {
  2635. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2636. _ ("Failed to start HTTPS server for `%s'\n"),
  2637. s5r->domain);
  2638. cleanup_s5r (s5r);
  2639. GNUNET_free (domain);
  2640. return;
  2641. }
  2642. }
  2643. else
  2644. {
  2645. domain = NULL;
  2646. GNUNET_assert (NULL != httpd);
  2647. hd = httpd;
  2648. }
  2649. fd = GNUNET_NETWORK_get_fd (s5r->sock);
  2650. addr = GNUNET_NETWORK_get_addr (s5r->sock);
  2651. len = GNUNET_NETWORK_get_addrlen (s5r->sock);
  2652. s5r->state = SOCKS5_SOCKET_WITH_MHD;
  2653. if (MHD_YES !=
  2654. MHD_add_connection (hd->daemon,
  2655. fd,
  2656. addr,
  2657. len))
  2658. {
  2659. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2660. _ ("Failed to pass client to MHD\n"));
  2661. cleanup_s5r (s5r);
  2662. GNUNET_free_non_null (domain);
  2663. return;
  2664. }
  2665. s5r->hd = hd;
  2666. schedule_httpd (hd);
  2667. s5r->timeout_task = GNUNET_SCHEDULER_add_delayed (HTTP_HANDSHAKE_TIMEOUT,
  2668. &timeout_s5r_handshake,
  2669. s5r);
  2670. GNUNET_free_non_null (domain);
  2671. }
  2672. /* ********************* SOCKS handling ************************* */
  2673. /**
  2674. * Write data from buffer to socks5 client, then continue with state machine.
  2675. *
  2676. * @param cls the closure with the `struct Socks5Request`
  2677. */
  2678. static void
  2679. do_write (void *cls)
  2680. {
  2681. struct Socks5Request *s5r = cls;
  2682. ssize_t len;
  2683. s5r->wtask = NULL;
  2684. len = GNUNET_NETWORK_socket_send (s5r->sock,
  2685. s5r->wbuf,
  2686. s5r->wbuf_len);
  2687. if (len <= 0)
  2688. {
  2689. /* write error: connection closed, shutdown, etc.; just clean up */
  2690. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2691. "Write Error\n");
  2692. cleanup_s5r (s5r);
  2693. return;
  2694. }
  2695. memmove (s5r->wbuf,
  2696. &s5r->wbuf[len],
  2697. s5r->wbuf_len - len);
  2698. s5r->wbuf_len -= len;
  2699. if (s5r->wbuf_len > 0)
  2700. {
  2701. /* not done writing */
  2702. s5r->wtask =
  2703. GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
  2704. s5r->sock,
  2705. &do_write, s5r);
  2706. return;
  2707. }
  2708. /* we're done writing, continue with state machine! */
  2709. switch (s5r->state)
  2710. {
  2711. case SOCKS5_INIT:
  2712. GNUNET_assert (0);
  2713. break;
  2714. case SOCKS5_REQUEST:
  2715. GNUNET_assert (NULL != s5r->rtask);
  2716. break;
  2717. case SOCKS5_DATA_TRANSFER:
  2718. setup_data_transfer (s5r);
  2719. return;
  2720. case SOCKS5_WRITE_THEN_CLEANUP:
  2721. cleanup_s5r (s5r);
  2722. return;
  2723. default:
  2724. GNUNET_break (0);
  2725. break;
  2726. }
  2727. }
  2728. /**
  2729. * Return a server response message indicating a failure to the client.
  2730. *
  2731. * @param s5r request to return failure code for
  2732. * @param sc status code to return
  2733. */
  2734. static void
  2735. signal_socks_failure (struct Socks5Request *s5r,
  2736. enum Socks5StatusCode sc)
  2737. {
  2738. struct Socks5ServerResponseMessage *s_resp;
  2739. GNUNET_break (0 == s5r->wbuf_len); /* Should happen first in any transmission, right? */
  2740. GNUNET_assert (SOCKS_BUFFERSIZE - s5r->wbuf_len >=
  2741. sizeof(struct Socks5ServerResponseMessage));
  2742. s_resp = (struct Socks5ServerResponseMessage *) &s5r->wbuf[s5r->wbuf_len];
  2743. memset (s_resp, 0, sizeof(struct Socks5ServerResponseMessage));
  2744. s_resp->version = SOCKS_VERSION_5;
  2745. s_resp->reply = sc;
  2746. s5r->state = SOCKS5_WRITE_THEN_CLEANUP;
  2747. if (NULL != s5r->wtask)
  2748. s5r->wtask =
  2749. GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
  2750. s5r->sock,
  2751. &do_write, s5r);
  2752. }
  2753. /**
  2754. * Return a server response message indicating success.
  2755. *
  2756. * @param s5r request to return success status message for
  2757. */
  2758. static void
  2759. signal_socks_success (struct Socks5Request *s5r)
  2760. {
  2761. struct Socks5ServerResponseMessage *s_resp;
  2762. s_resp = (struct Socks5ServerResponseMessage *) &s5r->wbuf[s5r->wbuf_len];
  2763. s_resp->version = SOCKS_VERSION_5;
  2764. s_resp->reply = SOCKS5_STATUS_REQUEST_GRANTED;
  2765. s_resp->reserved = 0;
  2766. s_resp->addr_type = SOCKS5_AT_IPV4;
  2767. /* zero out IPv4 address and port */
  2768. memset (&s_resp[1],
  2769. 0,
  2770. sizeof(struct in_addr) + sizeof(uint16_t));
  2771. s5r->wbuf_len += sizeof(struct Socks5ServerResponseMessage)
  2772. + sizeof(struct in_addr) + sizeof(uint16_t);
  2773. if (NULL == s5r->wtask)
  2774. s5r->wtask =
  2775. GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
  2776. s5r->sock,
  2777. &do_write, s5r);
  2778. }
  2779. /**
  2780. * Process GNS results for target domain.
  2781. *
  2782. * @param cls the `struct Socks5Request *`
  2783. * @param tld #GNUNET_YES if this was a GNS TLD.
  2784. * @param rd_count number of records returned
  2785. * @param rd record data
  2786. */
  2787. static void
  2788. handle_gns_result (void *cls,
  2789. int tld,
  2790. uint32_t rd_count,
  2791. const struct GNUNET_GNSRECORD_Data *rd)
  2792. {
  2793. struct Socks5Request *s5r = cls;
  2794. const struct GNUNET_GNSRECORD_Data *r;
  2795. int got_ip;
  2796. s5r->gns_lookup = NULL;
  2797. s5r->is_gns = tld;
  2798. got_ip = GNUNET_NO;
  2799. for (uint32_t i = 0; i < rd_count; i++)
  2800. {
  2801. r = &rd[i];
  2802. switch (r->record_type)
  2803. {
  2804. case GNUNET_DNSPARSER_TYPE_A:
  2805. {
  2806. struct sockaddr_in *in;
  2807. if (sizeof(struct in_addr) != r->data_size)
  2808. {
  2809. GNUNET_break_op (0);
  2810. break;
  2811. }
  2812. if (GNUNET_YES == got_ip)
  2813. break;
  2814. if (GNUNET_OK !=
  2815. GNUNET_NETWORK_test_pf (PF_INET))
  2816. break;
  2817. got_ip = GNUNET_YES;
  2818. in = (struct sockaddr_in *) &s5r->destination_address;
  2819. in->sin_family = AF_INET;
  2820. GNUNET_memcpy (&in->sin_addr,
  2821. r->data,
  2822. r->data_size);
  2823. in->sin_port = htons (s5r->port);
  2824. #if HAVE_SOCKADDR_IN_SIN_LEN
  2825. in->sin_len = sizeof(*in);
  2826. #endif
  2827. }
  2828. break;
  2829. case GNUNET_DNSPARSER_TYPE_AAAA:
  2830. {
  2831. struct sockaddr_in6 *in;
  2832. if (sizeof(struct in6_addr) != r->data_size)
  2833. {
  2834. GNUNET_break_op (0);
  2835. break;
  2836. }
  2837. if (GNUNET_YES == got_ip)
  2838. break;
  2839. if (GNUNET_YES == disable_v6)
  2840. break;
  2841. if (GNUNET_OK !=
  2842. GNUNET_NETWORK_test_pf (PF_INET6))
  2843. break;
  2844. /* FIXME: allow user to disable IPv6 per configuration option... */
  2845. got_ip = GNUNET_YES;
  2846. in = (struct sockaddr_in6 *) &s5r->destination_address;
  2847. in->sin6_family = AF_INET6;
  2848. GNUNET_memcpy (&in->sin6_addr,
  2849. r->data,
  2850. r->data_size);
  2851. in->sin6_port = htons (s5r->port);
  2852. #if HAVE_SOCKADDR_IN_SIN_LEN
  2853. in->sin6_len = sizeof(*in);
  2854. #endif
  2855. }
  2856. break;
  2857. case GNUNET_GNSRECORD_TYPE_VPN:
  2858. GNUNET_break (0); /* should have been translated within GNS */
  2859. break;
  2860. case GNUNET_GNSRECORD_TYPE_LEHO:
  2861. GNUNET_free_non_null (s5r->leho);
  2862. s5r->leho = GNUNET_strndup (r->data,
  2863. r->data_size);
  2864. break;
  2865. case GNUNET_GNSRECORD_TYPE_BOX:
  2866. {
  2867. const struct GNUNET_GNSRECORD_BoxRecord *box;
  2868. if (r->data_size < sizeof(struct GNUNET_GNSRECORD_BoxRecord))
  2869. {
  2870. GNUNET_break_op (0);
  2871. break;
  2872. }
  2873. box = r->data;
  2874. if ((ntohl (box->record_type) != GNUNET_DNSPARSER_TYPE_TLSA) ||
  2875. (ntohs (box->protocol) != IPPROTO_TCP) ||
  2876. (ntohs (box->service) != s5r->port))
  2877. break; /* BOX record does not apply */
  2878. if (s5r->num_danes >= MAX_DANES)
  2879. {
  2880. GNUNET_break (0); /* MAX_DANES too small */
  2881. break;
  2882. }
  2883. s5r->is_tls = GNUNET_YES; /* This should be TLS */
  2884. s5r->dane_data_len[s5r->num_danes]
  2885. = r->data_size - sizeof(struct GNUNET_GNSRECORD_BoxRecord);
  2886. s5r->dane_data[s5r->num_danes]
  2887. = GNUNET_memdup (&box[1],
  2888. s5r->dane_data_len[s5r->num_danes]);
  2889. s5r->num_danes++;
  2890. break;
  2891. }
  2892. default:
  2893. /* don't care */
  2894. break;
  2895. }
  2896. }
  2897. if ((GNUNET_YES != got_ip) &&
  2898. (GNUNET_YES == tld))
  2899. {
  2900. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2901. "Name resolution failed to yield useful IP address.\n");
  2902. signal_socks_failure (s5r,
  2903. SOCKS5_STATUS_GENERAL_FAILURE);
  2904. return;
  2905. }
  2906. s5r->state = SOCKS5_DATA_TRANSFER;
  2907. signal_socks_success (s5r);
  2908. }
  2909. /**
  2910. * Remove the first @a len bytes from the beginning of the read buffer.
  2911. *
  2912. * @param s5r the handle clear the read buffer for
  2913. * @param len number of bytes in read buffer to advance
  2914. */
  2915. static void
  2916. clear_from_s5r_rbuf (struct Socks5Request *s5r,
  2917. size_t len)
  2918. {
  2919. GNUNET_assert (len <= s5r->rbuf_len);
  2920. memmove (s5r->rbuf,
  2921. &s5r->rbuf[len],
  2922. s5r->rbuf_len - len);
  2923. s5r->rbuf_len -= len;
  2924. }
  2925. /**
  2926. * Read data from incoming Socks5 connection
  2927. *
  2928. * @param cls the closure with the `struct Socks5Request`
  2929. */
  2930. static void
  2931. do_s5r_read (void *cls)
  2932. {
  2933. struct Socks5Request *s5r = cls;
  2934. const struct Socks5ClientHelloMessage *c_hello;
  2935. struct Socks5ServerHelloMessage *s_hello;
  2936. const struct Socks5ClientRequestMessage *c_req;
  2937. ssize_t rlen;
  2938. size_t alen;
  2939. const struct GNUNET_SCHEDULER_TaskContext *tc;
  2940. s5r->rtask = NULL;
  2941. tc = GNUNET_SCHEDULER_get_task_context ();
  2942. if ((NULL != tc->read_ready) &&
  2943. (GNUNET_NETWORK_fdset_isset (tc->read_ready,
  2944. s5r->sock)))
  2945. {
  2946. rlen = GNUNET_NETWORK_socket_recv (s5r->sock,
  2947. &s5r->rbuf[s5r->rbuf_len],
  2948. sizeof(s5r->rbuf) - s5r->rbuf_len);
  2949. if (rlen <= 0)
  2950. {
  2951. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2952. "socks5 client disconnected.\n");
  2953. cleanup_s5r (s5r);
  2954. return;
  2955. }
  2956. s5r->rbuf_len += rlen;
  2957. }
  2958. s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  2959. s5r->sock,
  2960. &do_s5r_read, s5r);
  2961. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2962. "Processing %zu bytes of socks data in state %d\n",
  2963. s5r->rbuf_len,
  2964. s5r->state);
  2965. switch (s5r->state)
  2966. {
  2967. case SOCKS5_INIT:
  2968. c_hello = (const struct Socks5ClientHelloMessage*) &s5r->rbuf;
  2969. if ((s5r->rbuf_len < sizeof(struct Socks5ClientHelloMessage)) ||
  2970. (s5r->rbuf_len < sizeof(struct Socks5ClientHelloMessage)
  2971. + c_hello->num_auth_methods))
  2972. return; /* need more data */
  2973. if (SOCKS_VERSION_5 != c_hello->version)
  2974. {
  2975. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2976. _ ("Unsupported socks version %d\n"),
  2977. (int) c_hello->version);
  2978. cleanup_s5r (s5r);
  2979. return;
  2980. }
  2981. clear_from_s5r_rbuf (s5r,
  2982. sizeof(struct Socks5ClientHelloMessage)
  2983. + c_hello->num_auth_methods);
  2984. GNUNET_assert (0 == s5r->wbuf_len);
  2985. s_hello = (struct Socks5ServerHelloMessage *) &s5r->wbuf;
  2986. s5r->wbuf_len = sizeof(struct Socks5ServerHelloMessage);
  2987. s_hello->version = SOCKS_VERSION_5;
  2988. s_hello->auth_method = SOCKS_AUTH_NONE;
  2989. GNUNET_assert (NULL == s5r->wtask);
  2990. s5r->wtask = GNUNET_SCHEDULER_add_write_net (GNUNET_TIME_UNIT_FOREVER_REL,
  2991. s5r->sock,
  2992. &do_write, s5r);
  2993. s5r->state = SOCKS5_REQUEST;
  2994. return;
  2995. case SOCKS5_REQUEST:
  2996. c_req = (const struct Socks5ClientRequestMessage *) &s5r->rbuf;
  2997. if (s5r->rbuf_len < sizeof(struct Socks5ClientRequestMessage))
  2998. return;
  2999. switch (c_req->command)
  3000. {
  3001. case SOCKS5_CMD_TCP_STREAM:
  3002. /* handled below */
  3003. break;
  3004. default:
  3005. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3006. _ ("Unsupported socks command %d\n"),
  3007. (int) c_req->command);
  3008. signal_socks_failure (s5r,
  3009. SOCKS5_STATUS_COMMAND_NOT_SUPPORTED);
  3010. return;
  3011. }
  3012. switch (c_req->addr_type)
  3013. {
  3014. case SOCKS5_AT_IPV4:
  3015. {
  3016. const struct in_addr *v4 = (const struct in_addr *) &c_req[1];
  3017. const uint16_t *port = (const uint16_t *) &v4[1];
  3018. struct sockaddr_in *in;
  3019. s5r->port = ntohs (*port);
  3020. alen = sizeof(struct in_addr);
  3021. if (s5r->rbuf_len < sizeof(struct Socks5ClientRequestMessage)
  3022. + alen + sizeof(uint16_t))
  3023. return; /* need more data */
  3024. in = (struct sockaddr_in *) &s5r->destination_address;
  3025. in->sin_family = AF_INET;
  3026. in->sin_addr = *v4;
  3027. in->sin_port = *port;
  3028. #if HAVE_SOCKADDR_IN_SIN_LEN
  3029. in->sin_len = sizeof(*in);
  3030. #endif
  3031. s5r->state = SOCKS5_DATA_TRANSFER;
  3032. }
  3033. break;
  3034. case SOCKS5_AT_IPV6:
  3035. {
  3036. const struct in6_addr *v6 = (const struct in6_addr *) &c_req[1];
  3037. const uint16_t *port = (const uint16_t *) &v6[1];
  3038. struct sockaddr_in6 *in;
  3039. s5r->port = ntohs (*port);
  3040. alen = sizeof(struct in6_addr);
  3041. if (s5r->rbuf_len < sizeof(struct Socks5ClientRequestMessage)
  3042. + alen + sizeof(uint16_t))
  3043. return; /* need more data */
  3044. in = (struct sockaddr_in6 *) &s5r->destination_address;
  3045. in->sin6_family = AF_INET6;
  3046. in->sin6_addr = *v6;
  3047. in->sin6_port = *port;
  3048. #if HAVE_SOCKADDR_IN_SIN_LEN
  3049. in->sin6_len = sizeof(*in);
  3050. #endif
  3051. s5r->state = SOCKS5_DATA_TRANSFER;
  3052. }
  3053. break;
  3054. case SOCKS5_AT_DOMAINNAME:
  3055. {
  3056. const uint8_t *dom_len;
  3057. const char *dom_name;
  3058. const uint16_t *port;
  3059. dom_len = (const uint8_t *) &c_req[1];
  3060. alen = *dom_len + 1;
  3061. if (s5r->rbuf_len < sizeof(struct Socks5ClientRequestMessage)
  3062. + alen + sizeof(uint16_t))
  3063. return; /* need more data */
  3064. dom_name = (const char *) &dom_len[1];
  3065. port = (const uint16_t *) &dom_name[*dom_len];
  3066. s5r->domain = GNUNET_strndup (dom_name,
  3067. *dom_len);
  3068. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  3069. "Requested connection is to %s:%d\n",
  3070. // (HTTPS_PORT == s5r->port) ? "s" : "",
  3071. s5r->domain,
  3072. ntohs (*port));
  3073. s5r->state = SOCKS5_RESOLVING;
  3074. s5r->port = ntohs (*port);
  3075. s5r->is_tls = (HTTPS_PORT == s5r->port) ? GNUNET_YES : GNUNET_NO;
  3076. s5r->gns_lookup = GNUNET_GNS_lookup_with_tld (gns_handle,
  3077. s5r->domain,
  3078. GNUNET_DNSPARSER_TYPE_A,
  3079. GNUNET_GNS_LO_LOCAL_MASTER /* only cached */,
  3080. &handle_gns_result,
  3081. s5r);
  3082. break;
  3083. }
  3084. default:
  3085. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3086. _ ("Unsupported socks address type %d\n"),
  3087. (int) c_req->addr_type);
  3088. signal_socks_failure (s5r,
  3089. SOCKS5_STATUS_ADDRESS_TYPE_NOT_SUPPORTED);
  3090. return;
  3091. }
  3092. clear_from_s5r_rbuf (s5r,
  3093. sizeof(struct Socks5ClientRequestMessage)
  3094. + alen + sizeof(uint16_t));
  3095. if (0 != s5r->rbuf_len)
  3096. {
  3097. /* read more bytes than healthy, why did the client send more!? */
  3098. GNUNET_break_op (0);
  3099. signal_socks_failure (s5r,
  3100. SOCKS5_STATUS_GENERAL_FAILURE);
  3101. return;
  3102. }
  3103. if (SOCKS5_DATA_TRANSFER == s5r->state)
  3104. {
  3105. /* if we are not waiting for GNS resolution, signal success */
  3106. signal_socks_success (s5r);
  3107. }
  3108. /* We are done reading right now */
  3109. GNUNET_SCHEDULER_cancel (s5r->rtask);
  3110. s5r->rtask = NULL;
  3111. return;
  3112. case SOCKS5_RESOLVING:
  3113. GNUNET_assert (0);
  3114. return;
  3115. case SOCKS5_DATA_TRANSFER:
  3116. GNUNET_assert (0);
  3117. return;
  3118. default:
  3119. GNUNET_assert (0);
  3120. return;
  3121. }
  3122. }
  3123. /**
  3124. * Accept new incoming connections
  3125. *
  3126. * @param cls the closure with the lsock4 or lsock6
  3127. * @param tc the scheduler context
  3128. */
  3129. static void
  3130. do_accept (void *cls)
  3131. {
  3132. struct GNUNET_NETWORK_Handle *lsock = cls;
  3133. struct GNUNET_NETWORK_Handle *s;
  3134. struct Socks5Request *s5r;
  3135. GNUNET_assert (NULL != lsock);
  3136. if (lsock == lsock4)
  3137. ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  3138. lsock,
  3139. &do_accept,
  3140. lsock);
  3141. else if (lsock == lsock6)
  3142. ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  3143. lsock,
  3144. &do_accept,
  3145. lsock);
  3146. else
  3147. GNUNET_assert (0);
  3148. s = GNUNET_NETWORK_socket_accept (lsock,
  3149. NULL,
  3150. NULL);
  3151. if (NULL == s)
  3152. {
  3153. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
  3154. "accept");
  3155. return;
  3156. }
  3157. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  3158. "Got an inbound connection, waiting for data\n");
  3159. s5r = GNUNET_new (struct Socks5Request);
  3160. GNUNET_CONTAINER_DLL_insert (s5r_head,
  3161. s5r_tail,
  3162. s5r);
  3163. s5r->sock = s;
  3164. s5r->state = SOCKS5_INIT;
  3165. s5r->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  3166. s5r->sock,
  3167. &do_s5r_read,
  3168. s5r);
  3169. }
  3170. /* ******************* General / main code ********************* */
  3171. /**
  3172. * Task run on shutdown
  3173. *
  3174. * @param cls closure
  3175. */
  3176. static void
  3177. do_shutdown (void *cls)
  3178. {
  3179. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  3180. "Shutting down...\n");
  3181. /* MHD requires resuming before destroying the daemons */
  3182. for (struct Socks5Request *s5r = s5r_head;
  3183. NULL != s5r;
  3184. s5r = s5r->next)
  3185. {
  3186. if (s5r->suspended)
  3187. {
  3188. s5r->suspended = GNUNET_NO;
  3189. MHD_resume_connection (s5r->con);
  3190. }
  3191. }
  3192. while (NULL != mhd_httpd_head)
  3193. kill_httpd (mhd_httpd_head);
  3194. while (NULL != s5r_head)
  3195. cleanup_s5r (s5r_head);
  3196. if (NULL != lsock4)
  3197. {
  3198. GNUNET_NETWORK_socket_close (lsock4);
  3199. lsock4 = NULL;
  3200. }
  3201. if (NULL != lsock6)
  3202. {
  3203. GNUNET_NETWORK_socket_close (lsock6);
  3204. lsock6 = NULL;
  3205. }
  3206. if (NULL != curl_multi)
  3207. {
  3208. curl_multi_cleanup (curl_multi);
  3209. curl_multi = NULL;
  3210. }
  3211. if (NULL != gns_handle)
  3212. {
  3213. GNUNET_GNS_disconnect (gns_handle);
  3214. gns_handle = NULL;
  3215. }
  3216. if (NULL != curl_download_task)
  3217. {
  3218. GNUNET_SCHEDULER_cancel (curl_download_task);
  3219. curl_download_task = NULL;
  3220. }
  3221. if (NULL != ltask4)
  3222. {
  3223. GNUNET_SCHEDULER_cancel (ltask4);
  3224. ltask4 = NULL;
  3225. }
  3226. if (NULL != ltask6)
  3227. {
  3228. GNUNET_SCHEDULER_cancel (ltask6);
  3229. ltask6 = NULL;
  3230. }
  3231. gnutls_x509_crt_deinit (proxy_ca.cert);
  3232. gnutls_x509_privkey_deinit (proxy_ca.key);
  3233. gnutls_global_deinit ();
  3234. }
  3235. /**
  3236. * Create an IPv4 listen socket bound to our port.
  3237. *
  3238. * @return NULL on error
  3239. */
  3240. static struct GNUNET_NETWORK_Handle *
  3241. bind_v4 ()
  3242. {
  3243. struct GNUNET_NETWORK_Handle *ls;
  3244. struct sockaddr_in sa4;
  3245. int eno;
  3246. memset (&sa4, 0, sizeof(sa4));
  3247. sa4.sin_family = AF_INET;
  3248. sa4.sin_port = htons (port);
  3249. sa4.sin_addr.s_addr = address;
  3250. #if HAVE_SOCKADDR_IN_SIN_LEN
  3251. sa4.sin_len = sizeof(sa4);
  3252. #endif
  3253. ls = GNUNET_NETWORK_socket_create (AF_INET,
  3254. SOCK_STREAM,
  3255. 0);
  3256. if (NULL == ls)
  3257. return NULL;
  3258. if (GNUNET_OK !=
  3259. GNUNET_NETWORK_socket_bind (ls,
  3260. (const struct sockaddr *) &sa4,
  3261. sizeof(sa4)))
  3262. {
  3263. eno = errno;
  3264. GNUNET_NETWORK_socket_close (ls);
  3265. errno = eno;
  3266. return NULL;
  3267. }
  3268. return ls;
  3269. }
  3270. /**
  3271. * Create an IPv6 listen socket bound to our port.
  3272. *
  3273. * @return NULL on error
  3274. */
  3275. static struct GNUNET_NETWORK_Handle *
  3276. bind_v6 ()
  3277. {
  3278. struct GNUNET_NETWORK_Handle *ls;
  3279. struct sockaddr_in6 sa6;
  3280. int eno;
  3281. memset (&sa6, 0, sizeof(sa6));
  3282. sa6.sin6_family = AF_INET6;
  3283. sa6.sin6_port = htons (port);
  3284. sa6.sin6_addr = address6;
  3285. #if HAVE_SOCKADDR_IN_SIN_LEN
  3286. sa6.sin6_len = sizeof(sa6);
  3287. #endif
  3288. ls = GNUNET_NETWORK_socket_create (AF_INET6,
  3289. SOCK_STREAM,
  3290. 0);
  3291. if (NULL == ls)
  3292. return NULL;
  3293. if (GNUNET_OK !=
  3294. GNUNET_NETWORK_socket_bind (ls,
  3295. (const struct sockaddr *) &sa6,
  3296. sizeof(sa6)))
  3297. {
  3298. eno = errno;
  3299. GNUNET_NETWORK_socket_close (ls);
  3300. errno = eno;
  3301. return NULL;
  3302. }
  3303. return ls;
  3304. }
  3305. /**
  3306. * Main function that will be run
  3307. *
  3308. * @param cls closure
  3309. * @param args remaining command-line arguments
  3310. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  3311. * @param c configuration
  3312. */
  3313. static void
  3314. run (void *cls,
  3315. char *const *args,
  3316. const char *cfgfile,
  3317. const struct GNUNET_CONFIGURATION_Handle *c)
  3318. {
  3319. char*cafile_cfg = NULL;
  3320. char*cafile;
  3321. char*addr_str;
  3322. struct MhdHttpList *hd;
  3323. cfg = c;
  3324. /* Get address to bind to */
  3325. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "gns-proxy",
  3326. "BIND_TO",
  3327. &addr_str))
  3328. {
  3329. // No address specified
  3330. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3331. "Don't know what to bind to...\n");
  3332. GNUNET_free (addr_str);
  3333. GNUNET_SCHEDULER_shutdown ();
  3334. return;
  3335. }
  3336. if (1 != inet_pton (AF_INET, addr_str, &address))
  3337. {
  3338. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3339. "Unable to parse address %s\n",
  3340. addr_str);
  3341. GNUNET_free (addr_str);
  3342. GNUNET_SCHEDULER_shutdown ();
  3343. return;
  3344. }
  3345. GNUNET_free (addr_str);
  3346. /* Get address to bind to */
  3347. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "gns-proxy",
  3348. "BIND_TO6",
  3349. &addr_str))
  3350. {
  3351. // No address specified
  3352. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3353. "Don't know what to bind6 to...\n");
  3354. GNUNET_free (addr_str);
  3355. GNUNET_SCHEDULER_shutdown ();
  3356. return;
  3357. }
  3358. if (1 != inet_pton (AF_INET6, addr_str, &address6))
  3359. {
  3360. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3361. "Unable to parse IPv6 address %s\n",
  3362. addr_str);
  3363. GNUNET_free (addr_str);
  3364. GNUNET_SCHEDULER_shutdown ();
  3365. return;
  3366. }
  3367. GNUNET_free (addr_str);
  3368. if (NULL == (curl_multi = curl_multi_init ()))
  3369. {
  3370. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3371. "Failed to create cURL multi handle!\n");
  3372. return;
  3373. }
  3374. cafile = cafile_opt;
  3375. if (NULL == cafile)
  3376. {
  3377. if (GNUNET_OK !=
  3378. GNUNET_CONFIGURATION_get_value_filename (cfg,
  3379. "gns-proxy",
  3380. "PROXY_CACERT",
  3381. &cafile_cfg))
  3382. {
  3383. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  3384. "gns-proxy",
  3385. "PROXY_CACERT");
  3386. return;
  3387. }
  3388. cafile = cafile_cfg;
  3389. }
  3390. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  3391. "Using `%s' as CA\n",
  3392. cafile);
  3393. gnutls_global_init ();
  3394. gnutls_x509_crt_init (&proxy_ca.cert);
  3395. gnutls_x509_privkey_init (&proxy_ca.key);
  3396. if ((GNUNET_OK !=
  3397. load_cert_from_file (proxy_ca.cert,
  3398. cafile)) ||
  3399. (GNUNET_OK !=
  3400. load_key_from_file (proxy_ca.key,
  3401. cafile)))
  3402. {
  3403. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3404. _ ("Failed to load X.509 key and certificate from `%s'\n"),
  3405. cafile);
  3406. gnutls_x509_crt_deinit (proxy_ca.cert);
  3407. gnutls_x509_privkey_deinit (proxy_ca.key);
  3408. gnutls_global_deinit ();
  3409. GNUNET_free_non_null (cafile_cfg);
  3410. return;
  3411. }
  3412. GNUNET_free_non_null (cafile_cfg);
  3413. if (NULL == (gns_handle = GNUNET_GNS_connect (cfg)))
  3414. {
  3415. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3416. "Unable to connect to GNS!\n");
  3417. gnutls_x509_crt_deinit (proxy_ca.cert);
  3418. gnutls_x509_privkey_deinit (proxy_ca.key);
  3419. gnutls_global_deinit ();
  3420. return;
  3421. }
  3422. GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
  3423. NULL);
  3424. /* Open listen socket for socks proxy */
  3425. lsock6 = bind_v6 ();
  3426. if (NULL == lsock6)
  3427. {
  3428. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
  3429. "bind");
  3430. }
  3431. else
  3432. {
  3433. if (GNUNET_OK !=
  3434. GNUNET_NETWORK_socket_listen (lsock6,
  3435. 5))
  3436. {
  3437. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
  3438. "listen");
  3439. GNUNET_NETWORK_socket_close (lsock6);
  3440. lsock6 = NULL;
  3441. }
  3442. else
  3443. {
  3444. ltask6 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  3445. lsock6,
  3446. &do_accept,
  3447. lsock6);
  3448. }
  3449. }
  3450. lsock4 = bind_v4 ();
  3451. if (NULL == lsock4)
  3452. {
  3453. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
  3454. "bind");
  3455. }
  3456. else
  3457. {
  3458. if (GNUNET_OK !=
  3459. GNUNET_NETWORK_socket_listen (lsock4,
  3460. 5))
  3461. {
  3462. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
  3463. "listen");
  3464. GNUNET_NETWORK_socket_close (lsock4);
  3465. lsock4 = NULL;
  3466. }
  3467. else
  3468. {
  3469. ltask4 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  3470. lsock4,
  3471. &do_accept,
  3472. lsock4);
  3473. }
  3474. }
  3475. if ((NULL == lsock4) &&
  3476. (NULL == lsock6))
  3477. {
  3478. GNUNET_SCHEDULER_shutdown ();
  3479. return;
  3480. }
  3481. if (0 != curl_global_init (CURL_GLOBAL_WIN32))
  3482. {
  3483. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3484. "cURL global init failed!\n");
  3485. GNUNET_SCHEDULER_shutdown ();
  3486. return;
  3487. }
  3488. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  3489. "Proxy listens on port %u\n",
  3490. (unsigned int) port);
  3491. /* start MHD daemon for HTTP */
  3492. hd = GNUNET_new (struct MhdHttpList);
  3493. hd->daemon = MHD_start_daemon (MHD_USE_DEBUG | MHD_USE_NO_LISTEN_SOCKET
  3494. | MHD_ALLOW_SUSPEND_RESUME,
  3495. 0,
  3496. NULL, NULL,
  3497. &create_response, hd,
  3498. MHD_OPTION_CONNECTION_TIMEOUT, (unsigned
  3499. int) 16,
  3500. MHD_OPTION_NOTIFY_COMPLETED, &mhd_completed_cb,
  3501. NULL,
  3502. MHD_OPTION_NOTIFY_CONNECTION,
  3503. &mhd_connection_cb, NULL,
  3504. MHD_OPTION_URI_LOG_CALLBACK, &mhd_log_callback,
  3505. NULL,
  3506. MHD_OPTION_END);
  3507. if (NULL == hd->daemon)
  3508. {
  3509. GNUNET_free (hd);
  3510. GNUNET_SCHEDULER_shutdown ();
  3511. return;
  3512. }
  3513. httpd = hd;
  3514. GNUNET_CONTAINER_DLL_insert (mhd_httpd_head,
  3515. mhd_httpd_tail,
  3516. hd);
  3517. }
  3518. /**
  3519. * The main function for gnunet-gns-proxy.
  3520. *
  3521. * @param argc number of arguments from the command line
  3522. * @param argv command line arguments
  3523. * @return 0 ok, 1 on error
  3524. */
  3525. int
  3526. main (int argc,
  3527. char *const *argv)
  3528. {
  3529. struct GNUNET_GETOPT_CommandLineOption options[] = {
  3530. GNUNET_GETOPT_option_uint16 ('p',
  3531. "port",
  3532. NULL,
  3533. gettext_noop (
  3534. "listen on specified port (default: 7777)"),
  3535. &port),
  3536. GNUNET_GETOPT_option_string ('a',
  3537. "authority",
  3538. NULL,
  3539. gettext_noop ("pem file to use as CA"),
  3540. &cafile_opt),
  3541. GNUNET_GETOPT_option_flag ('6',
  3542. "disable-ivp6",
  3543. gettext_noop ("disable use of IPv6"),
  3544. &disable_v6),
  3545. GNUNET_GETOPT_OPTION_END
  3546. };
  3547. static const char*page =
  3548. "<html><head><title>gnunet-gns-proxy</title>"
  3549. "</head><body>cURL fail</body></html>";
  3550. int ret;
  3551. if (GNUNET_OK !=
  3552. GNUNET_STRINGS_get_utf8_args (argc, argv,
  3553. &argc, &argv))
  3554. return 2;
  3555. GNUNET_log_setup ("gnunet-gns-proxy",
  3556. "WARNING",
  3557. NULL);
  3558. curl_failure_response
  3559. = MHD_create_response_from_buffer (strlen (page),
  3560. (void *) page,
  3561. MHD_RESPMEM_PERSISTENT);
  3562. ret =
  3563. (GNUNET_OK ==
  3564. GNUNET_PROGRAM_run (argc, argv,
  3565. "gnunet-gns-proxy",
  3566. _ ("GNUnet GNS proxy"),
  3567. options,
  3568. &run, NULL)) ? 0 : 1;
  3569. MHD_destroy_response (curl_failure_response);
  3570. GNUNET_free_non_null ((char *) argv);
  3571. return ret;
  3572. }
  3573. /* end of gnunet-gns-proxy.c */