gnunet_sensor_util_lib.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C)
  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., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @author Omar Tarabai
  19. *
  20. * @file
  21. * Sensor utilities
  22. *
  23. * @defgroup sensor Sensor Utilities library
  24. *
  25. * @{
  26. */
  27. #ifndef GNUNET_SENSOR_UTIL_LIB_H
  28. #define GNUNET_SENSOR_UTIL_LIB_H
  29. #ifdef __cplusplus
  30. extern "C"
  31. {
  32. #if 0 /* keep Emacsens' auto-indent happy */
  33. }
  34. #endif
  35. #endif
  36. /**
  37. * Structure containing sensor definition
  38. */
  39. struct GNUNET_SENSOR_SensorInfo
  40. {
  41. /**
  42. * The configuration handle
  43. * carrying sensor information
  44. */
  45. struct GNUNET_CONFIGURATION_Handle *cfg;
  46. /**
  47. * Sensor name
  48. */
  49. char *name;
  50. /**
  51. * Path to definition file
  52. */
  53. char *def_file;
  54. /**
  55. * First part of version number
  56. */
  57. uint16_t version_major;
  58. /**
  59. * Second part of version number
  60. */
  61. uint16_t version_minor;
  62. /**
  63. * Sensor description
  64. */
  65. char *description;
  66. /**
  67. * Sensor currently enabled
  68. */
  69. int enabled;
  70. /**
  71. * Category under which the sensor falls (e.g. tcp, datastore)
  72. */
  73. char *category;
  74. /**
  75. * When does the sensor become active
  76. */
  77. struct GNUNET_TIME_Absolute *start_time;
  78. /**
  79. * When does the sensor expire
  80. */
  81. struct GNUNET_TIME_Absolute *end_time;
  82. /**
  83. * Time interval to collect sensor information (e.g. every 1 min)
  84. */
  85. struct GNUNET_TIME_Relative interval;
  86. /**
  87. * Lifetime of an information sample after which it is deleted from storage
  88. * If not supplied, will default to the interval value
  89. */
  90. struct GNUNET_TIME_Relative lifetime;
  91. /**
  92. * A set of required peer capabilities for the sensor to collect meaningful information (e.g. ipv6)
  93. */
  94. char *capabilities;
  95. /**
  96. * Either "gnunet-statistics" or external "process"
  97. */
  98. char *source;
  99. /**
  100. * Name of the GNUnet service that is the source for the gnunet-statistics entry
  101. */
  102. char *gnunet_stat_service;
  103. /**
  104. * Name of the gnunet-statistics entry
  105. */
  106. char *gnunet_stat_name;
  107. /**
  108. * Handle to statistics get request (OR NULL)
  109. */
  110. struct GNUNET_STATISTICS_GetHandle *gnunet_stat_get_handle;
  111. /**
  112. * Name of the external process to be executed
  113. */
  114. char *ext_process;
  115. /**
  116. * Arguments to be passed to the external process
  117. */
  118. char *ext_args;
  119. /**
  120. * Handle to the external process
  121. */
  122. struct GNUNET_OS_CommandHandle *ext_cmd;
  123. /**
  124. * Did we already receive a value
  125. * from the currently running external
  126. * proccess ? #GNUNET_YES / #GNUNET_NO
  127. */
  128. int ext_cmd_value_received;
  129. /**
  130. * The output datatype to be expected
  131. */
  132. char *expected_datatype;
  133. /**
  134. * Peer-identity of peer running collection point
  135. */
  136. struct GNUNET_PeerIdentity *collection_point;
  137. /**
  138. * Do we report received sensor values to collection point?
  139. * #GNUNET_YES / #GNUNET_NO
  140. */
  141. int report_values;
  142. /**
  143. * Time interval to send sensor values to collection point (e.g. every 30 mins)
  144. */
  145. struct GNUNET_TIME_Relative value_reporting_interval;
  146. /**
  147. * Do we report anomalies to collection point?
  148. * #GNUNET_YES / #GNUNET_NO
  149. */
  150. int report_anomalies;
  151. /**
  152. * Execution task (OR NULL)
  153. */
  154. struct GNUNET_SCHEDULER_Task * execution_task;
  155. /**
  156. * Is the sensor being executed
  157. */
  158. int running;
  159. };
  160. /**
  161. * Anomaly report received and stored by sensor dashboard.
  162. * Sensor name and peer id are not included because they are part of the
  163. * peerstore key.
  164. */
  165. struct GNUNET_SENSOR_DashboardAnomalyEntry
  166. {
  167. /**
  168. * New anomaly status
  169. */
  170. uint16_t anomalous;
  171. /**
  172. * Percentage of neighbors reported the same anomaly
  173. */
  174. float anomalous_neighbors;
  175. };
  176. GNUNET_NETWORK_STRUCT_BEGIN
  177. /**
  178. * Used to communicate brief information about a sensor.
  179. */
  180. struct GNUNET_SENSOR_SensorBriefMessage
  181. {
  182. /**
  183. * GNUNET general message header.
  184. */
  185. struct GNUNET_MessageHeader header;
  186. /**
  187. * Size of sensor name string, allocated at position 0 after this struct.
  188. */
  189. uint16_t name_size;
  190. /**
  191. * First part of sensor version number
  192. */
  193. uint16_t version_major;
  194. /**
  195. * Second part of sensor version number
  196. */
  197. uint16_t version_minor;
  198. };
  199. /**
  200. * Used to communicate full information about a sensor.
  201. */
  202. struct GNUNET_SENSOR_SensorFullMessage
  203. {
  204. /**
  205. * GNUNET general message header.
  206. */
  207. struct GNUNET_MessageHeader header;
  208. /**
  209. * Size of sensor name.
  210. * Name allocated at position 0 after this struct.
  211. */
  212. uint16_t sensorname_size;
  213. /**
  214. * Size of the sensor definition file carrying full sensor information.
  215. * The file content allocated at position 1 after this struct.
  216. */
  217. uint16_t sensorfile_size;
  218. /**
  219. * Name of the file (usually script) associated with this sensor.
  220. * At the moment we only support having one file per sensor.
  221. * The file name is allocated at position 2 after this struct.
  222. */
  223. uint16_t scriptname_size;
  224. /**
  225. * Size of the file (usually script) associated with this sensor.
  226. * The file content is allocated at position 3 after this struct.
  227. */
  228. uint16_t scriptfile_size;
  229. };
  230. /**
  231. * Used to communicate sensor values to
  232. * collection points (SENSORDASHBAORD service)
  233. */
  234. struct GNUNET_SENSOR_ValueMessage
  235. {
  236. /**
  237. * GNUNET general message header
  238. */
  239. struct GNUNET_MessageHeader header;
  240. /**
  241. * Hash of sensor name
  242. */
  243. struct GNUNET_HashCode sensorname_hash;
  244. /**
  245. * First part of sensor version number
  246. */
  247. uint16_t sensorversion_major;
  248. /**
  249. * Second part of sensor version number
  250. */
  251. uint16_t sensorversion_minor;
  252. /**
  253. * Timestamp of recorded reading
  254. */
  255. struct GNUNET_TIME_Absolute timestamp;
  256. /**
  257. * Size of sensor value, allocated at poistion 0 after this struct
  258. */
  259. uint16_t value_size;
  260. };
  261. /**
  262. * Message carrying an anomaly status change report
  263. */
  264. struct GNUNET_SENSOR_AnomalyReportMessage
  265. {
  266. /**
  267. * Hash of sensor name
  268. */
  269. struct GNUNET_HashCode sensorname_hash;
  270. /**
  271. * First part of sensor version number
  272. */
  273. uint16_t sensorversion_major;
  274. /**
  275. * Second part of sensor version name
  276. */
  277. uint16_t sensorversion_minor;
  278. /**
  279. * New anomaly status
  280. */
  281. uint16_t anomalous;
  282. /**
  283. * Percentage of neighbors reported the same anomaly
  284. */
  285. float anomalous_neighbors;
  286. };
  287. GNUNET_NETWORK_STRUCT_END
  288. /**
  289. * Given two version numbers as major and minor, compare them.
  290. *
  291. * @param v1_major First part of first version number
  292. * @param v1_minor Second part of first version number
  293. * @param v2_major First part of second version number
  294. * @param v2_minor Second part of second version number
  295. */
  296. int
  297. GNUNET_SENSOR_version_compare (uint16_t v1_major, uint16_t v1_minor,
  298. uint16_t v2_major, uint16_t v2_minor);
  299. /**
  300. * Reads sensor definitions from given sensor directory.
  301. *
  302. * @param sensordir Path to sensor directory.
  303. * @return a multihashmap of loaded sensors
  304. */
  305. struct GNUNET_CONTAINER_MultiHashMap *
  306. GNUNET_SENSOR_load_all_sensors (char *sensor_dir);
  307. /**
  308. * Get path to the default directory containing the sensor definition files with
  309. * a trailing directory separator.
  310. *
  311. * @return Default sensor files directory full path
  312. */
  313. char *
  314. GNUNET_SENSOR_get_default_sensor_dir ();
  315. /**
  316. * Destroys a group of sensors in a hashmap and the hashmap itself
  317. *
  318. * @param sensors hashmap containing the sensors
  319. */
  320. void
  321. GNUNET_SENSOR_destroy_sensors (struct GNUNET_CONTAINER_MultiHashMap *sensors);
  322. struct GNUNET_SENSOR_crypto_pow_context;
  323. /**
  324. * Block carrying arbitrary data + its proof-of-work + signature
  325. */
  326. struct GNUNET_SENSOR_crypto_pow_block
  327. {
  328. /**
  329. * Proof-of-work value
  330. */
  331. uint64_t pow;
  332. /**
  333. * Data signature
  334. */
  335. struct GNUNET_CRYPTO_EddsaSignature signature;
  336. /**
  337. * Size of the msg component (allocated after this struct)
  338. */
  339. size_t msg_size;
  340. /**
  341. * Purpose of signing.
  342. * Data is allocated after this (timestamp, public_key, msg).
  343. */
  344. struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
  345. /**
  346. * First part of data - timestamp
  347. */
  348. struct GNUNET_TIME_Absolute timestamp;
  349. /**
  350. * Second part of data - Public key
  351. */
  352. struct GNUNET_CRYPTO_EddsaPublicKey public_key;
  353. };
  354. /**
  355. * Continuation called with a status result.
  356. *
  357. * @param cls closure
  358. * @param pow Proof-of-work value
  359. * @param purpose Signed block (size, purpose, data)
  360. * @param signature Signature, NULL on error
  361. */
  362. typedef void (*GNUNET_SENSOR_UTIL_pow_callback) (void *cls,
  363. struct
  364. GNUNET_SENSOR_crypto_pow_block
  365. * block);
  366. /**
  367. * Cancel an operation started by #GNUNET_SENSOR_crypto_pow_sign().
  368. * Call only before callback function passed to #GNUNET_SENSOR_crypto_pow_sign()
  369. * is called with the result.
  370. */
  371. void
  372. GNUNET_SENSOR_crypto_pow_sign_cancel (struct GNUNET_SENSOR_crypto_pow_context
  373. *cx);
  374. /**
  375. * Calculate proof-of-work and sign a message.
  376. *
  377. * @param msg Message to calculate pow and sign
  378. * @param msg_size size of msg
  379. * @param timestamp Timestamp to add to the message to protect against replay attacks
  380. * @param public_key Public key of the origin peer, to protect against redirect attacks
  381. * @param private_key Private key of the origin peer to sign the result
  382. * @param matching_bits Number of leading zeros required in the result hash
  383. * @param callback Callback function to call with the result
  384. * @param callback_cls Closure for callback
  385. * @return Operation context
  386. */
  387. struct GNUNET_SENSOR_crypto_pow_context *
  388. GNUNET_SENSOR_crypto_pow_sign (void *msg, size_t msg_size,
  389. struct GNUNET_TIME_Absolute *timestamp,
  390. struct GNUNET_CRYPTO_EddsaPublicKey *public_key,
  391. struct GNUNET_CRYPTO_EddsaPrivateKey
  392. *private_key, int matching_bits,
  393. GNUNET_SENSOR_UTIL_pow_callback callback,
  394. void *callback_cls);
  395. /**
  396. * Verify that proof-of-work and signature in the given block are valid.
  397. * If all valid, a pointer to the payload within the block is set and the size
  398. * of the payload is returned.
  399. *
  400. * **VERY IMPORTANT** : You will still need to verify the timestamp yourself.
  401. *
  402. * @param block The block received and needs to be verified
  403. * @param matching_bits Number of leading zeros in the hash used to verify pow
  404. * @param public_key Public key of the peer that sent this block
  405. * @param payload Where to store the pointer to the payload
  406. * @return Size of the payload
  407. */
  408. size_t
  409. GNUNET_SENSOR_crypto_verify_pow_sign (struct GNUNET_SENSOR_crypto_pow_block *
  410. block, int matching_bits,
  411. struct GNUNET_CRYPTO_EddsaPublicKey *
  412. public_key, void **payload);
  413. #if 0 /* keep Emacsens' auto-indent happy */
  414. {
  415. #endif
  416. #ifdef __cplusplus
  417. }
  418. #endif
  419. /* ifndef GNUNET_SENSOR_UTIL_LIB_H */
  420. #endif
  421. /** @} */ /* end of group */
  422. /* end of gnunet_sensor_util_lib.h */