tor_multipath_voip/src/socks5.c

108 lines
3.3 KiB
C
Raw Normal View History

2019-02-08 13:28:39 +00:00
#include "socks5.h"
2019-02-15 14:45:56 +00:00
int socks5_handshake_syn(int sock) {
//@TODO: Refactor the client handshake management
2019-02-12 20:45:15 +00:00
struct client_handshake ch = {
.ver = 0x05,
.nmethods = 0x01,
.methods = {0x00}
};
2019-02-08 13:28:39 +00:00
int size = sizeof(uint8_t) * (2 + ch.nmethods);
if (size != write(sock, &ch, size)) {
2019-02-15 14:45:56 +00:00
perror("write failed on tcp socket in socks5");
2019-02-12 20:45:15 +00:00
return -1;
2019-02-08 13:28:39 +00:00
}
2019-02-15 14:45:56 +00:00
return 0;
}
2019-02-08 13:28:39 +00:00
2019-02-15 14:45:56 +00:00
int socks5_handshake_ack(int sock) {
//@TODO: Refactor the client handshake management
struct client_handshake ch = {
.ver = 0x05,
.nmethods = 0x01,
.methods = {0x00}
};
2019-02-08 13:28:39 +00:00
struct server_handshake sh = {0};
int err = read_entity(sock, &sh, sizeof(struct server_handshake));
if (err == -1) {
2019-02-12 20:45:15 +00:00
perror("sock5 handshake failed read");
return -1;
}
if (ch.ver != sh.ver || sh.method != ch.methods[0]) {
fprintf(stderr, "Protocol error: client asks for ver=%d, method=%d and server answers with ver=%d, method=%d\n",
ch.ver, ch.methods[0], sh.ver, sh.method);
return -1;
2019-02-08 13:28:39 +00:00
}
printf("[server_handshake] ver=%d, method=%d\n", sh.ver, sh.method);
2019-02-12 20:45:15 +00:00
return 0;
2019-02-08 13:28:39 +00:00
}
2019-02-12 20:45:15 +00:00
int socks5_reply(int sock) {
2019-03-05 15:57:14 +00:00
int res;
2019-02-08 13:28:39 +00:00
struct server_reply sr = {0};
2019-03-05 15:57:14 +00:00
res = read_entity(sock, &sr, sizeof(uint8_t) * 4);
2019-03-20 16:02:23 +00:00
if (res == -1) goto read_error;
2019-03-05 15:57:14 +00:00
2019-02-08 13:28:39 +00:00
switch(sr.atyp) {
case ATYP_IPV4:
if (read_entity(sock, sr.bind_addr.ipv4, sizeof(uint8_t) * 4) == -1)
goto read_error;
break;
case ATYP_DOMAINNAME:
if (read_entity(sock, &sr.bind_addr.dns.len, sizeof(uint8_t) * 4) == -1)
goto read_error;
if (read_entity(sock, sr.bind_addr.dns.str, sizeof(char) * sr.bind_addr.dns.len) == -1)
goto read_error;
break;
case ATYP_IPV6:
if (read_entity(sock, sr.bind_addr.ipv6, sizeof(uint8_t) * 16) == -1)
goto read_error;
break;
default:
2019-02-12 20:45:15 +00:00
fprintf(stderr, "Unsupported ATYP in server reply\n");
return -128;
2019-02-08 13:28:39 +00:00
}
2019-03-05 15:57:14 +00:00
res = read_entity(sock, &sr.port, sizeof(uint16_t));
if (res == -1) {
perror("read_entity");
exit(EXIT_FAILURE);
}
2019-02-08 13:28:39 +00:00
2019-02-12 20:45:15 +00:00
if (sr.rep < 0 || sr.rep > 0x08) {
fprintf(stderr, "Invalid reply field\n");
return -128;
}
2019-02-08 13:28:39 +00:00
2019-02-12 20:45:15 +00:00
printf("[server_reply] ver=%d, rep=%s, atyp=%d, port=%d\n", sr.ver, rep_msg[sr.rep], sr.atyp, sr.port);
return -sr.rep;
2019-02-08 13:28:39 +00:00
read_error:
2019-02-12 20:45:15 +00:00
fprintf(stderr, "Unable to read ATYP\n");
return -128;
2019-02-08 13:28:39 +00:00
}
2019-02-12 20:45:15 +00:00
int socks5_connect_dns(int sock, char* addr, uint16_t port) {
2019-02-08 13:28:39 +00:00
char buffer[262];
size_t domainLength = strlen(addr);
if (domainLength > 255) {
fprintf(stderr, "domain is too long\n");
exit(EXIT_FAILURE);
}
struct client_request cr = { 0x05, CMD_CONNECT, 0x00, ATYP_DOMAINNAME, (uint8_t)domainLength, addr, htons(port) };
size_t written = 0;
fill_buffer(&written, buffer, &cr, 5*sizeof(uint8_t));
fill_buffer(&written, buffer, cr.dst_addr, cr.dst_addr_len*sizeof(char));
fill_buffer(&written, buffer, &cr.port, sizeof(uint16_t));
if (written != write(sock, buffer, written)) {
fprintf(stderr, "partial/failed write\n");
2019-02-12 20:45:15 +00:00
return -1;
2019-02-08 13:28:39 +00:00
}
2019-02-15 14:45:56 +00:00
return 0;
2019-02-08 13:28:39 +00:00
}
char* socks5_rep (enum socks5_rep rep) {
return rep_msg[rep];
}