tor_multipath_voip/src/socks5.h

148 lines
2.6 KiB
C
Raw Normal View History

2019-02-08 16:37:02 +00:00
#pragma once
2021-01-05 09:56:54 +00:00
2019-02-08 13:28:39 +00:00
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
2020-02-21 13:57:54 +00:00
#include <arpa/inet.h>
2019-02-08 13:28:39 +00:00
#include "net_tools.h"
2019-03-21 12:01:48 +00:00
#include "evt_core.h"
2019-02-08 13:28:39 +00:00
2019-02-15 14:45:56 +00:00
enum socks5_state {
SOCKS5_STATE_NEW,
SOCKS5_STATE_ACK,
SOCKS5_STATE_RDY,
SOCKS5_STATE_ERR
};
2019-02-08 13:28:39 +00:00
enum cmd {
CMD_CONNECT = 0x01,
CMD_BIND = 0x02,
CMD_UDP_ASSOCIATE = 0x03
};
enum atyp {
ATYP_IPV4 = 0x01,
ATYP_DOMAINNAME = 0x03,
ATYP_IPV6 = 0x04
};
2020-02-21 16:55:06 +00:00
static char *atyp_str[] = {
"INVALID",
"IPV4",
2020-02-21 17:19:16 +00:00
"INVALID",
"DOMAIN_NAME",
2020-02-21 16:55:06 +00:00
"IPV6"
};
2019-03-21 17:43:38 +00:00
enum ver {
VER_SOCKS5 = 0x05
};
enum methods {
METHOD_NOAUTH = 0x00,
METHOD_GSSAPI = 0x01,
METHOD_USERPASS = 0x02,
METHOD_NOACCEPT = 0xff
};
2019-02-08 13:28:39 +00:00
union socks5_addr {
struct {
uint8_t len;
2019-03-22 16:14:35 +00:00
char str[256];
2019-02-08 13:28:39 +00:00
} dns;
uint8_t ipv4[4];
uint8_t ipv6[16];
};
enum socks5_rep {
SOCKS5_REP_SUCCESS,
SOCKS5_REP_GENERAL_FAILURE,
SOCKS5_REP_CONOTALLOWED,
SOCKS5_REP_NETUNREACH,
SOCKS5_REP_HOSTUNREACH,
SOCKS5_REP_COREFUSED,
SOCKS5_REP_TTLEXP,
SOCKS5_REP_CMDNOTSUP,
SOCKS5_REP_ADDRNOTSUP
};
2019-02-13 14:32:38 +00:00
static char* rep_msg[] = {
2019-02-12 20:45:15 +00:00
"Succeeded",
"General SOCKS server failure",
"Connection not allowed by ruleset",
"Network unreachable",
"Host unreachable",
"Connection refused",
"TTL expired",
"Command not supported",
"Address type not supported"
};
2019-02-08 13:28:39 +00:00
/*
* RFC 1928 Messages
* https://tools.ietf.org/html/rfc1928
*/
2020-02-21 14:26:28 +00:00
#pragma pack(1)
2019-02-08 13:28:39 +00:00
struct client_handshake {
uint8_t ver;
uint8_t nmethods;
uint8_t methods[255];
};
struct server_handshake {
uint8_t ver;
uint8_t method;
};
struct client_request {
uint8_t ver;
uint8_t cmd;
uint8_t rsv;
uint8_t atyp;
2020-02-21 14:26:28 +00:00
union socks5_addr dest_addr;
2019-02-08 13:28:39 +00:00
uint16_t port;
};
struct server_reply {
uint8_t ver;
uint8_t rep;
uint8_t rsv;
uint8_t atyp;
union socks5_addr bind_addr;
uint16_t port;
};
2021-01-26 14:19:09 +00:00
#pragma pack()
2019-02-08 13:28:39 +00:00
2019-03-25 14:26:30 +00:00
struct socks5_ctx {
uint16_t port;
2019-03-25 16:20:47 +00:00
char* addr;
2019-03-25 14:26:30 +00:00
struct client_handshake ch;
struct server_handshake sh;
struct client_request cr;
struct server_reply sr;
uint64_t ch_cursor;
uint64_t sh_cursor;
uint64_t cr_cursor;
uint64_t sr_cursor;
char cr_buffer[262];
2020-02-21 13:57:54 +00:00
char sr_buffer[263];
2019-03-25 14:26:30 +00:00
size_t ch_size;
size_t cr_size;
2020-02-21 13:57:54 +00:00
size_t sr_size;
2019-03-25 14:26:30 +00:00
uint8_t sr_host_read;
2020-02-21 16:55:06 +00:00
uint8_t cr_host_read;
2019-03-25 14:26:30 +00:00
};
void socks5_init(struct evt_core_ctx* ctx);
void socks5_create_dns_client(struct evt_core_ctx* ctx, char* proxy_host, char* proxy_port, char* addr, uint16_t port);
char* socks5_rep (enum socks5_rep rep);
2020-02-20 17:42:46 +00:00
void socks5_server_init(struct evt_core_ctx* ctx);
void socks5_server_handle_req(struct evt_core_ctx* ctx, int fd);