s5.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Copyright StrongLoop, Inc. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #ifndef S5_H_
  22. #define S5_H_
  23. #include <stddef.h>
  24. #include <stdint.h>
  25. #define S5_ERR_MAP(V) \
  26. V(-1, bad_version, "Bad protocol version.") \
  27. V(-2, bad_cmd, "Bad protocol command.") \
  28. V(-3, bad_atyp, "Bad address type.") \
  29. V(0, ok, "No error.") \
  30. V(1, auth_select, "Select authentication method.") \
  31. V(2, auth_verify, "Verify authentication.") \
  32. V(3, exec_cmd, "Execute command.") \
  33. typedef enum {
  34. #define S5_ERR_GEN(code, name, _) s5_ ## name = code,
  35. S5_ERR_MAP(S5_ERR_GEN)
  36. #undef S5_ERR_GEN
  37. s5_max_errors
  38. } s5_err;
  39. typedef enum {
  40. S5_AUTH_NONE = 1 << 0,
  41. S5_AUTH_GSSAPI = 1 << 1,
  42. S5_AUTH_PASSWD = 1 << 2
  43. } s5_auth_method;
  44. typedef enum {
  45. s5_auth_allow,
  46. s5_auth_deny
  47. } s5_auth_result;
  48. typedef enum {
  49. s5_atyp_ipv4,
  50. s5_atyp_ipv6,
  51. s5_atyp_host
  52. } s5_atyp;
  53. typedef enum {
  54. s5_cmd_tcp_connect,
  55. s5_cmd_tcp_bind,
  56. s5_cmd_udp_assoc
  57. } s5_cmd;
  58. typedef struct {
  59. uint32_t arg0; /* Scratch space for the state machine. */
  60. uint32_t arg1; /* Scratch space for the state machine. */
  61. uint8_t state;
  62. uint8_t methods;
  63. uint8_t cmd;
  64. uint8_t atyp;
  65. uint8_t userlen;
  66. uint8_t passlen;
  67. uint16_t dport;
  68. uint8_t username[257];
  69. uint8_t password[257];
  70. uint8_t daddr[257]; /* TODO(bnoordhuis) Merge with username/password. */
  71. } s5_ctx;
  72. void s5_init(s5_ctx *ctx);
  73. s5_err s5_parse(s5_ctx *cx, uint8_t **data, size_t *size);
  74. /* Only call after s5_parse() has returned s5_want_auth_method. */
  75. unsigned int s5_auth_methods(const s5_ctx *cx);
  76. /* Call after s5_parse() has returned s5_want_auth_method. */
  77. int s5_select_auth(s5_ctx *cx, s5_auth_method method);
  78. const char *s5_strerror(s5_err err);
  79. #endif /* S5_H_ */