psyc_util_lib.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. /*
  2. * This file is part of GNUnet
  3. * (C) 2013 Christian Grothoff (and other contributing authors)
  4. *
  5. * GNUnet is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published
  7. * by the Free Software Foundation; either version 3, or (at your
  8. * option) any later version.
  9. *
  10. * GNUnet is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GNUnet; see the file COPYING. If not, write to the
  17. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. * Boston, MA 02111-1307, USA.
  19. */
  20. /**
  21. * @file psyc/psyc_util_lib.c
  22. * @brief PSYC utilities; receiving/transmitting/logging PSYC messages.
  23. * @author Gabor X Toth
  24. */
  25. #include <inttypes.h>
  26. #include "platform.h"
  27. #include "gnunet_util_lib.h"
  28. #include "gnunet_env_lib.h"
  29. #include "gnunet_psyc_service.h"
  30. #include "gnunet_psyc_util_lib.h"
  31. #define LOG(kind,...) GNUNET_log_from (kind, "psyc-util",__VA_ARGS__)
  32. struct GNUNET_PSYC_TransmitHandle
  33. {
  34. /**
  35. * Client connection to service.
  36. */
  37. struct GNUNET_CLIENT_MANAGER_Connection *client;
  38. /**
  39. * Message currently being received from the client.
  40. */
  41. struct GNUNET_MessageHeader *msg;
  42. /**
  43. * Callback to request next modifier from client.
  44. */
  45. GNUNET_PSYC_TransmitNotifyModifier notify_mod;
  46. /**
  47. * Closure for the notify callbacks.
  48. */
  49. void *notify_mod_cls;
  50. /**
  51. * Callback to request next data fragment from client.
  52. */
  53. GNUNET_PSYC_TransmitNotifyData notify_data;
  54. /**
  55. * Closure for the notify callbacks.
  56. */
  57. void *notify_data_cls;
  58. /**
  59. * Modifier of the environment that is currently being transmitted.
  60. */
  61. struct GNUNET_ENV_Modifier *mod;
  62. /**
  63. *
  64. */
  65. const char *mod_value;
  66. /**
  67. * Number of bytes remaining to be transmitted from the current modifier value.
  68. */
  69. uint32_t mod_value_remaining;
  70. /**
  71. * State of the current message being received from client.
  72. */
  73. enum GNUNET_PSYC_MessageState state;
  74. /**
  75. * Number of PSYC_TRANSMIT_ACK messages we are still waiting for.
  76. */
  77. uint8_t acks_pending;
  78. /**
  79. * Is transmission paused?
  80. */
  81. uint8_t paused;
  82. /**
  83. * Are we currently transmitting a message?
  84. */
  85. uint8_t in_transmit;
  86. };
  87. struct GNUNET_PSYC_ReceiveHandle
  88. {
  89. /**
  90. * Message callback.
  91. */
  92. GNUNET_PSYC_MessageCallback message_cb;
  93. /**
  94. * Message part callback.
  95. */
  96. GNUNET_PSYC_MessagePartCallback message_part_cb;
  97. /**
  98. * Closure for the callbacks.
  99. */
  100. void *cb_cls;
  101. /**
  102. * ID of the message being received from the PSYC service.
  103. */
  104. uint64_t message_id;
  105. /**
  106. * Public key of the slave from which a message is being received.
  107. */
  108. struct GNUNET_CRYPTO_EcdsaPublicKey slave_key;
  109. /**
  110. * State of the currently being received message from the PSYC service.
  111. */
  112. enum GNUNET_PSYC_MessageState state;
  113. /**
  114. * Flags for the currently being received message from the PSYC service.
  115. */
  116. enum GNUNET_PSYC_MessageFlags flags;
  117. /**
  118. * Expected value size for the modifier being received from the PSYC service.
  119. */
  120. uint32_t mod_value_size_expected;
  121. /**
  122. * Actual value size for the modifier being received from the PSYC service.
  123. */
  124. uint32_t mod_value_size;
  125. };
  126. /**** Messages ****/
  127. /**
  128. * Create a PSYC message.
  129. *
  130. * @param method_name
  131. * PSYC method for the message.
  132. * @param env
  133. * Environment for the message.
  134. * @param data
  135. * Data payload for the message.
  136. * @param data_size
  137. * Size of @a data.
  138. *
  139. * @return Message header with size information,
  140. * followed by the message parts.
  141. */
  142. struct GNUNET_PSYC_Message *
  143. GNUNET_PSYC_message_create (const char *method_name,
  144. const struct GNUNET_ENV_Environment *env,
  145. const void *data,
  146. size_t data_size)
  147. {
  148. struct GNUNET_ENV_Modifier *mod = NULL;
  149. struct GNUNET_PSYC_MessageMethod *pmeth = NULL;
  150. struct GNUNET_PSYC_MessageModifier *pmod = NULL;
  151. struct GNUNET_MessageHeader *pmsg = NULL;
  152. uint16_t env_size = 0;
  153. if (NULL != env)
  154. {
  155. mod = GNUNET_ENV_environment_head (env);
  156. while (NULL != mod)
  157. {
  158. env_size += sizeof (*pmod) + strlen (mod->name) + 1 + mod->value_size;
  159. mod = mod->next;
  160. }
  161. }
  162. struct GNUNET_PSYC_Message *msg;
  163. uint16_t method_name_size = strlen (method_name) + 1;
  164. if (method_name_size == 1)
  165. return NULL;
  166. uint16_t msg_size = sizeof (*msg) /* header */
  167. + sizeof (*pmeth) + method_name_size /* method */
  168. + env_size /* modifiers */
  169. + ((0 < data_size) ? sizeof (*pmsg) + data_size : 0)/* data */
  170. + sizeof (*pmsg); /* end of message */
  171. msg = GNUNET_malloc (msg_size);
  172. msg->header.size = htons (msg_size);
  173. msg->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE); /* FIXME */
  174. pmeth = (struct GNUNET_PSYC_MessageMethod *) &msg[1];
  175. pmeth->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD);
  176. pmeth->header.size = htons (sizeof (*pmeth) + method_name_size);
  177. memcpy (&pmeth[1], method_name, method_name_size);
  178. uint16_t p = sizeof (*msg) + sizeof (*pmeth) + method_name_size;
  179. if (NULL != env)
  180. {
  181. mod = GNUNET_ENV_environment_head (env);
  182. while (NULL != mod)
  183. {
  184. uint16_t mod_name_size = strlen (mod->name) + 1;
  185. pmod = (struct GNUNET_PSYC_MessageModifier *) ((char *) msg + p);
  186. pmod->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER);
  187. pmod->header.size = sizeof (*pmod) + mod_name_size + mod->value_size;
  188. p += pmod->header.size;
  189. pmod->header.size = htons (pmod->header.size);
  190. memcpy (&pmod[1], mod->name, mod_name_size);
  191. if (0 < mod->value_size)
  192. memcpy ((char *) &pmod[1] + mod_name_size, mod->value, mod->value_size);
  193. mod = mod->next;
  194. }
  195. }
  196. if (0 < data_size)
  197. {
  198. pmsg = (struct GNUNET_MessageHeader *) ((char *) msg + p);
  199. pmsg->size = sizeof (*pmsg) + data_size;
  200. p += pmsg->size;
  201. pmsg->size = htons (pmsg->size);
  202. pmsg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA);
  203. memcpy (&pmsg[1], data, data_size);
  204. }
  205. pmsg = (struct GNUNET_MessageHeader *) ((char *) msg + p);
  206. pmsg->size = htons (sizeof (*pmsg));
  207. pmsg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END);
  208. GNUNET_assert (p + sizeof (*pmsg) == msg_size);
  209. return msg;
  210. }
  211. void
  212. GNUNET_PSYC_log_message (enum GNUNET_ErrorType kind,
  213. const struct GNUNET_MessageHeader *msg)
  214. {
  215. uint16_t size = ntohs (msg->size);
  216. uint16_t type = ntohs (msg->type);
  217. GNUNET_log (kind, "Message of type %d and size %u:\n", type, size);
  218. switch (type)
  219. {
  220. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE:
  221. {
  222. struct GNUNET_PSYC_MessageHeader *pmsg
  223. = (struct GNUNET_PSYC_MessageHeader *) msg;
  224. GNUNET_log (kind, "\tID: %" PRIu64 "\tflags: %x" PRIu32 "\n",
  225. GNUNET_ntohll (pmsg->message_id), ntohl (pmsg->flags));
  226. break;
  227. }
  228. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
  229. {
  230. struct GNUNET_PSYC_MessageMethod *meth
  231. = (struct GNUNET_PSYC_MessageMethod *) msg;
  232. GNUNET_log (kind, "\t%.*s\n", size - sizeof (*meth), &meth[1]);
  233. break;
  234. }
  235. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
  236. {
  237. struct GNUNET_PSYC_MessageModifier *mod
  238. = (struct GNUNET_PSYC_MessageModifier *) msg;
  239. uint16_t name_size = ntohs (mod->name_size);
  240. char oper = ' ' < mod->oper ? mod->oper : ' ';
  241. GNUNET_log (kind, "\t%c%.*s\t%.*s\n", oper, name_size, &mod[1],
  242. size - sizeof (*mod) - name_size,
  243. ((char *) &mod[1]) + name_size);
  244. break;
  245. }
  246. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
  247. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
  248. GNUNET_log (kind, "\t%.*s\n", size - sizeof (*msg), &msg[1]);
  249. break;
  250. }
  251. }
  252. /**** Transmitting messages ****/
  253. /**
  254. * Create a transmission handle.
  255. */
  256. struct GNUNET_PSYC_TransmitHandle *
  257. GNUNET_PSYC_transmit_create (struct GNUNET_CLIENT_MANAGER_Connection *client)
  258. {
  259. struct GNUNET_PSYC_TransmitHandle *tmit = GNUNET_malloc (sizeof (*tmit));
  260. tmit->client = client;
  261. return tmit;
  262. }
  263. /**
  264. * Destroy a transmission handle.
  265. */
  266. void
  267. GNUNET_PSYC_transmit_destroy (struct GNUNET_PSYC_TransmitHandle *tmit)
  268. {
  269. GNUNET_free (tmit);
  270. }
  271. /**
  272. * Queue a message part for transmission.
  273. *
  274. * The message part is added to the current message buffer.
  275. * When this buffer is full, it is added to the transmission queue.
  276. *
  277. * @param tmit Transmission handle.
  278. * @param msg Message part, or NULL.
  279. * @param end End of message? #GNUNET_YES or #GNUNET_NO.
  280. */
  281. static void
  282. transmit_queue_insert (struct GNUNET_PSYC_TransmitHandle *tmit,
  283. const struct GNUNET_MessageHeader *msg,
  284. uint8_t end)
  285. {
  286. uint16_t size = (NULL != msg) ? ntohs (msg->size) : 0;
  287. LOG (GNUNET_ERROR_TYPE_DEBUG,
  288. "Queueing message part of type %u and size %u (end: %u)).\n",
  289. ntohs (msg->type), size, end);
  290. if (NULL != tmit->msg)
  291. {
  292. if (NULL == msg
  293. || GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD < tmit->msg->size + size)
  294. {
  295. /* End of message or buffer is full, add it to transmission queue
  296. * and start with empty buffer */
  297. tmit->msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE);
  298. tmit->msg->size = htons (tmit->msg->size);
  299. GNUNET_CLIENT_MANAGER_transmit (tmit->client, tmit->msg);
  300. tmit->msg = NULL;
  301. tmit->acks_pending++;
  302. }
  303. else
  304. {
  305. /* Message fits in current buffer, append */
  306. tmit->msg = GNUNET_realloc (tmit->msg, tmit->msg->size + size);
  307. memcpy ((char *) tmit->msg + tmit->msg->size, msg, size);
  308. tmit->msg->size += size;
  309. }
  310. }
  311. if (NULL == tmit->msg && NULL != msg)
  312. {
  313. /* Empty buffer, copy over message. */
  314. tmit->msg = GNUNET_malloc (sizeof (*tmit->msg) + size);
  315. tmit->msg->size = sizeof (*tmit->msg) + size;
  316. memcpy (&tmit->msg[1], msg, size);
  317. }
  318. if (NULL != tmit->msg
  319. && (GNUNET_YES == end
  320. || (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD
  321. < tmit->msg->size + sizeof (struct GNUNET_MessageHeader))))
  322. {
  323. /* End of message or buffer is full, add it to transmission queue. */
  324. tmit->msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE);
  325. tmit->msg->size = htons (tmit->msg->size);
  326. GNUNET_CLIENT_MANAGER_transmit (tmit->client, tmit->msg);
  327. tmit->msg = NULL;
  328. tmit->acks_pending++;
  329. }
  330. if (GNUNET_YES == end)
  331. tmit->in_transmit = GNUNET_NO;
  332. }
  333. /**
  334. * Request data from client to transmit.
  335. *
  336. * @param tmit Transmission handle.
  337. */
  338. static void
  339. transmit_data (struct GNUNET_PSYC_TransmitHandle *tmit)
  340. {
  341. uint16_t data_size = GNUNET_PSYC_DATA_MAX_PAYLOAD;
  342. char data[GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD] = "";
  343. struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) data;
  344. msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA);
  345. int notify_ret = tmit->notify_data (tmit->notify_data_cls, &data_size, &msg[1]);
  346. LOG (GNUNET_ERROR_TYPE_DEBUG,
  347. "transmit_data (ret: %d, size: %u): %.*s\n",
  348. notify_ret, data_size, data_size, &msg[1]);
  349. switch (notify_ret)
  350. {
  351. case GNUNET_NO:
  352. if (0 == data_size)
  353. {
  354. /* Transmission paused, nothing to send. */
  355. tmit->paused = GNUNET_YES;
  356. return;
  357. }
  358. break;
  359. case GNUNET_YES:
  360. tmit->state = GNUNET_PSYC_MESSAGE_STATE_END;
  361. break;
  362. default:
  363. LOG (GNUNET_ERROR_TYPE_ERROR,
  364. "TransmitNotifyData callback returned error when requesting data.\n");
  365. tmit->state = GNUNET_PSYC_MESSAGE_STATE_CANCEL;
  366. msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL);
  367. msg->size = htons (sizeof (*msg));
  368. transmit_queue_insert (tmit, msg, GNUNET_YES);
  369. return;
  370. }
  371. if (0 < data_size)
  372. {
  373. GNUNET_assert (data_size <= GNUNET_PSYC_DATA_MAX_PAYLOAD);
  374. msg->size = htons (sizeof (*msg) + data_size);
  375. transmit_queue_insert (tmit, msg, !notify_ret);
  376. }
  377. /* End of message. */
  378. if (GNUNET_YES == notify_ret)
  379. {
  380. msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END);
  381. msg->size = htons (sizeof (*msg));
  382. transmit_queue_insert (tmit, msg, GNUNET_YES);
  383. }
  384. }
  385. /**
  386. * Request a modifier from a client to transmit.
  387. *
  388. * @param tmit Transmission handle.
  389. */
  390. static void
  391. transmit_mod (struct GNUNET_PSYC_TransmitHandle *tmit)
  392. {
  393. uint16_t max_data_size, data_size;
  394. char data[GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD] = "";
  395. struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) data;
  396. int notify_ret;
  397. switch (tmit->state)
  398. {
  399. case GNUNET_PSYC_MESSAGE_STATE_MODIFIER:
  400. {
  401. struct GNUNET_PSYC_MessageModifier *mod
  402. = (struct GNUNET_PSYC_MessageModifier *) msg;
  403. max_data_size = data_size = GNUNET_PSYC_MODIFIER_MAX_PAYLOAD;
  404. msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER);
  405. msg->size = sizeof (struct GNUNET_PSYC_MessageModifier);
  406. notify_ret = tmit->notify_mod (tmit->notify_mod_cls, &data_size, &mod[1],
  407. &mod->oper, &mod->value_size);
  408. mod->name_size = strnlen ((char *) &mod[1], data_size) + 1;
  409. LOG (GNUNET_ERROR_TYPE_DEBUG,
  410. "transmit_mod (ret: %d, size: %u + %u): %.*s\n",
  411. notify_ret, mod->name_size, mod->value_size, data_size, &mod[1]);
  412. if (mod->name_size < data_size)
  413. {
  414. tmit->mod_value_remaining
  415. = mod->value_size - (data_size - mod->name_size);
  416. mod->value_size = htonl (mod->value_size);
  417. mod->name_size = htons (mod->name_size);
  418. }
  419. else if (0 < data_size)
  420. {
  421. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got invalid modifier name.\n");
  422. notify_ret = GNUNET_SYSERR;
  423. }
  424. break;
  425. }
  426. case GNUNET_PSYC_MESSAGE_STATE_MOD_CONT:
  427. {
  428. max_data_size = data_size = GNUNET_PSYC_MOD_CONT_MAX_PAYLOAD;
  429. msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT);
  430. msg->size = sizeof (struct GNUNET_MessageHeader);
  431. notify_ret = tmit->notify_mod (tmit->notify_mod_cls,
  432. &data_size, &msg[1], NULL, NULL);
  433. tmit->mod_value_remaining -= data_size;
  434. LOG (GNUNET_ERROR_TYPE_DEBUG,
  435. "transmit_mod (ret: %d, size: %u): %.*s\n",
  436. notify_ret, data_size, data_size, &msg[1]);
  437. break;
  438. }
  439. default:
  440. GNUNET_assert (0);
  441. }
  442. switch (notify_ret)
  443. {
  444. case GNUNET_NO:
  445. if (0 == data_size)
  446. { /* Transmission paused, nothing to send. */
  447. tmit->paused = GNUNET_YES;
  448. return;
  449. }
  450. tmit->state
  451. = (0 == tmit->mod_value_remaining)
  452. ? GNUNET_PSYC_MESSAGE_STATE_MODIFIER
  453. : GNUNET_PSYC_MESSAGE_STATE_MOD_CONT;
  454. break;
  455. case GNUNET_YES: /* End of modifiers. */
  456. GNUNET_assert (0 == tmit->mod_value_remaining);
  457. break;
  458. default:
  459. LOG (GNUNET_ERROR_TYPE_ERROR,
  460. "TransmitNotifyModifier callback returned with error.\n");
  461. tmit->state = GNUNET_PSYC_MESSAGE_STATE_CANCEL;
  462. msg->type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL);
  463. msg->size = htons (sizeof (*msg));
  464. transmit_queue_insert (tmit, msg, GNUNET_YES);
  465. return;
  466. }
  467. if (0 < data_size)
  468. {
  469. GNUNET_assert (data_size <= max_data_size);
  470. msg->size = htons (msg->size + data_size);
  471. transmit_queue_insert (tmit, msg, GNUNET_NO);
  472. }
  473. if (GNUNET_YES == notify_ret)
  474. {
  475. tmit->state = GNUNET_PSYC_MESSAGE_STATE_DATA;
  476. if (0 == tmit->acks_pending)
  477. transmit_data (tmit);
  478. }
  479. else
  480. {
  481. transmit_mod (tmit);
  482. }
  483. }
  484. int
  485. transmit_notify_env (void *cls, uint16_t *data_size, void *data, uint8_t *oper,
  486. uint32_t *full_value_size)
  487. {
  488. struct GNUNET_PSYC_TransmitHandle *tmit = cls;
  489. uint16_t name_size = 0;
  490. size_t value_size = 0;
  491. const char *value = NULL;
  492. if (NULL != oper)
  493. { /* New modifier */
  494. if (NULL != tmit->mod)
  495. tmit->mod = tmit->mod->next;
  496. if (NULL == tmit->mod)
  497. { /* No more modifiers, continue with data */
  498. *data_size = 0;
  499. return GNUNET_YES;
  500. }
  501. GNUNET_assert (tmit->mod->value_size < UINT32_MAX);
  502. *full_value_size = tmit->mod->value_size;
  503. *oper = tmit->mod->oper;
  504. name_size = strlen (tmit->mod->name) + 1;
  505. if (name_size + tmit->mod->value_size <= *data_size)
  506. {
  507. *data_size = name_size + tmit->mod->value_size;
  508. }
  509. else
  510. {
  511. value_size = *data_size - name_size;
  512. tmit->mod_value = tmit->mod->value + value_size;
  513. }
  514. memcpy (data, tmit->mod->name, name_size);
  515. memcpy ((char *)data + name_size, tmit->mod->value, value_size);
  516. return GNUNET_NO;
  517. }
  518. else
  519. { /* Modifier continuation */
  520. GNUNET_assert (NULL != tmit->mod_value && 0 < tmit->mod_value_remaining);
  521. value = tmit->mod_value;
  522. if (tmit->mod_value_remaining <= *data_size)
  523. {
  524. value_size = tmit->mod_value_remaining;
  525. tmit->mod_value = NULL;
  526. }
  527. else
  528. {
  529. value_size = *data_size;
  530. tmit->mod_value += value_size;
  531. }
  532. if (*data_size < value_size)
  533. {
  534. LOG (GNUNET_ERROR_TYPE_DEBUG,
  535. "Value in environment larger than buffer: %u < %zu\n",
  536. *data_size, value_size);
  537. *data_size = 0;
  538. return GNUNET_NO;
  539. }
  540. *data_size = value_size;
  541. memcpy (data, value, value_size);
  542. return (tmit->mod_value = NULL) ? GNUNET_YES : GNUNET_NO;
  543. }
  544. }
  545. /**
  546. * Transmit a message.
  547. *
  548. * @param tmit Transmission handle.
  549. * @param method_name Which method should be invoked.
  550. * @param env Environment for the message.
  551. * Should stay available until the first call to notify_data.
  552. * Can be NULL if there are no modifiers or @a notify_mod is provided instead.
  553. * @param notify_mod Function to call to obtain modifiers.
  554. * Can be NULL if there are no modifiers or @a env is provided instead.
  555. * @param notify_data Function to call to obtain fragments of the data.
  556. * @param notify_cls Closure for @a notify_mod and @a notify_data.
  557. * @param flags Flags for the message being transmitted.
  558. *
  559. * @return #GNUNET_OK if the transmission was started.
  560. * #GNUNET_SYSERR if another transmission is already going on.
  561. */
  562. int
  563. GNUNET_PSYC_transmit_message (struct GNUNET_PSYC_TransmitHandle *tmit,
  564. const char *method_name,
  565. const struct GNUNET_ENV_Environment *env,
  566. GNUNET_PSYC_TransmitNotifyModifier notify_mod,
  567. GNUNET_PSYC_TransmitNotifyData notify_data,
  568. void *notify_cls,
  569. uint32_t flags)
  570. {
  571. if (GNUNET_NO != tmit->in_transmit)
  572. return GNUNET_SYSERR;
  573. tmit->in_transmit = GNUNET_YES;
  574. size_t size = strlen (method_name) + 1;
  575. struct GNUNET_PSYC_MessageMethod *pmeth;
  576. tmit->msg = GNUNET_malloc (sizeof (*tmit->msg) + sizeof (*pmeth) + size);
  577. tmit->msg->size = sizeof (*tmit->msg) + sizeof (*pmeth) + size;
  578. pmeth = (struct GNUNET_PSYC_MessageMethod *) &tmit->msg[1];
  579. pmeth->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD);
  580. pmeth->header.size = htons (sizeof (*pmeth) + size);
  581. pmeth->flags = htonl (flags);
  582. memcpy (&pmeth[1], method_name, size);
  583. tmit->state = GNUNET_PSYC_MESSAGE_STATE_MODIFIER;
  584. tmit->notify_data = notify_data;
  585. tmit->notify_data_cls = notify_cls;
  586. if (NULL != notify_mod)
  587. {
  588. tmit->notify_mod = notify_mod;
  589. tmit->notify_mod_cls = notify_cls;
  590. }
  591. else
  592. {
  593. tmit->notify_mod = &transmit_notify_env;
  594. tmit->notify_mod_cls = tmit;
  595. if (NULL != env)
  596. {
  597. struct GNUNET_ENV_Modifier mod = {};
  598. mod.next = GNUNET_ENV_environment_head (env);
  599. tmit->mod = &mod;
  600. }
  601. else
  602. {
  603. tmit->mod = NULL;
  604. }
  605. }
  606. transmit_mod (tmit);
  607. return GNUNET_OK;
  608. }
  609. /**
  610. * Resume transmission.
  611. *
  612. * @param tmit Transmission handle.
  613. */
  614. void
  615. GNUNET_PSYC_transmit_resume (struct GNUNET_PSYC_TransmitHandle *tmit)
  616. {
  617. if (0 == tmit->acks_pending)
  618. {
  619. tmit->paused = GNUNET_NO;
  620. transmit_data (tmit);
  621. }
  622. }
  623. /**
  624. * Abort transmission request.
  625. *
  626. * @param tmit Transmission handle.
  627. */
  628. void
  629. GNUNET_PSYC_transmit_cancel (struct GNUNET_PSYC_TransmitHandle *tmit)
  630. {
  631. if (GNUNET_NO == tmit->in_transmit)
  632. return;
  633. tmit->state = GNUNET_PSYC_MESSAGE_STATE_CANCEL;
  634. tmit->in_transmit = GNUNET_NO;
  635. tmit->paused = GNUNET_NO;
  636. /* FIXME */
  637. struct GNUNET_MessageHeader msg;
  638. msg.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA);
  639. msg.type = htons (GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL);
  640. msg.size = htons (sizeof (msg));
  641. transmit_queue_insert (tmit, &msg, GNUNET_YES);
  642. }
  643. /**
  644. * Got acknowledgement of a transmitted message part, continue transmission.
  645. *
  646. * @param tmit Transmission handle.
  647. */
  648. void
  649. GNUNET_PSYC_transmit_got_ack (struct GNUNET_PSYC_TransmitHandle *tmit)
  650. {
  651. if (0 == tmit->acks_pending)
  652. {
  653. LOG (GNUNET_ERROR_TYPE_WARNING, "Ignoring extraneous message ACK\n");
  654. GNUNET_break (0);
  655. return;
  656. }
  657. tmit->acks_pending--;
  658. switch (tmit->state)
  659. {
  660. case GNUNET_PSYC_MESSAGE_STATE_MODIFIER:
  661. case GNUNET_PSYC_MESSAGE_STATE_MOD_CONT:
  662. if (GNUNET_NO == tmit->paused)
  663. transmit_mod (tmit);
  664. break;
  665. case GNUNET_PSYC_MESSAGE_STATE_DATA:
  666. if (GNUNET_NO == tmit->paused)
  667. transmit_data (tmit);
  668. break;
  669. case GNUNET_PSYC_MESSAGE_STATE_END:
  670. case GNUNET_PSYC_MESSAGE_STATE_CANCEL:
  671. break;
  672. default:
  673. LOG (GNUNET_ERROR_TYPE_DEBUG,
  674. "Ignoring message ACK in state %u.\n", tmit->state);
  675. }
  676. }
  677. /**** Receiving messages ****/
  678. /**
  679. * Create handle for receiving messages.
  680. */
  681. struct GNUNET_PSYC_ReceiveHandle *
  682. GNUNET_PSYC_receive_create (GNUNET_PSYC_MessageCallback message_cb,
  683. GNUNET_PSYC_MessagePartCallback message_part_cb,
  684. void *cb_cls)
  685. {
  686. struct GNUNET_PSYC_ReceiveHandle *recv = GNUNET_malloc (sizeof (*recv));
  687. recv->message_cb = message_cb;
  688. recv->message_part_cb = message_part_cb;
  689. recv->cb_cls = cb_cls;
  690. return recv;
  691. }
  692. /**
  693. * Destroy handle for receiving messages.
  694. */
  695. void
  696. GNUNET_PSYC_receive_destroy (struct GNUNET_PSYC_ReceiveHandle *recv)
  697. {
  698. GNUNET_free (recv);
  699. }
  700. /**
  701. * Reset stored data related to the last received message.
  702. */
  703. void
  704. GNUNET_PSYC_receive_reset (struct GNUNET_PSYC_ReceiveHandle *recv)
  705. {
  706. recv->state = GNUNET_PSYC_MESSAGE_STATE_START;
  707. recv->flags = 0;
  708. recv->message_id = 0;
  709. recv->mod_value_size = 0;
  710. recv->mod_value_size_expected = 0;
  711. }
  712. static void
  713. recv_error (struct GNUNET_PSYC_ReceiveHandle *recv)
  714. {
  715. if (NULL != recv->message_part_cb)
  716. recv->message_part_cb (recv->cb_cls, recv->message_id, 0, recv->flags, NULL);
  717. if (NULL != recv->message_cb)
  718. recv->message_cb (recv->cb_cls, recv->message_id, recv->flags, NULL);
  719. GNUNET_PSYC_receive_reset (recv);
  720. }
  721. /**
  722. * Handle incoming PSYC message.
  723. *
  724. * @param recv Receive handle.
  725. * @param msg The message.
  726. *
  727. * @return #GNUNET_OK on success,
  728. * #GNUNET_SYSERR on receive error.
  729. */
  730. int
  731. GNUNET_PSYC_receive_message (struct GNUNET_PSYC_ReceiveHandle *recv,
  732. const struct GNUNET_PSYC_MessageHeader *msg)
  733. {
  734. uint16_t size = ntohs (msg->header.size);
  735. uint32_t flags = ntohl (msg->flags);
  736. uint64_t message_id;
  737. GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG,
  738. (struct GNUNET_MessageHeader *) msg);
  739. if (GNUNET_PSYC_MESSAGE_STATE_START == recv->state)
  740. {
  741. recv->message_id = GNUNET_ntohll (msg->message_id);
  742. recv->flags = flags;
  743. recv->slave_key = msg->slave_key;
  744. recv->mod_value_size = 0;
  745. recv->mod_value_size_expected = 0;
  746. }
  747. else if (GNUNET_ntohll (msg->message_id) != recv->message_id)
  748. {
  749. // FIXME
  750. LOG (GNUNET_ERROR_TYPE_WARNING,
  751. "Unexpected message ID. Got: %" PRIu64 ", expected: %" PRIu64 "\n",
  752. GNUNET_ntohll (msg->message_id), recv->message_id);
  753. GNUNET_break_op (0);
  754. recv_error (recv);
  755. return GNUNET_SYSERR;
  756. }
  757. else if (flags != recv->flags)
  758. {
  759. LOG (GNUNET_ERROR_TYPE_WARNING,
  760. "Unexpected message flags. Got: %lu, expected: %lu\n",
  761. flags, recv->flags);
  762. GNUNET_break_op (0);
  763. recv_error (recv);
  764. return GNUNET_SYSERR;
  765. }
  766. message_id = recv->message_id;
  767. uint16_t pos = 0, psize = 0, ptype, size_eq, size_min;
  768. for (pos = 0; sizeof (*msg) + pos < size; pos += psize)
  769. {
  770. const struct GNUNET_MessageHeader *pmsg
  771. = (const struct GNUNET_MessageHeader *) ((char *) &msg[1] + pos);
  772. psize = ntohs (pmsg->size);
  773. ptype = ntohs (pmsg->type);
  774. size_eq = size_min = 0;
  775. if (psize < sizeof (*pmsg) || sizeof (*msg) + pos + psize > size)
  776. {
  777. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  778. "Dropping message of type %u with invalid size %u.\n",
  779. ptype, psize);
  780. recv_error (recv);
  781. return GNUNET_SYSERR;
  782. }
  783. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  784. "Received message part from PSYC.\n");
  785. GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
  786. switch (ptype)
  787. {
  788. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
  789. size_min = sizeof (struct GNUNET_PSYC_MessageMethod);
  790. break;
  791. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
  792. size_min = sizeof (struct GNUNET_PSYC_MessageModifier);
  793. break;
  794. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
  795. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
  796. size_min = sizeof (struct GNUNET_MessageHeader);
  797. break;
  798. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
  799. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
  800. size_eq = sizeof (struct GNUNET_MessageHeader);
  801. break;
  802. default:
  803. GNUNET_break_op (0);
  804. recv_error (recv);
  805. return GNUNET_SYSERR;
  806. }
  807. if (! ((0 < size_eq && psize == size_eq)
  808. || (0 < size_min && size_min <= psize)))
  809. {
  810. GNUNET_break_op (0);
  811. recv_error (recv);
  812. return GNUNET_SYSERR;
  813. }
  814. switch (ptype)
  815. {
  816. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
  817. {
  818. struct GNUNET_PSYC_MessageMethod *meth
  819. = (struct GNUNET_PSYC_MessageMethod *) pmsg;
  820. if (GNUNET_PSYC_MESSAGE_STATE_START != recv->state)
  821. {
  822. LOG (GNUNET_ERROR_TYPE_WARNING,
  823. "Dropping out of order message method (%u).\n",
  824. recv->state);
  825. /* It is normal to receive an incomplete message right after connecting,
  826. * but should not happen later.
  827. * FIXME: add a check for this condition.
  828. */
  829. GNUNET_break_op (0);
  830. recv_error (recv);
  831. return GNUNET_SYSERR;
  832. }
  833. if ('\0' != *((char *) meth + psize - 1))
  834. {
  835. LOG (GNUNET_ERROR_TYPE_WARNING,
  836. "Dropping message with malformed method. "
  837. "Message ID: %" PRIu64 "\n", recv->message_id);
  838. GNUNET_break_op (0);
  839. recv_error (recv);
  840. return GNUNET_SYSERR;
  841. }
  842. recv->state = GNUNET_PSYC_MESSAGE_STATE_METHOD;
  843. break;
  844. }
  845. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
  846. {
  847. if (!(GNUNET_PSYC_MESSAGE_STATE_METHOD == recv->state
  848. || GNUNET_PSYC_MESSAGE_STATE_MODIFIER == recv->state
  849. || GNUNET_PSYC_MESSAGE_STATE_MOD_CONT == recv->state))
  850. {
  851. LOG (GNUNET_ERROR_TYPE_WARNING,
  852. "Dropping out of order message modifier (%u).\n",
  853. recv->state);
  854. GNUNET_break_op (0);
  855. recv_error (recv);
  856. return GNUNET_SYSERR;
  857. }
  858. struct GNUNET_PSYC_MessageModifier *mod
  859. = (struct GNUNET_PSYC_MessageModifier *) pmsg;
  860. uint16_t name_size = ntohs (mod->name_size);
  861. recv->mod_value_size_expected = ntohl (mod->value_size);
  862. recv->mod_value_size = psize - sizeof (*mod) - name_size;
  863. if (psize < sizeof (*mod) + name_size
  864. || '\0' != *((char *) &mod[1] + name_size - 1)
  865. || recv->mod_value_size_expected < recv->mod_value_size)
  866. {
  867. LOG (GNUNET_ERROR_TYPE_WARNING, "Dropping malformed modifier.\n");
  868. GNUNET_break_op (0);
  869. recv_error (recv);
  870. return GNUNET_SYSERR;
  871. }
  872. recv->state = GNUNET_PSYC_MESSAGE_STATE_MODIFIER;
  873. break;
  874. }
  875. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MOD_CONT:
  876. {
  877. recv->mod_value_size += psize - sizeof (*pmsg);
  878. if (!(GNUNET_PSYC_MESSAGE_STATE_MODIFIER == recv->state
  879. || GNUNET_PSYC_MESSAGE_STATE_MOD_CONT == recv->state)
  880. || recv->mod_value_size_expected < recv->mod_value_size)
  881. {
  882. LOG (GNUNET_ERROR_TYPE_WARNING,
  883. "Dropping out of order message modifier continuation "
  884. "!(%u == %u || %u == %u) || %lu < %lu.\n",
  885. GNUNET_PSYC_MESSAGE_STATE_MODIFIER, recv->state,
  886. GNUNET_PSYC_MESSAGE_STATE_MOD_CONT, recv->state,
  887. recv->mod_value_size_expected, recv->mod_value_size);
  888. GNUNET_break_op (0);
  889. recv_error (recv);
  890. return GNUNET_SYSERR;
  891. }
  892. break;
  893. }
  894. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
  895. {
  896. if (recv->state < GNUNET_PSYC_MESSAGE_STATE_METHOD
  897. || recv->mod_value_size_expected != recv->mod_value_size)
  898. {
  899. LOG (GNUNET_ERROR_TYPE_WARNING,
  900. "Dropping out of order message data fragment "
  901. "(%u < %u || %lu != %lu).\n",
  902. recv->state, GNUNET_PSYC_MESSAGE_STATE_METHOD,
  903. recv->mod_value_size_expected, recv->mod_value_size);
  904. GNUNET_break_op (0);
  905. recv_error (recv);
  906. return GNUNET_SYSERR;
  907. }
  908. recv->state = GNUNET_PSYC_MESSAGE_STATE_DATA;
  909. break;
  910. }
  911. }
  912. if (NULL != recv->message_part_cb)
  913. recv->message_part_cb (recv->cb_cls, recv->message_id, 0, // FIXME: data_offset
  914. recv->flags, pmsg);
  915. switch (ptype)
  916. {
  917. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
  918. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL:
  919. GNUNET_PSYC_receive_reset (recv);
  920. break;
  921. }
  922. }
  923. if (NULL != recv->message_cb)
  924. recv->message_cb (recv->cb_cls, message_id, flags, msg);
  925. return GNUNET_OK;
  926. }
  927. /**
  928. * Check if @a data contains a series of valid message parts.
  929. *
  930. * @param data_size Size of @a data.
  931. * @param data Data.
  932. * @param[out] first_ptype Type of first message part.
  933. * @param[out] last_ptype Type of last message part.
  934. *
  935. * @return Number of message parts found in @a data.
  936. * or GNUNET_SYSERR if the message contains invalid parts.
  937. */
  938. int
  939. GNUNET_PSYC_receive_check_parts (uint16_t data_size, const char *data,
  940. uint16_t *first_ptype, uint16_t *last_ptype)
  941. {
  942. const struct GNUNET_MessageHeader *pmsg;
  943. uint16_t parts = 0, ptype = 0, psize = 0, pos = 0;
  944. if (NULL != first_ptype)
  945. *first_ptype = 0;
  946. if (NULL != last_ptype)
  947. *last_ptype = 0;
  948. for (pos = 0; pos < data_size; pos += psize, parts++)
  949. {
  950. pmsg = (const struct GNUNET_MessageHeader *) (data + pos);
  951. GNUNET_PSYC_log_message (GNUNET_ERROR_TYPE_DEBUG, pmsg);
  952. psize = ntohs (pmsg->size);
  953. ptype = ntohs (pmsg->type);
  954. if (0 == parts && NULL != first_ptype)
  955. *first_ptype = ptype;
  956. if (NULL != last_ptype
  957. && *last_ptype < GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END)
  958. *last_ptype = ptype;
  959. if (psize < sizeof (*pmsg)
  960. || pos + psize > data_size
  961. || ptype < GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD
  962. || GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_CANCEL < ptype)
  963. {
  964. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  965. "Invalid message part of type %u and size %u.\n",
  966. ptype, psize);
  967. return GNUNET_SYSERR;
  968. }
  969. /* FIXME: check message part order */
  970. }
  971. return parts;
  972. }
  973. struct ParseMessageClosure
  974. {
  975. struct GNUNET_ENV_Environment *env;
  976. const char **method_name;
  977. const void **data;
  978. uint16_t *data_size;
  979. enum GNUNET_PSYC_MessageState msg_state;
  980. };
  981. static void
  982. parse_message_part_cb (void *cls, uint64_t message_id, uint64_t data_offset,
  983. uint32_t flags, const struct GNUNET_MessageHeader *msg)
  984. {
  985. struct ParseMessageClosure *pmc = cls;
  986. if (NULL == msg)
  987. {
  988. pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_ERROR;
  989. return;
  990. }
  991. switch (ntohs (msg->type))
  992. {
  993. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_METHOD:
  994. {
  995. struct GNUNET_PSYC_MessageMethod *
  996. pmeth = (struct GNUNET_PSYC_MessageMethod *) msg;
  997. *pmc->method_name = (const char *) &pmeth[1];
  998. pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_METHOD;
  999. break;
  1000. }
  1001. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_MODIFIER:
  1002. {
  1003. struct GNUNET_PSYC_MessageModifier *
  1004. pmod = (struct GNUNET_PSYC_MessageModifier *) msg;
  1005. const char *name = (const char *) &pmod[1];
  1006. const void *value = name + pmod->name_size;
  1007. GNUNET_ENV_environment_add (pmc->env, pmod->oper, name, value,
  1008. pmod->value_size);
  1009. pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_MODIFIER;
  1010. break;
  1011. }
  1012. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_DATA:
  1013. *pmc->data = &msg[1];
  1014. *pmc->data_size = ntohs (msg->size) - sizeof (*msg);
  1015. pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_DATA;
  1016. break;
  1017. case GNUNET_MESSAGE_TYPE_PSYC_MESSAGE_END:
  1018. pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_END;
  1019. break;
  1020. default:
  1021. pmc->msg_state = GNUNET_PSYC_MESSAGE_STATE_ERROR;
  1022. }
  1023. }
  1024. /**
  1025. * Parse PSYC message.
  1026. *
  1027. * @param msg
  1028. * The PSYC message to parse.
  1029. * @param[out] method_name
  1030. * Pointer to the method name inside @a pmsg.
  1031. * @param env
  1032. * The environment for the message with a list of modifiers.
  1033. * @param[out] data
  1034. * Pointer to data inside @a pmsg.
  1035. * @param[out] data_size
  1036. * Size of @data is written here.
  1037. *
  1038. * @return #GNUNET_OK on success,
  1039. * #GNUNET_SYSERR on parse error.
  1040. */
  1041. int
  1042. GNUNET_PSYC_message_parse (const struct GNUNET_PSYC_Message *msg,
  1043. const char **method_name,
  1044. struct GNUNET_ENV_Environment *env,
  1045. const void **data,
  1046. uint16_t *data_size)
  1047. {
  1048. struct ParseMessageClosure cls;
  1049. cls.env = env;
  1050. cls.method_name = method_name;
  1051. cls.data = data;
  1052. cls.data_size = data_size;
  1053. uint16_t msg_size = ntohs (msg->header.size);
  1054. struct GNUNET_PSYC_MessageHeader *
  1055. pmsg = GNUNET_malloc (sizeof (*pmsg) + msg_size - sizeof (*msg));
  1056. memcpy (&pmsg[1], &msg[1], msg_size - sizeof (*msg));
  1057. struct GNUNET_PSYC_ReceiveHandle *
  1058. recv = GNUNET_PSYC_receive_create (NULL, &parse_message_part_cb, &cls);
  1059. GNUNET_PSYC_receive_message (recv, pmsg);
  1060. GNUNET_PSYC_receive_destroy (recv);
  1061. GNUNET_free (pmsg);
  1062. return (GNUNET_PSYC_MESSAGE_STATE_END == cls.msg_state)
  1063. ? GNUNET_OK
  1064. : GNUNET_SYSERR;
  1065. }