gnunet-communicator-udp.c 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2010-2014, 2018, 2019 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/gnunet-communicator-udp.c
  18. * @brief Transport plugin using UDP.
  19. * @author Christian Grothoff
  20. *
  21. * TODO:
  22. * - consider imposing transmission limits in the absence
  23. * of ACKs; or: maybe this should be done at TNG service level?
  24. * (at least the receiver might want to enforce limits on
  25. * KX/DH operations per sender in here) (#5552)
  26. * - overall, we should look more into flow control support
  27. * (either in backchannel, or general solution in TNG service)
  28. * - handle addresses discovered from broadcasts (#5551)
  29. * (think: what was the story again on address validation?
  30. * where is the API for that!?!)
  31. * - support DNS names in BINDTO option (#5528)
  32. * - support NAT connection reversal method (#5529)
  33. * - support other UDP-specific NAT traversal methods (#)
  34. */
  35. #include "platform.h"
  36. #include "gnunet_util_lib.h"
  37. #include "gnunet_protocols.h"
  38. #include "gnunet_signatures.h"
  39. #include "gnunet_constants.h"
  40. #include "gnunet_nt_lib.h"
  41. #include "gnunet_nat_service.h"
  42. #include "gnunet_statistics_service.h"
  43. #include "gnunet_transport_application_service.h"
  44. #include "gnunet_transport_communication_service.h"
  45. /**
  46. * How often do we rekey based on time (at least)
  47. */
  48. #define DEFAULT_REKEY_TIME_INTERVAL GNUNET_TIME_UNIT_DAYS
  49. /**
  50. * How long do we wait until we must have received the initial KX?
  51. */
  52. #define PROTO_QUEUE_TIMEOUT GNUNET_TIME_UNIT_MINUTES
  53. /**
  54. * How often do we broadcast our presence on the LAN?
  55. */
  56. #define BROADCAST_FREQUENCY GNUNET_TIME_UNIT_MINUTES
  57. /**
  58. * How often do we scan for changes to our network interfaces?
  59. */
  60. #define INTERFACE_SCAN_FREQUENCY \
  61. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
  62. /**
  63. * How long do we believe our addresses to remain up (before
  64. * the other peer should revalidate).
  65. */
  66. #define ADDRESS_VALIDITY_PERIOD GNUNET_TIME_UNIT_HOURS
  67. #define WORKING_QUEUE_INTERVALL \
  68. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS,1)
  69. /**
  70. * AES key size.
  71. */
  72. #define AES_KEY_SIZE (256 / 8)
  73. /**
  74. * AES (GCM) IV size.
  75. */
  76. #define AES_IV_SIZE (96 / 8)
  77. /**
  78. * Size of the GCM tag.
  79. */
  80. #define GCM_TAG_SIZE (128 / 8)
  81. #define GENERATE_AT_ONCE 2
  82. /**
  83. * If we fall below this number of available KCNs,
  84. * we generate additional ACKs until we reach
  85. * #KCN_TARGET.
  86. * Should be large enough that we don't generate ACKs all
  87. * the time and still have enough time for the ACK to
  88. * arrive before the sender runs out. So really this
  89. * should ideally be based on the RTT.
  90. */
  91. #define KCN_THRESHOLD 92
  92. /**
  93. * How many KCNs do we keep around *after* we hit
  94. * the #KCN_THRESHOLD? Should be larger than
  95. * #KCN_THRESHOLD so we do not generate just one
  96. * ACK at the time.
  97. */
  98. #define KCN_TARGET 128
  99. /**
  100. * What is the maximum delta between KCN sequence numbers
  101. * that we allow. Used to expire 'ancient' KCNs that likely
  102. * were dropped by the network. Must be larger than
  103. * KCN_TARGET (otherwise we generate new KCNs all the time),
  104. * but not too large (otherwise packet loss may cause
  105. * sender to fall back to KX needlessly when sender runs
  106. * out of ACK'ed KCNs due to losses).
  107. */
  108. #define MAX_SQN_DELTA 160
  109. /**
  110. * How many shared master secrets do we keep around
  111. * at most per sender? Should be large enough so
  112. * that we generally have a chance of sending an ACK
  113. * before the sender already rotated out the master
  114. * secret. Generally values around #KCN_TARGET make
  115. * sense. Might make sense to adapt to RTT if we had
  116. * a good measurement...
  117. */
  118. #define MAX_SECRETS 128000
  119. /**
  120. * Default value for how often we do rekey based on number of bytes transmitted?
  121. * (additionally randomized).
  122. */
  123. #define DEFAULT_REKEY_MAX_BYTES (1024LLU * 1024 * 1024 * 4LLU)
  124. /**
  125. * Address prefix used by the communicator.
  126. */
  127. #define COMMUNICATOR_ADDRESS_PREFIX "udp"
  128. /**
  129. * Configuration section used by the communicator.
  130. */
  131. #define COMMUNICATOR_CONFIG_SECTION "communicator-udp"
  132. GNUNET_NETWORK_STRUCT_BEGIN
  133. /**
  134. * Signature we use to verify that the ephemeral key was really chosen by
  135. * the specified sender. If possible, the receiver should respond with
  136. * a `struct UDPAck` (possibly via backchannel).
  137. */
  138. struct UdpHandshakeSignature
  139. {
  140. /**
  141. * Purpose must be #GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE
  142. */
  143. struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
  144. /**
  145. * Identity of the inititor of the UDP connection (UDP client).
  146. */
  147. struct GNUNET_PeerIdentity sender;
  148. /**
  149. * Presumed identity of the target of the UDP connection (UDP server)
  150. */
  151. struct GNUNET_PeerIdentity receiver;
  152. /**
  153. * Ephemeral key used by the @e sender.
  154. */
  155. struct GNUNET_CRYPTO_EcdhePublicKey ephemeral;
  156. /**
  157. * Monotonic time of @e sender, to possibly help detect replay attacks
  158. * (if receiver persists times by sender).
  159. */
  160. struct GNUNET_TIME_AbsoluteNBO monotonic_time;
  161. };
  162. /**
  163. * "Plaintext" header at beginning of KX message. Followed
  164. * by encrypted `struct UDPConfirmation`.
  165. */
  166. struct InitialKX
  167. {
  168. /**
  169. * Ephemeral key for KX.
  170. */
  171. struct GNUNET_CRYPTO_EcdhePublicKey ephemeral;
  172. /**
  173. * HMAC for the following encrypted message, using GCM. HMAC uses
  174. * key derived from the handshake with sequence number zero.
  175. */
  176. char gcm_tag[GCM_TAG_SIZE];
  177. /**
  178. * A flag indicating, if the sender is doing rekeying.
  179. */
  180. int rekeying;
  181. };
  182. /**
  183. * Encrypted continuation of UDP initial handshake, followed
  184. * by message header with payload.
  185. */
  186. struct UDPConfirmation
  187. {
  188. /**
  189. * Sender's identity
  190. */
  191. struct GNUNET_PeerIdentity sender;
  192. /**
  193. * Sender's signature of type #GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE
  194. */
  195. struct GNUNET_CRYPTO_EddsaSignature sender_sig;
  196. /**
  197. * Monotonic time of @e sender, to possibly help detect replay attacks
  198. * (if receiver persists times by sender).
  199. */
  200. struct GNUNET_TIME_AbsoluteNBO monotonic_time;
  201. /* followed by messages */
  202. /* padding may follow actual messages */
  203. };
  204. /**
  205. * UDP key acknowledgement. May be sent via backchannel. Allows the
  206. * sender to use `struct UDPBox` with the acknowledge key henceforth.
  207. */
  208. struct UDPAck
  209. {
  210. /**
  211. * Type is #GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK.
  212. */
  213. struct GNUNET_MessageHeader header;
  214. /**
  215. * Sequence acknowledgement limit. Specifies current maximum sequence
  216. * number supported by receiver.
  217. */
  218. uint32_t sequence_max GNUNET_PACKED;
  219. /**
  220. * Sequence acknowledgement limit. Specifies current maximum sequence
  221. * number supported by receiver.
  222. */
  223. uint32_t acks_available GNUNET_PACKED;
  224. /**
  225. * CMAC of the base key being acknowledged.
  226. */
  227. struct GNUNET_HashCode cmac;
  228. };
  229. /**
  230. * Signature we use to verify that the broadcast was really made by
  231. * the peer that claims to have made it. Basically, affirms that the
  232. * peer is really using this IP address (albeit possibly not in _our_
  233. * LAN). Makes it difficult for peers in the LAN to claim to
  234. * be just any global peer -- an attacker must have at least
  235. * shared a LAN with the peer they're pretending to be here.
  236. */
  237. struct UdpBroadcastSignature
  238. {
  239. /**
  240. * Purpose must be #GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST
  241. */
  242. struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
  243. /**
  244. * Identity of the inititor of the UDP broadcast.
  245. */
  246. struct GNUNET_PeerIdentity sender;
  247. /**
  248. * Hash of the sender's UDP address.
  249. */
  250. struct GNUNET_HashCode h_address;
  251. };
  252. /**
  253. * Broadcast by peer in LAN announcing its presence. Unusual in that
  254. * we don't pad these to full MTU, as we cannot prevent being
  255. * recognized in LAN as GNUnet peers if this feature is enabled
  256. * anyway. Also, the entire message is in cleartext.
  257. */
  258. struct UDPBroadcast
  259. {
  260. /**
  261. * Sender's peer identity.
  262. */
  263. struct GNUNET_PeerIdentity sender;
  264. /**
  265. * Sender's signature of type
  266. * #GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST
  267. */
  268. struct GNUNET_CRYPTO_EddsaSignature sender_sig;
  269. };
  270. /**
  271. * UDP message box. Always sent encrypted, only allowed after
  272. * the receiver sent a `struct UDPAck` for the base key!
  273. */
  274. struct UDPBox
  275. {
  276. /**
  277. * Key and IV identification code. KDF applied to an acknowledged
  278. * base key and a sequence number. Sequence numbers must be used
  279. * monotonically increasing up to the maximum specified in
  280. * `struct UDPAck`. Without further `struct UDPAck`s, the sender
  281. * must fall back to sending handshakes!
  282. */
  283. struct GNUNET_ShortHashCode kid;
  284. /**
  285. * 128-bit authentication tag for the following encrypted message,
  286. * from GCM. MAC starts at the @e body_start that follows and
  287. * extends until the end of the UDP payload. If the @e hmac is
  288. * wrong, the receiver should check if the message might be a
  289. * `struct UdpHandshakeSignature`.
  290. */
  291. char gcm_tag[GCM_TAG_SIZE];
  292. /**
  293. * A flag indicating, if the sender is doing rekeying.
  294. */
  295. int rekeying;
  296. };
  297. /**
  298. * UDP message box. Always sent encrypted, only allowed after
  299. * the receiver sent a `struct UDPAck` for the base key!
  300. */
  301. struct UDPRekey
  302. {
  303. /**
  304. * Key and IV identification code. KDF applied to an acknowledged
  305. * base key and a sequence number. Sequence numbers must be used
  306. * monotonically increasing up to the maximum specified in
  307. * `struct UDPAck`. Without further `struct UDPAck`s, the sender
  308. * must fall back to sending handshakes!
  309. */
  310. struct GNUNET_ShortHashCode kid;
  311. /**
  312. * 128-bit authentication tag for the following encrypted message,
  313. * from GCM. MAC starts at the @e body_start that follows and
  314. * extends until the end of the UDP payload. If the @e hmac is
  315. * wrong, the receiver should check if the message might be a
  316. * `struct UdpHandshakeSignature`.
  317. */
  318. char gcm_tag[GCM_TAG_SIZE];
  319. /**
  320. * Sender's identity
  321. */
  322. struct GNUNET_PeerIdentity sender;
  323. };
  324. GNUNET_NETWORK_STRUCT_END
  325. /**
  326. * Shared secret we generated for a particular sender or receiver.
  327. */
  328. struct SharedSecret;
  329. /**
  330. * Pre-generated "kid" code (key and IV identification code) to
  331. * quickly derive master key for a `struct UDPBox`.
  332. */
  333. struct KeyCacheEntry
  334. {
  335. /**
  336. * Kept in a DLL.
  337. */
  338. struct KeyCacheEntry *next;
  339. /**
  340. * Kept in a DLL.
  341. */
  342. struct KeyCacheEntry *prev;
  343. /**
  344. * Key and IV identification code. KDF applied to an acknowledged
  345. * base key and a sequence number. Sequence numbers must be used
  346. * monotonically increasing up to the maximum specified in
  347. * `struct UDPAck`. Without further `struct UDPAck`s, the sender
  348. * must fall back to sending handshakes!
  349. */
  350. struct GNUNET_ShortHashCode kid;
  351. /**
  352. * Corresponding shared secret.
  353. */
  354. struct SharedSecret *ss;
  355. /**
  356. * Sequence number used to derive this entry from master key.
  357. */
  358. uint32_t sequence_number;
  359. };
  360. /**
  361. * Information we track per sender address we have recently been
  362. * in contact with (decryption from sender).
  363. */
  364. struct SenderAddress;
  365. /**
  366. * Information we track per receiving address we have recently been
  367. * in contact with (encryption to receiver).
  368. */
  369. struct ReceiverAddress;
  370. /**
  371. * Shared secret we generated for a particular sender or receiver.
  372. */
  373. struct SharedSecret
  374. {
  375. /**
  376. * Kept in a DLL.
  377. */
  378. struct SharedSecret *next;
  379. /**
  380. * Kept in a DLL.
  381. */
  382. struct SharedSecret *prev;
  383. /**
  384. * Kept in a DLL, sorted by sequence number. Only if we are decrypting.
  385. */
  386. struct KeyCacheEntry *kce_head;
  387. /**
  388. * Kept in a DLL, sorted by sequence number. Only if we are decrypting.
  389. */
  390. struct KeyCacheEntry *kce_tail;
  391. /**
  392. * Sender we use this shared secret with, or NULL.
  393. */
  394. struct SenderAddress *sender;
  395. /**
  396. * Receiver we use this shared secret with, or NULL.
  397. */
  398. struct ReceiverAddress *receiver;
  399. /**
  400. * Master shared secret.
  401. */
  402. struct GNUNET_HashCode master;
  403. /**
  404. * CMAC is used to identify @e master in ACKs.
  405. */
  406. struct GNUNET_HashCode cmac;
  407. /**
  408. * Up to which sequence number did we use this @e master already?
  409. * (for encrypting only)
  410. */
  411. uint32_t sequence_used;
  412. /**
  413. * Up to which sequence number did the other peer allow us to use
  414. * this key, or up to which number did we allow the other peer to
  415. * use this key?
  416. */
  417. uint32_t sequence_allowed;
  418. /**
  419. * Number of active KCN entries.
  420. */
  421. unsigned int active_kce_count;
  422. };
  423. /**
  424. * Information we track per sender address we have recently been
  425. * in contact with (we decrypt messages from the sender).
  426. */
  427. struct SenderAddress
  428. {
  429. /**
  430. * Shared secret we use with @e target for rekeying.
  431. */
  432. struct SharedSecret *ss_rekey;
  433. /**
  434. * Flag indicating sender is initiated rekeying for this receiver.
  435. */
  436. int rekeying;
  437. /**
  438. * To whom are we talking to.
  439. */
  440. struct GNUNET_PeerIdentity target;
  441. /**
  442. * Entry in sender expiration heap.
  443. */
  444. struct GNUNET_CONTAINER_HeapNode *hn;
  445. /**
  446. * Shared secrets we used with @e target, first used is head.
  447. */
  448. struct SharedSecret *ss_head;
  449. /**
  450. * Shared secrets we used with @e target, last used is tail.
  451. */
  452. struct SharedSecret *ss_tail;
  453. /**
  454. * Address of the other peer.
  455. */
  456. struct sockaddr *address;
  457. /**
  458. * Length of the address.
  459. */
  460. socklen_t address_len;
  461. /**
  462. * Timeout for this sender.
  463. */
  464. struct GNUNET_TIME_Absolute timeout;
  465. /**
  466. * Length of the DLL at @a ss_head.
  467. */
  468. unsigned int num_secrets;
  469. /**
  470. * Number of BOX keys from ACKs we have currently
  471. * available for this sender.
  472. */
  473. unsigned int acks_available;
  474. /**
  475. * Which network type does this queue use?
  476. */
  477. enum GNUNET_NetworkType nt;
  478. /**
  479. * sender_destroy already called on sender.
  480. */
  481. int sender_destroy_called;
  482. };
  483. /**
  484. * Information we track per receiving address we have recently been
  485. * in contact with (encryption to receiver).
  486. */
  487. struct ReceiverAddress
  488. {
  489. /**
  490. * Shared secret we use with @e target for rekeying.
  491. */
  492. struct SharedSecret *ss_rekey;
  493. /**
  494. * Acks available when we started rekeying.
  495. */
  496. unsigned int rekey_acks_available;
  497. /**
  498. * Send bytes for this receiver address.
  499. */
  500. uint64_t rekey_send_bytes;
  501. /**
  502. * Timeout for this receiver address.
  503. */
  504. struct GNUNET_TIME_Absolute rekey_timeout;
  505. /**
  506. * Flag indicating sender is initiated rekeying for this receiver.
  507. */
  508. int rekeying;
  509. /**
  510. * Numer of kce we retain for sending the rekeying shared secret.
  511. */
  512. int number_rekeying_kce;
  513. /**
  514. * To whom are we talking to.
  515. */
  516. struct GNUNET_PeerIdentity target;
  517. /**
  518. * Shared secrets we received from @e target, first used is head.
  519. */
  520. struct SharedSecret *ss_head;
  521. /**
  522. * Shared secrets we received with @e target, last used is tail.
  523. */
  524. struct SharedSecret *ss_tail;
  525. /**
  526. * Address of the receiver in the human-readable format
  527. * with the #COMMUNICATOR_ADDRESS_PREFIX.
  528. */
  529. char *foreign_addr;
  530. /**
  531. * Address of the other peer.
  532. */
  533. struct sockaddr *address;
  534. /**
  535. * Length of the address.
  536. */
  537. socklen_t address_len;
  538. /**
  539. * Entry in sender expiration heap.
  540. */
  541. struct GNUNET_CONTAINER_HeapNode *hn;
  542. /**
  543. * KX message queue we are providing for the #ch.
  544. */
  545. struct GNUNET_MQ_Handle *kx_mq;
  546. /**
  547. * Default message queue we are providing for the #ch.
  548. */
  549. struct GNUNET_MQ_Handle *d_mq;
  550. /**
  551. * handle for KX queue with the #ch.
  552. */
  553. struct GNUNET_TRANSPORT_QueueHandle *kx_qh;
  554. /**
  555. * handle for default queue with the #ch.
  556. */
  557. struct GNUNET_TRANSPORT_QueueHandle *d_qh;
  558. /**
  559. * Timeout for this receiver address.
  560. */
  561. struct GNUNET_TIME_Absolute timeout;
  562. /**
  563. * MTU we allowed transport for this receiver's KX queue.
  564. */
  565. size_t kx_mtu;
  566. /**
  567. * MTU we allowed transport for this receiver's default queue.
  568. */
  569. size_t d_mtu;
  570. /**
  571. * Length of the DLL at @a ss_head.
  572. */
  573. unsigned int num_secrets;
  574. /**
  575. * Number of BOX keys from ACKs we have currently
  576. * available for this receiver.
  577. */
  578. unsigned int acks_available;
  579. /**
  580. * Which network type does this queue use?
  581. */
  582. enum GNUNET_NetworkType nt;
  583. /**
  584. * receiver_destroy already called on receiver.
  585. */
  586. int receiver_destroy_called;
  587. };
  588. /**
  589. * Interface we broadcast our presence on.
  590. */
  591. struct BroadcastInterface
  592. {
  593. /**
  594. * Kept in a DLL.
  595. */
  596. struct BroadcastInterface *next;
  597. /**
  598. * Kept in a DLL.
  599. */
  600. struct BroadcastInterface *prev;
  601. /**
  602. * Task for this broadcast interface.
  603. */
  604. struct GNUNET_SCHEDULER_Task *broadcast_task;
  605. /**
  606. * Sender's address of the interface.
  607. */
  608. struct sockaddr *sa;
  609. /**
  610. * Broadcast address to use on the interface.
  611. */
  612. struct sockaddr *ba;
  613. /**
  614. * Message we broadcast on this interface.
  615. */
  616. struct UDPBroadcast bcm;
  617. /**
  618. * If this is an IPv6 interface, this is the request
  619. * we use to join/leave the group.
  620. */
  621. struct ipv6_mreq mcreq;
  622. /**
  623. * Number of bytes in @e sa.
  624. */
  625. socklen_t salen;
  626. /**
  627. * Was this interface found in the last #iface_proc() scan?
  628. */
  629. int found;
  630. };
  631. /**
  632. * The rekey interval
  633. */
  634. static struct GNUNET_TIME_Relative rekey_interval;
  635. /**
  636. * How often we do rekey based on number of bytes transmitted
  637. */
  638. static unsigned long long rekey_max_bytes;
  639. /**
  640. * Shared secret we finished the last kce working queue for.
  641. */
  642. struct SharedSecret *ss_finished;
  643. /**
  644. * Cache of pre-generated key IDs.
  645. */
  646. static struct GNUNET_CONTAINER_MultiShortmap *key_cache;
  647. /**
  648. * ID of read task
  649. */
  650. static struct GNUNET_SCHEDULER_Task *read_task;
  651. /**
  652. * ID of timeout task
  653. */
  654. static struct GNUNET_SCHEDULER_Task *timeout_task;
  655. /**
  656. * ID of kce working queue task
  657. */
  658. static struct GNUNET_SCHEDULER_Task *kce_task;
  659. /**
  660. * ID of kce rekey working queue task
  661. */
  662. static struct GNUNET_SCHEDULER_Task *kce_task_rekey;
  663. /**
  664. * Is the kce_task finished?
  665. */
  666. static int kce_task_finished = GNUNET_NO;
  667. /**
  668. * ID of master broadcast task
  669. */
  670. static struct GNUNET_SCHEDULER_Task *broadcast_task;
  671. /**
  672. * For logging statistics.
  673. */
  674. static struct GNUNET_STATISTICS_Handle *stats;
  675. /**
  676. * Our environment.
  677. */
  678. static struct GNUNET_TRANSPORT_CommunicatorHandle *ch;
  679. /**
  680. * Receivers (map from peer identity to `struct ReceiverAddress`)
  681. */
  682. static struct GNUNET_CONTAINER_MultiPeerMap *receivers;
  683. /**
  684. * Senders (map from peer identity to `struct SenderAddress`)
  685. */
  686. static struct GNUNET_CONTAINER_MultiPeerMap *senders;
  687. /**
  688. * Expiration heap for senders (contains `struct SenderAddress`)
  689. */
  690. static struct GNUNET_CONTAINER_Heap *senders_heap;
  691. /**
  692. * Expiration heap for receivers (contains `struct ReceiverAddress`)
  693. */
  694. static struct GNUNET_CONTAINER_Heap *receivers_heap;
  695. /**
  696. * Broadcast interface tasks. Kept in a DLL.
  697. */
  698. static struct BroadcastInterface *bi_head;
  699. /**
  700. * Broadcast interface tasks. Kept in a DLL.
  701. */
  702. static struct BroadcastInterface *bi_tail;
  703. /**
  704. * Our socket.
  705. */
  706. static struct GNUNET_NETWORK_Handle *udp_sock;
  707. /**
  708. * #GNUNET_YES if #udp_sock supports IPv6.
  709. */
  710. static int have_v6_socket;
  711. /**
  712. * Our public key.
  713. */
  714. static struct GNUNET_PeerIdentity my_identity;
  715. /**
  716. * Our private key.
  717. */
  718. static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
  719. /**
  720. * Our configuration.
  721. */
  722. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  723. /**
  724. * Our handle to report addresses for validation to TRANSPORT.
  725. */
  726. static struct GNUNET_TRANSPORT_ApplicationHandle *ah;
  727. /**
  728. * Network scanner to determine network types.
  729. */
  730. static struct GNUNET_NT_InterfaceScanner *is;
  731. /**
  732. * Connection to NAT service.
  733. */
  734. static struct GNUNET_NAT_Handle *nat;
  735. /**
  736. * Port number to which we are actually bound.
  737. */
  738. static uint16_t my_port;
  739. /**
  740. * An interface went away, stop broadcasting on it.
  741. *
  742. * @param bi entity to close down
  743. */
  744. static void
  745. bi_destroy (struct BroadcastInterface *bi)
  746. {
  747. if (AF_INET6 == bi->sa->sa_family)
  748. {
  749. /* Leave the multicast group */
  750. if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt (udp_sock,
  751. IPPROTO_IPV6,
  752. IPV6_LEAVE_GROUP,
  753. &bi->mcreq,
  754. sizeof(bi->mcreq)))
  755. {
  756. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
  757. }
  758. }
  759. GNUNET_CONTAINER_DLL_remove (bi_head, bi_tail, bi);
  760. GNUNET_SCHEDULER_cancel (bi->broadcast_task);
  761. GNUNET_free (bi->sa);
  762. GNUNET_free (bi->ba);
  763. GNUNET_free (bi);
  764. }
  765. /**
  766. * Destroys a receiving state due to timeout or shutdown.
  767. *
  768. * @param receiver entity to close down
  769. */
  770. static void
  771. receiver_destroy (struct ReceiverAddress *receiver)
  772. {
  773. receiver->receiver_destroy_called = GNUNET_YES;
  774. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  775. "Disconnecting receiver for peer `%s'\n",
  776. GNUNET_i2s (&receiver->target));
  777. /*if (NULL != (mq = receiver->kx_mq))
  778. {
  779. receiver->kx_mq = NULL;
  780. GNUNET_MQ_destroy (mq);
  781. }*/
  782. if (NULL != receiver->kx_qh)
  783. {
  784. GNUNET_TRANSPORT_communicator_mq_del (receiver->kx_qh);
  785. receiver->kx_qh = NULL;
  786. receiver->kx_mq = NULL;
  787. }
  788. /*if (NULL != (mq = receiver->d_mq))
  789. {
  790. receiver->d_mq = NULL;
  791. GNUNET_MQ_destroy (mq);
  792. }*/
  793. if (NULL != receiver->d_qh)
  794. {
  795. GNUNET_TRANSPORT_communicator_mq_del (receiver->d_qh);
  796. receiver->d_qh = NULL;
  797. }
  798. GNUNET_assert (GNUNET_YES ==
  799. GNUNET_CONTAINER_multipeermap_remove (receivers,
  800. &receiver->target,
  801. receiver));
  802. GNUNET_assert (receiver == GNUNET_CONTAINER_heap_remove_node (receiver->hn));
  803. GNUNET_STATISTICS_set (stats,
  804. "# receivers active",
  805. GNUNET_CONTAINER_multipeermap_size (receivers),
  806. GNUNET_NO);
  807. GNUNET_free (receiver->address);
  808. GNUNET_free (receiver->foreign_addr);
  809. GNUNET_free (receiver);
  810. }
  811. /**
  812. * Free memory used by key cache entry.
  813. *
  814. * @param kce the key cache entry
  815. */
  816. static void
  817. kce_destroy (struct KeyCacheEntry *kce)
  818. {
  819. struct SharedSecret *ss = kce->ss;
  820. ss->active_kce_count--;
  821. ss->sender->acks_available--;
  822. GNUNET_CONTAINER_DLL_remove (ss->kce_head, ss->kce_tail, kce);
  823. GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multishortmap_remove (key_cache,
  824. &kce->kid,
  825. kce));
  826. GNUNET_free (kce);
  827. }
  828. /**
  829. * Compute @a kid.
  830. *
  831. * @param msec master secret for HMAC calculation
  832. * @param serial number for the @a smac calculation
  833. * @param kid[out] where to write the key ID
  834. */
  835. static void
  836. get_kid (const struct GNUNET_HashCode *msec,
  837. uint32_t serial,
  838. struct GNUNET_ShortHashCode *kid)
  839. {
  840. uint32_t sid = htonl (serial);
  841. GNUNET_CRYPTO_hkdf (kid,
  842. sizeof(*kid),
  843. GCRY_MD_SHA512,
  844. GCRY_MD_SHA256,
  845. &sid,
  846. sizeof(sid),
  847. msec,
  848. sizeof(*msec),
  849. "UDP-KID",
  850. strlen ("UDP-KID"),
  851. NULL,
  852. 0);
  853. }
  854. /**
  855. * Setup key cache entry for sequence number @a seq and shared secret @a ss.
  856. *
  857. * @param ss shared secret
  858. * @param seq sequence number for the key cache entry
  859. */
  860. static void
  861. kce_generate (struct SharedSecret *ss, uint32_t seq)
  862. {
  863. struct KeyCacheEntry *kce;
  864. GNUNET_assert (0 < seq);
  865. kce = GNUNET_new (struct KeyCacheEntry);
  866. kce->ss = ss;
  867. kce->sequence_number = seq;
  868. get_kid (&ss->master, seq, &kce->kid);
  869. GNUNET_CONTAINER_DLL_insert (ss->kce_head, ss->kce_tail, kce);
  870. ss->active_kce_count++;
  871. ss->sender->acks_available++;
  872. (void) GNUNET_CONTAINER_multishortmap_put (
  873. key_cache,
  874. &kce->kid,
  875. kce,
  876. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  877. GNUNET_STATISTICS_set (stats,
  878. "# KIDs active",
  879. GNUNET_CONTAINER_multishortmap_size (key_cache),
  880. GNUNET_NO);
  881. }
  882. /**
  883. * Destroy @a ss and associated key cache entries.
  884. *
  885. * @param ss shared secret to destroy
  886. * @param withoutKce If GNUNET_YES shared secrets with kce will not be destroyed.
  887. */
  888. static int
  889. secret_destroy (struct SharedSecret *ss, int withoutKce)
  890. {
  891. struct SenderAddress *sender;
  892. struct ReceiverAddress *receiver;
  893. struct KeyCacheEntry *kce;
  894. if (withoutKce && (ss->sequence_allowed > 0))
  895. return GNUNET_NO;
  896. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  897. "secret %s destroy %u %u\n",
  898. GNUNET_h2s (&ss->master),
  899. withoutKce,
  900. ss->sequence_allowed);
  901. if (NULL != (sender = ss->sender))
  902. {
  903. GNUNET_CONTAINER_DLL_remove (sender->ss_head, sender->ss_tail, ss);
  904. sender->num_secrets--;
  905. }
  906. if (NULL != (receiver = ss->receiver))
  907. {
  908. GNUNET_CONTAINER_DLL_remove (receiver->ss_head, receiver->ss_tail, ss);
  909. receiver->num_secrets--;
  910. // Uncomment this for alternativ 1 of backchannel functionality
  911. receiver->acks_available -= (ss->sequence_allowed - ss->sequence_used);
  912. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  913. "%u receiver->acks_available 3\n",
  914. receiver->acks_available);
  915. // Until here for alternativ 1
  916. }
  917. while (NULL != (kce = ss->kce_head))
  918. kce_destroy (kce);
  919. GNUNET_STATISTICS_update (stats, "# Secrets active", -1, GNUNET_NO);
  920. GNUNET_STATISTICS_set (stats,
  921. "# KIDs active",
  922. GNUNET_CONTAINER_multishortmap_size (key_cache),
  923. GNUNET_NO);
  924. GNUNET_free (ss);
  925. return GNUNET_YES;
  926. }
  927. /**
  928. * Functions with this signature are called whenever we need
  929. * to close a sender's state due to timeout.
  930. *
  931. * @param sender entity to close down
  932. */
  933. static void
  934. sender_destroy (struct SenderAddress *sender)
  935. {
  936. sender->sender_destroy_called = GNUNET_YES;
  937. GNUNET_assert (
  938. GNUNET_YES ==
  939. GNUNET_CONTAINER_multipeermap_remove (senders, &sender->target, sender));
  940. GNUNET_assert (sender == GNUNET_CONTAINER_heap_remove_node (sender->hn));
  941. GNUNET_STATISTICS_set (stats,
  942. "# senders active",
  943. GNUNET_CONTAINER_multipeermap_size (senders),
  944. GNUNET_NO);
  945. GNUNET_free (sender->address);
  946. GNUNET_free (sender);
  947. }
  948. /**
  949. * Compute @a key and @a iv.
  950. *
  951. * @param msec master secret for calculation
  952. * @param serial number for the @a smac calculation
  953. * @param key[out] where to write the decrption key
  954. * @param iv[out] where to write the IV
  955. */
  956. static void
  957. get_iv_key (const struct GNUNET_HashCode *msec,
  958. uint32_t serial,
  959. char key[AES_KEY_SIZE],
  960. char iv[AES_IV_SIZE])
  961. {
  962. uint32_t sid = htonl (serial);
  963. char res[AES_KEY_SIZE + AES_IV_SIZE];
  964. GNUNET_CRYPTO_hkdf (res,
  965. sizeof(res),
  966. GCRY_MD_SHA512,
  967. GCRY_MD_SHA256,
  968. &sid,
  969. sizeof(sid),
  970. msec,
  971. sizeof(*msec),
  972. "UDP-IV-KEY",
  973. strlen ("UDP-IV-KEY"),
  974. NULL,
  975. 0);
  976. memcpy (key, res, AES_KEY_SIZE);
  977. memcpy (iv, &res[AES_KEY_SIZE], AES_IV_SIZE);
  978. }
  979. /**
  980. * Increment sender timeout due to activity.
  981. *
  982. * @param sender address for which the timeout should be rescheduled
  983. */
  984. static void
  985. reschedule_sender_timeout (struct SenderAddress *sender)
  986. {
  987. sender->timeout =
  988. GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
  989. GNUNET_CONTAINER_heap_update_cost (sender->hn, sender->timeout.abs_value_us);
  990. }
  991. /**
  992. * Increment receiver timeout due to activity.
  993. *
  994. * @param receiver address for which the timeout should be rescheduled
  995. */
  996. static void
  997. reschedule_receiver_timeout (struct ReceiverAddress *receiver)
  998. {
  999. receiver->timeout =
  1000. GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
  1001. GNUNET_CONTAINER_heap_update_cost (receiver->hn,
  1002. receiver->timeout.abs_value_us);
  1003. }
  1004. /**
  1005. * Task run to check #receiver_heap and #sender_heap for timeouts.
  1006. *
  1007. * @param cls unused, NULL
  1008. */
  1009. static void
  1010. check_timeouts (void *cls)
  1011. {
  1012. struct GNUNET_TIME_Relative st;
  1013. struct GNUNET_TIME_Relative rt;
  1014. struct GNUNET_TIME_Relative delay;
  1015. struct ReceiverAddress *receiver;
  1016. struct SenderAddress *sender;
  1017. (void) cls;
  1018. timeout_task = NULL;
  1019. rt = GNUNET_TIME_UNIT_FOREVER_REL;
  1020. while (NULL != (receiver = GNUNET_CONTAINER_heap_peek (receivers_heap)))
  1021. {
  1022. /* if (GNUNET_YES != receiver->receiver_destroy_called) */
  1023. /* { */
  1024. rt = GNUNET_TIME_absolute_get_remaining (receiver->timeout);
  1025. if (0 != rt.rel_value_us)
  1026. break;
  1027. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1028. "Receiver timed out\n");
  1029. receiver_destroy (receiver);
  1030. // }
  1031. }
  1032. st = GNUNET_TIME_UNIT_FOREVER_REL;
  1033. while (NULL != (sender = GNUNET_CONTAINER_heap_peek (senders_heap)))
  1034. {
  1035. if (GNUNET_YES != sender->sender_destroy_called)
  1036. {
  1037. st = GNUNET_TIME_absolute_get_remaining (sender->timeout);
  1038. if (0 != st.rel_value_us)
  1039. break;
  1040. sender_destroy (sender);
  1041. }
  1042. }
  1043. delay = GNUNET_TIME_relative_min (rt, st);
  1044. if (delay.rel_value_us < GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
  1045. timeout_task = GNUNET_SCHEDULER_add_delayed (delay, &check_timeouts, NULL);
  1046. }
  1047. /**
  1048. * Calcualte cmac from master in @a ss.
  1049. *
  1050. * @param ss[in,out] data structure to complete
  1051. */
  1052. static void
  1053. calculate_cmac (struct SharedSecret *ss)
  1054. {
  1055. GNUNET_CRYPTO_hkdf (&ss->cmac,
  1056. sizeof(ss->cmac),
  1057. GCRY_MD_SHA512,
  1058. GCRY_MD_SHA256,
  1059. "CMAC",
  1060. strlen ("CMAC"),
  1061. &ss->master,
  1062. sizeof(ss->master),
  1063. "UDP-CMAC",
  1064. strlen ("UDP-CMAC"),
  1065. NULL,
  1066. 0);
  1067. }
  1068. /**
  1069. * We received @a plaintext_len bytes of @a plaintext from a @a sender.
  1070. * Pass it on to CORE.
  1071. *
  1072. * @param queue the queue that received the plaintext
  1073. * @param plaintext the plaintext that was received
  1074. * @param plaintext_len number of bytes of plaintext received
  1075. */
  1076. static void
  1077. pass_plaintext_to_core (struct SenderAddress *sender,
  1078. const void *plaintext,
  1079. size_t plaintext_len)
  1080. {
  1081. const struct GNUNET_MessageHeader *hdr = plaintext;
  1082. const char *pos = plaintext;
  1083. while (ntohs (hdr->size) < plaintext_len)
  1084. {
  1085. GNUNET_STATISTICS_update (stats,
  1086. "# bytes given to core",
  1087. ntohs (hdr->size),
  1088. GNUNET_NO);
  1089. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1090. "Giving %u bytes to TNG\n", ntohs (hdr->size));
  1091. GNUNET_assert (GNUNET_SYSERR !=
  1092. GNUNET_TRANSPORT_communicator_receive (ch,
  1093. &sender->target,
  1094. hdr,
  1095. ADDRESS_VALIDITY_PERIOD,
  1096. NULL /* no flow control possible */
  1097. ,
  1098. NULL));
  1099. /* move on to next message, if any */
  1100. plaintext_len -= ntohs (hdr->size);
  1101. if (plaintext_len < sizeof(*hdr))
  1102. break;
  1103. pos += ntohs (hdr->size);
  1104. hdr = (const struct GNUNET_MessageHeader *) pos;
  1105. // TODO for now..., we do not actually sen >1msg or have a way of telling
  1106. // if we are done
  1107. break;
  1108. }
  1109. GNUNET_STATISTICS_update (stats,
  1110. "# bytes padding discarded",
  1111. plaintext_len,
  1112. GNUNET_NO);
  1113. }
  1114. /**
  1115. * Setup @a cipher based on shared secret @a msec and
  1116. * serial number @a serial.
  1117. *
  1118. * @param msec master shared secret
  1119. * @param serial serial number of cipher to set up
  1120. * @param cipher[out] cipher to initialize
  1121. */
  1122. static void
  1123. setup_cipher (const struct GNUNET_HashCode *msec,
  1124. uint32_t serial,
  1125. gcry_cipher_hd_t *cipher)
  1126. {
  1127. char key[AES_KEY_SIZE];
  1128. char iv[AES_IV_SIZE];
  1129. int rc;
  1130. GNUNET_assert (0 ==
  1131. gcry_cipher_open (cipher,
  1132. GCRY_CIPHER_AES256 /* low level: go for speed */,
  1133. GCRY_CIPHER_MODE_GCM,
  1134. 0 /* flags */));
  1135. get_iv_key (msec, serial, key, iv);
  1136. rc = gcry_cipher_setkey (*cipher, key, sizeof(key));
  1137. GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
  1138. rc = gcry_cipher_setiv (*cipher, iv, sizeof(iv));
  1139. GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
  1140. }
  1141. /**
  1142. * Try to decrypt @a buf using shared secret @a ss and key/iv
  1143. * derived using @a serial.
  1144. *
  1145. * @param ss shared secret
  1146. * @param tag GCM authentication tag
  1147. * @param serial serial number to use
  1148. * @param in_buf input buffer to decrypt
  1149. * @param in_buf_size number of bytes in @a in_buf and available in @a out_buf
  1150. * @param out_buf where to write the result
  1151. * @return #GNUNET_OK on success
  1152. */
  1153. static int
  1154. try_decrypt (const struct SharedSecret *ss,
  1155. const char tag[GCM_TAG_SIZE],
  1156. uint32_t serial,
  1157. const char *in_buf,
  1158. size_t in_buf_size,
  1159. char *out_buf)
  1160. {
  1161. gcry_cipher_hd_t cipher;
  1162. setup_cipher (&ss->master, serial, &cipher);
  1163. GNUNET_assert (
  1164. 0 ==
  1165. gcry_cipher_decrypt (cipher, out_buf, in_buf_size, in_buf, in_buf_size));
  1166. if (0 != gcry_cipher_checktag (cipher, tag, GCM_TAG_SIZE))
  1167. {
  1168. gcry_cipher_close (cipher);
  1169. GNUNET_STATISTICS_update (stats,
  1170. "# AEAD authentication failures",
  1171. 1,
  1172. GNUNET_NO);
  1173. return GNUNET_SYSERR;
  1174. }
  1175. gcry_cipher_close (cipher);
  1176. return GNUNET_OK;
  1177. }
  1178. /**
  1179. * Setup shared secret for decryption.
  1180. *
  1181. * @param ephemeral ephemeral key we received from the other peer
  1182. * @return new shared secret
  1183. */
  1184. static struct SharedSecret *
  1185. setup_shared_secret_dec (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral)
  1186. {
  1187. struct SharedSecret *ss;
  1188. ss = GNUNET_new (struct SharedSecret);
  1189. GNUNET_CRYPTO_eddsa_ecdh (my_private_key, ephemeral, &ss->master);
  1190. return ss;
  1191. }
  1192. /**
  1193. * Setup shared secret for encryption.
  1194. *
  1195. * @param ephemeral ephemeral key we are sending to the other peer
  1196. * @param receiver[in,out] queue to initialize encryption key for
  1197. * @return new shared secret
  1198. */
  1199. static struct SharedSecret *
  1200. setup_shared_secret_enc (const struct GNUNET_CRYPTO_EcdhePrivateKey *ephemeral,
  1201. struct ReceiverAddress *receiver, int add_to_receiver)
  1202. {
  1203. struct SharedSecret *ss;
  1204. ss = GNUNET_new (struct SharedSecret);
  1205. GNUNET_CRYPTO_ecdh_eddsa (ephemeral,
  1206. &receiver->target.public_key,
  1207. &ss->master);
  1208. calculate_cmac (ss);
  1209. ss->receiver = receiver;
  1210. GNUNET_CONTAINER_DLL_insert (receiver->ss_head, receiver->ss_tail, ss);
  1211. receiver->num_secrets++;
  1212. GNUNET_STATISTICS_update (stats, "# Secrets active", 1, GNUNET_NO);
  1213. return ss;
  1214. }
  1215. /**
  1216. * Setup the MQ for the @a receiver. If a queue exists,
  1217. * the existing one is destroyed. Then the MTU is
  1218. * recalculated and a fresh queue is initialized.
  1219. *
  1220. * @param receiver receiver to setup MQ for
  1221. */
  1222. static void
  1223. setup_receiver_mq (struct ReceiverAddress *receiver);
  1224. /**
  1225. * Destroying all secrets. Depending on parameter we keep those secrets having a kce.
  1226. *
  1227. * @param ss The secret we will not destroy.
  1228. * @param withoutKce If GNUNET_YES shared secrets with kce will not be destroyed.
  1229. */
  1230. static void
  1231. destroy_all_secrets (struct SharedSecret *ss, int withoutKce)
  1232. {
  1233. struct SenderAddress *sender;
  1234. struct ReceiverAddress *receiver;
  1235. struct SharedSecret *ss_to_destroy;
  1236. struct SharedSecret *ss_start;
  1237. struct SharedSecret *pos;
  1238. int at_least_one_destroyed = GNUNET_NO;
  1239. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1240. "Starting destroy all withoutKce: %u.\n",
  1241. withoutKce);
  1242. if (NULL != (sender = ss->sender))
  1243. {
  1244. ss_start = sender->ss_head;
  1245. }
  1246. else if (NULL != (receiver = ss->receiver))
  1247. {
  1248. ss_start = receiver->ss_head;
  1249. }
  1250. else
  1251. {
  1252. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  1253. "Shared secret has no sender or receiver!\n");
  1254. return;
  1255. }
  1256. pos = ss_start;
  1257. while (NULL != pos)
  1258. {
  1259. ss_to_destroy = pos;
  1260. pos = pos->next;
  1261. if (ss != ss_to_destroy)
  1262. at_least_one_destroyed = secret_destroy (ss_to_destroy, withoutKce);
  1263. }
  1264. if ((ss != ss_start) && ! at_least_one_destroyed)
  1265. {
  1266. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1267. "Really destroying all.\n");
  1268. destroy_all_secrets (ss_start, GNUNET_NO);
  1269. }
  1270. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1271. "Finished destroy all.\n");
  1272. }
  1273. static void
  1274. add_acks (struct SharedSecret *ss, int acks_to_add)
  1275. {
  1276. struct ReceiverAddress *receiver = ss->receiver;
  1277. if (NULL == ss)
  1278. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1279. "secret NULL!\n");
  1280. if (NULL == receiver)
  1281. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1282. "Receiver NULL!\n");
  1283. if (NULL == receiver->d_qh)
  1284. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1285. "Queue NULL!\n");
  1286. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1287. "Tell transport we have %u more acks!\n",
  1288. acks_to_add);
  1289. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1290. "%u kce for rekeying.\n",
  1291. receiver->number_rekeying_kce);
  1292. GNUNET_TRANSPORT_communicator_mq_update (ch,
  1293. receiver->d_qh,
  1294. acks_to_add,
  1295. 1);
  1296. // Until here for alternativ 1
  1297. /* move ss to head to avoid discarding it anytime soon! */
  1298. GNUNET_CONTAINER_DLL_remove (receiver->ss_head, receiver->ss_tail, ss);
  1299. GNUNET_CONTAINER_DLL_insert (receiver->ss_head, receiver->ss_tail, ss);
  1300. destroy_all_secrets (ss, GNUNET_YES);
  1301. }
  1302. static uint32_t
  1303. reset_rekey_kces (struct ReceiverAddress *receiver,
  1304. uint32_t acks_to_add)
  1305. {
  1306. int needed_for_rekeying;
  1307. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1308. "%u kce for rekeying and %u acks_to_add\n",
  1309. receiver->number_rekeying_kce,
  1310. acks_to_add);
  1311. needed_for_rekeying = (3 - receiver->number_rekeying_kce);
  1312. if (acks_to_add <= needed_for_rekeying)
  1313. {
  1314. receiver->number_rekeying_kce += acks_to_add;
  1315. acks_to_add = 0;
  1316. }
  1317. else
  1318. {
  1319. acks_to_add -= (3 - receiver->number_rekeying_kce);
  1320. receiver->number_rekeying_kce = 3;
  1321. }
  1322. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1323. "%u kce for rekeying and %u acks_to_add\n",
  1324. receiver->number_rekeying_kce,
  1325. acks_to_add);
  1326. return acks_to_add;
  1327. }
  1328. static void
  1329. add_acks_rekey (struct ReceiverAddress *receiver)
  1330. {
  1331. uint32_t acks_to_add = receiver->ss_rekey->sequence_allowed;
  1332. if (receiver->number_rekeying_kce < 3)
  1333. acks_to_add = reset_rekey_kces (receiver, acks_to_add);
  1334. receiver->acks_available = receiver->ss_rekey->sequence_allowed;
  1335. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1336. "%u receiver->acks_available 4\n",
  1337. receiver->acks_available);
  1338. /* add_acks (receiver->ss_rekey, acks_to_add - 3); */
  1339. if (0 != acks_to_add)
  1340. {
  1341. add_acks (receiver->ss_rekey, acks_to_add);
  1342. }
  1343. receiver->ss_rekey = NULL;
  1344. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1345. "# rekeying successful\n");
  1346. GNUNET_STATISTICS_update (stats,
  1347. "# rekeying successful",
  1348. 1,
  1349. GNUNET_NO);
  1350. }
  1351. /**
  1352. * We received an ACK for @a pid. Check if it is for
  1353. * the receiver in @a value and if so, handle it and
  1354. * return #GNUNET_NO. Otherwise, return #GNUNET_YES.
  1355. *
  1356. * @param cls a `const struct UDPAck`
  1357. * @param pid peer the ACK is from
  1358. * @param value a `struct ReceiverAddress`
  1359. * @return #GNUNET_YES to continue to iterate
  1360. */
  1361. static int
  1362. handle_ack (void *cls, const struct GNUNET_PeerIdentity *pid, void *value)
  1363. {
  1364. const struct UDPAck *ack = cls;
  1365. struct ReceiverAddress *receiver = value;
  1366. uint32_t acks_to_add;
  1367. uint32_t allowed;
  1368. // int needed_for_rekeying;
  1369. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1370. "in handle ack with cmac %s\n",
  1371. GNUNET_h2s (&ack->cmac));
  1372. if (NULL != receiver->ss_rekey)
  1373. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1374. "We have rekey secret with cmac %s \n",
  1375. GNUNET_h2s (&receiver->ss_rekey->cmac));
  1376. if ((NULL != receiver->ss_rekey) && (0 == memcmp (&ack->cmac,
  1377. &receiver->ss_rekey->cmac,
  1378. sizeof(struct
  1379. GNUNET_HashCode))) )
  1380. {
  1381. allowed = ntohl (ack->sequence_max);
  1382. if (allowed > receiver->ss_rekey->sequence_allowed)
  1383. {
  1384. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1385. "%u > %u (%u %u) for rekey secrect %s\n", allowed,
  1386. receiver->ss_rekey->sequence_allowed,
  1387. receiver->acks_available,
  1388. ack->acks_available,
  1389. GNUNET_h2s (&receiver->ss_rekey->master));
  1390. receiver->ss_rekey->sequence_allowed = allowed;
  1391. if (GNUNET_NO == receiver->rekeying)
  1392. add_acks_rekey (receiver);
  1393. return GNUNET_NO;
  1394. }
  1395. }
  1396. (void) pid;
  1397. for (struct SharedSecret *ss = receiver->ss_head; NULL != ss; ss = ss->next)
  1398. {
  1399. if (0 == memcmp (&ack->cmac, &ss->cmac, sizeof(struct GNUNET_HashCode)))
  1400. {
  1401. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1402. "Found matching mac\n");
  1403. allowed = ntohl (ack->sequence_max);
  1404. if (allowed > ss->sequence_allowed)
  1405. {
  1406. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1407. "%u > %u (%u %u) for secrect %s\n", allowed,
  1408. ss->sequence_allowed,
  1409. receiver->acks_available,
  1410. ack->acks_available,
  1411. GNUNET_h2s (&ss->master));
  1412. // Uncomment this for alternativ 1 of backchannel functionality
  1413. acks_to_add = (allowed - ss->sequence_allowed);
  1414. if ((GNUNET_NO == receiver->rekeying) &&
  1415. (receiver->number_rekeying_kce < 3) )
  1416. acks_to_add = reset_rekey_kces (receiver, acks_to_add);
  1417. /* if ((GNUNET_NO == receiver->rekeying) && */
  1418. /* (receiver->number_rekeying_kce < */
  1419. /* 3) ) */
  1420. /* { */
  1421. /* needed_for_rekeying = (3 - receiver->number_rekeying_kce); */
  1422. /* if (acks_to_add <= needed_for_rekeying) */
  1423. /* { */
  1424. /* receiver->number_rekeying_kce += acks_to_add; */
  1425. /* acks_to_add = 0; */
  1426. /* } */
  1427. /* else */
  1428. /* { */
  1429. /* acks_to_add -= (3 - receiver->number_rekeying_kce); */
  1430. /* receiver->number_rekeying_kce = 3; */
  1431. /* } */
  1432. /* } */
  1433. /* GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, */
  1434. /* "%u kce for rekeying\n", */
  1435. /* receiver->number_rekeying_kce); */
  1436. if ((0 != acks_to_add) && (GNUNET_NO == receiver->rekeying))
  1437. {
  1438. receiver->acks_available += (allowed - ss->sequence_allowed);
  1439. ss->sequence_allowed = allowed;
  1440. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1441. "%u receiver->acks_available 5\n",
  1442. receiver->acks_available);
  1443. add_acks (ss, acks_to_add);
  1444. }
  1445. }
  1446. return GNUNET_NO;
  1447. }
  1448. }
  1449. return GNUNET_YES;
  1450. }
  1451. /**
  1452. * Test if we have received a valid message in plaintext.
  1453. * If so, handle it.
  1454. *
  1455. * @param sender peer to process inbound plaintext for
  1456. * @param buf buffer we received
  1457. * @param buf_size number of bytes in @a buf
  1458. */
  1459. static void
  1460. try_handle_plaintext (struct SenderAddress *sender,
  1461. const void *buf,
  1462. size_t buf_size)
  1463. {
  1464. const struct GNUNET_MessageHeader *hdr =
  1465. (const struct GNUNET_MessageHeader *) buf;
  1466. const struct UDPAck *ack = (const struct UDPAck *) buf;
  1467. uint16_t type;
  1468. if (sizeof(*hdr) > buf_size)
  1469. return; /* not even a header */
  1470. if (ntohs (hdr->size) > buf_size)
  1471. return; /* not even a header */
  1472. type = ntohs (hdr->type);
  1473. switch (type)
  1474. {
  1475. case GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK:
  1476. /* lookup master secret by 'cmac', then update sequence_max */
  1477. GNUNET_CONTAINER_multipeermap_get_multiple (receivers,
  1478. &sender->target,
  1479. &handle_ack,
  1480. (void *) ack);
  1481. /* There could be more messages after the ACK, handle those as well */
  1482. buf += ntohs (hdr->size);
  1483. buf_size -= ntohs (hdr->size);
  1484. pass_plaintext_to_core (sender, buf, buf_size);
  1485. break;
  1486. case GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_PAD:
  1487. /* skip padding */
  1488. break;
  1489. default:
  1490. pass_plaintext_to_core (sender, buf, buf_size);
  1491. }
  1492. }
  1493. static void
  1494. kce_generate_cb (void *cls)
  1495. {
  1496. struct SharedSecret *ss = cls;
  1497. kce_task = NULL;
  1498. if (((GNUNET_NO == ss->sender->rekeying) && (ss->sender->acks_available <
  1499. KCN_TARGET) ) ||
  1500. ((ss->sender->ss_rekey == ss) && (GNUNET_YES == ss->sender->rekeying) &&
  1501. (ss->sender->acks_available < 128)))
  1502. {
  1503. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1504. "Precomputing keys for master %s\n",
  1505. GNUNET_h2s (&(ss->master)));
  1506. for (int i = 0; i < GENERATE_AT_ONCE; i++)
  1507. kce_generate (ss, ++ss->sequence_allowed);
  1508. kce_task = GNUNET_SCHEDULER_add_delayed (WORKING_QUEUE_INTERVALL,
  1509. kce_generate_cb,
  1510. ss);
  1511. }
  1512. else
  1513. {
  1514. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1515. "We have enough keys.\n");
  1516. ss_finished = ss;
  1517. kce_task_finished = GNUNET_YES;
  1518. }
  1519. }
  1520. static void
  1521. kce_generate_rekey_cb (void *cls)
  1522. {
  1523. struct SharedSecret *ss = cls;
  1524. kce_task_rekey = NULL;
  1525. if (NULL == kce_task)
  1526. {
  1527. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1528. "Precomputing keys for rekey master %s\n",
  1529. GNUNET_h2s (&(ss->master)));
  1530. for (int i = 0; i < GENERATE_AT_ONCE; i++)
  1531. kce_generate (ss, ++ss->sequence_allowed);
  1532. kce_task = GNUNET_SCHEDULER_add_delayed (WORKING_QUEUE_INTERVALL,
  1533. kce_generate_cb,
  1534. ss);
  1535. kce_task_rekey = NULL;
  1536. }
  1537. else
  1538. {
  1539. kce_task_rekey = GNUNET_SCHEDULER_add_delayed (WORKING_QUEUE_INTERVALL,
  1540. kce_generate_rekey_cb,
  1541. ss);
  1542. }
  1543. }
  1544. /**
  1545. * We established a shared secret with a sender. We should try to send
  1546. * the sender an `struct UDPAck` at the next opportunity to allow the
  1547. * sender to use @a ss longer (assuming we did not yet already
  1548. * recently).
  1549. *
  1550. * @param ss shared secret to generate ACKs for
  1551. * @param intial The SharedSecret came with initial KX.
  1552. */
  1553. static void
  1554. consider_ss_ack (struct SharedSecret *ss, int initial)
  1555. {
  1556. GNUNET_assert (NULL != ss->sender);
  1557. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1558. "Considering SS UDPAck %s\n",
  1559. GNUNET_i2s_full (&ss->sender->target));
  1560. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1561. "We have %u acks available.\n",
  1562. ss->sender->acks_available);
  1563. /* drop ancient KeyCacheEntries */
  1564. while ((NULL != ss->kce_head) &&
  1565. (MAX_SQN_DELTA <
  1566. ss->kce_head->sequence_number - ss->kce_tail->sequence_number))
  1567. kce_destroy (ss->kce_tail);
  1568. if (GNUNET_NO == initial)
  1569. kce_generate (ss, ++ss->sequence_allowed);
  1570. /*if (0 == ss->sender->acks_available)
  1571. {
  1572. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1573. "Generating keys\n");
  1574. while (ss->active_kce_count < KCN_TARGET)
  1575. kce_generate (ss, ++ss->sequence_allowed);
  1576. }*/
  1577. if (((NULL != kce_task) && kce_task_finished) || (GNUNET_NO == initial))
  1578. {
  1579. struct UDPAck ack;
  1580. struct SharedSecret *ss_tell;
  1581. if (GNUNET_NO != initial)
  1582. ss_tell = ss_finished;
  1583. else
  1584. ss_tell = ss;
  1585. ack.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK);
  1586. ack.header.size = htons (sizeof(ack));
  1587. ack.sequence_max = htonl (ss_tell->sequence_allowed);
  1588. ack.acks_available = ss->sender->acks_available;
  1589. ack.cmac = ss_tell->cmac;
  1590. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1591. "Notifying transport of UDPAck %s with intial %u and master %s\n",
  1592. GNUNET_i2s_full (&ss_tell->sender->target),
  1593. initial,
  1594. GNUNET_h2s (&(ss_tell->master)));
  1595. GNUNET_TRANSPORT_communicator_notify (ch,
  1596. &ss_tell->sender->target,
  1597. COMMUNICATOR_ADDRESS_PREFIX,
  1598. &ack.header);
  1599. if (GNUNET_NO != initial)
  1600. {
  1601. destroy_all_secrets (ss, GNUNET_YES);
  1602. kce_task = NULL;
  1603. kce_task_finished = GNUNET_NO;
  1604. }
  1605. }
  1606. else if ((NULL == kce_task) && ((KCN_THRESHOLD >
  1607. ss->sender->acks_available) ||
  1608. (GNUNET_YES == ss->sender->rekeying) ||
  1609. (ss->sender->num_secrets > MAX_SECRETS) ))
  1610. {
  1611. // kce_generate (ss, ++ss->sequence_allowed);
  1612. // kce_generate (ss, ++ss->sequence_allowed);
  1613. kce_task = GNUNET_SCHEDULER_add_delayed (WORKING_QUEUE_INTERVALL,
  1614. kce_generate_cb,
  1615. ss);
  1616. }
  1617. else if ((NULL == kce_task_rekey) && (GNUNET_YES ==
  1618. ss->sender->rekeying) )
  1619. {
  1620. kce_task_rekey = GNUNET_SCHEDULER_add_delayed (WORKING_QUEUE_INTERVALL,
  1621. kce_generate_rekey_cb,
  1622. ss);
  1623. }
  1624. }
  1625. /**
  1626. * We received a @a box with matching @a kce. Decrypt and process it.
  1627. *
  1628. * @param box the data we received
  1629. * @param box_len number of bytes in @a box
  1630. * @param kce key index to decrypt @a box
  1631. */
  1632. static void
  1633. decrypt_box (const struct UDPBox *box,
  1634. size_t box_len,
  1635. struct KeyCacheEntry *kce)
  1636. {
  1637. struct SharedSecret *ss = kce->ss;
  1638. char out_buf[box_len - sizeof(*box)];
  1639. GNUNET_assert (NULL != ss->sender);
  1640. if (GNUNET_OK != try_decrypt (ss,
  1641. box->gcm_tag,
  1642. kce->sequence_number,
  1643. (const char *) &box[1],
  1644. sizeof(out_buf),
  1645. out_buf))
  1646. {
  1647. GNUNET_STATISTICS_update (stats,
  1648. "# Decryption failures with valid KCE",
  1649. 1,
  1650. GNUNET_NO);
  1651. kce_destroy (kce);
  1652. return;
  1653. }
  1654. kce_destroy (kce);
  1655. GNUNET_STATISTICS_update (stats,
  1656. "# bytes decrypted with BOX",
  1657. sizeof(out_buf),
  1658. GNUNET_NO);
  1659. GNUNET_STATISTICS_update (stats,
  1660. "# messages decrypted with BOX",
  1661. 1,
  1662. GNUNET_NO);
  1663. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1664. "decrypted UDPBox with kid %s\n",
  1665. GNUNET_sh2s (&box->kid));
  1666. try_handle_plaintext (ss->sender, out_buf, sizeof(out_buf));
  1667. if ((GNUNET_NO == box->rekeying) && (GNUNET_YES == ss->sender->rekeying))
  1668. {
  1669. ss->sender->rekeying = GNUNET_NO;
  1670. ss->sender->ss_rekey = NULL;
  1671. // destroy_all_secrets (ss, GNUNET_NO);
  1672. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1673. "Receiver stopped rekeying.\n");
  1674. }
  1675. else if (GNUNET_NO == box->rekeying)
  1676. consider_ss_ack (ss, GNUNET_NO);
  1677. else
  1678. {
  1679. ss->sender->rekeying = GNUNET_YES;
  1680. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1681. "Got Box: Receiver doing rekeying.\n");
  1682. }
  1683. }
  1684. /**
  1685. * We received a @a rekey with matching @a kce. Decrypt and process it.
  1686. *
  1687. * @param rekey the data we received
  1688. * @param rekey_len number of bytes in @a rekey
  1689. * @param kce key index to decrypt @a rekey
  1690. */
  1691. static void
  1692. decrypt_rekey (const struct UDPRekey *rekey,
  1693. size_t rekey_len,
  1694. struct KeyCacheEntry *kce,
  1695. struct SenderAddress *sender)
  1696. {
  1697. struct SharedSecret *ss = kce->ss;
  1698. struct SharedSecret *ss_rekey;
  1699. char out_buf[rekey_len - sizeof(*rekey)];
  1700. struct GNUNET_HashCode *master;
  1701. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1702. "decrypt_rekey.\n");
  1703. GNUNET_assert (NULL != ss->sender);
  1704. if (GNUNET_OK != try_decrypt (ss,
  1705. rekey->gcm_tag,
  1706. kce->sequence_number,
  1707. (const char *) &rekey[1],
  1708. sizeof(out_buf),
  1709. out_buf))
  1710. {
  1711. GNUNET_STATISTICS_update (stats,
  1712. "# Decryption failures with valid KCE",
  1713. 1,
  1714. GNUNET_NO);
  1715. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1716. "Decryption with kid %s failed\n",
  1717. GNUNET_sh2s (&rekey->kid));
  1718. kce_destroy (kce);
  1719. return;
  1720. }
  1721. kce_destroy (kce);
  1722. GNUNET_STATISTICS_update (stats,
  1723. "# bytes decrypted with Rekey",
  1724. sizeof(out_buf),
  1725. GNUNET_NO);
  1726. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1727. "decrypted UDPRekey with kid %s\n",
  1728. GNUNET_sh2s (&rekey->kid));
  1729. /*cmac = (struct GNUNET_HashCode *) out_buf;
  1730. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1731. "Received secret with cmac %s \n",
  1732. GNUNET_h2s (&cmac));*/
  1733. // ss_rekey = (struct SharedSecret *) out_buf;
  1734. master = (struct GNUNET_HashCode *) out_buf;
  1735. ss_rekey = GNUNET_new (struct SharedSecret);
  1736. ss_rekey->master = *master;
  1737. calculate_cmac (ss_rekey);
  1738. ss_rekey->sender = sender;
  1739. // ss_rekey->sequence_used = 0;
  1740. // ss_rekey->sequence_allowed = 0;
  1741. /* ss_rekey->active_kce_count = 0; */
  1742. /* ss_rekey->prev = NULL; */
  1743. /* ss_rekey->next = NULL; */
  1744. /* GNUNET_assert (ss_rekey->prev == NULL && sender->ss_head != ss_rekey); */
  1745. /* GNUNET_assert (ss_rekey->next == NULL && sender->ss_tail != ss_rekey); */
  1746. GNUNET_CONTAINER_DLL_insert (sender->ss_head, sender->ss_tail, ss_rekey);
  1747. sender->ss_rekey = ss_rekey;
  1748. sender->num_secrets++;
  1749. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1750. "Received secret with cmac %s\n",
  1751. GNUNET_h2s (&(ss_rekey->cmac)));
  1752. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1753. "Received secret with master %s.\n",
  1754. GNUNET_h2s (&(ss_rekey->master)));
  1755. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1756. "We have %u sequence_allowed.\n",
  1757. ss_rekey->sequence_allowed);
  1758. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1759. "We have a sender %p\n",
  1760. ss_rekey->sender);
  1761. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1762. "We have %u acks available.\n",
  1763. ss_rekey->sender->acks_available);
  1764. consider_ss_ack (ss_rekey, GNUNET_YES);
  1765. }
  1766. /**
  1767. * Closure for #find_sender_by_address()
  1768. */
  1769. struct SearchContext
  1770. {
  1771. /**
  1772. * Address we are looking for.
  1773. */
  1774. const struct sockaddr *address;
  1775. /**
  1776. * Number of bytes in @e address.
  1777. */
  1778. socklen_t address_len;
  1779. /**
  1780. * Return value to set if we found a match.
  1781. */
  1782. struct SenderAddress *sender;
  1783. };
  1784. /**
  1785. * Find existing `struct SenderAddress` by matching addresses.
  1786. *
  1787. * @param cls a `struct SearchContext`
  1788. * @param key ignored, must match already
  1789. * @param value a `struct SenderAddress`
  1790. * @return #GNUNET_YES if not found (continue to search), #GNUNET_NO if found
  1791. */
  1792. static int
  1793. find_sender_by_address (void *cls,
  1794. const struct GNUNET_PeerIdentity *key,
  1795. void *value)
  1796. {
  1797. struct SearchContext *sc = cls;
  1798. struct SenderAddress *sender = value;
  1799. if ((sender->address_len == sc->address_len) &&
  1800. (0 == memcmp (sender->address, sc->address, sender->address_len)))
  1801. {
  1802. sc->sender = sender;
  1803. return GNUNET_NO; /* stop iterating! */
  1804. }
  1805. return GNUNET_YES;
  1806. }
  1807. /**
  1808. * Create sender address for @a target. Note that we
  1809. * might already have one, so a fresh one is only allocated
  1810. * if one does not yet exist for @a address.
  1811. *
  1812. * @param target peer to generate address for
  1813. * @param address target address
  1814. * @param address_len number of bytes in @a address
  1815. * @return data structure to keep track of key material for
  1816. * decrypting data from @a target
  1817. */
  1818. static struct SenderAddress *
  1819. setup_sender (const struct GNUNET_PeerIdentity *target,
  1820. const struct sockaddr *address,
  1821. socklen_t address_len)
  1822. {
  1823. struct SenderAddress *sender;
  1824. struct SearchContext sc = { .address = address,
  1825. .address_len = address_len,
  1826. .sender = NULL };
  1827. GNUNET_CONTAINER_multipeermap_get_multiple (senders,
  1828. target,
  1829. &find_sender_by_address,
  1830. &sc);
  1831. if (NULL != sc.sender)
  1832. {
  1833. reschedule_sender_timeout (sc.sender);
  1834. return sc.sender;
  1835. }
  1836. sender = GNUNET_new (struct SenderAddress);
  1837. sender->target = *target;
  1838. sender->address = GNUNET_memdup (address, address_len);
  1839. sender->address_len = address_len;
  1840. (void) GNUNET_CONTAINER_multipeermap_put (
  1841. senders,
  1842. &sender->target,
  1843. sender,
  1844. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  1845. GNUNET_STATISTICS_set (stats,
  1846. "# senders active",
  1847. GNUNET_CONTAINER_multipeermap_size (receivers),
  1848. GNUNET_NO);
  1849. sender->timeout =
  1850. GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
  1851. sender->hn = GNUNET_CONTAINER_heap_insert (senders_heap,
  1852. sender,
  1853. sender->timeout.abs_value_us);
  1854. sender->nt = GNUNET_NT_scanner_get_type (is, address, address_len);
  1855. if (NULL == timeout_task)
  1856. timeout_task = GNUNET_SCHEDULER_add_now (&check_timeouts, NULL);
  1857. return sender;
  1858. }
  1859. /**
  1860. * Check signature from @a uc against @a ephemeral.
  1861. *
  1862. * @param ephermal key that is signed
  1863. * @param uc signature of claimant
  1864. * @return #GNUNET_OK if signature is valid
  1865. */
  1866. static int
  1867. verify_confirmation (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral,
  1868. const struct UDPConfirmation *uc)
  1869. {
  1870. struct UdpHandshakeSignature uhs;
  1871. uhs.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE);
  1872. uhs.purpose.size = htonl (sizeof(uhs));
  1873. uhs.sender = uc->sender;
  1874. uhs.receiver = my_identity;
  1875. uhs.ephemeral = *ephemeral;
  1876. uhs.monotonic_time = uc->monotonic_time;
  1877. return GNUNET_CRYPTO_eddsa_verify (
  1878. GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE,
  1879. &uhs,
  1880. &uc->sender_sig,
  1881. &uc->sender.public_key);
  1882. }
  1883. /**
  1884. * Converts @a address to the address string format used by this
  1885. * communicator in HELLOs.
  1886. *
  1887. * @param address the address to convert, must be AF_INET or AF_INET6.
  1888. * @param address_len number of bytes in @a address
  1889. * @return string representation of @a address
  1890. */
  1891. static char *
  1892. sockaddr_to_udpaddr_string (const struct sockaddr *address,
  1893. socklen_t address_len)
  1894. {
  1895. char *ret;
  1896. switch (address->sa_family)
  1897. {
  1898. case AF_INET:
  1899. GNUNET_asprintf (&ret,
  1900. "%s-%s",
  1901. COMMUNICATOR_ADDRESS_PREFIX,
  1902. GNUNET_a2s (address, address_len));
  1903. break;
  1904. case AF_INET6:
  1905. GNUNET_asprintf (&ret,
  1906. "%s-%s",
  1907. COMMUNICATOR_ADDRESS_PREFIX,
  1908. GNUNET_a2s (address, address_len));
  1909. break;
  1910. default:
  1911. GNUNET_assert (0);
  1912. }
  1913. return ret;
  1914. }
  1915. /**
  1916. * Socket read task.
  1917. *
  1918. * @param cls NULL
  1919. */
  1920. static void
  1921. sock_read (void *cls)
  1922. {
  1923. struct sockaddr_storage sa;
  1924. socklen_t salen = sizeof(sa);
  1925. char buf[UINT16_MAX];
  1926. ssize_t rcvd;
  1927. (void) cls;
  1928. read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  1929. udp_sock,
  1930. &sock_read,
  1931. NULL);
  1932. rcvd = GNUNET_NETWORK_socket_recvfrom (udp_sock,
  1933. buf,
  1934. sizeof(buf),
  1935. (struct sockaddr *) &sa,
  1936. &salen);
  1937. if (-1 == rcvd)
  1938. {
  1939. GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "recv");
  1940. return;
  1941. }
  1942. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1943. "Read %lu bytes\n", rcvd);
  1944. if (rcvd > sizeof(struct UDPRekey))
  1945. {
  1946. const struct UDPRekey *rekey;
  1947. const struct UDPBox *box;
  1948. struct KeyCacheEntry *kce;
  1949. struct SenderAddress *sender;
  1950. int do_decrypt = GNUNET_NO;
  1951. rekey = (const struct UDPRekey *) buf;
  1952. box = (const struct UDPBox *) buf;
  1953. kce = GNUNET_CONTAINER_multishortmap_get (key_cache, &rekey->kid);
  1954. if ((GNUNET_YES == box->rekeying) || (GNUNET_NO == box->rekeying))
  1955. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1956. "UDPRekey has rekeying %u\n",
  1957. box->rekeying);
  1958. else
  1959. do_decrypt = GNUNET_YES;
  1960. if ((GNUNET_YES == do_decrypt) && (NULL != kce) && (GNUNET_YES ==
  1961. kce->ss->sender->
  1962. rekeying))
  1963. {
  1964. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1965. "UDPRekey with kid %s\n",
  1966. GNUNET_sh2s (&rekey->kid));
  1967. sender = setup_sender (&rekey->sender, (const struct sockaddr *) &sa,
  1968. salen);
  1969. if (NULL != sender->ss_rekey)
  1970. return;
  1971. decrypt_rekey (rekey, (size_t) rcvd, kce, sender);
  1972. return;
  1973. }
  1974. }
  1975. /* first, see if it is a UDPBox */
  1976. if (rcvd > sizeof(struct UDPBox))
  1977. {
  1978. const struct UDPBox *box;
  1979. struct KeyCacheEntry *kce;
  1980. box = (const struct UDPBox *) buf;
  1981. kce = GNUNET_CONTAINER_multishortmap_get (key_cache, &box->kid);
  1982. if (NULL != kce)
  1983. {
  1984. decrypt_box (box, (size_t) rcvd, kce);
  1985. return;
  1986. }
  1987. }
  1988. /* next, check if it is a broadcast */
  1989. if (sizeof(struct UDPBroadcast) == rcvd)
  1990. {
  1991. const struct UDPBroadcast *ub;
  1992. struct UdpBroadcastSignature uhs;
  1993. ub = (const struct UDPBroadcast *) buf;
  1994. uhs.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST);
  1995. uhs.purpose.size = htonl (sizeof(uhs));
  1996. uhs.sender = ub->sender;
  1997. GNUNET_CRYPTO_hash (&sa, salen, &uhs.h_address);
  1998. if (GNUNET_OK ==
  1999. GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST,
  2000. &uhs,
  2001. &ub->sender_sig,
  2002. &ub->sender.public_key))
  2003. {
  2004. char *addr_s;
  2005. enum GNUNET_NetworkType nt;
  2006. addr_s =
  2007. sockaddr_to_udpaddr_string ((const struct sockaddr *) &sa, salen);
  2008. GNUNET_STATISTICS_update (stats, "# broadcasts received", 1, GNUNET_NO);
  2009. /* use our own mechanism to determine network type */
  2010. nt =
  2011. GNUNET_NT_scanner_get_type (is, (const struct sockaddr *) &sa, salen);
  2012. GNUNET_TRANSPORT_application_validate (ah, &ub->sender, nt, addr_s);
  2013. GNUNET_free (addr_s);
  2014. return;
  2015. }
  2016. /* continue with KX, mostly for statistics... */
  2017. }
  2018. /* finally, test if it is a KX */
  2019. if (rcvd < sizeof(struct UDPConfirmation) + sizeof(struct InitialKX))
  2020. {
  2021. GNUNET_STATISTICS_update (stats,
  2022. "# messages dropped (no kid, too small for KX)",
  2023. 1,
  2024. GNUNET_NO);
  2025. return;
  2026. }
  2027. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2028. "Got KX\n");
  2029. {
  2030. const struct InitialKX *kx;
  2031. struct SharedSecret *ss;
  2032. char pbuf[rcvd - sizeof(struct InitialKX)];
  2033. const struct UDPConfirmation *uc;
  2034. struct SenderAddress *sender;
  2035. kx = (const struct InitialKX *) buf;
  2036. ss = setup_shared_secret_dec (&kx->ephemeral);
  2037. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2038. "Before DEC\n");
  2039. if (GNUNET_OK != try_decrypt (ss,
  2040. kx->gcm_tag,
  2041. 0,
  2042. &buf[sizeof(*kx)],
  2043. sizeof(pbuf),
  2044. pbuf))
  2045. {
  2046. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2047. "Unable to decrypt tag, dropping...\n");
  2048. GNUNET_free (ss);
  2049. GNUNET_STATISTICS_update (
  2050. stats,
  2051. "# messages dropped (no kid, AEAD decryption failed)",
  2052. 1,
  2053. GNUNET_NO);
  2054. return;
  2055. }
  2056. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2057. "Before VERIFY\n");
  2058. uc = (const struct UDPConfirmation *) pbuf;
  2059. if (GNUNET_OK != verify_confirmation (&kx->ephemeral, uc))
  2060. {
  2061. GNUNET_break_op (0);
  2062. GNUNET_free (ss);
  2063. GNUNET_STATISTICS_update (stats,
  2064. "# messages dropped (sender signature invalid)",
  2065. 1,
  2066. GNUNET_NO);
  2067. return;
  2068. }
  2069. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2070. "Before SETUP_SENDER\n");
  2071. calculate_cmac (ss);
  2072. sender = setup_sender (&uc->sender, (const struct sockaddr *) &sa, salen);
  2073. ss->sender = sender;
  2074. GNUNET_CONTAINER_DLL_insert (sender->ss_head, sender->ss_tail, ss);
  2075. sender->num_secrets++;
  2076. GNUNET_STATISTICS_update (stats, "# Secrets active", 1, GNUNET_NO);
  2077. GNUNET_STATISTICS_update (stats,
  2078. "# messages decrypted without BOX",
  2079. 1,
  2080. GNUNET_NO);
  2081. try_handle_plaintext (sender, &uc[1], sizeof(pbuf) - sizeof(*uc));
  2082. if ((GNUNET_NO == kx->rekeying) && (GNUNET_YES == ss->sender->rekeying))
  2083. {
  2084. ss->sender->rekeying = GNUNET_NO;
  2085. sender->ss_rekey = NULL;
  2086. // destroy_all_secrets (ss, GNUNET_NO);
  2087. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2088. "Receiver stopped rekeying.\n");
  2089. }
  2090. else if (GNUNET_NO == kx->rekeying)
  2091. consider_ss_ack (ss, GNUNET_YES);
  2092. else
  2093. {
  2094. ss->sender->rekeying = GNUNET_YES;
  2095. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2096. "Got KX: Receiver doing rekeying.\n");
  2097. }
  2098. /*if (sender->num_secrets > MAX_SECRETS)
  2099. secret_destroy (sender->ss_tail);*/
  2100. }
  2101. }
  2102. /**
  2103. * Convert UDP bind specification to a `struct sockaddr *`
  2104. *
  2105. * @param bindto bind specification to convert
  2106. * @param[out] sock_len set to the length of the address
  2107. * @return converted bindto specification
  2108. */
  2109. static struct sockaddr *
  2110. udp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
  2111. {
  2112. struct sockaddr *in;
  2113. unsigned int port;
  2114. char dummy[2];
  2115. char *colon;
  2116. char *cp;
  2117. if (1 == sscanf (bindto, "%u%1s", &port, dummy))
  2118. {
  2119. /* interpreting value as just a PORT number */
  2120. if (port > UINT16_MAX)
  2121. {
  2122. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2123. "BINDTO specification `%s' invalid: value too large for port\n",
  2124. bindto);
  2125. return NULL;
  2126. }
  2127. if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
  2128. (GNUNET_YES ==
  2129. GNUNET_CONFIGURATION_get_value_yesno (cfg,
  2130. COMMUNICATOR_CONFIG_SECTION,
  2131. "DISABLE_V6")))
  2132. {
  2133. struct sockaddr_in *i4;
  2134. i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
  2135. i4->sin_family = AF_INET;
  2136. i4->sin_port = htons ((uint16_t) port);
  2137. *sock_len = sizeof(struct sockaddr_in);
  2138. in = (struct sockaddr *) i4;
  2139. }
  2140. else
  2141. {
  2142. struct sockaddr_in6 *i6;
  2143. i6 = GNUNET_malloc (sizeof(struct sockaddr_in6));
  2144. i6->sin6_family = AF_INET6;
  2145. i6->sin6_port = htons ((uint16_t) port);
  2146. *sock_len = sizeof(struct sockaddr_in6);
  2147. in = (struct sockaddr *) i6;
  2148. }
  2149. return in;
  2150. }
  2151. cp = GNUNET_strdup (bindto);
  2152. colon = strrchr (cp, ':');
  2153. if (NULL != colon)
  2154. {
  2155. /* interpet value after colon as port */
  2156. *colon = '\0';
  2157. colon++;
  2158. if (1 == sscanf (colon, "%u%1s", &port, dummy))
  2159. {
  2160. /* interpreting value as just a PORT number */
  2161. if (port > UINT16_MAX)
  2162. {
  2163. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2164. "BINDTO specification `%s' invalid: value too large for port\n",
  2165. bindto);
  2166. GNUNET_free (cp);
  2167. return NULL;
  2168. }
  2169. }
  2170. else
  2171. {
  2172. GNUNET_log (
  2173. GNUNET_ERROR_TYPE_ERROR,
  2174. "BINDTO specification `%s' invalid: last ':' not followed by number\n",
  2175. bindto);
  2176. GNUNET_free (cp);
  2177. return NULL;
  2178. }
  2179. }
  2180. else
  2181. {
  2182. /* interpret missing port as 0, aka pick any free one */
  2183. port = 0;
  2184. }
  2185. {
  2186. /* try IPv4 */
  2187. struct sockaddr_in v4;
  2188. if (1 == inet_pton (AF_INET, cp, &v4.sin_addr))
  2189. {
  2190. v4.sin_family = AF_INET;
  2191. v4.sin_port = htons ((uint16_t) port);
  2192. #if HAVE_SOCKADDR_IN_SIN_LEN
  2193. v4.sin_len = sizeof(struct sockaddr_in);
  2194. #endif
  2195. in = GNUNET_memdup (&v4, sizeof(struct sockaddr_in));
  2196. *sock_len = sizeof(struct sockaddr_in);
  2197. GNUNET_free (cp);
  2198. return in;
  2199. }
  2200. }
  2201. {
  2202. /* try IPv6 */
  2203. struct sockaddr_in6 v6;
  2204. const char *start;
  2205. start = cp;
  2206. if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
  2207. {
  2208. start++; /* skip over '[' */
  2209. cp[strlen (cp) - 1] = '\0'; /* eat ']' */
  2210. }
  2211. if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
  2212. {
  2213. v6.sin6_family = AF_INET6;
  2214. v6.sin6_port = htons ((uint16_t) port);
  2215. #if HAVE_SOCKADDR_IN_SIN_LEN
  2216. v6.sin6_len = sizeof(sizeof(struct sockaddr_in6));
  2217. #endif
  2218. in = GNUNET_memdup (&v6, sizeof(v6));
  2219. *sock_len = sizeof(v6);
  2220. GNUNET_free (cp);
  2221. return in;
  2222. }
  2223. }
  2224. /* #5528 FIXME (feature!): maybe also try getnameinfo()? */
  2225. GNUNET_free (cp);
  2226. return NULL;
  2227. }
  2228. /**
  2229. * Pad @a dgram by @a pad_size using @a out_cipher.
  2230. *
  2231. * @param out_cipher cipher to use
  2232. * @param dgram datagram to pad
  2233. * @param pad_size number of bytes of padding to append
  2234. */
  2235. static void
  2236. do_pad (gcry_cipher_hd_t out_cipher, char *dgram, size_t pad_size)
  2237. {
  2238. char pad[pad_size];
  2239. GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, pad, sizeof(pad));
  2240. if (sizeof(pad) > sizeof(struct GNUNET_MessageHeader))
  2241. {
  2242. struct GNUNET_MessageHeader hdr =
  2243. { .size = htons (sizeof(pad)),
  2244. .type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_PAD) };
  2245. memcpy (pad, &hdr, sizeof(hdr));
  2246. }
  2247. GNUNET_assert (
  2248. 0 ==
  2249. gcry_cipher_encrypt (out_cipher, dgram, sizeof(pad), pad, sizeof(pad)));
  2250. }
  2251. /**
  2252. * Signature of functions implementing the sending functionality of a
  2253. * message queue.
  2254. *
  2255. * @param mq the message queue
  2256. * @param msg the message to send
  2257. * @param impl_state our `struct ReceiverAddress`
  2258. */
  2259. static void
  2260. mq_send_kx (struct GNUNET_MQ_Handle *mq,
  2261. const struct GNUNET_MessageHeader *msg,
  2262. void *impl_state)
  2263. {
  2264. struct ReceiverAddress *receiver = impl_state;
  2265. uint16_t msize = ntohs (msg->size);
  2266. struct UdpHandshakeSignature uhs;
  2267. struct UDPConfirmation uc;
  2268. struct InitialKX kx;
  2269. struct GNUNET_CRYPTO_EcdhePrivateKey epriv;
  2270. char dgram[receiver->kx_mtu + sizeof(uc) + sizeof(kx)];
  2271. size_t dpos;
  2272. gcry_cipher_hd_t out_cipher;
  2273. struct SharedSecret *ss;
  2274. GNUNET_assert (mq == receiver->kx_mq);
  2275. if (msize > receiver->kx_mtu)
  2276. {
  2277. GNUNET_break (0);
  2278. if (GNUNET_YES != receiver->receiver_destroy_called)
  2279. receiver_destroy (receiver);
  2280. return;
  2281. }
  2282. reschedule_receiver_timeout (receiver);
  2283. /* setup key material */
  2284. GNUNET_CRYPTO_ecdhe_key_create (&epriv);
  2285. ss = setup_shared_secret_enc (&epriv, receiver, GNUNET_YES);
  2286. if (receiver->num_secrets > MAX_SECRETS)
  2287. {
  2288. destroy_all_secrets (ss, GNUNET_YES);
  2289. }
  2290. setup_cipher (&ss->master, 0, &out_cipher);
  2291. /* compute 'uc' */
  2292. uc.sender = my_identity;
  2293. uc.monotonic_time =
  2294. GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_monotonic (cfg));
  2295. uhs.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE);
  2296. uhs.purpose.size = htonl (sizeof(uhs));
  2297. uhs.sender = my_identity;
  2298. uhs.receiver = receiver->target;
  2299. GNUNET_CRYPTO_ecdhe_key_get_public (&epriv, &uhs.ephemeral);
  2300. uhs.monotonic_time = uc.monotonic_time;
  2301. GNUNET_CRYPTO_eddsa_sign (my_private_key,
  2302. &uhs,
  2303. &uc.sender_sig);
  2304. /* Leave space for kx */
  2305. dpos = sizeof(kx);
  2306. /* Append encrypted uc to dgram */
  2307. GNUNET_assert (0 == gcry_cipher_encrypt (out_cipher,
  2308. &dgram[dpos],
  2309. sizeof(uc),
  2310. &uc,
  2311. sizeof(uc)));
  2312. dpos += sizeof(uc);
  2313. /* Append encrypted payload to dgram */
  2314. GNUNET_assert (
  2315. 0 == gcry_cipher_encrypt (out_cipher, &dgram[dpos], msize, msg, msize));
  2316. dpos += msize;
  2317. do_pad (out_cipher, &dgram[dpos], sizeof(dgram) - dpos);
  2318. /* Datagram starts with kx */
  2319. kx.ephemeral = uhs.ephemeral;
  2320. GNUNET_assert (
  2321. 0 == gcry_cipher_gettag (out_cipher, kx.gcm_tag, sizeof(kx.gcm_tag)));
  2322. gcry_cipher_close (out_cipher);
  2323. if (GNUNET_NO == receiver->rekeying)
  2324. kx.rekeying = GNUNET_NO;
  2325. else
  2326. kx.rekeying = GNUNET_YES;
  2327. memcpy (dgram, &kx, sizeof(kx));
  2328. if (-1 == GNUNET_NETWORK_socket_sendto (udp_sock,
  2329. dgram,
  2330. sizeof(dgram),
  2331. receiver->address,
  2332. receiver->address_len))
  2333. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "send");
  2334. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2335. "Sending KX to %s\n", GNUNET_a2s (receiver->address,
  2336. receiver->address_len));
  2337. GNUNET_MQ_impl_send_continue (mq);
  2338. }
  2339. static void
  2340. check_for_rekeying (struct ReceiverAddress *receiver, struct UDPBox *box)
  2341. {
  2342. struct GNUNET_TIME_Relative rt;
  2343. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2344. "Timeout is %lu\n.",
  2345. receiver->rekey_timeout.abs_value_us);
  2346. if (0 == receiver->rekey_timeout.abs_value_us)
  2347. {
  2348. receiver->rekey_timeout = GNUNET_TIME_relative_to_absolute (
  2349. rekey_interval);
  2350. }
  2351. else
  2352. {
  2353. rt = GNUNET_TIME_absolute_get_remaining (receiver->rekey_timeout);
  2354. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2355. "Relative time is %lu and timeout is %lu\n.",
  2356. rt.rel_value_us,
  2357. receiver->rekey_timeout.abs_value_us);
  2358. if ((0 == rt.rel_value_us) || (receiver->rekey_send_bytes >
  2359. rekey_max_bytes) )
  2360. {
  2361. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2362. "Bytes send %lu greater than %llu max bytes\n.",
  2363. receiver->rekey_send_bytes,
  2364. rekey_max_bytes);
  2365. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2366. "Relative time is %lu and timeout is %lu\n.",
  2367. rt.rel_value_us,
  2368. receiver->rekey_timeout.abs_value_us);
  2369. receiver->rekey_timeout.abs_value_us = 0;
  2370. receiver->rekey_send_bytes = 0;
  2371. receiver->ss_rekey = NULL;
  2372. // destroy_all_secrets (ss, GNUNET_NO);
  2373. receiver->rekeying = GNUNET_YES;
  2374. receiver->rekey_acks_available = receiver->acks_available;
  2375. box->rekeying = GNUNET_YES;
  2376. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2377. "Sender started rekeying.\n");
  2378. if (GNUNET_YES == box->rekeying)
  2379. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2380. "Sending rekeying with kid %s\n",
  2381. GNUNET_sh2s (&box->kid));
  2382. }
  2383. }
  2384. }
  2385. static void
  2386. send_UDPRekey (struct ReceiverAddress *receiver, struct SharedSecret *ss)
  2387. {
  2388. uint8_t is_ss_rekey_sequence_allowed_zero = GNUNET_NO;
  2389. uint8_t is_acks_available_below = GNUNET_NO;
  2390. uint8_t send_rekey = GNUNET_NO;
  2391. uint16_t not_below;
  2392. struct GNUNET_CRYPTO_EcdhePrivateKey epriv;
  2393. struct UDPRekey *rekey;
  2394. size_t dpos;
  2395. char rekey_dgram[sizeof(struct UDPRekey) + receiver->d_mtu];
  2396. if (NULL != receiver->ss_rekey)
  2397. {
  2398. not_below = (receiver->rekey_acks_available
  2399. - (receiver->rekey_acks_available % 3)) / 3;
  2400. is_ss_rekey_sequence_allowed_zero = (0 ==
  2401. receiver->ss_rekey->sequence_allowed);
  2402. is_acks_available_below = (receiver->acks_available >= not_below);
  2403. send_rekey = (0 == (receiver->acks_available - not_below) % not_below) &&
  2404. is_acks_available_below && is_ss_rekey_sequence_allowed_zero;
  2405. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2406. "send_rekey: %u, %u, %u\n",
  2407. send_rekey,
  2408. receiver->rekey_acks_available,
  2409. receiver->acks_available);
  2410. }
  2411. else if (NULL == receiver->ss_rekey)
  2412. {
  2413. /* setup key material */
  2414. GNUNET_CRYPTO_ecdhe_key_create (&epriv);
  2415. receiver->ss_rekey = setup_shared_secret_enc (&epriv, receiver,
  2416. GNUNET_NO);
  2417. receiver->ss_rekey->sequence_allowed = 0;
  2418. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2419. "Setup secret with cmac %s\n",
  2420. GNUNET_h2s (&(receiver->ss_rekey->cmac)));
  2421. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2422. "Setup secret with master %s.\n",
  2423. GNUNET_h2s (&(receiver->ss_rekey->master)));
  2424. }
  2425. if (send_rekey)
  2426. {
  2427. GNUNET_assert (0 != receiver->number_rekeying_kce);
  2428. gcry_cipher_hd_t rekey_out_cipher;
  2429. while (NULL != ss && ss->sequence_used >= ss->sequence_allowed)
  2430. {
  2431. ss = ss->prev;
  2432. }
  2433. if (NULL != ss)
  2434. {
  2435. rekey = (struct UDPRekey *) rekey_dgram;
  2436. rekey->sender = my_identity;
  2437. ss->sequence_used++;
  2438. get_kid (&ss->master, ss->sequence_used, &rekey->kid);
  2439. receiver->number_rekeying_kce--;
  2440. setup_cipher (&ss->master, ss->sequence_used, &rekey_out_cipher);
  2441. /* Append encrypted payload to dgram */
  2442. dpos = sizeof(struct UDPRekey);
  2443. GNUNET_assert (
  2444. 0 == gcry_cipher_encrypt (rekey_out_cipher, &rekey_dgram[dpos],
  2445. sizeof(receiver->ss_rekey->master),
  2446. &(receiver->ss_rekey->master),
  2447. sizeof(receiver->ss_rekey->master)));
  2448. dpos += sizeof(receiver->ss_rekey->master);
  2449. /* GNUNET_assert ( */
  2450. /* 0 == gcry_cipher_encrypt (rekey_out_cipher, &rekey_dgram[dpos], */
  2451. /* /\*sizeof(receiver->ss_rekey->cmac), */
  2452. /* &(receiver->ss_rekey->cmac), */
  2453. /* sizeof(receiver->ss_rekey->cmac))); */
  2454. /* dpos += sizeof(receiver->ss_rekey->cmac);*\/ */
  2455. /* sizeof(receiver->ss_rekey), */
  2456. /* receiver->ss_rekey, */
  2457. /* sizeof(receiver->ss_rekey))); */
  2458. /* dpos += sizeof(receiver->ss_rekey); */
  2459. do_pad (rekey_out_cipher, &rekey_dgram[dpos], sizeof(rekey_dgram)
  2460. - dpos);
  2461. GNUNET_assert (0 == gcry_cipher_gettag (rekey_out_cipher,
  2462. rekey->gcm_tag,
  2463. sizeof(rekey->gcm_tag)));
  2464. gcry_cipher_close (rekey_out_cipher);
  2465. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2466. "Sending rekey with kid %s and master %s\n",
  2467. GNUNET_sh2s (&rekey->kid),
  2468. GNUNET_h2s (&(receiver->ss_rekey->master)));
  2469. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2470. "Sending rekey with cmac %s\n",
  2471. GNUNET_h2s (&(receiver->ss_rekey->cmac)));
  2472. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2473. "%u rekey kces left.\n",
  2474. receiver->number_rekeying_kce);
  2475. if (-1 == GNUNET_NETWORK_socket_sendto (udp_sock,
  2476. rekey_dgram,
  2477. sizeof(rekey_dgram),
  2478. receiver->address,
  2479. receiver->address_len))
  2480. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "send");
  2481. receiver->acks_available--;
  2482. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2483. "%u receiver->acks_available 1\n",
  2484. receiver->acks_available);
  2485. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2486. "Sending UDPRekey to %s\n", GNUNET_a2s (receiver->address,
  2487. receiver->
  2488. address_len));
  2489. }
  2490. }
  2491. }
  2492. /**
  2493. * Signature of functions implementing the sending functionality of a
  2494. * message queue.
  2495. *
  2496. * @param mq the message queue
  2497. * @param msg the message to send
  2498. * @param impl_state our `struct ReceiverAddress`
  2499. */
  2500. static void
  2501. mq_send_d (struct GNUNET_MQ_Handle *mq,
  2502. const struct GNUNET_MessageHeader *msg,
  2503. void *impl_state)
  2504. {
  2505. struct ReceiverAddress *receiver = impl_state;
  2506. uint16_t msize = ntohs (msg->size);
  2507. GNUNET_assert (mq == receiver->d_mq);
  2508. if ((msize > receiver->d_mtu) ||
  2509. (0 == receiver->acks_available))
  2510. {
  2511. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2512. "msize: %u, mtu: %lu, acks: %u\n",
  2513. msize,
  2514. receiver->d_mtu,
  2515. receiver->acks_available);
  2516. GNUNET_break (0);
  2517. if (GNUNET_YES != receiver->receiver_destroy_called)
  2518. receiver_destroy (receiver);
  2519. return;
  2520. }
  2521. reschedule_receiver_timeout (receiver);
  2522. /* begin "BOX" encryption method, scan for ACKs from tail! */
  2523. for (struct SharedSecret *ss = receiver->ss_tail; NULL != ss; ss = ss->prev)
  2524. {
  2525. if (0 < ss->sequence_used)
  2526. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2527. "Trying to send UDPBox with shared secrect %s sequence_used %u and ss->sequence_allowed %u\n",
  2528. GNUNET_h2s (&ss->master),
  2529. ss->sequence_used,
  2530. ss->sequence_allowed);
  2531. // Uncomment this for alternativ 1 of backchannel functionality
  2532. if (ss->sequence_used >= ss->sequence_allowed)
  2533. // Until here for alternativ 1
  2534. // Uncomment this for alternativ 2 of backchannel functionality
  2535. // if (0 == ss->sequence_allowed)
  2536. // Until here for alternativ 2
  2537. {
  2538. continue;
  2539. }
  2540. char dgram[sizeof(struct UDPBox) + receiver->d_mtu];
  2541. struct UDPBox *box;
  2542. gcry_cipher_hd_t out_cipher;
  2543. size_t dpos;
  2544. box = (struct UDPBox *) dgram;
  2545. ss->sequence_used++;
  2546. get_kid (&ss->master, ss->sequence_used, &box->kid);
  2547. setup_cipher (&ss->master, ss->sequence_used, &out_cipher);
  2548. /* Append encrypted payload to dgram */
  2549. dpos = sizeof(struct UDPBox);
  2550. GNUNET_assert (
  2551. 0 == gcry_cipher_encrypt (out_cipher, &dgram[dpos], msize, msg, msize));
  2552. dpos += msize;
  2553. do_pad (out_cipher, &dgram[dpos], sizeof(dgram) - dpos);
  2554. GNUNET_assert (0 == gcry_cipher_gettag (out_cipher,
  2555. box->gcm_tag,
  2556. sizeof(box->gcm_tag)));
  2557. gcry_cipher_close (out_cipher);
  2558. receiver->rekey_send_bytes += sizeof(struct UDPBox) + receiver->d_mtu;
  2559. if (GNUNET_NO == receiver->rekeying)
  2560. box->rekeying = GNUNET_NO;
  2561. else
  2562. box->rekeying = GNUNET_YES;
  2563. if (-1 == GNUNET_NETWORK_socket_sendto (udp_sock,
  2564. dgram,
  2565. sizeof(dgram),
  2566. receiver->address,
  2567. receiver->address_len))
  2568. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "send");
  2569. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2570. "Sending UDPBox %u acks left\n",
  2571. receiver->acks_available);
  2572. GNUNET_MQ_impl_send_continue (mq);
  2573. receiver->acks_available--;
  2574. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2575. "%u receiver->acks_available 2\n",
  2576. receiver->acks_available);
  2577. check_for_rekeying (receiver, box);
  2578. if (0 == receiver->acks_available - receiver->number_rekeying_kce)
  2579. {
  2580. /* We have no more ACKs */
  2581. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2582. "No more acks\n");
  2583. if (GNUNET_YES == receiver->rekeying)
  2584. {
  2585. receiver->rekeying = GNUNET_NO;
  2586. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2587. "Sender stopped rekeying\n");
  2588. if ((NULL != receiver->ss_rekey) && (0 <
  2589. receiver->ss_rekey->
  2590. sequence_allowed) )
  2591. add_acks_rekey (receiver);
  2592. }
  2593. }
  2594. else if ((GNUNET_YES == receiver->rekeying) )
  2595. {
  2596. send_UDPRekey (receiver, ss);
  2597. }
  2598. return;
  2599. }
  2600. }
  2601. /**
  2602. * Signature of functions implementing the destruction of a message
  2603. * queue. Implementations must not free @a mq, but should take care
  2604. * of @a impl_state.
  2605. *
  2606. * @param mq the message queue to destroy
  2607. * @param impl_state our `struct ReceiverAddress`
  2608. */
  2609. static void
  2610. mq_destroy_d (struct GNUNET_MQ_Handle *mq, void *impl_state)
  2611. {
  2612. struct ReceiverAddress *receiver = impl_state;
  2613. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2614. "Default MQ destroyed\n");
  2615. if (mq == receiver->d_mq)
  2616. {
  2617. receiver->d_mq = NULL;
  2618. if (GNUNET_YES != receiver->receiver_destroy_called)
  2619. receiver_destroy (receiver);
  2620. }
  2621. }
  2622. /**
  2623. * Signature of functions implementing the destruction of a message
  2624. * queue. Implementations must not free @a mq, but should take care
  2625. * of @a impl_state.
  2626. *
  2627. * @param mq the message queue to destroy
  2628. * @param impl_state our `struct ReceiverAddress`
  2629. */
  2630. static void
  2631. mq_destroy_kx (struct GNUNET_MQ_Handle *mq, void *impl_state)
  2632. {
  2633. struct ReceiverAddress *receiver = impl_state;
  2634. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2635. "KX MQ destroyed\n");
  2636. if (mq == receiver->kx_mq)
  2637. {
  2638. receiver->kx_mq = NULL;
  2639. if (GNUNET_YES != receiver->receiver_destroy_called)
  2640. receiver_destroy (receiver);
  2641. }
  2642. }
  2643. /**
  2644. * Implementation function that cancels the currently sent message.
  2645. *
  2646. * @param mq message queue
  2647. * @param impl_state our `struct RecvierAddress`
  2648. */
  2649. static void
  2650. mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
  2651. {
  2652. /* Cancellation is impossible with UDP; bail */
  2653. GNUNET_assert (0);
  2654. }
  2655. /**
  2656. * Generic error handler, called with the appropriate
  2657. * error code and the same closure specified at the creation of
  2658. * the message queue.
  2659. * Not every message queue implementation supports an error handler.
  2660. *
  2661. * @param cls our `struct ReceiverAddress`
  2662. * @param error error code
  2663. */
  2664. static void
  2665. mq_error (void *cls, enum GNUNET_MQ_Error error)
  2666. {
  2667. struct ReceiverAddress *receiver = cls;
  2668. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2669. "MQ error in queue to %s: %d\n",
  2670. GNUNET_i2s (&receiver->target),
  2671. (int) error);
  2672. receiver_destroy (receiver);
  2673. }
  2674. /**
  2675. * Setup the MQ for the @a receiver. If a queue exists,
  2676. * the existing one is destroyed. Then the MTU is
  2677. * recalculated and a fresh queue is initialized.
  2678. *
  2679. * @param receiver receiver to setup MQ for
  2680. */
  2681. static void
  2682. setup_receiver_mq (struct ReceiverAddress *receiver)
  2683. {
  2684. size_t base_mtu;
  2685. /*if (NULL != receiver->kx_qh)
  2686. {
  2687. GNUNET_TRANSPORT_communicator_mq_del (receiver->kx_qh);
  2688. receiver->kx_qh = NULL;
  2689. }
  2690. if (NULL != receiver->d_qh)
  2691. {
  2692. GNUNET_TRANSPORT_communicator_mq_del (receiver->d_qh);
  2693. receiver->d_qh = NULL;
  2694. }*/
  2695. // GNUNET_assert (NULL == receiver->mq);
  2696. switch (receiver->address->sa_family)
  2697. {
  2698. case AF_INET:
  2699. base_mtu = 1480 /* Ethernet MTU, 1500 - Ethernet header - VLAN tag */
  2700. - sizeof(struct GNUNET_TUN_IPv4Header) /* 20 */
  2701. - sizeof(struct GNUNET_TUN_UdpHeader) /* 8 */;
  2702. break;
  2703. case AF_INET6:
  2704. base_mtu = 1280 /* Minimum MTU required by IPv6 */
  2705. - sizeof(struct GNUNET_TUN_IPv6Header) /* 40 */
  2706. - sizeof(struct GNUNET_TUN_UdpHeader) /* 8 */;
  2707. break;
  2708. default:
  2709. GNUNET_assert (0);
  2710. break;
  2711. }
  2712. /* MTU based on full KX messages */
  2713. receiver->kx_mtu = base_mtu - sizeof(struct InitialKX) /* 48 */
  2714. - sizeof(struct UDPConfirmation); /* 104 */
  2715. /* MTU based on BOXed messages */
  2716. receiver->d_mtu = base_mtu - sizeof(struct UDPBox);
  2717. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2718. "Setting up MQs and QHs\n");
  2719. /* => Effective MTU for CORE will range from 1080 (IPv6 + KX) to
  2720. 1404 (IPv4 + Box) bytes, depending on circumstances... */
  2721. if (NULL == receiver->kx_mq)
  2722. receiver->kx_mq = GNUNET_MQ_queue_for_callbacks (&mq_send_kx,
  2723. &mq_destroy_kx,
  2724. &mq_cancel,
  2725. receiver,
  2726. NULL,
  2727. &mq_error,
  2728. receiver);
  2729. if (NULL == receiver->d_mq)
  2730. receiver->d_mq = GNUNET_MQ_queue_for_callbacks (&mq_send_d,
  2731. &mq_destroy_d,
  2732. &mq_cancel,
  2733. receiver,
  2734. NULL,
  2735. &mq_error,
  2736. receiver);
  2737. receiver->kx_qh =
  2738. GNUNET_TRANSPORT_communicator_mq_add (ch,
  2739. &receiver->target,
  2740. receiver->foreign_addr,
  2741. receiver->kx_mtu,
  2742. GNUNET_TRANSPORT_QUEUE_LENGTH_UNLIMITED,
  2743. 0, /* Priority */
  2744. receiver->nt,
  2745. GNUNET_TRANSPORT_CS_OUTBOUND,
  2746. receiver->kx_mq);
  2747. receiver->d_qh =
  2748. GNUNET_TRANSPORT_communicator_mq_add (ch,
  2749. &receiver->target,
  2750. receiver->foreign_addr,
  2751. receiver->d_mtu,
  2752. 0, /* Initialize with 0 acks */
  2753. 1, /* Priority */
  2754. receiver->nt,
  2755. GNUNET_TRANSPORT_CS_OUTBOUND,
  2756. receiver->d_mq);
  2757. }
  2758. /**
  2759. * Function called by the transport service to initialize a
  2760. * message queue given address information about another peer.
  2761. * If and when the communication channel is established, the
  2762. * communicator must call #GNUNET_TRANSPORT_communicator_mq_add()
  2763. * to notify the service that the channel is now up. It is
  2764. * the responsibility of the communicator to manage sane
  2765. * retries and timeouts for any @a peer/@a address combination
  2766. * provided by the transport service. Timeouts and retries
  2767. * do not need to be signalled to the transport service.
  2768. *
  2769. * @param cls closure
  2770. * @param peer identity of the other peer
  2771. * @param address where to send the message, human-readable
  2772. * communicator-specific format, 0-terminated, UTF-8
  2773. * @return #GNUNET_OK on success, #GNUNET_SYSERR if the provided address is
  2774. * invalid
  2775. */
  2776. static int
  2777. mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
  2778. {
  2779. struct ReceiverAddress *receiver;
  2780. const char *path;
  2781. struct sockaddr *in;
  2782. socklen_t in_len;
  2783. if (0 != strncmp (address,
  2784. COMMUNICATOR_ADDRESS_PREFIX "-",
  2785. strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
  2786. {
  2787. GNUNET_break_op (0);
  2788. return GNUNET_SYSERR;
  2789. }
  2790. path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
  2791. in = udp_address_to_sockaddr (path, &in_len);
  2792. receiver = GNUNET_new (struct ReceiverAddress);
  2793. receiver->address = in;
  2794. receiver->address_len = in_len;
  2795. receiver->target = *peer;
  2796. receiver->nt = GNUNET_NT_scanner_get_type (is, in, in_len);
  2797. (void) GNUNET_CONTAINER_multipeermap_put (
  2798. receivers,
  2799. &receiver->target,
  2800. receiver,
  2801. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  2802. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2803. "Added %s to receivers\n",
  2804. GNUNET_i2s_full (&receiver->target));
  2805. receiver->timeout =
  2806. GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
  2807. receiver->hn = GNUNET_CONTAINER_heap_insert (receivers_heap,
  2808. receiver,
  2809. receiver->timeout.abs_value_us);
  2810. GNUNET_STATISTICS_set (stats,
  2811. "# receivers active",
  2812. GNUNET_CONTAINER_multipeermap_size (receivers),
  2813. GNUNET_NO);
  2814. receiver->foreign_addr =
  2815. sockaddr_to_udpaddr_string (receiver->address, receiver->address_len);
  2816. setup_receiver_mq (receiver);
  2817. if (NULL == timeout_task)
  2818. timeout_task = GNUNET_SCHEDULER_add_now (&check_timeouts, NULL);
  2819. return GNUNET_OK;
  2820. }
  2821. /**
  2822. * Iterator over all receivers to clean up.
  2823. *
  2824. * @param cls NULL
  2825. * @param target unused
  2826. * @param value the queue to destroy
  2827. * @return #GNUNET_OK to continue to iterate
  2828. */
  2829. static int
  2830. get_receiver_delete_it (void *cls,
  2831. const struct GNUNET_PeerIdentity *target,
  2832. void *value)
  2833. {
  2834. struct ReceiverAddress *receiver = value;
  2835. (void) cls;
  2836. (void) target;
  2837. receiver_destroy (receiver);
  2838. return GNUNET_OK;
  2839. }
  2840. /**
  2841. * Iterator over all senders to clean up.
  2842. *
  2843. * @param cls NULL
  2844. * @param target unused
  2845. * @param value the queue to destroy
  2846. * @return #GNUNET_OK to continue to iterate
  2847. */
  2848. static int
  2849. get_sender_delete_it (void *cls,
  2850. const struct GNUNET_PeerIdentity *target,
  2851. void *value)
  2852. {
  2853. struct SenderAddress *sender = value;
  2854. (void) cls;
  2855. (void) target;
  2856. sender_destroy (sender);
  2857. return GNUNET_OK;
  2858. }
  2859. /**
  2860. * Shutdown the UNIX communicator.
  2861. *
  2862. * @param cls NULL (always)
  2863. */
  2864. static void
  2865. do_shutdown (void *cls)
  2866. {
  2867. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2868. "do_shutdown\n");
  2869. if (NULL != nat)
  2870. {
  2871. GNUNET_NAT_unregister (nat);
  2872. nat = NULL;
  2873. }
  2874. while (NULL != bi_head)
  2875. bi_destroy (bi_head);
  2876. if (NULL != broadcast_task)
  2877. {
  2878. GNUNET_SCHEDULER_cancel (broadcast_task);
  2879. broadcast_task = NULL;
  2880. }
  2881. if (NULL != kce_task_rekey)
  2882. {
  2883. GNUNET_SCHEDULER_cancel (kce_task_rekey);
  2884. kce_task_rekey = NULL;
  2885. }
  2886. if (NULL != kce_task)
  2887. {
  2888. GNUNET_SCHEDULER_cancel (kce_task);
  2889. kce_task = NULL;
  2890. }
  2891. if (NULL != timeout_task)
  2892. {
  2893. GNUNET_SCHEDULER_cancel (timeout_task);
  2894. timeout_task = NULL;
  2895. }
  2896. if (NULL != read_task)
  2897. {
  2898. GNUNET_SCHEDULER_cancel (read_task);
  2899. read_task = NULL;
  2900. }
  2901. if (NULL != udp_sock)
  2902. {
  2903. GNUNET_break (GNUNET_OK ==
  2904. GNUNET_NETWORK_socket_close (udp_sock));
  2905. udp_sock = NULL;
  2906. }
  2907. GNUNET_CONTAINER_multipeermap_iterate (receivers,
  2908. &get_receiver_delete_it,
  2909. NULL);
  2910. GNUNET_CONTAINER_multipeermap_destroy (receivers);
  2911. GNUNET_CONTAINER_multipeermap_iterate (senders,
  2912. &get_sender_delete_it,
  2913. NULL);
  2914. GNUNET_CONTAINER_multipeermap_destroy (senders);
  2915. GNUNET_CONTAINER_multishortmap_destroy (key_cache);
  2916. GNUNET_CONTAINER_heap_destroy (senders_heap);
  2917. GNUNET_CONTAINER_heap_destroy (receivers_heap);
  2918. if (NULL != timeout_task)
  2919. {
  2920. GNUNET_SCHEDULER_cancel (timeout_task);
  2921. timeout_task = NULL;
  2922. }
  2923. if (NULL != ch)
  2924. {
  2925. GNUNET_TRANSPORT_communicator_disconnect (ch);
  2926. ch = NULL;
  2927. }
  2928. if (NULL != ah)
  2929. {
  2930. GNUNET_TRANSPORT_application_done (ah);
  2931. ah = NULL;
  2932. }
  2933. if (NULL != stats)
  2934. {
  2935. GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
  2936. stats = NULL;
  2937. }
  2938. if (NULL != my_private_key)
  2939. {
  2940. GNUNET_free (my_private_key);
  2941. my_private_key = NULL;
  2942. }
  2943. if (NULL != is)
  2944. {
  2945. GNUNET_NT_scanner_done (is);
  2946. is = NULL;
  2947. }
  2948. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2949. "do_shutdown finished\n");
  2950. }
  2951. /**
  2952. * Function called when the transport service has received a
  2953. * backchannel message for this communicator (!) via a different return
  2954. * path. Should be an acknowledgement.
  2955. *
  2956. * @param cls closure, NULL
  2957. * @param sender which peer sent the notification
  2958. * @param msg payload
  2959. */
  2960. static void
  2961. enc_notify_cb (void *cls,
  2962. const struct GNUNET_PeerIdentity *sender,
  2963. const struct GNUNET_MessageHeader *msg)
  2964. {
  2965. const struct UDPAck *ack;
  2966. (void) cls;
  2967. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2968. "Storing UDPAck received from backchannel from %s\n",
  2969. GNUNET_i2s_full (sender));
  2970. if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK) ||
  2971. (ntohs (msg->size) != sizeof(struct UDPAck)))
  2972. {
  2973. GNUNET_break_op (0);
  2974. return;
  2975. }
  2976. ack = (const struct UDPAck *) msg;
  2977. GNUNET_CONTAINER_multipeermap_get_multiple (receivers,
  2978. sender,
  2979. &handle_ack,
  2980. (void *) ack);
  2981. }
  2982. /**
  2983. * Signature of the callback passed to #GNUNET_NAT_register() for
  2984. * a function to call whenever our set of 'valid' addresses changes.
  2985. *
  2986. * @param cls closure
  2987. * @param app_ctx[in,out] location where the app can store stuff
  2988. * on add and retrieve it on remove
  2989. * @param add_remove #GNUNET_YES to add a new public IP address,
  2990. * #GNUNET_NO to remove a previous (now invalid) one
  2991. * @param ac address class the address belongs to
  2992. * @param addr either the previous or the new public IP address
  2993. * @param addrlen actual length of the @a addr
  2994. */
  2995. static void
  2996. nat_address_cb (void *cls,
  2997. void **app_ctx,
  2998. int add_remove,
  2999. enum GNUNET_NAT_AddressClass ac,
  3000. const struct sockaddr *addr,
  3001. socklen_t addrlen)
  3002. {
  3003. char *my_addr;
  3004. struct GNUNET_TRANSPORT_AddressIdentifier *ai;
  3005. if (GNUNET_YES == add_remove)
  3006. {
  3007. enum GNUNET_NetworkType nt;
  3008. GNUNET_asprintf (&my_addr,
  3009. "%s-%s",
  3010. COMMUNICATOR_ADDRESS_PREFIX,
  3011. GNUNET_a2s (addr, addrlen));
  3012. nt = GNUNET_NT_scanner_get_type (is, addr, addrlen);
  3013. ai =
  3014. GNUNET_TRANSPORT_communicator_address_add (ch,
  3015. my_addr,
  3016. nt,
  3017. GNUNET_TIME_UNIT_FOREVER_REL);
  3018. GNUNET_free (my_addr);
  3019. *app_ctx = ai;
  3020. }
  3021. else
  3022. {
  3023. ai = *app_ctx;
  3024. GNUNET_TRANSPORT_communicator_address_remove (ai);
  3025. *app_ctx = NULL;
  3026. }
  3027. }
  3028. /**
  3029. * Broadcast our presence on one of our interfaces.
  3030. *
  3031. * @param cls a `struct BroadcastInterface`
  3032. */
  3033. static void
  3034. ifc_broadcast (void *cls)
  3035. {
  3036. struct BroadcastInterface *bi = cls;
  3037. struct GNUNET_TIME_Relative delay;
  3038. delay = BROADCAST_FREQUENCY;
  3039. delay.rel_value_us =
  3040. GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, delay.rel_value_us);
  3041. bi->broadcast_task =
  3042. GNUNET_SCHEDULER_add_delayed (INTERFACE_SCAN_FREQUENCY, &ifc_broadcast, bi);
  3043. switch (bi->sa->sa_family)
  3044. {
  3045. case AF_INET: {
  3046. static int yes = 1;
  3047. static int no = 0;
  3048. ssize_t sent;
  3049. if (GNUNET_OK !=
  3050. GNUNET_NETWORK_socket_setsockopt (udp_sock,
  3051. SOL_SOCKET,
  3052. SO_BROADCAST,
  3053. &yes,
  3054. sizeof(int)))
  3055. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
  3056. "setsockopt");
  3057. sent = GNUNET_NETWORK_socket_sendto (udp_sock,
  3058. &bi->bcm,
  3059. sizeof(bi->bcm),
  3060. bi->ba,
  3061. bi->salen);
  3062. if (-1 == sent)
  3063. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
  3064. "sendto");
  3065. if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt (udp_sock,
  3066. SOL_SOCKET,
  3067. SO_BROADCAST,
  3068. &no,
  3069. sizeof(int)))
  3070. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
  3071. "setsockopt");
  3072. break;
  3073. }
  3074. case AF_INET6: {
  3075. ssize_t sent;
  3076. struct sockaddr_in6 dst;
  3077. dst.sin6_family = AF_INET6;
  3078. dst.sin6_port = htons (my_port);
  3079. dst.sin6_addr = bi->mcreq.ipv6mr_multiaddr;
  3080. dst.sin6_scope_id = ((struct sockaddr_in6 *) bi->ba)->sin6_scope_id;
  3081. sent = GNUNET_NETWORK_socket_sendto (udp_sock,
  3082. &bi->bcm,
  3083. sizeof(bi->bcm),
  3084. (const struct sockaddr *) &dst,
  3085. sizeof(dst));
  3086. if (-1 == sent)
  3087. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto");
  3088. break;
  3089. }
  3090. default:
  3091. GNUNET_break (0);
  3092. break;
  3093. }
  3094. }
  3095. /**
  3096. * Callback function invoked for each interface found.
  3097. * Activates/deactivates broadcast interfaces.
  3098. *
  3099. * @param cls NULL
  3100. * @param name name of the interface (can be NULL for unknown)
  3101. * @param isDefault is this presumably the default interface
  3102. * @param addr address of this interface (can be NULL for unknown or unassigned)
  3103. * @param broadcast_addr the broadcast address (can be NULL for unknown or
  3104. * unassigned)
  3105. * @param netmask the network mask (can be NULL for unknown or unassigned)
  3106. * @param addrlen length of the address
  3107. * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
  3108. */
  3109. static int
  3110. iface_proc (void *cls,
  3111. const char *name,
  3112. int isDefault,
  3113. const struct sockaddr *addr,
  3114. const struct sockaddr *broadcast_addr,
  3115. const struct sockaddr *netmask,
  3116. socklen_t addrlen)
  3117. {
  3118. struct BroadcastInterface *bi;
  3119. enum GNUNET_NetworkType network;
  3120. struct UdpBroadcastSignature ubs;
  3121. (void) cls;
  3122. (void) netmask;
  3123. if (NULL == addr)
  3124. return GNUNET_YES; /* need to know our address! */
  3125. network = GNUNET_NT_scanner_get_type (is, addr, addrlen);
  3126. if (GNUNET_NT_LOOPBACK == network)
  3127. {
  3128. /* Broadcasting on loopback does not make sense */
  3129. return GNUNET_YES;
  3130. }
  3131. for (bi = bi_head; NULL != bi; bi = bi->next)
  3132. {
  3133. if ((bi->salen == addrlen) && (0 == memcmp (addr, bi->sa, addrlen)))
  3134. {
  3135. bi->found = GNUNET_YES;
  3136. return GNUNET_OK;
  3137. }
  3138. }
  3139. if ((AF_INET6 == addr->sa_family) && (NULL == broadcast_addr))
  3140. return GNUNET_OK; /* broadcast_addr is required for IPv6! */
  3141. if ((AF_INET6 == addr->sa_family) && (GNUNET_YES != have_v6_socket))
  3142. return GNUNET_OK; /* not using IPv6 */
  3143. bi = GNUNET_new (struct BroadcastInterface);
  3144. bi->sa = GNUNET_memdup (addr,
  3145. addrlen);
  3146. if ( (NULL != broadcast_addr) &&
  3147. (addrlen == sizeof (struct sockaddr_in)) )
  3148. {
  3149. struct sockaddr_in *ba;
  3150. ba = GNUNET_memdup (broadcast_addr,
  3151. addrlen);
  3152. ba->sin_port = htons (2086); /* always GNUnet port, ignore configuration! */
  3153. bi->ba = (struct sockaddr *) ba;
  3154. }
  3155. bi->salen = addrlen;
  3156. bi->found = GNUNET_YES;
  3157. bi->bcm.sender = my_identity;
  3158. ubs.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST);
  3159. ubs.purpose.size = htonl (sizeof(ubs));
  3160. ubs.sender = my_identity;
  3161. GNUNET_CRYPTO_hash (addr, addrlen, &ubs.h_address);
  3162. GNUNET_CRYPTO_eddsa_sign (my_private_key,
  3163. &ubs,
  3164. &bi->bcm.sender_sig);
  3165. if (NULL != bi->ba)
  3166. {
  3167. bi->broadcast_task = GNUNET_SCHEDULER_add_now (&ifc_broadcast, bi);
  3168. GNUNET_CONTAINER_DLL_insert (bi_head, bi_tail, bi);
  3169. }
  3170. if ((AF_INET6 == addr->sa_family) && (NULL != broadcast_addr))
  3171. {
  3172. /* Create IPv6 multicast request */
  3173. const struct sockaddr_in6 *s6 =
  3174. (const struct sockaddr_in6 *) broadcast_addr;
  3175. GNUNET_assert (
  3176. 1 == inet_pton (AF_INET6, "FF05::13B", &bi->mcreq.ipv6mr_multiaddr));
  3177. /* http://tools.ietf.org/html/rfc2553#section-5.2:
  3178. *
  3179. * IPV6_JOIN_GROUP
  3180. *
  3181. * Join a multicast group on a specified local interface. If the
  3182. * interface index is specified as 0, the kernel chooses the local
  3183. * interface. For example, some kernels look up the multicast
  3184. * group in the normal IPv6 routing table and using the resulting
  3185. * interface; we do this for each interface, so no need to use
  3186. * zero (anymore...).
  3187. */bi->mcreq.ipv6mr_interface = s6->sin6_scope_id;
  3188. /* Join the multicast group */
  3189. if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt (udp_sock,
  3190. IPPROTO_IPV6,
  3191. IPV6_JOIN_GROUP,
  3192. &bi->mcreq,
  3193. sizeof(bi->mcreq)))
  3194. {
  3195. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
  3196. }
  3197. }
  3198. return GNUNET_OK;
  3199. }
  3200. /**
  3201. * Scan interfaces to broadcast our presence on the LAN.
  3202. *
  3203. * @param cls NULL, unused
  3204. */
  3205. static void
  3206. do_broadcast (void *cls)
  3207. {
  3208. struct BroadcastInterface *bin;
  3209. (void) cls;
  3210. for (struct BroadcastInterface *bi = bi_head; NULL != bi; bi = bi->next)
  3211. bi->found = GNUNET_NO;
  3212. GNUNET_OS_network_interfaces_list (&iface_proc, NULL);
  3213. for (struct BroadcastInterface *bi = bi_head; NULL != bi; bi = bin)
  3214. {
  3215. bin = bi->next;
  3216. if (GNUNET_NO == bi->found)
  3217. bi_destroy (bi);
  3218. }
  3219. broadcast_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_SCAN_FREQUENCY,
  3220. &do_broadcast,
  3221. NULL);
  3222. }
  3223. /**
  3224. * Setup communicator and launch network interactions.
  3225. *
  3226. * @param cls NULL (always)
  3227. * @param args remaining command-line arguments
  3228. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  3229. * @param c configuration
  3230. */
  3231. static void
  3232. run (void *cls,
  3233. char *const *args,
  3234. const char *cfgfile,
  3235. const struct GNUNET_CONFIGURATION_Handle *c)
  3236. {
  3237. char *bindto;
  3238. struct sockaddr *in;
  3239. socklen_t in_len;
  3240. struct sockaddr_storage in_sto;
  3241. socklen_t sto_len;
  3242. (void) cls;
  3243. cfg = c;
  3244. if (GNUNET_OK !=
  3245. GNUNET_CONFIGURATION_get_value_string (cfg,
  3246. COMMUNICATOR_CONFIG_SECTION,
  3247. "BINDTO",
  3248. &bindto))
  3249. {
  3250. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  3251. COMMUNICATOR_CONFIG_SECTION,
  3252. "BINDTO");
  3253. return;
  3254. }
  3255. if (GNUNET_OK !=
  3256. GNUNET_CONFIGURATION_get_value_time (cfg,
  3257. COMMUNICATOR_CONFIG_SECTION,
  3258. "REKEY_INTERVAL",
  3259. &rekey_interval))
  3260. rekey_interval = DEFAULT_REKEY_TIME_INTERVAL;
  3261. if (GNUNET_OK !=
  3262. GNUNET_CONFIGURATION_get_value_size (cfg,
  3263. COMMUNICATOR_CONFIG_SECTION,
  3264. "REKEY_MAX_BYTES",
  3265. &rekey_max_bytes))
  3266. rekey_max_bytes = DEFAULT_REKEY_MAX_BYTES;
  3267. in = udp_address_to_sockaddr (bindto, &in_len);
  3268. if (NULL == in)
  3269. {
  3270. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  3271. "Failed to setup UDP socket address with path `%s'\n",
  3272. bindto);
  3273. GNUNET_free (bindto);
  3274. return;
  3275. }
  3276. udp_sock =
  3277. GNUNET_NETWORK_socket_create (in->sa_family,
  3278. SOCK_DGRAM,
  3279. IPPROTO_UDP);
  3280. if (NULL == udp_sock)
  3281. {
  3282. GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
  3283. GNUNET_free (in);
  3284. GNUNET_free (bindto);
  3285. return;
  3286. }
  3287. if (AF_INET6 == in->sa_family)
  3288. have_v6_socket = GNUNET_YES;
  3289. if (GNUNET_OK !=
  3290. GNUNET_NETWORK_socket_bind (udp_sock,
  3291. in,
  3292. in_len))
  3293. {
  3294. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
  3295. "bind",
  3296. bindto);
  3297. GNUNET_NETWORK_socket_close (udp_sock);
  3298. udp_sock = NULL;
  3299. GNUNET_free (in);
  3300. GNUNET_free (bindto);
  3301. return;
  3302. }
  3303. /* We might have bound to port 0, allowing the OS to figure it out;
  3304. thus, get the real IN-address from the socket */
  3305. sto_len = sizeof(in_sto);
  3306. if (0 != getsockname (GNUNET_NETWORK_get_fd (udp_sock),
  3307. (struct sockaddr *) &in_sto,
  3308. &sto_len))
  3309. {
  3310. memcpy (&in_sto, in, in_len);
  3311. sto_len = in_len;
  3312. }
  3313. GNUNET_free (in);
  3314. GNUNET_free (bindto);
  3315. in = (struct sockaddr *) &in_sto;
  3316. in_len = sto_len;
  3317. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  3318. "Bound to `%s'\n",
  3319. GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
  3320. switch (in->sa_family)
  3321. {
  3322. case AF_INET:
  3323. my_port = ntohs (((struct sockaddr_in *) in)->sin_port);
  3324. break;
  3325. case AF_INET6:
  3326. my_port = ntohs (((struct sockaddr_in6 *) in)->sin6_port);
  3327. break;
  3328. default:
  3329. GNUNET_break (0);
  3330. my_port = 0;
  3331. }
  3332. stats = GNUNET_STATISTICS_create ("C-UDP", cfg);
  3333. senders = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES);
  3334. receivers = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES);
  3335. senders_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
  3336. receivers_heap =
  3337. GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
  3338. key_cache = GNUNET_CONTAINER_multishortmap_create (1024, GNUNET_YES);
  3339. GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
  3340. is = GNUNET_NT_scanner_init ();
  3341. my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
  3342. if (NULL == my_private_key)
  3343. {
  3344. GNUNET_log (
  3345. GNUNET_ERROR_TYPE_ERROR,
  3346. _ (
  3347. "Transport service is lacking key configuration settings. Exiting.\n"));
  3348. GNUNET_SCHEDULER_shutdown ();
  3349. return;
  3350. }
  3351. GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_identity.public_key);
  3352. /* start reading */
  3353. read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
  3354. udp_sock,
  3355. &sock_read,
  3356. NULL);
  3357. ch = GNUNET_TRANSPORT_communicator_connect (cfg,
  3358. COMMUNICATOR_CONFIG_SECTION,
  3359. COMMUNICATOR_ADDRESS_PREFIX,
  3360. GNUNET_TRANSPORT_CC_UNRELIABLE,
  3361. &mq_init,
  3362. NULL,
  3363. &enc_notify_cb,
  3364. NULL);
  3365. if (NULL == ch)
  3366. {
  3367. GNUNET_break (0);
  3368. GNUNET_SCHEDULER_shutdown ();
  3369. return;
  3370. }
  3371. ah = GNUNET_TRANSPORT_application_init (cfg);
  3372. if (NULL == ah)
  3373. {
  3374. GNUNET_break (0);
  3375. GNUNET_SCHEDULER_shutdown ();
  3376. return;
  3377. }
  3378. /* start broadcasting */
  3379. if (GNUNET_YES !=
  3380. GNUNET_CONFIGURATION_get_value_yesno (cfg,
  3381. COMMUNICATOR_CONFIG_SECTION,
  3382. "DISABLE_BROADCAST"))
  3383. {
  3384. broadcast_task = GNUNET_SCHEDULER_add_now (&do_broadcast, NULL);
  3385. }
  3386. nat = GNUNET_NAT_register (cfg,
  3387. COMMUNICATOR_CONFIG_SECTION,
  3388. IPPROTO_UDP,
  3389. 1 /* one address */,
  3390. (const struct sockaddr **) &in,
  3391. &in_len,
  3392. &nat_address_cb,
  3393. NULL /* FIXME: support reversal: #5529 */,
  3394. NULL /* closure */);
  3395. }
  3396. /**
  3397. * The main function for the UNIX communicator.
  3398. *
  3399. * @param argc number of arguments from the command line
  3400. * @param argv command line arguments
  3401. * @return 0 ok, 1 on error
  3402. */
  3403. int
  3404. main (int argc, char *const *argv)
  3405. {
  3406. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  3407. GNUNET_GETOPT_OPTION_END
  3408. };
  3409. int ret;
  3410. if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
  3411. return 2;
  3412. ret = (GNUNET_OK == GNUNET_PROGRAM_run (argc,
  3413. argv,
  3414. "gnunet-communicator-udp",
  3415. _ ("GNUnet UDP communicator"),
  3416. options,
  3417. &run,
  3418. NULL))
  3419. ? 0
  3420. : 1;
  3421. GNUNET_free_nz ((void *) argv);
  3422. return ret;
  3423. }
  3424. /* end of gnunet-communicator-udp.c */