diff options
author | Thomas Gschwantner <tharre3@gmail.com> | 2019-09-25 18:13:13 +0200 |
---|---|---|
committer | Thomas Gschwantner <tharre3@gmail.com> | 2019-12-11 06:22:17 +0100 |
commit | 1b5a1c72c734d03ae45b5eae1a18978fa110d14a (patch) | |
tree | 68de2e816e023aab955be51310a9092d16cd6239 /common.h | |
parent | Don't validate RTA_GATEWAY and fix an error string (diff) | |
download | wg-dynamic-1b5a1c72c734d03ae45b5eae1a18978fa110d14a.tar.xz wg-dynamic-1b5a1c72c734d03ae45b5eae1a18978fa110d14a.zip |
Serialize/deserialize messages into a struct
Instead of a list of attributes, parse messages into a proper struct to
avoid duplicating code in the server/client for handling this list, as
well as making parsing nicer in general.
Diffstat (limited to 'common.h')
-rw-r--r-- | common.h | 39 |
1 files changed, 18 insertions, 21 deletions
@@ -15,11 +15,8 @@ #include "netlink.h" #define MAX_CONNECTIONS 16 - #define MAX_LINESIZE 4096 - #define RECV_BUFSIZE 8192 - #define MAX_RESPONSE_SIZE 8192 static const char WG_DYNAMIC_ADDR[] = "fe80::"; @@ -27,11 +24,11 @@ static const uint16_t WG_DYNAMIC_PORT = 970; /* ASCII sum of "wireguard" */ #define ITEMS \ E(WGKEY_UNKNOWN, "") /* must be the first entry */ \ + E(WGKEY_EOMSG, "") \ /* CMD START */ \ E(WGKEY_REQUEST_IP, "request_ip") \ E(WGKEY_ENDCMD, "") \ /* CMD END */ \ - E(WGKEY_INCOMPLETE, "") \ E(WGKEY_IPV4, "ipv4") \ E(WGKEY_IPV6, "ipv6") \ E(WGKEY_LEASESTART, "leasestart") \ @@ -50,6 +47,7 @@ static const char *const WG_DYNAMIC_KEY[] = { ITEMS }; #define ITEMS \ E(E_NO_ERROR, "Success") /* must be the first entry */ \ E(E_INVALID_REQ, "Invalid request") \ + E(E_UNSUPP_PROTO, "Unsupported protocol") \ E(E_IP_UNAVAIL, "Chosen IP unavailable") #define E(x, y) x, @@ -60,22 +58,21 @@ static const char *const WG_DYNAMIC_ERR[] = { ITEMS }; #undef E #undef ITEMS -struct wg_dynamic_attr { - enum wg_dynamic_key key; - size_t len; - struct wg_dynamic_attr *next; - unsigned char value[]; -}; - struct wg_dynamic_request { enum wg_dynamic_key cmd; uint32_t version; - int fd; - wg_key pubkey; - struct in6_addr lladdr; unsigned char *buf; - size_t buflen; - struct wg_dynamic_attr *first, *last; + size_t len; /* <= MAX_LINESIZE */ + void *result; +}; + +struct wg_dynamic_request_ip { + struct in_addr ipv4; + struct in6_addr ipv6; + uint8_t cidrv4, cidrv6; + uint32_t leasetime, start, wg_errno; + bool has_ipv4, has_ipv6; + char *errmsg; }; struct wg_combined_ip { @@ -89,14 +86,14 @@ struct wg_combined_ip { #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) +int handle_request(int fd, struct wg_dynamic_request *req, + unsigned char buf[RECV_BUFSIZE + MAX_LINESIZE], + size_t *remaining); void free_wg_dynamic_request(struct wg_dynamic_request *req); -bool handle_request(struct wg_dynamic_request *req, - bool (*success)(struct wg_dynamic_request *), - bool (*error)(struct wg_dynamic_request *, int)); -bool send_message(struct wg_dynamic_request *req, const void *buf, size_t len); +size_t serialize_request_ip(bool include_header, char *buf, size_t len, + struct wg_dynamic_request_ip *rip); void print_to_buf(char *buf, size_t bufsize, size_t *offset, char *fmt, ...); uint32_t current_time(); -void close_connection(struct wg_dynamic_request *req); bool is_link_local(unsigned char *addr); void iface_get_all_addrs(uint8_t family, mnl_cb_t data_cb, void *cb_data); int data_attr_cb(const struct nlattr *attr, void *data); |