gnunet-service-transport.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785
  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. struct GNUNET_TIME_Relative delay;
  597. const struct GNUNET_HELLO_Address *addr;
  598. delay = GNUNET_TIME_absolute_get_duration (stcc->send_time);
  599. addr = GST_neighbour_get_current_address (&stcc->target);
  600. if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
  601. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  602. "It took us %s to send %u/%u bytes to %s (%d, %s)\n",
  603. GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES),
  604. (unsigned int) bytes_payload,
  605. (unsigned int) bytes_on_wire,
  606. GNUNET_i2s (&stcc->target),
  607. success,
  608. (NULL != addr) ? addr->transport_name : "%");
  609. else
  610. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  611. "It took us %s to send %u/%u bytes to %s (%d, %s)\n",
  612. GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES),
  613. (unsigned int) bytes_payload,
  614. (unsigned int) bytes_on_wire,
  615. GNUNET_i2s (&stcc->target),
  616. success,
  617. (NULL != addr) ? addr->transport_name : "%");
  618. if (GNUNET_NO == stcc->down)
  619. {
  620. /* Only send confirmation if we are still connected */
  621. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  622. "Sending SEND_OK for transmission request %llu\n",
  623. stcc->uuid);
  624. send_ok_msg.header.size = htons (sizeof(send_ok_msg));
  625. send_ok_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_OK);
  626. send_ok_msg.bytes_msg = htonl (bytes_payload);
  627. send_ok_msg.bytes_physical = htonl (bytes_on_wire);
  628. send_ok_msg.success = htonl (success);
  629. send_ok_msg.peer = stcc->target;
  630. unicast (stcc->tc, &send_ok_msg.header, GNUNET_NO);
  631. }
  632. GNUNET_assert (
  633. GNUNET_OK ==
  634. GNUNET_CONTAINER_multipeermap_remove (active_stccs, &stcc->target, stcc));
  635. GNUNET_free (stcc);
  636. }
  637. /**
  638. * Client asked for transmission to a peer. Process the request.
  639. *
  640. * @param cls the client
  641. * @param obm the send message that was sent
  642. */
  643. static int
  644. check_client_send (void *cls, const struct OutboundMessage *obm)
  645. {
  646. uint16_t size;
  647. const struct GNUNET_MessageHeader *obmm;
  648. size = ntohs (obm->header.size) - sizeof(struct OutboundMessage);
  649. if (size < sizeof(struct GNUNET_MessageHeader))
  650. {
  651. GNUNET_break (0);
  652. return GNUNET_SYSERR;
  653. }
  654. obmm = (const struct GNUNET_MessageHeader *) &obm[1];
  655. if (size != ntohs (obmm->size))
  656. {
  657. GNUNET_break (0);
  658. return GNUNET_SYSERR;
  659. }
  660. return GNUNET_OK;
  661. }
  662. /**
  663. * Client asked for transmission to a peer. Process the request.
  664. *
  665. * @param cls the client
  666. * @param obm the send message that was sent
  667. */
  668. static void
  669. handle_client_send (void *cls, const struct OutboundMessage *obm)
  670. {
  671. static unsigned long long uuid_gen;
  672. struct TransportClient *tc = cls;
  673. const struct GNUNET_MessageHeader *obmm;
  674. struct SendTransmitContinuationContext *stcc;
  675. obmm = (const struct GNUNET_MessageHeader *) &obm[1];
  676. if (GNUNET_NO == GST_neighbours_test_connected (&obm->peer))
  677. {
  678. /* not connected, not allowed to send; can happen due to asynchronous operations */
  679. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  680. "Could not send message to peer `%s': not connected\n",
  681. GNUNET_i2s (&obm->peer));
  682. GNUNET_STATISTICS_update (
  683. GST_stats,
  684. gettext_noop ("# bytes payload dropped (other peer was not connected)"),
  685. ntohs (obmm->size),
  686. GNUNET_NO);
  687. GNUNET_SERVICE_client_continue (tc->client);
  688. return;
  689. }
  690. GNUNET_log (
  691. GNUNET_ERROR_TYPE_DEBUG,
  692. "Received SEND request %llu for `%s' and first message of type %u and total size %u\n",
  693. uuid_gen,
  694. GNUNET_i2s (&obm->peer),
  695. ntohs (obmm->type),
  696. ntohs (obmm->size));
  697. GNUNET_SERVICE_client_continue (tc->client);
  698. stcc = GNUNET_new (struct SendTransmitContinuationContext);
  699. stcc->target = obm->peer;
  700. stcc->tc = tc;
  701. stcc->send_time = GNUNET_TIME_absolute_get ();
  702. stcc->uuid = uuid_gen++;
  703. (void) GNUNET_CONTAINER_multipeermap_put (
  704. active_stccs,
  705. &stcc->target,
  706. stcc,
  707. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  708. GST_manipulation_send (&obm->peer,
  709. obmm,
  710. ntohs (obmm->size),
  711. GNUNET_TIME_relative_ntoh (obm->timeout),
  712. &handle_send_transmit_continuation,
  713. stcc);
  714. }
  715. /**
  716. * Take the given address and append it to the set of results sent back to
  717. * the client. This function may be called serveral times for a single
  718. * conversion. The last invocation will be with a @a address of
  719. * NULL and a @a res of #GNUNET_OK. Thus, to indicate conversion
  720. * errors, the callback might be called first with @a address NULL and
  721. * @a res being #GNUNET_SYSERR. In that case, there will still be a
  722. * subsequent call later with @a address NULL and @a res #GNUNET_OK.
  723. *
  724. * @param cls the `struct AddressToStringContext`
  725. * @param buf text to transmit (contains the human-readable address, or NULL)
  726. * @param res #GNUNET_OK if conversion was successful, #GNUNET_SYSERR on error,
  727. * never #GNUNET_NO
  728. */
  729. static void
  730. transmit_address_to_client (void *cls, const char *buf, int res)
  731. {
  732. struct AddressToStringContext *actx = cls;
  733. struct GNUNET_MQ_Envelope *env;
  734. struct AddressToStringResultMessage *atsm;
  735. size_t slen;
  736. GNUNET_assert ((GNUNET_OK == res) || (GNUNET_SYSERR == res));
  737. if (NULL == actx->tc)
  738. return;
  739. if (NULL == buf)
  740. {
  741. env = GNUNET_MQ_msg (atsm,
  742. GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
  743. if (GNUNET_OK == res)
  744. {
  745. /* this was the last call, transmit */
  746. atsm->res = htonl (GNUNET_OK);
  747. atsm->addr_len = htonl (0);
  748. GNUNET_MQ_send (actx->tc->mq, env);
  749. GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, actx);
  750. GNUNET_free (actx);
  751. return;
  752. }
  753. if (GNUNET_SYSERR == res)
  754. {
  755. /* address conversion failed, but there will be more callbacks */
  756. atsm->res = htonl (GNUNET_SYSERR);
  757. atsm->addr_len = htonl (0);
  758. GNUNET_MQ_send (actx->tc->mq, env);
  759. return;
  760. }
  761. }
  762. GNUNET_assert (GNUNET_OK == res);
  763. /* succesful conversion, append*/
  764. slen = strlen (buf) + 1;
  765. env =
  766. GNUNET_MQ_msg_extra (atsm,
  767. slen,
  768. GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
  769. atsm->res = htonl (GNUNET_YES);
  770. atsm->addr_len = htonl (slen);
  771. GNUNET_memcpy (&atsm[1], buf, slen);
  772. GNUNET_MQ_send (actx->tc->mq, env);
  773. }
  774. /**
  775. * Client asked to resolve an address. Check the request.
  776. *
  777. * @param cls the client
  778. * @param alum the resolution request
  779. * @return #GNUNET_OK if @a alum is well-formed
  780. */
  781. static int
  782. check_client_address_to_string (void *cls,
  783. const struct AddressLookupMessage *alum)
  784. {
  785. const char *plugin_name;
  786. const char *address;
  787. uint32_t address_len;
  788. uint16_t size;
  789. size = ntohs (alum->header.size);
  790. address_len = ntohs (alum->addrlen);
  791. if (size <= sizeof(struct AddressLookupMessage) + address_len)
  792. {
  793. GNUNET_break (0);
  794. return GNUNET_SYSERR;
  795. }
  796. address = (const char *) &alum[1];
  797. plugin_name = (const char *) &address[address_len];
  798. if ('\0' != plugin_name[size - sizeof(struct AddressLookupMessage)
  799. - address_len - 1])
  800. {
  801. GNUNET_break (0);
  802. return GNUNET_SYSERR;
  803. }
  804. return GNUNET_OK;
  805. }
  806. /**
  807. * Client asked to resolve an address. Process the request.
  808. *
  809. * @param cls the client
  810. * @param alum the resolution request
  811. */
  812. static void
  813. handle_client_address_to_string (void *cls,
  814. const struct AddressLookupMessage *alum)
  815. {
  816. struct TransportClient *tc = cls;
  817. struct GNUNET_TRANSPORT_PluginFunctions *papi;
  818. const char *plugin_name;
  819. const char *address;
  820. uint32_t address_len;
  821. struct AddressToStringContext *actx;
  822. struct GNUNET_MQ_Envelope *env;
  823. struct AddressToStringResultMessage *atsm;
  824. struct GNUNET_TIME_Relative rtimeout;
  825. int32_t numeric;
  826. address_len = ntohs (alum->addrlen);
  827. address = (const char *) &alum[1];
  828. plugin_name = (const char *) &address[address_len];
  829. rtimeout = GNUNET_TIME_relative_ntoh (alum->timeout);
  830. numeric = ntohs (alum->numeric_only);
  831. papi = GST_plugins_printer_find (plugin_name);
  832. if (NULL == papi)
  833. {
  834. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  835. "Failed to find plugin `%s'\n",
  836. plugin_name);
  837. env = GNUNET_MQ_msg (atsm,
  838. GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
  839. atsm->res = htonl (GNUNET_SYSERR);
  840. atsm->addr_len = htonl (0);
  841. GNUNET_MQ_send (tc->mq, env);
  842. env = GNUNET_MQ_msg (atsm,
  843. GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING_REPLY);
  844. atsm->res = htonl (GNUNET_OK);
  845. atsm->addr_len = htonl (0);
  846. GNUNET_MQ_send (tc->mq, env);
  847. return;
  848. }
  849. actx = GNUNET_new (struct AddressToStringContext);
  850. actx->tc = tc;
  851. GNUNET_CONTAINER_DLL_insert (a2s_head, a2s_tail, actx);
  852. GNUNET_SERVICE_client_disable_continue_warning (tc->client);
  853. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  854. "Pretty-printing address of %u bytes using plugin `%s'\n",
  855. address_len,
  856. plugin_name);
  857. papi->address_pretty_printer (papi->cls,
  858. plugin_name,
  859. address,
  860. address_len,
  861. numeric,
  862. rtimeout,
  863. &transmit_address_to_client,
  864. actx);
  865. }
  866. /**
  867. * Compose #PeerIterateResponseMessage using the given peer and address.
  868. *
  869. * @param peer identity of the peer
  870. * @param address the address, NULL on disconnect
  871. * @return composed message
  872. */
  873. static struct PeerIterateResponseMessage *
  874. compose_address_iterate_response_message (
  875. const struct GNUNET_PeerIdentity *peer,
  876. const struct GNUNET_HELLO_Address *address)
  877. {
  878. struct PeerIterateResponseMessage *msg;
  879. size_t size;
  880. size_t tlen;
  881. size_t alen;
  882. char *addr;
  883. GNUNET_assert (NULL != peer);
  884. if (NULL != address)
  885. {
  886. tlen = strlen (address->transport_name) + 1;
  887. alen = address->address_length;
  888. }
  889. else
  890. {
  891. tlen = 0;
  892. alen = 0;
  893. }
  894. size = (sizeof(struct PeerIterateResponseMessage) + alen + tlen);
  895. msg = GNUNET_malloc (size);
  896. msg->header.size = htons (size);
  897. msg->header.type =
  898. htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
  899. msg->reserved = htonl (0);
  900. msg->peer = *peer;
  901. msg->addrlen = htonl (alen);
  902. msg->pluginlen = htonl (tlen);
  903. if (NULL != address)
  904. {
  905. msg->local_address_info = htonl ((uint32_t) address->local_info);
  906. addr = (char *) &msg[1];
  907. GNUNET_memcpy (addr, address->address, alen);
  908. GNUNET_memcpy (&addr[alen], address->transport_name, tlen);
  909. }
  910. return msg;
  911. }
  912. /**
  913. * Context for #send_validation_information() and
  914. * #send_peer_information().
  915. */
  916. struct IterationContext
  917. {
  918. /**
  919. * Context to use for the transmission.
  920. */
  921. struct TransportClient *tc;
  922. /**
  923. * Which peers do we care about?
  924. */
  925. struct GNUNET_PeerIdentity id;
  926. /**
  927. * #GNUNET_YES if @e id should be ignored because we want all peers.
  928. */
  929. int all;
  930. };
  931. /**
  932. * Output information of neighbours to the given client.
  933. *
  934. * @param cls the `struct PeerIterationContext *`
  935. * @param peer identity of the neighbour
  936. * @param address the address
  937. * @param state current state this peer is in
  938. * @param state_timeout timeout for the current state of the peer
  939. * @param bandwidth_in inbound quota in NBO
  940. * @param bandwidth_out outbound quota in NBO
  941. */
  942. static void
  943. send_peer_information (void *cls,
  944. const struct GNUNET_PeerIdentity *peer,
  945. const struct GNUNET_HELLO_Address *address,
  946. enum GNUNET_TRANSPORT_PeerState state,
  947. struct GNUNET_TIME_Absolute state_timeout,
  948. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  949. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
  950. {
  951. struct IterationContext *pc = cls;
  952. struct GNUNET_MQ_Envelope *env;
  953. struct PeerIterateResponseMessage *msg;
  954. if ((GNUNET_YES != pc->all) && (0 != memcmp (peer, &pc->id, sizeof(pc->id))))
  955. return;
  956. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  957. "Sending information about `%s' using address `%s' in state `%s'\n",
  958. GNUNET_i2s (peer),
  959. (NULL != address) ? GST_plugins_a2s (address) : "<none>",
  960. GNUNET_TRANSPORT_ps2s (state));
  961. msg = compose_address_iterate_response_message (peer, address);
  962. msg->state = htonl (state);
  963. msg->state_timeout = GNUNET_TIME_absolute_hton (state_timeout);
  964. env = GNUNET_MQ_msg_copy (&msg->header);
  965. GNUNET_free (msg);
  966. GNUNET_MQ_send (pc->tc->mq, env);
  967. }
  968. /**
  969. * Client asked to obtain information about a specific or all peers
  970. * Process the request.
  971. *
  972. * @param cls the client
  973. * @param msg the peer address information request
  974. */
  975. static void
  976. handle_client_monitor_peers (void *cls, const struct PeerMonitorMessage *msg)
  977. {
  978. struct TransportClient *tc = cls;
  979. struct IterationContext pc;
  980. if (CT_NONE != tc->type)
  981. {
  982. GNUNET_break (0);
  983. GNUNET_SERVICE_client_drop (tc->client);
  984. return;
  985. }
  986. GNUNET_SERVICE_client_disable_continue_warning (tc->client);
  987. GNUNET_SERVICE_client_mark_monitor (tc->client);
  988. /* Send initial list */
  989. pc.tc = tc;
  990. if (0 == memcmp (&msg->peer, &all_zeros, sizeof(struct GNUNET_PeerIdentity)))
  991. {
  992. /* iterate over all neighbours */
  993. pc.all = GNUNET_YES;
  994. pc.id = msg->peer;
  995. }
  996. else
  997. {
  998. /* just return one neighbour */
  999. pc.all = GNUNET_NO;
  1000. pc.id = msg->peer;
  1001. }
  1002. GST_neighbours_iterate (&send_peer_information, &pc);
  1003. if (GNUNET_YES != ntohl (msg->one_shot))
  1004. {
  1005. tc->details.monitor_peer = msg->peer;
  1006. tc->type = CT_MONITOR;
  1007. if (0 !=
  1008. memcmp (&msg->peer, &all_zeros, sizeof(struct GNUNET_PeerIdentity)))
  1009. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1010. "Client %p started monitoring of the peer `%s'\n",
  1011. tc,
  1012. GNUNET_i2s (&msg->peer));
  1013. else
  1014. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1015. "Client %p started monitoring all peers\n",
  1016. tc);
  1017. }
  1018. else
  1019. {
  1020. struct GNUNET_MessageHeader *msg;
  1021. struct GNUNET_MQ_Envelope *env;
  1022. env =
  1023. GNUNET_MQ_msg (msg,
  1024. GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE_END);
  1025. GNUNET_MQ_send (tc->mq, env);
  1026. }
  1027. }
  1028. /**
  1029. * Function called by the plugin with information about the
  1030. * current sessions managed by the plugin (for monitoring).
  1031. *
  1032. * @param cls closure
  1033. * @param session session handle this information is about,
  1034. * NULL to indicate that we are "in sync" (initial
  1035. * iteration complete)
  1036. * @param info information about the state of the session,
  1037. * NULL if @a session is also NULL and we are
  1038. * merely signalling that the initial iteration is over
  1039. */
  1040. static void
  1041. plugin_session_info_cb (void *cls,
  1042. struct GNUNET_ATS_Session *session,
  1043. const struct GNUNET_TRANSPORT_SessionInfo *info)
  1044. {
  1045. struct GNUNET_MQ_Envelope *env;
  1046. struct TransportPluginMonitorMessage *msg;
  1047. struct GNUNET_MessageHeader *sync;
  1048. size_t size;
  1049. size_t slen;
  1050. uint16_t alen;
  1051. char *name;
  1052. char *addr;
  1053. if (0 == GNUNET_notification_context_get_size (plugin_nc))
  1054. {
  1055. GST_plugins_monitor_subscribe (NULL, NULL);
  1056. return;
  1057. }
  1058. if ((NULL == info) && (NULL == session))
  1059. {
  1060. /* end of initial iteration */
  1061. if (NULL != sync_client)
  1062. {
  1063. env =
  1064. GNUNET_MQ_msg (sync, GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_SYNC);
  1065. GNUNET_MQ_send (sync_client->mq, env);
  1066. sync_client = NULL;
  1067. }
  1068. return;
  1069. }
  1070. GNUNET_assert (NULL != info);
  1071. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1072. "Plugin event for peer %s on transport %s\n",
  1073. GNUNET_i2s (&info->address->peer),
  1074. info->address->transport_name);
  1075. slen = strlen (info->address->transport_name) + 1;
  1076. alen = info->address->address_length;
  1077. size = sizeof(struct TransportPluginMonitorMessage) + slen + alen;
  1078. if (size > UINT16_MAX)
  1079. {
  1080. GNUNET_break (0);
  1081. return;
  1082. }
  1083. msg = GNUNET_malloc (size);
  1084. msg->header.size = htons (size);
  1085. msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_EVENT);
  1086. msg->session_state = htons ((uint16_t) info->state);
  1087. msg->is_inbound = htons ((int16_t) info->is_inbound);
  1088. msg->msgs_pending = htonl (info->num_msg_pending);
  1089. msg->bytes_pending = htonl (info->num_bytes_pending);
  1090. msg->timeout = GNUNET_TIME_absolute_hton (info->session_timeout);
  1091. msg->delay = GNUNET_TIME_absolute_hton (info->receive_delay);
  1092. msg->peer = info->address->peer;
  1093. msg->session_id = (uint64_t) (intptr_t) session;
  1094. msg->plugin_name_len = htons (slen);
  1095. msg->plugin_address_len = htons (alen);
  1096. name = (char *) &msg[1];
  1097. GNUNET_memcpy (name, info->address->transport_name, slen);
  1098. addr = &name[slen];
  1099. GNUNET_memcpy (addr, info->address->address, alen);
  1100. if (NULL != sync_client)
  1101. {
  1102. struct GNUNET_MQ_Envelope *env;
  1103. env = GNUNET_MQ_msg_copy (&msg->header);
  1104. GNUNET_MQ_send (sync_client->mq, env);
  1105. }
  1106. else
  1107. {
  1108. GNUNET_notification_context_broadcast (plugin_nc, &msg->header, GNUNET_NO);
  1109. }
  1110. GNUNET_free (msg);
  1111. }
  1112. /**
  1113. * Client asked to obtain information about all plugin connections.
  1114. *
  1115. * @param cls the client
  1116. * @param message the peer address information request
  1117. */
  1118. static void
  1119. handle_client_monitor_plugins (void *cls,
  1120. const struct GNUNET_MessageHeader *message)
  1121. {
  1122. struct TransportClient *tc = cls;
  1123. GNUNET_SERVICE_client_mark_monitor (tc->client);
  1124. GNUNET_SERVICE_client_disable_continue_warning (tc->client);
  1125. GNUNET_notification_context_add (plugin_nc, tc->mq);
  1126. GNUNET_assert (NULL == sync_client);
  1127. sync_client = tc;
  1128. GST_plugins_monitor_subscribe (&plugin_session_info_cb, NULL);
  1129. }
  1130. /**
  1131. * Broadcast the given message to all of our clients.
  1132. *
  1133. * @param msg message to broadcast
  1134. * @param may_drop #GNUNET_YES if the message can be dropped / is payload
  1135. */
  1136. void
  1137. GST_clients_broadcast (const struct GNUNET_MessageHeader *msg, int may_drop)
  1138. {
  1139. int done;
  1140. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1141. "Asked to broadcast message of type %u with %u bytes\n",
  1142. (unsigned int) ntohs (msg->type),
  1143. (unsigned int) ntohs (msg->size));
  1144. done = GNUNET_NO;
  1145. for (struct TransportClient *tc = clients_head; NULL != tc; tc = tc->next)
  1146. {
  1147. if (CT_NONE == tc->type)
  1148. continue; /* client not yet ready */
  1149. if ((GNUNET_YES == may_drop) && (CT_CORE != tc->type))
  1150. continue; /* skip, this client does not care about payload */
  1151. unicast (tc, msg, may_drop);
  1152. done = GNUNET_YES;
  1153. }
  1154. if (GNUNET_NO == done)
  1155. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1156. "Message of type %u not delivered, is CORE service up?\n",
  1157. ntohs (msg->type));
  1158. }
  1159. /**
  1160. * Broadcast the new active address to all clients monitoring the peer.
  1161. *
  1162. * @param peer peer this update is about (never NULL)
  1163. * @param address address, NULL on disconnect
  1164. * @param state the current state of the peer
  1165. * @param state_timeout the time out for the state
  1166. */
  1167. void
  1168. GST_clients_broadcast_peer_notification (
  1169. const struct GNUNET_PeerIdentity *peer,
  1170. const struct GNUNET_HELLO_Address *address,
  1171. enum GNUNET_TRANSPORT_PeerState state,
  1172. struct GNUNET_TIME_Absolute state_timeout)
  1173. {
  1174. struct GNUNET_MQ_Envelope *env;
  1175. struct PeerIterateResponseMessage *msg;
  1176. msg = compose_address_iterate_response_message (peer, address);
  1177. msg->state = htonl (state);
  1178. msg->state_timeout = GNUNET_TIME_absolute_hton (state_timeout);
  1179. for (struct TransportClient *tc = clients_head; NULL != tc; tc = tc->next)
  1180. {
  1181. if (CT_MONITOR != tc->type)
  1182. continue;
  1183. if ((0 == memcmp (&tc->details.monitor_peer,
  1184. &all_zeros,
  1185. sizeof(struct GNUNET_PeerIdentity))) ||
  1186. (0 == memcmp (&tc->details.monitor_peer,
  1187. peer,
  1188. sizeof(struct GNUNET_PeerIdentity))))
  1189. {
  1190. env = GNUNET_MQ_msg_copy (&msg->header);
  1191. GNUNET_MQ_send (tc->mq, env);
  1192. }
  1193. }
  1194. GNUNET_free (msg);
  1195. }
  1196. /**
  1197. * Mark the peer as down so we don't call the continuation
  1198. * context in the future.
  1199. *
  1200. * @param cls NULL
  1201. * @param peer peer that got disconnected
  1202. * @param value a `struct SendTransmitContinuationContext` to mark
  1203. * @return #GNUNET_OK (continue to iterate)
  1204. */
  1205. static int
  1206. mark_peer_down (void *cls, const struct GNUNET_PeerIdentity *peer, void *value)
  1207. {
  1208. struct SendTransmitContinuationContext *stcc = value;
  1209. stcc->down = GNUNET_YES;
  1210. return GNUNET_OK;
  1211. }
  1212. /**
  1213. * Notify all clients about a disconnect, and cancel
  1214. * pending SEND_OK messages for this peer.
  1215. *
  1216. * @param peer peer that disconnected
  1217. */
  1218. void
  1219. GST_clients_broadcast_disconnect (const struct GNUNET_PeerIdentity *peer)
  1220. {
  1221. struct DisconnectInfoMessage disconnect_msg;
  1222. GNUNET_CONTAINER_multipeermap_get_multiple (active_stccs,
  1223. peer,
  1224. &mark_peer_down,
  1225. NULL);
  1226. disconnect_msg.header.size = htons (sizeof(struct DisconnectInfoMessage));
  1227. disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
  1228. disconnect_msg.reserved = htonl (0);
  1229. disconnect_msg.peer = *peer;
  1230. GST_clients_broadcast (&disconnect_msg.header, GNUNET_NO);
  1231. }
  1232. /**
  1233. * Transmit our HELLO message to the given (connected) neighbour.
  1234. *
  1235. * @param cls the 'HELLO' message
  1236. * @param peer identity of the peer
  1237. * @param address the address
  1238. * @param state current state this peer is in
  1239. * @param state_timeout timeout for the current state of the peer
  1240. * @param bandwidth_in inbound quota in NBO
  1241. * @param bandwidth_out outbound quota in NBO
  1242. */
  1243. static void
  1244. transmit_our_hello (void *cls,
  1245. const struct GNUNET_PeerIdentity *peer,
  1246. const struct GNUNET_HELLO_Address *address,
  1247. enum GNUNET_TRANSPORT_PeerState state,
  1248. struct GNUNET_TIME_Absolute state_timeout,
  1249. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  1250. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
  1251. {
  1252. const struct GNUNET_MessageHeader *hello = cls;
  1253. if (0 == memcmp (peer, &GST_my_identity, sizeof(struct GNUNET_PeerIdentity)))
  1254. return; /* not to ourselves */
  1255. if (GNUNET_NO == GST_neighbours_test_connected (peer))
  1256. return;
  1257. GST_neighbours_send (peer,
  1258. hello,
  1259. ntohs (hello->size),
  1260. hello_expiration,
  1261. NULL,
  1262. NULL);
  1263. }
  1264. /**
  1265. * My HELLO has changed. Tell everyone who should know.
  1266. *
  1267. * @param cls unused
  1268. * @param hello new HELLO
  1269. */
  1270. static void
  1271. process_hello_update (void *cls, const struct GNUNET_MessageHeader *hello)
  1272. {
  1273. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Broadcasting HELLO to clients\n");
  1274. GST_clients_broadcast (hello, GNUNET_NO);
  1275. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Broadcasting HELLO to neighbours\n");
  1276. GST_neighbours_iterate (&transmit_our_hello, (void *) hello);
  1277. }
  1278. /**
  1279. * We received some payload. Prepare to pass it on to our clients.
  1280. *
  1281. * @param address address and (claimed) identity of the other peer
  1282. * @param session identifier used for this session (NULL for plugins
  1283. * that do not offer bi-directional communication to the sender
  1284. * using the same "connection")
  1285. * @param message the message to process
  1286. * @return how long the plugin should wait until receiving more data
  1287. */
  1288. static struct GNUNET_TIME_Relative
  1289. process_payload (const struct GNUNET_HELLO_Address *address,
  1290. struct GNUNET_ATS_Session *session,
  1291. const struct GNUNET_MessageHeader *message)
  1292. {
  1293. struct GNUNET_TIME_Relative ret;
  1294. int do_forward;
  1295. struct InboundMessage *im;
  1296. size_t msg_size = ntohs (message->size);
  1297. size_t size = sizeof(struct InboundMessage) + msg_size;
  1298. char buf[size] GNUNET_ALIGN;
  1299. do_forward = GNUNET_SYSERR;
  1300. ret = GST_neighbours_calculate_receive_delay (&address->peer,
  1301. msg_size,
  1302. &do_forward);
  1303. if (! GST_neighbours_test_connected (&address->peer))
  1304. {
  1305. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1306. "Discarded %u bytes type %u payload from peer `%s'\n",
  1307. (unsigned int) msg_size,
  1308. ntohs (message->type),
  1309. GNUNET_i2s (&address->peer));
  1310. GNUNET_STATISTICS_update (
  1311. GST_stats,
  1312. gettext_noop ("# bytes payload discarded due to not connected peer"),
  1313. msg_size,
  1314. GNUNET_NO);
  1315. return ret;
  1316. }
  1317. if (GNUNET_YES != do_forward)
  1318. return ret;
  1319. im = (struct InboundMessage *) buf;
  1320. im->header.size = htons (size);
  1321. im->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_RECV);
  1322. im->peer = address->peer;
  1323. GNUNET_memcpy (&im[1], message, ntohs (message->size));
  1324. GST_clients_broadcast (&im->header, GNUNET_YES);
  1325. return ret;
  1326. }
  1327. /**
  1328. * Task to asynchronously terminate a session.
  1329. *
  1330. * @param cls the `struct GNUNET_ATS_SessionKiller` with the information for the kill
  1331. */
  1332. static void
  1333. kill_session_task (void *cls)
  1334. {
  1335. struct GNUNET_ATS_SessionKiller *sk = cls;
  1336. sk->task = NULL;
  1337. GNUNET_CONTAINER_DLL_remove (sk_head, sk_tail, sk);
  1338. sk->plugin->disconnect_session (sk->plugin->cls, sk->session);
  1339. GNUNET_free (sk);
  1340. }
  1341. /**
  1342. * Force plugin to terminate session due to communication
  1343. * issue.
  1344. *
  1345. * @param plugin_name name of the plugin
  1346. * @param session session to termiante
  1347. */
  1348. static void
  1349. kill_session (const char *plugin_name, struct GNUNET_ATS_Session *session)
  1350. {
  1351. struct GNUNET_TRANSPORT_PluginFunctions *plugin;
  1352. struct GNUNET_ATS_SessionKiller *sk;
  1353. for (sk = sk_head; NULL != sk; sk = sk->next)
  1354. if (sk->session == session)
  1355. return;
  1356. plugin = GST_plugins_find (plugin_name);
  1357. if (NULL == plugin)
  1358. {
  1359. GNUNET_break (0);
  1360. return;
  1361. }
  1362. /* need to issue disconnect asynchronously */
  1363. sk = GNUNET_new (struct GNUNET_ATS_SessionKiller);
  1364. sk->session = session;
  1365. sk->plugin = plugin;
  1366. sk->task = GNUNET_SCHEDULER_add_now (&kill_session_task, sk);
  1367. GNUNET_CONTAINER_DLL_insert (sk_head, sk_tail, sk);
  1368. }
  1369. /**
  1370. * Black list check result for try_connect call
  1371. * If connection to the peer is allowed request adddress and ???
  1372. *
  1373. * @param cls the message
  1374. * @param peer the peer
  1375. * @param address the address
  1376. * @param session the session
  1377. * @param result the result
  1378. */
  1379. static void
  1380. connect_bl_check_cont (void *cls,
  1381. const struct GNUNET_PeerIdentity *peer,
  1382. const struct GNUNET_HELLO_Address *address,
  1383. struct GNUNET_ATS_Session *session,
  1384. int result)
  1385. {
  1386. struct GNUNET_MessageHeader *msg = cls;
  1387. if (GNUNET_OK == result)
  1388. {
  1389. /* Blacklist allows to speak to this peer, forward SYN to neighbours */
  1390. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1391. "Received SYN message from peer `%s' at `%s'\n",
  1392. GNUNET_i2s (peer),
  1393. GST_plugins_a2s (address));
  1394. if (GNUNET_OK != GST_neighbours_handle_session_syn (msg, peer))
  1395. {
  1396. GST_blacklist_abort_matching (address, session);
  1397. kill_session (address->transport_name, session);
  1398. }
  1399. GNUNET_free (msg);
  1400. return;
  1401. }
  1402. GNUNET_free (msg);
  1403. if (GNUNET_SYSERR == result)
  1404. return; /* check was aborted, session destroyed */
  1405. /* Blacklist denies to speak to this peer */
  1406. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1407. "Discarding SYN message from `%s' due to denied blacklist check\n",
  1408. GNUNET_i2s (peer));
  1409. kill_session (address->transport_name, session);
  1410. }
  1411. /**
  1412. * Function called by the transport for each received message.
  1413. *
  1414. * @param cls closure, const char* with the name of the plugin we received the message from
  1415. * @param address address and (claimed) identity of the other peer
  1416. * @param message the message, NULL if we only care about
  1417. * learning about the delay until we should receive again
  1418. * @param session identifier used for this session (NULL for plugins
  1419. * that do not offer bi-directional communication to the sender
  1420. * using the same "connection")
  1421. * @return how long the plugin should wait until receiving more data
  1422. * (plugins that do not support this, can ignore the return value)
  1423. */
  1424. struct GNUNET_TIME_Relative
  1425. GST_receive_callback (void *cls,
  1426. const struct GNUNET_HELLO_Address *address,
  1427. struct GNUNET_ATS_Session *session,
  1428. const struct GNUNET_MessageHeader *message)
  1429. {
  1430. const char *plugin_name = cls;
  1431. struct GNUNET_TIME_Relative ret;
  1432. uint16_t type;
  1433. ret = GNUNET_TIME_UNIT_ZERO;
  1434. if (NULL == message)
  1435. goto end;
  1436. type = ntohs (message->type);
  1437. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1438. "Received message with type %u from peer `%s' at %s\n",
  1439. type,
  1440. GNUNET_i2s (&address->peer),
  1441. GST_plugins_a2s (address));
  1442. GNUNET_STATISTICS_update (GST_stats,
  1443. gettext_noop ("# bytes total received"),
  1444. ntohs (message->size),
  1445. GNUNET_NO);
  1446. GST_neighbours_notify_data_recv (address, message);
  1447. switch (type)
  1448. {
  1449. case GNUNET_MESSAGE_TYPE_HELLO_LEGACY:
  1450. /* Legacy HELLO message, discard */
  1451. return ret;
  1452. case GNUNET_MESSAGE_TYPE_HELLO:
  1453. if (GNUNET_OK != GST_validation_handle_hello (message))
  1454. {
  1455. GNUNET_break_op (0);
  1456. GST_blacklist_abort_matching (address, session);
  1457. }
  1458. return ret;
  1459. case GNUNET_MESSAGE_TYPE_TRANSPORT_PING:
  1460. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1461. "Processing PING from `%s'\n",
  1462. GST_plugins_a2s (address));
  1463. if (GNUNET_OK !=
  1464. GST_validation_handle_ping (&address->peer, message, address, session))
  1465. {
  1466. GST_blacklist_abort_matching (address, session);
  1467. kill_session (plugin_name, session);
  1468. }
  1469. break;
  1470. case GNUNET_MESSAGE_TYPE_TRANSPORT_PONG:
  1471. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1472. "Processing PONG from `%s'\n",
  1473. GST_plugins_a2s (address));
  1474. if (GNUNET_OK != GST_validation_handle_pong (&address->peer, message))
  1475. {
  1476. GNUNET_break_op (0);
  1477. GST_blacklist_abort_matching (address, session);
  1478. kill_session (plugin_name, session);
  1479. }
  1480. break;
  1481. case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN:
  1482. /* Do blacklist check if communication with this peer is allowed */
  1483. (void) GST_blacklist_test_allowed (&address->peer,
  1484. NULL,
  1485. &connect_bl_check_cont,
  1486. GNUNET_copy_message (message),
  1487. address,
  1488. session);
  1489. break;
  1490. case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK:
  1491. if (GNUNET_OK !=
  1492. GST_neighbours_handle_session_syn_ack (message, address, session))
  1493. {
  1494. GST_blacklist_abort_matching (address, session);
  1495. kill_session (plugin_name, session);
  1496. }
  1497. break;
  1498. case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK:
  1499. if (GNUNET_OK !=
  1500. GST_neighbours_handle_session_ack (message, address, session))
  1501. {
  1502. GNUNET_break_op (0);
  1503. GST_blacklist_abort_matching (address, session);
  1504. kill_session (plugin_name, session);
  1505. }
  1506. break;
  1507. case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT:
  1508. GST_neighbours_handle_disconnect_message (&address->peer, message);
  1509. break;
  1510. case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_QUOTA:
  1511. GST_neighbours_handle_quota_message (&address->peer, message);
  1512. break;
  1513. case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE:
  1514. GST_neighbours_keepalive (&address->peer, message);
  1515. break;
  1516. case GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE:
  1517. GST_neighbours_keepalive_response (&address->peer, message);
  1518. break;
  1519. default:
  1520. /* should be payload */
  1521. GNUNET_STATISTICS_update (GST_stats,
  1522. gettext_noop ("# bytes payload received"),
  1523. ntohs (message->size),
  1524. GNUNET_NO);
  1525. ret = process_payload (address, session, message);
  1526. break;
  1527. }
  1528. end:
  1529. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1530. "Allowing receive from peer %s to continue in %s\n",
  1531. GNUNET_i2s (&address->peer),
  1532. GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
  1533. return ret;
  1534. }
  1535. /**
  1536. * Function that will be called for each address the transport
  1537. * is aware that it might be reachable under. Update our HELLO.
  1538. *
  1539. * @param cls name of the plugin (const char*)
  1540. * @param add_remove should the address added (YES) or removed (NO) from the
  1541. * set of valid addresses?
  1542. * @param address the address to add or remove
  1543. */
  1544. static void
  1545. plugin_env_address_change_notification (
  1546. void *cls,
  1547. int add_remove,
  1548. const struct GNUNET_HELLO_Address *address)
  1549. {
  1550. static int addresses = 0;
  1551. if (GNUNET_YES == add_remove)
  1552. {
  1553. addresses++;
  1554. GNUNET_STATISTICS_update (GST_stats, "# transport addresses", 1, GNUNET_NO);
  1555. }
  1556. else if (GNUNET_NO == add_remove)
  1557. {
  1558. if (0 == addresses)
  1559. {
  1560. GNUNET_break (0);
  1561. }
  1562. else
  1563. {
  1564. addresses--;
  1565. GNUNET_STATISTICS_update (GST_stats,
  1566. "# transport addresses",
  1567. -1,
  1568. GNUNET_NO);
  1569. }
  1570. }
  1571. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1572. "Transport now has %u addresses to communicate\n",
  1573. addresses);
  1574. GST_hello_modify_addresses (add_remove, address);
  1575. }
  1576. /**
  1577. * Function that will be called whenever the plugin internally
  1578. * cleans up a session pointer and hence the service needs to
  1579. * discard all of those sessions as well. Plugins that do not
  1580. * use sessions can simply omit calling this function and always
  1581. * use NULL wherever a session pointer is needed. This function
  1582. * should be called BEFORE a potential "TransmitContinuation"
  1583. * from the "TransmitFunction".
  1584. *
  1585. * @param cls closure
  1586. * @param address which address was the session for
  1587. * @param session which session is being destoyed
  1588. */
  1589. static void
  1590. plugin_env_session_end (void *cls,
  1591. const struct GNUNET_HELLO_Address *address,
  1592. struct GNUNET_ATS_Session *session)
  1593. {
  1594. struct GNUNET_ATS_SessionKiller *sk;
  1595. if (NULL == address)
  1596. {
  1597. GNUNET_break (0);
  1598. return;
  1599. }
  1600. if (NULL == session)
  1601. {
  1602. GNUNET_break (0);
  1603. return;
  1604. }
  1605. GNUNET_assert (strlen (address->transport_name) > 0);
  1606. GNUNET_log (
  1607. GNUNET_ERROR_TYPE_DEBUG,
  1608. "Notification from plugin about terminated session %p from peer `%s' address `%s'\n",
  1609. session,
  1610. GNUNET_i2s (&address->peer),
  1611. GST_plugins_a2s (address));
  1612. GST_neighbours_session_terminated (&address->peer, session);
  1613. GST_ats_del_session (address, session);
  1614. GST_blacklist_abort_matching (address, session);
  1615. for (sk = sk_head; NULL != sk; sk = sk->next)
  1616. {
  1617. if (sk->session == session)
  1618. {
  1619. GNUNET_CONTAINER_DLL_remove (sk_head, sk_tail, sk);
  1620. GNUNET_SCHEDULER_cancel (sk->task);
  1621. GNUNET_free (sk);
  1622. break;
  1623. }
  1624. }
  1625. }
  1626. /**
  1627. * Black list check result from blacklist check triggered when a
  1628. * plugin gave us a new session in #plugin_env_session_start(). If
  1629. * connection to the peer is disallowed, kill the session.
  1630. *
  1631. * @param cls NULL
  1632. * @param peer the peer
  1633. * @param address address associated with the request
  1634. * @param session session associated with the request
  1635. * @param result the result
  1636. */
  1637. static void
  1638. plugin_env_session_start_bl_check_cont (
  1639. void *cls,
  1640. const struct GNUNET_PeerIdentity *peer,
  1641. const struct GNUNET_HELLO_Address *address,
  1642. struct GNUNET_ATS_Session *session,
  1643. int result)
  1644. {
  1645. if (GNUNET_OK != result)
  1646. {
  1647. kill_session (address->transport_name, session);
  1648. return;
  1649. }
  1650. if (GNUNET_YES !=
  1651. GNUNET_HELLO_address_check_option (address,
  1652. GNUNET_HELLO_ADDRESS_INFO_INBOUND))
  1653. {
  1654. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1655. "Informing verifier about inbound session's address `%s'\n",
  1656. GST_plugins_a2s (address));
  1657. GST_validation_handle_address (address);
  1658. }
  1659. }
  1660. /**
  1661. * Plugin tells transport service about a new inbound session
  1662. *
  1663. * @param cls unused
  1664. * @param address the address
  1665. * @param session the new session
  1666. * @param scope network scope information
  1667. */
  1668. static void
  1669. plugin_env_session_start (void *cls,
  1670. const struct GNUNET_HELLO_Address *address,
  1671. struct GNUNET_ATS_Session *session,
  1672. enum GNUNET_NetworkType scope)
  1673. {
  1674. struct GNUNET_ATS_Properties prop;
  1675. if (NULL == address)
  1676. {
  1677. GNUNET_break (0);
  1678. return;
  1679. }
  1680. if (NULL == session)
  1681. {
  1682. GNUNET_break (0);
  1683. return;
  1684. }
  1685. GNUNET_log (
  1686. GNUNET_ERROR_TYPE_INFO,
  1687. "Notification from plugin `%s' about new session from peer `%s' address `%s'\n",
  1688. address->transport_name,
  1689. GNUNET_i2s (&address->peer),
  1690. GST_plugins_a2s (address));
  1691. if (GNUNET_YES ==
  1692. GNUNET_HELLO_address_check_option (address,
  1693. GNUNET_HELLO_ADDRESS_INFO_INBOUND))
  1694. {
  1695. /* inbound is always new, but outbound MAY already be known, but
  1696. for example for UNIX, we have symmetric connections and thus we
  1697. may not know the address yet; add if necessary! */
  1698. /* FIXME: maybe change API here so we just pass scope? */
  1699. memset (&prop, 0, sizeof(prop));
  1700. GNUNET_break (GNUNET_NT_UNSPECIFIED != scope);
  1701. prop.scope = scope;
  1702. GST_ats_add_inbound_address (address, session, &prop);
  1703. }
  1704. /* Do blacklist check if communication with this peer is allowed */
  1705. (void) GST_blacklist_test_allowed (&address->peer,
  1706. address->transport_name,
  1707. &plugin_env_session_start_bl_check_cont,
  1708. NULL,
  1709. address,
  1710. session);
  1711. }
  1712. /**
  1713. * Function called by ATS to notify the callee that the
  1714. * assigned bandwidth or address for a given peer was changed. If the
  1715. * callback is called with address/bandwidth assignments of zero, the
  1716. * ATS disconnect function will still be called once the disconnect
  1717. * actually happened.
  1718. *
  1719. * @param cls closure
  1720. * @param peer the peer this address is intended for
  1721. * @param address address to use (for peer given in address)
  1722. * @param session session to use (if available)
  1723. * @param bandwidth_out assigned outbound bandwidth for the connection in NBO,
  1724. * 0 to disconnect from peer
  1725. * @param bandwidth_in assigned inbound bandwidth for the connection in NBO,
  1726. * 0 to disconnect from peer
  1727. * @param ats ATS information
  1728. * @param ats_count number of @a ats elements
  1729. */
  1730. static void
  1731. ats_request_address_change (void *cls,
  1732. const struct GNUNET_PeerIdentity *peer,
  1733. const struct GNUNET_HELLO_Address *address,
  1734. struct GNUNET_ATS_Session *session,
  1735. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
  1736. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
  1737. {
  1738. uint32_t bw_in = ntohl (bandwidth_in.value__);
  1739. uint32_t bw_out = ntohl (bandwidth_out.value__);
  1740. if (NULL == peer)
  1741. {
  1742. /* ATS service died, all suggestions become invalid!
  1743. (but we'll keep using the allocations for a little
  1744. while, to keep going while ATS restarts) */
  1745. /* FIXME: We should drop all
  1746. connections now, as ATS won't explicitly tell
  1747. us and be unaware of ongoing resource allocations! */
  1748. return;
  1749. }
  1750. /* ATS tells me to disconnect from peer */
  1751. if ((0 == bw_in) && (0 == bw_out))
  1752. {
  1753. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1754. "ATS tells me to disconnect from peer `%s'\n",
  1755. GNUNET_i2s (peer));
  1756. GST_neighbours_force_disconnect (peer);
  1757. return;
  1758. }
  1759. GNUNET_assert (NULL != address);
  1760. GNUNET_STATISTICS_update (GST_stats,
  1761. "# ATS suggestions received",
  1762. 1,
  1763. GNUNET_NO);
  1764. GST_neighbours_switch_to_address (address,
  1765. session,
  1766. bandwidth_in,
  1767. bandwidth_out);
  1768. }
  1769. /**
  1770. * Closure for #test_connection_ok().
  1771. */
  1772. struct TestConnectionContext
  1773. {
  1774. /**
  1775. * Is this the first neighbour we're checking?
  1776. */
  1777. int first;
  1778. /**
  1779. * Handle to the blacklisting client we need to ask.
  1780. */
  1781. struct TransportClient *tc;
  1782. };
  1783. /**
  1784. * Got the result about an existing connection from a new blacklister.
  1785. * Shutdown the neighbour if necessary.
  1786. *
  1787. * @param cls unused
  1788. * @param peer the neighbour that was investigated
  1789. * @param address address associated with the request
  1790. * @param session session associated with the request
  1791. * @param allowed #GNUNET_OK if we can keep it,
  1792. * #GNUNET_NO if we must shutdown the connection
  1793. */
  1794. static void
  1795. confirm_or_drop_neighbour (void *cls,
  1796. const struct GNUNET_PeerIdentity *peer,
  1797. const struct GNUNET_HELLO_Address *address,
  1798. struct GNUNET_ATS_Session *session,
  1799. int allowed)
  1800. {
  1801. if (GNUNET_OK == allowed)
  1802. return; /* we're done */
  1803. GNUNET_STATISTICS_update (GST_stats,
  1804. gettext_noop ("# disconnects due to blacklist"),
  1805. 1,
  1806. GNUNET_NO);
  1807. GST_neighbours_force_disconnect (peer);
  1808. }
  1809. /**
  1810. * Test if an existing connection is still acceptable given a new
  1811. * blacklisting client.
  1812. *
  1813. * @param cls the `struct TestConnectionContext *`
  1814. * @param peer identity of the peer
  1815. * @param address the address
  1816. * @param state current state this peer is in
  1817. * @param state_timeout timeout for the current state of the peer
  1818. * @param bandwidth_in bandwidth assigned inbound
  1819. * @param bandwidth_out bandwidth assigned outbound
  1820. */
  1821. static void
  1822. test_connection_ok (void *cls,
  1823. const struct GNUNET_PeerIdentity *peer,
  1824. const struct GNUNET_HELLO_Address *address,
  1825. enum GNUNET_TRANSPORT_PeerState state,
  1826. struct GNUNET_TIME_Absolute state_timeout,
  1827. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
  1828. struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
  1829. {
  1830. struct TestConnectionContext *tcc = cls;
  1831. struct GST_BlacklistCheck *bc;
  1832. bc = GNUNET_new (struct GST_BlacklistCheck);
  1833. GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
  1834. bc->peer = *peer;
  1835. bc->address = GNUNET_HELLO_address_copy (address);
  1836. bc->cont = &confirm_or_drop_neighbour;
  1837. bc->cont_cls = NULL;
  1838. bc->bl_pos = tcc->tc;
  1839. if (GNUNET_YES == tcc->first)
  1840. {
  1841. /* all would wait for the same client, no need to
  1842. * create more than just the first task right now */
  1843. bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
  1844. tcc->first = GNUNET_NO;
  1845. }
  1846. }
  1847. /**
  1848. * Initialize a blacklisting client. We got a blacklist-init
  1849. * message from this client, add it to the list of clients
  1850. * to query for blacklisting.
  1851. *
  1852. * @param cls the client
  1853. * @param message the blacklist-init message that was sent
  1854. */
  1855. static void
  1856. handle_client_blacklist_init (void *cls,
  1857. const struct GNUNET_MessageHeader *message)
  1858. {
  1859. struct TransportClient *tc = cls;
  1860. struct TestConnectionContext tcc;
  1861. if (CT_NONE != tc->type)
  1862. {
  1863. GNUNET_break (0);
  1864. GNUNET_SERVICE_client_drop (tc->client);
  1865. return;
  1866. }
  1867. GNUNET_SERVICE_client_mark_monitor (tc->client);
  1868. tc->type = CT_BLACKLIST;
  1869. tc->details.blacklist.call_receive_done = GNUNET_YES;
  1870. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "New blacklist client %p\n", tc);
  1871. /* confirm that all existing connections are OK! */
  1872. tcc.tc = tc;
  1873. tcc.first = GNUNET_YES;
  1874. GST_neighbours_iterate (&test_connection_ok, &tcc);
  1875. }
  1876. /**
  1877. * Free the given entry in the blacklist.
  1878. *
  1879. * @param cls unused
  1880. * @param key host identity (unused)
  1881. * @param value the blacklist entry
  1882. * @return #GNUNET_OK (continue to iterate)
  1883. */
  1884. static int
  1885. free_blacklist_entry (void *cls,
  1886. const struct GNUNET_PeerIdentity *key,
  1887. void *value)
  1888. {
  1889. char *be = value;
  1890. GNUNET_free_non_null (be);
  1891. return GNUNET_OK;
  1892. }
  1893. /**
  1894. * Set traffic metric to manipulate
  1895. *
  1896. * @param cls closure
  1897. * @param message containing information
  1898. */
  1899. static void
  1900. handle_client_set_metric (void *cls, const struct TrafficMetricMessage *tm)
  1901. {
  1902. struct TransportClient *tc = cls;
  1903. GST_manipulation_set_metric (tm);
  1904. GNUNET_SERVICE_client_continue (tc->client);
  1905. }
  1906. /**
  1907. * Function called when the service shuts down. Unloads our plugins
  1908. * and cancels pending validations.
  1909. *
  1910. * @param cls closure, unused
  1911. */
  1912. static void
  1913. shutdown_task (void *cls)
  1914. {
  1915. struct AddressToStringContext *cur;
  1916. GST_neighbours_stop ();
  1917. GST_plugins_unload ();
  1918. GST_validation_stop ();
  1919. GST_ats_done ();
  1920. GNUNET_ATS_scheduling_done (GST_ats);
  1921. GST_ats = NULL;
  1922. GNUNET_ATS_connectivity_done (GST_ats_connect);
  1923. GST_ats_connect = NULL;
  1924. GNUNET_NT_scanner_done (GST_is);
  1925. GST_is = NULL;
  1926. while (NULL != (cur = a2s_head))
  1927. {
  1928. GNUNET_CONTAINER_DLL_remove (a2s_head, a2s_tail, cur);
  1929. GNUNET_free (cur);
  1930. }
  1931. if (NULL != plugin_nc)
  1932. {
  1933. GNUNET_notification_context_destroy (plugin_nc);
  1934. plugin_nc = NULL;
  1935. }
  1936. GNUNET_CONTAINER_multipeermap_destroy (active_stccs);
  1937. active_stccs = NULL;
  1938. if (NULL != blacklist)
  1939. {
  1940. GNUNET_CONTAINER_multipeermap_iterate (blacklist,
  1941. &free_blacklist_entry,
  1942. NULL);
  1943. GNUNET_CONTAINER_multipeermap_destroy (blacklist);
  1944. blacklist = NULL;
  1945. }
  1946. GST_hello_stop ();
  1947. GST_manipulation_stop ();
  1948. if (NULL != GST_peerinfo)
  1949. {
  1950. GNUNET_PEERINFO_disconnect (GST_peerinfo);
  1951. GST_peerinfo = NULL;
  1952. }
  1953. if (NULL != GST_stats)
  1954. {
  1955. GNUNET_STATISTICS_destroy (GST_stats, GNUNET_NO);
  1956. GST_stats = NULL;
  1957. }
  1958. }
  1959. /**
  1960. * Perform next action in the blacklist check.
  1961. *
  1962. * @param cls the `struct GST_BlacklistCheck *`
  1963. */
  1964. static void
  1965. do_blacklist_check (void *cls)
  1966. {
  1967. struct GST_BlacklistCheck *bc = cls;
  1968. struct TransportClient *tc;
  1969. struct GNUNET_MQ_Envelope *env;
  1970. struct BlacklistMessage *bm;
  1971. bc->task = NULL;
  1972. while (NULL != (tc = bc->bl_pos))
  1973. {
  1974. if (CT_BLACKLIST == tc->type)
  1975. break;
  1976. bc->bl_pos = tc->next;
  1977. }
  1978. if (NULL == tc)
  1979. {
  1980. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1981. "No other blacklist clients active, will allow neighbour `%s'\n",
  1982. GNUNET_i2s (&bc->peer));
  1983. bc->cont (bc->cont_cls, &bc->peer, bc->address, bc->session, GNUNET_OK);
  1984. GST_blacklist_test_cancel (bc);
  1985. return;
  1986. }
  1987. if ((NULL != tc->details.blacklist.bc) ||
  1988. (GNUNET_NO != tc->details.blacklist.waiting_for_reply))
  1989. return; /* someone else busy with this client */
  1990. tc->details.blacklist.bc = bc;
  1991. env = GNUNET_MQ_msg (bm, GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
  1992. bm->is_allowed = htonl (0);
  1993. bm->peer = bc->peer;
  1994. GNUNET_MQ_send (tc->mq, env);
  1995. if (GNUNET_YES == tc->details.blacklist.call_receive_done)
  1996. {
  1997. tc->details.blacklist.call_receive_done = GNUNET_NO;
  1998. GNUNET_SERVICE_client_continue (tc->client);
  1999. }
  2000. tc->details.blacklist.waiting_for_reply = GNUNET_YES;
  2001. }
  2002. /**
  2003. * A blacklisting client has sent us reply. Process it.
  2004. *
  2005. * @param cls the client
  2006. * @param msg the blacklist-reply message that was sent
  2007. */
  2008. static void
  2009. handle_client_blacklist_reply (void *cls, const struct BlacklistMessage *msg)
  2010. {
  2011. struct TransportClient *tc = cls;
  2012. struct GST_BlacklistCheck *bc;
  2013. if (CT_BLACKLIST != tc->type)
  2014. {
  2015. GNUNET_break (0);
  2016. GNUNET_SERVICE_client_drop (tc->client);
  2017. return;
  2018. }
  2019. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2020. "Blacklist client %p sent reply for `%s'\n",
  2021. tc,
  2022. GNUNET_i2s (&msg->peer));
  2023. bc = tc->details.blacklist.bc;
  2024. tc->details.blacklist.bc = NULL;
  2025. tc->details.blacklist.waiting_for_reply = GNUNET_NO;
  2026. tc->details.blacklist.call_receive_done = GNUNET_YES;
  2027. if (NULL != bc)
  2028. {
  2029. /* only run this if the blacklist check has not been
  2030. * cancelled in the meantime... */
  2031. GNUNET_assert (bc->bl_pos == tc);
  2032. if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
  2033. {
  2034. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2035. "Blacklist check failed, peer not allowed\n");
  2036. /* For the duration of the continuation, make the ongoing
  2037. check invisible (to avoid double-cancellation); then
  2038. add it back again so we can re-use GST_blacklist_test_cancel() */
  2039. GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
  2040. bc->cont (bc->cont_cls, &bc->peer, bc->address, bc->session, GNUNET_NO);
  2041. GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
  2042. GST_blacklist_test_cancel (bc);
  2043. tc->details.blacklist.call_receive_done = GNUNET_NO;
  2044. GNUNET_SERVICE_client_continue (tc->client);
  2045. return;
  2046. }
  2047. else
  2048. {
  2049. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2050. "Blacklist check succeeded, continuing with checks\n");
  2051. tc->details.blacklist.call_receive_done = GNUNET_NO;
  2052. GNUNET_SERVICE_client_continue (tc->client);
  2053. bc->bl_pos = tc->next;
  2054. bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
  2055. }
  2056. }
  2057. /* check if any other blacklist checks are waiting for this blacklister */
  2058. for (bc = bc_head; bc != NULL; bc = bc->next)
  2059. if ((bc->bl_pos == tc) && (NULL == bc->task))
  2060. {
  2061. bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
  2062. break;
  2063. }
  2064. }
  2065. /**
  2066. * Add the given peer to the blacklist (for the given transport).
  2067. *
  2068. * @param peer peer to blacklist
  2069. * @param transport_name transport to blacklist for this peer, NULL for all
  2070. */
  2071. void
  2072. GST_blacklist_add_peer (const struct GNUNET_PeerIdentity *peer,
  2073. const char *transport_name)
  2074. {
  2075. char *transport = NULL;
  2076. if (NULL != transport_name)
  2077. {
  2078. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2079. "Adding peer `%s' with plugin `%s' to blacklist\n",
  2080. GNUNET_i2s (peer),
  2081. transport_name);
  2082. transport = GNUNET_strdup (transport_name);
  2083. }
  2084. else
  2085. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2086. "Adding peer `%s' with all plugins to blacklist\n",
  2087. GNUNET_i2s (peer));
  2088. if (NULL == blacklist)
  2089. blacklist =
  2090. GNUNET_CONTAINER_multipeermap_create (TRANSPORT_BLACKLIST_HT_SIZE,
  2091. GNUNET_NO);
  2092. GNUNET_CONTAINER_multipeermap_put (blacklist,
  2093. peer,
  2094. transport,
  2095. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
  2096. }
  2097. /**
  2098. * Abort blacklist if @a address and @a session match.
  2099. *
  2100. * @param address address used to abort matching checks
  2101. * @param session session used to abort matching checks
  2102. */
  2103. void
  2104. GST_blacklist_abort_matching (const struct GNUNET_HELLO_Address *address,
  2105. struct GNUNET_ATS_Session *session)
  2106. {
  2107. struct GST_BlacklistCheck *bc;
  2108. struct GST_BlacklistCheck *n;
  2109. n = bc_head;
  2110. while (NULL != (bc = n))
  2111. {
  2112. n = bc->next;
  2113. if ((bc->session == session) &&
  2114. (0 == GNUNET_HELLO_address_cmp (bc->address, address)))
  2115. {
  2116. bc->cont (bc->cont_cls,
  2117. &bc->peer,
  2118. bc->address,
  2119. bc->session,
  2120. GNUNET_SYSERR);
  2121. GST_blacklist_test_cancel (bc);
  2122. }
  2123. }
  2124. }
  2125. /**
  2126. * Test if the given blacklist entry matches. If so,
  2127. * abort the iteration.
  2128. *
  2129. * @param cls the transport name to match (const char*)
  2130. * @param key the key (unused)
  2131. * @param value the 'char *' (name of a blacklisted transport)
  2132. * @return #GNUNET_OK if the entry does not match, #GNUNET_NO if it matches
  2133. */
  2134. static int
  2135. test_blacklisted (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
  2136. {
  2137. const char *transport_name = cls;
  2138. char *be = value;
  2139. /* Blacklist entry be:
  2140. * (NULL == be): peer is blacklisted with all plugins
  2141. * (NULL != be): peer is blacklisted for a specific plugin
  2142. *
  2143. * If (NULL != transport_name) we look for a transport specific entry:
  2144. * if (transport_name == be) forbidden
  2145. *
  2146. */GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2147. "Comparing BL request for peer `%4s':`%s' with BL entry: `%s'\n",
  2148. GNUNET_i2s (key),
  2149. (NULL == transport_name) ? "unspecified" : transport_name,
  2150. (NULL == be) ? "all plugins" : be);
  2151. /* all plugins for this peer were blacklisted: disallow */
  2152. if (NULL == value)
  2153. return GNUNET_NO;
  2154. /* blacklist check for specific transport */
  2155. if ((NULL != transport_name) && (NULL != value))
  2156. {
  2157. if (0 == strcmp (transport_name, be))
  2158. return GNUNET_NO; /* plugin is blacklisted! */
  2159. }
  2160. return GNUNET_OK;
  2161. }
  2162. /**
  2163. * Test if a peer/transport combination is blacklisted.
  2164. *
  2165. * @param peer the identity of the peer to test
  2166. * @param transport_name name of the transport to test, never NULL
  2167. * @param cont function to call with result
  2168. * @param cont_cls closure for @a cont
  2169. * @param address address to pass back to @a cont, can be NULL
  2170. * @param session session to pass back to @a cont, can be NULL
  2171. * @return handle to the blacklist check, NULL if the decision
  2172. * was made instantly and @a cont was already called
  2173. */
  2174. struct GST_BlacklistCheck *
  2175. GST_blacklist_test_allowed (const struct GNUNET_PeerIdentity *peer,
  2176. const char *transport_name,
  2177. GST_BlacklistTestContinuation cont,
  2178. void *cont_cls,
  2179. const struct GNUNET_HELLO_Address *address,
  2180. struct GNUNET_ATS_Session *session)
  2181. {
  2182. struct GST_BlacklistCheck *bc;
  2183. struct TransportClient *tc;
  2184. GNUNET_assert (NULL != peer);
  2185. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2186. "Blacklist check for peer `%s':%s\n",
  2187. GNUNET_i2s (peer),
  2188. (NULL != transport_name) ? transport_name : "unspecified");
  2189. /* Check local blacklist by iterating over hashmap
  2190. * If iteration is aborted, we found a matching blacklist entry */
  2191. if ((NULL != blacklist) &&
  2192. (GNUNET_SYSERR ==
  2193. GNUNET_CONTAINER_multipeermap_get_multiple (blacklist,
  2194. peer,
  2195. &test_blacklisted,
  2196. (void *) transport_name)))
  2197. {
  2198. /* Disallowed by config, disapprove instantly */
  2199. GNUNET_STATISTICS_update (GST_stats,
  2200. gettext_noop ("# disconnects due to blacklist"),
  2201. 1,
  2202. GNUNET_NO);
  2203. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2204. _ ("Disallowing connection to peer `%s' on transport %s\n"),
  2205. GNUNET_i2s (peer),
  2206. (NULL != transport_name) ? transport_name : "unspecified");
  2207. if (NULL != cont)
  2208. cont (cont_cls, peer, address, session, GNUNET_NO);
  2209. return NULL;
  2210. }
  2211. for (tc = clients_head; NULL != tc; tc = tc->next)
  2212. if (CT_BLACKLIST == tc->type)
  2213. break;
  2214. if (NULL == tc)
  2215. {
  2216. /* no blacklist clients, approve instantly */
  2217. if (NULL != cont)
  2218. cont (cont_cls, peer, address, session, GNUNET_OK);
  2219. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2220. "Allowing connection to peer `%s' %s\n",
  2221. GNUNET_i2s (peer),
  2222. (NULL != transport_name) ? transport_name : "");
  2223. return NULL;
  2224. }
  2225. /* need to query blacklist clients */
  2226. bc = GNUNET_new (struct GST_BlacklistCheck);
  2227. GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
  2228. bc->peer = *peer;
  2229. bc->address = GNUNET_HELLO_address_copy (address);
  2230. bc->session = session;
  2231. bc->cont = cont;
  2232. bc->cont_cls = cont_cls;
  2233. bc->bl_pos = tc;
  2234. bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
  2235. return bc;
  2236. }
  2237. /**
  2238. * Cancel a blacklist check.
  2239. *
  2240. * @param bc check to cancel
  2241. */
  2242. void
  2243. GST_blacklist_test_cancel (struct GST_BlacklistCheck *bc)
  2244. {
  2245. GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
  2246. if (NULL != bc->bl_pos)
  2247. {
  2248. if ((CT_BLACKLIST == bc->bl_pos->type) &&
  2249. (bc->bl_pos->details.blacklist.bc == bc))
  2250. {
  2251. /* we're at the head of the queue, remove us! */
  2252. bc->bl_pos->details.blacklist.bc = NULL;
  2253. }
  2254. }
  2255. if (NULL != bc->task)
  2256. {
  2257. GNUNET_SCHEDULER_cancel (bc->task);
  2258. bc->task = NULL;
  2259. }
  2260. GNUNET_free_non_null (bc->address);
  2261. GNUNET_free (bc);
  2262. }
  2263. /**
  2264. * Function to iterate over options in the blacklisting section for a peer.
  2265. *
  2266. * @param cls closure
  2267. * @param section name of the section
  2268. * @param option name of the option
  2269. * @param value value of the option
  2270. */
  2271. static void
  2272. blacklist_cfg_iter (void *cls,
  2273. const char *section,
  2274. const char *option,
  2275. const char *value)
  2276. {
  2277. unsigned int *res = cls;
  2278. struct GNUNET_PeerIdentity peer;
  2279. char *plugs;
  2280. char *pos;
  2281. if (GNUNET_OK !=
  2282. GNUNET_CRYPTO_eddsa_public_key_from_string (option,
  2283. strlen (option),
  2284. &peer.public_key))
  2285. return;
  2286. if ((NULL == value) || (0 == strcmp (value, "")))
  2287. {
  2288. /* Blacklist whole peer */
  2289. GST_blacklist_add_peer (&peer, NULL);
  2290. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2291. _ ("Adding blacklisting entry for peer `%s'\n"),
  2292. GNUNET_i2s (&peer));
  2293. }
  2294. else
  2295. {
  2296. plugs = GNUNET_strdup (value);
  2297. for (pos = strtok (plugs, " "); pos != NULL; pos = strtok (NULL, " "))
  2298. {
  2299. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2300. _ ("Adding blacklisting entry for peer `%s':`%s'\n"),
  2301. GNUNET_i2s (&peer),
  2302. pos);
  2303. GST_blacklist_add_peer (&peer, pos);
  2304. }
  2305. GNUNET_free (plugs);
  2306. }
  2307. (*res)++;
  2308. }
  2309. /**
  2310. * Read blacklist configuration
  2311. *
  2312. * @param cfg the configuration handle
  2313. * @param my_id my peer identity
  2314. */
  2315. static void
  2316. read_blacklist_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg,
  2317. const struct GNUNET_PeerIdentity *my_id)
  2318. {
  2319. char cfg_sect[512];
  2320. unsigned int res = 0;
  2321. GNUNET_snprintf (cfg_sect,
  2322. sizeof(cfg_sect),
  2323. "transport-blacklist-%s",
  2324. GNUNET_i2s_full (my_id));
  2325. GNUNET_CONFIGURATION_iterate_section_values (cfg,
  2326. cfg_sect,
  2327. &blacklist_cfg_iter,
  2328. &res);
  2329. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2330. "Loaded %u blacklisting entries from configuration\n",
  2331. res);
  2332. }
  2333. /**
  2334. * Initiate transport service.
  2335. *
  2336. * @param cls closure
  2337. * @param c configuration to use
  2338. * @param service the initialized service
  2339. */
  2340. static void
  2341. run (void *cls,
  2342. const struct GNUNET_CONFIGURATION_Handle *c,
  2343. struct GNUNET_SERVICE_Handle *service)
  2344. {
  2345. char *keyfile;
  2346. long long unsigned int max_fd_cfg;
  2347. int max_fd_rlimit;
  2348. int max_fd;
  2349. int friend_only;
  2350. /* setup globals */
  2351. GST_cfg = c;
  2352. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c,
  2353. "PEER",
  2354. "PRIVATE_KEY",
  2355. &keyfile))
  2356. {
  2357. GNUNET_log (
  2358. GNUNET_ERROR_TYPE_ERROR,
  2359. _ (
  2360. "Transport service is lacking key configuration settings. Exiting.\n"));
  2361. GNUNET_SCHEDULER_shutdown ();
  2362. return;
  2363. }
  2364. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_time (c,
  2365. "transport",
  2366. "HELLO_EXPIRATION",
  2367. &hello_expiration))
  2368. {
  2369. hello_expiration = GNUNET_CONSTANTS_HELLO_ADDRESS_EXPIRATION;
  2370. }
  2371. if (GNUNET_SYSERR ==
  2372. GNUNET_CRYPTO_eddsa_key_from_file (keyfile,
  2373. GNUNET_YES,
  2374. &GST_my_private_key))
  2375. {
  2376. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2377. "Failed to setup peer's private key\n");
  2378. GNUNET_SCHEDULER_shutdown ();
  2379. GNUNET_free (keyfile);
  2380. return;
  2381. }
  2382. GNUNET_free (keyfile);
  2383. GST_stats = GNUNET_STATISTICS_create ("transport", GST_cfg);
  2384. GST_peerinfo = GNUNET_PEERINFO_connect (GST_cfg);
  2385. GNUNET_CRYPTO_eddsa_key_get_public (&GST_my_private_key,
  2386. &GST_my_identity.public_key);
  2387. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  2388. "My identity is `%s'\n",
  2389. GNUNET_i2s_full (&GST_my_identity));
  2390. GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
  2391. if (NULL == GST_peerinfo)
  2392. {
  2393. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  2394. _ ("Could not access PEERINFO service. Exiting.\n"));
  2395. GNUNET_SCHEDULER_shutdown ();
  2396. return;
  2397. }
  2398. max_fd_rlimit = 0;
  2399. #if HAVE_GETRLIMIT
  2400. {
  2401. struct rlimit r_file;
  2402. if (0 == getrlimit (RLIMIT_NOFILE, &r_file))
  2403. {
  2404. max_fd_rlimit = r_file.rlim_cur;
  2405. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2406. "Maximum number of open files was: %u/%u\n",
  2407. (unsigned int) r_file.rlim_cur,
  2408. (unsigned int) r_file.rlim_max);
  2409. }
  2410. max_fd_rlimit =
  2411. (9 * max_fd_rlimit) / 10; /* Keep 10% for rest of transport */
  2412. }
  2413. #endif
  2414. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (GST_cfg,
  2415. "transport",
  2416. "MAX_FD",
  2417. &max_fd_cfg))
  2418. max_fd_cfg = max_fd_rlimit;
  2419. if (max_fd_cfg > max_fd_rlimit)
  2420. max_fd = max_fd_cfg;
  2421. else
  2422. max_fd = max_fd_rlimit;
  2423. if (max_fd < DEFAULT_MAX_FDS)
  2424. max_fd = DEFAULT_MAX_FDS;
  2425. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2426. "Limiting number of sockets to %u: validation %u, neighbors: %u\n",
  2427. max_fd,
  2428. (max_fd / 3),
  2429. (max_fd / 3) * 2);
  2430. friend_only =
  2431. GNUNET_CONFIGURATION_get_value_yesno (GST_cfg, "topology", "FRIENDS-ONLY");
  2432. if (GNUNET_SYSERR == friend_only)
  2433. friend_only = GNUNET_NO; /* According to topology defaults */
  2434. /* start subsystems */
  2435. /* Disable DSTJ peer */
  2436. {
  2437. struct GNUNET_PeerIdentity dstj;
  2438. const char *ds = "DSTJBRRKZ8TBW3FGK6B0M5QXWT9WYNZ45H5MCV4HY7ST64Q8T9F0";
  2439. GNUNET_assert (
  2440. GNUNET_OK ==
  2441. GNUNET_CRYPTO_eddsa_public_key_from_string (ds,
  2442. strlen (ds),
  2443. &dstj.public_key));
  2444. GST_blacklist_add_peer (&dstj, NULL);
  2445. }
  2446. read_blacklist_configuration (GST_cfg, &GST_my_identity);
  2447. GST_is = GNUNET_NT_scanner_init ();
  2448. GST_ats_connect = GNUNET_ATS_connectivity_init (GST_cfg);
  2449. GST_ats =
  2450. GNUNET_ATS_scheduling_init (GST_cfg, &ats_request_address_change, NULL);
  2451. GST_ats_init ();
  2452. GST_manipulation_init ();
  2453. GST_plugins_load (&GST_manipulation_recv,
  2454. &plugin_env_address_change_notification,
  2455. &plugin_env_session_start,
  2456. &plugin_env_session_end);
  2457. GST_hello_start (friend_only, &process_hello_update, NULL);
  2458. GST_neighbours_start ((max_fd / 3) * 2);
  2459. active_stccs = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_YES);
  2460. plugin_nc = GNUNET_notification_context_create (0);
  2461. GST_validation_start ((max_fd / 3));
  2462. }
  2463. /**
  2464. * Define "main" method using service macro.
  2465. */
  2466. GNUNET_SERVICE_MAIN (
  2467. "transport",
  2468. GNUNET_SERVICE_OPTION_NONE,
  2469. &run,
  2470. &client_connect_cb,
  2471. &client_disconnect_cb,
  2472. NULL,
  2473. GNUNET_MQ_hd_fixed_size (client_start,
  2474. GNUNET_MESSAGE_TYPE_TRANSPORT_START,
  2475. struct StartMessage,
  2476. NULL),
  2477. GNUNET_MQ_hd_var_size (client_hello,
  2478. GNUNET_MESSAGE_TYPE_HELLO,
  2479. struct GNUNET_MessageHeader,
  2480. NULL),
  2481. GNUNET_MQ_hd_var_size (client_send,
  2482. GNUNET_MESSAGE_TYPE_TRANSPORT_SEND,
  2483. struct OutboundMessage,
  2484. NULL),
  2485. GNUNET_MQ_hd_var_size (client_address_to_string,
  2486. GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_TO_STRING,
  2487. struct AddressLookupMessage,
  2488. NULL),
  2489. GNUNET_MQ_hd_fixed_size (client_monitor_peers,
  2490. GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST,
  2491. struct PeerMonitorMessage,
  2492. NULL),
  2493. GNUNET_MQ_hd_fixed_size (client_blacklist_init,
  2494. GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_INIT,
  2495. struct GNUNET_MessageHeader,
  2496. NULL),
  2497. GNUNET_MQ_hd_fixed_size (client_blacklist_reply,
  2498. GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_REPLY,
  2499. struct BlacklistMessage,
  2500. NULL),
  2501. GNUNET_MQ_hd_fixed_size (client_set_metric,
  2502. GNUNET_MESSAGE_TYPE_TRANSPORT_TRAFFIC_METRIC,
  2503. struct TrafficMetricMessage,
  2504. NULL),
  2505. GNUNET_MQ_hd_fixed_size (client_monitor_plugins,
  2506. GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_START,
  2507. struct GNUNET_MessageHeader,
  2508. NULL),
  2509. GNUNET_MQ_handler_end ());
  2510. /* end of file gnunet-service-transport.c */