luat_netdrv_openvpn_client.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma once
  2. #include <stddef.h>
  3. #include <stdint.h>
  4. #include "lwip/ip_addr.h"
  5. #include "lwip/netif.h"
  6. #include "lwip/pbuf.h"
  7. #include "lwip/udp.h"
  8. #include "mbedtls/ssl.h"
  9. #include "mbedtls/x509_crt.h"
  10. #include "mbedtls/pk.h"
  11. #include "mbedtls/ctr_drbg.h"
  12. #include "mbedtls/entropy.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /* Event types for OpenVPN client state */
  17. typedef enum {
  18. OVPN_EVENT_CONNECTED = 0, /* Connection established (static key mode) */
  19. OVPN_EVENT_TLS_HANDSHAKE_OK, /* TLS/DTLS handshake succeeded */
  20. OVPN_EVENT_TLS_HANDSHAKE_FAIL, /* TLS/DTLS handshake failed */
  21. OVPN_EVENT_KEEPALIVE_TIMEOUT, /* Keepalive timeout (30s no response) */
  22. OVPN_EVENT_AUTH_FAILED, /* HMAC authentication failed */
  23. OVPN_EVENT_DISCONNECTED, /* Connection closed */
  24. OVPN_EVENT_DATA_RX, /* Data packet received (optional, for activity indication) */
  25. OVPN_EVENT_DATA_TX, /* Data packet sent (optional, for activity indication) */
  26. } ovpn_event_t;
  27. /* Event callback function type */
  28. typedef void (*ovpn_event_cb_t)(ovpn_event_t event, void *user_data);
  29. typedef struct {
  30. const char *remote_host; // optional; if NULL, remote_ip must be set
  31. ip_addr_t remote_ip; // required for now (UDP only)
  32. uint16_t remote_port; // server port
  33. uint8_t adapter_index; // defaults to NW_ADAPTER_INDEX_LWIP_USER0
  34. uint16_t tun_mtu; // defaults to 1500
  35. const uint8_t *static_key; // optional static key material
  36. size_t static_key_len; // up to 64 bytes stored
  37. const char *ca_cert_pem; // CA certificate (PEM)
  38. size_t ca_cert_len;
  39. const char *client_cert_pem; // client certificate (PEM)
  40. size_t client_cert_len;
  41. const char *client_key_pem; // client private key (PEM)
  42. size_t client_key_len;
  43. ovpn_event_cb_t event_cb; // Event callback function (optional)
  44. void *user_data; // User-defined data passed to callback
  45. } ovpn_client_cfg_t;
  46. typedef struct {
  47. uint64_t tx_pkts;
  48. uint64_t tx_bytes;
  49. uint64_t rx_pkts;
  50. uint64_t rx_bytes;
  51. uint64_t drop_auth;
  52. uint64_t drop_replay;
  53. uint64_t drop_malformed;
  54. uint64_t ping_sent;
  55. uint64_t ping_recv;
  56. } ovpn_client_stats_t;
  57. typedef struct ovpn_client {
  58. struct netif netif;
  59. struct udp_pcb *udp;
  60. ip_addr_t remote_ip;
  61. uint16_t remote_port;
  62. uint16_t mtu;
  63. uint8_t adapter_index;
  64. uint8_t started;
  65. uint32_t tx_seq;
  66. uint32_t rx_max_seq;
  67. uint32_t rx_window;
  68. uint8_t rx_initialized;
  69. uint32_t last_activity_ms;
  70. uint32_t last_ping_ms;
  71. uint8_t key[64]; /* OVPN_MAX_KEY_LEN */
  72. size_t key_len;
  73. ovpn_client_stats_t stats;
  74. uint8_t debug;
  75. uint8_t use_tls;
  76. uint8_t tls_ready;
  77. mbedtls_ssl_context ssl;
  78. mbedtls_ssl_config conf;
  79. mbedtls_x509_crt ca;
  80. mbedtls_x509_crt client_cert;
  81. mbedtls_pk_context client_key;
  82. mbedtls_ctr_drbg_context drbg;
  83. mbedtls_entropy_context entropy;
  84. struct {
  85. struct pbuf *pkt; /* pending UDP packet for DTLS read */
  86. uint16_t offset;
  87. } rx_pending;
  88. struct {
  89. uint32_t int_ms;
  90. uint32_t fin_ms;
  91. } dtls_timer;
  92. uint8_t *tls_buf; /* Pre-allocated TLS temporary buffer (1600 bytes) */
  93. ovpn_event_cb_t event_cb; /* Event callback function */
  94. void *user_data; /* User-defined data */
  95. /* Certificate data copies (copied from Lua stack to prevent garbage collection) */
  96. uint8_t *ca_cert_buf; /* CA certificate copy */
  97. size_t ca_cert_len;
  98. uint8_t *client_cert_buf; /* Client certificate copy */
  99. size_t client_cert_len;
  100. uint8_t *client_key_buf; /* Client private key copy */
  101. size_t client_key_len;
  102. } ovpn_client_t;
  103. int ovpn_client_init(ovpn_client_t *cli, const ovpn_client_cfg_t *cfg);
  104. int ovpn_client_start(ovpn_client_t *cli);
  105. void ovpn_client_stop(ovpn_client_t *cli);
  106. void ovpn_client_get_stats(ovpn_client_t *cli, ovpn_client_stats_t *out);
  107. void ovpn_client_set_debug(ovpn_client_t *cli, int enable);
  108. #ifdef __cplusplus
  109. }
  110. #endif