gnunet-service-transport.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010-2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file transport/gnunet-service-transport.c
  18. * @brief main for gnunet-service-transport
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_hello_lib.h"
  24. #include "gnunet_statistics_service.h"
  25. #include "gnunet_transport_service.h"
  26. #include "gnunet_peerinfo_service.h"
  27. #include "gnunet_ats_service.h"
  28. #include "gnunet-service-transport.h"
  29. #include "gnunet-service-transport_ats.h"
  30. #include "gnunet-service-transport_hello.h"
  31. #include "gnunet-service-transport_neighbours.h"
  32. #include "gnunet-service-transport_plugins.h"
  33. #include "gnunet-service-transport_validation.h"
  34. #include "gnunet-service-transport_manipulation.h"
  35. #include "transport.h"
  36. /**
  37. * Size of the blacklist hash map.
  38. */
  39. #define TRANSPORT_BLACKLIST_HT_SIZE 64
  40. /**
  41. * How many messages can we have pending for a given client process
  42. * before we start to drop incoming messages? We typically should
  43. * have only one client and so this would be the primary buffer for
  44. * messages, so the number should be chosen rather generously.
  45. *
  46. * The expectation here is that most of the time the queue is large
  47. * enough so that a drop is virtually never required. Note that
  48. * this value must be about as large as 'TOTAL_MSGS' in the
  49. * 'test_transport_api_reliability.c', otherwise that testcase may
  50. * fail.
  51. */
  52. #define MAX_PENDING (128 * 1024)
  53. /**
  54. * Information we need for an asynchronous session kill.
  55. */
  56. struct GNUNET_ATS_SessionKiller
  57. {
  58. /**
  59. * Kept in a DLL.
  60. */
  61. struct GNUNET_ATS_SessionKiller *next;
  62. /**
  63. * Kept in a DLL.
  64. */
  65. struct GNUNET_ATS_SessionKiller *prev;
  66. /**
  67. * Session to kill.
  68. */
  69. struct GNUNET_ATS_Session *session;
  70. /**
  71. * Plugin for the session.
  72. */
  73. struct GNUNET_TRANSPORT_PluginFunctions *plugin;
  74. /**
  75. * The kill task.
  76. */
  77. struct GNUNET_SCHEDULER_Task *task;
  78. };
  79. /**
  80. * What type of client is the `struct TransportClient` about?
  81. */
  82. enum ClientType
  83. {
  84. /**
  85. * We do not know yet (client is fresh).
  86. */
  87. CT_NONE = 0,
  88. /**
  89. * Is the CORE service, we need to forward traffic to it.
  90. */
  91. CT_CORE = 1,
  92. /**
  93. * It is a monitor, forward monitor data.
  94. */
  95. CT_MONITOR = 2,
  96. /**
  97. * It is a blacklist, query about allowed connections.
  98. */
  99. CT_BLACKLIST = 3,
  100. /**
  101. * CORE client without any handlers.
  102. */
  103. CT_CORE_NO_HANDLERS = 4
  104. };
  105. /**
  106. * Context we use when performing a blacklist check.
  107. */
  108. struct GST_BlacklistCheck;
  109. /**
  110. * Client connected to the transport service.
  111. */
  112. struct TransportClient
  113. {
  114. /**
  115. * This is a doubly-linked list.
  116. */
  117. struct TransportClient *next;
  118. /**
  119. * This is a doubly-linked list.
  120. */
  121. struct TransportClient *prev;
  122. /**
  123. * Handle to the client.
  124. */
  125. struct GNUNET_SERVICE_Client *client;
  126. /**
  127. * Message queue to the client.
  128. */
  129. struct GNUNET_MQ_Handle *mq;
  130. /**
  131. * What type of client is this?
  132. */
  133. enum ClientType type;
  134. union
  135. {
  136. /**
  137. * Peer identity to monitor the addresses of.
  138. * Zero to monitor all neighbours. Valid if
  139. * @e type is CT_MONITOR.
  140. */
  141. struct GNUNET_PeerIdentity monitor_peer;
  142. /**
  143. * Additional details if @e type is CT_BLACKLIST.
  144. */
  145. struct
  146. {
  147. /**
  148. * Blacklist check that we're currently performing (or NULL
  149. * if we're performing one that has been cancelled).
  150. */
  151. struct GST_BlacklistCheck *bc;
  152. /**
  153. * Set to #GNUNET_YES if we're currently waiting for a reply.
  154. */
  155. int waiting_for_reply;
  156. /**
  157. * #GNUNET_YES if we have to call receive_done for this client
  158. */
  159. int call_receive_done;
  160. } blacklist;
  161. } details;
  162. };
  163. /**
  164. * Context we use when performing a blacklist check.
  165. */
  166. struct GST_BlacklistCheck
  167. {
  168. /**
  169. * This is a linked list.
  170. */
  171. struct GST_BlacklistCheck *next;
  172. /**
  173. * This is a linked list.
  174. */
  175. struct GST_BlacklistCheck *prev;
  176. /**
  177. * Peer being checked.
  178. */
  179. struct GNUNET_PeerIdentity peer;
  180. /**
  181. * Continuation to call with the result.
  182. */
  183. GST_BlacklistTestContinuation cont;
  184. /**
  185. * Closure for @e cont.
  186. */
  187. void *cont_cls;
  188. /**
  189. * Address for #GST_blacklist_abort_matching(), can be NULL.
  190. */
  191. struct GNUNET_HELLO_Address *address;
  192. /**
  193. * Session for #GST_blacklist_abort_matching(), can be NULL.
  194. */
  195. struct GNUNET_ATS_Session *session;
  196. /**
  197. * Our current position in the blacklisters list.
  198. */
  199. struct TransportClient *bl_pos;
  200. /**
  201. * Current task performing the check.
  202. */
  203. struct GNUNET_SCHEDULER_Task *task;
  204. };
  205. /**
  206. * Context for address to string operations
  207. */
  208. struct AddressToStringContext
  209. {
  210. /**
  211. * This is a doubly-linked list.
  212. */
  213. struct AddressToStringContext *next;
  214. /**
  215. * This is a doubly-linked list.
  216. */
  217. struct AddressToStringContext *prev;
  218. /**
  219. * Client that made the request.
  220. */
  221. struct TransportClient *tc;
  222. };
  223. /**
  224. * Closure for #handle_send_transmit_continuation()
  225. */
  226. struct SendTransmitContinuationContext
  227. {
  228. /**
  229. * Client that made the request.
  230. */
  231. struct TransportClient *tc;
  232. /**
  233. * Peer that was the target.
  234. */
  235. struct GNUNET_PeerIdentity target;
  236. /**
  237. * At what time did we receive the message?
  238. */
  239. struct GNUNET_TIME_Absolute send_time;
  240. /**
  241. * Unique ID, for logging.
  242. */
  243. unsigned long long uuid;
  244. /**
  245. * Set to #GNUNET_YES if the connection for @e target goes
  246. * down and we thus must no longer send the
  247. * #GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK message.
  248. */
  249. int down;
  250. };
  251. /**
  252. * Head of linked list of all clients to this service.
  253. */
  254. static struct TransportClient *clients_head;
  255. /**
  256. * Tail of linked list of all clients to this service.
  257. */
  258. static struct TransportClient *clients_tail;
  259. /**
  260. * Map of peer identities to active send transmit continuation
  261. * contexts. Used to flag contexts as 'dead' when a connection goes
  262. * down. Values are of type `struct SendTransmitContinuationContext
  263. * *`.
  264. */
  265. static struct GNUNET_CONTAINER_MultiPeerMap *active_stccs;
  266. /**
  267. * Head of linked list of all pending address iterations
  268. */
  269. static struct AddressToStringContext *a2s_head;
  270. /**
  271. * Tail of linked list of all pending address iterations
  272. */
  273. static struct AddressToStringContext *a2s_tail;
  274. /**
  275. * Head of DLL of active blacklisting queries.
  276. */
  277. static struct GST_BlacklistCheck *bc_head;
  278. /**
  279. * Tail of DLL of active blacklisting queries.
  280. */
  281. static struct GST_BlacklistCheck *bc_tail;
  282. /**
  283. * Hashmap of blacklisted peers. Values are of type 'char *' (transport names),
  284. * can be NULL if we have no static blacklist.
  285. */
  286. static struct GNUNET_CONTAINER_MultiPeerMap *blacklist;
  287. /**
  288. * Notification context, to send updates on changes to active plugin
  289. * connections.
  290. */
  291. static struct GNUNET_NotificationContext *plugin_nc;
  292. /**
  293. * Plugin monitoring client we are currently syncing, NULL if all
  294. * monitoring clients are in sync.
  295. */
  296. static struct TransportClient *sync_client;
  297. /**
  298. * Peer identity that is all zeros, used as a way to indicate
  299. * "all peers". Used for comparissons.
  300. */
  301. static struct GNUNET_PeerIdentity all_zeros;
  302. /**
  303. * Statistics handle.
  304. */
  305. struct GNUNET_STATISTICS_Handle *GST_stats;
  306. /**
  307. * Configuration handle.
  308. */
  309. const struct GNUNET_CONFIGURATION_Handle *GST_cfg;
  310. /**
  311. * Configuration handle.
  312. */
  313. struct GNUNET_PeerIdentity GST_my_identity;
  314. /**
  315. * Handle to peerinfo service.
  316. */
  317. struct GNUNET_PEERINFO_Handle *GST_peerinfo;
  318. /**
  319. * Our private key.
  320. */
  321. struct GNUNET_CRYPTO_EddsaPrivateKey GST_my_private_key;
  322. /**
  323. * ATS scheduling handle.
  324. */
  325. struct GNUNET_ATS_SchedulingHandle *GST_ats;
  326. /**
  327. * ATS connectivity handle.
  328. */
  329. struct GNUNET_ATS_ConnectivityHandle *GST_ats_connect;
  330. /**
  331. * Hello address expiration
  332. */
  333. struct GNUNET_TIME_Relative hello_expiration;
  334. /**
  335. * Head of DLL of asynchronous tasks to kill sessions.
  336. */
  337. static struct GNUNET_ATS_SessionKiller *sk_head;
  338. /**
  339. * Tail of DLL of asynchronous tasks to kill sessions.
  340. */
  341. static struct GNUNET_ATS_SessionKiller *sk_tail;
  342. /**
  343. * Interface scanner determines our LAN address range(s).
  344. */
  345. struct GNUNET_NT_InterfaceScanner *GST_is;
  346. /**
  347. * Queue the given message for transmission to the given client
  348. *
  349. * @param tc target of the message
  350. * @param msg message to transmit
  351. * @param may_drop #GNUNET_YES if the message can be dropped
  352. */
  353. static void
  354. unicast (struct TransportClient *tc,
  355. const struct GNUNET_MessageHeader *msg,
  356. int may_drop)
  357. {
  358. struct GNUNET_MQ_Envelope *env;
  359. if ((GNUNET_MQ_get_length (tc->mq) >= MAX_PENDING) &&
  360. (GNUNET_YES == may_drop))
  361. {
  362. GNUNET_log (
  363. GNUNET_ERROR_TYPE_DEBUG,
  364. "Dropping message of type %u and size %u, have %u/%u messages pending\n",
  365. ntohs (msg->type),
  366. ntohs (msg->size),
  367. GNUNET_MQ_get_length (tc->mq),
  368. MAX_PENDING);
  369. GNUNET_STATISTICS_update (GST_stats,
  370. gettext_noop (
  371. "# messages dropped due to slow client"),
  372. 1,
  373. GNUNET_NO);
  374. return;
  375. }
  376. env = GNUNET_MQ_msg_copy (msg);
  377. GNUNET_MQ_send (tc->mq, env);
  378. }
  379. /**
  380. * Called whenever a client connects. Allocates our
  381. * data structures associated with that client.
  382. *
  383. * @param cls closure, NULL
  384. * @param client identification of the client
  385. * @param mq message queue for the client
  386. * @return our `struct TransportClient`
  387. */
  388. static void *
  389. client_connect_cb (void *cls,
  390. struct GNUNET_SERVICE_Client *client,
  391. struct GNUNET_MQ_Handle *mq)
  392. {
  393. struct TransportClient *tc;
  394. tc = GNUNET_new (struct TransportClient);
  395. tc->client = client;
  396. tc->mq = mq;
  397. GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, tc);
  398. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", tc);
  399. return tc;
  400. }
  401. /**
  402. * Perform next action in the blacklist check.
  403. *
  404. * @param cls the `struct BlacklistCheck*`
  405. */
  406. static void
  407. do_blacklist_check (void *cls);
  408. /**
  409. * Mark the peer as down so we don't call the continuation
  410. * context in the future.
  411. *
  412. * @param cls a `struct TransportClient`
  413. * @param peer a peer we are sending to
  414. * @param value a `struct SendTransmitContinuationContext` to mark
  415. * @return #GNUNET_OK (continue to iterate)
  416. */
  417. static int
  418. mark_match_down (void *cls, const struct GNUNET_PeerIdentity *peer, void *value)
  419. {
  420. struct TransportClient *tc = cls;
  421. struct SendTransmitContinuationContext *stcc = value;
  422. if (tc == stcc->tc)
  423. {
  424. stcc->down = GNUNET_YES;
  425. stcc->tc = NULL;
  426. }
  427. return GNUNET_OK;
  428. }
  429. /**
  430. * Called whenever a client is disconnected. Frees our
  431. * resources associated with that client.
  432. *
  433. * @param cls closure, NULL
  434. * @param client identification of the client
  435. * @param app_ctx our `struct TransportClient`
  436. */
  437. static void
  438. client_disconnect_cb (void *cls,
  439. struct GNUNET_SERVICE_Client *client,
  440. void *app_ctx)
  441. {
  442. struct TransportClient *tc = app_ctx;
  443. struct GST_BlacklistCheck *bc;
  444. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  445. "Client %p disconnected, cleaning up.\n",
  446. tc);
  447. if (NULL != active_stccs)
  448. GNUNET_CONTAINER_multipeermap_iterate (active_stccs,
  449. &mark_match_down,
  450. tc);
  451. for (struct AddressToStringContext *cur = a2s_head; NULL != cur;
  452. cur = cur->next)
  453. {
  454. if (cur->tc == tc)
  455. cur->tc = NULL;
  456. }
  457. GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, tc);
  458. switch (tc->type)
  459. {
  460. case CT_NONE:
  461. break;
  462. case CT_CORE:
  463. break;
  464. case CT_MONITOR:
  465. break;
  466. case CT_BLACKLIST:
  467. for (bc = bc_head; NULL != bc; bc = bc->next)
  468. {
  469. if (bc->bl_pos != tc)
  470. continue;
  471. bc->bl_pos = tc->next;
  472. if (NULL == bc->task)
  473. bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
  474. }
  475. break;
  476. case CT_CORE_NO_HANDLERS:
  477. break;
  478. }
  479. GNUNET_free (tc);
  480. }
  481. /**
  482. * Function called for each of our connected neighbours. Notify the
  483. * client about the existing neighbour.
  484. *
  485. * @param cls the `struct TransportClient *` to notify
  486. * @param peer identity of the neighbour
  487. * @param address the address
  488. * @param state the current state of the peer
  489. * @param state_timeout the time out for the state
  490. * @param bandwidth_in inbound bandwidth in NBO
  491. * @param bandwidth_out outbound bandwidth in NBO
  492. */
  493. static void
  494. notify_client_about_neighbour (void *cls,
  495. const struct GNUNET_PeerIdentity *peer,
  496. const struct GNUNET_HELLO_Address *address,
  497. enum GNUNET_TRANSPORT_PeerState state,
  498. struct GNUNET_TIME_Absolute state_timeout,
  499. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  500. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
  501. {
  502. struct TransportClient *tc = cls;
  503. struct ConnectInfoMessage cim;
  504. if (GNUNET_NO == GST_neighbours_test_connected (peer))
  505. return;
  506. cim.header.size = htons (sizeof(struct ConnectInfoMessage));
  507. cim.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
  508. cim.id = *peer;
  509. cim.quota_out = bandwidth_out;
  510. unicast (tc, &cim.header, GNUNET_NO);
  511. }
  512. /**
  513. * Initialize a normal client. We got a start message from this
  514. * client, add it to the list of clients for broadcasting of inbound
  515. * messages.
  516. *
  517. * @param cls the client
  518. * @param start the start message that was sent
  519. */
  520. static void
  521. handle_client_start (void *cls, const struct StartMessage *start)
  522. {
  523. struct TransportClient *tc = cls;
  524. const struct GNUNET_MessageHeader *hello;
  525. uint32_t options;
  526. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p sent START\n", tc);
  527. options = ntohl (start->options);
  528. if ((0 != (1 & options)) &&
  529. (0 != memcmp (&start->self,
  530. &GST_my_identity,
  531. sizeof(struct GNUNET_PeerIdentity))))
  532. {
  533. /* client thinks this is a different peer, reject */
  534. GNUNET_break (0);
  535. GNUNET_SERVICE_client_drop (tc->client);
  536. return;
  537. }
  538. if (CT_NONE != tc->type)
  539. {
  540. GNUNET_break (0);
  541. GNUNET_SERVICE_client_drop (tc->client);
  542. return;
  543. }
  544. if (0 != (2 & options))
  545. tc->type = CT_CORE;
  546. else
  547. tc->type = CT_CORE_NO_HANDLERS;
  548. hello = GST_hello_get ();
  549. if (NULL != hello)
  550. unicast (tc, hello, GNUNET_NO);
  551. GST_neighbours_iterate (&notify_client_about_neighbour, tc);
  552. GNUNET_SERVICE_client_continue (tc->client);
  553. }
  554. /**
  555. * Client sent us a HELLO. Check the request.
  556. *
  557. * @param cls the client
  558. * @param message the HELLO message
  559. */
  560. static int
  561. check_client_hello (void *cls, const struct GNUNET_MessageHeader *message)
  562. {
  563. return GNUNET_OK; /* FIXME: check here? */
  564. }
  565. /**
  566. * Client sent us a HELLO. Process the request.
  567. *
  568. * @param cls the client
  569. * @param message the HELLO message
  570. */
  571. static void
  572. handle_client_hello (void *cls, const struct GNUNET_MessageHeader *message)
  573. {
  574. struct TransportClient *tc = cls;
  575. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Received HELLO message\n");
  576. GST_validation_handle_hello (message);
  577. GNUNET_SERVICE_client_continue (tc->client);
  578. }
  579. /**
  580. * Function called after the transmission is done. Notify the client that it is
  581. * OK to send the next message.
  582. *
  583. * @param cls closure
  584. * @param success #GNUNET_OK on success, #GNUNET_NO on failure, #GNUNET_SYSERR if we're not connected
  585. * @param bytes_payload bytes payload sent
  586. * @param bytes_on_wire bytes sent on wire
  587. */
  588. static void
  589. handle_send_transmit_continuation (void *cls,
  590. int success,
  591. size_t bytes_payload,
  592. size_t bytes_on_wire)
  593. {
  594. struct SendTransmitContinuationContext *stcc = cls;
  595. struct SendOkMessage send_ok_msg;
  596. #ifdef ENABLE_TTD
  597. struct GNUNET_TIME_Relative delay;
  598. const struct GNUNET_HELLO_Address *addr;
  599. delay = GNUNET_TIME_absolute_get_duration (stcc->send_time);
  600. addr = GST_neighbour_get_current_address (&stcc->target);
  601. if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
  602. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  603. "It took us %s to send %u/%u bytes to %s (%d, %s)\n",
  604. GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES),
  605. (unsigned int) bytes_payload,
  606. (unsigned int) bytes_on_wire,
  607. GNUNET_i2s (&stcc->target),
  608. success,
  609. (NULL != addr) ? addr->transport_name : "%");
  610. else
  611. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  612. "It took us %s to send %u/%u bytes to %s (%d, %s)\n",
  613. GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES),
  614. (unsigned int) bytes_payload,
  615. (unsigned int) bytes_on_wire,
  616. GNUNET_i2s (&stcc->target),
  617. success,
  618. (NULL != addr) ? addr->transport_name : "%");
  619. #endif
  620. if (GNUNET_NO == stcc->down)
  621. {
  622. /* Only send confirmation if we are still connected */
  623. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  624. "Sending SEND_OK for transmission request %llu\n",
  625. stcc->uuid);
  626. send_ok_msg.header.size = htons (sizeof(send_ok_msg));
  627. send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
  628. send_ok_msg.bytes_msg = htons (bytes_payload);
  629. send_ok_msg.bytes_physical = htonl (bytes_on_wire);
  630. send_ok_msg.success = htons (success);
  631. send_ok_msg.peer = stcc->target;
  632. unicast (stcc->tc, &send_ok_msg.header, GNUNET_NO);
  633. }
  634. GNUNET_assert (
  635. GNUNET_OK ==
  636. GNUNET_CONTAINER_multipeermap_remove (active_stccs, &stcc->target, stcc));
  637. GNUNET_free (stcc);
  638. }
  639. /**
  640. * Client asked for transmission to a peer. Process the request.
  641. *
  642. * @param cls the client
  643. * @param obm the send message that was sent
  644. */
  645. static int
  646. check_client_send (void *cls, const struct OutboundMessage *obm)
  647. {
  648. uint16_t size;
  649. const struct GNUNET_MessageHeader *obmm;
  650. size = ntohs (obm->header.size) - sizeof(struct OutboundMessage);
  651. if (size < sizeof(struct GNUNET_MessageHeader))
  652. {
  653. GNUNET_break (0);
  654. return GNUNET_SYSERR;
  655. }
  656. obmm = (const struct GNUNET_MessageHeader *) &obm[1];
  657. if (size != ntohs (obmm->size))
  658. {
  659. GNUNET_break (0);
  660. return GNUNET_SYSERR;
  661. }
  662. return GNUNET_OK;
  663. }
  664. /**
  665. * Client asked for transmission to a peer. Process the request.
  666. *
  667. * @param cls the client
  668. * @param obm the send message that was sent
  669. */
  670. static void
  671. handle_client_send (void *cls, const struct OutboundMessage *obm)
  672. {
  673. static unsigned long long uuid_gen;
  674. struct TransportClient *tc = cls;
  675. const struct GNUNET_MessageHeader *obmm;
  676. struct SendTransmitContinuationContext *stcc;
  677. obmm = (const struct GNUNET_MessageHeader *) &obm[1];
  678. if (GNUNET_NO == GST_neighbours_test_connected (&obm->peer))
  679. {
  680. /* not connected, not allowed to send; can happen due to asynchronous operations */
  681. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  682. "Could not send message to peer `%s': not connected\n",
  683. GNUNET_i2s (&obm->peer));
  684. GNUNET_STATISTICS_update (
  685. GST_stats,
  686. gettext_noop ("# bytes payload dropped (other peer was not connected)"),
  687. ntohs (obmm->size),
  688. GNUNET_NO);
  689. GNUNET_SERVICE_client_continue (tc->client);
  690. return;
  691. }
  692. GNUNET_log (
  693. GNUNET_ERROR_TYPE_DEBUG,
  694. "Received SEND request %llu for `%s' and first message of type %u and total size %u\n",
  695. uuid_gen,
  696. GNUNET_i2s (&obm->peer),
  697. ntohs (obmm->type),
  698. ntohs (obmm->size));
  699. GNUNET_SERVICE_client_continue (tc->client);
  700. stcc = GNUNET_new (struct SendTransmitContinuationContext);
  701. stcc->target = obm->peer;
  702. stcc->tc = tc;
  703. stcc->send_time = GNUNET_TIME_absolute_get ();
  704. stcc->uuid = uuid_gen++;
  705. (void) GNUNET_CONTAINER_multipeermap_put (
  706. active_stccs,
  707. &stcc->target,
  708. stcc,
  709. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  710. GST_manipulation_send (&obm->peer,
  711. obmm,
  712. ntohs (obmm->size),
  713. GNUNET_TIME_relative_ntoh (obm->timeout),
  714. &handle_send_transmit_continuation,
  715. stcc);
  716. }
  717. /**
  718. * Take the given address and append it to the set of results sent back to
  719. * the client. This function may be called serveral times for a single
  720. * conversion. The last invocation will be with a @a address of
  721. * NULL and a @a res of #GNUNET_OK. Thus, to indicate conversion
  722. * errors, the callback might be called first with @a address NULL and
  723. * @a res being #GNUNET_SYSERR. In that case, there will still be a
  724. * subsequent call later with @a address NULL and @a res #GNUNET_OK.
  725. *
  726. * @param cls the `struct AddressToStringContext`
  727. * @param buf text to transmit (contains the human-readable address, or NULL)
  728. * @param res #GNUNET_OK if conversion was successful, #GNUNET_SYSERR on error,
  729. * never #GNUNET_NO
  730. */
  731. static void
  732. transmit_address_to_client (void *cls, const char *buf, int res)
  733. {
  734. struct AddressToStringContext *actx = cls;
  735. struct GNUNET_MQ_Envelope *env;
  736. struct AddressToStringResultMessage *atsm;
  737. size_t slen;
  738. GNUNET_assert ((GNUNET_OK == res) || (GNUNET_SYSERR == res));
  739. if (NULL == actx->tc)
  740. return;
  741. if (NULL == buf)
  742. {
  743. env = GNUNET_MQ_msg (atsm,
  744. GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
  745. if (GNUNET_OK == res)
  746. {
  747. /* this was the last call, transmit */
  748. atsm->res = htonl (GNUNET_OK);
  749. atsm->addr_len = htonl (0);
  750. GNUNET_MQ_send (actx->tc->mq, env);
  751. GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, actx);
  752. GNUNET_free (actx);
  753. return;
  754. }
  755. if (GNUNET_SYSERR == res)
  756. {
  757. /* address conversion failed, but there will be more callbacks */
  758. atsm->res = htonl (GNUNET_SYSERR);
  759. atsm->addr_len = htonl (0);
  760. GNUNET_MQ_send (actx->tc->mq, env);
  761. return;
  762. }
  763. }
  764. GNUNET_assert (GNUNET_OK == res);
  765. /* succesful conversion, append*/
  766. slen = strlen (buf) + 1;
  767. env =
  768. GNUNET_MQ_msg_extra (atsm,
  769. slen,
  770. GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
  771. atsm->res = htonl (GNUNET_YES);
  772. atsm->addr_len = htonl (slen);
  773. GNUNET_memcpy (&atsm[1], buf, slen);
  774. GNUNET_MQ_send (actx->tc->mq, env);
  775. }
  776. /**
  777. * Client asked to resolve an address. Check the request.
  778. *
  779. * @param cls the client
  780. * @param alum the resolution request
  781. * @return #GNUNET_OK if @a alum is well-formed
  782. */
  783. static int
  784. check_client_address_to_string (void *cls,
  785. const struct AddressLookupMessage *alum)
  786. {
  787. const char *plugin_name;
  788. const char *address;
  789. uint32_t address_len;
  790. uint16_t size;
  791. size = ntohs (alum->header.size);
  792. address_len = ntohs (alum->addrlen);
  793. if (size <= sizeof(struct AddressLookupMessage) + address_len)
  794. {
  795. GNUNET_break (0);
  796. return GNUNET_SYSERR;
  797. }
  798. address = (const char *) &alum[1];
  799. plugin_name = (const char *) &address[address_len];
  800. if ('\0' != plugin_name[size - sizeof(struct AddressLookupMessage)
  801. - address_len - 1])
  802. {
  803. GNUNET_break (0);
  804. return GNUNET_SYSERR;
  805. }
  806. return GNUNET_OK;
  807. }
  808. /**
  809. * Client asked to resolve an address. Process the request.
  810. *
  811. * @param cls the client
  812. * @param alum the resolution request
  813. */
  814. static void
  815. handle_client_address_to_string (void *cls,
  816. const struct AddressLookupMessage *alum)
  817. {
  818. struct TransportClient *tc = cls;
  819. struct GNUNET_TRANSPORT_PluginFunctions *papi;
  820. const char *plugin_name;
  821. const char *address;
  822. uint32_t address_len;
  823. struct AddressToStringContext *actx;
  824. struct GNUNET_MQ_Envelope *env;
  825. struct AddressToStringResultMessage *atsm;
  826. struct GNUNET_TIME_Relative rtimeout;
  827. int32_t numeric;
  828. address_len = ntohs (alum->addrlen);
  829. address = (const char *) &alum[1];
  830. plugin_name = (const char *) &address[address_len];
  831. rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout);
  832. numeric = ntohs (alum->numeric_only);
  833. papi = GST_plugins_printer_find (plugin_name);
  834. if (NULL == papi)
  835. {
  836. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  837. "Failed to find plugin `%s'\n",
  838. plugin_name);
  839. env = GNUNET_MQ_msg (atsm,
  840. GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
  841. atsm->res = htonl (GNUNET_SYSERR);
  842. atsm->addr_len = htonl (0);
  843. GNUNET_MQ_send (tc->mq, env);
  844. env = GNUNET_MQ_msg (atsm,
  845. GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
  846. atsm->res = htonl (GNUNET_OK);
  847. atsm->addr_len = htonl (0);
  848. GNUNET_MQ_send (tc->mq, env);
  849. return;
  850. }
  851. actx = GNUNET_new (struct AddressToStringContext);
  852. actx->tc = tc;
  853. GNUNET_CONTAINER_DLL_insert (a2s_head, a2s_tail, actx);
  854. GNUNET_SERVICE_client_disable_continue_warning (tc->client);
  855. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  856. "Pretty-printing address of %u bytes using plugin `%s'\n",
  857. address_len,
  858. plugin_name);
  859. papi->address_pretty_printer (papi->cls,
  860. plugin_name,
  861. address,
  862. address_len,
  863. numeric,
  864. rtimeout,
  865. &transmit_address_to_client,
  866. actx);
  867. }
  868. /**
  869. * Compose #PeerIterateResponseMessage using the given peer and address.
  870. *
  871. * @param peer identity of the peer
  872. * @param address the address, NULL on disconnect
  873. * @return composed message
  874. */
  875. static struct PeerIterateResponseMessage *
  876. compose_address_iterate_response_message (
  877. const struct GNUNET_PeerIdentity *peer,
  878. const struct GNUNET_HELLO_Address *address)
  879. {
  880. struct PeerIterateResponseMessage *msg;
  881. size_t size;
  882. size_t tlen;
  883. size_t alen;
  884. char *addr;
  885. GNUNET_assert (NULL != peer);
  886. if (NULL != address)
  887. {
  888. tlen = strlen (address->transport_name) + 1;
  889. alen = address->address_length;
  890. }
  891. else
  892. {
  893. tlen = 0;
  894. alen = 0;
  895. }
  896. size = (sizeof(struct PeerIterateResponseMessage) + alen + tlen);
  897. msg = GNUNET_malloc (size);
  898. msg->header.size = htons (size);
  899. msg->header.type =
  900. htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
  901. msg->reserved = htonl (0);
  902. msg->peer = *peer;
  903. msg->addrlen = htonl (alen);
  904. msg->pluginlen = htonl (tlen);
  905. if (NULL != address)
  906. {
  907. msg->local_address_info = htonl ((uint32_t) address->local_info);
  908. addr = (char *) &msg[1];
  909. GNUNET_memcpy (addr, address->address, alen);
  910. GNUNET_memcpy (&addr[alen], address->transport_name, tlen);
  911. }
  912. return msg;
  913. }
  914. /**
  915. * Context for #send_validation_information() and
  916. * #send_peer_information().
  917. */
  918. struct IterationContext
  919. {
  920. /**
  921. * Context to use for the transmission.
  922. */
  923. struct TransportClient *tc;
  924. /**
  925. * Which peers do we care about?
  926. */
  927. struct GNUNET_PeerIdentity id;
  928. /**
  929. * #GNUNET_YES if @e id should be ignored because we want all peers.
  930. */
  931. int all;
  932. };
  933. /**
  934. * Output information of neighbours to the given client.
  935. *
  936. * @param cls the `struct PeerIterationContext *`
  937. * @param peer identity of the neighbour
  938. * @param address the address
  939. * @param state current state this peer is in
  940. * @param state_timeout timeout for the current state of the peer
  941. * @param bandwidth_in inbound quota in NBO
  942. * @param bandwidth_out outbound quota in NBO
  943. */
  944. static void
  945. send_peer_information (void *cls,
  946. const struct GNUNET_PeerIdentity *peer,
  947. const struct GNUNET_HELLO_Address *address,
  948. enum GNUNET_TRANSPORT_PeerState state,
  949. struct GNUNET_TIME_Absolute state_timeout,
  950. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  951. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
  952. {
  953. struct IterationContext *pc = cls;
  954. struct GNUNET_MQ_Envelope *env;
  955. struct PeerIterateResponseMessage *msg;
  956. if ((GNUNET_YES != pc->all) && (0 != memcmp (peer, &pc->id, sizeof(pc->id))))
  957. return;
  958. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  959. "Sending information about `%s' using address `%s' in state `%s'\n",
  960. GNUNET_i2s (peer),
  961. (NULL != address) ? GST_plugins_a2s (address) : "<none>",
  962. GNUNET_TRANSPORT_ps2s (state));
  963. msg = compose_address_iterate_response_message (peer, address);
  964. msg->state = htonl (state);
  965. msg->state_timeout = GNUNET_TIME_absolute_hton (state_timeout);
  966. env = GNUNET_MQ_msg_copy (&msg->header);
  967. GNUNET_free (msg);
  968. GNUNET_MQ_send (pc->tc->mq, env);
  969. }
  970. /**
  971. * Client asked to obtain information about a specific or all peers
  972. * Process the request.
  973. *
  974. * @param cls the client
  975. * @param msg the peer address information request
  976. */
  977. static void
  978. handle_client_monitor_peers (void *cls, const struct PeerMonitorMessage *msg)
  979. {
  980. struct TransportClient *tc = cls;
  981. struct IterationContext pc;
  982. if (CT_NONE != tc->type)
  983. {
  984. GNUNET_break (0);
  985. GNUNET_SERVICE_client_drop (tc->client);
  986. return;
  987. }
  988. GNUNET_SERVICE_client_disable_continue_warning (tc->client);
  989. GNUNET_SERVICE_client_mark_monitor (tc->client);
  990. /* Send initial list */
  991. pc.tc = tc;
  992. if (0 == memcmp (&msg->peer, &all_zeros, sizeof(struct GNUNET_PeerIdentity)))
  993. {
  994. /* iterate over all neighbours */
  995. pc.all = GNUNET_YES;
  996. pc.id = msg->peer;
  997. }
  998. else
  999. {
  1000. /* just return one neighbour */
  1001. pc.all = GNUNET_NO;
  1002. pc.id = msg->peer;
  1003. }
  1004. GST_neighbours_iterate (&send_peer_information, &pc);
  1005. if (GNUNET_YES != ntohl (msg->one_shot))
  1006. {
  1007. tc->details.monitor_peer = msg->peer;
  1008. tc->type = CT_MONITOR;
  1009. if (0 !=
  1010. memcmp (&msg->peer, &all_zeros, sizeof(struct GNUNET_PeerIdentity)))
  1011. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1012. "Client %p started monitoring of the peer `%s'\n",
  1013. tc,
  1014. GNUNET_i2s (&msg->peer));
  1015. else
  1016. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1017. "Client %p started monitoring all peers\n",
  1018. tc);
  1019. }
  1020. else
  1021. {
  1022. struct GNUNET_MessageHeader *msg;
  1023. struct GNUNET_MQ_Envelope *env;
  1024. env =
  1025. GNUNET_MQ_msg (msg,
  1026. GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE_END);
  1027. GNUNET_MQ_send (tc->mq, env);
  1028. }
  1029. }
  1030. /**
  1031. * Function called by the plugin with information about the
  1032. * current sessions managed by the plugin (for monitoring).
  1033. *
  1034. * @param cls closure
  1035. * @param session session handle this information is about,
  1036. * NULL to indicate that we are "in sync" (initial
  1037. * iteration complete)
  1038. * @param info information about the state of the session,
  1039. * NULL if @a session is also NULL and we are
  1040. * merely signalling that the initial iteration is over
  1041. */
  1042. static void
  1043. plugin_session_info_cb (void *cls,
  1044. struct GNUNET_ATS_Session *session,
  1045. const struct GNUNET_TRANSPORT_SessionInfo *info)
  1046. {
  1047. struct GNUNET_MQ_Envelope *env;
  1048. struct TransportPluginMonitorMessage *msg;
  1049. struct GNUNET_MessageHeader *sync;
  1050. size_t size;
  1051. size_t slen;
  1052. uint16_t alen;
  1053. char *name;
  1054. char *addr;
  1055. if (0 == GNUNET_notification_context_get_size (plugin_nc))
  1056. {
  1057. GST_plugins_monitor_subscribe (NULL, NULL);
  1058. return;
  1059. }
  1060. if ((NULL == info) && (NULL == session))
  1061. {
  1062. /* end of initial iteration */
  1063. if (NULL != sync_client)
  1064. {
  1065. env =
  1066. GNUNET_MQ_msg (sync, GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_SYNC);
  1067. GNUNET_MQ_send (sync_client->mq, env);
  1068. sync_client = NULL;
  1069. }
  1070. return;
  1071. }
  1072. GNUNET_assert (NULL != info);
  1073. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1074. "Plugin event for peer %s on transport %s\n",
  1075. GNUNET_i2s (&info->address->peer),
  1076. info->address->transport_name);
  1077. slen = strlen (info->address->transport_name) + 1;
  1078. alen = info->address->address_length;
  1079. size = sizeof(struct TransportPluginMonitorMessage) + slen + alen;
  1080. if (size > UINT16_MAX)
  1081. {
  1082. GNUNET_break (0);
  1083. return;
  1084. }
  1085. msg = GNUNET_malloc (size);
  1086. msg->header.size = htons (size);
  1087. msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_EVENT);
  1088. msg->session_state = htons ((uint16_t) info->state);
  1089. msg->is_inbound = htons ((int16_t) info->is_inbound);
  1090. msg->msgs_pending = htonl (info->num_msg_pending);
  1091. msg->bytes_pending = htonl (info->num_bytes_pending);
  1092. msg->timeout = GNUNET_TIME_absolute_hton (info->session_timeout);
  1093. msg->delay = GNUNET_TIME_absolute_hton (info->receive_delay);
  1094. msg->peer = info->address->peer;
  1095. msg->session_id = (uint64_t) (intptr_t) session;
  1096. msg->plugin_name_len = htons (slen);
  1097. msg->plugin_address_len = htons (alen);
  1098. name = (char *) &msg[1];
  1099. GNUNET_memcpy (name, info->address->transport_name, slen);
  1100. addr = &name[slen];
  1101. GNUNET_memcpy (addr, info->address->address, alen);
  1102. if (NULL != sync_client)
  1103. {
  1104. struct GNUNET_MQ_Envelope *env;
  1105. env = GNUNET_MQ_msg_copy (&msg->header);
  1106. GNUNET_MQ_send (sync_client->mq, env);
  1107. }
  1108. else
  1109. {
  1110. GNUNET_notification_context_broadcast (plugin_nc, &msg->header, GNUNET_NO);
  1111. }
  1112. GNUNET_free (msg);
  1113. }
  1114. /**
  1115. * Client asked to obtain information about all plugin connections.
  1116. *
  1117. * @param cls the client
  1118. * @param message the peer address information request
  1119. */
  1120. static void
  1121. handle_client_monitor_plugins (void *cls,
  1122. const struct GNUNET_MessageHeader *message)
  1123. {
  1124. struct TransportClient *tc = cls;
  1125. GNUNET_SERVICE_client_mark_monitor (tc->client);
  1126. GNUNET_SERVICE_client_disable_continue_warning (tc->client);
  1127. GNUNET_notification_context_add (plugin_nc, tc->mq);
  1128. GNUNET_assert (NULL == sync_client);
  1129. sync_client = tc;
  1130. GST_plugins_monitor_subscribe (&plugin_session_info_cb, NULL);
  1131. }
  1132. /**
  1133. * Broadcast the given message to all of our clients.
  1134. *
  1135. * @param msg message to broadcast
  1136. * @param may_drop #GNUNET_YES if the message can be dropped / is payload
  1137. */
  1138. void
  1139. GST_clients_broadcast (const struct GNUNET_MessageHeader *msg, int may_drop)
  1140. {
  1141. int done;
  1142. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1143. "Asked to broadcast message of type %u with %u bytes\n",
  1144. (unsigned int) ntohs (msg->type),
  1145. (unsigned int) ntohs (msg->size));
  1146. done = GNUNET_NO;
  1147. for (struct TransportClient *tc = clients_head; NULL != tc; tc = tc->next)
  1148. {
  1149. if (CT_NONE == tc->type)
  1150. continue; /* client not yet ready */
  1151. if ((GNUNET_YES == may_drop) && (CT_CORE != tc->type))
  1152. continue; /* skip, this client does not care about payload */
  1153. unicast (tc, msg, may_drop);
  1154. done = GNUNET_YES;
  1155. }
  1156. if (GNUNET_NO == done)
  1157. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1158. "Message of type %u not delivered, is CORE service up?\n",
  1159. ntohs (msg->type));
  1160. }
  1161. /**
  1162. * Broadcast the new active address to all clients monitoring the peer.
  1163. *
  1164. * @param peer peer this update is about (never NULL)
  1165. * @param address address, NULL on disconnect
  1166. * @param state the current state of the peer
  1167. * @param state_timeout the time out for the state
  1168. */
  1169. void
  1170. GST_clients_broadcast_peer_notification (
  1171. const struct GNUNET_PeerIdentity *peer,
  1172. const struct GNUNET_HELLO_Address *address,
  1173. enum GNUNET_TRANSPORT_PeerState state,
  1174. struct GNUNET_TIME_Absolute state_timeout)
  1175. {
  1176. struct GNUNET_MQ_Envelope *env;
  1177. struct PeerIterateResponseMessage *msg;
  1178. msg = compose_address_iterate_response_message (peer, address);
  1179. msg->state = htonl (state);
  1180. msg->state_timeout = GNUNET_TIME_absolute_hton (state_timeout);
  1181. for (struct TransportClient *tc = clients_head; NULL != tc; tc = tc->next)
  1182. {
  1183. if (CT_MONITOR != tc->type)
  1184. continue;
  1185. if ((0 == memcmp (&tc->details.monitor_peer,
  1186. &all_zeros,
  1187. sizeof(struct GNUNET_PeerIdentity))) ||
  1188. (0 == memcmp (&tc->details.monitor_peer,
  1189. peer,
  1190. sizeof(struct GNUNET_PeerIdentity))))
  1191. {
  1192. env = GNUNET_MQ_msg_copy (&msg->header);
  1193. GNUNET_MQ_send (tc->mq, env);
  1194. }
  1195. }
  1196. GNUNET_free (msg);
  1197. }
  1198. /**
  1199. * Mark the peer as down so we don't call the continuation
  1200. * context in the future.
  1201. *
  1202. * @param cls NULL
  1203. * @param peer peer that got disconnected
  1204. * @param value a `struct SendTransmitContinuationContext` to mark
  1205. * @return #GNUNET_OK (continue to iterate)
  1206. */
  1207. static int
  1208. mark_peer_down (void *cls, const struct GNUNET_PeerIdentity *peer, void *value)
  1209. {
  1210. struct SendTransmitContinuationContext *stcc = value;
  1211. stcc->down = GNUNET_YES;
  1212. return GNUNET_OK;
  1213. }
  1214. /**
  1215. * Notify all clients about a disconnect, and cancel
  1216. * pending SEND_OK messages for this peer.
  1217. *
  1218. * @param peer peer that disconnected
  1219. */
  1220. void
  1221. GST_clients_broadcast_disconnect (const struct GNUNET_PeerIdentity *peer)
  1222. {
  1223. struct DisconnectInfoMessage disconnect_msg;
  1224. GNUNET_CONTAINER_multipeermap_get_multiple (active_stccs,
  1225. peer,
  1226. &mark_peer_down,
  1227. NULL);
  1228. disconnect_msg.header.size = htons (sizeof(struct DisconnectInfoMessage));
  1229. disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
  1230. disconnect_msg.reserved = htonl (0);
  1231. disconnect_msg.peer = *peer;
  1232. GST_clients_broadcast (&disconnect_msg.header, GNUNET_NO);
  1233. }
  1234. /**
  1235. * Transmit our HELLO message to the given (connected) neighbour.
  1236. *
  1237. * @param cls the 'HELLO' message
  1238. * @param peer identity of the peer
  1239. * @param address the address
  1240. * @param state current state this peer is in
  1241. * @param state_timeout timeout for the current state of the peer
  1242. * @param bandwidth_in inbound quota in NBO
  1243. * @param bandwidth_out outbound quota in NBO
  1244. */
  1245. static void
  1246. transmit_our_hello (void *cls,
  1247. const struct GNUNET_PeerIdentity *peer,
  1248. const struct GNUNET_HELLO_Address *address,
  1249. enum GNUNET_TRANSPORT_PeerState state,
  1250. struct GNUNET_TIME_Absolute state_timeout,
  1251. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  1252. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
  1253. {
  1254. const struct GNUNET_MessageHeader *hello = cls;
  1255. if (0 == memcmp (peer, &GST_my_identity, sizeof(struct GNUNET_PeerIdentity)))
  1256. return; /* not to ourselves */
  1257. if (GNUNET_NO == GST_neighbours_test_connected (peer))
  1258. return;
  1259. GST_neighbours_send (peer,
  1260. hello,
  1261. ntohs (hello->size),
  1262. hello_expiration,
  1263. NULL,
  1264. NULL);
  1265. }
  1266. /**
  1267. * My HELLO has changed. Tell everyone who should know.
  1268. *
  1269. * @param cls unused
  1270. * @param hello new HELLO
  1271. */
  1272. static void
  1273. process_hello_update (void *cls, const struct GNUNET_MessageHeader *hello)
  1274. {
  1275. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Broadcasting HELLO to clients\n");
  1276. GST_clients_broadcast (hello, GNUNET_NO);
  1277. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Broadcasting HELLO to neighbours\n");
  1278. GST_neighbours_iterate (&transmit_our_hello, (void *) hello);
  1279. }
  1280. /**
  1281. * We received some payload. Prepare to pass it on to our clients.
  1282. *
  1283. * @param address address and (claimed) identity of the other peer
  1284. * @param session identifier used for this session (NULL for plugins
  1285. * that do not offer bi-directional communication to the sender
  1286. * using the same "connection")
  1287. * @param message the message to process
  1288. * @return how long the plugin should wait until receiving more data
  1289. */
  1290. static struct GNUNET_TIME_Relative
  1291. process_payload (const struct GNUNET_HELLO_Address *address,
  1292. struct GNUNET_ATS_Session *session,
  1293. const struct GNUNET_MessageHeader *message)
  1294. {
  1295. struct GNUNET_TIME_Relative ret;
  1296. int do_forward;
  1297. struct InboundMessage *im;
  1298. size_t msg_size = ntohs (message->size);
  1299. size_t size = sizeof(struct InboundMessage) + msg_size;
  1300. char buf[size] GNUNET_ALIGN;
  1301. do_forward = GNUNET_SYSERR;
  1302. ret = GST_neighbours_calculate_receive_delay (&address->peer,
  1303. msg_size,
  1304. &do_forward);
  1305. if (! GST_neighbours_test_connected (&address->peer))
  1306. {
  1307. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1308. "Discarded %u bytes type %u payload from peer `%s'\n",
  1309. (unsigned int) msg_size,
  1310. ntohs (message->type),
  1311. GNUNET_i2s (&address->peer));
  1312. GNUNET_STATISTICS_update (
  1313. GST_stats,
  1314. gettext_noop ("# bytes payload discarded due to not connected peer"),
  1315. msg_size,
  1316. GNUNET_NO);
  1317. return ret;
  1318. }
  1319. if (GNUNET_YES != do_forward)
  1320. return ret;
  1321. im = (struct InboundMessage *) buf;
  1322. im->header.size = htons (size);
  1323. im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
  1324. im->peer = address->peer;
  1325. GNUNET_memcpy (&im[1], message, ntohs (message->size));
  1326. GST_clients_broadcast (&im->header, GNUNET_YES);
  1327. return ret;
  1328. }
  1329. /**
  1330. * Task to asynchronously terminate a session.
  1331. *
  1332. * @param cls the `struct GNUNET_ATS_SessionKiller` with the information for the kill
  1333. */
  1334. static void
  1335. kill_session_task (void *cls)
  1336. {
  1337. struct GNUNET_ATS_SessionKiller *sk = cls;
  1338. sk->task = NULL;
  1339. GNUNET_CONTAINER_DLL_remove (sk_head, sk_tail, sk);
  1340. sk->plugin->disconnect_session (sk->plugin->cls, sk->session);
  1341. GNUNET_free (sk);
  1342. }
  1343. /**
  1344. * Force plugin to terminate session due to communication
  1345. * issue.
  1346. *
  1347. * @param plugin_name name of the plugin
  1348. * @param session session to termiante
  1349. */
  1350. static void
  1351. kill_session (const char *plugin_name, struct GNUNET_ATS_Session *session)
  1352. {
  1353. struct GNUNET_TRANSPORT_PluginFunctions *plugin;
  1354. struct GNUNET_ATS_SessionKiller *sk;
  1355. for (sk = sk_head; NULL != sk; sk = sk->next)
  1356. if (sk->session == session)
  1357. return;
  1358. plugin = GST_plugins_find (plugin_name);
  1359. if (NULL == plugin)
  1360. {
  1361. GNUNET_break (0);
  1362. return;
  1363. }
  1364. /* need to issue disconnect asynchronously */
  1365. sk = GNUNET_new (struct GNUNET_ATS_SessionKiller);
  1366. sk->session = session;
  1367. sk->plugin = plugin;
  1368. sk->task = GNUNET_SCHEDULER_add_now (&kill_session_task, sk);
  1369. GNUNET_CONTAINER_DLL_insert (sk_head, sk_tail, sk);
  1370. }
  1371. /**
  1372. * Black list check result for try_connect call
  1373. * If connection to the peer is allowed request adddress and ???
  1374. *
  1375. * @param cls the message
  1376. * @param peer the peer
  1377. * @param address the address
  1378. * @param session the session
  1379. * @param result the result
  1380. */
  1381. static void
  1382. connect_bl_check_cont (void *cls,
  1383. const struct GNUNET_PeerIdentity *peer,
  1384. const struct GNUNET_HELLO_Address *address,
  1385. struct GNUNET_ATS_Session *session,
  1386. int result)
  1387. {
  1388. struct GNUNET_MessageHeader *msg = cls;
  1389. if (GNUNET_OK == result)
  1390. {
  1391. /* Blacklist allows to speak to this peer, forward SYN to neighbours */
  1392. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1393. "Received SYN message from peer `%s' at `%s'\n",
  1394. GNUNET_i2s (peer),
  1395. GST_plugins_a2s (address));
  1396. if (GNUNET_OK != GST_neighbours_handle_session_syn (msg, peer))
  1397. {
  1398. GST_blacklist_abort_matching (address, session);
  1399. kill_session (address->transport_name, session);
  1400. }
  1401. GNUNET_free (msg);
  1402. return;
  1403. }
  1404. GNUNET_free (msg);
  1405. if (GNUNET_SYSERR == result)
  1406. return; /* check was aborted, session destroyed */
  1407. /* Blacklist denies to speak to this peer */
  1408. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1409. "Discarding SYN message from `%s' due to denied blacklist check\n",
  1410. GNUNET_i2s (peer));
  1411. kill_session (address->transport_name, session);
  1412. }
  1413. /**
  1414. * Function called by the transport for each received message.
  1415. *
  1416. * @param cls closure, const char* with the name of the plugin we received the message from
  1417. * @param address address and (claimed) identity of the other peer
  1418. * @param message the message, NULL if we only care about
  1419. * learning about the delay until we should receive again
  1420. * @param session identifier used for this session (NULL for plugins
  1421. * that do not offer bi-directional communication to the sender
  1422. * using the same "connection")
  1423. * @return how long the plugin should wait until receiving more data
  1424. * (plugins that do not support this, can ignore the return value)
  1425. */
  1426. struct GNUNET_TIME_Relative
  1427. GST_receive_callback (void *cls,
  1428. const struct GNUNET_HELLO_Address *address,
  1429. struct GNUNET_ATS_Session *session,
  1430. const struct GNUNET_MessageHeader *message)
  1431. {
  1432. const char *plugin_name = cls;
  1433. struct GNUNET_TIME_Relative ret;
  1434. uint16_t type;
  1435. ret = GNUNET_TIME_UNIT_ZERO;
  1436. if (NULL == message)
  1437. goto end;
  1438. type = ntohs (message->type);
  1439. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1440. "Received message with type %u from peer `%s' at %s\n",
  1441. type,
  1442. GNUNET_i2s (&address->peer),
  1443. GST_plugins_a2s (address));
  1444. GNUNET_STATISTICS_update (GST_stats,
  1445. gettext_noop ("# bytes total received"),
  1446. ntohs (message->size),
  1447. GNUNET_NO);
  1448. GST_neighbours_notify_data_recv (address, message);
  1449. switch (type)
  1450. {
  1451. case GNUNET_MESSAGE_TYPE_HELLO_LEGACY:
  1452. /* Legacy HELLO message, discard */
  1453. return ret;
  1454. case GNUNET_MESSAGE_TYPE_HELLO:
  1455. if (GNUNET_OK != GST_validation_handle_hello (message))
  1456. {
  1457. GNUNET_break_op (0);
  1458. GST_blacklist_abort_matching (address, session);
  1459. }
  1460. return ret;
  1461. case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
  1462. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1463. "Processing PING from `%s'\n",
  1464. GST_plugins_a2s (address));
  1465. if (GNUNET_OK !=
  1466. GST_validation_handle_ping (&address->peer, message, address, session))
  1467. {
  1468. GST_blacklist_abort_matching (address, session);
  1469. kill_session (plugin_name, session);
  1470. }
  1471. break;
  1472. case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
  1473. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1474. "Processing PONG from `%s'\n",
  1475. GST_plugins_a2s (address));
  1476. if (GNUNET_OK != GST_validation_handle_pong (&address->peer, message))
  1477. {
  1478. GNUNET_break_op (0);
  1479. GST_blacklist_abort_matching (address, session);
  1480. kill_session (plugin_name, session);
  1481. }
  1482. break;
  1483. case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN:
  1484. /* Do blacklist check if communication with this peer is allowed */
  1485. (void) GST_blacklist_test_allowed (&address->peer,
  1486. NULL,
  1487. &connect_bl_check_cont,
  1488. GNUNET_copy_message (message),
  1489. address,
  1490. session);
  1491. break;
  1492. case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK:
  1493. if (GNUNET_OK !=
  1494. GST_neighbours_handle_session_syn_ack (message, address, session))
  1495. {
  1496. GST_blacklist_abort_matching (address, session);
  1497. kill_session (plugin_name, session);
  1498. }
  1499. break;
  1500. case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK:
  1501. if (GNUNET_OK !=
  1502. GST_neighbours_handle_session_ack (message, address, session))
  1503. {
  1504. GNUNET_break_op (0);
  1505. GST_blacklist_abort_matching (address, session);
  1506. kill_session (plugin_name, session);
  1507. }
  1508. break;
  1509. case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT:
  1510. GST_neighbours_handle_disconnect_message (&address->peer, message);
  1511. break;
  1512. case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_QUOTA:
  1513. GST_neighbours_handle_quota_message (&address->peer, message);
  1514. break;
  1515. case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE:
  1516. GST_neighbours_keepalive (&address->peer, message);
  1517. break;
  1518. case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE:
  1519. GST_neighbours_keepalive_response (&address->peer, message);
  1520. break;
  1521. default:
  1522. /* should be payload */
  1523. GNUNET_STATISTICS_update (GST_stats,
  1524. gettext_noop ("# bytes payload received"),
  1525. ntohs (message->size),
  1526. GNUNET_NO);
  1527. ret = process_payload (address, session, message);
  1528. break;
  1529. }
  1530. end:
  1531. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1532. "Allowing receive from peer %s to continue in %s\n",
  1533. GNUNET_i2s (&address->peer),
  1534. GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
  1535. return ret;
  1536. }
  1537. /**
  1538. * Function that will be called for each address the transport
  1539. * is aware that it might be reachable under. Update our HELLO.
  1540. *
  1541. * @param cls name of the plugin (const char*)
  1542. * @param add_remove should the address added (YES) or removed (NO) from the
  1543. * set of valid addresses?
  1544. * @param address the address to add or remove
  1545. */
  1546. static void
  1547. plugin_env_address_change_notification (
  1548. void *cls,
  1549. int add_remove,
  1550. const struct GNUNET_HELLO_Address *address)
  1551. {
  1552. static int addresses = 0;
  1553. if (GNUNET_YES == add_remove)
  1554. {
  1555. addresses++;
  1556. GNUNET_STATISTICS_update (GST_stats, "# transport addresses", 1, GNUNET_NO);
  1557. }
  1558. else if (GNUNET_NO == add_remove)
  1559. {
  1560. if (0 == addresses)
  1561. {
  1562. GNUNET_break (0);
  1563. }
  1564. else
  1565. {
  1566. addresses--;
  1567. GNUNET_STATISTICS_update (GST_stats,
  1568. "# transport addresses",
  1569. -1,
  1570. GNUNET_NO);
  1571. }
  1572. }
  1573. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1574. "Transport now has %u addresses to communicate\n",
  1575. addresses);
  1576. GST_hello_modify_addresses (add_remove, address);
  1577. }
  1578. /**
  1579. * Function that will be called whenever the plugin internally
  1580. * cleans up a session pointer and hence the service needs to
  1581. * discard all of those sessions as well. Plugins that do not
  1582. * use sessions can simply omit calling this function and always
  1583. * use NULL wherever a session pointer is needed. This function
  1584. * should be called BEFORE a potential "TransmitContinuation"
  1585. * from the "TransmitFunction".
  1586. *
  1587. * @param cls closure
  1588. * @param address which address was the session for
  1589. * @param session which session is being destoyed
  1590. */
  1591. static void
  1592. plugin_env_session_end (void *cls,
  1593. const struct GNUNET_HELLO_Address *address,
  1594. struct GNUNET_ATS_Session *session)
  1595. {
  1596. struct GNUNET_ATS_SessionKiller *sk;
  1597. if (NULL == address)
  1598. {
  1599. GNUNET_break (0);
  1600. return;
  1601. }
  1602. if (NULL == session)
  1603. {
  1604. GNUNET_break (0);
  1605. return;
  1606. }
  1607. GNUNET_assert (strlen (address->transport_name) > 0);
  1608. GNUNET_log (
  1609. GNUNET_ERROR_TYPE_DEBUG,
  1610. "Notification from plugin about terminated session %p from peer `%s' address `%s'\n",
  1611. session,
  1612. GNUNET_i2s (&address->peer),
  1613. GST_plugins_a2s (address));
  1614. GST_neighbours_session_terminated (&address->peer, session);
  1615. GST_ats_del_session (address, session);
  1616. GST_blacklist_abort_matching (address, session);
  1617. for (sk = sk_head; NULL != sk; sk = sk->next)
  1618. {
  1619. if (sk->session == session)
  1620. {
  1621. GNUNET_CONTAINER_DLL_remove (sk_head, sk_tail, sk);
  1622. GNUNET_SCHEDULER_cancel (sk->task);
  1623. GNUNET_free (sk);
  1624. break;
  1625. }
  1626. }
  1627. }
  1628. /**
  1629. * Black list check result from blacklist check triggered when a
  1630. * plugin gave us a new session in #plugin_env_session_start(). If
  1631. * connection to the peer is disallowed, kill the session.
  1632. *
  1633. * @param cls NULL
  1634. * @param peer the peer
  1635. * @param address address associated with the request
  1636. * @param session session associated with the request
  1637. * @param result the result
  1638. */
  1639. static void
  1640. plugin_env_session_start_bl_check_cont (
  1641. void *cls,
  1642. const struct GNUNET_PeerIdentity *peer,
  1643. const struct GNUNET_HELLO_Address *address,
  1644. struct GNUNET_ATS_Session *session,
  1645. int result)
  1646. {
  1647. if (GNUNET_OK != result)
  1648. {
  1649. kill_session (address->transport_name, session);
  1650. return;
  1651. }
  1652. if (GNUNET_YES !=
  1653. GNUNET_HELLO_address_check_option (address,
  1654. GNUNET_HELLO_ADDRESS_INFO_INBOUND))
  1655. {
  1656. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1657. "Informing verifier about inbound session's address `%s'\n",
  1658. GST_plugins_a2s (address));
  1659. GST_validation_handle_address (address);
  1660. }
  1661. }
  1662. /**
  1663. * Plugin tells transport service about a new inbound session
  1664. *
  1665. * @param cls unused
  1666. * @param address the address
  1667. * @param session the new session
  1668. * @param scope network scope information
  1669. */
  1670. static void
  1671. plugin_env_session_start (void *cls,
  1672. const struct GNUNET_HELLO_Address *address,
  1673. struct GNUNET_ATS_Session *session,
  1674. enum GNUNET_NetworkType scope)
  1675. {
  1676. struct GNUNET_ATS_Properties prop;
  1677. if (NULL == address)
  1678. {
  1679. GNUNET_break (0);
  1680. return;
  1681. }
  1682. if (NULL == session)
  1683. {
  1684. GNUNET_break (0);
  1685. return;
  1686. }
  1687. GNUNET_log (
  1688. GNUNET_ERROR_TYPE_INFO,
  1689. "Notification from plugin `%s' about new session from peer `%s' address `%s'\n",
  1690. address->transport_name,
  1691. GNUNET_i2s (&address->peer),
  1692. GST_plugins_a2s (address));
  1693. if (GNUNET_YES ==
  1694. GNUNET_HELLO_address_check_option (address,
  1695. GNUNET_HELLO_ADDRESS_INFO_INBOUND))
  1696. {
  1697. /* inbound is always new, but outbound MAY already be known, but
  1698. for example for UNIX, we have symmetric connections and thus we
  1699. may not know the address yet; add if necessary! */
  1700. /* FIXME: maybe change API here so we just pass scope? */
  1701. memset (&prop, 0, sizeof(prop));
  1702. GNUNET_break (GNUNET_NT_UNSPECIFIED != scope);
  1703. prop.scope = scope;
  1704. GST_ats_add_inbound_address (address, session, &prop);
  1705. }
  1706. /* Do blacklist check if communication with this peer is allowed */
  1707. (void) GST_blacklist_test_allowed (&address->peer,
  1708. address->transport_name,
  1709. &plugin_env_session_start_bl_check_cont,
  1710. NULL,
  1711. address,
  1712. session);
  1713. }
  1714. /**
  1715. * Function called by ATS to notify the callee that the
  1716. * assigned bandwidth or address for a given peer was changed. If the
  1717. * callback is called with address/bandwidth assignments of zero, the
  1718. * ATS disconnect function will still be called once the disconnect
  1719. * actually happened.
  1720. *
  1721. * @param cls closure
  1722. * @param peer the peer this address is intended for
  1723. * @param address address to use (for peer given in address)
  1724. * @param session session to use (if available)
  1725. * @param bandwidth_out assigned outbound bandwidth for the connection in NBO,
  1726. * 0 to disconnect from peer
  1727. * @param bandwidth_in assigned inbound bandwidth for the connection in NBO,
  1728. * 0 to disconnect from peer
  1729. * @param ats ATS information
  1730. * @param ats_count number of @a ats elements
  1731. */
  1732. static void
  1733. ats_request_address_change (void *cls,
  1734. const struct GNUNET_PeerIdentity *peer,
  1735. const struct GNUNET_HELLO_Address *address,
  1736. struct GNUNET_ATS_Session *session,
  1737. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  1738. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
  1739. {
  1740. uint32_t bw_in = ntohl (bandwidth_in.value__);
  1741. uint32_t bw_out = ntohl (bandwidth_out.value__);
  1742. if (NULL == peer)
  1743. {
  1744. /* ATS service died, all suggestions become invalid!
  1745. (but we'll keep using the allocations for a little
  1746. while, to keep going while ATS restarts) */
  1747. /* FIXME: We should drop all
  1748. connections now, as ATS won't explicitly tell
  1749. us and be unaware of ongoing resource allocations! */
  1750. return;
  1751. }
  1752. /* ATS tells me to disconnect from peer */
  1753. if ((0 == bw_in) && (0 == bw_out))
  1754. {
  1755. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1756. "ATS tells me to disconnect from peer `%s'\n",
  1757. GNUNET_i2s (peer));
  1758. GST_neighbours_force_disconnect (peer);
  1759. return;
  1760. }
  1761. GNUNET_assert (NULL != address);
  1762. GNUNET_STATISTICS_update (GST_stats,
  1763. "# ATS suggestions received",
  1764. 1,
  1765. GNUNET_NO);
  1766. GST_neighbours_switch_to_address (address,
  1767. session,
  1768. bandwidth_in,
  1769. bandwidth_out);
  1770. }
  1771. /**
  1772. * Closure for #test_connection_ok().
  1773. */
  1774. struct TestConnectionContext
  1775. {
  1776. /**
  1777. * Is this the first neighbour we're checking?
  1778. */
  1779. int first;
  1780. /**
  1781. * Handle to the blacklisting client we need to ask.
  1782. */
  1783. struct TransportClient *tc;
  1784. };
  1785. /**
  1786. * Got the result about an existing connection from a new blacklister.
  1787. * Shutdown the neighbour if necessary.
  1788. *
  1789. * @param cls unused
  1790. * @param peer the neighbour that was investigated
  1791. * @param address address associated with the request
  1792. * @param session session associated with the request
  1793. * @param allowed #GNUNET_OK if we can keep it,
  1794. * #GNUNET_NO if we must shutdown the connection
  1795. */
  1796. static void
  1797. confirm_or_drop_neighbour (void *cls,
  1798. const struct GNUNET_PeerIdentity *peer,
  1799. const struct GNUNET_HELLO_Address *address,
  1800. struct GNUNET_ATS_Session *session,
  1801. int allowed)
  1802. {
  1803. if (GNUNET_OK == allowed)
  1804. return; /* we're done */
  1805. GNUNET_STATISTICS_update (GST_stats,
  1806. gettext_noop ("# disconnects due to blacklist"),
  1807. 1,
  1808. GNUNET_NO);
  1809. GST_neighbours_force_disconnect (peer);
  1810. }
  1811. /**
  1812. * Test if an existing connection is still acceptable given a new
  1813. * blacklisting client.
  1814. *
  1815. * @param cls the `struct TestConnectionContext *`
  1816. * @param peer identity of the peer
  1817. * @param address the address
  1818. * @param state current state this peer is in
  1819. * @param state_timeout timeout for the current state of the peer
  1820. * @param bandwidth_in bandwidth assigned inbound
  1821. * @param bandwidth_out bandwidth assigned outbound
  1822. */
  1823. static void
  1824. test_connection_ok (void *cls,
  1825. const struct GNUNET_PeerIdentity *peer,
  1826. const struct GNUNET_HELLO_Address *address,
  1827. enum GNUNET_TRANSPORT_PeerState state,
  1828. struct GNUNET_TIME_Absolute state_timeout,
  1829. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  1830. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
  1831. {
  1832. struct TestConnectionContext *tcc = cls;
  1833. struct GST_BlacklistCheck *bc;
  1834. bc = GNUNET_new (struct GST_BlacklistCheck);
  1835. GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
  1836. bc->peer = *peer;
  1837. bc->address = GNUNET_HELLO_address_copy (address);
  1838. bc->cont = &confirm_or_drop_neighbour;
  1839. bc->cont_cls = NULL;
  1840. bc->bl_pos = tcc->tc;
  1841. if (GNUNET_YES == tcc->first)
  1842. {
  1843. /* all would wait for the same client, no need to
  1844. * create more than just the first task right now */
  1845. bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
  1846. tcc->first = GNUNET_NO;
  1847. }
  1848. }
  1849. /**
  1850. * Initialize a blacklisting client. We got a blacklist-init
  1851. * message from this client, add it to the list of clients
  1852. * to query for blacklisting.
  1853. *
  1854. * @param cls the client
  1855. * @param message the blacklist-init message that was sent
  1856. */
  1857. static void
  1858. handle_client_blacklist_init (void *cls,
  1859. const struct GNUNET_MessageHeader *message)
  1860. {
  1861. struct TransportClient *tc = cls;
  1862. struct TestConnectionContext tcc;
  1863. if (CT_NONE != tc->type)
  1864. {
  1865. GNUNET_break (0);
  1866. GNUNET_SERVICE_client_drop (tc->client);
  1867. return;
  1868. }
  1869. GNUNET_SERVICE_client_mark_monitor (tc->client);
  1870. tc->type = CT_BLACKLIST;
  1871. tc->details.blacklist.call_receive_done = GNUNET_YES;
  1872. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New blacklist client %p\n", tc);
  1873. /* confirm that all existing connections are OK! */
  1874. tcc.tc = tc;
  1875. tcc.first = GNUNET_YES;
  1876. GST_neighbours_iterate (&test_connection_ok, &tcc);
  1877. }
  1878. /**
  1879. * Free the given entry in the blacklist.
  1880. *
  1881. * @param cls unused
  1882. * @param key host identity (unused)
  1883. * @param value the blacklist entry
  1884. * @return #GNUNET_OK (continue to iterate)
  1885. */
  1886. static int
  1887. free_blacklist_entry (void *cls,
  1888. const struct GNUNET_PeerIdentity *key,
  1889. void *value)
  1890. {
  1891. char *be = value;
  1892. GNUNET_free (be);
  1893. return GNUNET_OK;
  1894. }
  1895. /**
  1896. * Set traffic metric to manipulate
  1897. *
  1898. * @param cls closure
  1899. * @param message containing information
  1900. */
  1901. static void
  1902. handle_client_set_metric (void *cls, const struct TrafficMetricMessage *tm)
  1903. {
  1904. struct TransportClient *tc = cls;
  1905. GST_manipulation_set_metric (tm);
  1906. GNUNET_SERVICE_client_continue (tc->client);
  1907. }
  1908. /**
  1909. * Function called when the service shuts down. Unloads our plugins
  1910. * and cancels pending validations.
  1911. *
  1912. * @param cls closure, unused
  1913. */
  1914. static void
  1915. shutdown_task (void *cls)
  1916. {
  1917. struct AddressToStringContext *cur;
  1918. GST_neighbours_stop ();
  1919. GST_plugins_unload ();
  1920. GST_validation_stop ();
  1921. GST_ats_done ();
  1922. GNUNET_ATS_scheduling_done (GST_ats);
  1923. GST_ats = NULL;
  1924. GNUNET_ATS_connectivity_done (GST_ats_connect);
  1925. GST_ats_connect = NULL;
  1926. GNUNET_NT_scanner_done (GST_is);
  1927. GST_is = NULL;
  1928. while (NULL != (cur = a2s_head))
  1929. {
  1930. GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, cur);
  1931. GNUNET_free (cur);
  1932. }
  1933. if (NULL != plugin_nc)
  1934. {
  1935. GNUNET_notification_context_destroy (plugin_nc);
  1936. plugin_nc = NULL;
  1937. }
  1938. GNUNET_CONTAINER_multipeermap_destroy (active_stccs);
  1939. active_stccs = NULL;
  1940. if (NULL != blacklist)
  1941. {
  1942. GNUNET_CONTAINER_multipeermap_iterate (blacklist,
  1943. &free_blacklist_entry,
  1944. NULL);
  1945. GNUNET_CONTAINER_multipeermap_destroy (blacklist);
  1946. blacklist = NULL;
  1947. }
  1948. GST_hello_stop ();
  1949. GST_manipulation_stop ();
  1950. if (NULL != GST_peerinfo)
  1951. {
  1952. GNUNET_PEERINFO_disconnect (GST_peerinfo);
  1953. GST_peerinfo = NULL;
  1954. }
  1955. if (NULL != GST_stats)
  1956. {
  1957. GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
  1958. GST_stats = NULL;
  1959. }
  1960. }
  1961. /**
  1962. * Perform next action in the blacklist check.
  1963. *
  1964. * @param cls the `struct GST_BlacklistCheck *`
  1965. */
  1966. static void
  1967. do_blacklist_check (void *cls)
  1968. {
  1969. struct GST_BlacklistCheck *bc = cls;
  1970. struct TransportClient *tc;
  1971. struct GNUNET_MQ_Envelope *env;
  1972. struct BlacklistMessage *bm;
  1973. bc->task = NULL;
  1974. while (NULL != (tc = bc->bl_pos))
  1975. {
  1976. if (CT_BLACKLIST == tc->type)
  1977. break;
  1978. bc->bl_pos = tc->next;
  1979. }
  1980. if (NULL == tc)
  1981. {
  1982. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1983. "No other blacklist clients active, will allow neighbour `%s'\n",
  1984. GNUNET_i2s (&bc->peer));
  1985. bc->cont (bc->cont_cls, &bc->peer, bc->address, bc->session, GNUNET_OK);
  1986. GST_blacklist_test_cancel (bc);
  1987. return;
  1988. }
  1989. if ((NULL != tc->details.blacklist.bc) ||
  1990. (GNUNET_NO != tc->details.blacklist.waiting_for_reply))
  1991. return; /* someone else busy with this client */
  1992. tc->details.blacklist.bc = bc;
  1993. env = GNUNET_MQ_msg (bm, GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
  1994. bm->is_allowed = htonl (0);
  1995. bm->peer = bc->peer;
  1996. GNUNET_MQ_send (tc->mq, env);
  1997. if (GNUNET_YES == tc->details.blacklist.call_receive_done)
  1998. {
  1999. tc->details.blacklist.call_receive_done = GNUNET_NO;
  2000. GNUNET_SERVICE_client_continue (tc->client);
  2001. }
  2002. tc->details.blacklist.waiting_for_reply = GNUNET_YES;
  2003. }
  2004. /**
  2005. * A blacklisting client has sent us reply. Process it.
  2006. *
  2007. * @param cls the client
  2008. * @param msg the blacklist-reply message that was sent
  2009. */
  2010. static void
  2011. handle_client_blacklist_reply (void *cls, const struct BlacklistMessage *msg)
  2012. {
  2013. struct TransportClient *tc = cls;
  2014. struct GST_BlacklistCheck *bc;
  2015. if (CT_BLACKLIST != tc->type)
  2016. {
  2017. GNUNET_break (0);
  2018. GNUNET_SERVICE_client_drop (tc->client);
  2019. return;
  2020. }
  2021. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2022. "Blacklist client %p sent reply for `%s'\n",
  2023. tc,
  2024. GNUNET_i2s (&msg->peer));
  2025. bc = tc->details.blacklist.bc;
  2026. tc->details.blacklist.bc = NULL;
  2027. tc->details.blacklist.waiting_for_reply = GNUNET_NO;
  2028. tc->details.blacklist.call_receive_done = GNUNET_YES;
  2029. if (NULL != bc)
  2030. {
  2031. /* only run this if the blacklist check has not been
  2032. * cancelled in the meantime... */
  2033. GNUNET_assert (bc->bl_pos == tc);
  2034. if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
  2035. {
  2036. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2037. "Blacklist check failed, peer not allowed\n");
  2038. /* For the duration of the continuation, make the ongoing
  2039. check invisible (to avoid double-cancellation); then
  2040. add it back again so we can re-use GST_blacklist_test_cancel() */
  2041. GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
  2042. bc->cont (bc->cont_cls, &bc->peer, bc->address, bc->session, GNUNET_NO);
  2043. GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
  2044. GST_blacklist_test_cancel (bc);
  2045. tc->details.blacklist.call_receive_done = GNUNET_NO;
  2046. GNUNET_SERVICE_client_continue (tc->client);
  2047. return;
  2048. }
  2049. else
  2050. {
  2051. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2052. "Blacklist check succeeded, continuing with checks\n");
  2053. tc->details.blacklist.call_receive_done = GNUNET_NO;
  2054. GNUNET_SERVICE_client_continue (tc->client);
  2055. bc->bl_pos = tc->next;
  2056. bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
  2057. }
  2058. }
  2059. /* check if any other blacklist checks are waiting for this blacklister */
  2060. for (bc = bc_head; bc != NULL; bc = bc->next)
  2061. if ((bc->bl_pos == tc) && (NULL == bc->task))
  2062. {
  2063. bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
  2064. break;
  2065. }
  2066. }
  2067. /**
  2068. * Add the given peer to the blacklist (for the given transport).
  2069. *
  2070. * @param peer peer to blacklist
  2071. * @param transport_name transport to blacklist for this peer, NULL for all
  2072. */
  2073. void
  2074. GST_blacklist_add_peer (const struct GNUNET_PeerIdentity *peer,
  2075. const char *transport_name)
  2076. {
  2077. char *transport = NULL;
  2078. if (NULL != transport_name)
  2079. {
  2080. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2081. "Adding peer `%s' with plugin `%s' to blacklist\n",
  2082. GNUNET_i2s (peer),
  2083. transport_name);
  2084. transport = GNUNET_strdup (transport_name);
  2085. }
  2086. else
  2087. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2088. "Adding peer `%s' with all plugins to blacklist\n",
  2089. GNUNET_i2s (peer));
  2090. if (NULL == blacklist)
  2091. blacklist =
  2092. GNUNET_CONTAINER_multipeermap_create (TRANSPORT_BLACKLIST_HT_SIZE,
  2093. GNUNET_NO);
  2094. GNUNET_CONTAINER_multipeermap_put (blacklist,
  2095. peer,
  2096. transport,
  2097. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  2098. }
  2099. /**
  2100. * Abort blacklist if @a address and @a session match.
  2101. *
  2102. * @param address address used to abort matching checks
  2103. * @param session session used to abort matching checks
  2104. */
  2105. void
  2106. GST_blacklist_abort_matching (const struct GNUNET_HELLO_Address *address,
  2107. struct GNUNET_ATS_Session *session)
  2108. {
  2109. struct GST_BlacklistCheck *bc;
  2110. struct GST_BlacklistCheck *n;
  2111. n = bc_head;
  2112. while (NULL != (bc = n))
  2113. {
  2114. n = bc->next;
  2115. if ((bc->session == session) &&
  2116. (0 == GNUNET_HELLO_address_cmp (bc->address, address)))
  2117. {
  2118. bc->cont (bc->cont_cls,
  2119. &bc->peer,
  2120. bc->address,
  2121. bc->session,
  2122. GNUNET_SYSERR);
  2123. GST_blacklist_test_cancel (bc);
  2124. }
  2125. }
  2126. }
  2127. /**
  2128. * Test if the given blacklist entry matches. If so,
  2129. * abort the iteration.
  2130. *
  2131. * @param cls the transport name to match (const char*)
  2132. * @param key the key (unused)
  2133. * @param value the 'char *' (name of a blacklisted transport)
  2134. * @return #GNUNET_OK if the entry does not match, #GNUNET_NO if it matches
  2135. */
  2136. static int
  2137. test_blacklisted (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
  2138. {
  2139. const char *transport_name = cls;
  2140. char *be = value;
  2141. /* Blacklist entry be:
  2142. * (NULL == be): peer is blacklisted with all plugins
  2143. * (NULL != be): peer is blacklisted for a specific plugin
  2144. *
  2145. * If (NULL != transport_name) we look for a transport specific entry:
  2146. * if (transport_name == be) forbidden
  2147. *
  2148. */GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2149. "Comparing BL request for peer `%4s':`%s' with BL entry: `%s'\n",
  2150. GNUNET_i2s (key),
  2151. (NULL == transport_name) ? "unspecified" : transport_name,
  2152. (NULL == be) ? "all plugins" : be);
  2153. /* all plugins for this peer were blacklisted: disallow */
  2154. if (NULL == value)
  2155. return GNUNET_NO;
  2156. /* blacklist check for specific transport */
  2157. if ((NULL != transport_name) && (NULL != value))
  2158. {
  2159. if (0 == strcmp (transport_name, be))
  2160. return GNUNET_NO; /* plugin is blacklisted! */
  2161. }
  2162. return GNUNET_OK;
  2163. }
  2164. /**
  2165. * Test if a peer/transport combination is blacklisted.
  2166. *
  2167. * @param peer the identity of the peer to test
  2168. * @param transport_name name of the transport to test, never NULL
  2169. * @param cont function to call with result
  2170. * @param cont_cls closure for @a cont
  2171. * @param address address to pass back to @a cont, can be NULL
  2172. * @param session session to pass back to @a cont, can be NULL
  2173. * @return handle to the blacklist check, NULL if the decision
  2174. * was made instantly and @a cont was already called
  2175. */
  2176. struct GST_BlacklistCheck *
  2177. GST_blacklist_test_allowed (const struct GNUNET_PeerIdentity *peer,
  2178. const char *transport_name,
  2179. GST_BlacklistTestContinuation cont,
  2180. void *cont_cls,
  2181. const struct GNUNET_HELLO_Address *address,
  2182. struct GNUNET_ATS_Session *session)
  2183. {
  2184. struct GST_BlacklistCheck *bc;
  2185. struct TransportClient *tc;
  2186. GNUNET_assert (NULL != peer);
  2187. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2188. "Blacklist check for peer `%s':%s\n",
  2189. GNUNET_i2s (peer),
  2190. (NULL != transport_name) ? transport_name : "unspecified");
  2191. /* Check local blacklist by iterating over hashmap
  2192. * If iteration is aborted, we found a matching blacklist entry */
  2193. if ((NULL != blacklist) &&
  2194. (GNUNET_SYSERR ==
  2195. GNUNET_CONTAINER_multipeermap_get_multiple (blacklist,
  2196. peer,
  2197. &test_blacklisted,
  2198. (void *) transport_name)))
  2199. {
  2200. /* Disallowed by config, disapprove instantly */
  2201. GNUNET_STATISTICS_update (GST_stats,
  2202. gettext_noop ("# disconnects due to blacklist"),
  2203. 1,
  2204. GNUNET_NO);
  2205. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2206. _ ("Disallowing connection to peer `%s' on transport %s\n"),
  2207. GNUNET_i2s (peer),
  2208. (NULL != transport_name) ? transport_name : "unspecified");
  2209. if (NULL != cont)
  2210. cont (cont_cls, peer, address, session, GNUNET_NO);
  2211. return NULL;
  2212. }
  2213. for (tc = clients_head; NULL != tc; tc = tc->next)
  2214. if (CT_BLACKLIST == tc->type)
  2215. break;
  2216. if (NULL == tc)
  2217. {
  2218. /* no blacklist clients, approve instantly */
  2219. if (NULL != cont)
  2220. cont (cont_cls, peer, address, session, GNUNET_OK);
  2221. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2222. "Allowing connection to peer `%s' %s\n",
  2223. GNUNET_i2s (peer),
  2224. (NULL != transport_name) ? transport_name : "");
  2225. return NULL;
  2226. }
  2227. /* need to query blacklist clients */
  2228. bc = GNUNET_new (struct GST_BlacklistCheck);
  2229. GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
  2230. bc->peer = *peer;
  2231. bc->address = GNUNET_HELLO_address_copy (address);
  2232. bc->session = session;
  2233. bc->cont = cont;
  2234. bc->cont_cls = cont_cls;
  2235. bc->bl_pos = tc;
  2236. bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
  2237. return bc;
  2238. }
  2239. /**
  2240. * Cancel a blacklist check.
  2241. *
  2242. * @param bc check to cancel
  2243. */
  2244. void
  2245. GST_blacklist_test_cancel (struct GST_BlacklistCheck *bc)
  2246. {
  2247. GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
  2248. if (NULL != bc->bl_pos)
  2249. {
  2250. if ((CT_BLACKLIST == bc->bl_pos->type) &&
  2251. (bc->bl_pos->details.blacklist.bc == bc))
  2252. {
  2253. /* we're at the head of the queue, remove us! */
  2254. bc->bl_pos->details.blacklist.bc = NULL;
  2255. }
  2256. }
  2257. if (NULL != bc->task)
  2258. {
  2259. GNUNET_SCHEDULER_cancel (bc->task);
  2260. bc->task = NULL;
  2261. }
  2262. GNUNET_free (bc->address);
  2263. GNUNET_free (bc);
  2264. }
  2265. /**
  2266. * Function to iterate over options in the blacklisting section for a peer.
  2267. *
  2268. * @param cls closure
  2269. * @param section name of the section
  2270. * @param option name of the option
  2271. * @param value value of the option
  2272. */
  2273. static void
  2274. blacklist_cfg_iter (void *cls,
  2275. const char *section,
  2276. const char *option,
  2277. const char *value)
  2278. {
  2279. unsigned int *res = cls;
  2280. struct GNUNET_PeerIdentity peer;
  2281. char *plugs;
  2282. char *pos;
  2283. if (GNUNET_OK !=
  2284. GNUNET_CRYPTO_eddsa_public_key_from_string (option,
  2285. strlen (option),
  2286. &peer.public_key))
  2287. return;
  2288. if ((NULL == value) || (0 == strcmp (value, "")))
  2289. {
  2290. /* Blacklist whole peer */
  2291. GST_blacklist_add_peer (&peer, NULL);
  2292. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2293. _ ("Adding blacklisting entry for peer `%s'\n"),
  2294. GNUNET_i2s (&peer));
  2295. }
  2296. else
  2297. {
  2298. plugs = GNUNET_strdup (value);
  2299. for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
  2300. {
  2301. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2302. _ ("Adding blacklisting entry for peer `%s':`%s'\n"),
  2303. GNUNET_i2s (&peer),
  2304. pos);
  2305. GST_blacklist_add_peer (&peer, pos);
  2306. }
  2307. GNUNET_free (plugs);
  2308. }
  2309. (*res)++;
  2310. }
  2311. /**
  2312. * Read blacklist configuration
  2313. *
  2314. * @param cfg the configuration handle
  2315. * @param my_id my peer identity
  2316. */
  2317. static void
  2318. read_blacklist_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg,
  2319. const struct GNUNET_PeerIdentity *my_id)
  2320. {
  2321. char cfg_sect[512];
  2322. unsigned int res = 0;
  2323. GNUNET_snprintf (cfg_sect,
  2324. sizeof(cfg_sect),
  2325. "transport-blacklist-%s",
  2326. GNUNET_i2s_full (my_id));
  2327. GNUNET_CONFIGURATION_iterate_section_values (cfg,
  2328. cfg_sect,
  2329. &blacklist_cfg_iter,
  2330. &res);
  2331. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2332. "Loaded %u blacklisting entries from configuration\n",
  2333. res);
  2334. }
  2335. /**
  2336. * Initiate transport service.
  2337. *
  2338. * @param cls closure
  2339. * @param c configuration to use
  2340. * @param service the initialized service
  2341. */
  2342. static void
  2343. run (void *cls,
  2344. const struct GNUNET_CONFIGURATION_Handle *c,
  2345. struct GNUNET_SERVICE_Handle *service)
  2346. {
  2347. char *keyfile;
  2348. long long unsigned int max_fd_cfg;
  2349. int max_fd_rlimit;
  2350. int max_fd;
  2351. int friend_only;
  2352. /* setup globals */
  2353. GST_cfg = c;
  2354. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c,
  2355. "PEER",
  2356. "PRIVATE_KEY",
  2357. &keyfile))
  2358. {
  2359. GNUNET_log (
  2360. GNUNET_ERROR_TYPE_ERROR,
  2361. _ (
  2362. "Transport service is lacking key configuration settings. Exiting.\n"));
  2363. GNUNET_SCHEDULER_shutdown ();
  2364. return;
  2365. }
  2366. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (c,
  2367. "transport",
  2368. "HELLO_EXPIRATION",
  2369. &hello_expiration))
  2370. {
  2371. hello_expiration = GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION;
  2372. }
  2373. if (GNUNET_SYSERR ==
  2374. GNUNET_CRYPTO_eddsa_key_from_file (keyfile,
  2375. GNUNET_YES,
  2376. &GST_my_private_key))
  2377. {
  2378. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2379. "Failed to setup peer's private key\n");
  2380. GNUNET_SCHEDULER_shutdown ();
  2381. GNUNET_free (keyfile);
  2382. return;
  2383. }
  2384. GNUNET_free (keyfile);
  2385. GST_stats = GNUNET_STATISTICS_create ("transport", GST_cfg);
  2386. GST_peerinfo = GNUNET_PEERINFO_connect (GST_cfg);
  2387. GNUNET_CRYPTO_eddsa_key_get_public (&GST_my_private_key,
  2388. &GST_my_identity.public_key);
  2389. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2390. "My identity is `%s'\n",
  2391. GNUNET_i2s_full (&GST_my_identity));
  2392. GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
  2393. if (NULL == GST_peerinfo)
  2394. {
  2395. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2396. _ ("Could not access PEERINFO service. Exiting.\n"));
  2397. GNUNET_SCHEDULER_shutdown ();
  2398. return;
  2399. }
  2400. max_fd_rlimit = 0;
  2401. #if HAVE_GETRLIMIT
  2402. {
  2403. struct rlimit r_file;
  2404. if (0 == getrlimit (RLIMIT_NOFILE, &r_file))
  2405. {
  2406. max_fd_rlimit = r_file.rlim_cur;
  2407. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2408. "Maximum number of open files was: %u/%u\n",
  2409. (unsigned int) r_file.rlim_cur,
  2410. (unsigned int) r_file.rlim_max);
  2411. }
  2412. max_fd_rlimit =
  2413. (9 * max_fd_rlimit) / 10; /* Keep 10% for rest of transport */
  2414. }
  2415. #endif
  2416. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (GST_cfg,
  2417. "transport",
  2418. "MAX_FD",
  2419. &max_fd_cfg))
  2420. max_fd_cfg = max_fd_rlimit;
  2421. if (max_fd_cfg > max_fd_rlimit)
  2422. max_fd = max_fd_cfg;
  2423. else
  2424. max_fd = max_fd_rlimit;
  2425. if (max_fd < DEFAULT_MAX_FDS)
  2426. max_fd = DEFAULT_MAX_FDS;
  2427. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2428. "Limiting number of sockets to %u: validation %u, neighbors: %u\n",
  2429. max_fd,
  2430. (max_fd / 3),
  2431. (max_fd / 3) * 2);
  2432. friend_only =
  2433. GNUNET_CONFIGURATION_get_value_yesno (GST_cfg, "topology", "FRIENDS-ONLY");
  2434. if (GNUNET_SYSERR == friend_only)
  2435. friend_only = GNUNET_NO; /* According to topology defaults */
  2436. /* start subsystems */
  2437. /* Disable DSTJ peer */
  2438. {
  2439. struct GNUNET_PeerIdentity dstj;
  2440. const char *ds = "DSTJBRRKZ8TBW3FGK6B0M5QXWT9WYNZ45H5MCV4HY7ST64Q8T9F0";
  2441. GNUNET_assert (
  2442. GNUNET_OK ==
  2443. GNUNET_CRYPTO_eddsa_public_key_from_string (ds,
  2444. strlen (ds),
  2445. &dstj.public_key));
  2446. GST_blacklist_add_peer (&dstj, NULL);
  2447. }
  2448. read_blacklist_configuration (GST_cfg, &GST_my_identity);
  2449. GST_is = GNUNET_NT_scanner_init ();
  2450. GST_ats_connect = GNUNET_ATS_connectivity_init (GST_cfg);
  2451. GST_ats =
  2452. GNUNET_ATS_scheduling_init (GST_cfg, &ats_request_address_change, NULL);
  2453. GST_ats_init ();
  2454. GST_manipulation_init ();
  2455. GST_plugins_load (&GST_manipulation_recv,
  2456. &plugin_env_address_change_notification,
  2457. &plugin_env_session_start,
  2458. &plugin_env_session_end);
  2459. GST_hello_start (friend_only, &process_hello_update, NULL);
  2460. GST_neighbours_start ((max_fd / 3) * 2);
  2461. active_stccs = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
  2462. plugin_nc = GNUNET_notification_context_create (0);
  2463. GST_validation_start ((max_fd / 3));
  2464. }
  2465. /**
  2466. * Define "main" method using service macro.
  2467. */
  2468. GNUNET_SERVICE_MAIN (
  2469. "transport",
  2470. GNUNET_SERVICE_OPTION_NONE,
  2471. &run,
  2472. &client_connect_cb,
  2473. &client_disconnect_cb,
  2474. NULL,
  2475. GNUNET_MQ_hd_fixed_size (client_start,
  2476. GNUNET_MESSAGE_TYPE_TRANSPORT_START,
  2477. struct StartMessage,
  2478. NULL),
  2479. GNUNET_MQ_hd_var_size (client_hello,
  2480. GNUNET_MESSAGE_TYPE_HELLO,
  2481. struct GNUNET_MessageHeader,
  2482. NULL),
  2483. GNUNET_MQ_hd_var_size (client_send,
  2484. GNUNET_MESSAGE_TYPE_TRANSPORT_SEND,
  2485. struct OutboundMessage,
  2486. NULL),
  2487. GNUNET_MQ_hd_var_size (client_address_to_string,
  2488. GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING,
  2489. struct AddressLookupMessage,
  2490. NULL),
  2491. GNUNET_MQ_hd_fixed_size (client_monitor_peers,
  2492. GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST,
  2493. struct PeerMonitorMessage,
  2494. NULL),
  2495. GNUNET_MQ_hd_fixed_size (client_blacklist_init,
  2496. GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT,
  2497. struct GNUNET_MessageHeader,
  2498. NULL),
  2499. GNUNET_MQ_hd_fixed_size (client_blacklist_reply,
  2500. GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY,
  2501. struct BlacklistMessage,
  2502. NULL),
  2503. GNUNET_MQ_hd_fixed_size (client_set_metric,
  2504. GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC,
  2505. struct TrafficMetricMessage,
  2506. NULL),
  2507. GNUNET_MQ_hd_fixed_size (client_monitor_plugins,
  2508. GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_START,
  2509. struct GNUNET_MessageHeader,
  2510. NULL),
  2511. GNUNET_MQ_handler_end ());
  2512. /* end of file gnunet-service-transport.c */