Improve proxy server support
[tinc] / src / proxy.h
1 #ifndef TINC_PROXY_H
2 #define TINC_PROXY_H
3
4 #include "system.h"
5
6 #include "net.h"
7
8 PACKED(struct socks4_request_t {
9         uint8_t version;
10         uint8_t command;
11         uint16_t dstport;
12         struct in_addr dstip;
13         char id[];
14 });
15
16 PACKED(struct socks4_response_t {
17         uint8_t version;
18         uint8_t status;
19         uint16_t dstport;
20         struct in_addr dstip;
21 });
22
23 typedef struct socks4_request_t socks4_request_t;
24 typedef struct socks4_response_t socks4_response_t;
25
26 PACKED(struct socks5_greet_t {
27         uint8_t version;
28         uint8_t nmethods;
29         uint8_t authmethod;
30 });
31
32 typedef struct socks5_greet_t socks5_greet_t;
33
34 PACKED(struct socks5_conn_hdr_t {
35         uint8_t version;
36         uint8_t command;
37         uint8_t reserved;
38         uint8_t addr_type;
39 });
40
41 PACKED(struct socks5_ipv4_t {
42         struct in_addr addr;
43         uint16_t port;
44 });
45
46 PACKED(struct socks5_ipv6_t {
47         struct in6_addr addr;
48         uint16_t port;
49 });
50
51 typedef struct socks5_conn_hdr_t socks5_conn_hdr_t;
52 typedef struct socks5_ipv4_t socks5_ipv4_t;
53 typedef struct socks5_ipv6_t socks5_ipv6_t;
54
55 PACKED(struct socks5_conn_req_t {
56         socks5_conn_hdr_t header;
57         union {
58                 socks5_ipv4_t ipv4;
59                 socks5_ipv6_t ipv6;
60         } dst;
61 });
62
63 PACKED(struct socks5_server_choice_t {
64         uint8_t socks_version;
65         uint8_t auth_method;
66 });
67
68 PACKED(struct socks5_auth_status_t {
69         uint8_t auth_version;
70         uint8_t auth_status;
71 });
72
73 typedef struct socks5_auth_status_t socks5_auth_status_t;
74
75 PACKED(struct socks5_conn_resp_t {
76         uint8_t socks_version;
77         uint8_t conn_status;
78         uint8_t reserved;
79         uint8_t addr_type;
80 });
81
82 typedef struct socks5_conn_req_t socks5_conn_req_t;
83 typedef struct socks5_server_choice_t socks5_server_choice_t;
84 typedef struct socks5_conn_resp_t socks5_conn_resp_t;
85
86 PACKED(struct socks5_resp_t {
87         socks5_server_choice_t choice;
88
89         union {
90                 // if choice == password
91                 struct {
92                         socks5_auth_status_t status;
93                         socks5_conn_resp_t resp;
94                 } pass;
95
96                 // if choice == anonymous
97                 socks5_conn_resp_t anon;
98         };
99 });
100
101 typedef struct socks5_resp_t socks5_resp_t;
102
103 // Get the length of a connection request to a SOCKS 4 or 5 proxy
104 extern size_t socks_req_len(proxytype_t type, const sockaddr_t *sa);
105
106 // Create a request to connect to a SOCKS 4 or 5 proxy.
107 // Returns the expected response length, or zero on error.
108 extern size_t create_socks_req(proxytype_t type, void *req, const sockaddr_t *sa);
109
110 // Check that SOCKS server provided a valid response and permitted further requests
111 extern bool check_socks_resp(proxytype_t type, const void *buf, size_t len);
112
113 #endif // TINC_PROXY_H