netpacket.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #ifndef NETPACKET_H_
  2. #define NETPACKET_H_
  3. #include <stdint.h>
  4. #include "crypto.h"
  5. #include "netproto.h"
  6. /* Internal netpacket cookie. */
  7. typedef struct netpacket_internal NETPACKET_CONNECTION;
  8. /* Function for sending a request once a connection is established. */
  9. typedef int sendpacket_callback(void *, NETPACKET_CONNECTION *);
  10. /* Function for handling a response packet. */
  11. typedef int handlepacket_callback(void *, NETPACKET_CONNECTION *,
  12. int, uint8_t, const uint8_t *, size_t);
  13. /* packet types. */
  14. #define NETPACKET_REGISTER_REQUEST 0x00
  15. #define NETPACKET_REGISTER_CHALLENGE 0x80
  16. #define NETPACKET_REGISTER_CHA_RESPONSE 0x01
  17. #define NETPACKET_REGISTER_RESPONSE 0x81
  18. #define NETPACKET_TRANSACTION_GETNONCE 0x10
  19. #define NETPACKET_TRANSACTION_GETNONCE_RESPONSE 0x90
  20. #define NETPACKET_TRANSACTION_START 0x11
  21. #define NETPACKET_TRANSACTION_START_RESPONSE 0x91
  22. #define NETPACKET_TRANSACTION_COMMIT 0x12
  23. #define NETPACKET_TRANSACTION_COMMIT_RESPONSE 0x92
  24. #define NETPACKET_TRANSACTION_CHECKPOINT 0x13
  25. #define NETPACKET_TRANSACTION_CHECKPOINT_RESPONSE 0x93
  26. #define NETPACKET_TRANSACTION_CANCEL 0x14
  27. #define NETPACKET_TRANSACTION_CANCEL_RESPONSE 0x94
  28. #define NETPACKET_TRANSACTION_TRYCOMMIT 0x15
  29. #define NETPACKET_TRANSACTION_TRYCOMMIT_RESPONSE 0x95
  30. #define NETPACKET_TRANSACTION_ISCHECKPOINTED 0x16
  31. #define NETPACKET_TRANSACTION_ISCHECKPOINTED_RESPONSE 0x96
  32. #define NETPACKET_WRITE_FEXIST 0x20
  33. #define NETPACKET_WRITE_FEXIST_RESPONSE 0xa0
  34. #define NETPACKET_WRITE_FILE 0x21
  35. #define NETPACKET_WRITE_FILE_RESPONSE 0xa1
  36. #define NETPACKET_DELETE_FILE 0x30
  37. #define NETPACKET_DELETE_FILE_RESPONSE 0xb0
  38. #define NETPACKET_READ_FILE 0x40
  39. #define NETPACKET_READ_FILE_RESPONSE 0xc0
  40. #define NETPACKET_DIRECTORY 0x50
  41. #define NETPACKET_DIRECTORY_D 0x51
  42. #define NETPACKET_DIRECTORY_RESPONSE 0xd0
  43. /* Maximum number of files listed in a NETPACKET_DIRECTORY_RESPONSE packet. */
  44. #define NETPACKET_DIRECTORY_RESPONSE_MAXFILES 8000
  45. /**
  46. * netpacket_hmac_verify(type, nonce, packetbuf, pos, key):
  47. * Verify that HMAC(type || nonce || packetbuf[0 .. pos - 1]) using the
  48. * specified key matches packetbuf[pos .. pos + 31]. If nonce is NULL, omit
  49. * it from the data being HMACed as appropriate. Return -1 on error, 0 on
  50. * success, or 1 if the hash does not match.
  51. */
  52. int netpacket_hmac_verify(uint8_t, const uint8_t[32], const uint8_t *,
  53. size_t, int);
  54. /**
  55. * netpacket_register_request(NPC, user, callback):
  56. * Construct and send a NETPACKET_REGISTER_REQUEST packet asking to register
  57. * a new machine belonging to the specified user.
  58. */
  59. int netpacket_register_request(NETPACKET_CONNECTION *, const char *,
  60. handlepacket_callback *);
  61. /**
  62. * netpacket_register_cha_response(NPC, keys, name, register_key, callback):
  63. * Construct and send a NETPACKET_REGISTER_CHA_RESPONSE packet providing the
  64. * given access ${keys} and user-friendly ${name}, signed using the shared key
  65. * ${register_key} computed by hashing the Diffie-Hellman shared secret K.
  66. */
  67. int netpacket_register_cha_response(NETPACKET_CONNECTION *,
  68. const uint8_t[96], const char *, const uint8_t[32],
  69. handlepacket_callback *);
  70. /**
  71. * netpacket_transaction_getnonce(NPC, machinenum, callback):
  72. * Construct and send a NETPACKET_TRANSACTION_GETNONCE packet asking to get
  73. * a transaction server nonce.
  74. */
  75. int netpacket_transaction_getnonce(NETPACKET_CONNECTION *, uint64_t,
  76. handlepacket_callback *);
  77. /**
  78. * netpacket_transaction_start(NPC, machinenum, operation, snonce, cnonce,
  79. * state, callback):
  80. * Construct and send a NETPACKET_TRANSACTION_START packet asking to
  81. * start a transaction; the transaction is a write transaction if
  82. * ${operation} is 0, a delete transaction if ${operation} is 1, a fsck
  83. * transaction with the delete key if ${operation} is 2, or a fsck
  84. * transaction with the write key if ${operation} is 3.
  85. */
  86. int netpacket_transaction_start(NETPACKET_CONNECTION *, uint64_t,
  87. uint8_t, const uint8_t[32], const uint8_t[32], const uint8_t[32],
  88. handlepacket_callback *);
  89. /**
  90. * netpacket_transaction_commit(NPC, machinenum, whichkey, nonce, callback):
  91. * Construct and send a NETPACKET_TRANSACTION_COMMIT packet asking to commit
  92. * a transaction; the packet is signed with the write access key if
  93. * ${whichkey} is 0, and with the delete access key if ${whichkey} is 1.
  94. */
  95. int netpacket_transaction_commit(NETPACKET_CONNECTION *, uint64_t,
  96. uint8_t, const uint8_t[32], handlepacket_callback *);
  97. /**
  98. * netpacket_transaction_checkpoint(NPC, machinenum, whichkey, ckptnonce,
  99. * nonce, callback):
  100. * Construct and send a NETPACKET_TRANSACTION_CHECKPOINT packet asking to
  101. * create a checkpoint in a write transaction.
  102. */
  103. int netpacket_transaction_checkpoint(NETPACKET_CONNECTION *, uint64_t,
  104. uint8_t, const uint8_t[32], const uint8_t[32], handlepacket_callback *);
  105. /**
  106. * netpacket_transaction_cancel(NPC, machinenum, whichkey, snonce, cnonce,
  107. * state, callback):
  108. * Construct and send a NETPACKET_TRANSACTION_CANCEL packet asking to cancel
  109. * a pending transaction if the state is correct.
  110. */
  111. int netpacket_transaction_cancel(NETPACKET_CONNECTION *, uint64_t,
  112. uint8_t, const uint8_t[32], const uint8_t[32], const uint8_t[32],
  113. handlepacket_callback *);
  114. /**
  115. * netpacket_transaction_trycommit(NPC, machinenum, whichkey, nonce,
  116. * callback):
  117. * Construct and send a NETPACKET_TRANSACTION_TRYCOMMIT packet asking to
  118. * commit a transaction; the packet is signed with the write access key if
  119. * ${whichkey} is 0, and with the delete access key if ${whichkey} is 1.
  120. */
  121. int netpacket_transaction_trycommit(NETPACKET_CONNECTION *, uint64_t,
  122. uint8_t, const uint8_t[32], handlepacket_callback *);
  123. /**
  124. * netpacket_transaction_ischeckpointed(NPC, machinenum, whichkey, nonce,
  125. * callback):
  126. * Construct and send a NETPACKET_TRANSACTION_ISCHECKPOINTED packet asking if
  127. * a checkpointed write transaction is in progress; the packet is signed with
  128. * the write access key if ${whichkey} is 0, and with the delete access key
  129. * if ${whichkey} is 1.
  130. */
  131. int netpacket_transaction_ischeckpointed(NETPACKET_CONNECTION *, uint64_t,
  132. uint8_t, const uint8_t[32], handlepacket_callback *);
  133. /**
  134. * netpacket_write_fexist(NPC, machinenum, class, name, nonce, callback):
  135. * Construct and send a NETPACKET_WRITE_FEXIST packet asking if the
  136. * specified file exists.
  137. */
  138. int netpacket_write_fexist(NETPACKET_CONNECTION *, uint64_t, uint8_t,
  139. const uint8_t[32], const uint8_t[32], handlepacket_callback *);
  140. /**
  141. * netpacket_write_file(NPC, machinenum, class, name, buf, buflen,
  142. * nonce, callback):
  143. * Construct and send a NETPACKET_WRITE_FILE packet asking to write the
  144. * specified file.
  145. */
  146. int netpacket_write_file(NETPACKET_CONNECTION *, uint64_t, uint8_t,
  147. const uint8_t[32], const uint8_t *, size_t,
  148. const uint8_t[32], handlepacket_callback *);
  149. /**
  150. * netpacket_delete_file(NPC, machinenum, class, name, nonce, callback):
  151. * Construct and send a NETPACKET_DELETE_FILE packet asking to delete the
  152. * specified file.
  153. */
  154. int netpacket_delete_file(NETPACKET_CONNECTION *, uint64_t, uint8_t,
  155. const uint8_t[32], const uint8_t[32], handlepacket_callback *);
  156. /**
  157. * netpacket_read_file(NPC, machinenum, class, name, size, callback):
  158. * Construct and send a NETPACKET_READ_FILE packet asking to read the
  159. * specified file, which should be ${size} (<= 262144) bytes long if ${size}
  160. * is not (uint32_t)(-1).
  161. */
  162. int netpacket_read_file(NETPACKET_CONNECTION *, uint64_t, uint8_t,
  163. const uint8_t[32], uint32_t, handlepacket_callback *);
  164. /**
  165. * netpacket_directory(NPC, machinenum, class, start, snonce, cnonce, key,
  166. * callback):
  167. * Construct and send a NETPACKET_DIRECTORY packet (if key == 0) or
  168. * NETPACKET_DIRECTORY_D packet (otherwise) asking for a list of files
  169. * of the specified class starting from the specified position.
  170. */
  171. int netpacket_directory(NETPACKET_CONNECTION *, uint64_t, uint8_t,
  172. const uint8_t[32], const uint8_t[32], const uint8_t[32], int,
  173. handlepacket_callback *);
  174. /**
  175. * netpacket_directory_readmore(NPC, callback):
  176. * Read more NETPACKET_DIRECTORY_RESPONSE packets.
  177. */
  178. int netpacket_directory_readmore(NETPACKET_CONNECTION *,
  179. handlepacket_callback *);
  180. /**
  181. * netpacket_open(useragent):
  182. * Return a netpacket connection cookie.
  183. */
  184. NETPACKET_CONNECTION * netpacket_open(const char *);
  185. /**
  186. * netpacket_op(NPC, writepacket, cookie):
  187. * Call ${writepacket} to send a request to the server over the provided
  188. * netpacket connection.
  189. */
  190. int netpacket_op(NETPACKET_CONNECTION *, sendpacket_callback *, void *);
  191. /**
  192. * netpacket_getstats(NPC, in, out, queued):
  193. * Obtain the number of bytes received and sent via the connection, and the
  194. * number of bytes ${queued} to be written.
  195. */
  196. void netpacket_getstats(NETPACKET_CONNECTION *, uint64_t *, uint64_t *,
  197. uint64_t *);
  198. /**
  199. * netpacket_close(NPC):
  200. * Close a netpacket connection.
  201. */
  202. int netpacket_close(NETPACKET_CONNECTION *);
  203. #endif /* !NETPACKET_H_ */