plugin_transport_udp.c 117 KB

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