gnunet_transport_service.h 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009-2014 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file include/gnunet_transport_service.h
  19. * @brief low-level P2P IO
  20. * @author Christian Grothoff
  21. */
  22. #ifndef GNUNET_TRANSPORT_SERVICE_H
  23. #define GNUNET_TRANSPORT_SERVICE_H
  24. #ifdef __cplusplus
  25. extern "C"
  26. {
  27. #if 0 /* keep Emacsens' auto-indent happy */
  28. }
  29. #endif
  30. #endif
  31. #include "gnunet_util_lib.h"
  32. #include "gnunet_ats_service.h"
  33. /**
  34. * Version number of the transport API.
  35. */
  36. #define GNUNET_TRANSPORT_VERSION 0x00000002
  37. /**
  38. * Function called by the transport for each received message.
  39. *
  40. * @param cls closure
  41. * @param peer (claimed) identity of the other peer
  42. * @param message the message
  43. */
  44. typedef void
  45. (*GNUNET_TRANSPORT_ReceiveCallback) (void *cls,
  46. const struct GNUNET_PeerIdentity *peer,
  47. const struct GNUNET_MessageHeader *message);
  48. /**
  49. * Opaque handle to the service.
  50. */
  51. struct GNUNET_TRANSPORT_Handle;
  52. /**
  53. * Function called to notify transport users that another
  54. * peer connected to us.
  55. *
  56. * @param cls closure
  57. * @param peer the peer that connected
  58. */
  59. typedef void
  60. (*GNUNET_TRANSPORT_NotifyConnect) (void *cls,
  61. const struct GNUNET_PeerIdentity *peer);
  62. /**
  63. * Function called to notify transport users that another
  64. * peer disconnected from us.
  65. *
  66. * @param cls closure
  67. * @param peer the peer that disconnected
  68. */
  69. typedef void
  70. (*GNUNET_TRANSPORT_NotifyDisconnect) (void *cls,
  71. const struct GNUNET_PeerIdentity *peer);
  72. /**
  73. * Connect to the transport service. Note that the connection may
  74. * complete (or fail) asynchronously.
  75. *
  76. * @param cfg configuration to use
  77. * @param self our own identity (API should check that it matches
  78. * the identity found by transport), or NULL (no check)
  79. * @param cls closure for the callbacks
  80. * @param rec receive function to call, or NULL
  81. * @param nc function to call on connect events, or NULL
  82. * @param nd function to call on disconnect events, or NULL
  83. * @return NULL on error
  84. */
  85. struct GNUNET_TRANSPORT_Handle *
  86. GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
  87. const struct GNUNET_PeerIdentity *self,
  88. void *cls,
  89. GNUNET_TRANSPORT_ReceiveCallback rec,
  90. GNUNET_TRANSPORT_NotifyConnect nc,
  91. GNUNET_TRANSPORT_NotifyDisconnect nd);
  92. /**
  93. * Function called if we have "excess" bandwidth to a peer.
  94. * The notification will happen the first time we have excess
  95. * bandwidth, and then only again after the client has performed
  96. * some transmission to the peer.
  97. *
  98. * Excess bandwidth is defined as being allowed (by ATS) to send
  99. * more data, and us reaching the limit of the capacity build-up
  100. * (which, if we go past it, means we don't use available bandwidth).
  101. * See also the "max carry" in `struct GNUNET_BANDWIDTH_Tracker`.
  102. *
  103. * @param cls the closure
  104. * @param neighbour peer that we have excess bandwidth to
  105. */
  106. typedef void
  107. (*GNUNET_TRANSPORT_NotifyExcessBandwidth)(void *cls,
  108. const struct GNUNET_PeerIdentity *neighbour);
  109. /**
  110. * Connect to the transport service. Note that the connection may
  111. * complete (or fail) asynchronously.
  112. *
  113. * @param cfg configuration to use
  114. * @param self our own identity (API should check that it matches
  115. * the identity found by transport), or NULL (no check)
  116. * @param cls closure for the callbacks
  117. * @param rec receive function to call, or NULL
  118. * @param nc function to call on connect events, or NULL
  119. * @param nd function to call on disconnect events, or NULL
  120. * @param neb function to call if we have excess bandwidth to a peer
  121. * @return NULL on error
  122. */
  123. struct GNUNET_TRANSPORT_Handle *
  124. GNUNET_TRANSPORT_connect2 (const struct GNUNET_CONFIGURATION_Handle *cfg,
  125. const struct GNUNET_PeerIdentity *self,
  126. void *cls,
  127. GNUNET_TRANSPORT_ReceiveCallback rec,
  128. GNUNET_TRANSPORT_NotifyConnect nc,
  129. GNUNET_TRANSPORT_NotifyDisconnect nd,
  130. GNUNET_TRANSPORT_NotifyExcessBandwidth neb);
  131. /**
  132. * Disconnect from the transport service.
  133. *
  134. * @param handle handle returned from connect
  135. */
  136. void
  137. GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle);
  138. /* ************************* Connections *********************** */
  139. /**
  140. * Opaque handle for a transmission-ready request.
  141. */
  142. struct GNUNET_TRANSPORT_TryConnectHandle;
  143. /**
  144. * Function to call with result of the try connect request.
  145. *
  146. * @param cls closure
  147. * @param result #GNUNET_OK if message was transmitted to transport service
  148. * #GNUNET_SYSERR if message was not transmitted to transport service
  149. */
  150. typedef void
  151. (*GNUNET_TRANSPORT_TryConnectCallback) (void *cls,
  152. int result);
  153. /**
  154. * Ask the transport service to establish a connection to
  155. * the given peer.
  156. *
  157. * @param handle connection to transport service
  158. * @param target who we should try to connect to
  159. * @param cb callback to be called when request was transmitted to transport
  160. * service
  161. * @param cb_cls closure for the callback @a cb
  162. * @return a `struct GNUNET_TRANSPORT_TryConnectHandle` handle or
  163. * NULL on failure (@a cb will not be called)
  164. */
  165. struct GNUNET_TRANSPORT_TryConnectHandle *
  166. GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
  167. const struct GNUNET_PeerIdentity *target,
  168. GNUNET_TRANSPORT_TryConnectCallback cb,
  169. void *cb_cls);
  170. /**
  171. * Cancel the request to transport to try a connect
  172. * Callback will not be called
  173. *
  174. * @param tch handle to cancel
  175. */
  176. void
  177. GNUNET_TRANSPORT_try_connect_cancel (struct GNUNET_TRANSPORT_TryConnectHandle *tch);
  178. /**
  179. * Opaque handle for a transmission-ready request.
  180. */
  181. struct GNUNET_TRANSPORT_TryDisconnectHandle;
  182. /**
  183. * Function to call with result of the try connect request.
  184. *
  185. * @param cls closure
  186. * @param result #GNUNET_OK if message was transmitted to transport service
  187. * #GNUNET_SYSERR if message was not transmitted to transport service
  188. */
  189. typedef void
  190. (*GNUNET_TRANSPORT_TryDisconnectCallback) (void *cls,
  191. int result);
  192. /**
  193. * Ask the transport service to disconnect from the given peer.
  194. *
  195. * @param handle connection to transport service
  196. * @param target who we should try to disconnect from
  197. * @param cb callback to be called when request was transmitted to transport
  198. * service
  199. * @param cb_cls closure for the callback @a cb
  200. * @return a `struct GNUNET_TRANSPORT_TryConnectHandle` handle or
  201. * NULL on failure (@a cb will not be called)
  202. */
  203. struct GNUNET_TRANSPORT_TryDisconnectHandle *
  204. GNUNET_TRANSPORT_try_disconnect (struct GNUNET_TRANSPORT_Handle *handle,
  205. const struct GNUNET_PeerIdentity *target,
  206. GNUNET_TRANSPORT_TryDisconnectCallback cb,
  207. void *cb_cls);
  208. /**
  209. * Cancel the request to transport to disconnect.
  210. * Callback will not be called anymore.
  211. *
  212. * @param tdh handle for operation to cancel
  213. */
  214. void
  215. GNUNET_TRANSPORT_try_disconnect_cancel (struct GNUNET_TRANSPORT_TryDisconnectHandle *tdh);
  216. /* ************************* Sending *************************** */
  217. /**
  218. * Opaque handle for a transmission-ready request.
  219. */
  220. struct GNUNET_TRANSPORT_TransmitHandle;
  221. /**
  222. * Function called to notify a client about the connection begin ready
  223. * to queue more data. @a buf will be NULL and @a size zero if the
  224. * connection was closed for writing in the meantime.
  225. *
  226. * @param cls closure
  227. * @param size number of bytes available in @a buf
  228. * @param buf where the callee should write the message
  229. * @return number of bytes written to @a buf
  230. */
  231. typedef size_t
  232. (*GNUNET_TRANSPORT_TransmitReadyNotify) (void *cls,
  233. size_t size,
  234. void *buf);
  235. /**
  236. * Check if we could queue a message of the given size for
  237. * transmission. The transport service will take both its internal
  238. * buffers and bandwidth limits imposed by the other peer into
  239. * consideration when answering this query.
  240. *
  241. * @param handle connection to transport service
  242. * @param target who should receive the message
  243. * @param size how big is the message we want to transmit?
  244. * @param timeout after how long should we give up (and call
  245. * notify with buf NULL and size 0)?
  246. * @param notify function to call when we are ready to
  247. * send such a message
  248. * @param notify_cls closure for @a notify
  249. * @return NULL if someone else is already waiting to be notified
  250. * non-NULL if the notify callback was queued (can be used to cancel
  251. * using #GNUNET_TRANSPORT_notify_transmit_ready_cancel())
  252. */
  253. struct GNUNET_TRANSPORT_TransmitHandle *
  254. GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
  255. const struct GNUNET_PeerIdentity *target,
  256. size_t size,
  257. struct GNUNET_TIME_Relative timeout,
  258. GNUNET_TRANSPORT_TransmitReadyNotify notify,
  259. void *notify_cls);
  260. /**
  261. * Cancel the specified transmission-ready notification.
  262. *
  263. * @param th handle of the transmission notification request to cancel
  264. */
  265. void
  266. GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct GNUNET_TRANSPORT_TransmitHandle *th);
  267. /**
  268. * Checks if a given peer is connected to us
  269. *
  270. * @param handle connection to transport service
  271. * @param peer the peer to check
  272. * @return #GNUNET_YES (connected) or #GNUNET_NO (disconnected)
  273. */
  274. int
  275. GNUNET_TRANSPORT_check_peer_connected (struct GNUNET_TRANSPORT_Handle *handle,
  276. const struct GNUNET_PeerIdentity *peer);
  277. /* *********************** Metric manipulation ***************** */
  278. /**
  279. * Set transport metrics for a peer and a direction
  280. *
  281. * @param handle transport handle
  282. * @param peer the peer to set the metric for
  283. * @param prop the performance metrics to set
  284. * @param delay_in inbound delay to introduce
  285. * @param delay_out outbound delay to introduce
  286. *
  287. * Note: Delay restrictions in receiving direction will be enforced
  288. * with one message delay.
  289. */
  290. void
  291. GNUNET_TRANSPORT_set_traffic_metric (struct GNUNET_TRANSPORT_Handle *handle,
  292. const struct GNUNET_PeerIdentity *peer,
  293. const struct GNUNET_ATS_Properties *prop,
  294. struct GNUNET_TIME_Relative delay_in,
  295. struct GNUNET_TIME_Relative delay_out);
  296. /* *************************** HELLO *************************** */
  297. /**
  298. * Function called whenever there is an update to the
  299. * HELLO of this peer.
  300. *
  301. * @param cls closure
  302. * @param hello our updated HELLO
  303. */
  304. typedef void
  305. (*GNUNET_TRANSPORT_HelloUpdateCallback) (void *cls,
  306. const struct GNUNET_MessageHeader *hello);
  307. /**
  308. * Handle to cancel a #GNUNET_TRANSPORT_get_hello() operation.
  309. */
  310. struct GNUNET_TRANSPORT_GetHelloHandle;
  311. /**
  312. * Obtain updates on changes to the HELLO message for this peer. The callback
  313. * given in this function is never called synchronously.
  314. *
  315. * @param handle connection to transport service
  316. * @param rec function to call with the HELLO
  317. * @param rec_cls closure for @a rec
  318. * @return handle to cancel the operation
  319. */
  320. struct GNUNET_TRANSPORT_GetHelloHandle *
  321. GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
  322. GNUNET_TRANSPORT_HelloUpdateCallback rec,
  323. void *rec_cls);
  324. /**
  325. * Stop receiving updates about changes to our HELLO message.
  326. *
  327. * @param ghh handle to cancel
  328. */
  329. void
  330. GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh);
  331. /**
  332. * Handle for a #GNUNET_TRANSPORT_offer_hello operation
  333. */
  334. struct GNUNET_TRANSPORT_OfferHelloHandle;
  335. /**
  336. * Offer the transport service the HELLO of another peer. Note that
  337. * the transport service may just ignore this message if the HELLO is
  338. * malformed or useless due to our local configuration.
  339. *
  340. * @param handle connection to transport service
  341. * @param hello the hello message
  342. * @param cont continuation to call when HELLO has been sent,
  343. * tc reason #GNUNET_SCHEDULER_REASON_TIMEOUT for fail
  344. * tc reasong #GNUNET_SCHEDULER_REASON_READ_READY for success
  345. * @param cls closure for continuation
  346. * @return a GNUNET_TRANSPORT_OfferHelloHandle handle or NULL on failure,
  347. * in case of failure cont will not be called
  348. *
  349. */
  350. struct GNUNET_TRANSPORT_OfferHelloHandle *
  351. GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
  352. const struct GNUNET_MessageHeader *hello,
  353. GNUNET_SCHEDULER_TaskCallback cont, void *cls);
  354. /**
  355. * Cancel the request to transport to offer the HELLO message
  356. *
  357. * @param ohh the `struct GNUNET_TRANSPORT_OfferHelloHandle` to cancel
  358. */
  359. void
  360. GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh);
  361. /* *********************** Address to String ******************* */
  362. /**
  363. * Handle to cancel a pending address lookup.
  364. */
  365. struct GNUNET_TRANSPORT_AddressToStringContext;
  366. /**
  367. * Function to call with a textual representation of an address. This
  368. * function will be called several times with different possible
  369. * textual representations, and a last time with @a address being NULL
  370. * to signal the end of the iteration. Note that @a address NULL
  371. * always is the last call, regardless of the value in @a res.
  372. *
  373. * @param cls closure
  374. * @param address NULL on end of iteration,
  375. * otherwise 0-terminated printable UTF-8 string,
  376. * in particular an empty string if @a res is #GNUNET_NO
  377. * @param res result of the address to string conversion:
  378. * if #GNUNET_OK: conversion successful
  379. * if #GNUNET_NO: address was invalid (or not supported)
  380. * if #GNUNET_SYSERR: communication error (IPC error)
  381. */
  382. typedef void
  383. (*GNUNET_TRANSPORT_AddressToStringCallback) (void *cls,
  384. const char *address,
  385. int res);
  386. /**
  387. * Convert a binary address into a human readable address.
  388. *
  389. * @param cfg configuration to use
  390. * @param address address to convert (binary format)
  391. * @param numeric should (IP) addresses be displayed in numeric form
  392. * (otherwise do reverse DNS lookup)
  393. * @param timeout how long is the lookup allowed to take at most
  394. * @param aluc function to call with the results
  395. * @param aluc_cls closure for @a aluc
  396. * @return handle to cancel the operation, NULL on error
  397. */
  398. struct GNUNET_TRANSPORT_AddressToStringContext *
  399. GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
  400. const struct GNUNET_HELLO_Address *address,
  401. int numeric,
  402. struct GNUNET_TIME_Relative timeout,
  403. GNUNET_TRANSPORT_AddressToStringCallback aluc,
  404. void *aluc_cls);
  405. /**
  406. * Cancel request for address conversion.
  407. *
  408. * @param pic the context handle
  409. */
  410. void
  411. GNUNET_TRANSPORT_address_to_string_cancel (struct GNUNET_TRANSPORT_AddressToStringContext *pic);
  412. /* *********************** Monitoring ************************** */
  413. /**
  414. * Possible state of a neighbour. Initially, we are #GNUNET_TRANSPORT_PS_NOT_CONNECTED.
  415. *
  416. * Then, there are two main paths. If we receive a SYN message, we give
  417. * the inbound address to ATS. After the check we ask ATS for a suggestion
  418. * (#GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS). If ATS makes a suggestion, we
  419. * send our SYN_ACK and go to #GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK.
  420. * If we receive a ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED
  421. * (and notify everyone about the new connection). If the operation times out,
  422. * we go to #GNUNET_TRANSPORT_PS_DISCONNECT.
  423. *
  424. * The other case is where we transmit a SYN message first. We
  425. * start with #GNUNET_TRANSPORT_PS_INIT_ATS. If we get an address, we send
  426. * the SYN message and go to state #GNUNET_TRANSPORT_PS_CONNECT_SENT.
  427. * Once we receive a SYN_ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED
  428. * (and notify everyone about the new connection and send
  429. * back a ACK). If the operation times out, we go to
  430. * #GNUNET_TRANSPORT_PS_DISCONNECT.
  431. *
  432. * If the session is in trouble (i.e. transport-level disconnect or
  433. * timeout), we go to #GNUNET_TRANSPORT_PS_RECONNECT_ATS where we ask ATS for a new
  434. * address (we don't notify anyone about the disconnect yet). Once we
  435. * have a new address, we enter #GNUNET_TRANSPORT_PS_RECONNECT_SENT and send a
  436. * SYN message. If we receive a
  437. * SYN_ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED and nobody noticed that we had
  438. * trouble; we also send a ACK at this time just in case. If
  439. * the operation times out, we go to #GNUNET_TRANSPORT_PS_DISCONNECT (and notify everyone
  440. * about the lost connection).
  441. *
  442. * If ATS decides to switch addresses while we have a normal
  443. * connection, we go to #GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT
  444. * and send a SESSION_CONNECT. If we get a ACK back, we switch the
  445. * primary connection to the suggested alternative from ATS, go back
  446. * to #GNUNET_TRANSPORT_PS_CONNECTED and send a ACK to the other peer just to be
  447. * sure. If the operation times out
  448. * we go to #GNUNET_TRANSPORT_PS_CONNECTED (and notify ATS that the given alternative
  449. * address is "invalid").
  450. *
  451. * Once a session is in #GNUNET_TRANSPORT_PS_DISCONNECT, it is cleaned up and then goes
  452. * to (#GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED). If we receive an explicit disconnect
  453. * request, we can go from any state to #GNUNET_TRANSPORT_PS_DISCONNECT, possibly after
  454. * generating disconnect notifications.
  455. *
  456. * Note that it is quite possible that while we are in any of these
  457. * states, we could receive a 'SYN' request from the other peer.
  458. * We then enter a 'weird' state where we pursue our own primary state
  459. * machine (as described above), but with the 'send_connect_ack' flag
  460. * set to 1. If our state machine allows us to send a 'SYN_ACK'
  461. * (because we have an acceptable address), we send the 'SYN_ACK'
  462. * and set the 'send_connect_ack' to 2. If we then receive a
  463. * 'ACK', we go to #GNUNET_TRANSPORT_PS_CONNECTED (and reset 'send_connect_ack'
  464. * to 0).
  465. *
  466. */
  467. enum GNUNET_TRANSPORT_PeerState
  468. {
  469. /**
  470. * Fresh peer or completely disconnected
  471. */
  472. GNUNET_TRANSPORT_PS_NOT_CONNECTED = 0,
  473. /**
  474. * Asked to initiate connection, trying to get address from ATS
  475. */
  476. GNUNET_TRANSPORT_PS_INIT_ATS,
  477. /**
  478. * Sent SYN message to other peer, waiting for SYN_ACK
  479. */
  480. GNUNET_TRANSPORT_PS_SYN_SENT,
  481. /**
  482. * Received a SYN, asking ATS about address suggestions.
  483. */
  484. GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
  485. /**
  486. * SYN request from other peer was SYN_ACK'ed, waiting for ACK.
  487. */
  488. GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
  489. /**
  490. * Got our SYN_ACK/ACK, connection is up.
  491. */
  492. GNUNET_TRANSPORT_PS_CONNECTED,
  493. /**
  494. * Connection got into trouble, rest of the system still believes
  495. * it to be up, but we're getting a new address from ATS.
  496. */
  497. GNUNET_TRANSPORT_PS_RECONNECT_ATS,
  498. /**
  499. * Sent SYN over new address (either by ATS telling us to switch
  500. * addresses or from RECONNECT_ATS); if this fails, we need to tell
  501. * the rest of the system about a disconnect.
  502. */
  503. GNUNET_TRANSPORT_PS_RECONNECT_SENT,
  504. /**
  505. * We have some primary connection, but ATS suggested we switch
  506. * to some alternative; we now sent a SYN message for the
  507. * alternative session to the other peer and waiting for a
  508. * SYN_ACK to make this our primary connection.
  509. */
  510. GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
  511. /**
  512. * Disconnect in progress (we're sending the DISCONNECT message to the
  513. * other peer; after that is finished, the state will be cleaned up).
  514. */
  515. GNUNET_TRANSPORT_PS_DISCONNECT,
  516. /**
  517. * We're finished with the disconnect; and are cleaning up the state
  518. * now! We put the struct into this state when we are really in the
  519. * task that calls 'free' on it and are about to remove the record
  520. * from the map. We should never find a 'struct NeighbourMapEntry'
  521. * in this state in the map. Accessing a 'struct NeighbourMapEntry'
  522. * in this state virtually always means using memory that has been
  523. * freed (the exception being the cleanup code in #free_neighbour()).
  524. */
  525. GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED
  526. };
  527. /**
  528. * Convert a transport state to a human readable string.
  529. *
  530. * @param state the state
  531. */
  532. const char *
  533. GNUNET_TRANSPORT_ps2s (enum GNUNET_TRANSPORT_PeerState state);
  534. /**
  535. * Check if a state is defined as connected
  536. *
  537. * @param state the state value
  538. * @return #GNUNET_YES or #GNUNET_NO
  539. */
  540. int
  541. GNUNET_TRANSPORT_is_connected (enum GNUNET_TRANSPORT_PeerState state);
  542. /**
  543. * Handle for a #GNUNET_TRANSPORT_monitor_peers operation.
  544. */
  545. struct GNUNET_TRANSPORT_PeerMonitoringContext;
  546. /**
  547. * Function to call with information about a peer
  548. *
  549. * If one_shot was set to #GNUNET_YES to iterate over all peers once,
  550. * a final call with NULL for peer and address will follow when done.
  551. * In this case state and timeout do not contain valid values.
  552. *
  553. * The #GNUNET_TRANSPORT_monitor_peers_cancel() call MUST not be called from
  554. * within this function!
  555. *
  556. *
  557. * @param cls closure
  558. * @param peer peer this update is about,
  559. * NULL if this is the final last callback for a iteration operation
  560. * @param address address, NULL if this is the final callback for iteration op
  561. * @param state current state this peer is in
  562. * @param state_timeout timeout for the current state of the peer
  563. */
  564. typedef void
  565. (*GNUNET_TRANSPORT_PeerIterateCallback) (void *cls,
  566. const struct GNUNET_PeerIdentity *peer,
  567. const struct GNUNET_HELLO_Address *address,
  568. enum GNUNET_TRANSPORT_PeerState state,
  569. struct GNUNET_TIME_Absolute state_timeout);
  570. /**
  571. * Return information about a specific peer or all peers currently known to
  572. * transport service once or in monitoring mode. To obtain information about
  573. * a specific peer, a peer identity can be passed. To obtain information about
  574. * all peers currently known to transport service, NULL can be passed as peer
  575. * identity.
  576. *
  577. * For each peer, the callback is called with information about the address used
  578. * to communicate with this peer, the state this peer is currently in and the
  579. * the current timeout for this state.
  580. *
  581. * Upon completion, the #GNUNET_TRANSPORT_PeerIterateCallback is called one
  582. * more time with `NULL`. After this, the operation must no longer be
  583. * explicitly canceled.
  584. *
  585. * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the
  586. * the peer_callback!
  587. *
  588. * @param cfg configuration to use
  589. * @param peer a specific peer identity to obtain information for,
  590. * NULL for all peers
  591. * @param one_shot #GNUNET_YES to return the current state and then end (with NULL+NULL),
  592. * #GNUNET_NO to monitor peers continuously
  593. * @param timeout how long is the lookup allowed to take at most
  594. * @param peer_callback function to call with the results
  595. * @param peer_callback_cls closure for @a peer_callback
  596. */
  597. struct GNUNET_TRANSPORT_PeerMonitoringContext *
  598. GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
  599. const struct GNUNET_PeerIdentity *peer,
  600. int one_shot,
  601. struct GNUNET_TIME_Relative timeout,
  602. GNUNET_TRANSPORT_PeerIterateCallback peer_callback,
  603. void *peer_callback_cls);
  604. /**
  605. * Cancel request to monitor peers
  606. *
  607. * @param pic handle for the request to cancel
  608. */
  609. void
  610. GNUNET_TRANSPORT_monitor_peers_cancel (struct GNUNET_TRANSPORT_PeerMonitoringContext *pic);
  611. /**
  612. * Handle for a #GNUNET_TRANSPORT_monitor_validation_entries() operation.
  613. */
  614. struct GNUNET_TRANSPORT_ValidationMonitoringContext;
  615. /**
  616. * Current state of a validation process.
  617. *
  618. * FIXME: what state is used to indicate that a validation
  619. * was successful? If that is clarified/determined, "UGH" in
  620. * ~gnunet-peerinfo-gtk.c:1103 should be resolved.
  621. */
  622. enum GNUNET_TRANSPORT_ValidationState
  623. {
  624. /**
  625. * Undefined state
  626. *
  627. * Used for final callback indicating operation done
  628. */
  629. GNUNET_TRANSPORT_VS_NONE,
  630. /**
  631. * Fresh validation entry
  632. *
  633. * Entry was just created, no validation process was executed
  634. */
  635. GNUNET_TRANSPORT_VS_NEW,
  636. /**
  637. * Updated validation entry
  638. *
  639. * This is an update for an existing validation entry
  640. */
  641. GNUNET_TRANSPORT_VS_UPDATE,
  642. /**
  643. * Timeout for validation entry
  644. *
  645. * A timeout occured during the validation process
  646. */
  647. GNUNET_TRANSPORT_VS_TIMEOUT,
  648. /**
  649. * Validation entry is removed
  650. *
  651. * The validation entry is getting removed due to a failed validation
  652. */
  653. GNUNET_TRANSPORT_VS_REMOVE
  654. };
  655. /**
  656. * Function to call with validation information about a peer
  657. *
  658. * This function is called by the transport validation monitoring api to
  659. * indicate a change to a validation entry. The information included represent
  660. * the current state of the validation entry,
  661. *
  662. * If the monitoring was called with `one_shot==GNUNET_YES`, a final callback
  663. * with `address==NULL` is executed.
  664. *
  665. * @param cls closure
  666. * @param address address this update is about,
  667. * NULL if this is the final last callback for a iteration operation
  668. * @param last_validation when was this address last validated
  669. * @param valid_until when does this address expire
  670. * @param next_validation time of the next validation operation
  671. * @param state state in the validation state machine
  672. */
  673. typedef void
  674. (*GNUNET_TRANSPORT_ValidationIterateCallback) (void *cls,
  675. const struct GNUNET_HELLO_Address *address,
  676. struct GNUNET_TIME_Absolute last_validation,
  677. struct GNUNET_TIME_Absolute valid_until,
  678. struct GNUNET_TIME_Absolute next_validation,
  679. enum GNUNET_TRANSPORT_ValidationState state);
  680. /**
  681. * Convert validation state to human-readable string.
  682. *
  683. * @param state the state value
  684. * @return corresponding string
  685. */
  686. const char *
  687. GNUNET_TRANSPORT_vs2s (enum GNUNET_TRANSPORT_ValidationState state);
  688. /**
  689. * Return information about pending address validation operations for a specific
  690. * or all peers
  691. *
  692. * @param cfg configuration to use
  693. * @param peer a specific peer identity to obtain validation entries for,
  694. * NULL for all peers
  695. * @param one_shot #GNUNET_YES to return all entries and then end (with NULL+NULL),
  696. * #GNUNET_NO to monitor validation entries continuously
  697. * @param timeout how long is the lookup allowed to take at most
  698. * @param validation_callback function to call with the results
  699. * @param validation_callback_cls closure for @a validation_callback
  700. */
  701. struct GNUNET_TRANSPORT_ValidationMonitoringContext *
  702. GNUNET_TRANSPORT_monitor_validation_entries (const struct GNUNET_CONFIGURATION_Handle *cfg,
  703. const struct GNUNET_PeerIdentity *peer,
  704. int one_shot,
  705. struct GNUNET_TIME_Relative timeout,
  706. GNUNET_TRANSPORT_ValidationIterateCallback validation_callback,
  707. void *validation_callback_cls);
  708. /**
  709. * Return information about all current pending validation operations
  710. *
  711. * @param vic handle for the request to cancel
  712. */
  713. void
  714. GNUNET_TRANSPORT_monitor_validation_entries_cancel (struct GNUNET_TRANSPORT_ValidationMonitoringContext *vic);
  715. /* *********************** Blacklisting ************************ */
  716. /**
  717. * Handle for blacklisting peers.
  718. */
  719. struct GNUNET_TRANSPORT_Blacklist;
  720. /**
  721. * Function that decides if a connection is acceptable or not.
  722. *
  723. * @param cls closure
  724. * @param pid peer to approve or disapproave
  725. * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
  726. */
  727. typedef int
  728. (*GNUNET_TRANSPORT_BlacklistCallback) (void *cls,
  729. const struct GNUNET_PeerIdentity *pid);
  730. /**
  731. * Install a blacklist callback. The service will be queried for all
  732. * existing connections as well as any fresh connections to check if
  733. * they are permitted. If the blacklisting callback is unregistered,
  734. * all hosts that were denied in the past will automatically be
  735. * whitelisted again. Cancelling the blacklist handle is also the
  736. * only way to re-enable connections from peers that were previously
  737. * blacklisted.
  738. *
  739. * @param cfg configuration to use
  740. * @param cb callback to invoke to check if connections are allowed
  741. * @param cb_cls closure for @a cb
  742. * @return NULL on error, otherwise handle for cancellation
  743. */
  744. struct GNUNET_TRANSPORT_Blacklist *
  745. GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
  746. GNUNET_TRANSPORT_BlacklistCallback cb,
  747. void *cb_cls);
  748. /**
  749. * Abort the blacklist. Note that this function is the only way for
  750. * removing a peer from the blacklist.
  751. *
  752. * @param br handle of the request that is to be cancelled
  753. */
  754. void
  755. GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br);
  756. /**
  757. * Handle for a plugin session state monitor.
  758. */
  759. struct GNUNET_TRANSPORT_PluginMonitor;
  760. /**
  761. * Abstract representation of a plugin's session.
  762. * Corresponds to the `struct Session` within the TRANSPORT service.
  763. */
  764. struct GNUNET_TRANSPORT_PluginSession;
  765. /**
  766. * Possible states of a session in a plugin.
  767. */
  768. enum GNUNET_TRANSPORT_SessionState
  769. {
  770. /**
  771. * The session was created (first call for each session object).
  772. */
  773. GNUNET_TRANSPORT_SS_INIT,
  774. /**
  775. * Initial session handshake is in progress.
  776. */
  777. GNUNET_TRANSPORT_SS_HANDSHAKE,
  778. /**
  779. * Session is fully UP.
  780. */
  781. GNUNET_TRANSPORT_SS_UP,
  782. /**
  783. * This is just an update about the session,
  784. * the state did not change.
  785. */
  786. GNUNET_TRANSPORT_SS_UPDATE,
  787. /**
  788. * Session is being torn down and about to disappear.
  789. * Last call for each session object.
  790. */
  791. GNUNET_TRANSPORT_SS_DONE
  792. };
  793. /**
  794. * Information about a plugin's session.
  795. */
  796. struct GNUNET_TRANSPORT_SessionInfo
  797. {
  798. /**
  799. * New state of the session.
  800. */
  801. enum GNUNET_TRANSPORT_SessionState state;
  802. /**
  803. * #GNUNET_YES if this is an inbound connection,
  804. * #GNUNET_NO if this is an outbound connection,
  805. * #GNUNET_SYSERR if connections of this plugin
  806. * are so fundamentally bidirectional
  807. * that they have no 'initiator'
  808. */
  809. int is_inbound;
  810. /**
  811. * Number of messages pending transmission for this session.
  812. */
  813. uint32_t num_msg_pending;
  814. /**
  815. * Number of bytes pending transmission for this session.
  816. */
  817. uint32_t num_bytes_pending;
  818. /**
  819. * Until when does this plugin refuse to receive to manage
  820. * staying within the inbound quota? ZERO if receive is
  821. * active.
  822. */
  823. struct GNUNET_TIME_Absolute receive_delay;
  824. /**
  825. * At what time will this session timeout (unless activity
  826. * happens)?
  827. */
  828. struct GNUNET_TIME_Absolute session_timeout;
  829. /**
  830. * Address used by the session. Can be NULL if none is available.
  831. */
  832. const struct GNUNET_HELLO_Address *address;
  833. };
  834. /**
  835. * Function called by the plugin with information about the
  836. * current sessions managed by the plugin (for monitoring).
  837. *
  838. * @param cls closure
  839. * @param session session handle this information is about,
  840. * NULL to indicate that we are "in sync" (initial
  841. * iteration complete)
  842. * @param session_ctx storage location where the application
  843. * can store data; will point to NULL on #GNUNET_TRANSPORT_SS_INIT,
  844. * and must be reset to NULL on #GNUNET_TRANSPORT_SS_DONE
  845. * @param info information about the state of the session,
  846. * NULL if @a session is also NULL and we are
  847. * merely signalling that the initial iteration is over;
  848. * NULL with @a session being non-NULL if the monitor
  849. * was being cancelled while sessions were active
  850. */
  851. typedef void
  852. (*GNUNET_TRANSPORT_SessionMonitorCallback) (void *cls,
  853. struct GNUNET_TRANSPORT_PluginSession *session,
  854. void **session_ctx,
  855. const struct GNUNET_TRANSPORT_SessionInfo *info);
  856. /**
  857. * Install a plugin session state monitor callback. The callback
  858. * will be notified whenever the session changes.
  859. *
  860. * @param cfg configuration to use
  861. * @param cb callback to invoke on events
  862. * @param cb_cls closure for @a cb
  863. * @return NULL on error, otherwise handle for cancellation
  864. */
  865. struct GNUNET_TRANSPORT_PluginMonitor *
  866. GNUNET_TRANSPORT_monitor_plugins (const struct GNUNET_CONFIGURATION_Handle *cfg,
  867. GNUNET_TRANSPORT_SessionMonitorCallback cb,
  868. void *cb_cls);
  869. /**
  870. * Cancel monitoring the plugin session state. The callback will be
  871. * called once for each session that is up with the "info" argument
  872. * being NULL (this is just to enable client-side cleanup).
  873. *
  874. * @param pm handle of the request that is to be cancelled
  875. */
  876. void
  877. GNUNET_TRANSPORT_monitor_plugins_cancel (struct GNUNET_TRANSPORT_PluginMonitor *pm);
  878. #if 0 /* keep Emacsens' auto-indent happy */
  879. {
  880. #endif
  881. #ifdef __cplusplus
  882. }
  883. #endif
  884. /* ifndef GNUNET_TRANSPORT_SERVICE_H */
  885. #endif
  886. /* end of gnunet_transport_service.h */