cadet_api.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2011 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 cadet/cadet_api.c
  19. * @brief cadet api: client implementation of new cadet service
  20. * @author Bartlomiej Polot
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_cadet_service.h"
  25. #include "cadet.h"
  26. #include "cadet_protocol.h"
  27. #define LOG(kind,...) GNUNET_log_from (kind, "cadet-api",__VA_ARGS__)
  28. #define DATA_OVERHEAD sizeof(struct GNUNET_CADET_LocalData)
  29. /******************************************************************************/
  30. /************************ DATA STRUCTURES ****************************/
  31. /******************************************************************************/
  32. /**
  33. * Transmission queue to the service
  34. */
  35. struct GNUNET_CADET_TransmitHandle
  36. {
  37. /**
  38. * Double Linked list
  39. */
  40. struct GNUNET_CADET_TransmitHandle *next;
  41. /**
  42. * Double Linked list
  43. */
  44. struct GNUNET_CADET_TransmitHandle *prev;
  45. /**
  46. * Channel this message is sent on / for (may be NULL for control messages).
  47. */
  48. struct GNUNET_CADET_Channel *channel;
  49. /**
  50. * Callback to obtain the message to transmit, or NULL if we
  51. * got the message in 'data'. Notice that messages built
  52. * by 'notify' need to be encapsulated with information about
  53. * the 'target'.
  54. */
  55. GNUNET_CONNECTION_TransmitReadyNotify notify;
  56. /**
  57. * Closure for 'notify'
  58. */
  59. void *notify_cls;
  60. /**
  61. * How long is this message valid. Once the timeout has been
  62. * reached, the message must no longer be sent. If this
  63. * is a message with a 'notify' callback set, the 'notify'
  64. * function should be called with 'buf' NULL and size 0.
  65. */
  66. struct GNUNET_TIME_Absolute timeout;
  67. /**
  68. * Task triggering a timeout, can be NO_TASK if the timeout is FOREVER.
  69. */
  70. struct GNUNET_SCHEDULER_Task * timeout_task;
  71. /**
  72. * Size of 'data' -- or the desired size of 'notify' if 'data' is NULL.
  73. */
  74. size_t size;
  75. };
  76. union CadetInfoCB {
  77. /**
  78. * Channel callback.
  79. */
  80. GNUNET_CADET_ChannelCB channel_cb;
  81. /**
  82. * Monitor callback
  83. */
  84. GNUNET_CADET_PeersCB peers_cb;
  85. /**
  86. * Monitor callback
  87. */
  88. GNUNET_CADET_PeerCB peer_cb;
  89. /**
  90. * Monitor callback
  91. */
  92. GNUNET_CADET_TunnelsCB tunnels_cb;
  93. /**
  94. * Tunnel callback.
  95. */
  96. GNUNET_CADET_TunnelCB tunnel_cb;
  97. };
  98. /**
  99. * Opaque handle to the service.
  100. */
  101. struct GNUNET_CADET_Handle
  102. {
  103. /**
  104. * Handle to the server connection, to send messages later
  105. */
  106. struct GNUNET_CLIENT_Connection *client;
  107. /**
  108. * Set of handlers used for processing incoming messages in the channels
  109. */
  110. const struct GNUNET_CADET_MessageHandler *message_handlers;
  111. /**
  112. * Number of handlers in the handlers array.
  113. */
  114. unsigned int n_handlers;
  115. /**
  116. * Ports open.
  117. */
  118. const uint32_t *ports;
  119. /**
  120. * Number of ports.
  121. */
  122. unsigned int n_ports;
  123. /**
  124. * Double linked list of the channels this client is connected to, head.
  125. */
  126. struct GNUNET_CADET_Channel *channels_head;
  127. /**
  128. * Double linked list of the channels this client is connected to, tail.
  129. */
  130. struct GNUNET_CADET_Channel *channels_tail;
  131. /**
  132. * Callback for inbound channel creation
  133. */
  134. GNUNET_CADET_InboundChannelNotificationHandler *new_channel;
  135. /**
  136. * Callback for inbound channel disconnection
  137. */
  138. GNUNET_CADET_ChannelEndHandler *cleaner;
  139. /**
  140. * Handle to cancel pending transmissions in case of disconnection
  141. */
  142. struct GNUNET_CLIENT_TransmitHandle *th;
  143. /**
  144. * Closure for all the handlers given by the client
  145. */
  146. void *cls;
  147. /**
  148. * Messages to send to the service, head.
  149. */
  150. struct GNUNET_CADET_TransmitHandle *th_head;
  151. /**
  152. * Messages to send to the service, tail.
  153. */
  154. struct GNUNET_CADET_TransmitHandle *th_tail;
  155. /**
  156. * chid of the next channel to create (to avoid reusing IDs often)
  157. */
  158. CADET_ChannelNumber next_chid;
  159. /**
  160. * Have we started the task to receive messages from the service
  161. * yet? We do this after we send the 'CADET_LOCAL_CONNECT' message.
  162. */
  163. int in_receive;
  164. /**
  165. * Configuration given by the client, in case of reconnection
  166. */
  167. const struct GNUNET_CONFIGURATION_Handle *cfg;
  168. /**
  169. * Time to the next reconnect in case one reconnect fails
  170. */
  171. struct GNUNET_TIME_Relative reconnect_time;
  172. /**
  173. * Task for trying to reconnect.
  174. */
  175. struct GNUNET_SCHEDULER_Task * reconnect_task;
  176. /**
  177. * Callback for an info task (only one active at a time).
  178. */
  179. union CadetInfoCB info_cb;
  180. /**
  181. * Info callback closure for @c info_cb.
  182. */
  183. void *info_cls;
  184. };
  185. /**
  186. * Description of a peer
  187. */
  188. struct GNUNET_CADET_Peer
  189. {
  190. /**
  191. * ID of the peer in short form
  192. */
  193. GNUNET_PEER_Id id;
  194. /**
  195. * Channel this peer belongs to
  196. */
  197. struct GNUNET_CADET_Channel *t;
  198. };
  199. /**
  200. * Opaque handle to a channel.
  201. */
  202. struct GNUNET_CADET_Channel
  203. {
  204. /**
  205. * DLL next
  206. */
  207. struct GNUNET_CADET_Channel *next;
  208. /**
  209. * DLL prev
  210. */
  211. struct GNUNET_CADET_Channel *prev;
  212. /**
  213. * Handle to the cadet this channel belongs to
  214. */
  215. struct GNUNET_CADET_Handle *cadet;
  216. /**
  217. * Local ID of the channel
  218. */
  219. CADET_ChannelNumber chid;
  220. /**
  221. * Port number.
  222. */
  223. uint32_t port;
  224. /**
  225. * Other end of the channel.
  226. */
  227. GNUNET_PEER_Id peer;
  228. /**
  229. * Any data the caller wants to put in here
  230. */
  231. void *ctx;
  232. /**
  233. * Size of packet queued in this channel
  234. */
  235. unsigned int packet_size;
  236. /**
  237. * Channel options: reliability, etc.
  238. */
  239. enum GNUNET_CADET_ChannelOption options;
  240. /**
  241. * Are we allowed to send to the service?
  242. */
  243. int allow_send;
  244. };
  245. /**
  246. * Implementation state for cadet's message queue.
  247. */
  248. struct CadetMQState
  249. {
  250. /**
  251. * The current transmit handle, or NULL
  252. * if no transmit is active.
  253. */
  254. struct GNUNET_CADET_TransmitHandle *th;
  255. /**
  256. * Channel to send the data over.
  257. */
  258. struct GNUNET_CADET_Channel *channel;
  259. };
  260. /******************************************************************************/
  261. /*********************** DECLARATIONS *************************/
  262. /******************************************************************************/
  263. /**
  264. * Function called to send a message to the service.
  265. * "buf" will be NULL and "size" zero if the socket was closed for writing in
  266. * the meantime.
  267. *
  268. * @param cls closure, the cadet handle
  269. * @param size number of bytes available in buf
  270. * @param buf where the callee should write the connect message
  271. * @return number of bytes written to buf
  272. */
  273. static size_t
  274. send_callback (void *cls, size_t size, void *buf);
  275. /******************************************************************************/
  276. /*********************** AUXILIARY FUNCTIONS *************************/
  277. /******************************************************************************/
  278. /**
  279. * Check if transmission is a payload packet.
  280. *
  281. * @param th Transmission handle.
  282. *
  283. * @return #GNUNET_YES if it is a payload packet,
  284. * #GNUNET_NO if it is a cadet management packet.
  285. */
  286. static int
  287. th_is_payload (struct GNUNET_CADET_TransmitHandle *th)
  288. {
  289. return (th->notify != NULL) ? GNUNET_YES : GNUNET_NO;
  290. }
  291. /**
  292. * Check whether there is any message ready in the queue and find the size.
  293. *
  294. * @param h Cadet handle.
  295. *
  296. * @return The size of the first ready message in the queue, including overhead.
  297. * 0 if there is none.
  298. */
  299. static size_t
  300. message_ready_size (struct GNUNET_CADET_Handle *h)
  301. {
  302. struct GNUNET_CADET_TransmitHandle *th;
  303. struct GNUNET_CADET_Channel *ch;
  304. for (th = h->th_head; NULL != th; th = th->next)
  305. {
  306. ch = th->channel;
  307. if (GNUNET_NO == th_is_payload (th) || GNUNET_YES == ch->allow_send)
  308. return th->size;
  309. }
  310. return 0;
  311. }
  312. /**
  313. * Get the channel handler for the channel specified by id from the given handle
  314. * @param h Cadet handle
  315. * @param chid ID of the wanted channel
  316. * @return handle to the required channel or NULL if not found
  317. */
  318. static struct GNUNET_CADET_Channel *
  319. retrieve_channel (struct GNUNET_CADET_Handle *h, CADET_ChannelNumber chid)
  320. {
  321. struct GNUNET_CADET_Channel *ch;
  322. ch = h->channels_head;
  323. while (ch != NULL)
  324. {
  325. if (ch->chid == chid)
  326. return ch;
  327. ch = ch->next;
  328. }
  329. return NULL;
  330. }
  331. /**
  332. * Create a new channel and insert it in the channel list of the cadet handle
  333. *
  334. * @param h Cadet handle
  335. * @param chid Desired chid of the channel, 0 to assign one automatically.
  336. *
  337. * @return Handle to the created channel.
  338. */
  339. static struct GNUNET_CADET_Channel *
  340. create_channel (struct GNUNET_CADET_Handle *h, CADET_ChannelNumber chid)
  341. {
  342. struct GNUNET_CADET_Channel *ch;
  343. ch = GNUNET_new (struct GNUNET_CADET_Channel);
  344. GNUNET_CONTAINER_DLL_insert (h->channels_head, h->channels_tail, ch);
  345. ch->cadet = h;
  346. if (0 == chid)
  347. {
  348. ch->chid = h->next_chid;
  349. while (NULL != retrieve_channel (h, h->next_chid))
  350. {
  351. h->next_chid++;
  352. h->next_chid &= ~GNUNET_CADET_LOCAL_CHANNEL_ID_SERV;
  353. h->next_chid |= GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
  354. }
  355. }
  356. else
  357. {
  358. ch->chid = chid;
  359. }
  360. ch->allow_send = GNUNET_NO;
  361. return ch;
  362. }
  363. /**
  364. * Destroy the specified channel.
  365. * - Destroys all peers, calling the disconnect callback on each if needed
  366. * - Cancels all outgoing traffic for that channel, calling respective notifys
  367. * - Calls cleaner if channel was inbound
  368. * - Frees all memory used
  369. *
  370. * @param ch Pointer to the channel.
  371. * @param call_cleaner Whether to call the cleaner handler.
  372. *
  373. * @return Handle to the required channel or NULL if not found.
  374. */
  375. static void
  376. destroy_channel (struct GNUNET_CADET_Channel *ch, int call_cleaner)
  377. {
  378. struct GNUNET_CADET_Handle *h;
  379. struct GNUNET_CADET_TransmitHandle *th;
  380. struct GNUNET_CADET_TransmitHandle *next;
  381. LOG (GNUNET_ERROR_TYPE_DEBUG, " destroy_channel %X\n", ch->chid);
  382. if (NULL == ch)
  383. {
  384. GNUNET_break (0);
  385. return;
  386. }
  387. h = ch->cadet;
  388. GNUNET_CONTAINER_DLL_remove (h->channels_head, h->channels_tail, ch);
  389. /* signal channel destruction */
  390. if ( (NULL != h->cleaner) && (0 != ch->peer) && (GNUNET_YES == call_cleaner) )
  391. {
  392. LOG (GNUNET_ERROR_TYPE_DEBUG, " calling cleaner\n");
  393. h->cleaner (h->cls, ch, ch->ctx);
  394. }
  395. /* check that clients did not leave messages behind in the queue */
  396. for (th = h->th_head; NULL != th; th = next)
  397. {
  398. next = th->next;
  399. if (th->channel != ch)
  400. continue;
  401. /* Clients should have aborted their requests already.
  402. * Management traffic should be ok, as clients can't cancel that.
  403. * If the service crashed and we are reconnecting, it's ok.
  404. */
  405. GNUNET_break (GNUNET_NO == th_is_payload (th)
  406. || GNUNET_NO == h->in_receive);
  407. GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
  408. /* clean up request */
  409. if (NULL != th->timeout_task)
  410. GNUNET_SCHEDULER_cancel (th->timeout_task);
  411. GNUNET_free (th);
  412. }
  413. /* if there are no more pending requests with cadet service, cancel active request */
  414. /* Note: this should be unnecessary... */
  415. if ((0 == message_ready_size (h)) && (NULL != h->th))
  416. {
  417. GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
  418. h->th = NULL;
  419. }
  420. if (0 != ch->peer)
  421. GNUNET_PEER_change_rc (ch->peer, -1);
  422. GNUNET_free (ch);
  423. return;
  424. }
  425. /**
  426. * Notify client that the transmission has timed out
  427. *
  428. * @param cls closure
  429. * @param tc task context
  430. */
  431. static void
  432. timeout_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  433. {
  434. struct GNUNET_CADET_TransmitHandle *th = cls;
  435. struct GNUNET_CADET_Handle *cadet = th->channel->cadet;
  436. th->timeout_task = NULL;
  437. th->channel->packet_size = 0;
  438. GNUNET_CONTAINER_DLL_remove (cadet->th_head, cadet->th_tail, th);
  439. if (GNUNET_YES == th_is_payload (th))
  440. GNUNET_break (0 == th->notify (th->notify_cls, 0, NULL));
  441. GNUNET_free (th);
  442. if ((0 == message_ready_size (cadet)) && (NULL != cadet->th))
  443. {
  444. /* nothing ready to transmit, no point in asking for transmission */
  445. GNUNET_CLIENT_notify_transmit_ready_cancel (cadet->th);
  446. cadet->th = NULL;
  447. }
  448. }
  449. /**
  450. * Add a transmit handle to the transmission queue and set the
  451. * timeout if needed.
  452. *
  453. * @param h cadet handle with the queue head and tail
  454. * @param th handle to the packet to be transmitted
  455. */
  456. static void
  457. add_to_queue (struct GNUNET_CADET_Handle *h,
  458. struct GNUNET_CADET_TransmitHandle *th)
  459. {
  460. GNUNET_CONTAINER_DLL_insert_tail (h->th_head, h->th_tail, th);
  461. if (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us == th->timeout.abs_value_us)
  462. return;
  463. th->timeout_task =
  464. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
  465. (th->timeout), &timeout_transmission, th);
  466. }
  467. /**
  468. * Auxiliary function to send an already constructed packet to the service.
  469. * Takes care of creating a new queue element, copying the message and
  470. * calling the tmt_rdy function if necessary.
  471. *
  472. * @param h cadet handle
  473. * @param msg message to transmit
  474. * @param channel channel this send is related to (NULL if N/A)
  475. */
  476. static void
  477. send_packet (struct GNUNET_CADET_Handle *h,
  478. const struct GNUNET_MessageHeader *msg,
  479. struct GNUNET_CADET_Channel *channel);
  480. /**
  481. * Send an ack on the channel to confirm the processing of a message.
  482. *
  483. * @param ch Channel on which to send the ACK.
  484. */
  485. static void
  486. send_ack (struct GNUNET_CADET_Channel *ch)
  487. {
  488. struct GNUNET_CADET_LocalAck msg;
  489. LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK on channel %X\n", ch->chid);
  490. msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK);
  491. msg.header.size = htons (sizeof (msg));
  492. msg.channel_id = htonl (ch->chid);
  493. send_packet (ch->cadet, &msg.header, ch);
  494. return;
  495. }
  496. /**
  497. * Reconnect callback: tries to reconnect again after a failer previous
  498. * reconnecttion
  499. * @param cls closure (cadet handle)
  500. * @param tc task context
  501. */
  502. static void
  503. reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
  504. /**
  505. * Send a connect packet to the service with the applications and types
  506. * requested by the user.
  507. *
  508. * @param h The cadet handle.
  509. *
  510. */
  511. static void
  512. send_connect (struct GNUNET_CADET_Handle *h)
  513. {
  514. size_t size;
  515. size = sizeof (struct GNUNET_CADET_ClientConnect);
  516. size += h->n_ports * sizeof (uint32_t);
  517. {
  518. char buf[size] GNUNET_ALIGN;
  519. struct GNUNET_CADET_ClientConnect *msg;
  520. uint32_t *ports;
  521. uint16_t i;
  522. /* build connection packet */
  523. msg = (struct GNUNET_CADET_ClientConnect *) buf;
  524. msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT);
  525. msg->header.size = htons (size);
  526. ports = (uint32_t *) &msg[1];
  527. for (i = 0; i < h->n_ports; i++)
  528. {
  529. ports[i] = htonl (h->ports[i]);
  530. LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n",
  531. h->ports[i]);
  532. }
  533. LOG (GNUNET_ERROR_TYPE_DEBUG,
  534. "Sending %lu bytes long message with %u ports\n",
  535. ntohs (msg->header.size), h->n_ports);
  536. send_packet (h, &msg->header, NULL);
  537. }
  538. }
  539. /**
  540. * Reconnect to the service, retransmit all infomation to try to restore the
  541. * original state.
  542. *
  543. * @param h handle to the cadet
  544. *
  545. * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
  546. */
  547. static int
  548. do_reconnect (struct GNUNET_CADET_Handle *h)
  549. {
  550. LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
  551. LOG (GNUNET_ERROR_TYPE_DEBUG, "******* RECONNECT *******\n");
  552. LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
  553. LOG (GNUNET_ERROR_TYPE_DEBUG, "******** on %p *******\n", h);
  554. LOG (GNUNET_ERROR_TYPE_DEBUG, "*****************************\n");
  555. /* disconnect */
  556. if (NULL != h->th)
  557. {
  558. GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
  559. h->th = NULL;
  560. }
  561. if (NULL != h->client)
  562. {
  563. GNUNET_CLIENT_disconnect (h->client);
  564. }
  565. /* connect again */
  566. h->client = GNUNET_CLIENT_connect ("cadet", h->cfg);
  567. if (h->client == NULL)
  568. {
  569. h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
  570. &reconnect_cbk, h);
  571. h->reconnect_time =
  572. GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
  573. GNUNET_TIME_relative_multiply
  574. (h->reconnect_time, 2));
  575. LOG (GNUNET_ERROR_TYPE_DEBUG, "Next retry in %s\n",
  576. GNUNET_STRINGS_relative_time_to_string (h->reconnect_time,
  577. GNUNET_NO));
  578. GNUNET_break (0);
  579. return GNUNET_NO;
  580. }
  581. else
  582. {
  583. h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
  584. }
  585. send_connect (h);
  586. return GNUNET_YES;
  587. }
  588. /**
  589. * Reconnect callback: tries to reconnect again after a failer previous
  590. * reconnecttion
  591. * @param cls closure (cadet handle)
  592. * @param tc task context
  593. */
  594. static void
  595. reconnect_cbk (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  596. {
  597. struct GNUNET_CADET_Handle *h = cls;
  598. h->reconnect_task = NULL;
  599. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
  600. return;
  601. do_reconnect (h);
  602. }
  603. /**
  604. * Reconnect to the service, retransmit all infomation to try to restore the
  605. * original state.
  606. *
  607. * @param h handle to the cadet
  608. *
  609. * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
  610. */
  611. static void
  612. reconnect (struct GNUNET_CADET_Handle *h)
  613. {
  614. struct GNUNET_CADET_Channel *ch;
  615. struct GNUNET_CADET_Channel *next;
  616. LOG (GNUNET_ERROR_TYPE_DEBUG,
  617. "Requested RECONNECT, destroying all channels\n");
  618. h->in_receive = GNUNET_NO;
  619. for (ch = h->channels_head; NULL != ch; ch = next)
  620. {
  621. next = ch->next;
  622. destroy_channel (ch, GNUNET_YES);
  623. }
  624. if (NULL == h->reconnect_task)
  625. h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_time,
  626. &reconnect_cbk, h);
  627. }
  628. /******************************************************************************/
  629. /*********************** RECEIVE HANDLERS ****************************/
  630. /******************************************************************************/
  631. /**
  632. * Process the new channel notification and add it to the channels in the handle
  633. *
  634. * @param h The cadet handle
  635. * @param msg A message with the details of the new incoming channel
  636. */
  637. static void
  638. process_channel_created (struct GNUNET_CADET_Handle *h,
  639. const struct GNUNET_CADET_ChannelMessage *msg)
  640. {
  641. struct GNUNET_CADET_Channel *ch;
  642. CADET_ChannelNumber chid;
  643. uint32_t port;
  644. chid = ntohl (msg->channel_id);
  645. port = ntohl (msg->port);
  646. LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming channel %X:%u\n", chid, port);
  647. if (chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
  648. {
  649. GNUNET_break (0);
  650. return;
  651. }
  652. if (NULL != h->new_channel)
  653. {
  654. void *ctx;
  655. ch = create_channel (h, chid);
  656. ch->allow_send = GNUNET_NO;
  657. ch->peer = GNUNET_PEER_intern (&msg->peer);
  658. ch->cadet = h;
  659. ch->chid = chid;
  660. ch->port = port;
  661. ch->options = ntohl (msg->opt);
  662. LOG (GNUNET_ERROR_TYPE_DEBUG, " created channel %p\n", ch);
  663. ctx = h->new_channel (h->cls, ch, &msg->peer, ch->port, ch->options);
  664. if (NULL != ctx)
  665. ch->ctx = ctx;
  666. LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n");
  667. }
  668. else
  669. {
  670. struct GNUNET_CADET_ChannelMessage d_msg;
  671. LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n");
  672. d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
  673. d_msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage));
  674. d_msg.channel_id = msg->channel_id;
  675. memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
  676. d_msg.port = 0;
  677. d_msg.opt = 0;
  678. send_packet (h, &d_msg.header, NULL);
  679. }
  680. return;
  681. }
  682. /**
  683. * Process the channel destroy notification and free associated resources
  684. *
  685. * @param h The cadet handle
  686. * @param msg A message with the details of the channel being destroyed
  687. */
  688. static void
  689. process_channel_destroy (struct GNUNET_CADET_Handle *h,
  690. const struct GNUNET_CADET_ChannelMessage *msg)
  691. {
  692. struct GNUNET_CADET_Channel *ch;
  693. CADET_ChannelNumber chid;
  694. LOG (GNUNET_ERROR_TYPE_DEBUG, "Channel Destroy received from service\n");
  695. chid = ntohl (msg->channel_id);
  696. ch = retrieve_channel (h, chid);
  697. if (NULL == ch)
  698. {
  699. LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X unknown\n", chid);
  700. return;
  701. }
  702. LOG (GNUNET_ERROR_TYPE_DEBUG, " destroying channel %X\n", ch->chid);
  703. destroy_channel (ch, GNUNET_YES);
  704. }
  705. /**
  706. * Process the incoming data packets, call appropriate handlers.
  707. *
  708. * @param h The cadet handle
  709. * @param message A message encapsulating the data
  710. */
  711. static void
  712. process_incoming_data (struct GNUNET_CADET_Handle *h,
  713. const struct GNUNET_MessageHeader *message)
  714. {
  715. const struct GNUNET_MessageHeader *payload;
  716. const struct GNUNET_CADET_MessageHandler *handler;
  717. struct GNUNET_CADET_LocalData *dmsg;
  718. struct GNUNET_CADET_Channel *ch;
  719. size_t size;
  720. unsigned int i;
  721. uint16_t type;
  722. LOG (GNUNET_ERROR_TYPE_DEBUG, "Got a data message!\n");
  723. dmsg = (struct GNUNET_CADET_LocalData *) message;
  724. ch = retrieve_channel (h, ntohl (dmsg->id));
  725. if (NULL == ch)
  726. {
  727. GNUNET_break (0);
  728. return;
  729. }
  730. payload = (struct GNUNET_MessageHeader *) &dmsg[1];
  731. LOG (GNUNET_ERROR_TYPE_DEBUG, " %s data on channel %s [%X]\n",
  732. GC_f2s (ch->chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV),
  733. GNUNET_i2s (GNUNET_PEER_resolve2 (ch->peer)), ntohl (dmsg->id));
  734. size = ntohs (message->size);
  735. LOG (GNUNET_ERROR_TYPE_DEBUG, " %u bytes\n", size);
  736. type = ntohs (payload->type);
  737. size = ntohs (payload->size);
  738. LOG (GNUNET_ERROR_TYPE_DEBUG, " payload type %s\n", GC_m2s (type));
  739. for (i = 0; i < h->n_handlers; i++)
  740. {
  741. handler = &h->message_handlers[i];
  742. LOG (GNUNET_ERROR_TYPE_DEBUG, " checking handler for type %u\n",
  743. handler->type);
  744. if (handler->type == type)
  745. {
  746. if (GNUNET_OK !=
  747. handler->callback (h->cls, ch, &ch->ctx, payload))
  748. {
  749. LOG (GNUNET_ERROR_TYPE_DEBUG, "callback caused disconnection\n");
  750. GNUNET_CADET_channel_destroy (ch);
  751. return;
  752. }
  753. else
  754. {
  755. LOG (GNUNET_ERROR_TYPE_DEBUG,
  756. "callback completed successfully\n");
  757. return;
  758. }
  759. }
  760. }
  761. }
  762. /**
  763. * Process a local ACK message, enabling the client to send
  764. * more data to the service.
  765. *
  766. * @param h Cadet handle.
  767. * @param message Message itself.
  768. */
  769. static void
  770. process_ack (struct GNUNET_CADET_Handle *h,
  771. const struct GNUNET_MessageHeader *message)
  772. {
  773. struct GNUNET_CADET_LocalAck *msg;
  774. struct GNUNET_CADET_Channel *ch;
  775. CADET_ChannelNumber chid;
  776. LOG (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK!\n");
  777. msg = (struct GNUNET_CADET_LocalAck *) message;
  778. chid = ntohl (msg->channel_id);
  779. ch = retrieve_channel (h, chid);
  780. if (NULL == ch)
  781. {
  782. LOG (GNUNET_ERROR_TYPE_DEBUG, "ACK on unknown channel %X\n", chid);
  783. return;
  784. }
  785. LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X!\n", ch->chid);
  786. ch->allow_send = GNUNET_YES;
  787. if (NULL == h->th && 0 < ch->packet_size)
  788. {
  789. LOG (GNUNET_ERROR_TYPE_DEBUG, " tmt rdy was NULL, requesting!\n");
  790. h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, ch->packet_size,
  791. GNUNET_TIME_UNIT_FOREVER_REL,
  792. GNUNET_YES, &send_callback, h);
  793. }
  794. }
  795. /*
  796. * Process a local reply about info on all channels, pass info to the user.
  797. *
  798. * @param h Cadet handle.
  799. * @param message Message itself.
  800. */
  801. // static void
  802. // process_get_channels (struct GNUNET_CADET_Handle *h,
  803. // const struct GNUNET_MessageHeader *message)
  804. // {
  805. // struct GNUNET_CADET_LocalInfo *msg;
  806. //
  807. // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Get Channels messasge received\n");
  808. //
  809. // if (NULL == h->channels_cb)
  810. // {
  811. // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
  812. // return;
  813. // }
  814. //
  815. // msg = (struct GNUNET_CADET_LocalInfo *) message;
  816. // if (ntohs (message->size) !=
  817. // (sizeof (struct GNUNET_CADET_LocalInfo) +
  818. // sizeof (struct GNUNET_PeerIdentity)))
  819. // {
  820. // GNUNET_break_op (0);
  821. // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  822. // "Get channels message: size %hu - expected %u\n",
  823. // ntohs (message->size),
  824. // sizeof (struct GNUNET_CADET_LocalInfo));
  825. // return;
  826. // }
  827. // h->channels_cb (h->channels_cls,
  828. // ntohl (msg->channel_id),
  829. // &msg->owner,
  830. // &msg->destination);
  831. // }
  832. /*
  833. * Process a local monitor_channel reply, pass info to the user.
  834. *
  835. * @param h Cadet handle.
  836. * @param message Message itself.
  837. */
  838. // static void
  839. // process_show_channel (struct GNUNET_CADET_Handle *h,
  840. // const struct GNUNET_MessageHeader *message)
  841. // {
  842. // struct GNUNET_CADET_LocalInfo *msg;
  843. // size_t esize;
  844. //
  845. // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Show Channel messasge received\n");
  846. //
  847. // if (NULL == h->channel_cb)
  848. // {
  849. // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " ignored\n");
  850. // return;
  851. // }
  852. //
  853. // /* Verify message sanity */
  854. // msg = (struct GNUNET_CADET_LocalInfo *) message;
  855. // esize = sizeof (struct GNUNET_CADET_LocalInfo);
  856. // if (ntohs (message->size) != esize)
  857. // {
  858. // GNUNET_break_op (0);
  859. // GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  860. // "Show channel message: size %hu - expected %u\n",
  861. // ntohs (message->size),
  862. // esize);
  863. //
  864. // h->channel_cb (h->channel_cls, NULL, NULL);
  865. // h->channel_cb = NULL;
  866. // h->channel_cls = NULL;
  867. //
  868. // return;
  869. // }
  870. //
  871. // h->channel_cb (h->channel_cls,
  872. // &msg->destination,
  873. // &msg->owner);
  874. // }
  875. /**
  876. * Process a local reply about info on all tunnels, pass info to the user.
  877. *
  878. * @param h Cadet handle.
  879. * @param message Message itself.
  880. */
  881. static void
  882. process_get_peers (struct GNUNET_CADET_Handle *h,
  883. const struct GNUNET_MessageHeader *message)
  884. {
  885. struct GNUNET_CADET_LocalInfoPeer *msg;
  886. uint16_t size;
  887. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Peer messasge received\n");
  888. if (NULL == h->info_cb.peers_cb)
  889. {
  890. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
  891. return;
  892. }
  893. size = ntohs (message->size);
  894. if (sizeof (struct GNUNET_CADET_LocalInfoPeer) > size)
  895. {
  896. h->info_cb.peers_cb (h->info_cls, NULL, -1, 0, 0);
  897. h->info_cb.peers_cb = NULL;
  898. h->info_cls = NULL;
  899. return;
  900. }
  901. msg = (struct GNUNET_CADET_LocalInfoPeer *) message;
  902. h->info_cb.peers_cb (h->info_cls, &msg->destination,
  903. (int) ntohs (msg->tunnel),
  904. (unsigned int ) ntohs (msg->paths),
  905. 0);
  906. }
  907. /**
  908. * Process a local peer info reply, pass info to the user.
  909. *
  910. * @param h Cadet handle.
  911. * @param message Message itself.
  912. */
  913. static void
  914. process_get_peer (struct GNUNET_CADET_Handle *h,
  915. const struct GNUNET_MessageHeader *message)
  916. {
  917. struct GNUNET_CADET_LocalInfoTunnel *msg;
  918. size_t esize;
  919. size_t msize;
  920. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnel messasge received\n");
  921. if (NULL == h->info_cb.peer_cb)
  922. {
  923. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
  924. return;
  925. }
  926. /* Verify message sanity */
  927. msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
  928. msize = ntohs (message->size);
  929. esize = sizeof (struct GNUNET_CADET_LocalInfoPeer);
  930. if (esize > msize)
  931. {
  932. GNUNET_break_op (0);
  933. h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
  934. goto clean_cls;
  935. }
  936. // esize += ch_n * sizeof (CADET_ChannelNumber);
  937. // esize += c_n * sizeof (struct GNUNET_CADET_Hash);
  938. if (msize != esize)
  939. {
  940. GNUNET_break_op (0);
  941. GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "m:%u, e: %u\n", msize, esize);
  942. h->info_cb.peer_cb (h->info_cls, NULL, 0, 0, 0, NULL);
  943. goto clean_cls;
  944. }
  945. /* Call Callback with tunnel info. */
  946. h->info_cb.peer_cb (h->info_cls, &msg->destination, 0, 0, 0, NULL);
  947. clean_cls:
  948. h->info_cb.peer_cb = NULL;
  949. h->info_cls = NULL;
  950. }
  951. /**
  952. * Process a local reply about info on all tunnels, pass info to the user.
  953. *
  954. * @param h Cadet handle.
  955. * @param message Message itself.
  956. */
  957. static void
  958. process_get_tunnels (struct GNUNET_CADET_Handle *h,
  959. const struct GNUNET_MessageHeader *message)
  960. {
  961. struct GNUNET_CADET_LocalInfoTunnel *msg;
  962. uint16_t size;
  963. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnels messasge received\n");
  964. if (NULL == h->info_cb.tunnels_cb)
  965. {
  966. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
  967. return;
  968. }
  969. size = ntohs (message->size);
  970. if (sizeof (struct GNUNET_CADET_LocalInfoTunnel) > size)
  971. {
  972. h->info_cb.tunnels_cb (h->info_cls, NULL, 0, 0, 0, 0);
  973. h->info_cb.tunnels_cb = NULL;
  974. h->info_cls = NULL;
  975. return;
  976. }
  977. msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
  978. h->info_cb.tunnels_cb (h->info_cls, &msg->destination,
  979. ntohl (msg->channels), ntohl (msg->connections),
  980. ntohs (msg->estate), ntohs (msg->cstate));
  981. }
  982. /**
  983. * Process a local tunnel info reply, pass info to the user.
  984. *
  985. * @param h Cadet handle.
  986. * @param message Message itself.
  987. */
  988. static void
  989. process_get_tunnel (struct GNUNET_CADET_Handle *h,
  990. const struct GNUNET_MessageHeader *message)
  991. {
  992. struct GNUNET_CADET_LocalInfoTunnel *msg;
  993. size_t esize;
  994. size_t msize;
  995. unsigned int ch_n;
  996. unsigned int c_n;
  997. struct GNUNET_CADET_Hash *conns;
  998. CADET_ChannelNumber *chns;
  999. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get Tunnel messasge received\n");
  1000. if (NULL == h->info_cb.tunnel_cb)
  1001. {
  1002. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ignored\n");
  1003. return;
  1004. }
  1005. /* Verify message sanity */
  1006. msg = (struct GNUNET_CADET_LocalInfoTunnel *) message;
  1007. msize = ntohs (message->size);
  1008. esize = sizeof (struct GNUNET_CADET_LocalInfoTunnel);
  1009. if (esize > msize)
  1010. {
  1011. GNUNET_break_op (0);
  1012. h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0);
  1013. goto clean_cls;
  1014. }
  1015. ch_n = ntohl (msg->channels);
  1016. c_n = ntohl (msg->connections);
  1017. esize += ch_n * sizeof (CADET_ChannelNumber);
  1018. esize += c_n * sizeof (struct GNUNET_CADET_Hash);
  1019. if (msize != esize)
  1020. {
  1021. GNUNET_break_op (0);
  1022. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "m:%u, e: %u (%u ch, %u conn)\n",
  1023. msize, esize, ch_n, c_n);
  1024. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u (%u ch, %u conn)\n",
  1025. sizeof (struct GNUNET_CADET_LocalInfoTunnel),
  1026. sizeof (CADET_ChannelNumber), sizeof (struct GNUNET_HashCode));
  1027. h->info_cb.tunnel_cb (h->info_cls, NULL, 0, 0, NULL, NULL, 0, 0);
  1028. goto clean_cls;
  1029. }
  1030. /* Call Callback with tunnel info. */
  1031. conns = (struct GNUNET_CADET_Hash *) &msg[1];
  1032. chns = (CADET_ChannelNumber *) &conns[c_n];
  1033. h->info_cb.tunnel_cb (h->info_cls, &msg->destination,
  1034. ch_n, c_n, chns, conns,
  1035. ntohs (msg->estate), ntohs (msg->cstate));
  1036. clean_cls:
  1037. h->info_cb.tunnel_cb = NULL;
  1038. h->info_cls = NULL;
  1039. }
  1040. /**
  1041. * Function to process all messages received from the service
  1042. *
  1043. * @param cls closure
  1044. * @param msg message received, NULL on timeout or fatal error
  1045. */
  1046. static void
  1047. msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
  1048. {
  1049. struct GNUNET_CADET_Handle *h = cls;
  1050. uint16_t type;
  1051. if (msg == NULL)
  1052. {
  1053. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1054. "Cadet service disconnected, reconnecting\n", h);
  1055. reconnect (h);
  1056. return;
  1057. }
  1058. type = ntohs (msg->type);
  1059. LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
  1060. LOG (GNUNET_ERROR_TYPE_DEBUG, "Received a message: %s\n",
  1061. GC_m2s (type));
  1062. switch (type)
  1063. {
  1064. /* Notify of a new incoming channel */
  1065. case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
  1066. process_channel_created (h, (struct GNUNET_CADET_ChannelMessage *) msg);
  1067. break;
  1068. /* Notify of a channel disconnection */
  1069. case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY: /* TODO separate(gid problem)*/
  1070. case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
  1071. process_channel_destroy (h, (struct GNUNET_CADET_ChannelMessage *) msg);
  1072. break;
  1073. case GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA:
  1074. process_incoming_data (h, msg);
  1075. break;
  1076. case GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK:
  1077. process_ack (h, msg);
  1078. break;
  1079. // case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
  1080. // process_get_channels (h, msg);
  1081. // break;
  1082. // case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
  1083. // process_show_channel (h, msg);
  1084. // break;
  1085. case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
  1086. process_get_peers (h, msg);
  1087. break;
  1088. case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
  1089. process_get_peer (h, msg);
  1090. break;
  1091. case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
  1092. process_get_tunnels (h, msg);
  1093. break;
  1094. case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
  1095. process_get_tunnel (h, msg);
  1096. break;
  1097. // case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
  1098. // process_show_channel (h, msg);
  1099. // break;
  1100. default:
  1101. /* We shouldn't get any other packages, log and ignore */
  1102. LOG (GNUNET_ERROR_TYPE_WARNING,
  1103. "unsolicited message form service (type %s)\n",
  1104. GC_m2s (ntohs (msg->type)));
  1105. }
  1106. LOG (GNUNET_ERROR_TYPE_DEBUG, "message processed\n");
  1107. if (GNUNET_YES == h->in_receive)
  1108. {
  1109. GNUNET_CLIENT_receive (h->client, &msg_received, h,
  1110. GNUNET_TIME_UNIT_FOREVER_REL);
  1111. }
  1112. else
  1113. {
  1114. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1115. "in receive off, not calling CLIENT_receive\n");
  1116. }
  1117. }
  1118. /******************************************************************************/
  1119. /************************ SEND FUNCTIONS ****************************/
  1120. /******************************************************************************/
  1121. /**
  1122. * Function called to send a message to the service.
  1123. * "buf" will be NULL and "size" zero if the socket was closed for writing in
  1124. * the meantime.
  1125. *
  1126. * @param cls closure, the cadet handle
  1127. * @param size number of bytes available in buf
  1128. * @param buf where the callee should write the connect message
  1129. * @return number of bytes written to buf
  1130. */
  1131. static size_t
  1132. send_callback (void *cls, size_t size, void *buf)
  1133. {
  1134. struct GNUNET_CADET_Handle *h = cls;
  1135. struct GNUNET_CADET_TransmitHandle *th;
  1136. struct GNUNET_CADET_TransmitHandle *next;
  1137. struct GNUNET_CADET_Channel *ch;
  1138. char *cbuf = buf;
  1139. size_t tsize;
  1140. size_t psize;
  1141. size_t nsize;
  1142. LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
  1143. LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send callback, buffer %u\n", size);
  1144. if ((0 == size) || (NULL == buf))
  1145. {
  1146. LOG (GNUNET_ERROR_TYPE_DEBUG, "# Received NULL send callback on %p\n", h);
  1147. reconnect (h);
  1148. h->th = NULL;
  1149. return 0;
  1150. }
  1151. tsize = 0;
  1152. next = h->th_head;
  1153. nsize = message_ready_size (h);
  1154. while ((NULL != (th = next)) && (0 < nsize) && (size >= nsize))
  1155. {
  1156. ch = th->channel;
  1157. if (GNUNET_YES == th_is_payload (th))
  1158. {
  1159. struct GNUNET_CADET_LocalData *dmsg;
  1160. struct GNUNET_MessageHeader *mh;
  1161. LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload\n");
  1162. if (GNUNET_NO == ch->allow_send)
  1163. {
  1164. /* This channel is not ready to transmit yet, Try the next message */
  1165. next = th->next;
  1166. continue;
  1167. }
  1168. ch->packet_size = 0;
  1169. GNUNET_assert (size >= th->size);
  1170. dmsg = (struct GNUNET_CADET_LocalData *) cbuf;
  1171. mh = (struct GNUNET_MessageHeader *) &dmsg[1];
  1172. psize = th->notify (th->notify_cls, size - DATA_OVERHEAD, mh);
  1173. if (psize > 0)
  1174. {
  1175. GNUNET_assert (sizeof (struct GNUNET_MessageHeader) <= psize);
  1176. psize += DATA_OVERHEAD;
  1177. GNUNET_assert (size >= psize);
  1178. dmsg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA);
  1179. dmsg->header.size = htons (psize);
  1180. dmsg->id = htonl (ch->chid);
  1181. LOG (GNUNET_ERROR_TYPE_DEBUG, "# payload type %s\n",
  1182. GC_m2s (ntohs (mh->type)));
  1183. ch->allow_send = GNUNET_NO;
  1184. }
  1185. else
  1186. {
  1187. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1188. "# callback returned size 0, "
  1189. "application canceled transmission\n");
  1190. }
  1191. }
  1192. else
  1193. {
  1194. const struct GNUNET_MessageHeader *mh;
  1195. mh = (const struct GNUNET_MessageHeader *) &th[1];
  1196. LOG (GNUNET_ERROR_TYPE_DEBUG, "# cadet internal traffic, type %s\n",
  1197. GC_m2s (ntohs (mh->type)));
  1198. memcpy (cbuf, &th[1], th->size);
  1199. psize = th->size;
  1200. }
  1201. GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= psize);
  1202. if (th->timeout_task != NULL)
  1203. GNUNET_SCHEDULER_cancel (th->timeout_task);
  1204. next = th->next;
  1205. GNUNET_CONTAINER_DLL_remove (h->th_head, h->th_tail, th);
  1206. GNUNET_free (th);
  1207. nsize = message_ready_size (h);
  1208. cbuf += psize;
  1209. size -= psize;
  1210. tsize += psize;
  1211. }
  1212. LOG (GNUNET_ERROR_TYPE_DEBUG, "# total size: %u\n", tsize);
  1213. h->th = NULL;
  1214. size = message_ready_size (h);
  1215. if (0 != size)
  1216. {
  1217. LOG (GNUNET_ERROR_TYPE_DEBUG, "# next size: %u\n", size);
  1218. h->th =
  1219. GNUNET_CLIENT_notify_transmit_ready (h->client, size,
  1220. GNUNET_TIME_UNIT_FOREVER_REL,
  1221. GNUNET_YES, &send_callback, h);
  1222. }
  1223. else
  1224. {
  1225. if (NULL != h->th_head)
  1226. LOG (GNUNET_ERROR_TYPE_DEBUG, "# can't transmit any more\n");
  1227. else
  1228. LOG (GNUNET_ERROR_TYPE_DEBUG, "# nothing left to transmit\n");
  1229. }
  1230. if (GNUNET_NO == h->in_receive)
  1231. {
  1232. LOG (GNUNET_ERROR_TYPE_DEBUG, "# start receiving from service\n");
  1233. h->in_receive = GNUNET_YES;
  1234. GNUNET_CLIENT_receive (h->client, &msg_received, h,
  1235. GNUNET_TIME_UNIT_FOREVER_REL);
  1236. }
  1237. LOG (GNUNET_ERROR_TYPE_DEBUG, "# Send packet() END\n");
  1238. return tsize;
  1239. }
  1240. /**
  1241. * Auxiliary function to send an already constructed packet to the service.
  1242. * Takes care of creating a new queue element, copying the message and
  1243. * calling the tmt_rdy function if necessary.
  1244. *
  1245. * @param h cadet handle
  1246. * @param msg message to transmit
  1247. * @param channel channel this send is related to (NULL if N/A)
  1248. */
  1249. static void
  1250. send_packet (struct GNUNET_CADET_Handle *h,
  1251. const struct GNUNET_MessageHeader *msg,
  1252. struct GNUNET_CADET_Channel *channel)
  1253. {
  1254. struct GNUNET_CADET_TransmitHandle *th;
  1255. size_t msize;
  1256. LOG (GNUNET_ERROR_TYPE_DEBUG, " Sending message to service: %s\n",
  1257. GC_m2s(ntohs(msg->type)));
  1258. msize = ntohs (msg->size);
  1259. th = GNUNET_malloc (sizeof (struct GNUNET_CADET_TransmitHandle) + msize);
  1260. th->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
  1261. th->size = msize;
  1262. th->channel = channel;
  1263. memcpy (&th[1], msg, msize);
  1264. add_to_queue (h, th);
  1265. if (NULL != h->th)
  1266. return;
  1267. LOG (GNUNET_ERROR_TYPE_DEBUG, " calling ntfy tmt rdy for %u bytes\n", msize);
  1268. h->th =
  1269. GNUNET_CLIENT_notify_transmit_ready (h->client, msize,
  1270. GNUNET_TIME_UNIT_FOREVER_REL,
  1271. GNUNET_YES, &send_callback, h);
  1272. }
  1273. /******************************************************************************/
  1274. /********************** API CALL DEFINITIONS *************************/
  1275. /******************************************************************************/
  1276. struct GNUNET_CADET_Handle *
  1277. GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls,
  1278. GNUNET_CADET_InboundChannelNotificationHandler new_channel,
  1279. GNUNET_CADET_ChannelEndHandler cleaner,
  1280. const struct GNUNET_CADET_MessageHandler *handlers,
  1281. const uint32_t *ports)
  1282. {
  1283. struct GNUNET_CADET_Handle *h;
  1284. LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect()\n");
  1285. h = GNUNET_new (struct GNUNET_CADET_Handle);
  1286. LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h);
  1287. h->cfg = cfg;
  1288. h->new_channel = new_channel;
  1289. h->cleaner = cleaner;
  1290. h->client = GNUNET_CLIENT_connect ("cadet", cfg);
  1291. if (h->client == NULL)
  1292. {
  1293. GNUNET_break (0);
  1294. GNUNET_free (h);
  1295. return NULL;
  1296. }
  1297. h->cls = cls;
  1298. h->message_handlers = handlers;
  1299. h->ports = ports;
  1300. h->next_chid = GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
  1301. h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS;
  1302. h->reconnect_task = NULL;
  1303. if (NULL != ports && ports[0] != 0 && NULL == new_channel)
  1304. {
  1305. GNUNET_break (0);
  1306. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1307. "no new channel handler given, ports parameter is useless!!\n");
  1308. }
  1309. if ((NULL == ports || ports[0] == 0) && NULL != new_channel)
  1310. {
  1311. GNUNET_break (0);
  1312. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1313. "no ports given, new channel handler will never be called!!\n");
  1314. }
  1315. /* count handlers */
  1316. for (h->n_handlers = 0;
  1317. handlers && handlers[h->n_handlers].type;
  1318. h->n_handlers++) ;
  1319. for (h->n_ports = 0;
  1320. ports && ports[h->n_ports];
  1321. h->n_ports++) ;
  1322. send_connect (h);
  1323. LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect() END\n");
  1324. return h;
  1325. }
  1326. void
  1327. GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle)
  1328. {
  1329. struct GNUNET_CADET_Channel *ch;
  1330. struct GNUNET_CADET_Channel *aux;
  1331. struct GNUNET_CADET_TransmitHandle *th;
  1332. LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET DISCONNECT\n");
  1333. ch = handle->channels_head;
  1334. while (NULL != ch)
  1335. {
  1336. aux = ch->next;
  1337. if (ch->chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
  1338. {
  1339. GNUNET_break (0);
  1340. LOG (GNUNET_ERROR_TYPE_DEBUG, "channel %X not destroyed\n", ch->chid);
  1341. }
  1342. destroy_channel (ch, GNUNET_YES);
  1343. ch = aux;
  1344. }
  1345. while ( (th = handle->th_head) != NULL)
  1346. {
  1347. struct GNUNET_MessageHeader *msg;
  1348. /* Make sure it is an allowed packet (everything else should have been
  1349. * already canceled).
  1350. */
  1351. GNUNET_break (GNUNET_NO == th_is_payload (th));
  1352. msg = (struct GNUNET_MessageHeader *) &th[1];
  1353. switch (ntohs(msg->type))
  1354. {
  1355. case GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT:
  1356. case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
  1357. case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY:
  1358. case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS:
  1359. case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL:
  1360. case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER:
  1361. case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS:
  1362. case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL:
  1363. case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS:
  1364. break;
  1365. default:
  1366. GNUNET_break (0);
  1367. LOG (GNUNET_ERROR_TYPE_ERROR, "unexpected msg %u\n",
  1368. ntohs(msg->type));
  1369. }
  1370. GNUNET_CONTAINER_DLL_remove (handle->th_head, handle->th_tail, th);
  1371. GNUNET_free (th);
  1372. }
  1373. if (NULL != handle->th)
  1374. {
  1375. GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
  1376. handle->th = NULL;
  1377. }
  1378. if (NULL != handle->client)
  1379. {
  1380. GNUNET_CLIENT_disconnect (handle->client);
  1381. handle->client = NULL;
  1382. }
  1383. if (NULL != handle->reconnect_task)
  1384. {
  1385. GNUNET_SCHEDULER_cancel(handle->reconnect_task);
  1386. handle->reconnect_task = NULL;
  1387. }
  1388. GNUNET_free (handle);
  1389. }
  1390. /**
  1391. * Create a new channel towards a remote peer.
  1392. *
  1393. * If the destination port is not open by any peer or the destination peer
  1394. * does not accept the channel, #GNUNET_CADET_ChannelEndHandler will be called
  1395. * for this channel.
  1396. *
  1397. * @param h cadet handle
  1398. * @param channel_ctx client's channel context to associate with the channel
  1399. * @param peer peer identity the channel should go to
  1400. * @param port Port number.
  1401. * @param options CadetOption flag field, with all desired option bits set to 1.
  1402. *
  1403. * @return handle to the channel
  1404. */
  1405. struct GNUNET_CADET_Channel *
  1406. GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h,
  1407. void *channel_ctx,
  1408. const struct GNUNET_PeerIdentity *peer,
  1409. uint32_t port,
  1410. enum GNUNET_CADET_ChannelOption options)
  1411. {
  1412. struct GNUNET_CADET_Channel *ch;
  1413. struct GNUNET_CADET_ChannelMessage msg;
  1414. LOG (GNUNET_ERROR_TYPE_DEBUG,
  1415. "Creating new channel to %s:%u\n",
  1416. GNUNET_i2s (peer), port);
  1417. ch = create_channel (h, 0);
  1418. LOG (GNUNET_ERROR_TYPE_DEBUG, " at %p\n", ch);
  1419. LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", ch->chid);
  1420. ch->ctx = channel_ctx;
  1421. ch->peer = GNUNET_PEER_intern (peer);
  1422. msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE);
  1423. msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage));
  1424. msg.channel_id = htonl (ch->chid);
  1425. msg.port = htonl (port);
  1426. msg.peer = *peer;
  1427. msg.opt = htonl (options);
  1428. ch->allow_send = GNUNET_NO;
  1429. send_packet (h, &msg.header, ch);
  1430. return ch;
  1431. }
  1432. void
  1433. GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel)
  1434. {
  1435. struct GNUNET_CADET_Handle *h;
  1436. struct GNUNET_CADET_ChannelMessage msg;
  1437. struct GNUNET_CADET_TransmitHandle *th;
  1438. LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n");
  1439. h = channel->cadet;
  1440. msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
  1441. msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage));
  1442. msg.channel_id = htonl (channel->chid);
  1443. memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
  1444. msg.port = 0;
  1445. msg.opt = 0;
  1446. th = h->th_head;
  1447. while (th != NULL)
  1448. {
  1449. struct GNUNET_CADET_TransmitHandle *aux;
  1450. if (th->channel == channel)
  1451. {
  1452. aux = th->next;
  1453. if (GNUNET_YES == th_is_payload (th))
  1454. {
  1455. /* applications should cancel before destroying channel */
  1456. GNUNET_break (0);
  1457. th->notify (th->notify_cls, 0, NULL);
  1458. }
  1459. GNUNET_CADET_notify_transmit_ready_cancel (th);
  1460. th = aux;
  1461. }
  1462. else
  1463. th = th->next;
  1464. }
  1465. destroy_channel (channel, GNUNET_YES);
  1466. send_packet (h, &msg.header, NULL);
  1467. }
  1468. /**
  1469. * Get information about a channel.
  1470. *
  1471. * @param channel Channel handle.
  1472. * @param option Query (GNUNET_CADET_OPTION_*).
  1473. * @param ... dependant on option, currently not used
  1474. *
  1475. * @return Union with an answer to the query.
  1476. */
  1477. const union GNUNET_CADET_ChannelInfo *
  1478. GNUNET_CADET_channel_get_info (struct GNUNET_CADET_Channel *channel,
  1479. enum GNUNET_CADET_ChannelOption option, ...)
  1480. {
  1481. static int bool_flag;
  1482. const union GNUNET_CADET_ChannelInfo *ret;
  1483. switch (option)
  1484. {
  1485. case GNUNET_CADET_OPTION_NOBUFFER:
  1486. case GNUNET_CADET_OPTION_RELIABLE:
  1487. case GNUNET_CADET_OPTION_OOORDER:
  1488. if (0 != (option & channel->options))
  1489. bool_flag = GNUNET_YES;
  1490. else
  1491. bool_flag = GNUNET_NO;
  1492. ret = (const union GNUNET_CADET_ChannelInfo *) &bool_flag;
  1493. break;
  1494. case GNUNET_CADET_OPTION_PEER:
  1495. ret = (const union GNUNET_CADET_ChannelInfo *) GNUNET_PEER_resolve2 (channel->peer);
  1496. break;
  1497. default:
  1498. GNUNET_break (0);
  1499. return NULL;
  1500. }
  1501. return ret;
  1502. }
  1503. struct GNUNET_CADET_TransmitHandle *
  1504. GNUNET_CADET_notify_transmit_ready (struct GNUNET_CADET_Channel *channel, int cork,
  1505. struct GNUNET_TIME_Relative maxdelay,
  1506. size_t notify_size,
  1507. GNUNET_CONNECTION_TransmitReadyNotify notify,
  1508. void *notify_cls)
  1509. {
  1510. struct GNUNET_CADET_TransmitHandle *th;
  1511. GNUNET_assert (NULL != channel);
  1512. GNUNET_assert (GNUNET_CONSTANTS_MAX_CADET_MESSAGE_SIZE >= notify_size);
  1513. LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY\n");
  1514. LOG (GNUNET_ERROR_TYPE_DEBUG, " on channel %X\n", channel->chid);
  1515. LOG (GNUNET_ERROR_TYPE_DEBUG, " allow_send %d\n", channel->allow_send);
  1516. if (channel->chid >= GNUNET_CADET_LOCAL_CHANNEL_ID_SERV)
  1517. LOG (GNUNET_ERROR_TYPE_DEBUG, " to origin\n");
  1518. else
  1519. LOG (GNUNET_ERROR_TYPE_DEBUG, " to destination\n");
  1520. LOG (GNUNET_ERROR_TYPE_DEBUG, " payload size %u\n", notify_size);
  1521. GNUNET_assert (NULL != notify);
  1522. GNUNET_assert (0 == channel->packet_size); // Only one data packet allowed
  1523. th = GNUNET_new (struct GNUNET_CADET_TransmitHandle);
  1524. th->channel = channel;
  1525. th->timeout = GNUNET_TIME_relative_to_absolute (maxdelay);
  1526. th->size = notify_size + DATA_OVERHEAD;
  1527. channel->packet_size = th->size;
  1528. LOG (GNUNET_ERROR_TYPE_DEBUG, " total size %u\n", th->size);
  1529. th->notify = notify;
  1530. th->notify_cls = notify_cls;
  1531. add_to_queue (channel->cadet, th);
  1532. if (NULL != channel->cadet->th)
  1533. return th;
  1534. if (GNUNET_NO == channel->allow_send)
  1535. return th;
  1536. LOG (GNUNET_ERROR_TYPE_DEBUG, " call client notify tmt rdy\n");
  1537. channel->cadet->th =
  1538. GNUNET_CLIENT_notify_transmit_ready (channel->cadet->client, th->size,
  1539. GNUNET_TIME_UNIT_FOREVER_REL,
  1540. GNUNET_YES, &send_callback,
  1541. channel->cadet);
  1542. LOG (GNUNET_ERROR_TYPE_DEBUG, "CADET NOTIFY TRANSMIT READY END\n");
  1543. return th;
  1544. }
  1545. void
  1546. GNUNET_CADET_notify_transmit_ready_cancel (struct GNUNET_CADET_TransmitHandle *th)
  1547. {
  1548. struct GNUNET_CADET_Handle *cadet;
  1549. th->channel->packet_size = 0;
  1550. cadet = th->channel->cadet;
  1551. if (th->timeout_task != NULL)
  1552. GNUNET_SCHEDULER_cancel (th->timeout_task);
  1553. GNUNET_CONTAINER_DLL_remove (cadet->th_head, cadet->th_tail, th);
  1554. GNUNET_free (th);
  1555. if ((0 == message_ready_size (cadet)) && (NULL != cadet->th))
  1556. {
  1557. /* queue empty, no point in asking for transmission */
  1558. GNUNET_CLIENT_notify_transmit_ready_cancel (cadet->th);
  1559. cadet->th = NULL;
  1560. }
  1561. }
  1562. void
  1563. GNUNET_CADET_receive_done (struct GNUNET_CADET_Channel *channel)
  1564. {
  1565. send_ack (channel);
  1566. }
  1567. static void
  1568. send_info_request (struct GNUNET_CADET_Handle *h, uint16_t type)
  1569. {
  1570. struct GNUNET_MessageHeader msg;
  1571. msg.size = htons (sizeof (msg));
  1572. msg.type = htons (type);
  1573. send_packet (h, &msg, NULL);
  1574. }
  1575. /**
  1576. * Request a debug dump on the service's STDERR.
  1577. *
  1578. * WARNING: unstable API, likely to change in the future!
  1579. *
  1580. * @param h cadet handle
  1581. */
  1582. void
  1583. GNUNET_CADET_request_dump (struct GNUNET_CADET_Handle *h)
  1584. {
  1585. LOG (GNUNET_ERROR_TYPE_DEBUG, "requesting dump\n");
  1586. send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_DUMP);
  1587. }
  1588. /**
  1589. * Request information about peers known to the running cadet service.
  1590. * The callback will be called for every peer known to the service.
  1591. * Only one info request (of any kind) can be active at once.
  1592. *
  1593. *
  1594. * WARNING: unstable API, likely to change in the future!
  1595. *
  1596. * @param h Handle to the cadet peer.
  1597. * @param callback Function to call with the requested data.
  1598. * @param callback_cls Closure for @c callback.
  1599. *
  1600. * @return #GNUNET_OK / #GNUNET_SYSERR
  1601. */
  1602. int
  1603. GNUNET_CADET_get_peers (struct GNUNET_CADET_Handle *h,
  1604. GNUNET_CADET_PeersCB callback,
  1605. void *callback_cls)
  1606. {
  1607. if (NULL != h->info_cb.peers_cb)
  1608. {
  1609. GNUNET_break (0);
  1610. return GNUNET_SYSERR;
  1611. }
  1612. send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEERS);
  1613. h->info_cb.peers_cb = callback;
  1614. h->info_cls = callback_cls;
  1615. return GNUNET_OK;
  1616. }
  1617. /**
  1618. * Cancel a peer info request. The callback will not be called (anymore).
  1619. *
  1620. * WARNING: unstable API, likely to change in the future!
  1621. *
  1622. * @param h Cadet handle.
  1623. *
  1624. * @return Closure given to GNUNET_CADET_get_peers.
  1625. */
  1626. void *
  1627. GNUNET_CADET_get_peers_cancel (struct GNUNET_CADET_Handle *h)
  1628. {
  1629. void *cls;
  1630. cls = h->info_cls;
  1631. h->info_cb.peers_cb = NULL;
  1632. h->info_cls = NULL;
  1633. return cls;
  1634. }
  1635. /**
  1636. * Request information about a peer known to the running cadet peer.
  1637. * The callback will be called for the tunnel once.
  1638. * Only one info request (of any kind) can be active at once.
  1639. *
  1640. * WARNING: unstable API, likely to change in the future!
  1641. *
  1642. * @param h Handle to the cadet peer.
  1643. * @param id Peer whose tunnel to examine.
  1644. * @param callback Function to call with the requested data.
  1645. * @param callback_cls Closure for @c callback.
  1646. *
  1647. * @return #GNUNET_OK / #GNUNET_SYSERR
  1648. */
  1649. int
  1650. GNUNET_CADET_get_peer (struct GNUNET_CADET_Handle *h,
  1651. const struct GNUNET_PeerIdentity *id,
  1652. GNUNET_CADET_PeerCB callback,
  1653. void *callback_cls)
  1654. {
  1655. struct GNUNET_CADET_LocalInfo msg;
  1656. if (NULL != h->info_cb.peer_cb)
  1657. {
  1658. GNUNET_break (0);
  1659. return GNUNET_SYSERR;
  1660. }
  1661. memset (&msg, 0, sizeof (msg));
  1662. msg.header.size = htons (sizeof (msg));
  1663. msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER);
  1664. msg.peer = *id;
  1665. send_packet (h, &msg.header, NULL);
  1666. h->info_cb.peer_cb = callback;
  1667. h->info_cls = callback_cls;
  1668. return GNUNET_OK;
  1669. }
  1670. /**
  1671. * Request information about tunnels of the running cadet peer.
  1672. * The callback will be called for every tunnel of the service.
  1673. * Only one info request (of any kind) can be active at once.
  1674. *
  1675. * WARNING: unstable API, likely to change in the future!
  1676. *
  1677. * @param h Handle to the cadet peer.
  1678. * @param callback Function to call with the requested data.
  1679. * @param callback_cls Closure for @c callback.
  1680. *
  1681. * @return #GNUNET_OK / #GNUNET_SYSERR
  1682. */
  1683. int
  1684. GNUNET_CADET_get_tunnels (struct GNUNET_CADET_Handle *h,
  1685. GNUNET_CADET_TunnelsCB callback,
  1686. void *callback_cls)
  1687. {
  1688. if (NULL != h->info_cb.tunnels_cb)
  1689. {
  1690. GNUNET_break (0);
  1691. return GNUNET_SYSERR;
  1692. }
  1693. send_info_request (h, GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNELS);
  1694. h->info_cb.tunnels_cb = callback;
  1695. h->info_cls = callback_cls;
  1696. return GNUNET_OK;
  1697. }
  1698. /**
  1699. * Cancel a monitor request. The monitor callback will not be called.
  1700. *
  1701. * @param h Cadet handle.
  1702. *
  1703. * @return Closure given to GNUNET_CADET_get_tunnels.
  1704. */
  1705. void *
  1706. GNUNET_CADET_get_tunnels_cancel (struct GNUNET_CADET_Handle *h)
  1707. {
  1708. void *cls;
  1709. h->info_cb.tunnels_cb = NULL;
  1710. cls = h->info_cls;
  1711. h->info_cls = NULL;
  1712. return cls;
  1713. }
  1714. /**
  1715. * Request information about a tunnel of the running cadet peer.
  1716. * The callback will be called for the tunnel once.
  1717. * Only one info request (of any kind) can be active at once.
  1718. *
  1719. * WARNING: unstable API, likely to change in the future!
  1720. *
  1721. * @param h Handle to the cadet peer.
  1722. * @param id Peer whose tunnel to examine.
  1723. * @param callback Function to call with the requested data.
  1724. * @param callback_cls Closure for @c callback.
  1725. *
  1726. * @return #GNUNET_OK / #GNUNET_SYSERR
  1727. */
  1728. int
  1729. GNUNET_CADET_get_tunnel (struct GNUNET_CADET_Handle *h,
  1730. const struct GNUNET_PeerIdentity *id,
  1731. GNUNET_CADET_TunnelCB callback,
  1732. void *callback_cls)
  1733. {
  1734. struct GNUNET_CADET_LocalInfo msg;
  1735. if (NULL != h->info_cb.tunnel_cb)
  1736. {
  1737. GNUNET_break (0);
  1738. return GNUNET_SYSERR;
  1739. }
  1740. memset (&msg, 0, sizeof (msg));
  1741. msg.header.size = htons (sizeof (msg));
  1742. msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_TUNNEL);
  1743. msg.peer = *id;
  1744. send_packet (h, &msg.header, NULL);
  1745. h->info_cb.tunnel_cb = callback;
  1746. h->info_cls = callback_cls;
  1747. return GNUNET_OK;
  1748. }
  1749. /**
  1750. * Request information about a specific channel of the running cadet peer.
  1751. *
  1752. * WARNING: unstable API, likely to change in the future!
  1753. * FIXME Add destination option.
  1754. *
  1755. * @param h Handle to the cadet peer.
  1756. * @param initiator ID of the owner of the channel.
  1757. * @param channel_number Channel number.
  1758. * @param callback Function to call with the requested data.
  1759. * @param callback_cls Closure for @c callback.
  1760. *
  1761. * @return #GNUNET_OK / #GNUNET_SYSERR
  1762. */
  1763. int
  1764. GNUNET_CADET_show_channel (struct GNUNET_CADET_Handle *h,
  1765. struct GNUNET_PeerIdentity *initiator,
  1766. unsigned int channel_number,
  1767. GNUNET_CADET_ChannelCB callback,
  1768. void *callback_cls)
  1769. {
  1770. struct GNUNET_CADET_LocalInfo msg;
  1771. if (NULL != h->info_cb.channel_cb)
  1772. {
  1773. GNUNET_break (0);
  1774. return GNUNET_SYSERR;
  1775. }
  1776. msg.header.size = htons (sizeof (msg));
  1777. msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL);
  1778. msg.peer = *initiator;
  1779. msg.channel_id = htonl (channel_number);
  1780. // msg.reserved = 0;
  1781. send_packet (h, &msg.header, NULL);
  1782. h->info_cb.channel_cb = callback;
  1783. h->info_cls = callback_cls;
  1784. return GNUNET_OK;
  1785. }
  1786. /**
  1787. * Function called to notify a client about the connection
  1788. * begin ready to queue more data. "buf" will be
  1789. * NULL and "size" zero if the connection was closed for
  1790. * writing in the meantime.
  1791. *
  1792. * @param cls closure
  1793. * @param size number of bytes available in buf
  1794. * @param buf where the callee should write the message
  1795. * @return number of bytes written to buf
  1796. */
  1797. static size_t
  1798. cadet_mq_ntr (void *cls, size_t size,
  1799. void *buf)
  1800. {
  1801. struct GNUNET_MQ_Handle *mq = cls;
  1802. struct CadetMQState *state = GNUNET_MQ_impl_state (mq);
  1803. const struct GNUNET_MessageHeader *msg = GNUNET_MQ_impl_current (mq);
  1804. uint16_t msize;
  1805. state->th = NULL;
  1806. if (NULL == buf)
  1807. {
  1808. GNUNET_MQ_inject_error (mq, GNUNET_MQ_ERROR_WRITE);
  1809. return 0;
  1810. }
  1811. msize = ntohs (msg->size);
  1812. GNUNET_assert (msize <= size);
  1813. memcpy (buf, msg, msize);
  1814. GNUNET_MQ_impl_send_continue (mq);
  1815. return msize;
  1816. }
  1817. /**
  1818. * Signature of functions implementing the
  1819. * sending functionality of a message queue.
  1820. *
  1821. * @param mq the message queue
  1822. * @param msg the message to send
  1823. * @param impl_state state of the implementation
  1824. */
  1825. static void
  1826. cadet_mq_send_impl (struct GNUNET_MQ_Handle *mq,
  1827. const struct GNUNET_MessageHeader *msg,
  1828. void *impl_state)
  1829. {
  1830. struct CadetMQState *state = impl_state;
  1831. GNUNET_assert (NULL == state->th);
  1832. state->th =
  1833. GNUNET_CADET_notify_transmit_ready (state->channel,
  1834. /* FIXME: add option for corking */
  1835. GNUNET_NO,
  1836. GNUNET_TIME_UNIT_FOREVER_REL,
  1837. ntohs (msg->size),
  1838. &cadet_mq_ntr, mq);
  1839. }
  1840. /**
  1841. * Signature of functions implementing the
  1842. * destruction of a message queue.
  1843. * Implementations must not free 'mq', but should
  1844. * take care of 'impl_state'.
  1845. *
  1846. * @param mq the message queue to destroy
  1847. * @param impl_state state of the implementation
  1848. */
  1849. static void
  1850. cadet_mq_destroy_impl (struct GNUNET_MQ_Handle *mq,
  1851. void *impl_state)
  1852. {
  1853. struct CadetMQState *state = impl_state;
  1854. if (NULL != state->th)
  1855. GNUNET_CADET_notify_transmit_ready_cancel (state->th);
  1856. GNUNET_free (state);
  1857. }
  1858. /**
  1859. * Create a message queue for a cadet channel.
  1860. * The message queue can only be used to transmit messages,
  1861. * not to receive them.
  1862. *
  1863. * @param channel the channel to create the message qeue for
  1864. * @return a message queue to messages over the channel
  1865. */
  1866. struct GNUNET_MQ_Handle *
  1867. GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel)
  1868. {
  1869. struct GNUNET_MQ_Handle *mq;
  1870. struct CadetMQState *state;
  1871. state = GNUNET_new (struct CadetMQState);
  1872. state->channel = channel;
  1873. mq = GNUNET_MQ_queue_for_callbacks (&cadet_mq_send_impl,
  1874. &cadet_mq_destroy_impl,
  1875. NULL, /* FIXME: cancel impl. */
  1876. state,
  1877. NULL, /* no msg handlers */
  1878. NULL, /* no err handlers */
  1879. NULL); /* no handler cls */
  1880. return mq;
  1881. }