1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862 |
- /*
- This file is part of GNUnet
- Copyright (C) 2010-2014, 2018, 2019 GNUnet e.V.
- GNUnet is free software: you can redistribute it and/or modify it
- under the terms of the GNU Affero General Public License as published
- by the Free Software Foundation, either version 3 of the License,
- or (at your option) any later version.
- GNUnet is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Affero General Public License for more details.
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- SPDX-License-Identifier: AGPL3.0-or-later
- */
- /**
- * @file transport/gnunet-communicator-udp.c
- * @brief Transport plugin using UDP.
- * @author Christian Grothoff
- *
- * TODO:
- * - consider imposing transmission limits in the absence
- * of ACKs; or: maybe this should be done at TNG service level?
- * (at least the receiver might want to enforce limits on
- * KX/DH operations per sender in here) (#5552)
- * - overall, we should look more into flow control support
- * (either in backchannel, or general solution in TNG service)
- * - handle addresses discovered from broadcasts (#5551)
- * (think: what was the story again on address validation?
- * where is the API for that!?!)
- * - support DNS names in BINDTO option (#5528)
- * - support NAT connection reversal method (#5529)
- * - support other UDP-specific NAT traversal methods (#)
- */
- #include "platform.h"
- #include "gnunet_util_lib.h"
- #include "gnunet_protocols.h"
- #include "gnunet_signatures.h"
- #include "gnunet_constants.h"
- #include "gnunet_nt_lib.h"
- #include "gnunet_nat_service.h"
- #include "gnunet_statistics_service.h"
- #include "gnunet_transport_application_service.h"
- #include "gnunet_transport_communication_service.h"
- /**
- * How often do we rekey based on time (at least)
- */
- #define DEFAULT_REKEY_TIME_INTERVAL GNUNET_TIME_UNIT_DAYS
- /**
- * How long do we wait until we must have received the initial KX?
- */
- #define PROTO_QUEUE_TIMEOUT GNUNET_TIME_UNIT_MINUTES
- /**
- * How often do we broadcast our presence on the LAN?
- */
- #define BROADCAST_FREQUENCY GNUNET_TIME_UNIT_MINUTES
- /**
- * How often do we scan for changes to our network interfaces?
- */
- #define INTERFACE_SCAN_FREQUENCY \
- GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
- /**
- * How long do we believe our addresses to remain up (before
- * the other peer should revalidate).
- */
- #define ADDRESS_VALIDITY_PERIOD GNUNET_TIME_UNIT_HOURS
- #define WORKING_QUEUE_INTERVALL \
- GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MICROSECONDS,1)
- /**
- * AES key size.
- */
- #define AES_KEY_SIZE (256 / 8)
- /**
- * AES (GCM) IV size.
- */
- #define AES_IV_SIZE (96 / 8)
- /**
- * Size of the GCM tag.
- */
- #define GCM_TAG_SIZE (128 / 8)
- #define GENERATE_AT_ONCE 2
- /**
- * If we fall below this number of available KCNs,
- * we generate additional ACKs until we reach
- * #KCN_TARGET.
- * Should be large enough that we don't generate ACKs all
- * the time and still have enough time for the ACK to
- * arrive before the sender runs out. So really this
- * should ideally be based on the RTT.
- */
- #define KCN_THRESHOLD 92
- /**
- * How many KCNs do we keep around *after* we hit
- * the #KCN_THRESHOLD? Should be larger than
- * #KCN_THRESHOLD so we do not generate just one
- * ACK at the time.
- */
- #define KCN_TARGET 128
- /**
- * What is the maximum delta between KCN sequence numbers
- * that we allow. Used to expire 'ancient' KCNs that likely
- * were dropped by the network. Must be larger than
- * KCN_TARGET (otherwise we generate new KCNs all the time),
- * but not too large (otherwise packet loss may cause
- * sender to fall back to KX needlessly when sender runs
- * out of ACK'ed KCNs due to losses).
- */
- #define MAX_SQN_DELTA 160
- /**
- * How many shared master secrets do we keep around
- * at most per sender? Should be large enough so
- * that we generally have a chance of sending an ACK
- * before the sender already rotated out the master
- * secret. Generally values around #KCN_TARGET make
- * sense. Might make sense to adapt to RTT if we had
- * a good measurement...
- */
- #define MAX_SECRETS 128000
- /**
- * Default value for how often we do rekey based on number of bytes transmitted?
- * (additionally randomized).
- */
- #define DEFAULT_REKEY_MAX_BYTES (1024LLU * 1024 * 1024 * 4LLU)
- /**
- * Address prefix used by the communicator.
- */
- #define COMMUNICATOR_ADDRESS_PREFIX "udp"
- /**
- * Configuration section used by the communicator.
- */
- #define COMMUNICATOR_CONFIG_SECTION "communicator-udp"
- GNUNET_NETWORK_STRUCT_BEGIN
- /**
- * Signature we use to verify that the ephemeral key was really chosen by
- * the specified sender. If possible, the receiver should respond with
- * a `struct UDPAck` (possibly via backchannel).
- */
- struct UdpHandshakeSignature
- {
- /**
- * Purpose must be #GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE
- */
- struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
- /**
- * Identity of the inititor of the UDP connection (UDP client).
- */
- struct GNUNET_PeerIdentity sender;
- /**
- * Presumed identity of the target of the UDP connection (UDP server)
- */
- struct GNUNET_PeerIdentity receiver;
- /**
- * Ephemeral key used by the @e sender.
- */
- struct GNUNET_CRYPTO_EcdhePublicKey ephemeral;
- /**
- * Monotonic time of @e sender, to possibly help detect replay attacks
- * (if receiver persists times by sender).
- */
- struct GNUNET_TIME_AbsoluteNBO monotonic_time;
- };
- /**
- * "Plaintext" header at beginning of KX message. Followed
- * by encrypted `struct UDPConfirmation`.
- */
- struct InitialKX
- {
- /**
- * Ephemeral key for KX.
- */
- struct GNUNET_CRYPTO_EcdhePublicKey ephemeral;
- /**
- * HMAC for the following encrypted message, using GCM. HMAC uses
- * key derived from the handshake with sequence number zero.
- */
- char gcm_tag[GCM_TAG_SIZE];
- /**
- * A flag indicating, if the sender is doing rekeying.
- */
- int rekeying;
- };
- /**
- * Encrypted continuation of UDP initial handshake, followed
- * by message header with payload.
- */
- struct UDPConfirmation
- {
- /**
- * Sender's identity
- */
- struct GNUNET_PeerIdentity sender;
- /**
- * Sender's signature of type #GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE
- */
- struct GNUNET_CRYPTO_EddsaSignature sender_sig;
- /**
- * Monotonic time of @e sender, to possibly help detect replay attacks
- * (if receiver persists times by sender).
- */
- struct GNUNET_TIME_AbsoluteNBO monotonic_time;
- /* followed by messages */
- /* padding may follow actual messages */
- };
- /**
- * UDP key acknowledgement. May be sent via backchannel. Allows the
- * sender to use `struct UDPBox` with the acknowledge key henceforth.
- */
- struct UDPAck
- {
- /**
- * Type is #GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK.
- */
- struct GNUNET_MessageHeader header;
- /**
- * Sequence acknowledgement limit. Specifies current maximum sequence
- * number supported by receiver.
- */
- uint32_t sequence_max GNUNET_PACKED;
- /**
- * Sequence acknowledgement limit. Specifies current maximum sequence
- * number supported by receiver.
- */
- uint32_t acks_available GNUNET_PACKED;
- /**
- * CMAC of the base key being acknowledged.
- */
- struct GNUNET_HashCode cmac;
- };
- /**
- * Signature we use to verify that the broadcast was really made by
- * the peer that claims to have made it. Basically, affirms that the
- * peer is really using this IP address (albeit possibly not in _our_
- * LAN). Makes it difficult for peers in the LAN to claim to
- * be just any global peer -- an attacker must have at least
- * shared a LAN with the peer they're pretending to be here.
- */
- struct UdpBroadcastSignature
- {
- /**
- * Purpose must be #GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST
- */
- struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
- /**
- * Identity of the inititor of the UDP broadcast.
- */
- struct GNUNET_PeerIdentity sender;
- /**
- * Hash of the sender's UDP address.
- */
- struct GNUNET_HashCode h_address;
- };
- /**
- * Broadcast by peer in LAN announcing its presence. Unusual in that
- * we don't pad these to full MTU, as we cannot prevent being
- * recognized in LAN as GNUnet peers if this feature is enabled
- * anyway. Also, the entire message is in cleartext.
- */
- struct UDPBroadcast
- {
- /**
- * Sender's peer identity.
- */
- struct GNUNET_PeerIdentity sender;
- /**
- * Sender's signature of type
- * #GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST
- */
- struct GNUNET_CRYPTO_EddsaSignature sender_sig;
- };
- /**
- * UDP message box. Always sent encrypted, only allowed after
- * the receiver sent a `struct UDPAck` for the base key!
- */
- struct UDPBox
- {
- /**
- * Key and IV identification code. KDF applied to an acknowledged
- * base key and a sequence number. Sequence numbers must be used
- * monotonically increasing up to the maximum specified in
- * `struct UDPAck`. Without further `struct UDPAck`s, the sender
- * must fall back to sending handshakes!
- */
- struct GNUNET_ShortHashCode kid;
- /**
- * 128-bit authentication tag for the following encrypted message,
- * from GCM. MAC starts at the @e body_start that follows and
- * extends until the end of the UDP payload. If the @e hmac is
- * wrong, the receiver should check if the message might be a
- * `struct UdpHandshakeSignature`.
- */
- char gcm_tag[GCM_TAG_SIZE];
- /**
- * A flag indicating, if the sender is doing rekeying.
- */
- int rekeying;
- };
- /**
- * UDP message box. Always sent encrypted, only allowed after
- * the receiver sent a `struct UDPAck` for the base key!
- */
- struct UDPRekey
- {
- /**
- * Key and IV identification code. KDF applied to an acknowledged
- * base key and a sequence number. Sequence numbers must be used
- * monotonically increasing up to the maximum specified in
- * `struct UDPAck`. Without further `struct UDPAck`s, the sender
- * must fall back to sending handshakes!
- */
- struct GNUNET_ShortHashCode kid;
- /**
- * 128-bit authentication tag for the following encrypted message,
- * from GCM. MAC starts at the @e body_start that follows and
- * extends until the end of the UDP payload. If the @e hmac is
- * wrong, the receiver should check if the message might be a
- * `struct UdpHandshakeSignature`.
- */
- char gcm_tag[GCM_TAG_SIZE];
- /**
- * Sender's identity
- */
- struct GNUNET_PeerIdentity sender;
- };
- GNUNET_NETWORK_STRUCT_END
- /**
- * Shared secret we generated for a particular sender or receiver.
- */
- struct SharedSecret;
- /**
- * Pre-generated "kid" code (key and IV identification code) to
- * quickly derive master key for a `struct UDPBox`.
- */
- struct KeyCacheEntry
- {
- /**
- * Kept in a DLL.
- */
- struct KeyCacheEntry *next;
- /**
- * Kept in a DLL.
- */
- struct KeyCacheEntry *prev;
- /**
- * Key and IV identification code. KDF applied to an acknowledged
- * base key and a sequence number. Sequence numbers must be used
- * monotonically increasing up to the maximum specified in
- * `struct UDPAck`. Without further `struct UDPAck`s, the sender
- * must fall back to sending handshakes!
- */
- struct GNUNET_ShortHashCode kid;
- /**
- * Corresponding shared secret.
- */
- struct SharedSecret *ss;
- /**
- * Sequence number used to derive this entry from master key.
- */
- uint32_t sequence_number;
- };
- /**
- * Information we track per sender address we have recently been
- * in contact with (decryption from sender).
- */
- struct SenderAddress;
- /**
- * Information we track per receiving address we have recently been
- * in contact with (encryption to receiver).
- */
- struct ReceiverAddress;
- /**
- * Shared secret we generated for a particular sender or receiver.
- */
- struct SharedSecret
- {
- /**
- * Kept in a DLL.
- */
- struct SharedSecret *next;
- /**
- * Kept in a DLL.
- */
- struct SharedSecret *prev;
- /**
- * Kept in a DLL, sorted by sequence number. Only if we are decrypting.
- */
- struct KeyCacheEntry *kce_head;
- /**
- * Kept in a DLL, sorted by sequence number. Only if we are decrypting.
- */
- struct KeyCacheEntry *kce_tail;
- /**
- * Sender we use this shared secret with, or NULL.
- */
- struct SenderAddress *sender;
- /**
- * Receiver we use this shared secret with, or NULL.
- */
- struct ReceiverAddress *receiver;
- /**
- * Master shared secret.
- */
- struct GNUNET_HashCode master;
- /**
- * CMAC is used to identify @e master in ACKs.
- */
- struct GNUNET_HashCode cmac;
- /**
- * Up to which sequence number did we use this @e master already?
- * (for encrypting only)
- */
- uint32_t sequence_used;
- /**
- * Up to which sequence number did the other peer allow us to use
- * this key, or up to which number did we allow the other peer to
- * use this key?
- */
- uint32_t sequence_allowed;
- /**
- * Number of active KCN entries.
- */
- unsigned int active_kce_count;
- };
- /**
- * Information we track per sender address we have recently been
- * in contact with (we decrypt messages from the sender).
- */
- struct SenderAddress
- {
- /**
- * Shared secret we use with @e target for rekeying.
- */
- struct SharedSecret *ss_rekey;
- /**
- * Flag indicating sender is initiated rekeying for this receiver.
- */
- int rekeying;
- /**
- * To whom are we talking to.
- */
- struct GNUNET_PeerIdentity target;
- /**
- * Entry in sender expiration heap.
- */
- struct GNUNET_CONTAINER_HeapNode *hn;
- /**
- * Shared secrets we used with @e target, first used is head.
- */
- struct SharedSecret *ss_head;
- /**
- * Shared secrets we used with @e target, last used is tail.
- */
- struct SharedSecret *ss_tail;
- /**
- * Address of the other peer.
- */
- struct sockaddr *address;
- /**
- * Length of the address.
- */
- socklen_t address_len;
- /**
- * Timeout for this sender.
- */
- struct GNUNET_TIME_Absolute timeout;
- /**
- * Length of the DLL at @a ss_head.
- */
- unsigned int num_secrets;
- /**
- * Number of BOX keys from ACKs we have currently
- * available for this sender.
- */
- unsigned int acks_available;
- /**
- * Which network type does this queue use?
- */
- enum GNUNET_NetworkType nt;
- /**
- * sender_destroy already called on sender.
- */
- int sender_destroy_called;
- };
- /**
- * Information we track per receiving address we have recently been
- * in contact with (encryption to receiver).
- */
- struct ReceiverAddress
- {
- /**
- * Shared secret we use with @e target for rekeying.
- */
- struct SharedSecret *ss_rekey;
- /**
- * Acks available when we started rekeying.
- */
- unsigned int rekey_acks_available;
- /**
- * Send bytes for this receiver address.
- */
- uint64_t rekey_send_bytes;
- /**
- * Timeout for this receiver address.
- */
- struct GNUNET_TIME_Absolute rekey_timeout;
- /**
- * Flag indicating sender is initiated rekeying for this receiver.
- */
- int rekeying;
- /**
- * Numer of kce we retain for sending the rekeying shared secret.
- */
- int number_rekeying_kce;
- /**
- * To whom are we talking to.
- */
- struct GNUNET_PeerIdentity target;
- /**
- * Shared secrets we received from @e target, first used is head.
- */
- struct SharedSecret *ss_head;
- /**
- * Shared secrets we received with @e target, last used is tail.
- */
- struct SharedSecret *ss_tail;
- /**
- * Address of the receiver in the human-readable format
- * with the #COMMUNICATOR_ADDRESS_PREFIX.
- */
- char *foreign_addr;
- /**
- * Address of the other peer.
- */
- struct sockaddr *address;
- /**
- * Length of the address.
- */
- socklen_t address_len;
- /**
- * Entry in sender expiration heap.
- */
- struct GNUNET_CONTAINER_HeapNode *hn;
- /**
- * KX message queue we are providing for the #ch.
- */
- struct GNUNET_MQ_Handle *kx_mq;
- /**
- * Default message queue we are providing for the #ch.
- */
- struct GNUNET_MQ_Handle *d_mq;
- /**
- * handle for KX queue with the #ch.
- */
- struct GNUNET_TRANSPORT_QueueHandle *kx_qh;
- /**
- * handle for default queue with the #ch.
- */
- struct GNUNET_TRANSPORT_QueueHandle *d_qh;
- /**
- * Timeout for this receiver address.
- */
- struct GNUNET_TIME_Absolute timeout;
- /**
- * MTU we allowed transport for this receiver's KX queue.
- */
- size_t kx_mtu;
- /**
- * MTU we allowed transport for this receiver's default queue.
- */
- size_t d_mtu;
- /**
- * Length of the DLL at @a ss_head.
- */
- unsigned int num_secrets;
- /**
- * Number of BOX keys from ACKs we have currently
- * available for this receiver.
- */
- unsigned int acks_available;
- /**
- * Which network type does this queue use?
- */
- enum GNUNET_NetworkType nt;
- /**
- * receiver_destroy already called on receiver.
- */
- int receiver_destroy_called;
- };
- /**
- * Interface we broadcast our presence on.
- */
- struct BroadcastInterface
- {
- /**
- * Kept in a DLL.
- */
- struct BroadcastInterface *next;
- /**
- * Kept in a DLL.
- */
- struct BroadcastInterface *prev;
- /**
- * Task for this broadcast interface.
- */
- struct GNUNET_SCHEDULER_Task *broadcast_task;
- /**
- * Sender's address of the interface.
- */
- struct sockaddr *sa;
- /**
- * Broadcast address to use on the interface.
- */
- struct sockaddr *ba;
- /**
- * Message we broadcast on this interface.
- */
- struct UDPBroadcast bcm;
- /**
- * If this is an IPv6 interface, this is the request
- * we use to join/leave the group.
- */
- struct ipv6_mreq mcreq;
- /**
- * Number of bytes in @e sa.
- */
- socklen_t salen;
- /**
- * Was this interface found in the last #iface_proc() scan?
- */
- int found;
- };
- /**
- * The rekey interval
- */
- static struct GNUNET_TIME_Relative rekey_interval;
- /**
- * How often we do rekey based on number of bytes transmitted
- */
- static unsigned long long rekey_max_bytes;
- /**
- * Shared secret we finished the last kce working queue for.
- */
- struct SharedSecret *ss_finished;
- /**
- * Cache of pre-generated key IDs.
- */
- static struct GNUNET_CONTAINER_MultiShortmap *key_cache;
- /**
- * ID of read task
- */
- static struct GNUNET_SCHEDULER_Task *read_task;
- /**
- * ID of timeout task
- */
- static struct GNUNET_SCHEDULER_Task *timeout_task;
- /**
- * ID of kce working queue task
- */
- static struct GNUNET_SCHEDULER_Task *kce_task;
- /**
- * ID of kce rekey working queue task
- */
- static struct GNUNET_SCHEDULER_Task *kce_task_rekey;
- /**
- * Is the kce_task finished?
- */
- static int kce_task_finished = GNUNET_NO;
- /**
- * ID of master broadcast task
- */
- static struct GNUNET_SCHEDULER_Task *broadcast_task;
- /**
- * For logging statistics.
- */
- static struct GNUNET_STATISTICS_Handle *stats;
- /**
- * Our environment.
- */
- static struct GNUNET_TRANSPORT_CommunicatorHandle *ch;
- /**
- * Receivers (map from peer identity to `struct ReceiverAddress`)
- */
- static struct GNUNET_CONTAINER_MultiPeerMap *receivers;
- /**
- * Senders (map from peer identity to `struct SenderAddress`)
- */
- static struct GNUNET_CONTAINER_MultiPeerMap *senders;
- /**
- * Expiration heap for senders (contains `struct SenderAddress`)
- */
- static struct GNUNET_CONTAINER_Heap *senders_heap;
- /**
- * Expiration heap for receivers (contains `struct ReceiverAddress`)
- */
- static struct GNUNET_CONTAINER_Heap *receivers_heap;
- /**
- * Broadcast interface tasks. Kept in a DLL.
- */
- static struct BroadcastInterface *bi_head;
- /**
- * Broadcast interface tasks. Kept in a DLL.
- */
- static struct BroadcastInterface *bi_tail;
- /**
- * Our socket.
- */
- static struct GNUNET_NETWORK_Handle *udp_sock;
- /**
- * #GNUNET_YES if #udp_sock supports IPv6.
- */
- static int have_v6_socket;
- /**
- * Our public key.
- */
- static struct GNUNET_PeerIdentity my_identity;
- /**
- * Our private key.
- */
- static struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
- /**
- * Our configuration.
- */
- static const struct GNUNET_CONFIGURATION_Handle *cfg;
- /**
- * Our handle to report addresses for validation to TRANSPORT.
- */
- static struct GNUNET_TRANSPORT_ApplicationHandle *ah;
- /**
- * Network scanner to determine network types.
- */
- static struct GNUNET_NT_InterfaceScanner *is;
- /**
- * Connection to NAT service.
- */
- static struct GNUNET_NAT_Handle *nat;
- /**
- * Port number to which we are actually bound.
- */
- static uint16_t my_port;
- /**
- * An interface went away, stop broadcasting on it.
- *
- * @param bi entity to close down
- */
- static void
- bi_destroy (struct BroadcastInterface *bi)
- {
- if (AF_INET6 == bi->sa->sa_family)
- {
- /* Leave the multicast group */
- if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt (udp_sock,
- IPPROTO_IPV6,
- IPV6_LEAVE_GROUP,
- &bi->mcreq,
- sizeof(bi->mcreq)))
- {
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
- }
- }
- GNUNET_CONTAINER_DLL_remove (bi_head, bi_tail, bi);
- GNUNET_SCHEDULER_cancel (bi->broadcast_task);
- GNUNET_free (bi->sa);
- GNUNET_free (bi->ba);
- GNUNET_free (bi);
- }
- /**
- * Destroys a receiving state due to timeout or shutdown.
- *
- * @param receiver entity to close down
- */
- static void
- receiver_destroy (struct ReceiverAddress *receiver)
- {
- receiver->receiver_destroy_called = GNUNET_YES;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Disconnecting receiver for peer `%s'\n",
- GNUNET_i2s (&receiver->target));
- /*if (NULL != (mq = receiver->kx_mq))
- {
- receiver->kx_mq = NULL;
- GNUNET_MQ_destroy (mq);
- }*/
- if (NULL != receiver->kx_qh)
- {
- GNUNET_TRANSPORT_communicator_mq_del (receiver->kx_qh);
- receiver->kx_qh = NULL;
- receiver->kx_mq = NULL;
- }
- /*if (NULL != (mq = receiver->d_mq))
- {
- receiver->d_mq = NULL;
- GNUNET_MQ_destroy (mq);
- }*/
- if (NULL != receiver->d_qh)
- {
- GNUNET_TRANSPORT_communicator_mq_del (receiver->d_qh);
- receiver->d_qh = NULL;
- }
- GNUNET_assert (GNUNET_YES ==
- GNUNET_CONTAINER_multipeermap_remove (receivers,
- &receiver->target,
- receiver));
- GNUNET_assert (receiver == GNUNET_CONTAINER_heap_remove_node (receiver->hn));
- GNUNET_STATISTICS_set (stats,
- "# receivers active",
- GNUNET_CONTAINER_multipeermap_size (receivers),
- GNUNET_NO);
- GNUNET_free (receiver->address);
- GNUNET_free (receiver->foreign_addr);
- GNUNET_free (receiver);
- }
- /**
- * Free memory used by key cache entry.
- *
- * @param kce the key cache entry
- */
- static void
- kce_destroy (struct KeyCacheEntry *kce)
- {
- struct SharedSecret *ss = kce->ss;
- ss->active_kce_count--;
- ss->sender->acks_available--;
- GNUNET_CONTAINER_DLL_remove (ss->kce_head, ss->kce_tail, kce);
- GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multishortmap_remove (key_cache,
- &kce->kid,
- kce));
- GNUNET_free (kce);
- }
- /**
- * Compute @a kid.
- *
- * @param msec master secret for HMAC calculation
- * @param serial number for the @a smac calculation
- * @param kid[out] where to write the key ID
- */
- static void
- get_kid (const struct GNUNET_HashCode *msec,
- uint32_t serial,
- struct GNUNET_ShortHashCode *kid)
- {
- uint32_t sid = htonl (serial);
- GNUNET_CRYPTO_hkdf (kid,
- sizeof(*kid),
- GCRY_MD_SHA512,
- GCRY_MD_SHA256,
- &sid,
- sizeof(sid),
- msec,
- sizeof(*msec),
- "UDP-KID",
- strlen ("UDP-KID"),
- NULL,
- 0);
- }
- /**
- * Setup key cache entry for sequence number @a seq and shared secret @a ss.
- *
- * @param ss shared secret
- * @param seq sequence number for the key cache entry
- */
- static void
- kce_generate (struct SharedSecret *ss, uint32_t seq)
- {
- struct KeyCacheEntry *kce;
- GNUNET_assert (0 < seq);
- kce = GNUNET_new (struct KeyCacheEntry);
- kce->ss = ss;
- kce->sequence_number = seq;
- get_kid (&ss->master, seq, &kce->kid);
- GNUNET_CONTAINER_DLL_insert (ss->kce_head, ss->kce_tail, kce);
- ss->active_kce_count++;
- ss->sender->acks_available++;
- (void) GNUNET_CONTAINER_multishortmap_put (
- key_cache,
- &kce->kid,
- kce,
- GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
- GNUNET_STATISTICS_set (stats,
- "# KIDs active",
- GNUNET_CONTAINER_multishortmap_size (key_cache),
- GNUNET_NO);
- }
- /**
- * Destroy @a ss and associated key cache entries.
- *
- * @param ss shared secret to destroy
- * @param withoutKce If GNUNET_YES shared secrets with kce will not be destroyed.
- */
- static int
- secret_destroy (struct SharedSecret *ss, int withoutKce)
- {
- struct SenderAddress *sender;
- struct ReceiverAddress *receiver;
- struct KeyCacheEntry *kce;
- if (withoutKce && (ss->sequence_allowed > 0))
- return GNUNET_NO;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "secret %s destroy %u %u\n",
- GNUNET_h2s (&ss->master),
- withoutKce,
- ss->sequence_allowed);
- if (NULL != (sender = ss->sender))
- {
- GNUNET_CONTAINER_DLL_remove (sender->ss_head, sender->ss_tail, ss);
- sender->num_secrets--;
- }
- if (NULL != (receiver = ss->receiver))
- {
- GNUNET_CONTAINER_DLL_remove (receiver->ss_head, receiver->ss_tail, ss);
- receiver->num_secrets--;
- // Uncomment this for alternativ 1 of backchannel functionality
- receiver->acks_available -= (ss->sequence_allowed - ss->sequence_used);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "%u receiver->acks_available 3\n",
- receiver->acks_available);
- // Until here for alternativ 1
- }
- while (NULL != (kce = ss->kce_head))
- kce_destroy (kce);
- GNUNET_STATISTICS_update (stats, "# Secrets active", -1, GNUNET_NO);
- GNUNET_STATISTICS_set (stats,
- "# KIDs active",
- GNUNET_CONTAINER_multishortmap_size (key_cache),
- GNUNET_NO);
- GNUNET_free (ss);
- return GNUNET_YES;
- }
- /**
- * Functions with this signature are called whenever we need
- * to close a sender's state due to timeout.
- *
- * @param sender entity to close down
- */
- static void
- sender_destroy (struct SenderAddress *sender)
- {
- sender->sender_destroy_called = GNUNET_YES;
- GNUNET_assert (
- GNUNET_YES ==
- GNUNET_CONTAINER_multipeermap_remove (senders, &sender->target, sender));
- GNUNET_assert (sender == GNUNET_CONTAINER_heap_remove_node (sender->hn));
- GNUNET_STATISTICS_set (stats,
- "# senders active",
- GNUNET_CONTAINER_multipeermap_size (senders),
- GNUNET_NO);
- GNUNET_free (sender->address);
- GNUNET_free (sender);
- }
- /**
- * Compute @a key and @a iv.
- *
- * @param msec master secret for calculation
- * @param serial number for the @a smac calculation
- * @param key[out] where to write the decrption key
- * @param iv[out] where to write the IV
- */
- static void
- get_iv_key (const struct GNUNET_HashCode *msec,
- uint32_t serial,
- char key[AES_KEY_SIZE],
- char iv[AES_IV_SIZE])
- {
- uint32_t sid = htonl (serial);
- char res[AES_KEY_SIZE + AES_IV_SIZE];
- GNUNET_CRYPTO_hkdf (res,
- sizeof(res),
- GCRY_MD_SHA512,
- GCRY_MD_SHA256,
- &sid,
- sizeof(sid),
- msec,
- sizeof(*msec),
- "UDP-IV-KEY",
- strlen ("UDP-IV-KEY"),
- NULL,
- 0);
- memcpy (key, res, AES_KEY_SIZE);
- memcpy (iv, &res[AES_KEY_SIZE], AES_IV_SIZE);
- }
- /**
- * Increment sender timeout due to activity.
- *
- * @param sender address for which the timeout should be rescheduled
- */
- static void
- reschedule_sender_timeout (struct SenderAddress *sender)
- {
- sender->timeout =
- GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
- GNUNET_CONTAINER_heap_update_cost (sender->hn, sender->timeout.abs_value_us);
- }
- /**
- * Increment receiver timeout due to activity.
- *
- * @param receiver address for which the timeout should be rescheduled
- */
- static void
- reschedule_receiver_timeout (struct ReceiverAddress *receiver)
- {
- receiver->timeout =
- GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
- GNUNET_CONTAINER_heap_update_cost (receiver->hn,
- receiver->timeout.abs_value_us);
- }
- /**
- * Task run to check #receiver_heap and #sender_heap for timeouts.
- *
- * @param cls unused, NULL
- */
- static void
- check_timeouts (void *cls)
- {
- struct GNUNET_TIME_Relative st;
- struct GNUNET_TIME_Relative rt;
- struct GNUNET_TIME_Relative delay;
- struct ReceiverAddress *receiver;
- struct SenderAddress *sender;
- (void) cls;
- timeout_task = NULL;
- rt = GNUNET_TIME_UNIT_FOREVER_REL;
- while (NULL != (receiver = GNUNET_CONTAINER_heap_peek (receivers_heap)))
- {
- /* if (GNUNET_YES != receiver->receiver_destroy_called) */
- /* { */
- rt = GNUNET_TIME_absolute_get_remaining (receiver->timeout);
- if (0 != rt.rel_value_us)
- break;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Receiver timed out\n");
- receiver_destroy (receiver);
- // }
- }
- st = GNUNET_TIME_UNIT_FOREVER_REL;
- while (NULL != (sender = GNUNET_CONTAINER_heap_peek (senders_heap)))
- {
- if (GNUNET_YES != sender->sender_destroy_called)
- {
- st = GNUNET_TIME_absolute_get_remaining (sender->timeout);
- if (0 != st.rel_value_us)
- break;
- sender_destroy (sender);
- }
- }
- delay = GNUNET_TIME_relative_min (rt, st);
- if (delay.rel_value_us < GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
- timeout_task = GNUNET_SCHEDULER_add_delayed (delay, &check_timeouts, NULL);
- }
- /**
- * Calcualte cmac from master in @a ss.
- *
- * @param ss[in,out] data structure to complete
- */
- static void
- calculate_cmac (struct SharedSecret *ss)
- {
- GNUNET_CRYPTO_hkdf (&ss->cmac,
- sizeof(ss->cmac),
- GCRY_MD_SHA512,
- GCRY_MD_SHA256,
- "CMAC",
- strlen ("CMAC"),
- &ss->master,
- sizeof(ss->master),
- "UDP-CMAC",
- strlen ("UDP-CMAC"),
- NULL,
- 0);
- }
- /**
- * We received @a plaintext_len bytes of @a plaintext from a @a sender.
- * Pass it on to CORE.
- *
- * @param queue the queue that received the plaintext
- * @param plaintext the plaintext that was received
- * @param plaintext_len number of bytes of plaintext received
- */
- static void
- pass_plaintext_to_core (struct SenderAddress *sender,
- const void *plaintext,
- size_t plaintext_len)
- {
- const struct GNUNET_MessageHeader *hdr = plaintext;
- const char *pos = plaintext;
- while (ntohs (hdr->size) < plaintext_len)
- {
- GNUNET_STATISTICS_update (stats,
- "# bytes given to core",
- ntohs (hdr->size),
- GNUNET_NO);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Giving %u bytes to TNG\n", ntohs (hdr->size));
- GNUNET_assert (GNUNET_SYSERR !=
- GNUNET_TRANSPORT_communicator_receive (ch,
- &sender->target,
- hdr,
- ADDRESS_VALIDITY_PERIOD,
- NULL /* no flow control possible */
- ,
- NULL));
- /* move on to next message, if any */
- plaintext_len -= ntohs (hdr->size);
- if (plaintext_len < sizeof(*hdr))
- break;
- pos += ntohs (hdr->size);
- hdr = (const struct GNUNET_MessageHeader *) pos;
- // TODO for now..., we do not actually sen >1msg or have a way of telling
- // if we are done
- break;
- }
- GNUNET_STATISTICS_update (stats,
- "# bytes padding discarded",
- plaintext_len,
- GNUNET_NO);
- }
- /**
- * Setup @a cipher based on shared secret @a msec and
- * serial number @a serial.
- *
- * @param msec master shared secret
- * @param serial serial number of cipher to set up
- * @param cipher[out] cipher to initialize
- */
- static void
- setup_cipher (const struct GNUNET_HashCode *msec,
- uint32_t serial,
- gcry_cipher_hd_t *cipher)
- {
- char key[AES_KEY_SIZE];
- char iv[AES_IV_SIZE];
- int rc;
- GNUNET_assert (0 ==
- gcry_cipher_open (cipher,
- GCRY_CIPHER_AES256 /* low level: go for speed */,
- GCRY_CIPHER_MODE_GCM,
- 0 /* flags */));
- get_iv_key (msec, serial, key, iv);
- rc = gcry_cipher_setkey (*cipher, key, sizeof(key));
- GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
- rc = gcry_cipher_setiv (*cipher, iv, sizeof(iv));
- GNUNET_assert ((0 == rc) || ((char) rc == GPG_ERR_WEAK_KEY));
- }
- /**
- * Try to decrypt @a buf using shared secret @a ss and key/iv
- * derived using @a serial.
- *
- * @param ss shared secret
- * @param tag GCM authentication tag
- * @param serial serial number to use
- * @param in_buf input buffer to decrypt
- * @param in_buf_size number of bytes in @a in_buf and available in @a out_buf
- * @param out_buf where to write the result
- * @return #GNUNET_OK on success
- */
- static int
- try_decrypt (const struct SharedSecret *ss,
- const char tag[GCM_TAG_SIZE],
- uint32_t serial,
- const char *in_buf,
- size_t in_buf_size,
- char *out_buf)
- {
- gcry_cipher_hd_t cipher;
- setup_cipher (&ss->master, serial, &cipher);
- GNUNET_assert (
- 0 ==
- gcry_cipher_decrypt (cipher, out_buf, in_buf_size, in_buf, in_buf_size));
- if (0 != gcry_cipher_checktag (cipher, tag, GCM_TAG_SIZE))
- {
- gcry_cipher_close (cipher);
- GNUNET_STATISTICS_update (stats,
- "# AEAD authentication failures",
- 1,
- GNUNET_NO);
- return GNUNET_SYSERR;
- }
- gcry_cipher_close (cipher);
- return GNUNET_OK;
- }
- /**
- * Setup shared secret for decryption.
- *
- * @param ephemeral ephemeral key we received from the other peer
- * @return new shared secret
- */
- static struct SharedSecret *
- setup_shared_secret_dec (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral)
- {
- struct SharedSecret *ss;
- ss = GNUNET_new (struct SharedSecret);
- GNUNET_CRYPTO_eddsa_ecdh (my_private_key, ephemeral, &ss->master);
- return ss;
- }
- /**
- * Setup shared secret for encryption.
- *
- * @param ephemeral ephemeral key we are sending to the other peer
- * @param receiver[in,out] queue to initialize encryption key for
- * @return new shared secret
- */
- static struct SharedSecret *
- setup_shared_secret_enc (const struct GNUNET_CRYPTO_EcdhePrivateKey *ephemeral,
- struct ReceiverAddress *receiver, int add_to_receiver)
- {
- struct SharedSecret *ss;
- ss = GNUNET_new (struct SharedSecret);
- GNUNET_CRYPTO_ecdh_eddsa (ephemeral,
- &receiver->target.public_key,
- &ss->master);
- calculate_cmac (ss);
- ss->receiver = receiver;
- GNUNET_CONTAINER_DLL_insert (receiver->ss_head, receiver->ss_tail, ss);
- receiver->num_secrets++;
- GNUNET_STATISTICS_update (stats, "# Secrets active", 1, GNUNET_NO);
- return ss;
- }
- /**
- * Setup the MQ for the @a receiver. If a queue exists,
- * the existing one is destroyed. Then the MTU is
- * recalculated and a fresh queue is initialized.
- *
- * @param receiver receiver to setup MQ for
- */
- static void
- setup_receiver_mq (struct ReceiverAddress *receiver);
- /**
- * Destroying all secrets. Depending on parameter we keep those secrets having a kce.
- *
- * @param ss The secret we will not destroy.
- * @param withoutKce If GNUNET_YES shared secrets with kce will not be destroyed.
- */
- static void
- destroy_all_secrets (struct SharedSecret *ss, int withoutKce)
- {
- struct SenderAddress *sender;
- struct ReceiverAddress *receiver;
- struct SharedSecret *ss_to_destroy;
- struct SharedSecret *ss_start;
- struct SharedSecret *pos;
- int at_least_one_destroyed = GNUNET_NO;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Starting destroy all withoutKce: %u.\n",
- withoutKce);
- if (NULL != (sender = ss->sender))
- {
- ss_start = sender->ss_head;
- }
- else if (NULL != (receiver = ss->receiver))
- {
- ss_start = receiver->ss_head;
- }
- else
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Shared secret has no sender or receiver!\n");
- return;
- }
- pos = ss_start;
- while (NULL != pos)
- {
- ss_to_destroy = pos;
- pos = pos->next;
- if (ss != ss_to_destroy)
- at_least_one_destroyed = secret_destroy (ss_to_destroy, withoutKce);
- }
- if ((ss != ss_start) && ! at_least_one_destroyed)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Really destroying all.\n");
- destroy_all_secrets (ss_start, GNUNET_NO);
- }
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Finished destroy all.\n");
- }
- static void
- add_acks (struct SharedSecret *ss, int acks_to_add)
- {
- struct ReceiverAddress *receiver = ss->receiver;
- if (NULL == ss)
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "secret NULL!\n");
- if (NULL == receiver)
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Receiver NULL!\n");
- if (NULL == receiver->d_qh)
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Queue NULL!\n");
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Tell transport we have %u more acks!\n",
- acks_to_add);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "%u kce for rekeying.\n",
- receiver->number_rekeying_kce);
- GNUNET_TRANSPORT_communicator_mq_update (ch,
- receiver->d_qh,
- acks_to_add,
- 1);
- // Until here for alternativ 1
- /* move ss to head to avoid discarding it anytime soon! */
- GNUNET_CONTAINER_DLL_remove (receiver->ss_head, receiver->ss_tail, ss);
- GNUNET_CONTAINER_DLL_insert (receiver->ss_head, receiver->ss_tail, ss);
- destroy_all_secrets (ss, GNUNET_YES);
- }
- static uint32_t
- reset_rekey_kces (struct ReceiverAddress *receiver,
- uint32_t acks_to_add)
- {
- int needed_for_rekeying;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "%u kce for rekeying and %u acks_to_add\n",
- receiver->number_rekeying_kce,
- acks_to_add);
- needed_for_rekeying = (3 - receiver->number_rekeying_kce);
- if (acks_to_add <= needed_for_rekeying)
- {
- receiver->number_rekeying_kce += acks_to_add;
- acks_to_add = 0;
- }
- else
- {
- acks_to_add -= (3 - receiver->number_rekeying_kce);
- receiver->number_rekeying_kce = 3;
- }
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "%u kce for rekeying and %u acks_to_add\n",
- receiver->number_rekeying_kce,
- acks_to_add);
- return acks_to_add;
- }
- static void
- add_acks_rekey (struct ReceiverAddress *receiver)
- {
- uint32_t acks_to_add = receiver->ss_rekey->sequence_allowed;
- if (receiver->number_rekeying_kce < 3)
- acks_to_add = reset_rekey_kces (receiver, acks_to_add);
- receiver->acks_available = receiver->ss_rekey->sequence_allowed;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "%u receiver->acks_available 4\n",
- receiver->acks_available);
- /* add_acks (receiver->ss_rekey, acks_to_add - 3); */
- if (0 != acks_to_add)
- {
- add_acks (receiver->ss_rekey, acks_to_add);
- }
- receiver->ss_rekey = NULL;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "# rekeying successful\n");
- GNUNET_STATISTICS_update (stats,
- "# rekeying successful",
- 1,
- GNUNET_NO);
- }
- /**
- * We received an ACK for @a pid. Check if it is for
- * the receiver in @a value and if so, handle it and
- * return #GNUNET_NO. Otherwise, return #GNUNET_YES.
- *
- * @param cls a `const struct UDPAck`
- * @param pid peer the ACK is from
- * @param value a `struct ReceiverAddress`
- * @return #GNUNET_YES to continue to iterate
- */
- static int
- handle_ack (void *cls, const struct GNUNET_PeerIdentity *pid, void *value)
- {
- const struct UDPAck *ack = cls;
- struct ReceiverAddress *receiver = value;
- uint32_t acks_to_add;
- uint32_t allowed;
- // int needed_for_rekeying;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "in handle ack with cmac %s\n",
- GNUNET_h2s (&ack->cmac));
- if (NULL != receiver->ss_rekey)
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "We have rekey secret with cmac %s \n",
- GNUNET_h2s (&receiver->ss_rekey->cmac));
- if ((NULL != receiver->ss_rekey) && (0 == memcmp (&ack->cmac,
- &receiver->ss_rekey->cmac,
- sizeof(struct
- GNUNET_HashCode))) )
- {
- allowed = ntohl (ack->sequence_max);
- if (allowed > receiver->ss_rekey->sequence_allowed)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "%u > %u (%u %u) for rekey secrect %s\n", allowed,
- receiver->ss_rekey->sequence_allowed,
- receiver->acks_available,
- ack->acks_available,
- GNUNET_h2s (&receiver->ss_rekey->master));
- receiver->ss_rekey->sequence_allowed = allowed;
- if (GNUNET_NO == receiver->rekeying)
- add_acks_rekey (receiver);
- return GNUNET_NO;
- }
- }
- (void) pid;
- for (struct SharedSecret *ss = receiver->ss_head; NULL != ss; ss = ss->next)
- {
- if (0 == memcmp (&ack->cmac, &ss->cmac, sizeof(struct GNUNET_HashCode)))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Found matching mac\n");
- allowed = ntohl (ack->sequence_max);
- if (allowed > ss->sequence_allowed)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "%u > %u (%u %u) for secrect %s\n", allowed,
- ss->sequence_allowed,
- receiver->acks_available,
- ack->acks_available,
- GNUNET_h2s (&ss->master));
- // Uncomment this for alternativ 1 of backchannel functionality
- acks_to_add = (allowed - ss->sequence_allowed);
- if ((GNUNET_NO == receiver->rekeying) &&
- (receiver->number_rekeying_kce < 3) )
- acks_to_add = reset_rekey_kces (receiver, acks_to_add);
- /* if ((GNUNET_NO == receiver->rekeying) && */
- /* (receiver->number_rekeying_kce < */
- /* 3) ) */
- /* { */
- /* needed_for_rekeying = (3 - receiver->number_rekeying_kce); */
- /* if (acks_to_add <= needed_for_rekeying) */
- /* { */
- /* receiver->number_rekeying_kce += acks_to_add; */
- /* acks_to_add = 0; */
- /* } */
- /* else */
- /* { */
- /* acks_to_add -= (3 - receiver->number_rekeying_kce); */
- /* receiver->number_rekeying_kce = 3; */
- /* } */
- /* } */
- /* GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, */
- /* "%u kce for rekeying\n", */
- /* receiver->number_rekeying_kce); */
- if ((0 != acks_to_add) && (GNUNET_NO == receiver->rekeying))
- {
- receiver->acks_available += (allowed - ss->sequence_allowed);
- ss->sequence_allowed = allowed;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "%u receiver->acks_available 5\n",
- receiver->acks_available);
- add_acks (ss, acks_to_add);
- }
- }
- return GNUNET_NO;
- }
- }
- return GNUNET_YES;
- }
- /**
- * Test if we have received a valid message in plaintext.
- * If so, handle it.
- *
- * @param sender peer to process inbound plaintext for
- * @param buf buffer we received
- * @param buf_size number of bytes in @a buf
- */
- static void
- try_handle_plaintext (struct SenderAddress *sender,
- const void *buf,
- size_t buf_size)
- {
- const struct GNUNET_MessageHeader *hdr =
- (const struct GNUNET_MessageHeader *) buf;
- const struct UDPAck *ack = (const struct UDPAck *) buf;
- uint16_t type;
- if (sizeof(*hdr) > buf_size)
- return; /* not even a header */
- if (ntohs (hdr->size) > buf_size)
- return; /* not even a header */
- type = ntohs (hdr->type);
- switch (type)
- {
- case GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK:
- /* lookup master secret by 'cmac', then update sequence_max */
- GNUNET_CONTAINER_multipeermap_get_multiple (receivers,
- &sender->target,
- &handle_ack,
- (void *) ack);
- /* There could be more messages after the ACK, handle those as well */
- buf += ntohs (hdr->size);
- buf_size -= ntohs (hdr->size);
- pass_plaintext_to_core (sender, buf, buf_size);
- break;
- case GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_PAD:
- /* skip padding */
- break;
- default:
- pass_plaintext_to_core (sender, buf, buf_size);
- }
- }
- static void
- kce_generate_cb (void *cls)
- {
- struct SharedSecret *ss = cls;
- kce_task = NULL;
- if (((GNUNET_NO == ss->sender->rekeying) && (ss->sender->acks_available <
- KCN_TARGET) ) ||
- ((ss->sender->ss_rekey == ss) && (GNUNET_YES == ss->sender->rekeying) &&
- (ss->sender->acks_available < 128)))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Precomputing keys for master %s\n",
- GNUNET_h2s (&(ss->master)));
- for (int i = 0; i < GENERATE_AT_ONCE; i++)
- kce_generate (ss, ++ss->sequence_allowed);
- kce_task = GNUNET_SCHEDULER_add_delayed (WORKING_QUEUE_INTERVALL,
- kce_generate_cb,
- ss);
- }
- else
- {
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "We have enough keys.\n");
- ss_finished = ss;
- kce_task_finished = GNUNET_YES;
- }
- }
- static void
- kce_generate_rekey_cb (void *cls)
- {
- struct SharedSecret *ss = cls;
- kce_task_rekey = NULL;
- if (NULL == kce_task)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Precomputing keys for rekey master %s\n",
- GNUNET_h2s (&(ss->master)));
- for (int i = 0; i < GENERATE_AT_ONCE; i++)
- kce_generate (ss, ++ss->sequence_allowed);
- kce_task = GNUNET_SCHEDULER_add_delayed (WORKING_QUEUE_INTERVALL,
- kce_generate_cb,
- ss);
- kce_task_rekey = NULL;
- }
- else
- {
- kce_task_rekey = GNUNET_SCHEDULER_add_delayed (WORKING_QUEUE_INTERVALL,
- kce_generate_rekey_cb,
- ss);
- }
- }
- /**
- * We established a shared secret with a sender. We should try to send
- * the sender an `struct UDPAck` at the next opportunity to allow the
- * sender to use @a ss longer (assuming we did not yet already
- * recently).
- *
- * @param ss shared secret to generate ACKs for
- * @param intial The SharedSecret came with initial KX.
- */
- static void
- consider_ss_ack (struct SharedSecret *ss, int initial)
- {
- GNUNET_assert (NULL != ss->sender);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Considering SS UDPAck %s\n",
- GNUNET_i2s_full (&ss->sender->target));
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "We have %u acks available.\n",
- ss->sender->acks_available);
- /* drop ancient KeyCacheEntries */
- while ((NULL != ss->kce_head) &&
- (MAX_SQN_DELTA <
- ss->kce_head->sequence_number - ss->kce_tail->sequence_number))
- kce_destroy (ss->kce_tail);
- if (GNUNET_NO == initial)
- kce_generate (ss, ++ss->sequence_allowed);
- /*if (0 == ss->sender->acks_available)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Generating keys\n");
- while (ss->active_kce_count < KCN_TARGET)
- kce_generate (ss, ++ss->sequence_allowed);
- }*/
- if (((NULL != kce_task) && kce_task_finished) || (GNUNET_NO == initial))
- {
- struct UDPAck ack;
- struct SharedSecret *ss_tell;
- if (GNUNET_NO != initial)
- ss_tell = ss_finished;
- else
- ss_tell = ss;
- ack.header.type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK);
- ack.header.size = htons (sizeof(ack));
- ack.sequence_max = htonl (ss_tell->sequence_allowed);
- ack.acks_available = ss->sender->acks_available;
- ack.cmac = ss_tell->cmac;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Notifying transport of UDPAck %s with intial %u and master %s\n",
- GNUNET_i2s_full (&ss_tell->sender->target),
- initial,
- GNUNET_h2s (&(ss_tell->master)));
- GNUNET_TRANSPORT_communicator_notify (ch,
- &ss_tell->sender->target,
- COMMUNICATOR_ADDRESS_PREFIX,
- &ack.header);
- if (GNUNET_NO != initial)
- {
- destroy_all_secrets (ss, GNUNET_YES);
- kce_task = NULL;
- kce_task_finished = GNUNET_NO;
- }
- }
- else if ((NULL == kce_task) && ((KCN_THRESHOLD >
- ss->sender->acks_available) ||
- (GNUNET_YES == ss->sender->rekeying) ||
- (ss->sender->num_secrets > MAX_SECRETS) ))
- {
- // kce_generate (ss, ++ss->sequence_allowed);
- // kce_generate (ss, ++ss->sequence_allowed);
- kce_task = GNUNET_SCHEDULER_add_delayed (WORKING_QUEUE_INTERVALL,
- kce_generate_cb,
- ss);
- }
- else if ((NULL == kce_task_rekey) && (GNUNET_YES ==
- ss->sender->rekeying) )
- {
- kce_task_rekey = GNUNET_SCHEDULER_add_delayed (WORKING_QUEUE_INTERVALL,
- kce_generate_rekey_cb,
- ss);
- }
- }
- /**
- * We received a @a box with matching @a kce. Decrypt and process it.
- *
- * @param box the data we received
- * @param box_len number of bytes in @a box
- * @param kce key index to decrypt @a box
- */
- static void
- decrypt_box (const struct UDPBox *box,
- size_t box_len,
- struct KeyCacheEntry *kce)
- {
- struct SharedSecret *ss = kce->ss;
- char out_buf[box_len - sizeof(*box)];
- GNUNET_assert (NULL != ss->sender);
- if (GNUNET_OK != try_decrypt (ss,
- box->gcm_tag,
- kce->sequence_number,
- (const char *) &box[1],
- sizeof(out_buf),
- out_buf))
- {
- GNUNET_STATISTICS_update (stats,
- "# Decryption failures with valid KCE",
- 1,
- GNUNET_NO);
- kce_destroy (kce);
- return;
- }
- kce_destroy (kce);
- GNUNET_STATISTICS_update (stats,
- "# bytes decrypted with BOX",
- sizeof(out_buf),
- GNUNET_NO);
- GNUNET_STATISTICS_update (stats,
- "# messages decrypted with BOX",
- 1,
- GNUNET_NO);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "decrypted UDPBox with kid %s\n",
- GNUNET_sh2s (&box->kid));
- try_handle_plaintext (ss->sender, out_buf, sizeof(out_buf));
- if ((GNUNET_NO == box->rekeying) && (GNUNET_YES == ss->sender->rekeying))
- {
- ss->sender->rekeying = GNUNET_NO;
- ss->sender->ss_rekey = NULL;
- // destroy_all_secrets (ss, GNUNET_NO);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Receiver stopped rekeying.\n");
- }
- else if (GNUNET_NO == box->rekeying)
- consider_ss_ack (ss, GNUNET_NO);
- else
- {
- ss->sender->rekeying = GNUNET_YES;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Got Box: Receiver doing rekeying.\n");
- }
- }
- /**
- * We received a @a rekey with matching @a kce. Decrypt and process it.
- *
- * @param rekey the data we received
- * @param rekey_len number of bytes in @a rekey
- * @param kce key index to decrypt @a rekey
- */
- static void
- decrypt_rekey (const struct UDPRekey *rekey,
- size_t rekey_len,
- struct KeyCacheEntry *kce,
- struct SenderAddress *sender)
- {
- struct SharedSecret *ss = kce->ss;
- struct SharedSecret *ss_rekey;
- char out_buf[rekey_len - sizeof(*rekey)];
- struct GNUNET_HashCode *master;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "decrypt_rekey.\n");
- GNUNET_assert (NULL != ss->sender);
- if (GNUNET_OK != try_decrypt (ss,
- rekey->gcm_tag,
- kce->sequence_number,
- (const char *) &rekey[1],
- sizeof(out_buf),
- out_buf))
- {
- GNUNET_STATISTICS_update (stats,
- "# Decryption failures with valid KCE",
- 1,
- GNUNET_NO);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Decryption with kid %s failed\n",
- GNUNET_sh2s (&rekey->kid));
- kce_destroy (kce);
- return;
- }
- kce_destroy (kce);
- GNUNET_STATISTICS_update (stats,
- "# bytes decrypted with Rekey",
- sizeof(out_buf),
- GNUNET_NO);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "decrypted UDPRekey with kid %s\n",
- GNUNET_sh2s (&rekey->kid));
- /*cmac = (struct GNUNET_HashCode *) out_buf;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Received secret with cmac %s \n",
- GNUNET_h2s (&cmac));*/
- // ss_rekey = (struct SharedSecret *) out_buf;
- master = (struct GNUNET_HashCode *) out_buf;
- ss_rekey = GNUNET_new (struct SharedSecret);
- ss_rekey->master = *master;
- calculate_cmac (ss_rekey);
- ss_rekey->sender = sender;
- // ss_rekey->sequence_used = 0;
- // ss_rekey->sequence_allowed = 0;
- /* ss_rekey->active_kce_count = 0; */
- /* ss_rekey->prev = NULL; */
- /* ss_rekey->next = NULL; */
- /* GNUNET_assert (ss_rekey->prev == NULL && sender->ss_head != ss_rekey); */
- /* GNUNET_assert (ss_rekey->next == NULL && sender->ss_tail != ss_rekey); */
- GNUNET_CONTAINER_DLL_insert (sender->ss_head, sender->ss_tail, ss_rekey);
- sender->ss_rekey = ss_rekey;
- sender->num_secrets++;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Received secret with cmac %s\n",
- GNUNET_h2s (&(ss_rekey->cmac)));
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Received secret with master %s.\n",
- GNUNET_h2s (&(ss_rekey->master)));
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "We have %u sequence_allowed.\n",
- ss_rekey->sequence_allowed);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "We have a sender %p\n",
- ss_rekey->sender);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "We have %u acks available.\n",
- ss_rekey->sender->acks_available);
- consider_ss_ack (ss_rekey, GNUNET_YES);
- }
- /**
- * Closure for #find_sender_by_address()
- */
- struct SearchContext
- {
- /**
- * Address we are looking for.
- */
- const struct sockaddr *address;
- /**
- * Number of bytes in @e address.
- */
- socklen_t address_len;
- /**
- * Return value to set if we found a match.
- */
- struct SenderAddress *sender;
- };
- /**
- * Find existing `struct SenderAddress` by matching addresses.
- *
- * @param cls a `struct SearchContext`
- * @param key ignored, must match already
- * @param value a `struct SenderAddress`
- * @return #GNUNET_YES if not found (continue to search), #GNUNET_NO if found
- */
- static int
- find_sender_by_address (void *cls,
- const struct GNUNET_PeerIdentity *key,
- void *value)
- {
- struct SearchContext *sc = cls;
- struct SenderAddress *sender = value;
- if ((sender->address_len == sc->address_len) &&
- (0 == memcmp (sender->address, sc->address, sender->address_len)))
- {
- sc->sender = sender;
- return GNUNET_NO; /* stop iterating! */
- }
- return GNUNET_YES;
- }
- /**
- * Create sender address for @a target. Note that we
- * might already have one, so a fresh one is only allocated
- * if one does not yet exist for @a address.
- *
- * @param target peer to generate address for
- * @param address target address
- * @param address_len number of bytes in @a address
- * @return data structure to keep track of key material for
- * decrypting data from @a target
- */
- static struct SenderAddress *
- setup_sender (const struct GNUNET_PeerIdentity *target,
- const struct sockaddr *address,
- socklen_t address_len)
- {
- struct SenderAddress *sender;
- struct SearchContext sc = { .address = address,
- .address_len = address_len,
- .sender = NULL };
- GNUNET_CONTAINER_multipeermap_get_multiple (senders,
- target,
- &find_sender_by_address,
- &sc);
- if (NULL != sc.sender)
- {
- reschedule_sender_timeout (sc.sender);
- return sc.sender;
- }
- sender = GNUNET_new (struct SenderAddress);
- sender->target = *target;
- sender->address = GNUNET_memdup (address, address_len);
- sender->address_len = address_len;
- (void) GNUNET_CONTAINER_multipeermap_put (
- senders,
- &sender->target,
- sender,
- GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
- GNUNET_STATISTICS_set (stats,
- "# senders active",
- GNUNET_CONTAINER_multipeermap_size (receivers),
- GNUNET_NO);
- sender->timeout =
- GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
- sender->hn = GNUNET_CONTAINER_heap_insert (senders_heap,
- sender,
- sender->timeout.abs_value_us);
- sender->nt = GNUNET_NT_scanner_get_type (is, address, address_len);
- if (NULL == timeout_task)
- timeout_task = GNUNET_SCHEDULER_add_now (&check_timeouts, NULL);
- return sender;
- }
- /**
- * Check signature from @a uc against @a ephemeral.
- *
- * @param ephermal key that is signed
- * @param uc signature of claimant
- * @return #GNUNET_OK if signature is valid
- */
- static int
- verify_confirmation (const struct GNUNET_CRYPTO_EcdhePublicKey *ephemeral,
- const struct UDPConfirmation *uc)
- {
- struct UdpHandshakeSignature uhs;
- uhs.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE);
- uhs.purpose.size = htonl (sizeof(uhs));
- uhs.sender = uc->sender;
- uhs.receiver = my_identity;
- uhs.ephemeral = *ephemeral;
- uhs.monotonic_time = uc->monotonic_time;
- return GNUNET_CRYPTO_eddsa_verify (
- GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE,
- &uhs,
- &uc->sender_sig,
- &uc->sender.public_key);
- }
- /**
- * Converts @a address to the address string format used by this
- * communicator in HELLOs.
- *
- * @param address the address to convert, must be AF_INET or AF_INET6.
- * @param address_len number of bytes in @a address
- * @return string representation of @a address
- */
- static char *
- sockaddr_to_udpaddr_string (const struct sockaddr *address,
- socklen_t address_len)
- {
- char *ret;
- switch (address->sa_family)
- {
- case AF_INET:
- GNUNET_asprintf (&ret,
- "%s-%s",
- COMMUNICATOR_ADDRESS_PREFIX,
- GNUNET_a2s (address, address_len));
- break;
- case AF_INET6:
- GNUNET_asprintf (&ret,
- "%s-%s",
- COMMUNICATOR_ADDRESS_PREFIX,
- GNUNET_a2s (address, address_len));
- break;
- default:
- GNUNET_assert (0);
- }
- return ret;
- }
- /**
- * Socket read task.
- *
- * @param cls NULL
- */
- static void
- sock_read (void *cls)
- {
- struct sockaddr_storage sa;
- socklen_t salen = sizeof(sa);
- char buf[UINT16_MAX];
- ssize_t rcvd;
- (void) cls;
- read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
- udp_sock,
- &sock_read,
- NULL);
- rcvd = GNUNET_NETWORK_socket_recvfrom (udp_sock,
- buf,
- sizeof(buf),
- (struct sockaddr *) &sa,
- &salen);
- if (-1 == rcvd)
- {
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_DEBUG, "recv");
- return;
- }
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Read %lu bytes\n", rcvd);
- if (rcvd > sizeof(struct UDPRekey))
- {
- const struct UDPRekey *rekey;
- const struct UDPBox *box;
- struct KeyCacheEntry *kce;
- struct SenderAddress *sender;
- int do_decrypt = GNUNET_NO;
- rekey = (const struct UDPRekey *) buf;
- box = (const struct UDPBox *) buf;
- kce = GNUNET_CONTAINER_multishortmap_get (key_cache, &rekey->kid);
- if ((GNUNET_YES == box->rekeying) || (GNUNET_NO == box->rekeying))
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "UDPRekey has rekeying %u\n",
- box->rekeying);
- else
- do_decrypt = GNUNET_YES;
- if ((GNUNET_YES == do_decrypt) && (NULL != kce) && (GNUNET_YES ==
- kce->ss->sender->
- rekeying))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "UDPRekey with kid %s\n",
- GNUNET_sh2s (&rekey->kid));
- sender = setup_sender (&rekey->sender, (const struct sockaddr *) &sa,
- salen);
- if (NULL != sender->ss_rekey)
- return;
- decrypt_rekey (rekey, (size_t) rcvd, kce, sender);
- return;
- }
- }
- /* first, see if it is a UDPBox */
- if (rcvd > sizeof(struct UDPBox))
- {
- const struct UDPBox *box;
- struct KeyCacheEntry *kce;
- box = (const struct UDPBox *) buf;
- kce = GNUNET_CONTAINER_multishortmap_get (key_cache, &box->kid);
- if (NULL != kce)
- {
- decrypt_box (box, (size_t) rcvd, kce);
- return;
- }
- }
- /* next, check if it is a broadcast */
- if (sizeof(struct UDPBroadcast) == rcvd)
- {
- const struct UDPBroadcast *ub;
- struct UdpBroadcastSignature uhs;
- ub = (const struct UDPBroadcast *) buf;
- uhs.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST);
- uhs.purpose.size = htonl (sizeof(uhs));
- uhs.sender = ub->sender;
- GNUNET_CRYPTO_hash (&sa, salen, &uhs.h_address);
- if (GNUNET_OK ==
- GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST,
- &uhs,
- &ub->sender_sig,
- &ub->sender.public_key))
- {
- char *addr_s;
- enum GNUNET_NetworkType nt;
- addr_s =
- sockaddr_to_udpaddr_string ((const struct sockaddr *) &sa, salen);
- GNUNET_STATISTICS_update (stats, "# broadcasts received", 1, GNUNET_NO);
- /* use our own mechanism to determine network type */
- nt =
- GNUNET_NT_scanner_get_type (is, (const struct sockaddr *) &sa, salen);
- GNUNET_TRANSPORT_application_validate (ah, &ub->sender, nt, addr_s);
- GNUNET_free (addr_s);
- return;
- }
- /* continue with KX, mostly for statistics... */
- }
- /* finally, test if it is a KX */
- if (rcvd < sizeof(struct UDPConfirmation) + sizeof(struct InitialKX))
- {
- GNUNET_STATISTICS_update (stats,
- "# messages dropped (no kid, too small for KX)",
- 1,
- GNUNET_NO);
- return;
- }
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Got KX\n");
- {
- const struct InitialKX *kx;
- struct SharedSecret *ss;
- char pbuf[rcvd - sizeof(struct InitialKX)];
- const struct UDPConfirmation *uc;
- struct SenderAddress *sender;
- kx = (const struct InitialKX *) buf;
- ss = setup_shared_secret_dec (&kx->ephemeral);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Before DEC\n");
- if (GNUNET_OK != try_decrypt (ss,
- kx->gcm_tag,
- 0,
- &buf[sizeof(*kx)],
- sizeof(pbuf),
- pbuf))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Unable to decrypt tag, dropping...\n");
- GNUNET_free (ss);
- GNUNET_STATISTICS_update (
- stats,
- "# messages dropped (no kid, AEAD decryption failed)",
- 1,
- GNUNET_NO);
- return;
- }
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Before VERIFY\n");
- uc = (const struct UDPConfirmation *) pbuf;
- if (GNUNET_OK != verify_confirmation (&kx->ephemeral, uc))
- {
- GNUNET_break_op (0);
- GNUNET_free (ss);
- GNUNET_STATISTICS_update (stats,
- "# messages dropped (sender signature invalid)",
- 1,
- GNUNET_NO);
- return;
- }
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Before SETUP_SENDER\n");
- calculate_cmac (ss);
- sender = setup_sender (&uc->sender, (const struct sockaddr *) &sa, salen);
- ss->sender = sender;
- GNUNET_CONTAINER_DLL_insert (sender->ss_head, sender->ss_tail, ss);
- sender->num_secrets++;
- GNUNET_STATISTICS_update (stats, "# Secrets active", 1, GNUNET_NO);
- GNUNET_STATISTICS_update (stats,
- "# messages decrypted without BOX",
- 1,
- GNUNET_NO);
- try_handle_plaintext (sender, &uc[1], sizeof(pbuf) - sizeof(*uc));
- if ((GNUNET_NO == kx->rekeying) && (GNUNET_YES == ss->sender->rekeying))
- {
- ss->sender->rekeying = GNUNET_NO;
- sender->ss_rekey = NULL;
- // destroy_all_secrets (ss, GNUNET_NO);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Receiver stopped rekeying.\n");
- }
- else if (GNUNET_NO == kx->rekeying)
- consider_ss_ack (ss, GNUNET_YES);
- else
- {
- ss->sender->rekeying = GNUNET_YES;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Got KX: Receiver doing rekeying.\n");
- }
- /*if (sender->num_secrets > MAX_SECRETS)
- secret_destroy (sender->ss_tail);*/
- }
- }
- /**
- * Convert UDP bind specification to a `struct sockaddr *`
- *
- * @param bindto bind specification to convert
- * @param[out] sock_len set to the length of the address
- * @return converted bindto specification
- */
- static struct sockaddr *
- udp_address_to_sockaddr (const char *bindto, socklen_t *sock_len)
- {
- struct sockaddr *in;
- unsigned int port;
- char dummy[2];
- char *colon;
- char *cp;
- if (1 == sscanf (bindto, "%u%1s", &port, dummy))
- {
- /* interpreting value as just a PORT number */
- if (port > UINT16_MAX)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "BINDTO specification `%s' invalid: value too large for port\n",
- bindto);
- return NULL;
- }
- if ((GNUNET_NO == GNUNET_NETWORK_test_pf (PF_INET6)) ||
- (GNUNET_YES ==
- GNUNET_CONFIGURATION_get_value_yesno (cfg,
- COMMUNICATOR_CONFIG_SECTION,
- "DISABLE_V6")))
- {
- struct sockaddr_in *i4;
- i4 = GNUNET_malloc (sizeof(struct sockaddr_in));
- i4->sin_family = AF_INET;
- i4->sin_port = htons ((uint16_t) port);
- *sock_len = sizeof(struct sockaddr_in);
- in = (struct sockaddr *) i4;
- }
- else
- {
- struct sockaddr_in6 *i6;
- i6 = GNUNET_malloc (sizeof(struct sockaddr_in6));
- i6->sin6_family = AF_INET6;
- i6->sin6_port = htons ((uint16_t) port);
- *sock_len = sizeof(struct sockaddr_in6);
- in = (struct sockaddr *) i6;
- }
- return in;
- }
- cp = GNUNET_strdup (bindto);
- colon = strrchr (cp, ':');
- if (NULL != colon)
- {
- /* interpet value after colon as port */
- *colon = '\0';
- colon++;
- if (1 == sscanf (colon, "%u%1s", &port, dummy))
- {
- /* interpreting value as just a PORT number */
- if (port > UINT16_MAX)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "BINDTO specification `%s' invalid: value too large for port\n",
- bindto);
- GNUNET_free (cp);
- return NULL;
- }
- }
- else
- {
- GNUNET_log (
- GNUNET_ERROR_TYPE_ERROR,
- "BINDTO specification `%s' invalid: last ':' not followed by number\n",
- bindto);
- GNUNET_free (cp);
- return NULL;
- }
- }
- else
- {
- /* interpret missing port as 0, aka pick any free one */
- port = 0;
- }
- {
- /* try IPv4 */
- struct sockaddr_in v4;
- if (1 == inet_pton (AF_INET, cp, &v4.sin_addr))
- {
- v4.sin_family = AF_INET;
- v4.sin_port = htons ((uint16_t) port);
- #if HAVE_SOCKADDR_IN_SIN_LEN
- v4.sin_len = sizeof(struct sockaddr_in);
- #endif
- in = GNUNET_memdup (&v4, sizeof(struct sockaddr_in));
- *sock_len = sizeof(struct sockaddr_in);
- GNUNET_free (cp);
- return in;
- }
- }
- {
- /* try IPv6 */
- struct sockaddr_in6 v6;
- const char *start;
- start = cp;
- if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
- {
- start++; /* skip over '[' */
- cp[strlen (cp) - 1] = '\0'; /* eat ']' */
- }
- if (1 == inet_pton (AF_INET6, start, &v6.sin6_addr))
- {
- v6.sin6_family = AF_INET6;
- v6.sin6_port = htons ((uint16_t) port);
- #if HAVE_SOCKADDR_IN_SIN_LEN
- v6.sin6_len = sizeof(sizeof(struct sockaddr_in6));
- #endif
- in = GNUNET_memdup (&v6, sizeof(v6));
- *sock_len = sizeof(v6);
- GNUNET_free (cp);
- return in;
- }
- }
- /* #5528 FIXME (feature!): maybe also try getnameinfo()? */
- GNUNET_free (cp);
- return NULL;
- }
- /**
- * Pad @a dgram by @a pad_size using @a out_cipher.
- *
- * @param out_cipher cipher to use
- * @param dgram datagram to pad
- * @param pad_size number of bytes of padding to append
- */
- static void
- do_pad (gcry_cipher_hd_t out_cipher, char *dgram, size_t pad_size)
- {
- char pad[pad_size];
- GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, pad, sizeof(pad));
- if (sizeof(pad) > sizeof(struct GNUNET_MessageHeader))
- {
- struct GNUNET_MessageHeader hdr =
- { .size = htons (sizeof(pad)),
- .type = htons (GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_PAD) };
- memcpy (pad, &hdr, sizeof(hdr));
- }
- GNUNET_assert (
- 0 ==
- gcry_cipher_encrypt (out_cipher, dgram, sizeof(pad), pad, sizeof(pad)));
- }
- /**
- * Signature of functions implementing the sending functionality of a
- * message queue.
- *
- * @param mq the message queue
- * @param msg the message to send
- * @param impl_state our `struct ReceiverAddress`
- */
- static void
- mq_send_kx (struct GNUNET_MQ_Handle *mq,
- const struct GNUNET_MessageHeader *msg,
- void *impl_state)
- {
- struct ReceiverAddress *receiver = impl_state;
- uint16_t msize = ntohs (msg->size);
- struct UdpHandshakeSignature uhs;
- struct UDPConfirmation uc;
- struct InitialKX kx;
- struct GNUNET_CRYPTO_EcdhePrivateKey epriv;
- char dgram[receiver->kx_mtu + sizeof(uc) + sizeof(kx)];
- size_t dpos;
- gcry_cipher_hd_t out_cipher;
- struct SharedSecret *ss;
- GNUNET_assert (mq == receiver->kx_mq);
- if (msize > receiver->kx_mtu)
- {
- GNUNET_break (0);
- if (GNUNET_YES != receiver->receiver_destroy_called)
- receiver_destroy (receiver);
- return;
- }
- reschedule_receiver_timeout (receiver);
- /* setup key material */
- GNUNET_CRYPTO_ecdhe_key_create (&epriv);
- ss = setup_shared_secret_enc (&epriv, receiver, GNUNET_YES);
- if (receiver->num_secrets > MAX_SECRETS)
- {
- destroy_all_secrets (ss, GNUNET_YES);
- }
- setup_cipher (&ss->master, 0, &out_cipher);
- /* compute 'uc' */
- uc.sender = my_identity;
- uc.monotonic_time =
- GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get_monotonic (cfg));
- uhs.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_UDP_HANDSHAKE);
- uhs.purpose.size = htonl (sizeof(uhs));
- uhs.sender = my_identity;
- uhs.receiver = receiver->target;
- GNUNET_CRYPTO_ecdhe_key_get_public (&epriv, &uhs.ephemeral);
- uhs.monotonic_time = uc.monotonic_time;
- GNUNET_CRYPTO_eddsa_sign (my_private_key,
- &uhs,
- &uc.sender_sig);
- /* Leave space for kx */
- dpos = sizeof(kx);
- /* Append encrypted uc to dgram */
- GNUNET_assert (0 == gcry_cipher_encrypt (out_cipher,
- &dgram[dpos],
- sizeof(uc),
- &uc,
- sizeof(uc)));
- dpos += sizeof(uc);
- /* Append encrypted payload to dgram */
- GNUNET_assert (
- 0 == gcry_cipher_encrypt (out_cipher, &dgram[dpos], msize, msg, msize));
- dpos += msize;
- do_pad (out_cipher, &dgram[dpos], sizeof(dgram) - dpos);
- /* Datagram starts with kx */
- kx.ephemeral = uhs.ephemeral;
- GNUNET_assert (
- 0 == gcry_cipher_gettag (out_cipher, kx.gcm_tag, sizeof(kx.gcm_tag)));
- gcry_cipher_close (out_cipher);
- if (GNUNET_NO == receiver->rekeying)
- kx.rekeying = GNUNET_NO;
- else
- kx.rekeying = GNUNET_YES;
- memcpy (dgram, &kx, sizeof(kx));
- if (-1 == GNUNET_NETWORK_socket_sendto (udp_sock,
- dgram,
- sizeof(dgram),
- receiver->address,
- receiver->address_len))
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "send");
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Sending KX to %s\n", GNUNET_a2s (receiver->address,
- receiver->address_len));
- GNUNET_MQ_impl_send_continue (mq);
- }
- static void
- check_for_rekeying (struct ReceiverAddress *receiver, struct UDPBox *box)
- {
- struct GNUNET_TIME_Relative rt;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Timeout is %lu\n.",
- receiver->rekey_timeout.abs_value_us);
- if (0 == receiver->rekey_timeout.abs_value_us)
- {
- receiver->rekey_timeout = GNUNET_TIME_relative_to_absolute (
- rekey_interval);
- }
- else
- {
- rt = GNUNET_TIME_absolute_get_remaining (receiver->rekey_timeout);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Relative time is %lu and timeout is %lu\n.",
- rt.rel_value_us,
- receiver->rekey_timeout.abs_value_us);
- if ((0 == rt.rel_value_us) || (receiver->rekey_send_bytes >
- rekey_max_bytes) )
- {
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Bytes send %lu greater than %llu max bytes\n.",
- receiver->rekey_send_bytes,
- rekey_max_bytes);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Relative time is %lu and timeout is %lu\n.",
- rt.rel_value_us,
- receiver->rekey_timeout.abs_value_us);
- receiver->rekey_timeout.abs_value_us = 0;
- receiver->rekey_send_bytes = 0;
- receiver->ss_rekey = NULL;
- // destroy_all_secrets (ss, GNUNET_NO);
- receiver->rekeying = GNUNET_YES;
- receiver->rekey_acks_available = receiver->acks_available;
- box->rekeying = GNUNET_YES;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Sender started rekeying.\n");
- if (GNUNET_YES == box->rekeying)
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Sending rekeying with kid %s\n",
- GNUNET_sh2s (&box->kid));
- }
- }
- }
- static void
- send_UDPRekey (struct ReceiverAddress *receiver, struct SharedSecret *ss)
- {
- uint8_t is_ss_rekey_sequence_allowed_zero = GNUNET_NO;
- uint8_t is_acks_available_below = GNUNET_NO;
- uint8_t send_rekey = GNUNET_NO;
- uint16_t not_below;
- struct GNUNET_CRYPTO_EcdhePrivateKey epriv;
- struct UDPRekey *rekey;
- size_t dpos;
- char rekey_dgram[sizeof(struct UDPRekey) + receiver->d_mtu];
- if (NULL != receiver->ss_rekey)
- {
- not_below = (receiver->rekey_acks_available
- - (receiver->rekey_acks_available % 3)) / 3;
- is_ss_rekey_sequence_allowed_zero = (0 ==
- receiver->ss_rekey->sequence_allowed);
- is_acks_available_below = (receiver->acks_available >= not_below);
- send_rekey = (0 == (receiver->acks_available - not_below) % not_below) &&
- is_acks_available_below && is_ss_rekey_sequence_allowed_zero;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "send_rekey: %u, %u, %u\n",
- send_rekey,
- receiver->rekey_acks_available,
- receiver->acks_available);
- }
- else if (NULL == receiver->ss_rekey)
- {
- /* setup key material */
- GNUNET_CRYPTO_ecdhe_key_create (&epriv);
- receiver->ss_rekey = setup_shared_secret_enc (&epriv, receiver,
- GNUNET_NO);
- receiver->ss_rekey->sequence_allowed = 0;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Setup secret with cmac %s\n",
- GNUNET_h2s (&(receiver->ss_rekey->cmac)));
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Setup secret with master %s.\n",
- GNUNET_h2s (&(receiver->ss_rekey->master)));
- }
- if (send_rekey)
- {
- GNUNET_assert (0 != receiver->number_rekeying_kce);
- gcry_cipher_hd_t rekey_out_cipher;
- while (NULL != ss && ss->sequence_used >= ss->sequence_allowed)
- {
- ss = ss->prev;
- }
- if (NULL != ss)
- {
- rekey = (struct UDPRekey *) rekey_dgram;
- rekey->sender = my_identity;
- ss->sequence_used++;
- get_kid (&ss->master, ss->sequence_used, &rekey->kid);
- receiver->number_rekeying_kce--;
- setup_cipher (&ss->master, ss->sequence_used, &rekey_out_cipher);
- /* Append encrypted payload to dgram */
- dpos = sizeof(struct UDPRekey);
- GNUNET_assert (
- 0 == gcry_cipher_encrypt (rekey_out_cipher, &rekey_dgram[dpos],
- sizeof(receiver->ss_rekey->master),
- &(receiver->ss_rekey->master),
- sizeof(receiver->ss_rekey->master)));
- dpos += sizeof(receiver->ss_rekey->master);
- /* GNUNET_assert ( */
- /* 0 == gcry_cipher_encrypt (rekey_out_cipher, &rekey_dgram[dpos], */
- /* /\*sizeof(receiver->ss_rekey->cmac), */
- /* &(receiver->ss_rekey->cmac), */
- /* sizeof(receiver->ss_rekey->cmac))); */
- /* dpos += sizeof(receiver->ss_rekey->cmac);*\/ */
- /* sizeof(receiver->ss_rekey), */
- /* receiver->ss_rekey, */
- /* sizeof(receiver->ss_rekey))); */
- /* dpos += sizeof(receiver->ss_rekey); */
- do_pad (rekey_out_cipher, &rekey_dgram[dpos], sizeof(rekey_dgram)
- - dpos);
- GNUNET_assert (0 == gcry_cipher_gettag (rekey_out_cipher,
- rekey->gcm_tag,
- sizeof(rekey->gcm_tag)));
- gcry_cipher_close (rekey_out_cipher);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Sending rekey with kid %s and master %s\n",
- GNUNET_sh2s (&rekey->kid),
- GNUNET_h2s (&(receiver->ss_rekey->master)));
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Sending rekey with cmac %s\n",
- GNUNET_h2s (&(receiver->ss_rekey->cmac)));
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "%u rekey kces left.\n",
- receiver->number_rekeying_kce);
- if (-1 == GNUNET_NETWORK_socket_sendto (udp_sock,
- rekey_dgram,
- sizeof(rekey_dgram),
- receiver->address,
- receiver->address_len))
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "send");
- receiver->acks_available--;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "%u receiver->acks_available 1\n",
- receiver->acks_available);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Sending UDPRekey to %s\n", GNUNET_a2s (receiver->address,
- receiver->
- address_len));
- }
- }
- }
- /**
- * Signature of functions implementing the sending functionality of a
- * message queue.
- *
- * @param mq the message queue
- * @param msg the message to send
- * @param impl_state our `struct ReceiverAddress`
- */
- static void
- mq_send_d (struct GNUNET_MQ_Handle *mq,
- const struct GNUNET_MessageHeader *msg,
- void *impl_state)
- {
- struct ReceiverAddress *receiver = impl_state;
- uint16_t msize = ntohs (msg->size);
- GNUNET_assert (mq == receiver->d_mq);
- if ((msize > receiver->d_mtu) ||
- (0 == receiver->acks_available))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "msize: %u, mtu: %lu, acks: %u\n",
- msize,
- receiver->d_mtu,
- receiver->acks_available);
- GNUNET_break (0);
- if (GNUNET_YES != receiver->receiver_destroy_called)
- receiver_destroy (receiver);
- return;
- }
- reschedule_receiver_timeout (receiver);
- /* begin "BOX" encryption method, scan for ACKs from tail! */
- for (struct SharedSecret *ss = receiver->ss_tail; NULL != ss; ss = ss->prev)
- {
- if (0 < ss->sequence_used)
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Trying to send UDPBox with shared secrect %s sequence_used %u and ss->sequence_allowed %u\n",
- GNUNET_h2s (&ss->master),
- ss->sequence_used,
- ss->sequence_allowed);
- // Uncomment this for alternativ 1 of backchannel functionality
- if (ss->sequence_used >= ss->sequence_allowed)
- // Until here for alternativ 1
- // Uncomment this for alternativ 2 of backchannel functionality
- // if (0 == ss->sequence_allowed)
- // Until here for alternativ 2
- {
- continue;
- }
- char dgram[sizeof(struct UDPBox) + receiver->d_mtu];
- struct UDPBox *box;
- gcry_cipher_hd_t out_cipher;
- size_t dpos;
- box = (struct UDPBox *) dgram;
- ss->sequence_used++;
- get_kid (&ss->master, ss->sequence_used, &box->kid);
- setup_cipher (&ss->master, ss->sequence_used, &out_cipher);
- /* Append encrypted payload to dgram */
- dpos = sizeof(struct UDPBox);
- GNUNET_assert (
- 0 == gcry_cipher_encrypt (out_cipher, &dgram[dpos], msize, msg, msize));
- dpos += msize;
- do_pad (out_cipher, &dgram[dpos], sizeof(dgram) - dpos);
- GNUNET_assert (0 == gcry_cipher_gettag (out_cipher,
- box->gcm_tag,
- sizeof(box->gcm_tag)));
- gcry_cipher_close (out_cipher);
- receiver->rekey_send_bytes += sizeof(struct UDPBox) + receiver->d_mtu;
- if (GNUNET_NO == receiver->rekeying)
- box->rekeying = GNUNET_NO;
- else
- box->rekeying = GNUNET_YES;
- if (-1 == GNUNET_NETWORK_socket_sendto (udp_sock,
- dgram,
- sizeof(dgram),
- receiver->address,
- receiver->address_len))
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "send");
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Sending UDPBox %u acks left\n",
- receiver->acks_available);
- GNUNET_MQ_impl_send_continue (mq);
- receiver->acks_available--;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "%u receiver->acks_available 2\n",
- receiver->acks_available);
- check_for_rekeying (receiver, box);
- if (0 == receiver->acks_available - receiver->number_rekeying_kce)
- {
- /* We have no more ACKs */
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "No more acks\n");
- if (GNUNET_YES == receiver->rekeying)
- {
- receiver->rekeying = GNUNET_NO;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Sender stopped rekeying\n");
- if ((NULL != receiver->ss_rekey) && (0 <
- receiver->ss_rekey->
- sequence_allowed) )
- add_acks_rekey (receiver);
- }
- }
- else if ((GNUNET_YES == receiver->rekeying) )
- {
- send_UDPRekey (receiver, ss);
- }
- return;
- }
- }
- /**
- * Signature of functions implementing the destruction of a message
- * queue. Implementations must not free @a mq, but should take care
- * of @a impl_state.
- *
- * @param mq the message queue to destroy
- * @param impl_state our `struct ReceiverAddress`
- */
- static void
- mq_destroy_d (struct GNUNET_MQ_Handle *mq, void *impl_state)
- {
- struct ReceiverAddress *receiver = impl_state;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Default MQ destroyed\n");
- if (mq == receiver->d_mq)
- {
- receiver->d_mq = NULL;
- if (GNUNET_YES != receiver->receiver_destroy_called)
- receiver_destroy (receiver);
- }
- }
- /**
- * Signature of functions implementing the destruction of a message
- * queue. Implementations must not free @a mq, but should take care
- * of @a impl_state.
- *
- * @param mq the message queue to destroy
- * @param impl_state our `struct ReceiverAddress`
- */
- static void
- mq_destroy_kx (struct GNUNET_MQ_Handle *mq, void *impl_state)
- {
- struct ReceiverAddress *receiver = impl_state;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "KX MQ destroyed\n");
- if (mq == receiver->kx_mq)
- {
- receiver->kx_mq = NULL;
- if (GNUNET_YES != receiver->receiver_destroy_called)
- receiver_destroy (receiver);
- }
- }
- /**
- * Implementation function that cancels the currently sent message.
- *
- * @param mq message queue
- * @param impl_state our `struct RecvierAddress`
- */
- static void
- mq_cancel (struct GNUNET_MQ_Handle *mq, void *impl_state)
- {
- /* Cancellation is impossible with UDP; bail */
- GNUNET_assert (0);
- }
- /**
- * Generic error handler, called with the appropriate
- * error code and the same closure specified at the creation of
- * the message queue.
- * Not every message queue implementation supports an error handler.
- *
- * @param cls our `struct ReceiverAddress`
- * @param error error code
- */
- static void
- mq_error (void *cls, enum GNUNET_MQ_Error error)
- {
- struct ReceiverAddress *receiver = cls;
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "MQ error in queue to %s: %d\n",
- GNUNET_i2s (&receiver->target),
- (int) error);
- receiver_destroy (receiver);
- }
- /**
- * Setup the MQ for the @a receiver. If a queue exists,
- * the existing one is destroyed. Then the MTU is
- * recalculated and a fresh queue is initialized.
- *
- * @param receiver receiver to setup MQ for
- */
- static void
- setup_receiver_mq (struct ReceiverAddress *receiver)
- {
- size_t base_mtu;
- /*if (NULL != receiver->kx_qh)
- {
- GNUNET_TRANSPORT_communicator_mq_del (receiver->kx_qh);
- receiver->kx_qh = NULL;
- }
- if (NULL != receiver->d_qh)
- {
- GNUNET_TRANSPORT_communicator_mq_del (receiver->d_qh);
- receiver->d_qh = NULL;
- }*/
- // GNUNET_assert (NULL == receiver->mq);
- switch (receiver->address->sa_family)
- {
- case AF_INET:
- base_mtu = 1480 /* Ethernet MTU, 1500 - Ethernet header - VLAN tag */
- - sizeof(struct GNUNET_TUN_IPv4Header) /* 20 */
- - sizeof(struct GNUNET_TUN_UdpHeader) /* 8 */;
- break;
- case AF_INET6:
- base_mtu = 1280 /* Minimum MTU required by IPv6 */
- - sizeof(struct GNUNET_TUN_IPv6Header) /* 40 */
- - sizeof(struct GNUNET_TUN_UdpHeader) /* 8 */;
- break;
- default:
- GNUNET_assert (0);
- break;
- }
- /* MTU based on full KX messages */
- receiver->kx_mtu = base_mtu - sizeof(struct InitialKX) /* 48 */
- - sizeof(struct UDPConfirmation); /* 104 */
- /* MTU based on BOXed messages */
- receiver->d_mtu = base_mtu - sizeof(struct UDPBox);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Setting up MQs and QHs\n");
- /* => Effective MTU for CORE will range from 1080 (IPv6 + KX) to
- 1404 (IPv4 + Box) bytes, depending on circumstances... */
- if (NULL == receiver->kx_mq)
- receiver->kx_mq = GNUNET_MQ_queue_for_callbacks (&mq_send_kx,
- &mq_destroy_kx,
- &mq_cancel,
- receiver,
- NULL,
- &mq_error,
- receiver);
- if (NULL == receiver->d_mq)
- receiver->d_mq = GNUNET_MQ_queue_for_callbacks (&mq_send_d,
- &mq_destroy_d,
- &mq_cancel,
- receiver,
- NULL,
- &mq_error,
- receiver);
- receiver->kx_qh =
- GNUNET_TRANSPORT_communicator_mq_add (ch,
- &receiver->target,
- receiver->foreign_addr,
- receiver->kx_mtu,
- GNUNET_TRANSPORT_QUEUE_LENGTH_UNLIMITED,
- 0, /* Priority */
- receiver->nt,
- GNUNET_TRANSPORT_CS_OUTBOUND,
- receiver->kx_mq);
- receiver->d_qh =
- GNUNET_TRANSPORT_communicator_mq_add (ch,
- &receiver->target,
- receiver->foreign_addr,
- receiver->d_mtu,
- 0, /* Initialize with 0 acks */
- 1, /* Priority */
- receiver->nt,
- GNUNET_TRANSPORT_CS_OUTBOUND,
- receiver->d_mq);
- }
- /**
- * Function called by the transport service to initialize a
- * message queue given address information about another peer.
- * If and when the communication channel is established, the
- * communicator must call #GNUNET_TRANSPORT_communicator_mq_add()
- * to notify the service that the channel is now up. It is
- * the responsibility of the communicator to manage sane
- * retries and timeouts for any @a peer/@a address combination
- * provided by the transport service. Timeouts and retries
- * do not need to be signalled to the transport service.
- *
- * @param cls closure
- * @param peer identity of the other peer
- * @param address where to send the message, human-readable
- * communicator-specific format, 0-terminated, UTF-8
- * @return #GNUNET_OK on success, #GNUNET_SYSERR if the provided address is
- * invalid
- */
- static int
- mq_init (void *cls, const struct GNUNET_PeerIdentity *peer, const char *address)
- {
- struct ReceiverAddress *receiver;
- const char *path;
- struct sockaddr *in;
- socklen_t in_len;
- if (0 != strncmp (address,
- COMMUNICATOR_ADDRESS_PREFIX "-",
- strlen (COMMUNICATOR_ADDRESS_PREFIX "-")))
- {
- GNUNET_break_op (0);
- return GNUNET_SYSERR;
- }
- path = &address[strlen (COMMUNICATOR_ADDRESS_PREFIX "-")];
- in = udp_address_to_sockaddr (path, &in_len);
- receiver = GNUNET_new (struct ReceiverAddress);
- receiver->address = in;
- receiver->address_len = in_len;
- receiver->target = *peer;
- receiver->nt = GNUNET_NT_scanner_get_type (is, in, in_len);
- (void) GNUNET_CONTAINER_multipeermap_put (
- receivers,
- &receiver->target,
- receiver,
- GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Added %s to receivers\n",
- GNUNET_i2s_full (&receiver->target));
- receiver->timeout =
- GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
- receiver->hn = GNUNET_CONTAINER_heap_insert (receivers_heap,
- receiver,
- receiver->timeout.abs_value_us);
- GNUNET_STATISTICS_set (stats,
- "# receivers active",
- GNUNET_CONTAINER_multipeermap_size (receivers),
- GNUNET_NO);
- receiver->foreign_addr =
- sockaddr_to_udpaddr_string (receiver->address, receiver->address_len);
- setup_receiver_mq (receiver);
- if (NULL == timeout_task)
- timeout_task = GNUNET_SCHEDULER_add_now (&check_timeouts, NULL);
- return GNUNET_OK;
- }
- /**
- * Iterator over all receivers to clean up.
- *
- * @param cls NULL
- * @param target unused
- * @param value the queue to destroy
- * @return #GNUNET_OK to continue to iterate
- */
- static int
- get_receiver_delete_it (void *cls,
- const struct GNUNET_PeerIdentity *target,
- void *value)
- {
- struct ReceiverAddress *receiver = value;
- (void) cls;
- (void) target;
- receiver_destroy (receiver);
- return GNUNET_OK;
- }
- /**
- * Iterator over all senders to clean up.
- *
- * @param cls NULL
- * @param target unused
- * @param value the queue to destroy
- * @return #GNUNET_OK to continue to iterate
- */
- static int
- get_sender_delete_it (void *cls,
- const struct GNUNET_PeerIdentity *target,
- void *value)
- {
- struct SenderAddress *sender = value;
- (void) cls;
- (void) target;
- sender_destroy (sender);
- return GNUNET_OK;
- }
- /**
- * Shutdown the UNIX communicator.
- *
- * @param cls NULL (always)
- */
- static void
- do_shutdown (void *cls)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "do_shutdown\n");
- if (NULL != nat)
- {
- GNUNET_NAT_unregister (nat);
- nat = NULL;
- }
- while (NULL != bi_head)
- bi_destroy (bi_head);
- if (NULL != broadcast_task)
- {
- GNUNET_SCHEDULER_cancel (broadcast_task);
- broadcast_task = NULL;
- }
- if (NULL != kce_task_rekey)
- {
- GNUNET_SCHEDULER_cancel (kce_task_rekey);
- kce_task_rekey = NULL;
- }
- if (NULL != kce_task)
- {
- GNUNET_SCHEDULER_cancel (kce_task);
- kce_task = NULL;
- }
- if (NULL != timeout_task)
- {
- GNUNET_SCHEDULER_cancel (timeout_task);
- timeout_task = NULL;
- }
- if (NULL != read_task)
- {
- GNUNET_SCHEDULER_cancel (read_task);
- read_task = NULL;
- }
- if (NULL != udp_sock)
- {
- GNUNET_break (GNUNET_OK ==
- GNUNET_NETWORK_socket_close (udp_sock));
- udp_sock = NULL;
- }
- GNUNET_CONTAINER_multipeermap_iterate (receivers,
- &get_receiver_delete_it,
- NULL);
- GNUNET_CONTAINER_multipeermap_destroy (receivers);
- GNUNET_CONTAINER_multipeermap_iterate (senders,
- &get_sender_delete_it,
- NULL);
- GNUNET_CONTAINER_multipeermap_destroy (senders);
- GNUNET_CONTAINER_multishortmap_destroy (key_cache);
- GNUNET_CONTAINER_heap_destroy (senders_heap);
- GNUNET_CONTAINER_heap_destroy (receivers_heap);
- if (NULL != timeout_task)
- {
- GNUNET_SCHEDULER_cancel (timeout_task);
- timeout_task = NULL;
- }
- if (NULL != ch)
- {
- GNUNET_TRANSPORT_communicator_disconnect (ch);
- ch = NULL;
- }
- if (NULL != ah)
- {
- GNUNET_TRANSPORT_application_done (ah);
- ah = NULL;
- }
- if (NULL != stats)
- {
- GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
- stats = NULL;
- }
- if (NULL != my_private_key)
- {
- GNUNET_free (my_private_key);
- my_private_key = NULL;
- }
- if (NULL != is)
- {
- GNUNET_NT_scanner_done (is);
- is = NULL;
- }
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "do_shutdown finished\n");
- }
- /**
- * Function called when the transport service has received a
- * backchannel message for this communicator (!) via a different return
- * path. Should be an acknowledgement.
- *
- * @param cls closure, NULL
- * @param sender which peer sent the notification
- * @param msg payload
- */
- static void
- enc_notify_cb (void *cls,
- const struct GNUNET_PeerIdentity *sender,
- const struct GNUNET_MessageHeader *msg)
- {
- const struct UDPAck *ack;
- (void) cls;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Storing UDPAck received from backchannel from %s\n",
- GNUNET_i2s_full (sender));
- if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_COMMUNICATOR_UDP_ACK) ||
- (ntohs (msg->size) != sizeof(struct UDPAck)))
- {
- GNUNET_break_op (0);
- return;
- }
- ack = (const struct UDPAck *) msg;
- GNUNET_CONTAINER_multipeermap_get_multiple (receivers,
- sender,
- &handle_ack,
- (void *) ack);
- }
- /**
- * Signature of the callback passed to #GNUNET_NAT_register() for
- * a function to call whenever our set of 'valid' addresses changes.
- *
- * @param cls closure
- * @param app_ctx[in,out] location where the app can store stuff
- * on add and retrieve it on remove
- * @param add_remove #GNUNET_YES to add a new public IP address,
- * #GNUNET_NO to remove a previous (now invalid) one
- * @param ac address class the address belongs to
- * @param addr either the previous or the new public IP address
- * @param addrlen actual length of the @a addr
- */
- static void
- nat_address_cb (void *cls,
- void **app_ctx,
- int add_remove,
- enum GNUNET_NAT_AddressClass ac,
- const struct sockaddr *addr,
- socklen_t addrlen)
- {
- char *my_addr;
- struct GNUNET_TRANSPORT_AddressIdentifier *ai;
- if (GNUNET_YES == add_remove)
- {
- enum GNUNET_NetworkType nt;
- GNUNET_asprintf (&my_addr,
- "%s-%s",
- COMMUNICATOR_ADDRESS_PREFIX,
- GNUNET_a2s (addr, addrlen));
- nt = GNUNET_NT_scanner_get_type (is, addr, addrlen);
- ai =
- GNUNET_TRANSPORT_communicator_address_add (ch,
- my_addr,
- nt,
- GNUNET_TIME_UNIT_FOREVER_REL);
- GNUNET_free (my_addr);
- *app_ctx = ai;
- }
- else
- {
- ai = *app_ctx;
- GNUNET_TRANSPORT_communicator_address_remove (ai);
- *app_ctx = NULL;
- }
- }
- /**
- * Broadcast our presence on one of our interfaces.
- *
- * @param cls a `struct BroadcastInterface`
- */
- static void
- ifc_broadcast (void *cls)
- {
- struct BroadcastInterface *bi = cls;
- struct GNUNET_TIME_Relative delay;
- delay = BROADCAST_FREQUENCY;
- delay.rel_value_us =
- GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, delay.rel_value_us);
- bi->broadcast_task =
- GNUNET_SCHEDULER_add_delayed (INTERFACE_SCAN_FREQUENCY, &ifc_broadcast, bi);
- switch (bi->sa->sa_family)
- {
- case AF_INET: {
- static int yes = 1;
- static int no = 0;
- ssize_t sent;
- if (GNUNET_OK !=
- GNUNET_NETWORK_socket_setsockopt (udp_sock,
- SOL_SOCKET,
- SO_BROADCAST,
- &yes,
- sizeof(int)))
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
- "setsockopt");
- sent = GNUNET_NETWORK_socket_sendto (udp_sock,
- &bi->bcm,
- sizeof(bi->bcm),
- bi->ba,
- bi->salen);
- if (-1 == sent)
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
- "sendto");
- if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt (udp_sock,
- SOL_SOCKET,
- SO_BROADCAST,
- &no,
- sizeof(int)))
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
- "setsockopt");
- break;
- }
- case AF_INET6: {
- ssize_t sent;
- struct sockaddr_in6 dst;
- dst.sin6_family = AF_INET6;
- dst.sin6_port = htons (my_port);
- dst.sin6_addr = bi->mcreq.ipv6mr_multiaddr;
- dst.sin6_scope_id = ((struct sockaddr_in6 *) bi->ba)->sin6_scope_id;
- sent = GNUNET_NETWORK_socket_sendto (udp_sock,
- &bi->bcm,
- sizeof(bi->bcm),
- (const struct sockaddr *) &dst,
- sizeof(dst));
- if (-1 == sent)
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "sendto");
- break;
- }
- default:
- GNUNET_break (0);
- break;
- }
- }
- /**
- * Callback function invoked for each interface found.
- * Activates/deactivates broadcast interfaces.
- *
- * @param cls NULL
- * @param name name of the interface (can be NULL for unknown)
- * @param isDefault is this presumably the default interface
- * @param addr address of this interface (can be NULL for unknown or unassigned)
- * @param broadcast_addr the broadcast address (can be NULL for unknown or
- * unassigned)
- * @param netmask the network mask (can be NULL for unknown or unassigned)
- * @param addrlen length of the address
- * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
- */
- static int
- iface_proc (void *cls,
- const char *name,
- int isDefault,
- const struct sockaddr *addr,
- const struct sockaddr *broadcast_addr,
- const struct sockaddr *netmask,
- socklen_t addrlen)
- {
- struct BroadcastInterface *bi;
- enum GNUNET_NetworkType network;
- struct UdpBroadcastSignature ubs;
- (void) cls;
- (void) netmask;
- if (NULL == addr)
- return GNUNET_YES; /* need to know our address! */
- network = GNUNET_NT_scanner_get_type (is, addr, addrlen);
- if (GNUNET_NT_LOOPBACK == network)
- {
- /* Broadcasting on loopback does not make sense */
- return GNUNET_YES;
- }
- for (bi = bi_head; NULL != bi; bi = bi->next)
- {
- if ((bi->salen == addrlen) && (0 == memcmp (addr, bi->sa, addrlen)))
- {
- bi->found = GNUNET_YES;
- return GNUNET_OK;
- }
- }
- if ((AF_INET6 == addr->sa_family) && (NULL == broadcast_addr))
- return GNUNET_OK; /* broadcast_addr is required for IPv6! */
- if ((AF_INET6 == addr->sa_family) && (GNUNET_YES != have_v6_socket))
- return GNUNET_OK; /* not using IPv6 */
- bi = GNUNET_new (struct BroadcastInterface);
- bi->sa = GNUNET_memdup (addr,
- addrlen);
- if ( (NULL != broadcast_addr) &&
- (addrlen == sizeof (struct sockaddr_in)) )
- {
- struct sockaddr_in *ba;
- ba = GNUNET_memdup (broadcast_addr,
- addrlen);
- ba->sin_port = htons (2086); /* always GNUnet port, ignore configuration! */
- bi->ba = (struct sockaddr *) ba;
- }
- bi->salen = addrlen;
- bi->found = GNUNET_YES;
- bi->bcm.sender = my_identity;
- ubs.purpose.purpose = htonl (GNUNET_SIGNATURE_COMMUNICATOR_UDP_BROADCAST);
- ubs.purpose.size = htonl (sizeof(ubs));
- ubs.sender = my_identity;
- GNUNET_CRYPTO_hash (addr, addrlen, &ubs.h_address);
- GNUNET_CRYPTO_eddsa_sign (my_private_key,
- &ubs,
- &bi->bcm.sender_sig);
- if (NULL != bi->ba)
- {
- bi->broadcast_task = GNUNET_SCHEDULER_add_now (&ifc_broadcast, bi);
- GNUNET_CONTAINER_DLL_insert (bi_head, bi_tail, bi);
- }
- if ((AF_INET6 == addr->sa_family) && (NULL != broadcast_addr))
- {
- /* Create IPv6 multicast request */
- const struct sockaddr_in6 *s6 =
- (const struct sockaddr_in6 *) broadcast_addr;
- GNUNET_assert (
- 1 == inet_pton (AF_INET6, "FF05::13B", &bi->mcreq.ipv6mr_multiaddr));
- /* http://tools.ietf.org/html/rfc2553#section-5.2:
- *
- * IPV6_JOIN_GROUP
- *
- * Join a multicast group on a specified local interface. If the
- * interface index is specified as 0, the kernel chooses the local
- * interface. For example, some kernels look up the multicast
- * group in the normal IPv6 routing table and using the resulting
- * interface; we do this for each interface, so no need to use
- * zero (anymore...).
- */bi->mcreq.ipv6mr_interface = s6->sin6_scope_id;
- /* Join the multicast group */
- if (GNUNET_OK != GNUNET_NETWORK_socket_setsockopt (udp_sock,
- IPPROTO_IPV6,
- IPV6_JOIN_GROUP,
- &bi->mcreq,
- sizeof(bi->mcreq)))
- {
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
- }
- }
- return GNUNET_OK;
- }
- /**
- * Scan interfaces to broadcast our presence on the LAN.
- *
- * @param cls NULL, unused
- */
- static void
- do_broadcast (void *cls)
- {
- struct BroadcastInterface *bin;
- (void) cls;
- for (struct BroadcastInterface *bi = bi_head; NULL != bi; bi = bi->next)
- bi->found = GNUNET_NO;
- GNUNET_OS_network_interfaces_list (&iface_proc, NULL);
- for (struct BroadcastInterface *bi = bi_head; NULL != bi; bi = bin)
- {
- bin = bi->next;
- if (GNUNET_NO == bi->found)
- bi_destroy (bi);
- }
- broadcast_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_SCAN_FREQUENCY,
- &do_broadcast,
- NULL);
- }
- /**
- * Setup communicator and launch network interactions.
- *
- * @param cls NULL (always)
- * @param args remaining command-line arguments
- * @param cfgfile name of the configuration file used (for saving, can be NULL!)
- * @param c configuration
- */
- static void
- run (void *cls,
- char *const *args,
- const char *cfgfile,
- const struct GNUNET_CONFIGURATION_Handle *c)
- {
- char *bindto;
- struct sockaddr *in;
- socklen_t in_len;
- struct sockaddr_storage in_sto;
- socklen_t sto_len;
- (void) cls;
- cfg = c;
- if (GNUNET_OK !=
- GNUNET_CONFIGURATION_get_value_string (cfg,
- COMMUNICATOR_CONFIG_SECTION,
- "BINDTO",
- &bindto))
- {
- GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
- COMMUNICATOR_CONFIG_SECTION,
- "BINDTO");
- return;
- }
- if (GNUNET_OK !=
- GNUNET_CONFIGURATION_get_value_time (cfg,
- COMMUNICATOR_CONFIG_SECTION,
- "REKEY_INTERVAL",
- &rekey_interval))
- rekey_interval = DEFAULT_REKEY_TIME_INTERVAL;
- if (GNUNET_OK !=
- GNUNET_CONFIGURATION_get_value_size (cfg,
- COMMUNICATOR_CONFIG_SECTION,
- "REKEY_MAX_BYTES",
- &rekey_max_bytes))
- rekey_max_bytes = DEFAULT_REKEY_MAX_BYTES;
- in = udp_address_to_sockaddr (bindto, &in_len);
- if (NULL == in)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Failed to setup UDP socket address with path `%s'\n",
- bindto);
- GNUNET_free (bindto);
- return;
- }
- udp_sock =
- GNUNET_NETWORK_socket_create (in->sa_family,
- SOCK_DGRAM,
- IPPROTO_UDP);
- if (NULL == udp_sock)
- {
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
- GNUNET_free (in);
- GNUNET_free (bindto);
- return;
- }
- if (AF_INET6 == in->sa_family)
- have_v6_socket = GNUNET_YES;
- if (GNUNET_OK !=
- GNUNET_NETWORK_socket_bind (udp_sock,
- in,
- in_len))
- {
- GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
- "bind",
- bindto);
- GNUNET_NETWORK_socket_close (udp_sock);
- udp_sock = NULL;
- GNUNET_free (in);
- GNUNET_free (bindto);
- return;
- }
- /* We might have bound to port 0, allowing the OS to figure it out;
- thus, get the real IN-address from the socket */
- sto_len = sizeof(in_sto);
- if (0 != getsockname (GNUNET_NETWORK_get_fd (udp_sock),
- (struct sockaddr *) &in_sto,
- &sto_len))
- {
- memcpy (&in_sto, in, in_len);
- sto_len = in_len;
- }
- GNUNET_free (in);
- GNUNET_free (bindto);
- in = (struct sockaddr *) &in_sto;
- in_len = sto_len;
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Bound to `%s'\n",
- GNUNET_a2s ((const struct sockaddr *) &in_sto, sto_len));
- switch (in->sa_family)
- {
- case AF_INET:
- my_port = ntohs (((struct sockaddr_in *) in)->sin_port);
- break;
- case AF_INET6:
- my_port = ntohs (((struct sockaddr_in6 *) in)->sin6_port);
- break;
- default:
- GNUNET_break (0);
- my_port = 0;
- }
- stats = GNUNET_STATISTICS_create ("C-UDP", cfg);
- senders = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES);
- receivers = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES);
- senders_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
- receivers_heap =
- GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
- key_cache = GNUNET_CONTAINER_multishortmap_create (1024, GNUNET_YES);
- GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
- is = GNUNET_NT_scanner_init ();
- my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
- if (NULL == my_private_key)
- {
- GNUNET_log (
- GNUNET_ERROR_TYPE_ERROR,
- _ (
- "Transport service is lacking key configuration settings. Exiting.\n"));
- GNUNET_SCHEDULER_shutdown ();
- return;
- }
- GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_identity.public_key);
- /* start reading */
- read_task = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
- udp_sock,
- &sock_read,
- NULL);
- ch = GNUNET_TRANSPORT_communicator_connect (cfg,
- COMMUNICATOR_CONFIG_SECTION,
- COMMUNICATOR_ADDRESS_PREFIX,
- GNUNET_TRANSPORT_CC_UNRELIABLE,
- &mq_init,
- NULL,
- &enc_notify_cb,
- NULL);
- if (NULL == ch)
- {
- GNUNET_break (0);
- GNUNET_SCHEDULER_shutdown ();
- return;
- }
- ah = GNUNET_TRANSPORT_application_init (cfg);
- if (NULL == ah)
- {
- GNUNET_break (0);
- GNUNET_SCHEDULER_shutdown ();
- return;
- }
- /* start broadcasting */
- if (GNUNET_YES !=
- GNUNET_CONFIGURATION_get_value_yesno (cfg,
- COMMUNICATOR_CONFIG_SECTION,
- "DISABLE_BROADCAST"))
- {
- broadcast_task = GNUNET_SCHEDULER_add_now (&do_broadcast, NULL);
- }
- nat = GNUNET_NAT_register (cfg,
- COMMUNICATOR_CONFIG_SECTION,
- IPPROTO_UDP,
- 1 /* one address */,
- (const struct sockaddr **) &in,
- &in_len,
- &nat_address_cb,
- NULL /* FIXME: support reversal: #5529 */,
- NULL /* closure */);
- }
- /**
- * The main function for the UNIX communicator.
- *
- * @param argc number of arguments from the command line
- * @param argv command line arguments
- * @return 0 ok, 1 on error
- */
- int
- main (int argc, char *const *argv)
- {
- static const struct GNUNET_GETOPT_CommandLineOption options[] = {
- GNUNET_GETOPT_OPTION_END
- };
- int ret;
- if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
- return 2;
- ret = (GNUNET_OK == GNUNET_PROGRAM_run (argc,
- argv,
- "gnunet-communicator-udp",
- _ ("GNUnet UDP communicator"),
- options,
- &run,
- NULL))
- ? 0
- : 1;
- GNUNET_free_nz ((void *) argv);
- return ret;
- }
- /* end of gnunet-communicator-udp.c */
|