RPC.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef __RPCLIB_H_
  2. #define __RPCLIB_H_
  3. #include <stdint.h>
  4. #define CANARY_SZ sizeof(uint64_t)
  5. #define PADDING_SZ 12
  6. #define PADDING 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89
  7. #define RPC_PHRASE "zerotier\0"
  8. #define RPC_PHRASE_SZ 9
  9. #define RPC_TIMESTAMP_SZ 20
  10. // 1st RPC section (metdata)
  11. #define IDX_SIGNAL_PHRASE 0
  12. #define IDX_PID IDX_SIGNAL_PHRASE + RPC_PHRASE_SZ
  13. #define IDX_TID sizeof(pid_t) + IDX_PID
  14. #define IDX_COUNT IDX_TID + sizeof(pid_t)
  15. #define IDX_TIME IDX_COUNT + sizeof(int)
  16. #define IDX_PAYLOAD IDX_TIME + RPC_TIMESTAMP_SZ
  17. // 2nd RPC section (payload and canary)
  18. #define CMD_ID_IDX 0
  19. #define CANARY_IDX 1
  20. #define STRUCT_IDX CANARY_IDX+CANARY_SZ
  21. #define BUF_SZ 512
  22. #define ERR_OK 0
  23. /* RPC codes */
  24. #define RPC_UNDEFINED 0
  25. #define RPC_CONNECT 1
  26. #define RPC_CONNECT_SOCKARG 2
  27. #define RPC_CLOSE 3
  28. #define RPC_READ 4
  29. #define RPC_WRITE 5
  30. #define RPC_BIND 6
  31. #define RPC_ACCEPT 7
  32. #define RPC_LISTEN 8
  33. #define RPC_SOCKET 9
  34. #define RPC_SHUTDOWN 10
  35. #define RPC_GETSOCKNAME 11
  36. #define RPC_RETVAL 12
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. int get_retval(int);
  41. int rpc_join(const char * sockname);
  42. int rpc_send_command(char *path, int cmd, int forfd, void *data, int len);
  43. int get_new_fd(int sock);
  44. ssize_t sock_fd_write(int sock, int fd);
  45. ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd);
  46. void rpc_mutex_destroy();
  47. void rpc_mutex_init();
  48. /* Structures used for sending commands via RPC mechanism */
  49. struct bind_st {
  50. int sockfd;
  51. struct sockaddr_storage addr;
  52. socklen_t addrlen;
  53. int __tid;
  54. };
  55. struct connect_st {
  56. int __fd;
  57. struct sockaddr_storage __addr;
  58. socklen_t __len;
  59. int __tid;
  60. };
  61. struct close_st {
  62. int fd;
  63. };
  64. struct listen_st {
  65. int sockfd;
  66. int backlog;
  67. int __tid;
  68. };
  69. struct socket_st {
  70. int socket_family;
  71. int socket_type;
  72. int protocol;
  73. int __tid;
  74. };
  75. struct accept_st {
  76. int sockfd;
  77. struct sockaddr_storage addr;
  78. socklen_t addrlen;
  79. int __tid;
  80. };
  81. struct shutdown_st {
  82. int socket;
  83. int how;
  84. };
  85. struct getsockname_st {
  86. int sockfd;
  87. struct sockaddr_storage addr;
  88. socklen_t addrlen;
  89. };
  90. #ifdef __cplusplus
  91. }
  92. #endif
  93. #endif