plugin_transport_udp.c 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2010-2017 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. * @file transport/plugin_transport_udp.c
  18. * @brief Implementation of the UDP transport protocol
  19. * @author Christian Grothoff
  20. * @author Nathan Evans
  21. * @author Matthias Wachs
  22. */
  23. #include "platform.h"
  24. #include "plugin_transport_udp.h"
  25. #include "gnunet_hello_lib.h"
  26. #include "gnunet_util_lib.h"
  27. #include "gnunet_fragmentation_lib.h"
  28. #include "gnunet_nat_service.h"
  29. #include "gnunet_protocols.h"
  30. #include "gnunet_resolver_service.h"
  31. #include "gnunet_signatures.h"
  32. #include "gnunet_constants.h"
  33. #include "gnunet_statistics_service.h"
  34. #include "gnunet_transport_service.h"
  35. #include "gnunet_transport_plugin.h"
  36. #include "transport.h"
  37. #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
  38. /**
  39. * After how much inactivity should a UDP session time out?
  40. */
  41. #define UDP_SESSION_TIME_OUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
  42. /**
  43. * Number of messages we can defragment in parallel. We only really
  44. * defragment 1 message at a time, but if messages get re-ordered, we
  45. * may want to keep knowledge about the previous message to avoid
  46. * discarding the current message in favor of a single fragment of a
  47. * previous message. 3 should be good since we don't expect massive
  48. * message reorderings with UDP.
  49. */
  50. #define UDP_MAX_MESSAGES_IN_DEFRAG 3
  51. /**
  52. * We keep a defragmentation queue per sender address. How many
  53. * sender addresses do we support at the same time? Memory consumption
  54. * is roughly a factor of 32k * #UDP_MAX_MESSAGES_IN_DEFRAG times this
  55. * value. (So 128 corresponds to 12 MB and should suffice for
  56. * connecting to roughly 128 peers via UDP).
  57. */
  58. #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
  59. /**
  60. * UDP Message-Packet header (after defragmentation).
  61. */
  62. struct UDPMessage
  63. {
  64. /**
  65. * Message header.
  66. */
  67. struct GNUNET_MessageHeader header;
  68. /**
  69. * Always zero for now.
  70. */
  71. uint32_t reserved;
  72. /**
  73. * What is the identity of the sender
  74. */
  75. struct GNUNET_PeerIdentity sender;
  76. };
  77. /**
  78. * Closure for #append_port().
  79. */
  80. struct PrettyPrinterContext
  81. {
  82. /**
  83. * DLL
  84. */
  85. struct PrettyPrinterContext *next;
  86. /**
  87. * DLL
  88. */
  89. struct PrettyPrinterContext *prev;
  90. /**
  91. * Our plugin.
  92. */
  93. struct Plugin *plugin;
  94. /**
  95. * Resolver handle
  96. */
  97. struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
  98. /**
  99. * Function to call with the result.
  100. */
  101. GNUNET_TRANSPORT_AddressStringCallback asc;
  102. /**
  103. * Clsoure for @e asc.
  104. */
  105. void *asc_cls;
  106. /**
  107. * Timeout task
  108. */
  109. struct GNUNET_SCHEDULER_Task *timeout_task;
  110. /**
  111. * Is this an IPv6 address?
  112. */
  113. int ipv6;
  114. /**
  115. * Options
  116. */
  117. uint32_t options;
  118. /**
  119. * Port to add after the IP address.
  120. */
  121. uint16_t port;
  122. };
  123. /**
  124. * Session with another peer.
  125. */
  126. struct GNUNET_ATS_Session
  127. {
  128. /**
  129. * Which peer is this session for?
  130. */
  131. struct GNUNET_PeerIdentity target;
  132. /**
  133. * Tokenizer for inbound messages.
  134. */
  135. struct GNUNET_MessageStreamTokenizer *mst;
  136. /**
  137. * Plugin this session belongs to.
  138. */
  139. struct Plugin *plugin;
  140. /**
  141. * Context for dealing with fragments.
  142. */
  143. struct UDP_FragmentationContext *frag_ctx;
  144. /**
  145. * Desired delay for next sending we send to other peer
  146. */
  147. struct GNUNET_TIME_Relative flow_delay_for_other_peer;
  148. /**
  149. * Desired delay for transmissions we received from other peer.
  150. * This is for full messages, the value needs to be adjusted for
  151. * fragmented messages.
  152. */
  153. struct GNUNET_TIME_Relative flow_delay_from_other_peer;
  154. /**
  155. * Session timeout task
  156. */
  157. struct GNUNET_SCHEDULER_Task *timeout_task;
  158. /**
  159. * When does this session time out?
  160. */
  161. struct GNUNET_TIME_Absolute timeout;
  162. /**
  163. * What time did we last transmit?
  164. */
  165. struct GNUNET_TIME_Absolute last_transmit_time;
  166. /**
  167. * expected delay for ACKs
  168. */
  169. struct GNUNET_TIME_Relative last_expected_ack_delay;
  170. /**
  171. * desired delay between UDP messages
  172. */
  173. struct GNUNET_TIME_Relative last_expected_msg_delay;
  174. /**
  175. * Our own address.
  176. */
  177. struct GNUNET_HELLO_Address *address;
  178. /**
  179. * Number of bytes waiting for transmission to this peer.
  180. */
  181. unsigned long long bytes_in_queue;
  182. /**
  183. * Number of messages waiting for transmission to this peer.
  184. */
  185. unsigned int msgs_in_queue;
  186. /**
  187. * Reference counter to indicate that this session is
  188. * currently being used and must not be destroyed;
  189. * setting @e in_destroy will destroy it as soon as
  190. * possible.
  191. */
  192. unsigned int rc;
  193. /**
  194. * Network type of the address.
  195. */
  196. enum GNUNET_NetworkType scope;
  197. /**
  198. * Is this session about to be destroyed (sometimes we cannot
  199. * destroy a session immediately as below us on the stack
  200. * there might be code that still uses it; in this case,
  201. * @e rc is non-zero).
  202. */
  203. int in_destroy;
  204. };
  205. /**
  206. * Data structure to track defragmentation contexts based
  207. * on the source of the UDP traffic.
  208. */
  209. struct DefragContext
  210. {
  211. /**
  212. * Defragmentation context.
  213. */
  214. struct GNUNET_DEFRAGMENT_Context *defrag;
  215. /**
  216. * Reference to master plugin struct.
  217. */
  218. struct Plugin *plugin;
  219. /**
  220. * Node in the defrag heap.
  221. */
  222. struct GNUNET_CONTAINER_HeapNode *hnode;
  223. /**
  224. * Source address this receive context is for (allocated at the
  225. * end of the struct).
  226. */
  227. const union UdpAddress *udp_addr;
  228. /**
  229. * Who's message(s) are we defragmenting here?
  230. * Only initialized once we succeeded and
  231. * @e have_sender is set.
  232. */
  233. struct GNUNET_PeerIdentity sender;
  234. /**
  235. * Length of @e udp_addr.
  236. */
  237. size_t udp_addr_len;
  238. /**
  239. * Network type the address belongs to.
  240. */
  241. enum GNUNET_NetworkType network_type;
  242. /**
  243. * Has the @e sender field been initialized yet?
  244. */
  245. int have_sender;
  246. };
  247. /**
  248. * Context to send fragmented messages
  249. */
  250. struct UDP_FragmentationContext
  251. {
  252. /**
  253. * Next in linked list
  254. */
  255. struct UDP_FragmentationContext *next;
  256. /**
  257. * Previous in linked list
  258. */
  259. struct UDP_FragmentationContext *prev;
  260. /**
  261. * The plugin
  262. */
  263. struct Plugin *plugin;
  264. /**
  265. * Handle for fragmentation.
  266. */
  267. struct GNUNET_FRAGMENT_Context *frag;
  268. /**
  269. * The session this fragmentation context belongs to
  270. */
  271. struct GNUNET_ATS_Session *session;
  272. /**
  273. * Function to call upon completion of the transmission.
  274. */
  275. GNUNET_TRANSPORT_TransmitContinuation cont;
  276. /**
  277. * Closure for @e cont.
  278. */
  279. void *cont_cls;
  280. /**
  281. * Start time.
  282. */
  283. struct GNUNET_TIME_Absolute start_time;
  284. /**
  285. * Transmission time for the next fragment. Incremented by
  286. * the @e flow_delay_from_other_peer for each fragment when
  287. * we setup the fragments.
  288. */
  289. struct GNUNET_TIME_Absolute next_frag_time;
  290. /**
  291. * Desired delay for transmissions we received from other peer.
  292. * Adjusted to be per fragment (UDP_MTU), even though on the
  293. * wire it was for "full messages".
  294. */
  295. struct GNUNET_TIME_Relative flow_delay_from_other_peer;
  296. /**
  297. * Message timeout
  298. */
  299. struct GNUNET_TIME_Absolute timeout;
  300. /**
  301. * Payload size of original unfragmented message
  302. */
  303. size_t payload_size;
  304. /**
  305. * Bytes used to send all fragments on wire including UDP overhead
  306. */
  307. size_t on_wire_size;
  308. };
  309. /**
  310. * Function called when a message is removed from the
  311. * transmission queue.
  312. *
  313. * @param cls closure
  314. * @param udpw message wrapper finished
  315. * @param result #GNUNET_OK on success (message was sent)
  316. * #GNUNET_SYSERR if the target disconnected
  317. * or we had a timeout or other trouble sending
  318. */
  319. typedef void
  320. (*QueueContinuation) (void *cls,
  321. struct UDP_MessageWrapper *udpw,
  322. int result);
  323. /**
  324. * Information we track for each message in the queue.
  325. */
  326. struct UDP_MessageWrapper
  327. {
  328. /**
  329. * Session this message belongs to
  330. */
  331. struct GNUNET_ATS_Session *session;
  332. /**
  333. * DLL of messages, previous element
  334. */
  335. struct UDP_MessageWrapper *prev;
  336. /**
  337. * DLL of messages, next element
  338. */
  339. struct UDP_MessageWrapper *next;
  340. /**
  341. * Message with @e msg_size bytes including UDP-specific overhead.
  342. */
  343. char *msg_buf;
  344. /**
  345. * Function to call once the message wrapper is being removed
  346. * from the queue (with success or failure).
  347. */
  348. QueueContinuation qc;
  349. /**
  350. * Closure for @e qc.
  351. */
  352. void *qc_cls;
  353. /**
  354. * External continuation to call upon completion of the
  355. * transmission, NULL if this queue entry is not for a
  356. * message from the application.
  357. */
  358. GNUNET_TRANSPORT_TransmitContinuation cont;
  359. /**
  360. * Closure for @e cont.
  361. */
  362. void *cont_cls;
  363. /**
  364. * Fragmentation context.
  365. * frag_ctx == NULL if transport <= MTU
  366. * frag_ctx != NULL if transport > MTU
  367. */
  368. struct UDP_FragmentationContext *frag_ctx;
  369. /**
  370. * Message enqueue time.
  371. */
  372. struct GNUNET_TIME_Absolute start_time;
  373. /**
  374. * Desired transmission time for this message, based on the
  375. * flow limiting information we got from the other peer.
  376. */
  377. struct GNUNET_TIME_Absolute transmission_time;
  378. /**
  379. * Message timeout.
  380. */
  381. struct GNUNET_TIME_Absolute timeout;
  382. /**
  383. * Size of UDP message to send, including UDP-specific overhead.
  384. */
  385. size_t msg_size;
  386. /**
  387. * Payload size of original message.
  388. */
  389. size_t payload_size;
  390. };
  391. GNUNET_NETWORK_STRUCT_BEGIN
  392. /**
  393. * UDP ACK Message-Packet header.
  394. */
  395. struct UDP_ACK_Message
  396. {
  397. /**
  398. * Message header.
  399. */
  400. struct GNUNET_MessageHeader header;
  401. /**
  402. * Desired delay for flow control, in us (in NBO).
  403. * A value of UINT32_MAX indicates that the other
  404. * peer wants us to disconnect.
  405. */
  406. uint32_t delay GNUNET_PACKED;
  407. /**
  408. * What is the identity of the sender
  409. */
  410. struct GNUNET_PeerIdentity sender;
  411. };
  412. GNUNET_NETWORK_STRUCT_END
  413. /* ************************* Monitoring *********** */
  414. /**
  415. * If a session monitor is attached, notify it about the new
  416. * session state.
  417. *
  418. * @param plugin our plugin
  419. * @param session session that changed state
  420. * @param state new state of the session
  421. */
  422. static void
  423. notify_session_monitor (struct Plugin *plugin,
  424. struct GNUNET_ATS_Session *session,
  425. enum GNUNET_TRANSPORT_SessionState state)
  426. {
  427. struct GNUNET_TRANSPORT_SessionInfo info;
  428. if (NULL == plugin->sic)
  429. return;
  430. if (GNUNET_YES == session->in_destroy)
  431. return; /* already destroyed, just RC>0 left-over actions */
  432. memset (&info,
  433. 0,
  434. sizeof (info));
  435. info.state = state;
  436. info.is_inbound = GNUNET_SYSERR; /* hard to say */
  437. info.num_msg_pending = session->msgs_in_queue;
  438. info.num_bytes_pending = session->bytes_in_queue;
  439. /* info.receive_delay remains zero as this is not supported by UDP
  440. (cannot selectively not receive from 'some' peer while continuing
  441. to receive from others) */
  442. info.session_timeout = session->timeout;
  443. info.address = session->address;
  444. plugin->sic (plugin->sic_cls,
  445. session,
  446. &info);
  447. }
  448. /**
  449. * Return information about the given session to the monitor callback.
  450. *
  451. * @param cls the `struct Plugin` with the monitor callback (`sic`)
  452. * @param peer peer we send information about
  453. * @param value our `struct GNUNET_ATS_Session` to send information about
  454. * @return #GNUNET_OK (continue to iterate)
  455. */
  456. static int
  457. send_session_info_iter (void *cls,
  458. const struct GNUNET_PeerIdentity *peer,
  459. void *value)
  460. {
  461. struct Plugin *plugin = cls;
  462. struct GNUNET_ATS_Session *session = value;
  463. notify_session_monitor (plugin,
  464. session,
  465. GNUNET_TRANSPORT_SS_INIT);
  466. notify_session_monitor (plugin,
  467. session,
  468. GNUNET_TRANSPORT_SS_UP);
  469. return GNUNET_OK;
  470. }
  471. /**
  472. * Begin monitoring sessions of a plugin. There can only
  473. * be one active monitor per plugin (i.e. if there are
  474. * multiple monitors, the transport service needs to
  475. * multiplex the generated events over all of them).
  476. *
  477. * @param cls closure of the plugin
  478. * @param sic callback to invoke, NULL to disable monitor;
  479. * plugin will being by iterating over all active
  480. * sessions immediately and then enter monitor mode
  481. * @param sic_cls closure for @a sic
  482. */
  483. static void
  484. udp_plugin_setup_monitor (void *cls,
  485. GNUNET_TRANSPORT_SessionInfoCallback sic,
  486. void *sic_cls)
  487. {
  488. struct Plugin *plugin = cls;
  489. plugin->sic = sic;
  490. plugin->sic_cls = sic_cls;
  491. if (NULL != sic)
  492. {
  493. GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
  494. &send_session_info_iter,
  495. plugin);
  496. /* signal end of first iteration */
  497. sic (sic_cls,
  498. NULL,
  499. NULL);
  500. }
  501. }
  502. /* ****************** Little Helpers ****************** */
  503. /**
  504. * Function to free last resources associated with a session.
  505. *
  506. * @param s session to free
  507. */
  508. static void
  509. free_session (struct GNUNET_ATS_Session *s)
  510. {
  511. if (NULL != s->address)
  512. {
  513. GNUNET_HELLO_address_free (s->address);
  514. s->address = NULL;
  515. }
  516. if (NULL != s->frag_ctx)
  517. {
  518. GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag,
  519. NULL,
  520. NULL);
  521. GNUNET_free (s->frag_ctx);
  522. s->frag_ctx = NULL;
  523. }
  524. if (NULL != s->mst)
  525. {
  526. GNUNET_MST_destroy (s->mst);
  527. s->mst = NULL;
  528. }
  529. GNUNET_free (s);
  530. }
  531. /**
  532. * Function that is called to get the keepalive factor.
  533. * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
  534. * calculate the interval between keepalive packets.
  535. *
  536. * @param cls closure with the `struct Plugin`
  537. * @return keepalive factor
  538. */
  539. static unsigned int
  540. udp_query_keepalive_factor (void *cls)
  541. {
  542. return 15;
  543. }
  544. /**
  545. * Function obtain the network type for a session
  546. *
  547. * @param cls closure (`struct Plugin *`)
  548. * @param session the session
  549. * @return the network type
  550. */
  551. static enum GNUNET_NetworkType
  552. udp_plugin_get_network (void *cls,
  553. struct GNUNET_ATS_Session *session)
  554. {
  555. return session->scope;
  556. }
  557. /**
  558. * Function obtain the network type for an address.
  559. *
  560. * @param cls closure (`struct Plugin *`)
  561. * @param address the address
  562. * @return the network type
  563. */
  564. static enum GNUNET_NetworkType
  565. udp_plugin_get_network_for_address (void *cls,
  566. const struct GNUNET_HELLO_Address *address)
  567. {
  568. struct Plugin *plugin = cls;
  569. size_t addrlen;
  570. struct sockaddr_in a4;
  571. struct sockaddr_in6 a6;
  572. const struct IPv4UdpAddress *u4;
  573. const struct IPv6UdpAddress *u6;
  574. const void *sb;
  575. size_t sbs;
  576. addrlen = address->address_length;
  577. if (addrlen == sizeof(struct IPv6UdpAddress))
  578. {
  579. GNUNET_assert (NULL != address->address); /* make static analysis happy */
  580. u6 = address->address;
  581. memset (&a6, 0, sizeof(a6));
  582. #if HAVE_SOCKADDR_IN_SIN_LEN
  583. a6.sin6_len = sizeof (a6);
  584. #endif
  585. a6.sin6_family = AF_INET6;
  586. a6.sin6_port = u6->u6_port;
  587. GNUNET_memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
  588. sb = &a6;
  589. sbs = sizeof(a6);
  590. }
  591. else if (addrlen == sizeof(struct IPv4UdpAddress))
  592. {
  593. GNUNET_assert (NULL != address->address); /* make static analysis happy */
  594. u4 = address->address;
  595. memset (&a4, 0, sizeof(a4));
  596. #if HAVE_SOCKADDR_IN_SIN_LEN
  597. a4.sin_len = sizeof (a4);
  598. #endif
  599. a4.sin_family = AF_INET;
  600. a4.sin_port = u4->u4_port;
  601. a4.sin_addr.s_addr = u4->ipv4_addr;
  602. sb = &a4;
  603. sbs = sizeof(a4);
  604. }
  605. else
  606. {
  607. GNUNET_break (0);
  608. return GNUNET_NT_UNSPECIFIED;
  609. }
  610. return plugin->env->get_address_type (plugin->env->cls,
  611. sb,
  612. sbs);
  613. }
  614. /* ******************* Event loop ******************** */
  615. /**
  616. * We have been notified that our readset has something to read. We don't
  617. * know which socket needs to be read, so we have to check each one
  618. * Then reschedule this function to be called again once more is available.
  619. *
  620. * @param cls the plugin handle
  621. */
  622. static void
  623. udp_plugin_select_v4 (void *cls);
  624. /**
  625. * We have been notified that our readset has something to read. We don't
  626. * know which socket needs to be read, so we have to check each one
  627. * Then reschedule this function to be called again once more is available.
  628. *
  629. * @param cls the plugin handle
  630. */
  631. static void
  632. udp_plugin_select_v6 (void *cls);
  633. /**
  634. * (re)schedule IPv4-select tasks for this plugin.
  635. *
  636. * @param plugin plugin to reschedule
  637. */
  638. static void
  639. schedule_select_v4 (struct Plugin *plugin)
  640. {
  641. struct GNUNET_TIME_Relative min_delay;
  642. struct GNUNET_TIME_Relative delay;
  643. struct UDP_MessageWrapper *udpw;
  644. struct UDP_MessageWrapper *min_udpw;
  645. if ( (GNUNET_YES == plugin->enable_ipv4) &&
  646. (NULL != plugin->sockv4) )
  647. {
  648. /* Find a message ready to send:
  649. * Flow delay from other peer is expired or not set (0) */
  650. min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
  651. min_udpw = NULL;
  652. for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
  653. {
  654. delay = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
  655. if (delay.rel_value_us < min_delay.rel_value_us)
  656. {
  657. min_delay = delay;
  658. min_udpw = udpw;
  659. }
  660. }
  661. if (NULL != plugin->select_task_v4)
  662. GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
  663. if (NULL != min_udpw)
  664. {
  665. if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
  666. {
  667. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  668. "Calculated flow delay for UDPv4 at %s for %s\n",
  669. GNUNET_STRINGS_relative_time_to_string (min_delay,
  670. GNUNET_YES),
  671. GNUNET_i2s (&min_udpw->session->target));
  672. }
  673. else
  674. {
  675. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  676. "Calculated flow delay for UDPv4 at %s for %s\n",
  677. GNUNET_STRINGS_relative_time_to_string (min_delay,
  678. GNUNET_YES),
  679. GNUNET_i2s (&min_udpw->session->target));
  680. }
  681. }
  682. plugin->select_task_v4
  683. = GNUNET_SCHEDULER_add_read_net (min_delay,
  684. plugin->sockv4,
  685. &udp_plugin_select_v4,
  686. plugin);
  687. }
  688. }
  689. /**
  690. * (re)schedule IPv6-select tasks for this plugin.
  691. *
  692. * @param plugin plugin to reschedule
  693. */
  694. static void
  695. schedule_select_v6 (struct Plugin *plugin)
  696. {
  697. struct GNUNET_TIME_Relative min_delay;
  698. struct GNUNET_TIME_Relative delay;
  699. struct UDP_MessageWrapper *udpw;
  700. struct UDP_MessageWrapper *min_udpw;
  701. if ( (GNUNET_YES == plugin->enable_ipv6) &&
  702. (NULL != plugin->sockv6) )
  703. {
  704. min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
  705. min_udpw = NULL;
  706. for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
  707. {
  708. delay = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
  709. if (delay.rel_value_us < min_delay.rel_value_us)
  710. {
  711. min_delay = delay;
  712. min_udpw = udpw;
  713. }
  714. }
  715. if (NULL != plugin->select_task_v6)
  716. GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
  717. if (NULL != min_udpw)
  718. {
  719. if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
  720. {
  721. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  722. "Calculated flow delay for UDPv6 at %s for %s\n",
  723. GNUNET_STRINGS_relative_time_to_string (min_delay,
  724. GNUNET_YES),
  725. GNUNET_i2s (&min_udpw->session->target));
  726. }
  727. else
  728. {
  729. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  730. "Calculated flow delay for UDPv6 at %s for %s\n",
  731. GNUNET_STRINGS_relative_time_to_string (min_delay,
  732. GNUNET_YES),
  733. GNUNET_i2s (&min_udpw->session->target));
  734. }
  735. }
  736. plugin->select_task_v6
  737. = GNUNET_SCHEDULER_add_read_net (min_delay,
  738. plugin->sockv6,
  739. &udp_plugin_select_v6,
  740. plugin);
  741. }
  742. }
  743. /* ******************* Address to string and back ***************** */
  744. /**
  745. * Function called for a quick conversion of the binary address to
  746. * a numeric address. Note that the caller must not free the
  747. * address and that the next call to this function is allowed
  748. * to override the address again.
  749. *
  750. * @param cls closure
  751. * @param addr binary address (a `union UdpAddress`)
  752. * @param addrlen length of the @a addr
  753. * @return string representing the same address
  754. */
  755. const char *
  756. udp_address_to_string (void *cls,
  757. const void *addr,
  758. size_t addrlen)
  759. {
  760. static char rbuf[INET6_ADDRSTRLEN + 10];
  761. char buf[INET6_ADDRSTRLEN];
  762. const void *sb;
  763. struct in_addr a4;
  764. struct in6_addr a6;
  765. const struct IPv4UdpAddress *t4;
  766. const struct IPv6UdpAddress *t6;
  767. int af;
  768. uint16_t port;
  769. uint32_t options;
  770. if (NULL == addr)
  771. {
  772. GNUNET_break_op (0);
  773. return NULL;
  774. }
  775. if (addrlen == sizeof(struct IPv6UdpAddress))
  776. {
  777. t6 = addr;
  778. af = AF_INET6;
  779. options = ntohl (t6->options);
  780. port = ntohs (t6->u6_port);
  781. a6 = t6->ipv6_addr;
  782. sb = &a6;
  783. }
  784. else if (addrlen == sizeof(struct IPv4UdpAddress))
  785. {
  786. t4 = addr;
  787. af = AF_INET;
  788. options = ntohl (t4->options);
  789. port = ntohs (t4->u4_port);
  790. a4.s_addr = t4->ipv4_addr;
  791. sb = &a4;
  792. }
  793. else
  794. {
  795. GNUNET_break_op (0);
  796. return NULL;
  797. }
  798. inet_ntop (af,
  799. sb,
  800. buf,
  801. INET6_ADDRSTRLEN);
  802. GNUNET_snprintf (rbuf,
  803. sizeof(rbuf),
  804. (af == AF_INET6)
  805. ? "%s.%u.[%s]:%u"
  806. : "%s.%u.%s:%u",
  807. PLUGIN_NAME,
  808. options,
  809. buf,
  810. port);
  811. return rbuf;
  812. }
  813. /**
  814. * Function called to convert a string address to a binary address.
  815. *
  816. * @param cls closure (`struct Plugin *`)
  817. * @param addr string address
  818. * @param addrlen length of the address
  819. * @param buf location to store the buffer
  820. * @param added location to store the number of bytes in the buffer.
  821. * If the function returns #GNUNET_SYSERR, its contents are undefined.
  822. * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  823. */
  824. static int
  825. udp_string_to_address (void *cls,
  826. const char *addr,
  827. uint16_t addrlen,
  828. void **buf,
  829. size_t *added)
  830. {
  831. struct sockaddr_storage socket_address;
  832. char *address;
  833. char *plugin;
  834. char *optionstr;
  835. uint32_t options;
  836. /* Format tcp.options.address:port */
  837. address = NULL;
  838. plugin = NULL;
  839. optionstr = NULL;
  840. if ((NULL == addr) || (0 == addrlen))
  841. {
  842. GNUNET_break (0);
  843. return GNUNET_SYSERR;
  844. }
  845. if ('\0' != addr[addrlen - 1])
  846. {
  847. GNUNET_break (0);
  848. return GNUNET_SYSERR;
  849. }
  850. if (strlen (addr) != addrlen - 1)
  851. {
  852. GNUNET_break (0);
  853. return GNUNET_SYSERR;
  854. }
  855. plugin = GNUNET_strdup (addr);
  856. optionstr = strchr (plugin, '.');
  857. if (NULL == optionstr)
  858. {
  859. GNUNET_break (0);
  860. GNUNET_free (plugin);
  861. return GNUNET_SYSERR;
  862. }
  863. optionstr[0] = '\0';
  864. optionstr++;
  865. options = atol (optionstr);
  866. address = strchr (optionstr, '.');
  867. if (NULL == address)
  868. {
  869. GNUNET_break (0);
  870. GNUNET_free (plugin);
  871. return GNUNET_SYSERR;
  872. }
  873. address[0] = '\0';
  874. address++;
  875. if (GNUNET_OK !=
  876. GNUNET_STRINGS_to_address_ip (address,
  877. strlen (address),
  878. &socket_address))
  879. {
  880. GNUNET_break (0);
  881. GNUNET_free (plugin);
  882. return GNUNET_SYSERR;
  883. }
  884. GNUNET_free(plugin);
  885. switch (socket_address.ss_family)
  886. {
  887. case AF_INET:
  888. {
  889. struct IPv4UdpAddress *u4;
  890. const struct sockaddr_in *in4 = (const struct sockaddr_in *) &socket_address;
  891. u4 = GNUNET_new (struct IPv4UdpAddress);
  892. u4->options = htonl (options);
  893. u4->ipv4_addr = in4->sin_addr.s_addr;
  894. u4->u4_port = in4->sin_port;
  895. *buf = u4;
  896. *added = sizeof (struct IPv4UdpAddress);
  897. return GNUNET_OK;
  898. }
  899. case AF_INET6:
  900. {
  901. struct IPv6UdpAddress *u6;
  902. const struct sockaddr_in6 *in6 = (const struct sockaddr_in6 *) &socket_address;
  903. u6 = GNUNET_new (struct IPv6UdpAddress);
  904. u6->options = htonl (options);
  905. u6->ipv6_addr = in6->sin6_addr;
  906. u6->u6_port = in6->sin6_port;
  907. *buf = u6;
  908. *added = sizeof (struct IPv6UdpAddress);
  909. return GNUNET_OK;
  910. }
  911. default:
  912. GNUNET_break (0);
  913. return GNUNET_SYSERR;
  914. }
  915. }
  916. /**
  917. * Append our port and forward the result.
  918. *
  919. * @param cls a `struct PrettyPrinterContext *`
  920. * @param hostname result from DNS resolver
  921. */
  922. static void
  923. append_port (void *cls,
  924. const char *hostname)
  925. {
  926. struct PrettyPrinterContext *ppc = cls;
  927. struct Plugin *plugin = ppc->plugin;
  928. char *ret;
  929. if (NULL == hostname)
  930. {
  931. /* Final call, done */
  932. GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
  933. plugin->ppc_dll_tail,
  934. ppc);
  935. ppc->resolver_handle = NULL;
  936. ppc->asc (ppc->asc_cls,
  937. NULL,
  938. GNUNET_OK);
  939. GNUNET_free (ppc);
  940. return;
  941. }
  942. if (GNUNET_YES == ppc->ipv6)
  943. GNUNET_asprintf (&ret,
  944. "%s.%u.[%s]:%d",
  945. PLUGIN_NAME,
  946. ppc->options,
  947. hostname,
  948. ppc->port);
  949. else
  950. GNUNET_asprintf (&ret,
  951. "%s.%u.%s:%d",
  952. PLUGIN_NAME,
  953. ppc->options,
  954. hostname,
  955. ppc->port);
  956. ppc->asc (ppc->asc_cls,
  957. ret,
  958. GNUNET_OK);
  959. GNUNET_free (ret);
  960. }
  961. /**
  962. * Convert the transports address to a nice, human-readable format.
  963. *
  964. * @param cls closure with the `struct Plugin *`
  965. * @param type name of the transport that generated the address
  966. * @param addr one of the addresses of the host, NULL for the last address
  967. * the specific address format depends on the transport;
  968. * a `union UdpAddress`
  969. * @param addrlen length of the address
  970. * @param numeric should (IP) addresses be displayed in numeric form?
  971. * @param timeout after how long should we give up?
  972. * @param asc function to call on each string
  973. * @param asc_cls closure for @a asc
  974. */
  975. static void
  976. udp_plugin_address_pretty_printer (void *cls,
  977. const char *type,
  978. const void *addr,
  979. size_t addrlen,
  980. int numeric,
  981. struct GNUNET_TIME_Relative timeout,
  982. GNUNET_TRANSPORT_AddressStringCallback asc,
  983. void *asc_cls)
  984. {
  985. struct Plugin *plugin = cls;
  986. struct PrettyPrinterContext *ppc;
  987. const struct sockaddr *sb;
  988. size_t sbs;
  989. struct sockaddr_in a4;
  990. struct sockaddr_in6 a6;
  991. const struct IPv4UdpAddress *u4;
  992. const struct IPv6UdpAddress *u6;
  993. uint16_t port;
  994. uint32_t options;
  995. if (addrlen == sizeof(struct IPv6UdpAddress))
  996. {
  997. u6 = addr;
  998. memset (&a6,
  999. 0,
  1000. sizeof (a6));
  1001. a6.sin6_family = AF_INET6;
  1002. #if HAVE_SOCKADDR_IN_SIN_LEN
  1003. a6.sin6_len = sizeof (a6);
  1004. #endif
  1005. a6.sin6_port = u6->u6_port;
  1006. a6.sin6_addr = u6->ipv6_addr;
  1007. port = ntohs (u6->u6_port);
  1008. options = ntohl (u6->options);
  1009. sb = (const struct sockaddr *) &a6;
  1010. sbs = sizeof (a6);
  1011. }
  1012. else if (addrlen == sizeof (struct IPv4UdpAddress))
  1013. {
  1014. u4 = addr;
  1015. memset (&a4,
  1016. 0,
  1017. sizeof(a4));
  1018. a4.sin_family = AF_INET;
  1019. #if HAVE_SOCKADDR_IN_SIN_LEN
  1020. a4.sin_len = sizeof (a4);
  1021. #endif
  1022. a4.sin_port = u4->u4_port;
  1023. a4.sin_addr.s_addr = u4->ipv4_addr;
  1024. port = ntohs (u4->u4_port);
  1025. options = ntohl (u4->options);
  1026. sb = (const struct sockaddr *) &a4;
  1027. sbs = sizeof(a4);
  1028. }
  1029. else
  1030. {
  1031. /* invalid address */
  1032. GNUNET_break_op (0);
  1033. asc (asc_cls,
  1034. NULL,
  1035. GNUNET_SYSERR);
  1036. asc (asc_cls,
  1037. NULL,
  1038. GNUNET_OK);
  1039. return;
  1040. }
  1041. ppc = GNUNET_new (struct PrettyPrinterContext);
  1042. ppc->plugin = plugin;
  1043. ppc->asc = asc;
  1044. ppc->asc_cls = asc_cls;
  1045. ppc->port = port;
  1046. ppc->options = options;
  1047. if (addrlen == sizeof (struct IPv6UdpAddress))
  1048. ppc->ipv6 = GNUNET_YES;
  1049. else
  1050. ppc->ipv6 = GNUNET_NO;
  1051. GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head,
  1052. plugin->ppc_dll_tail,
  1053. ppc);
  1054. ppc->resolver_handle
  1055. = GNUNET_RESOLVER_hostname_get (sb,
  1056. sbs,
  1057. ! numeric,
  1058. timeout,
  1059. &append_port,
  1060. ppc);
  1061. }
  1062. /**
  1063. * Check if the given port is plausible (must be either our listen
  1064. * port or our advertised port). If it is neither, we return
  1065. * #GNUNET_SYSERR.
  1066. *
  1067. * @param plugin global variables
  1068. * @param in_port port number to check
  1069. * @return #GNUNET_OK if port is either our open or advertised port
  1070. */
  1071. static int
  1072. check_port (const struct Plugin *plugin,
  1073. uint16_t in_port)
  1074. {
  1075. if ( (plugin->port == in_port) ||
  1076. (plugin->aport == in_port) )
  1077. return GNUNET_OK;
  1078. return GNUNET_SYSERR;
  1079. }
  1080. /**
  1081. * Function that will be called to check if a binary address for this
  1082. * plugin is well-formed and corresponds to an address for THIS peer
  1083. * (as per our configuration). Naturally, if absolutely necessary,
  1084. * plugins can be a bit conservative in their answer, but in general
  1085. * plugins should make sure that the address does not redirect
  1086. * traffic to a 3rd party that might try to man-in-the-middle our
  1087. * traffic.
  1088. *
  1089. * @param cls closure, should be our handle to the Plugin
  1090. * @param addr pointer to a `union UdpAddress`
  1091. * @param addrlen length of @a addr
  1092. * @return #GNUNET_OK if this is a plausible address for this peer
  1093. * and transport, #GNUNET_SYSERR if not
  1094. */
  1095. static int
  1096. udp_plugin_check_address (void *cls,
  1097. const void *addr,
  1098. size_t addrlen)
  1099. {
  1100. struct Plugin *plugin = cls;
  1101. const struct IPv4UdpAddress *v4;
  1102. const struct IPv6UdpAddress *v6;
  1103. if (sizeof(struct IPv4UdpAddress) == addrlen)
  1104. {
  1105. struct sockaddr_in s4;
  1106. v4 = (const struct IPv4UdpAddress *) addr;
  1107. if (GNUNET_OK != check_port (plugin,
  1108. ntohs (v4->u4_port)))
  1109. return GNUNET_SYSERR;
  1110. memset (&s4, 0, sizeof (s4));
  1111. s4.sin_family = AF_INET;
  1112. #if HAVE_SOCKADDR_IN_SIN_LEN
  1113. s4.sin_len = sizeof (s4);
  1114. #endif
  1115. s4.sin_port = v4->u4_port;
  1116. s4.sin_addr.s_addr = v4->ipv4_addr;
  1117. if (GNUNET_OK !=
  1118. GNUNET_NAT_test_address (plugin->nat,
  1119. &s4,
  1120. sizeof (struct sockaddr_in)))
  1121. return GNUNET_SYSERR;
  1122. }
  1123. else if (sizeof(struct IPv6UdpAddress) == addrlen)
  1124. {
  1125. struct sockaddr_in6 s6;
  1126. v6 = (const struct IPv6UdpAddress *) addr;
  1127. if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
  1128. return GNUNET_OK; /* plausible, if unlikely... */
  1129. memset (&s6, 0, sizeof (s6));
  1130. s6.sin6_family = AF_INET6;
  1131. #if HAVE_SOCKADDR_IN_SIN_LEN
  1132. s6.sin6_len = sizeof (s6);
  1133. #endif
  1134. s6.sin6_port = v6->u6_port;
  1135. s6.sin6_addr = v6->ipv6_addr;
  1136. if (GNUNET_OK !=
  1137. GNUNET_NAT_test_address (plugin->nat,
  1138. &s6,
  1139. sizeof(struct sockaddr_in6)))
  1140. return GNUNET_SYSERR;
  1141. }
  1142. else
  1143. {
  1144. GNUNET_break_op (0);
  1145. return GNUNET_SYSERR;
  1146. }
  1147. return GNUNET_OK;
  1148. }
  1149. /**
  1150. * Our external IP address/port mapping has changed.
  1151. *
  1152. * @param cls closure, the `struct Plugin`
  1153. * @param app_ctx[in,out] location where the app can store stuff
  1154. * on add and retrieve it on remove
  1155. * @param add_remove #GNUNET_YES to mean the new public IP address,
  1156. * #GNUNET_NO to mean the previous (now invalid) one
  1157. * @param ac address class the address belongs to
  1158. * @param addr either the previous or the new public IP address
  1159. * @param addrlen actual length of the @a addr
  1160. */
  1161. static void
  1162. udp_nat_port_map_callback (void *cls,
  1163. void **app_ctx,
  1164. int add_remove,
  1165. enum GNUNET_NAT_AddressClass ac,
  1166. const struct sockaddr *addr,
  1167. socklen_t addrlen)
  1168. {
  1169. struct Plugin *plugin = cls;
  1170. struct GNUNET_HELLO_Address *address;
  1171. struct IPv4UdpAddress u4;
  1172. struct IPv6UdpAddress u6;
  1173. void *arg;
  1174. size_t args;
  1175. (void) app_ctx;
  1176. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1177. (GNUNET_YES == add_remove)
  1178. ? "NAT notification to add address `%s'\n"
  1179. : "NAT notification to remove address `%s'\n",
  1180. GNUNET_a2s (addr,
  1181. addrlen));
  1182. /* convert 'address' to our internal format */
  1183. switch (addr->sa_family)
  1184. {
  1185. case AF_INET:
  1186. {
  1187. const struct sockaddr_in *i4;
  1188. GNUNET_assert (sizeof(struct sockaddr_in) == addrlen);
  1189. i4 = (const struct sockaddr_in *) addr;
  1190. if (0 == ntohs (i4->sin_port))
  1191. return; /* Port = 0 means unmapped, ignore these for UDP. */
  1192. memset (&u4,
  1193. 0,
  1194. sizeof(u4));
  1195. u4.options = htonl (plugin->myoptions);
  1196. u4.ipv4_addr = i4->sin_addr.s_addr;
  1197. u4.u4_port = i4->sin_port;
  1198. arg = &u4;
  1199. args = sizeof (struct IPv4UdpAddress);
  1200. break;
  1201. }
  1202. case AF_INET6:
  1203. {
  1204. const struct sockaddr_in6 *i6;
  1205. GNUNET_assert (sizeof(struct sockaddr_in6) == addrlen);
  1206. i6 = (const struct sockaddr_in6 *) addr;
  1207. if (0 == ntohs (i6->sin6_port))
  1208. return; /* Port = 0 means unmapped, ignore these for UDP. */
  1209. memset (&u6,
  1210. 0,
  1211. sizeof(u6));
  1212. u6.options = htonl (plugin->myoptions);
  1213. u6.ipv6_addr = i6->sin6_addr;
  1214. u6.u6_port = i6->sin6_port;
  1215. arg = &u6;
  1216. args = sizeof (struct IPv6UdpAddress);
  1217. break;
  1218. }
  1219. default:
  1220. GNUNET_break (0);
  1221. return;
  1222. }
  1223. /* modify our published address list */
  1224. /* TODO: use 'ac' here in the future... */
  1225. address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
  1226. PLUGIN_NAME,
  1227. arg,
  1228. args,
  1229. GNUNET_HELLO_ADDRESS_INFO_NONE);
  1230. plugin->env->notify_address (plugin->env->cls,
  1231. add_remove,
  1232. address);
  1233. GNUNET_HELLO_address_free (address);
  1234. }
  1235. /* ********************* Finding sessions ******************* */
  1236. /**
  1237. * Closure for #session_cmp_it().
  1238. */
  1239. struct GNUNET_ATS_SessionCompareContext
  1240. {
  1241. /**
  1242. * Set to session matching the address.
  1243. */
  1244. struct GNUNET_ATS_Session *res;
  1245. /**
  1246. * Address we are looking for.
  1247. */
  1248. const struct GNUNET_HELLO_Address *address;
  1249. };
  1250. /**
  1251. * Find a session with a matching address.
  1252. *
  1253. * @param cls the `struct GNUNET_ATS_SessionCompareContext *`
  1254. * @param key peer identity (unused)
  1255. * @param value the `struct GNUNET_ATS_Session *`
  1256. * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
  1257. */
  1258. static int
  1259. session_cmp_it (void *cls,
  1260. const struct GNUNET_PeerIdentity *key,
  1261. void *value)
  1262. {
  1263. struct GNUNET_ATS_SessionCompareContext *cctx = cls;
  1264. struct GNUNET_ATS_Session *s = value;
  1265. if (0 == GNUNET_HELLO_address_cmp (s->address,
  1266. cctx->address))
  1267. {
  1268. GNUNET_assert (GNUNET_NO == s->in_destroy);
  1269. cctx->res = s;
  1270. return GNUNET_NO;
  1271. }
  1272. return GNUNET_OK;
  1273. }
  1274. /**
  1275. * Locate an existing session the transport service is using to
  1276. * send data to another peer. Performs some basic sanity checks
  1277. * on the address and then tries to locate a matching session.
  1278. *
  1279. * @param cls the plugin
  1280. * @param address the address we should locate the session by
  1281. * @return the session if it exists, or NULL if it is not found
  1282. */
  1283. static struct GNUNET_ATS_Session *
  1284. udp_plugin_lookup_session (void *cls,
  1285. const struct GNUNET_HELLO_Address *address)
  1286. {
  1287. struct Plugin *plugin = cls;
  1288. const struct IPv6UdpAddress *udp_a6;
  1289. const struct IPv4UdpAddress *udp_a4;
  1290. struct GNUNET_ATS_SessionCompareContext cctx;
  1291. if (NULL == address->address)
  1292. {
  1293. GNUNET_break (0);
  1294. return NULL;
  1295. }
  1296. if (sizeof(struct IPv4UdpAddress) == address->address_length)
  1297. {
  1298. if (NULL == plugin->sockv4)
  1299. return NULL;
  1300. udp_a4 = (const struct IPv4UdpAddress *) address->address;
  1301. if (0 == udp_a4->u4_port)
  1302. {
  1303. GNUNET_break (0);
  1304. return NULL;
  1305. }
  1306. }
  1307. else if (sizeof(struct IPv6UdpAddress) == address->address_length)
  1308. {
  1309. if (NULL == plugin->sockv6)
  1310. return NULL;
  1311. udp_a6 = (const struct IPv6UdpAddress *) address->address;
  1312. if (0 == udp_a6->u6_port)
  1313. {
  1314. GNUNET_break (0);
  1315. return NULL;
  1316. }
  1317. }
  1318. else
  1319. {
  1320. GNUNET_break (0);
  1321. return NULL;
  1322. }
  1323. /* check if session already exists */
  1324. cctx.address = address;
  1325. cctx.res = NULL;
  1326. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1327. "Looking for existing session for peer `%s' with address `%s'\n",
  1328. GNUNET_i2s (&address->peer),
  1329. udp_address_to_string (plugin,
  1330. address->address,
  1331. address->address_length));
  1332. GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
  1333. &address->peer,
  1334. &session_cmp_it,
  1335. &cctx);
  1336. if (NULL == cctx.res)
  1337. return NULL;
  1338. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1339. "Found existing session %p\n",
  1340. cctx.res);
  1341. return cctx.res;
  1342. }
  1343. /* ********************** Timeout ****************** */
  1344. /**
  1345. * Increment session timeout due to activity.
  1346. *
  1347. * @param s session to reschedule timeout activity for
  1348. */
  1349. static void
  1350. reschedule_session_timeout (struct GNUNET_ATS_Session *s)
  1351. {
  1352. if (GNUNET_YES == s->in_destroy)
  1353. return;
  1354. GNUNET_assert (NULL != s->timeout_task);
  1355. s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
  1356. }
  1357. /**
  1358. * Function that will be called whenever the transport service wants to
  1359. * notify the plugin that a session is still active and in use and
  1360. * therefore the session timeout for this session has to be updated
  1361. *
  1362. * @param cls closure with the `struct Plugin`
  1363. * @param peer which peer was the session for
  1364. * @param session which session is being updated
  1365. */
  1366. static void
  1367. udp_plugin_update_session_timeout (void *cls,
  1368. const struct GNUNET_PeerIdentity *peer,
  1369. struct GNUNET_ATS_Session *session)
  1370. {
  1371. struct Plugin *plugin = cls;
  1372. if (GNUNET_YES !=
  1373. GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
  1374. peer,
  1375. session))
  1376. {
  1377. GNUNET_break (0);
  1378. return;
  1379. }
  1380. /* Reschedule session timeout */
  1381. reschedule_session_timeout (session);
  1382. }
  1383. /* ************************* Sending ************************ */
  1384. /**
  1385. * Remove the given message from the transmission queue and
  1386. * update all applicable statistics.
  1387. *
  1388. * @param plugin the UDP plugin
  1389. * @param udpw message wrapper to dequeue
  1390. */
  1391. static void
  1392. dequeue (struct Plugin *plugin,
  1393. struct UDP_MessageWrapper *udpw)
  1394. {
  1395. struct GNUNET_ATS_Session *session = udpw->session;
  1396. if (plugin->bytes_in_buffer < udpw->msg_size)
  1397. {
  1398. GNUNET_break (0);
  1399. }
  1400. else
  1401. {
  1402. GNUNET_STATISTICS_update (plugin->env->stats,
  1403. "# UDP, total bytes in send buffers",
  1404. - (long long) udpw->msg_size,
  1405. GNUNET_NO);
  1406. plugin->bytes_in_buffer -= udpw->msg_size;
  1407. }
  1408. GNUNET_STATISTICS_update (plugin->env->stats,
  1409. "# UDP, total messages in send buffers",
  1410. -1,
  1411. GNUNET_NO);
  1412. if (sizeof(struct IPv4UdpAddress) == udpw->session->address->address_length)
  1413. {
  1414. GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
  1415. plugin->ipv4_queue_tail,
  1416. udpw);
  1417. }
  1418. else if (sizeof(struct IPv6UdpAddress) == udpw->session->address->address_length)
  1419. {
  1420. GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
  1421. plugin->ipv6_queue_tail,
  1422. udpw);
  1423. }
  1424. else
  1425. {
  1426. GNUNET_break (0);
  1427. return;
  1428. }
  1429. GNUNET_assert (session->msgs_in_queue > 0);
  1430. session->msgs_in_queue--;
  1431. GNUNET_assert (session->bytes_in_queue >= udpw->msg_size);
  1432. session->bytes_in_queue -= udpw->msg_size;
  1433. }
  1434. /**
  1435. * Enqueue a message for transmission and update statistics.
  1436. *
  1437. * @param plugin the UDP plugin
  1438. * @param udpw message wrapper to queue
  1439. */
  1440. static void
  1441. enqueue (struct Plugin *plugin,
  1442. struct UDP_MessageWrapper *udpw)
  1443. {
  1444. struct GNUNET_ATS_Session *session = udpw->session;
  1445. if (GNUNET_YES == session->in_destroy)
  1446. {
  1447. GNUNET_break (0);
  1448. GNUNET_free (udpw);
  1449. return;
  1450. }
  1451. if (plugin->bytes_in_buffer > INT64_MAX - udpw->msg_size)
  1452. {
  1453. GNUNET_break (0);
  1454. }
  1455. else
  1456. {
  1457. GNUNET_STATISTICS_update (plugin->env->stats,
  1458. "# UDP, total bytes in send buffers",
  1459. udpw->msg_size,
  1460. GNUNET_NO);
  1461. plugin->bytes_in_buffer += udpw->msg_size;
  1462. }
  1463. GNUNET_STATISTICS_update (plugin->env->stats,
  1464. "# UDP, total messages in send buffers",
  1465. 1,
  1466. GNUNET_NO);
  1467. if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
  1468. {
  1469. GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head,
  1470. plugin->ipv4_queue_tail,
  1471. udpw);
  1472. }
  1473. else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
  1474. {
  1475. GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
  1476. plugin->ipv6_queue_tail,
  1477. udpw);
  1478. }
  1479. else
  1480. {
  1481. GNUNET_break (0);
  1482. udpw->cont (udpw->cont_cls,
  1483. &session->target,
  1484. GNUNET_SYSERR,
  1485. udpw->msg_size,
  1486. 0);
  1487. GNUNET_free (udpw);
  1488. return;
  1489. }
  1490. session->msgs_in_queue++;
  1491. session->bytes_in_queue += udpw->msg_size;
  1492. }
  1493. /**
  1494. * We have completed our (attempt) to transmit a message that had to
  1495. * be fragmented -- either because we got an ACK saying that all
  1496. * fragments were received, or because of timeout / disconnect. Clean
  1497. * up our state.
  1498. *
  1499. * @param frag_ctx fragmentation context to clean up
  1500. * @param result #GNUNET_OK if we succeeded (got ACK),
  1501. * #GNUNET_SYSERR if the transmission failed
  1502. */
  1503. static void
  1504. fragmented_message_done (struct UDP_FragmentationContext *frag_ctx,
  1505. int result)
  1506. {
  1507. struct Plugin *plugin = frag_ctx->plugin;
  1508. struct GNUNET_ATS_Session *s = frag_ctx->session;
  1509. struct UDP_MessageWrapper *udpw;
  1510. struct UDP_MessageWrapper *tmp;
  1511. size_t overhead;
  1512. struct GNUNET_TIME_Relative delay;
  1513. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1514. "%p: Fragmented message removed with result %s\n",
  1515. frag_ctx,
  1516. (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
  1517. /* Call continuation for fragmented message */
  1518. if (frag_ctx->on_wire_size >= frag_ctx->payload_size)
  1519. overhead = frag_ctx->on_wire_size - frag_ctx->payload_size;
  1520. else
  1521. overhead = frag_ctx->on_wire_size;
  1522. delay = GNUNET_TIME_absolute_get_duration (frag_ctx->start_time);
  1523. if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
  1524. {
  1525. LOG (GNUNET_ERROR_TYPE_WARNING,
  1526. "Fragmented message acknowledged after %s (expected at %s)\n",
  1527. GNUNET_STRINGS_relative_time_to_string (delay,
  1528. GNUNET_YES),
  1529. GNUNET_STRINGS_absolute_time_to_string (frag_ctx->next_frag_time));
  1530. }
  1531. else
  1532. {
  1533. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1534. "Fragmented message acknowledged after %s (expected at %s)\n",
  1535. GNUNET_STRINGS_relative_time_to_string (delay,
  1536. GNUNET_YES),
  1537. GNUNET_STRINGS_absolute_time_to_string (frag_ctx->next_frag_time));
  1538. }
  1539. if (NULL != frag_ctx->cont)
  1540. frag_ctx->cont (frag_ctx->cont_cls,
  1541. &s->target,
  1542. result,
  1543. s->frag_ctx->payload_size,
  1544. frag_ctx->on_wire_size);
  1545. GNUNET_STATISTICS_update (plugin->env->stats,
  1546. "# UDP, fragmented messages active",
  1547. -1,
  1548. GNUNET_NO);
  1549. if (GNUNET_OK == result)
  1550. {
  1551. GNUNET_STATISTICS_update (plugin->env->stats,
  1552. "# UDP, fragmented msgs, messages, sent, success",
  1553. 1,
  1554. GNUNET_NO);
  1555. GNUNET_STATISTICS_update (plugin->env->stats,
  1556. "# UDP, fragmented msgs, bytes payload, sent, success",
  1557. s->frag_ctx->payload_size,
  1558. GNUNET_NO);
  1559. GNUNET_STATISTICS_update (plugin->env->stats,
  1560. "# UDP, fragmented msgs, bytes overhead, sent, success",
  1561. overhead,
  1562. GNUNET_NO);
  1563. GNUNET_STATISTICS_update (plugin->env->stats,
  1564. "# UDP, total, bytes overhead, sent",
  1565. overhead,
  1566. GNUNET_NO);
  1567. GNUNET_STATISTICS_update (plugin->env->stats,
  1568. "# UDP, total, bytes payload, sent",
  1569. s->frag_ctx->payload_size,
  1570. GNUNET_NO);
  1571. }
  1572. else
  1573. {
  1574. GNUNET_STATISTICS_update (plugin->env->stats,
  1575. "# UDP, fragmented msgs, messages, sent, failure",
  1576. 1,
  1577. GNUNET_NO);
  1578. GNUNET_STATISTICS_update (plugin->env->stats,
  1579. "# UDP, fragmented msgs, bytes payload, sent, failure",
  1580. s->frag_ctx->payload_size,
  1581. GNUNET_NO);
  1582. GNUNET_STATISTICS_update (plugin->env->stats,
  1583. "# UDP, fragmented msgs, bytes payload, sent, failure",
  1584. overhead,
  1585. GNUNET_NO);
  1586. GNUNET_STATISTICS_update (plugin->env->stats,
  1587. "# UDP, fragmented msgs, bytes payload, sent, failure",
  1588. overhead,
  1589. GNUNET_NO);
  1590. }
  1591. /* Remove remaining fragments from queue, no need to transmit those
  1592. any longer. */
  1593. if (s->address->address_length == sizeof(struct IPv6UdpAddress))
  1594. {
  1595. udpw = plugin->ipv6_queue_head;
  1596. while (NULL != udpw)
  1597. {
  1598. tmp = udpw->next;
  1599. if ( (udpw->frag_ctx != NULL) &&
  1600. (udpw->frag_ctx == frag_ctx) )
  1601. {
  1602. dequeue (plugin,
  1603. udpw);
  1604. GNUNET_free (udpw);
  1605. }
  1606. udpw = tmp;
  1607. }
  1608. }
  1609. if (s->address->address_length == sizeof(struct IPv4UdpAddress))
  1610. {
  1611. udpw = plugin->ipv4_queue_head;
  1612. while (NULL != udpw)
  1613. {
  1614. tmp = udpw->next;
  1615. if ( (NULL != udpw->frag_ctx) &&
  1616. (udpw->frag_ctx == frag_ctx) )
  1617. {
  1618. dequeue (plugin,
  1619. udpw);
  1620. GNUNET_free (udpw);
  1621. }
  1622. udpw = tmp;
  1623. }
  1624. }
  1625. notify_session_monitor (s->plugin,
  1626. s,
  1627. GNUNET_TRANSPORT_SS_UPDATE);
  1628. GNUNET_FRAGMENT_context_destroy (frag_ctx->frag,
  1629. &s->last_expected_msg_delay,
  1630. &s->last_expected_ack_delay);
  1631. s->frag_ctx = NULL;
  1632. GNUNET_free (frag_ctx);
  1633. }
  1634. /**
  1635. * We are finished with a fragment in the message queue.
  1636. * Notify the continuation and update statistics.
  1637. *
  1638. * @param cls the `struct Plugin *`
  1639. * @param udpw the queue entry
  1640. * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
  1641. */
  1642. static void
  1643. qc_fragment_sent (void *cls,
  1644. struct UDP_MessageWrapper *udpw,
  1645. int result)
  1646. {
  1647. struct Plugin *plugin = cls;
  1648. GNUNET_assert (NULL != udpw->frag_ctx);
  1649. if (GNUNET_OK == result)
  1650. {
  1651. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1652. "Fragment of message with %u bytes transmitted to %s\n",
  1653. (unsigned int) udpw->payload_size,
  1654. GNUNET_i2s (&udpw->session->target));
  1655. GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
  1656. GNUNET_STATISTICS_update (plugin->env->stats,
  1657. "# UDP, fragmented msgs, fragments, sent, success",
  1658. 1,
  1659. GNUNET_NO);
  1660. GNUNET_STATISTICS_update (plugin->env->stats,
  1661. "# UDP, fragmented msgs, fragments bytes, sent, success",
  1662. udpw->msg_size,
  1663. GNUNET_NO);
  1664. }
  1665. else
  1666. {
  1667. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1668. "Failed to transmit fragment of message with %u bytes to %s\n",
  1669. (unsigned int) udpw->payload_size,
  1670. GNUNET_i2s (&udpw->session->target));
  1671. fragmented_message_done (udpw->frag_ctx,
  1672. GNUNET_SYSERR);
  1673. GNUNET_STATISTICS_update (plugin->env->stats,
  1674. "# UDP, fragmented msgs, fragments, sent, failure",
  1675. 1,
  1676. GNUNET_NO);
  1677. GNUNET_STATISTICS_update (plugin->env->stats,
  1678. "# UDP, fragmented msgs, fragments bytes, sent, failure",
  1679. udpw->msg_size,
  1680. GNUNET_NO);
  1681. }
  1682. }
  1683. /**
  1684. * Function that is called with messages created by the fragmentation
  1685. * module. In the case of the `proc` callback of the
  1686. * #GNUNET_FRAGMENT_context_create() function, this function must
  1687. * eventually call #GNUNET_FRAGMENT_context_transmission_done().
  1688. *
  1689. * @param cls closure, the `struct UDP_FragmentationContext`
  1690. * @param msg the message that was created
  1691. */
  1692. static void
  1693. enqueue_fragment (void *cls,
  1694. const struct GNUNET_MessageHeader *msg)
  1695. {
  1696. struct UDP_FragmentationContext *frag_ctx = cls;
  1697. struct Plugin *plugin = frag_ctx->plugin;
  1698. struct UDP_MessageWrapper *udpw;
  1699. struct GNUNET_ATS_Session *session = frag_ctx->session;
  1700. size_t msg_len = ntohs (msg->size);
  1701. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1702. "Enqueuing fragment with %u bytes\n",
  1703. msg_len);
  1704. udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
  1705. udpw->session = session;
  1706. udpw->msg_buf = (char *) &udpw[1];
  1707. udpw->msg_size = msg_len;
  1708. udpw->payload_size = msg_len; /* FIXME: minus fragment overhead */
  1709. udpw->timeout = frag_ctx->timeout;
  1710. udpw->start_time = frag_ctx->start_time;
  1711. udpw->transmission_time = frag_ctx->next_frag_time;
  1712. frag_ctx->next_frag_time
  1713. = GNUNET_TIME_absolute_add (frag_ctx->next_frag_time,
  1714. frag_ctx->flow_delay_from_other_peer);
  1715. udpw->frag_ctx = frag_ctx;
  1716. udpw->qc = &qc_fragment_sent;
  1717. udpw->qc_cls = plugin;
  1718. GNUNET_memcpy (udpw->msg_buf,
  1719. msg,
  1720. msg_len);
  1721. enqueue (plugin,
  1722. udpw);
  1723. if (session->address->address_length == sizeof (struct IPv4UdpAddress))
  1724. schedule_select_v4 (plugin);
  1725. else
  1726. schedule_select_v6 (plugin);
  1727. }
  1728. /**
  1729. * We are finished with a message from the message queue.
  1730. * Notify the continuation and update statistics.
  1731. *
  1732. * @param cls the `struct Plugin *`
  1733. * @param udpw the queue entry
  1734. * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
  1735. */
  1736. static void
  1737. qc_message_sent (void *cls,
  1738. struct UDP_MessageWrapper *udpw,
  1739. int result)
  1740. {
  1741. struct Plugin *plugin = cls;
  1742. size_t overhead;
  1743. struct GNUNET_TIME_Relative delay;
  1744. if (udpw->msg_size >= udpw->payload_size)
  1745. overhead = udpw->msg_size - udpw->payload_size;
  1746. else
  1747. overhead = udpw->msg_size;
  1748. if (NULL != udpw->cont)
  1749. {
  1750. delay = GNUNET_TIME_absolute_get_duration (udpw->start_time);
  1751. if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
  1752. {
  1753. LOG (GNUNET_ERROR_TYPE_WARNING,
  1754. "Message sent via UDP with delay of %s\n",
  1755. GNUNET_STRINGS_relative_time_to_string (delay,
  1756. GNUNET_YES));
  1757. }
  1758. else
  1759. {
  1760. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1761. "Message sent via UDP with delay of %s\n",
  1762. GNUNET_STRINGS_relative_time_to_string (delay,
  1763. GNUNET_YES));
  1764. }
  1765. udpw->cont (udpw->cont_cls,
  1766. &udpw->session->target,
  1767. result,
  1768. udpw->payload_size,
  1769. overhead);
  1770. }
  1771. if (GNUNET_OK == result)
  1772. {
  1773. GNUNET_STATISTICS_update (plugin->env->stats,
  1774. "# UDP, unfragmented msgs, messages, sent, success",
  1775. 1,
  1776. GNUNET_NO);
  1777. GNUNET_STATISTICS_update (plugin->env->stats,
  1778. "# UDP, unfragmented msgs, bytes payload, sent, success",
  1779. udpw->payload_size,
  1780. GNUNET_NO);
  1781. GNUNET_STATISTICS_update (plugin->env->stats,
  1782. "# UDP, unfragmented msgs, bytes overhead, sent, success",
  1783. overhead,
  1784. GNUNET_NO);
  1785. GNUNET_STATISTICS_update (plugin->env->stats,
  1786. "# UDP, total, bytes overhead, sent",
  1787. overhead,
  1788. GNUNET_NO);
  1789. GNUNET_STATISTICS_update (plugin->env->stats,
  1790. "# UDP, total, bytes payload, sent",
  1791. udpw->payload_size,
  1792. GNUNET_NO);
  1793. }
  1794. else
  1795. {
  1796. GNUNET_STATISTICS_update (plugin->env->stats,
  1797. "# UDP, unfragmented msgs, messages, sent, failure",
  1798. 1,
  1799. GNUNET_NO);
  1800. GNUNET_STATISTICS_update (plugin->env->stats,
  1801. "# UDP, unfragmented msgs, bytes payload, sent, failure",
  1802. udpw->payload_size,
  1803. GNUNET_NO);
  1804. GNUNET_STATISTICS_update (plugin->env->stats,
  1805. "# UDP, unfragmented msgs, bytes overhead, sent, failure",
  1806. overhead,
  1807. GNUNET_NO);
  1808. }
  1809. }
  1810. /**
  1811. * Function that can be used by the transport service to transmit a
  1812. * message using the plugin. Note that in the case of a peer
  1813. * disconnecting, the continuation MUST be called prior to the
  1814. * disconnect notification itself. This function will be called with
  1815. * this peer's HELLO message to initiate a fresh connection to another
  1816. * peer.
  1817. *
  1818. * @param cls closure
  1819. * @param s which session must be used
  1820. * @param msgbuf the message to transmit
  1821. * @param msgbuf_size number of bytes in @a msgbuf
  1822. * @param priority how important is the message (most plugins will
  1823. * ignore message priority and just FIFO)
  1824. * @param to how long to wait at most for the transmission (does not
  1825. * require plugins to discard the message after the timeout,
  1826. * just advisory for the desired delay; most plugins will ignore
  1827. * this as well)
  1828. * @param cont continuation to call once the message has
  1829. * been transmitted (or if the transport is ready
  1830. * for the next transmission call; or if the
  1831. * peer disconnected...); can be NULL
  1832. * @param cont_cls closure for @a cont
  1833. * @return number of bytes used (on the physical network, with overheads);
  1834. * -1 on hard errors (i.e. address invalid); 0 is a legal value
  1835. * and does NOT mean that the message was not transmitted (DV)
  1836. */
  1837. static ssize_t
  1838. udp_plugin_send (void *cls,
  1839. struct GNUNET_ATS_Session *s,
  1840. const char *msgbuf,
  1841. size_t msgbuf_size,
  1842. unsigned int priority,
  1843. struct GNUNET_TIME_Relative to,
  1844. GNUNET_TRANSPORT_TransmitContinuation cont,
  1845. void *cont_cls)
  1846. {
  1847. struct Plugin *plugin = cls;
  1848. size_t udpmlen = msgbuf_size + sizeof(struct UDPMessage);
  1849. struct UDP_FragmentationContext *frag_ctx;
  1850. struct UDP_MessageWrapper *udpw;
  1851. struct UDPMessage *udp;
  1852. char mbuf[udpmlen] GNUNET_ALIGN;
  1853. struct GNUNET_TIME_Relative latency;
  1854. if ( (sizeof(struct IPv6UdpAddress) == s->address->address_length) &&
  1855. (NULL == plugin->sockv6) )
  1856. return GNUNET_SYSERR;
  1857. if ( (sizeof(struct IPv4UdpAddress) == s->address->address_length) &&
  1858. (NULL == plugin->sockv4) )
  1859. return GNUNET_SYSERR;
  1860. if (udpmlen >= GNUNET_MAX_MESSAGE_SIZE)
  1861. {
  1862. GNUNET_break (0);
  1863. return GNUNET_SYSERR;
  1864. }
  1865. if (GNUNET_YES !=
  1866. GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
  1867. &s->target,
  1868. s))
  1869. {
  1870. GNUNET_break (0);
  1871. return GNUNET_SYSERR;
  1872. }
  1873. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1874. "UDP transmits %u-byte message to `%s' using address `%s'\n",
  1875. udpmlen,
  1876. GNUNET_i2s (&s->target),
  1877. udp_address_to_string (plugin,
  1878. s->address->address,
  1879. s->address->address_length));
  1880. udp = (struct UDPMessage *) mbuf;
  1881. udp->header.size = htons (udpmlen);
  1882. udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
  1883. udp->reserved = htonl (0);
  1884. udp->sender = *plugin->env->my_identity;
  1885. /* We do not update the session time out here! Otherwise this
  1886. * session will not timeout since we send keep alive before session
  1887. * can timeout.
  1888. *
  1889. * For UDP we update session timeout only on receive, this will
  1890. * cover keep alives, since remote peer will reply with keep alive
  1891. * responses!
  1892. */
  1893. if (udpmlen <= UDP_MTU)
  1894. {
  1895. /* unfragmented message */
  1896. udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
  1897. udpw->session = s;
  1898. udpw->msg_buf = (char *) &udpw[1];
  1899. udpw->msg_size = udpmlen; /* message size with UDP overhead */
  1900. udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
  1901. udpw->start_time = GNUNET_TIME_absolute_get ();
  1902. udpw->timeout = GNUNET_TIME_relative_to_absolute (to);
  1903. udpw->transmission_time = s->last_transmit_time;
  1904. s->last_transmit_time
  1905. = GNUNET_TIME_absolute_add (s->last_transmit_time,
  1906. s->flow_delay_from_other_peer);
  1907. udpw->cont = cont;
  1908. udpw->cont_cls = cont_cls;
  1909. udpw->frag_ctx = NULL;
  1910. udpw->qc = &qc_message_sent;
  1911. udpw->qc_cls = plugin;
  1912. GNUNET_memcpy (udpw->msg_buf,
  1913. udp,
  1914. sizeof (struct UDPMessage));
  1915. GNUNET_memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)],
  1916. msgbuf,
  1917. msgbuf_size);
  1918. enqueue (plugin,
  1919. udpw);
  1920. GNUNET_STATISTICS_update (plugin->env->stats,
  1921. "# UDP, unfragmented messages queued total",
  1922. 1,
  1923. GNUNET_NO);
  1924. GNUNET_STATISTICS_update (plugin->env->stats,
  1925. "# UDP, unfragmented bytes payload queued total",
  1926. msgbuf_size,
  1927. GNUNET_NO);
  1928. if (s->address->address_length == sizeof (struct IPv4UdpAddress))
  1929. schedule_select_v4 (plugin);
  1930. else
  1931. schedule_select_v6 (plugin);
  1932. }
  1933. else
  1934. {
  1935. /* fragmented message */
  1936. if (NULL != s->frag_ctx)
  1937. return GNUNET_SYSERR;
  1938. GNUNET_memcpy (&udp[1],
  1939. msgbuf,
  1940. msgbuf_size);
  1941. frag_ctx = GNUNET_new (struct UDP_FragmentationContext);
  1942. frag_ctx->plugin = plugin;
  1943. frag_ctx->session = s;
  1944. frag_ctx->cont = cont;
  1945. frag_ctx->cont_cls = cont_cls;
  1946. frag_ctx->start_time = GNUNET_TIME_absolute_get ();
  1947. frag_ctx->next_frag_time = s->last_transmit_time;
  1948. frag_ctx->flow_delay_from_other_peer
  1949. = GNUNET_TIME_relative_divide (s->flow_delay_from_other_peer,
  1950. 1 + (msgbuf_size /
  1951. UDP_MTU));
  1952. frag_ctx->timeout = GNUNET_TIME_relative_to_absolute (to);
  1953. frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
  1954. frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
  1955. frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
  1956. UDP_MTU,
  1957. &plugin->tracker,
  1958. s->last_expected_msg_delay,
  1959. s->last_expected_ack_delay,
  1960. &udp->header,
  1961. &enqueue_fragment,
  1962. frag_ctx);
  1963. s->frag_ctx = frag_ctx;
  1964. s->last_transmit_time = frag_ctx->next_frag_time;
  1965. latency = GNUNET_TIME_absolute_get_remaining (s->last_transmit_time);
  1966. if (latency.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
  1967. LOG (GNUNET_ERROR_TYPE_WARNING,
  1968. "Enqueued fragments will take %s for transmission to %s (queue size: %u)\n",
  1969. GNUNET_STRINGS_relative_time_to_string (latency,
  1970. GNUNET_YES),
  1971. GNUNET_i2s (&s->target),
  1972. (unsigned int) s->msgs_in_queue);
  1973. else
  1974. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1975. "Enqueued fragments will take %s for transmission to %s (queue size: %u)\n",
  1976. GNUNET_STRINGS_relative_time_to_string (latency,
  1977. GNUNET_YES),
  1978. GNUNET_i2s (&s->target),
  1979. (unsigned int) s->msgs_in_queue);
  1980. GNUNET_STATISTICS_update (plugin->env->stats,
  1981. "# UDP, fragmented messages active",
  1982. 1,
  1983. GNUNET_NO);
  1984. GNUNET_STATISTICS_update (plugin->env->stats,
  1985. "# UDP, fragmented messages, total",
  1986. 1,
  1987. GNUNET_NO);
  1988. GNUNET_STATISTICS_update (plugin->env->stats,
  1989. "# UDP, fragmented bytes (payload)",
  1990. frag_ctx->payload_size,
  1991. GNUNET_NO);
  1992. }
  1993. notify_session_monitor (s->plugin,
  1994. s,
  1995. GNUNET_TRANSPORT_SS_UPDATE);
  1996. return udpmlen;
  1997. }
  1998. /* ********************** Receiving ********************** */
  1999. /**
  2000. * Closure for #find_receive_context().
  2001. */
  2002. struct FindReceiveContext
  2003. {
  2004. /**
  2005. * Where to store the result.
  2006. */
  2007. struct DefragContext *rc;
  2008. /**
  2009. * Session associated with this context.
  2010. */
  2011. struct GNUNET_ATS_Session *session;
  2012. /**
  2013. * Address to find.
  2014. */
  2015. const union UdpAddress *udp_addr;
  2016. /**
  2017. * Number of bytes in @e udp_addr.
  2018. */
  2019. size_t udp_addr_len;
  2020. };
  2021. /**
  2022. * Scan the heap for a receive context with the given address.
  2023. *
  2024. * @param cls the `struct FindReceiveContext`
  2025. * @param node internal node of the heap
  2026. * @param element value stored at the node (a `struct ReceiveContext`)
  2027. * @param cost cost associated with the node
  2028. * @return #GNUNET_YES if we should continue to iterate,
  2029. * #GNUNET_NO if not.
  2030. */
  2031. static int
  2032. find_receive_context (void *cls,
  2033. struct GNUNET_CONTAINER_HeapNode *node,
  2034. void *element,
  2035. GNUNET_CONTAINER_HeapCostType cost)
  2036. {
  2037. struct FindReceiveContext *frc = cls;
  2038. struct DefragContext *e = element;
  2039. if ( (frc->udp_addr_len == e->udp_addr_len) &&
  2040. (0 == memcmp (frc->udp_addr,
  2041. e->udp_addr,
  2042. frc->udp_addr_len)) )
  2043. {
  2044. frc->rc = e;
  2045. return GNUNET_NO;
  2046. }
  2047. return GNUNET_YES;
  2048. }
  2049. /**
  2050. * Functions with this signature are called whenever we need to close
  2051. * a session due to a disconnect or failure to establish a connection.
  2052. *
  2053. * @param cls closure with the `struct Plugin`
  2054. * @param s session to close down
  2055. * @return #GNUNET_OK on success
  2056. */
  2057. static int
  2058. udp_disconnect_session (void *cls,
  2059. struct GNUNET_ATS_Session *s)
  2060. {
  2061. struct Plugin *plugin = cls;
  2062. struct UDP_MessageWrapper *udpw;
  2063. struct UDP_MessageWrapper *next;
  2064. struct FindReceiveContext frc;
  2065. GNUNET_assert (GNUNET_YES != s->in_destroy);
  2066. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2067. "Session %p to peer `%s' at address %s ended\n",
  2068. s,
  2069. GNUNET_i2s (&s->target),
  2070. udp_address_to_string (plugin,
  2071. s->address->address,
  2072. s->address->address_length));
  2073. if (NULL != s->timeout_task)
  2074. {
  2075. GNUNET_SCHEDULER_cancel (s->timeout_task);
  2076. s->timeout_task = NULL;
  2077. }
  2078. if (NULL != s->frag_ctx)
  2079. {
  2080. /* Remove fragmented message due to disconnect */
  2081. fragmented_message_done (s->frag_ctx,
  2082. GNUNET_SYSERR);
  2083. }
  2084. GNUNET_assert (GNUNET_YES ==
  2085. GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
  2086. &s->target,
  2087. s));
  2088. frc.rc = NULL;
  2089. frc.udp_addr = s->address->address;
  2090. frc.udp_addr_len = s->address->address_length;
  2091. /* Lookup existing receive context for this address */
  2092. if (NULL != plugin->defrag_ctxs)
  2093. {
  2094. GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
  2095. &find_receive_context,
  2096. &frc);
  2097. if (NULL != frc.rc)
  2098. {
  2099. struct DefragContext *d_ctx = frc.rc;
  2100. GNUNET_CONTAINER_heap_remove_node (d_ctx->hnode);
  2101. GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
  2102. GNUNET_free (d_ctx);
  2103. }
  2104. }
  2105. s->in_destroy = GNUNET_YES;
  2106. next = plugin->ipv4_queue_head;
  2107. while (NULL != (udpw = next))
  2108. {
  2109. next = udpw->next;
  2110. if (udpw->session == s)
  2111. {
  2112. dequeue (plugin,
  2113. udpw);
  2114. udpw->qc (udpw->qc_cls,
  2115. udpw,
  2116. GNUNET_SYSERR);
  2117. GNUNET_free (udpw);
  2118. }
  2119. }
  2120. next = plugin->ipv6_queue_head;
  2121. while (NULL != (udpw = next))
  2122. {
  2123. next = udpw->next;
  2124. if (udpw->session == s)
  2125. {
  2126. dequeue (plugin,
  2127. udpw);
  2128. udpw->qc (udpw->qc_cls,
  2129. udpw,
  2130. GNUNET_SYSERR);
  2131. GNUNET_free (udpw);
  2132. }
  2133. }
  2134. if ( (NULL != s->frag_ctx) &&
  2135. (NULL != s->frag_ctx->cont) )
  2136. {
  2137. /* The 'frag_ctx' itself will be freed in #free_session() a bit
  2138. later, as it might be in use right now */
  2139. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2140. "Calling continuation for fragemented message to `%s' with result SYSERR\n",
  2141. GNUNET_i2s (&s->target));
  2142. s->frag_ctx->cont (s->frag_ctx->cont_cls,
  2143. &s->target,
  2144. GNUNET_SYSERR,
  2145. s->frag_ctx->payload_size,
  2146. s->frag_ctx->on_wire_size);
  2147. }
  2148. notify_session_monitor (s->plugin,
  2149. s,
  2150. GNUNET_TRANSPORT_SS_DONE);
  2151. plugin->env->session_end (plugin->env->cls,
  2152. s->address,
  2153. s);
  2154. GNUNET_STATISTICS_set (plugin->env->stats,
  2155. "# UDP sessions active",
  2156. GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
  2157. GNUNET_NO);
  2158. if (0 == s->rc)
  2159. free_session (s);
  2160. return GNUNET_OK;
  2161. }
  2162. /**
  2163. * Handle a #GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK message.
  2164. *
  2165. * @param plugin the UDP plugin
  2166. * @param msg the (presumed) UDP ACK message
  2167. * @param udp_addr sender address
  2168. * @param udp_addr_len number of bytes in @a udp_addr
  2169. */
  2170. static void
  2171. read_process_ack (struct Plugin *plugin,
  2172. const struct GNUNET_MessageHeader *msg,
  2173. const union UdpAddress *udp_addr,
  2174. socklen_t udp_addr_len)
  2175. {
  2176. const struct GNUNET_MessageHeader *ack;
  2177. const struct UDP_ACK_Message *udp_ack;
  2178. struct GNUNET_HELLO_Address *address;
  2179. struct GNUNET_ATS_Session *s;
  2180. struct GNUNET_TIME_Relative flow_delay;
  2181. /* check message format */
  2182. if (ntohs (msg->size)
  2183. < sizeof(struct UDP_ACK_Message) + sizeof(struct GNUNET_MessageHeader))
  2184. {
  2185. GNUNET_break_op (0);
  2186. return;
  2187. }
  2188. udp_ack = (const struct UDP_ACK_Message *) msg;
  2189. ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
  2190. if (ntohs (ack->size) != ntohs (msg->size) - sizeof(struct UDP_ACK_Message))
  2191. {
  2192. GNUNET_break_op(0);
  2193. return;
  2194. }
  2195. /* Locate session */
  2196. address = GNUNET_HELLO_address_allocate (&udp_ack->sender,
  2197. PLUGIN_NAME,
  2198. udp_addr,
  2199. udp_addr_len,
  2200. GNUNET_HELLO_ADDRESS_INFO_NONE);
  2201. s = udp_plugin_lookup_session (plugin,
  2202. address);
  2203. if (NULL == s)
  2204. {
  2205. LOG (GNUNET_ERROR_TYPE_WARNING,
  2206. "UDP session of address %s for ACK not found\n",
  2207. udp_address_to_string (plugin,
  2208. address->address,
  2209. address->address_length));
  2210. GNUNET_HELLO_address_free (address);
  2211. return;
  2212. }
  2213. if (NULL == s->frag_ctx)
  2214. {
  2215. LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
  2216. "Fragmentation context of address %s for ACK (%s) not found\n",
  2217. udp_address_to_string (plugin,
  2218. address->address,
  2219. address->address_length),
  2220. GNUNET_FRAGMENT_print_ack (ack));
  2221. GNUNET_HELLO_address_free (address);
  2222. return;
  2223. }
  2224. GNUNET_HELLO_address_free (address);
  2225. /* evaluate flow delay: how long should we wait between messages? */
  2226. if (UINT32_MAX == ntohl (udp_ack->delay))
  2227. {
  2228. /* Other peer asked for us to terminate the session */
  2229. LOG (GNUNET_ERROR_TYPE_INFO,
  2230. "Asked to disconnect UDP session of %s\n",
  2231. GNUNET_i2s (&udp_ack->sender));
  2232. udp_disconnect_session (plugin,
  2233. s);
  2234. return;
  2235. }
  2236. flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
  2237. if (flow_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
  2238. LOG (GNUNET_ERROR_TYPE_WARNING,
  2239. "We received a sending delay of %s for %s\n",
  2240. GNUNET_STRINGS_relative_time_to_string (flow_delay,
  2241. GNUNET_YES),
  2242. GNUNET_i2s (&udp_ack->sender));
  2243. else
  2244. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2245. "We received a sending delay of %s for %s\n",
  2246. GNUNET_STRINGS_relative_time_to_string (flow_delay,
  2247. GNUNET_YES),
  2248. GNUNET_i2s (&udp_ack->sender));
  2249. /* Flow delay is for the reassembled packet, however, our delay
  2250. is per packet, so we need to adjust: */
  2251. s->flow_delay_from_other_peer = flow_delay;
  2252. /* Handle ACK */
  2253. if (GNUNET_OK !=
  2254. GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag,
  2255. ack))
  2256. {
  2257. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2258. "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
  2259. (unsigned int) ntohs (msg->size),
  2260. GNUNET_i2s (&udp_ack->sender),
  2261. udp_address_to_string (plugin,
  2262. udp_addr,
  2263. udp_addr_len));
  2264. /* Expect more ACKs to arrive */
  2265. return;
  2266. }
  2267. /* Remove fragmented message after successful sending */
  2268. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2269. "Message from %s at %s full ACK'ed\n",
  2270. GNUNET_i2s (&udp_ack->sender),
  2271. udp_address_to_string (plugin,
  2272. udp_addr,
  2273. udp_addr_len));
  2274. fragmented_message_done (s->frag_ctx,
  2275. GNUNET_OK);
  2276. }
  2277. /**
  2278. * Message tokenizer has broken up an incomming message. Pass it on
  2279. * to the service.
  2280. *
  2281. * @param cls the `struct GNUNET_ATS_Session *`
  2282. * @param hdr the actual message
  2283. * @return #GNUNET_OK (always)
  2284. */
  2285. static int
  2286. process_inbound_tokenized_messages (void *cls,
  2287. const struct GNUNET_MessageHeader *hdr)
  2288. {
  2289. struct GNUNET_ATS_Session *session = cls;
  2290. struct Plugin *plugin = session->plugin;
  2291. if (GNUNET_YES == session->in_destroy)
  2292. return GNUNET_OK;
  2293. reschedule_session_timeout (session);
  2294. session->flow_delay_for_other_peer
  2295. = plugin->env->receive (plugin->env->cls,
  2296. session->address,
  2297. session,
  2298. hdr);
  2299. return GNUNET_OK;
  2300. }
  2301. /**
  2302. * Destroy a session, plugin is being unloaded.
  2303. *
  2304. * @param cls the `struct Plugin`
  2305. * @param key hash of public key of target peer
  2306. * @param value a `struct PeerSession *` to clean up
  2307. * @return #GNUNET_OK (continue to iterate)
  2308. */
  2309. static int
  2310. disconnect_and_free_it (void *cls,
  2311. const struct GNUNET_PeerIdentity *key,
  2312. void *value)
  2313. {
  2314. struct Plugin *plugin = cls;
  2315. udp_disconnect_session (plugin,
  2316. value);
  2317. return GNUNET_OK;
  2318. }
  2319. /**
  2320. * Disconnect from a remote node. Clean up session if we have one for
  2321. * this peer.
  2322. *
  2323. * @param cls closure for this call (should be handle to Plugin)
  2324. * @param target the peeridentity of the peer to disconnect
  2325. * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
  2326. */
  2327. static void
  2328. udp_disconnect (void *cls,
  2329. const struct GNUNET_PeerIdentity *target)
  2330. {
  2331. struct Plugin *plugin = cls;
  2332. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2333. "Disconnecting from peer `%s'\n",
  2334. GNUNET_i2s (target));
  2335. GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
  2336. target,
  2337. &disconnect_and_free_it,
  2338. plugin);
  2339. }
  2340. /**
  2341. * Session was idle, so disconnect it.
  2342. *
  2343. * @param cls the `struct GNUNET_ATS_Session` to time out
  2344. */
  2345. static void
  2346. session_timeout (void *cls)
  2347. {
  2348. struct GNUNET_ATS_Session *s = cls;
  2349. struct Plugin *plugin = s->plugin;
  2350. struct GNUNET_TIME_Relative left;
  2351. s->timeout_task = NULL;
  2352. left = GNUNET_TIME_absolute_get_remaining (s->timeout);
  2353. if (left.rel_value_us > 0)
  2354. {
  2355. /* not actually our turn yet, but let's at least update
  2356. the monitor, it may think we're about to die ... */
  2357. notify_session_monitor (s->plugin,
  2358. s,
  2359. GNUNET_TRANSPORT_SS_UPDATE);
  2360. s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
  2361. &session_timeout,
  2362. s);
  2363. return;
  2364. }
  2365. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2366. "Session %p was idle for %s, disconnecting\n",
  2367. s,
  2368. GNUNET_STRINGS_relative_time_to_string (UDP_SESSION_TIME_OUT,
  2369. GNUNET_YES));
  2370. /* call session destroy function */
  2371. udp_disconnect_session (plugin,
  2372. s);
  2373. }
  2374. /**
  2375. * Allocate a new session for the given endpoint address.
  2376. * Note that this function does not inform the service
  2377. * of the new session, this is the responsibility of the
  2378. * caller (if needed).
  2379. *
  2380. * @param cls the `struct Plugin`
  2381. * @param address address of the other peer to use
  2382. * @param network_type network type the address belongs to
  2383. * @return NULL on error, otherwise session handle
  2384. */
  2385. static struct GNUNET_ATS_Session *
  2386. udp_plugin_create_session (void *cls,
  2387. const struct GNUNET_HELLO_Address *address,
  2388. enum GNUNET_NetworkType network_type)
  2389. {
  2390. struct Plugin *plugin = cls;
  2391. struct GNUNET_ATS_Session *s;
  2392. s = GNUNET_new (struct GNUNET_ATS_Session);
  2393. s->mst = GNUNET_MST_create (&process_inbound_tokenized_messages,
  2394. s);
  2395. s->plugin = plugin;
  2396. s->address = GNUNET_HELLO_address_copy (address);
  2397. s->target = address->peer;
  2398. s->last_transmit_time = GNUNET_TIME_absolute_get ();
  2399. s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
  2400. 250);
  2401. s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
  2402. s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO;
  2403. s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
  2404. s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
  2405. s->timeout_task = GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT,
  2406. &session_timeout,
  2407. s);
  2408. s->scope = network_type;
  2409. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2410. "Creating new session %p for peer `%s' address `%s'\n",
  2411. s,
  2412. GNUNET_i2s (&address->peer),
  2413. udp_address_to_string (plugin,
  2414. address->address,
  2415. address->address_length));
  2416. GNUNET_assert (GNUNET_OK ==
  2417. GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
  2418. &s->target,
  2419. s,
  2420. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
  2421. GNUNET_STATISTICS_set (plugin->env->stats,
  2422. "# UDP sessions active",
  2423. GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
  2424. GNUNET_NO);
  2425. notify_session_monitor (plugin,
  2426. s,
  2427. GNUNET_TRANSPORT_SS_INIT);
  2428. return s;
  2429. }
  2430. /**
  2431. * Creates a new outbound session the transport service will use to
  2432. * send data to the peer.
  2433. *
  2434. * @param cls the `struct Plugin *`
  2435. * @param address the address
  2436. * @return the session or NULL of max connections exceeded
  2437. */
  2438. static struct GNUNET_ATS_Session *
  2439. udp_plugin_get_session (void *cls,
  2440. const struct GNUNET_HELLO_Address *address)
  2441. {
  2442. struct Plugin *plugin = cls;
  2443. struct GNUNET_ATS_Session *s;
  2444. enum GNUNET_NetworkType network_type = GNUNET_NT_UNSPECIFIED;
  2445. const struct IPv4UdpAddress *udp_v4;
  2446. const struct IPv6UdpAddress *udp_v6;
  2447. if (NULL == address)
  2448. {
  2449. GNUNET_break (0);
  2450. return NULL;
  2451. }
  2452. if ( (address->address_length != sizeof(struct IPv4UdpAddress)) &&
  2453. (address->address_length != sizeof(struct IPv6UdpAddress)) )
  2454. {
  2455. GNUNET_break_op (0);
  2456. return NULL;
  2457. }
  2458. if (NULL != (s = udp_plugin_lookup_session (cls,
  2459. address)))
  2460. return s;
  2461. /* need to create new session */
  2462. if (sizeof (struct IPv4UdpAddress) == address->address_length)
  2463. {
  2464. struct sockaddr_in v4;
  2465. udp_v4 = (const struct IPv4UdpAddress *) address->address;
  2466. memset (&v4, '\0', sizeof (v4));
  2467. v4.sin_family = AF_INET;
  2468. #if HAVE_SOCKADDR_IN_SIN_LEN
  2469. v4.sin_len = sizeof (struct sockaddr_in);
  2470. #endif
  2471. v4.sin_port = udp_v4->u4_port;
  2472. v4.sin_addr.s_addr = udp_v4->ipv4_addr;
  2473. network_type = plugin->env->get_address_type (plugin->env->cls,
  2474. (const struct sockaddr *) &v4,
  2475. sizeof (v4));
  2476. }
  2477. if (sizeof (struct IPv6UdpAddress) == address->address_length)
  2478. {
  2479. struct sockaddr_in6 v6;
  2480. udp_v6 = (const struct IPv6UdpAddress *) address->address;
  2481. memset (&v6, '\0', sizeof (v6));
  2482. v6.sin6_family = AF_INET6;
  2483. #if HAVE_SOCKADDR_IN_SIN_LEN
  2484. v6.sin6_len = sizeof (struct sockaddr_in6);
  2485. #endif
  2486. v6.sin6_port = udp_v6->u6_port;
  2487. v6.sin6_addr = udp_v6->ipv6_addr;
  2488. network_type = plugin->env->get_address_type (plugin->env->cls,
  2489. (const struct sockaddr *) &v6,
  2490. sizeof (v6));
  2491. }
  2492. GNUNET_break (GNUNET_NT_UNSPECIFIED != network_type);
  2493. return udp_plugin_create_session (cls,
  2494. address,
  2495. network_type);
  2496. }
  2497. /**
  2498. * We've received a UDP Message. Process it (pass contents to main service).
  2499. *
  2500. * @param plugin plugin context
  2501. * @param msg the message
  2502. * @param udp_addr sender address
  2503. * @param udp_addr_len number of bytes in @a udp_addr
  2504. * @param network_type network type the address belongs to
  2505. */
  2506. static void
  2507. process_udp_message (struct Plugin *plugin,
  2508. const struct UDPMessage *msg,
  2509. const union UdpAddress *udp_addr,
  2510. size_t udp_addr_len,
  2511. enum GNUNET_NetworkType network_type)
  2512. {
  2513. struct GNUNET_ATS_Session *s;
  2514. struct GNUNET_HELLO_Address *address;
  2515. GNUNET_break (GNUNET_NT_UNSPECIFIED != network_type);
  2516. if (0 != ntohl (msg->reserved))
  2517. {
  2518. GNUNET_break_op(0);
  2519. return;
  2520. }
  2521. if (ntohs (msg->header.size)
  2522. < sizeof(struct GNUNET_MessageHeader) + sizeof(struct UDPMessage))
  2523. {
  2524. GNUNET_break_op(0);
  2525. return;
  2526. }
  2527. address = GNUNET_HELLO_address_allocate (&msg->sender,
  2528. PLUGIN_NAME,
  2529. udp_addr,
  2530. udp_addr_len,
  2531. GNUNET_HELLO_ADDRESS_INFO_NONE);
  2532. if (NULL ==
  2533. (s = udp_plugin_lookup_session (plugin,
  2534. address)))
  2535. {
  2536. s = udp_plugin_create_session (plugin,
  2537. address,
  2538. network_type);
  2539. plugin->env->session_start (plugin->env->cls,
  2540. address,
  2541. s,
  2542. s->scope);
  2543. notify_session_monitor (plugin,
  2544. s,
  2545. GNUNET_TRANSPORT_SS_UP);
  2546. }
  2547. GNUNET_free (address);
  2548. s->rc++;
  2549. GNUNET_MST_from_buffer (s->mst,
  2550. (const char *) &msg[1],
  2551. ntohs (msg->header.size) - sizeof(struct UDPMessage),
  2552. GNUNET_YES,
  2553. GNUNET_NO);
  2554. s->rc--;
  2555. if ( (0 == s->rc) &&
  2556. (GNUNET_YES == s->in_destroy) )
  2557. free_session (s);
  2558. }
  2559. /**
  2560. * Process a defragmented message.
  2561. *
  2562. * @param cls the `struct DefragContext *`
  2563. * @param msg the message
  2564. */
  2565. static void
  2566. fragment_msg_proc (void *cls,
  2567. const struct GNUNET_MessageHeader *msg)
  2568. {
  2569. struct DefragContext *dc = cls;
  2570. const struct UDPMessage *um;
  2571. if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
  2572. {
  2573. GNUNET_break_op (0);
  2574. return;
  2575. }
  2576. if (ntohs (msg->size) < sizeof(struct UDPMessage))
  2577. {
  2578. GNUNET_break_op (0);
  2579. return;
  2580. }
  2581. um = (const struct UDPMessage *) msg;
  2582. dc->sender = um->sender;
  2583. dc->have_sender = GNUNET_YES;
  2584. process_udp_message (dc->plugin,
  2585. um,
  2586. dc->udp_addr,
  2587. dc->udp_addr_len,
  2588. dc->network_type);
  2589. }
  2590. /**
  2591. * We finished sending an acknowledgement. Update
  2592. * statistics.
  2593. *
  2594. * @param cls the `struct Plugin`
  2595. * @param udpw message queue entry of the ACK
  2596. * @param result #GNUNET_OK if the transmission worked,
  2597. * #GNUNET_SYSERR if we failed to send the ACK
  2598. */
  2599. static void
  2600. ack_message_sent (void *cls,
  2601. struct UDP_MessageWrapper *udpw,
  2602. int result)
  2603. {
  2604. struct Plugin *plugin = cls;
  2605. if (GNUNET_OK == result)
  2606. {
  2607. GNUNET_STATISTICS_update (plugin->env->stats,
  2608. "# UDP, ACK messages sent",
  2609. 1,
  2610. GNUNET_NO);
  2611. }
  2612. else
  2613. {
  2614. GNUNET_STATISTICS_update (plugin->env->stats,
  2615. "# UDP, ACK transmissions failed",
  2616. 1,
  2617. GNUNET_NO);
  2618. }
  2619. }
  2620. /**
  2621. * Transmit an acknowledgement.
  2622. *
  2623. * @param cls the `struct DefragContext *`
  2624. * @param id message ID (unused)
  2625. * @param msg ack to transmit
  2626. */
  2627. static void
  2628. ack_proc (void *cls,
  2629. uint32_t id,
  2630. const struct GNUNET_MessageHeader *msg)
  2631. {
  2632. struct DefragContext *rc = cls;
  2633. struct Plugin *plugin = rc->plugin;
  2634. size_t msize = sizeof(struct UDP_ACK_Message) + ntohs (msg->size);
  2635. struct UDP_ACK_Message *udp_ack;
  2636. uint32_t delay;
  2637. struct UDP_MessageWrapper *udpw;
  2638. struct GNUNET_ATS_Session *s;
  2639. struct GNUNET_HELLO_Address *address;
  2640. if (GNUNET_NO == rc->have_sender)
  2641. {
  2642. /* tried to defragment but never succeeded, hence will not ACK */
  2643. /* This can happen if we just lost msgs */
  2644. GNUNET_STATISTICS_update (plugin->env->stats,
  2645. "# UDP, fragments discarded without ACK",
  2646. 1,
  2647. GNUNET_NO);
  2648. return;
  2649. }
  2650. address = GNUNET_HELLO_address_allocate (&rc->sender,
  2651. PLUGIN_NAME,
  2652. rc->udp_addr,
  2653. rc->udp_addr_len,
  2654. GNUNET_HELLO_ADDRESS_INFO_NONE);
  2655. s = udp_plugin_lookup_session (plugin,
  2656. address);
  2657. GNUNET_HELLO_address_free (address);
  2658. if (NULL == s)
  2659. {
  2660. LOG (GNUNET_ERROR_TYPE_ERROR,
  2661. "Trying to transmit ACK to peer `%s' but no session found!\n",
  2662. udp_address_to_string (plugin,
  2663. rc->udp_addr,
  2664. rc->udp_addr_len));
  2665. GNUNET_CONTAINER_heap_remove_node (rc->hnode);
  2666. GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
  2667. GNUNET_free (rc);
  2668. GNUNET_STATISTICS_update (plugin->env->stats,
  2669. "# UDP, ACK transmissions failed",
  2670. 1,
  2671. GNUNET_NO);
  2672. return;
  2673. }
  2674. if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
  2675. s->flow_delay_for_other_peer.rel_value_us)
  2676. delay = UINT32_MAX;
  2677. else if (s->flow_delay_for_other_peer.rel_value_us < UINT32_MAX)
  2678. delay = s->flow_delay_for_other_peer.rel_value_us;
  2679. else
  2680. delay = UINT32_MAX - 1; /* largest value we can communicate */
  2681. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2682. "Sending ACK to `%s' including delay of %s\n",
  2683. udp_address_to_string (plugin,
  2684. rc->udp_addr,
  2685. rc->udp_addr_len),
  2686. GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
  2687. GNUNET_YES));
  2688. udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
  2689. udpw->msg_size = msize;
  2690. udpw->payload_size = 0;
  2691. udpw->session = s;
  2692. udpw->start_time = GNUNET_TIME_absolute_get ();
  2693. udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
  2694. udpw->msg_buf = (char *) &udpw[1];
  2695. udpw->qc = &ack_message_sent;
  2696. udpw->qc_cls = plugin;
  2697. udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
  2698. udp_ack->header.size = htons ((uint16_t) msize);
  2699. udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
  2700. udp_ack->delay = htonl (delay);
  2701. udp_ack->sender = *plugin->env->my_identity;
  2702. GNUNET_memcpy (&udp_ack[1],
  2703. msg,
  2704. ntohs (msg->size));
  2705. enqueue (plugin,
  2706. udpw);
  2707. notify_session_monitor (plugin,
  2708. s,
  2709. GNUNET_TRANSPORT_SS_UPDATE);
  2710. if (s->address->address_length == sizeof (struct IPv4UdpAddress))
  2711. schedule_select_v4 (plugin);
  2712. else
  2713. schedule_select_v6 (plugin);
  2714. }
  2715. /**
  2716. * We received a fragment, process it.
  2717. *
  2718. * @param plugin our plugin
  2719. * @param msg a message of type #GNUNET_MESSAGE_TYPE_FRAGMENT
  2720. * @param udp_addr sender address
  2721. * @param udp_addr_len number of bytes in @a udp_addr
  2722. * @param network_type network type the address belongs to
  2723. */
  2724. static void
  2725. read_process_fragment (struct Plugin *plugin,
  2726. const struct GNUNET_MessageHeader *msg,
  2727. const union UdpAddress *udp_addr,
  2728. size_t udp_addr_len,
  2729. enum GNUNET_NetworkType network_type)
  2730. {
  2731. struct DefragContext *d_ctx;
  2732. struct GNUNET_TIME_Absolute now;
  2733. struct FindReceiveContext frc;
  2734. frc.rc = NULL;
  2735. frc.udp_addr = udp_addr;
  2736. frc.udp_addr_len = udp_addr_len;
  2737. /* Lookup existing receive context for this address */
  2738. GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
  2739. &find_receive_context,
  2740. &frc);
  2741. now = GNUNET_TIME_absolute_get ();
  2742. d_ctx = frc.rc;
  2743. if (NULL == d_ctx)
  2744. {
  2745. /* Create a new defragmentation context */
  2746. d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + udp_addr_len);
  2747. GNUNET_memcpy (&d_ctx[1],
  2748. udp_addr,
  2749. udp_addr_len);
  2750. d_ctx->udp_addr = (const union UdpAddress *) &d_ctx[1];
  2751. d_ctx->udp_addr_len = udp_addr_len;
  2752. d_ctx->network_type = network_type;
  2753. d_ctx->plugin = plugin;
  2754. d_ctx->defrag = GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
  2755. UDP_MTU,
  2756. UDP_MAX_MESSAGES_IN_DEFRAG,
  2757. d_ctx,
  2758. &fragment_msg_proc,
  2759. &ack_proc);
  2760. d_ctx->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs,
  2761. d_ctx,
  2762. (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
  2763. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2764. "Created new defragmentation context for %u-byte fragment from `%s'\n",
  2765. (unsigned int) ntohs (msg->size),
  2766. udp_address_to_string (plugin,
  2767. udp_addr,
  2768. udp_addr_len));
  2769. }
  2770. else
  2771. {
  2772. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2773. "Found existing defragmentation context for %u-byte fragment from `%s'\n",
  2774. (unsigned int) ntohs (msg->size),
  2775. udp_address_to_string (plugin,
  2776. udp_addr,
  2777. udp_addr_len));
  2778. }
  2779. if (GNUNET_OK ==
  2780. GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag,
  2781. msg))
  2782. {
  2783. /* keep this 'rc' from expiring */
  2784. GNUNET_CONTAINER_heap_update_cost (d_ctx->hnode,
  2785. (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
  2786. }
  2787. if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
  2788. UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
  2789. {
  2790. /* remove 'rc' that was inactive the longest */
  2791. d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
  2792. GNUNET_assert (NULL != d_ctx);
  2793. GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
  2794. GNUNET_free (d_ctx);
  2795. GNUNET_STATISTICS_update (plugin->env->stats,
  2796. "# UDP, Defragmentations aborted",
  2797. 1,
  2798. GNUNET_NO);
  2799. }
  2800. }
  2801. /**
  2802. * Read and process a message from the given socket.
  2803. *
  2804. * @param plugin the overall plugin
  2805. * @param rsock socket to read from
  2806. */
  2807. static void
  2808. udp_select_read (struct Plugin *plugin,
  2809. struct GNUNET_NETWORK_Handle *rsock)
  2810. {
  2811. socklen_t fromlen;
  2812. struct sockaddr_storage addr;
  2813. char buf[65536] GNUNET_ALIGN;
  2814. ssize_t size;
  2815. const struct GNUNET_MessageHeader *msg;
  2816. struct IPv4UdpAddress v4;
  2817. struct IPv6UdpAddress v6;
  2818. const struct sockaddr *sa;
  2819. const struct sockaddr_in *sa4;
  2820. const struct sockaddr_in6 *sa6;
  2821. const union UdpAddress *int_addr;
  2822. size_t int_addr_len;
  2823. enum GNUNET_NetworkType network_type;
  2824. fromlen = sizeof (addr);
  2825. memset (&addr,
  2826. 0,
  2827. sizeof(addr));
  2828. size = GNUNET_NETWORK_socket_recvfrom (rsock,
  2829. buf,
  2830. sizeof (buf),
  2831. (struct sockaddr *) &addr,
  2832. &fromlen);
  2833. sa = (const struct sockaddr *) &addr;
  2834. #if MINGW
  2835. /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
  2836. * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
  2837. * on this socket has failed.
  2838. * Quote from MSDN:
  2839. * WSAECONNRESET - The virtual circuit was reset by the remote side
  2840. * executing a hard or abortive close. The application should close
  2841. * the socket; it is no longer usable. On a UDP-datagram socket this
  2842. * error indicates a previous send operation resulted in an ICMP Port
  2843. * Unreachable message.
  2844. */
  2845. if ( (-1 == size) &&
  2846. (ECONNRESET == errno) )
  2847. return;
  2848. #endif
  2849. if (-1 == size)
  2850. {
  2851. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2852. "UDP failed to receive data: %s\n",
  2853. STRERROR (errno));
  2854. /* Connection failure or something. Not a protocol violation. */
  2855. return;
  2856. }
  2857. /* Check if this is a STUN packet */
  2858. if (GNUNET_NO !=
  2859. GNUNET_NAT_stun_handle_packet (plugin->nat,
  2860. (const struct sockaddr *) &addr,
  2861. fromlen,
  2862. buf,
  2863. size))
  2864. return; /* was STUN, do not process further */
  2865. if (size < sizeof(struct GNUNET_MessageHeader))
  2866. {
  2867. LOG (GNUNET_ERROR_TYPE_WARNING,
  2868. "UDP got %u bytes from %s, which is not enough for a GNUnet message header\n",
  2869. (unsigned int ) size,
  2870. GNUNET_a2s (sa,
  2871. fromlen));
  2872. /* _MAY_ be a connection failure (got partial message) */
  2873. /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
  2874. GNUNET_break_op (0);
  2875. return;
  2876. }
  2877. msg = (const struct GNUNET_MessageHeader *) buf;
  2878. LOG (GNUNET_ERROR_TYPE_DEBUG,
  2879. "UDP received %u-byte message from `%s' type %u\n",
  2880. (unsigned int) size,
  2881. GNUNET_a2s (sa,
  2882. fromlen),
  2883. ntohs (msg->type));
  2884. if (size != ntohs (msg->size))
  2885. {
  2886. LOG (GNUNET_ERROR_TYPE_WARNING,
  2887. "UDP malformed message (size %u) header from %s\n",
  2888. (unsigned int) size,
  2889. GNUNET_a2s (sa,
  2890. fromlen));
  2891. GNUNET_break_op (0);
  2892. return;
  2893. }
  2894. GNUNET_STATISTICS_update (plugin->env->stats,
  2895. "# UDP, total bytes received",
  2896. size,
  2897. GNUNET_NO);
  2898. network_type = plugin->env->get_address_type (plugin->env->cls,
  2899. sa,
  2900. fromlen);
  2901. switch (sa->sa_family)
  2902. {
  2903. case AF_INET:
  2904. sa4 = (const struct sockaddr_in *) &addr;
  2905. v4.options = 0;
  2906. v4.ipv4_addr = sa4->sin_addr.s_addr;
  2907. v4.u4_port = sa4->sin_port;
  2908. int_addr = (union UdpAddress *) &v4;
  2909. int_addr_len = sizeof (v4);
  2910. break;
  2911. case AF_INET6:
  2912. sa6 = (const struct sockaddr_in6 *) &addr;
  2913. v6.options = 0;
  2914. v6.ipv6_addr = sa6->sin6_addr;
  2915. v6.u6_port = sa6->sin6_port;
  2916. int_addr = (union UdpAddress *) &v6;
  2917. int_addr_len = sizeof (v6);
  2918. break;
  2919. default:
  2920. GNUNET_break (0);
  2921. return;
  2922. }
  2923. switch (ntohs (msg->type))
  2924. {
  2925. case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
  2926. if (GNUNET_YES == plugin->enable_broadcasting_receiving)
  2927. udp_broadcast_receive (plugin,
  2928. buf,
  2929. size,
  2930. int_addr,
  2931. int_addr_len,
  2932. network_type);
  2933. return;
  2934. case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
  2935. if (ntohs (msg->size) < sizeof(struct UDPMessage))
  2936. {
  2937. GNUNET_break_op(0);
  2938. return;
  2939. }
  2940. process_udp_message (plugin,
  2941. (const struct UDPMessage *) msg,
  2942. int_addr,
  2943. int_addr_len,
  2944. network_type);
  2945. return;
  2946. case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
  2947. read_process_ack (plugin,
  2948. msg,
  2949. int_addr,
  2950. int_addr_len);
  2951. return;
  2952. case GNUNET_MESSAGE_TYPE_FRAGMENT:
  2953. read_process_fragment (plugin,
  2954. msg,
  2955. int_addr,
  2956. int_addr_len,
  2957. network_type);
  2958. return;
  2959. default:
  2960. GNUNET_break_op(0);
  2961. return;
  2962. }
  2963. }
  2964. /**
  2965. * Removes messages from the transmission queue that have
  2966. * timed out, and then selects a message that should be
  2967. * transmitted next.
  2968. *
  2969. * @param plugin the UDP plugin
  2970. * @param sock which socket should we process the queue for (v4 or v6)
  2971. * @return message selected for transmission, or NULL for none
  2972. */
  2973. static struct UDP_MessageWrapper *
  2974. remove_timeout_messages_and_select (struct Plugin *plugin,
  2975. struct GNUNET_NETWORK_Handle *sock)
  2976. {
  2977. struct UDP_MessageWrapper *udpw;
  2978. struct GNUNET_TIME_Relative remaining;
  2979. struct GNUNET_ATS_Session *session;
  2980. int removed;
  2981. removed = GNUNET_NO;
  2982. udpw = (sock == plugin->sockv4)
  2983. ? plugin->ipv4_queue_head
  2984. : plugin->ipv6_queue_head;
  2985. while (NULL != udpw)
  2986. {
  2987. session = udpw->session;
  2988. /* Find messages with timeout */
  2989. remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
  2990. if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
  2991. {
  2992. /* Message timed out */
  2993. removed = GNUNET_YES;
  2994. dequeue (plugin,
  2995. udpw);
  2996. udpw->qc (udpw->qc_cls,
  2997. udpw,
  2998. GNUNET_SYSERR);
  2999. GNUNET_free (udpw);
  3000. if (sock == plugin->sockv4)
  3001. {
  3002. udpw = plugin->ipv4_queue_head;
  3003. }
  3004. else if (sock == plugin->sockv6)
  3005. {
  3006. udpw = plugin->ipv6_queue_head;
  3007. }
  3008. else
  3009. {
  3010. GNUNET_break (0); /* should never happen */
  3011. udpw = NULL;
  3012. }
  3013. GNUNET_STATISTICS_update (plugin->env->stats,
  3014. "# messages discarded due to timeout",
  3015. 1,
  3016. GNUNET_NO);
  3017. }
  3018. else
  3019. {
  3020. /* Message did not time out, check transmission time */
  3021. remaining = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
  3022. if (0 == remaining.rel_value_us)
  3023. {
  3024. /* this message is not delayed */
  3025. LOG (GNUNET_ERROR_TYPE_DEBUG,
  3026. "Message for peer `%s' (%u bytes) is not delayed \n",
  3027. GNUNET_i2s (&udpw->session->target),
  3028. udpw->payload_size);
  3029. break; /* Found message to send, break */
  3030. }
  3031. else
  3032. {
  3033. /* Message is delayed, try next */
  3034. LOG (GNUNET_ERROR_TYPE_DEBUG,
  3035. "Message for peer `%s' (%u bytes) is delayed for %s\n",
  3036. GNUNET_i2s (&udpw->session->target),
  3037. udpw->payload_size,
  3038. GNUNET_STRINGS_relative_time_to_string (remaining,
  3039. GNUNET_YES));
  3040. udpw = udpw->next;
  3041. }
  3042. }
  3043. }
  3044. if (GNUNET_YES == removed)
  3045. notify_session_monitor (session->plugin,
  3046. session,
  3047. GNUNET_TRANSPORT_SS_UPDATE);
  3048. return udpw;
  3049. }
  3050. /**
  3051. * We failed to transmit a message via UDP. Generate
  3052. * a descriptive error message.
  3053. *
  3054. * @param plugin our plugin
  3055. * @param sa target address we were trying to reach
  3056. * @param slen number of bytes in @a sa
  3057. * @param error the errno value returned from the sendto() call
  3058. */
  3059. static void
  3060. analyze_send_error (struct Plugin *plugin,
  3061. const struct sockaddr *sa,
  3062. socklen_t slen,
  3063. int error)
  3064. {
  3065. enum GNUNET_NetworkType type;
  3066. type = plugin->env->get_address_type (plugin->env->cls,
  3067. sa,
  3068. slen);
  3069. if ( ( (GNUNET_NT_LAN == type) ||
  3070. (GNUNET_NT_WAN == type) ) &&
  3071. ( (ENETUNREACH == errno) ||
  3072. (ENETDOWN == errno) ) )
  3073. {
  3074. if (slen == sizeof (struct sockaddr_in))
  3075. {
  3076. /* IPv4: "Network unreachable" or "Network down"
  3077. *
  3078. * This indicates we do not have connectivity
  3079. */
  3080. LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
  3081. _("UDP could not transmit message to `%s': "
  3082. "Network seems down, please check your network configuration\n"),
  3083. GNUNET_a2s (sa,
  3084. slen));
  3085. }
  3086. if (slen == sizeof (struct sockaddr_in6))
  3087. {
  3088. /* IPv6: "Network unreachable" or "Network down"
  3089. *
  3090. * This indicates that this system is IPv6 enabled, but does not
  3091. * have a valid global IPv6 address assigned or we do not have
  3092. * connectivity
  3093. */
  3094. LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
  3095. _("UDP could not transmit IPv6 message! "
  3096. "Please check your network configuration and disable IPv6 if your "
  3097. "connection does not have a global IPv6 address\n"));
  3098. }
  3099. }
  3100. else
  3101. {
  3102. LOG (GNUNET_ERROR_TYPE_WARNING,
  3103. "UDP could not transmit message to `%s': `%s'\n",
  3104. GNUNET_a2s (sa,
  3105. slen),
  3106. STRERROR (error));
  3107. }
  3108. }
  3109. /**
  3110. * It is time to try to transmit a UDP message. Select one
  3111. * and send.
  3112. *
  3113. * @param plugin the plugin
  3114. * @param sock which socket (v4/v6) to send on
  3115. */
  3116. static void
  3117. udp_select_send (struct Plugin *plugin,
  3118. struct GNUNET_NETWORK_Handle *sock)
  3119. {
  3120. ssize_t sent;
  3121. socklen_t slen;
  3122. const struct sockaddr *a;
  3123. const struct IPv4UdpAddress *u4;
  3124. struct sockaddr_in a4;
  3125. const struct IPv6UdpAddress *u6;
  3126. struct sockaddr_in6 a6;
  3127. struct UDP_MessageWrapper *udpw;
  3128. /* Find message(s) to send */
  3129. while (NULL != (udpw = remove_timeout_messages_and_select (plugin,
  3130. sock)))
  3131. {
  3132. if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
  3133. {
  3134. u4 = udpw->session->address->address;
  3135. memset (&a4,
  3136. 0,
  3137. sizeof(a4));
  3138. a4.sin_family = AF_INET;
  3139. #if HAVE_SOCKADDR_IN_SIN_LEN
  3140. a4.sin_len = sizeof (a4);
  3141. #endif
  3142. a4.sin_port = u4->u4_port;
  3143. a4.sin_addr.s_addr = u4->ipv4_addr;
  3144. a = (const struct sockaddr *) &a4;
  3145. slen = sizeof (a4);
  3146. }
  3147. else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
  3148. {
  3149. u6 = udpw->session->address->address;
  3150. memset (&a6,
  3151. 0,
  3152. sizeof(a6));
  3153. a6.sin6_family = AF_INET6;
  3154. #if HAVE_SOCKADDR_IN_SIN_LEN
  3155. a6.sin6_len = sizeof (a6);
  3156. #endif
  3157. a6.sin6_port = u6->u6_port;
  3158. a6.sin6_addr = u6->ipv6_addr;
  3159. a = (const struct sockaddr *) &a6;
  3160. slen = sizeof (a6);
  3161. }
  3162. else
  3163. {
  3164. GNUNET_break (0);
  3165. dequeue (plugin,
  3166. udpw);
  3167. udpw->qc (udpw->qc_cls,
  3168. udpw,
  3169. GNUNET_SYSERR);
  3170. notify_session_monitor (plugin,
  3171. udpw->session,
  3172. GNUNET_TRANSPORT_SS_UPDATE);
  3173. GNUNET_free (udpw);
  3174. continue;
  3175. }
  3176. sent = GNUNET_NETWORK_socket_sendto (sock,
  3177. udpw->msg_buf,
  3178. udpw->msg_size,
  3179. a,
  3180. slen);
  3181. udpw->session->last_transmit_time
  3182. = GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_get (),
  3183. udpw->session->last_transmit_time);
  3184. dequeue (plugin,
  3185. udpw);
  3186. if (GNUNET_SYSERR == sent)
  3187. {
  3188. /* Failure */
  3189. analyze_send_error (plugin,
  3190. a,
  3191. slen,
  3192. errno);
  3193. udpw->qc (udpw->qc_cls,
  3194. udpw,
  3195. GNUNET_SYSERR);
  3196. GNUNET_STATISTICS_update (plugin->env->stats,
  3197. "# UDP, total, bytes, sent, failure",
  3198. sent,
  3199. GNUNET_NO);
  3200. GNUNET_STATISTICS_update (plugin->env->stats,
  3201. "# UDP, total, messages, sent, failure",
  3202. 1,
  3203. GNUNET_NO);
  3204. }
  3205. else
  3206. {
  3207. /* Success */
  3208. LOG (GNUNET_ERROR_TYPE_DEBUG,
  3209. "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
  3210. (unsigned int) (udpw->msg_size),
  3211. GNUNET_i2s (&udpw->session->target),
  3212. GNUNET_a2s (a,
  3213. slen),
  3214. (int ) sent,
  3215. (sent < 0) ? STRERROR (errno) : "ok");
  3216. GNUNET_STATISTICS_update (plugin->env->stats,
  3217. "# UDP, total, bytes, sent, success",
  3218. sent,
  3219. GNUNET_NO);
  3220. GNUNET_STATISTICS_update (plugin->env->stats,
  3221. "# UDP, total, messages, sent, success",
  3222. 1,
  3223. GNUNET_NO);
  3224. if (NULL != udpw->frag_ctx)
  3225. udpw->frag_ctx->on_wire_size += udpw->msg_size;
  3226. udpw->qc (udpw->qc_cls,
  3227. udpw,
  3228. GNUNET_OK);
  3229. }
  3230. notify_session_monitor (plugin,
  3231. udpw->session,
  3232. GNUNET_TRANSPORT_SS_UPDATE);
  3233. GNUNET_free (udpw);
  3234. }
  3235. }
  3236. /* ***************** Event loop (part 2) *************** */
  3237. /**
  3238. * We have been notified that our readset has something to read. We don't
  3239. * know which socket needs to be read, so we have to check each one
  3240. * Then reschedule this function to be called again once more is available.
  3241. *
  3242. * @param cls the plugin handle
  3243. */
  3244. static void
  3245. udp_plugin_select_v4 (void *cls)
  3246. {
  3247. struct Plugin *plugin = cls;
  3248. const struct GNUNET_SCHEDULER_TaskContext *tc;
  3249. plugin->select_task_v4 = NULL;
  3250. if (NULL == plugin->sockv4)
  3251. return;
  3252. tc = GNUNET_SCHEDULER_get_task_context ();
  3253. if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
  3254. (GNUNET_NETWORK_fdset_isset (tc->read_ready,
  3255. plugin->sockv4)))
  3256. udp_select_read (plugin,
  3257. plugin->sockv4);
  3258. udp_select_send (plugin,
  3259. plugin->sockv4);
  3260. schedule_select_v4 (plugin);
  3261. }
  3262. /**
  3263. * We have been notified that our readset has something to read. We don't
  3264. * know which socket needs to be read, so we have to check each one
  3265. * Then reschedule this function to be called again once more is available.
  3266. *
  3267. * @param cls the plugin handle
  3268. */
  3269. static void
  3270. udp_plugin_select_v6 (void *cls)
  3271. {
  3272. struct Plugin *plugin = cls;
  3273. const struct GNUNET_SCHEDULER_TaskContext *tc;
  3274. plugin->select_task_v6 = NULL;
  3275. if (NULL == plugin->sockv6)
  3276. return;
  3277. tc = GNUNET_SCHEDULER_get_task_context ();
  3278. if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
  3279. (GNUNET_NETWORK_fdset_isset (tc->read_ready,
  3280. plugin->sockv6)) )
  3281. udp_select_read (plugin,
  3282. plugin->sockv6);
  3283. udp_select_send (plugin,
  3284. plugin->sockv6);
  3285. schedule_select_v6 (plugin);
  3286. }
  3287. /* ******************* Initialization *************** */
  3288. /**
  3289. * Setup the UDP sockets (for IPv4 and IPv6) for the plugin.
  3290. *
  3291. * @param plugin the plugin to initialize
  3292. * @param bind_v6 IPv6 address to bind to (can be NULL, for 'any')
  3293. * @param bind_v4 IPv4 address to bind to (can be NULL, for 'any')
  3294. * @return number of sockets that were successfully bound
  3295. */
  3296. static unsigned int
  3297. setup_sockets (struct Plugin *plugin,
  3298. const struct sockaddr_in6 *bind_v6,
  3299. const struct sockaddr_in *bind_v4)
  3300. {
  3301. int tries;
  3302. unsigned int sockets_created = 0;
  3303. struct sockaddr_in6 server_addrv6;
  3304. struct sockaddr_in server_addrv4;
  3305. const struct sockaddr *server_addr;
  3306. const struct sockaddr *addrs[2];
  3307. socklen_t addrlens[2];
  3308. socklen_t addrlen;
  3309. int eno;
  3310. /* Create IPv6 socket */
  3311. eno = EINVAL;
  3312. if (GNUNET_YES == plugin->enable_ipv6)
  3313. {
  3314. plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6,
  3315. SOCK_DGRAM,
  3316. 0);
  3317. if (NULL == plugin->sockv6)
  3318. {
  3319. LOG (GNUNET_ERROR_TYPE_INFO,
  3320. _("Disabling IPv6 since it is not supported on this system!\n"));
  3321. plugin->enable_ipv6 = GNUNET_NO;
  3322. }
  3323. else
  3324. {
  3325. memset (&server_addrv6,
  3326. 0,
  3327. sizeof(struct sockaddr_in6));
  3328. #if HAVE_SOCKADDR_IN_SIN_LEN
  3329. server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
  3330. #endif
  3331. server_addrv6.sin6_family = AF_INET6;
  3332. if (NULL != bind_v6)
  3333. server_addrv6.sin6_addr = bind_v6->sin6_addr;
  3334. else
  3335. server_addrv6.sin6_addr = in6addr_any;
  3336. if (0 == plugin->port) /* autodetect */
  3337. server_addrv6.sin6_port
  3338. = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
  3339. 33537)
  3340. + 32000);
  3341. else
  3342. server_addrv6.sin6_port = htons (plugin->port);
  3343. addrlen = sizeof (struct sockaddr_in6);
  3344. server_addr = (const struct sockaddr *) &server_addrv6;
  3345. tries = 0;
  3346. while (tries < 10)
  3347. {
  3348. LOG(GNUNET_ERROR_TYPE_DEBUG,
  3349. "Binding to IPv6 `%s'\n",
  3350. GNUNET_a2s (server_addr,
  3351. addrlen));
  3352. /* binding */
  3353. if (GNUNET_OK ==
  3354. GNUNET_NETWORK_socket_bind (plugin->sockv6,
  3355. server_addr,
  3356. addrlen))
  3357. break;
  3358. eno = errno;
  3359. if (0 != plugin->port)
  3360. {
  3361. tries = 10; /* fail immediately */
  3362. break; /* bind failed on specific port */
  3363. }
  3364. /* autodetect */
  3365. server_addrv6.sin6_port
  3366. = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
  3367. 33537)
  3368. + 32000);
  3369. tries++;
  3370. }
  3371. if (tries >= 10)
  3372. {
  3373. GNUNET_NETWORK_socket_close (plugin->sockv6);
  3374. plugin->enable_ipv6 = GNUNET_NO;
  3375. plugin->sockv6 = NULL;
  3376. }
  3377. else
  3378. {
  3379. plugin->port = ntohs (server_addrv6.sin6_port);
  3380. }
  3381. if (NULL != plugin->sockv6)
  3382. {
  3383. LOG (GNUNET_ERROR_TYPE_DEBUG,
  3384. "IPv6 UDP socket created listinging at %s\n",
  3385. GNUNET_a2s (server_addr,
  3386. addrlen));
  3387. addrs[sockets_created] = server_addr;
  3388. addrlens[sockets_created] = addrlen;
  3389. sockets_created++;
  3390. }
  3391. else
  3392. {
  3393. LOG (GNUNET_ERROR_TYPE_WARNING,
  3394. _("Failed to bind UDP socket to %s: %s\n"),
  3395. GNUNET_a2s (server_addr,
  3396. addrlen),
  3397. STRERROR (eno));
  3398. }
  3399. }
  3400. }
  3401. /* Create IPv4 socket */
  3402. eno = EINVAL;
  3403. plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET,
  3404. SOCK_DGRAM,
  3405. 0);
  3406. if (NULL == plugin->sockv4)
  3407. {
  3408. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
  3409. "socket");
  3410. LOG (GNUNET_ERROR_TYPE_INFO,
  3411. _("Disabling IPv4 since it is not supported on this system!\n"));
  3412. plugin->enable_ipv4 = GNUNET_NO;
  3413. }
  3414. else
  3415. {
  3416. memset (&server_addrv4,
  3417. 0,
  3418. sizeof(struct sockaddr_in));
  3419. #if HAVE_SOCKADDR_IN_SIN_LEN
  3420. server_addrv4.sin_len = sizeof (struct sockaddr_in);
  3421. #endif
  3422. server_addrv4.sin_family = AF_INET;
  3423. if (NULL != bind_v4)
  3424. server_addrv4.sin_addr = bind_v4->sin_addr;
  3425. else
  3426. server_addrv4.sin_addr.s_addr = INADDR_ANY;
  3427. if (0 == plugin->port)
  3428. /* autodetect */
  3429. server_addrv4.sin_port
  3430. = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
  3431. 33537)
  3432. + 32000);
  3433. else
  3434. server_addrv4.sin_port = htons (plugin->port);
  3435. addrlen = sizeof (struct sockaddr_in);
  3436. server_addr = (const struct sockaddr *) &server_addrv4;
  3437. tries = 0;
  3438. while (tries < 10)
  3439. {
  3440. LOG (GNUNET_ERROR_TYPE_DEBUG,
  3441. "Binding to IPv4 `%s'\n",
  3442. GNUNET_a2s (server_addr,
  3443. addrlen));
  3444. /* binding */
  3445. if (GNUNET_OK ==
  3446. GNUNET_NETWORK_socket_bind (plugin->sockv4,
  3447. server_addr,
  3448. addrlen))
  3449. break;
  3450. eno = errno;
  3451. if (0 != plugin->port)
  3452. {
  3453. tries = 10; /* fail */
  3454. break; /* bind failed on specific port */
  3455. }
  3456. /* autodetect */
  3457. server_addrv4.sin_port
  3458. = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
  3459. 33537)
  3460. + 32000);
  3461. tries++;
  3462. }
  3463. if (tries >= 10)
  3464. {
  3465. GNUNET_NETWORK_socket_close (plugin->sockv4);
  3466. plugin->enable_ipv4 = GNUNET_NO;
  3467. plugin->sockv4 = NULL;
  3468. }
  3469. else
  3470. {
  3471. plugin->port = ntohs (server_addrv4.sin_port);
  3472. }
  3473. if (NULL != plugin->sockv4)
  3474. {
  3475. LOG (GNUNET_ERROR_TYPE_DEBUG,
  3476. "IPv4 socket created on port %s\n",
  3477. GNUNET_a2s (server_addr,
  3478. addrlen));
  3479. addrs[sockets_created] = server_addr;
  3480. addrlens[sockets_created] = addrlen;
  3481. sockets_created++;
  3482. }
  3483. else
  3484. {
  3485. LOG (GNUNET_ERROR_TYPE_ERROR,
  3486. _("Failed to bind UDP socket to %s: %s\n"),
  3487. GNUNET_a2s (server_addr,
  3488. addrlen),
  3489. STRERROR (eno));
  3490. }
  3491. }
  3492. if (0 == sockets_created)
  3493. {
  3494. LOG (GNUNET_ERROR_TYPE_WARNING,
  3495. _("Failed to open UDP sockets\n"));
  3496. return 0; /* No sockets created, return */
  3497. }
  3498. schedule_select_v4 (plugin);
  3499. schedule_select_v6 (plugin);
  3500. plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
  3501. "transport-udp",
  3502. IPPROTO_UDP,
  3503. sockets_created,
  3504. addrs,
  3505. addrlens,
  3506. &udp_nat_port_map_callback,
  3507. NULL,
  3508. plugin);
  3509. return sockets_created;
  3510. }
  3511. /**
  3512. * The exported method. Makes the core api available via a global and
  3513. * returns the udp transport API.
  3514. *
  3515. * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
  3516. * @return our `struct GNUNET_TRANSPORT_PluginFunctions`
  3517. */
  3518. void *
  3519. libgnunet_plugin_transport_udp_init (void *cls)
  3520. {
  3521. struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
  3522. struct GNUNET_TRANSPORT_PluginFunctions *api;
  3523. struct Plugin *p;
  3524. unsigned long long port;
  3525. unsigned long long aport;
  3526. unsigned long long udp_max_bps;
  3527. int enable_v6;
  3528. int enable_broadcasting;
  3529. int enable_broadcasting_recv;
  3530. char *bind4_address;
  3531. char *bind6_address;
  3532. struct GNUNET_TIME_Relative interval;
  3533. struct sockaddr_in server_addrv4;
  3534. struct sockaddr_in6 server_addrv6;
  3535. unsigned int res;
  3536. int have_bind4;
  3537. int have_bind6;
  3538. if (NULL == env->receive)
  3539. {
  3540. /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
  3541. initialze the plugin or the API */
  3542. api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
  3543. api->cls = NULL;
  3544. api->address_pretty_printer = &udp_plugin_address_pretty_printer;
  3545. api->address_to_string = &udp_address_to_string;
  3546. api->string_to_address = &udp_string_to_address;
  3547. return api;
  3548. }
  3549. /* Get port number: port == 0 : autodetect a port,
  3550. * > 0 : use this port, not given : 2086 default */
  3551. if (GNUNET_OK !=
  3552. GNUNET_CONFIGURATION_get_value_number (env->cfg,
  3553. "transport-udp",
  3554. "PORT",
  3555. &port))
  3556. port = 2086;
  3557. if (port > 65535)
  3558. {
  3559. GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
  3560. "transport-udp",
  3561. "PORT",
  3562. _("must be in [0,65535]"));
  3563. return NULL;
  3564. }
  3565. if (GNUNET_OK !=
  3566. GNUNET_CONFIGURATION_get_value_number (env->cfg,
  3567. "transport-udp",
  3568. "ADVERTISED_PORT",
  3569. &aport))
  3570. aport = port;
  3571. if (aport > 65535)
  3572. {
  3573. GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
  3574. "transport-udp",
  3575. "ADVERTISED_PORT",
  3576. _("must be in [0,65535]"));
  3577. return NULL;
  3578. }
  3579. if (GNUNET_YES ==
  3580. GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
  3581. "nat",
  3582. "DISABLEV6"))
  3583. enable_v6 = GNUNET_NO;
  3584. else
  3585. enable_v6 = GNUNET_YES;
  3586. have_bind4 = GNUNET_NO;
  3587. memset (&server_addrv4,
  3588. 0,
  3589. sizeof (server_addrv4));
  3590. if (GNUNET_YES ==
  3591. GNUNET_CONFIGURATION_get_value_string (env->cfg,
  3592. "transport-udp",
  3593. "BINDTO",
  3594. &bind4_address))
  3595. {
  3596. LOG (GNUNET_ERROR_TYPE_DEBUG,
  3597. "Binding UDP plugin to specific address: `%s'\n",
  3598. bind4_address);
  3599. if (1 != inet_pton (AF_INET,
  3600. bind4_address,
  3601. &server_addrv4.sin_addr))
  3602. {
  3603. GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
  3604. "transport-udp",
  3605. "BINDTO",
  3606. _("must be valid IPv4 address"));
  3607. GNUNET_free (bind4_address);
  3608. return NULL;
  3609. }
  3610. have_bind4 = GNUNET_YES;
  3611. }
  3612. GNUNET_free_non_null (bind4_address);
  3613. have_bind6 = GNUNET_NO;
  3614. memset (&server_addrv6,
  3615. 0,
  3616. sizeof (server_addrv6));
  3617. if (GNUNET_YES ==
  3618. GNUNET_CONFIGURATION_get_value_string (env->cfg,
  3619. "transport-udp",
  3620. "BINDTO6",
  3621. &bind6_address))
  3622. {
  3623. LOG (GNUNET_ERROR_TYPE_DEBUG,
  3624. "Binding udp plugin to specific address: `%s'\n",
  3625. bind6_address);
  3626. if (1 != inet_pton (AF_INET6,
  3627. bind6_address,
  3628. &server_addrv6.sin6_addr))
  3629. {
  3630. GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
  3631. "transport-udp",
  3632. "BINDTO6",
  3633. _("must be valid IPv6 address"));
  3634. GNUNET_free (bind6_address);
  3635. return NULL;
  3636. }
  3637. have_bind6 = GNUNET_YES;
  3638. }
  3639. GNUNET_free_non_null (bind6_address);
  3640. enable_broadcasting = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
  3641. "transport-udp",
  3642. "BROADCAST");
  3643. if (enable_broadcasting == GNUNET_SYSERR)
  3644. enable_broadcasting = GNUNET_NO;
  3645. enable_broadcasting_recv = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
  3646. "transport-udp",
  3647. "BROADCAST_RECEIVE");
  3648. if (enable_broadcasting_recv == GNUNET_SYSERR)
  3649. enable_broadcasting_recv = GNUNET_YES;
  3650. if (GNUNET_SYSERR ==
  3651. GNUNET_CONFIGURATION_get_value_time (env->cfg,
  3652. "transport-udp",
  3653. "BROADCAST_INTERVAL",
  3654. &interval))
  3655. {
  3656. interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
  3657. 10);
  3658. }
  3659. if (GNUNET_OK !=
  3660. GNUNET_CONFIGURATION_get_value_number (env->cfg,
  3661. "transport-udp",
  3662. "MAX_BPS",
  3663. &udp_max_bps))
  3664. {
  3665. /* 50 MB/s == infinity for practical purposes */
  3666. udp_max_bps = 1024 * 1024 * 50;
  3667. }
  3668. p = GNUNET_new (struct Plugin);
  3669. p->port = port;
  3670. p->aport = aport;
  3671. p->broadcast_interval = interval;
  3672. p->enable_ipv6 = enable_v6;
  3673. p->enable_ipv4 = GNUNET_YES; /* default */
  3674. p->enable_broadcasting = enable_broadcasting;
  3675. p->enable_broadcasting_receiving = enable_broadcasting_recv;
  3676. p->env = env;
  3677. p->sessions = GNUNET_CONTAINER_multipeermap_create (16,
  3678. GNUNET_NO);
  3679. p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
  3680. GNUNET_BANDWIDTH_tracker_init (&p->tracker,
  3681. NULL,
  3682. NULL,
  3683. GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps),
  3684. 30);
  3685. res = setup_sockets (p,
  3686. (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
  3687. (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL);
  3688. if ( (0 == res) ||
  3689. ( (NULL == p->sockv4) &&
  3690. (NULL == p->sockv6) ) )
  3691. {
  3692. LOG (GNUNET_ERROR_TYPE_ERROR,
  3693. _("Failed to create UDP network sockets\n"));
  3694. GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
  3695. GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
  3696. if (NULL != p->nat)
  3697. GNUNET_NAT_unregister (p->nat);
  3698. GNUNET_free (p);
  3699. return NULL;
  3700. }
  3701. /* Setup broadcasting and receiving beacons */
  3702. setup_broadcast (p,
  3703. &server_addrv6,
  3704. &server_addrv4);
  3705. api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
  3706. api->cls = p;
  3707. api->disconnect_session = &udp_disconnect_session;
  3708. api->query_keepalive_factor = &udp_query_keepalive_factor;
  3709. api->disconnect_peer = &udp_disconnect;
  3710. api->address_pretty_printer = &udp_plugin_address_pretty_printer;
  3711. api->address_to_string = &udp_address_to_string;
  3712. api->string_to_address = &udp_string_to_address;
  3713. api->check_address = &udp_plugin_check_address;
  3714. api->get_session = &udp_plugin_get_session;
  3715. api->send = &udp_plugin_send;
  3716. api->get_network = &udp_plugin_get_network;
  3717. api->get_network_for_address = &udp_plugin_get_network_for_address;
  3718. api->update_session_timeout = &udp_plugin_update_session_timeout;
  3719. api->setup_monitor = &udp_plugin_setup_monitor;
  3720. return api;
  3721. }
  3722. /**
  3723. * Function called on each entry in the defragmentation heap to
  3724. * clean it up.
  3725. *
  3726. * @param cls NULL
  3727. * @param node node in the heap (to be removed)
  3728. * @param element a `struct DefragContext` to be cleaned up
  3729. * @param cost unused
  3730. * @return #GNUNET_YES
  3731. */
  3732. static int
  3733. heap_cleanup_iterator (void *cls,
  3734. struct GNUNET_CONTAINER_HeapNode *node,
  3735. void *element,
  3736. GNUNET_CONTAINER_HeapCostType cost)
  3737. {
  3738. struct DefragContext *d_ctx = element;
  3739. GNUNET_CONTAINER_heap_remove_node (node);
  3740. GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
  3741. GNUNET_free (d_ctx);
  3742. return GNUNET_YES;
  3743. }
  3744. /**
  3745. * The exported method. Makes the core api available via a global and
  3746. * returns the udp transport API.
  3747. *
  3748. * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
  3749. * @return NULL
  3750. */
  3751. void *
  3752. libgnunet_plugin_transport_udp_done (void *cls)
  3753. {
  3754. struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
  3755. struct Plugin *plugin = api->cls;
  3756. struct PrettyPrinterContext *cur;
  3757. struct UDP_MessageWrapper *udpw;
  3758. if (NULL == plugin)
  3759. {
  3760. GNUNET_free (api);
  3761. return NULL;
  3762. }
  3763. stop_broadcast (plugin);
  3764. if (NULL != plugin->select_task_v4)
  3765. {
  3766. GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
  3767. plugin->select_task_v4 = NULL;
  3768. }
  3769. if (NULL != plugin->select_task_v6)
  3770. {
  3771. GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
  3772. plugin->select_task_v6 = NULL;
  3773. }
  3774. if (NULL != plugin->sockv4)
  3775. {
  3776. GNUNET_break (GNUNET_OK ==
  3777. GNUNET_NETWORK_socket_close (plugin->sockv4));
  3778. plugin->sockv4 = NULL;
  3779. }
  3780. if (NULL != plugin->sockv6)
  3781. {
  3782. GNUNET_break (GNUNET_OK ==
  3783. GNUNET_NETWORK_socket_close (plugin->sockv6));
  3784. plugin->sockv6 = NULL;
  3785. }
  3786. if (NULL != plugin->nat)
  3787. {
  3788. GNUNET_NAT_unregister (plugin->nat);
  3789. plugin->nat = NULL;
  3790. }
  3791. if (NULL != plugin->defrag_ctxs)
  3792. {
  3793. GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
  3794. &heap_cleanup_iterator,
  3795. NULL);
  3796. GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
  3797. plugin->defrag_ctxs = NULL;
  3798. }
  3799. while (NULL != (udpw = plugin->ipv4_queue_head))
  3800. {
  3801. dequeue (plugin,
  3802. udpw);
  3803. udpw->qc (udpw->qc_cls,
  3804. udpw,
  3805. GNUNET_SYSERR);
  3806. GNUNET_free (udpw);
  3807. }
  3808. while (NULL != (udpw = plugin->ipv6_queue_head))
  3809. {
  3810. dequeue (plugin,
  3811. udpw);
  3812. udpw->qc (udpw->qc_cls,
  3813. udpw,
  3814. GNUNET_SYSERR);
  3815. GNUNET_free (udpw);
  3816. }
  3817. GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
  3818. &disconnect_and_free_it,
  3819. plugin);
  3820. GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
  3821. while (NULL != (cur = plugin->ppc_dll_head))
  3822. {
  3823. GNUNET_break (0);
  3824. GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
  3825. plugin->ppc_dll_tail,
  3826. cur);
  3827. GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
  3828. if (NULL != cur->timeout_task)
  3829. {
  3830. GNUNET_SCHEDULER_cancel (cur->timeout_task);
  3831. cur->timeout_task = NULL;
  3832. }
  3833. GNUNET_free (cur);
  3834. }
  3835. GNUNET_free (plugin);
  3836. GNUNET_free (api);
  3837. return NULL;
  3838. }
  3839. /* end of plugin_transport_udp.c */