2011-02-25 17:40:11 +08:00
|
|
|
/***************************************************************************
|
|
|
|
core.c - description
|
|
|
|
-------------------
|
|
|
|
begin : Tue May 14 2002
|
2011-09-03 04:53:50 +08:00
|
|
|
copyright : netcreature (C) 2002
|
|
|
|
email : netcreature@users.sourceforge.net
|
2011-11-07 00:46:11 +08:00
|
|
|
***************************************************************************
|
|
|
|
* GPL *
|
|
|
|
***************************************************************************
|
2011-02-25 17:40:11 +08:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2011-09-03 01:55:50 +08:00
|
|
|
#include <string.h>
|
2011-02-25 17:40:11 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <time.h>
|
2011-09-03 04:53:50 +08:00
|
|
|
#include <sys/time.h>
|
2011-02-25 17:40:11 +08:00
|
|
|
#include <stdarg.h>
|
2011-11-07 00:46:11 +08:00
|
|
|
#ifdef THREAD_SAFE
|
|
|
|
#include <pthread.h>
|
|
|
|
pthread_mutex_t internal_ips_lock;
|
|
|
|
#endif
|
|
|
|
|
2011-02-25 17:40:11 +08:00
|
|
|
#include "core.h"
|
2011-11-06 21:12:50 +08:00
|
|
|
#include "common.h"
|
2011-02-25 17:40:11 +08:00
|
|
|
|
|
|
|
extern int tcp_read_time_out;
|
|
|
|
extern int tcp_connect_time_out;
|
|
|
|
extern int proxychains_quiet_mode;
|
2012-01-26 19:44:42 +08:00
|
|
|
extern unsigned int remote_dns_subnet;
|
2011-02-25 17:40:11 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
internal_ip_lookup_table internal_ips = { 0, 0, NULL };
|
2011-11-06 21:12:50 +08:00
|
|
|
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
uint32_t dalias_hash(char *s0) {
|
|
|
|
unsigned char *s = (void *) s0;
|
2011-11-06 21:12:50 +08:00
|
|
|
uint_fast32_t h = 0;
|
2012-01-28 01:59:44 +08:00
|
|
|
while(*s) {
|
|
|
|
h = 16 * h + *s++;
|
|
|
|
h ^= h >> 24 & 0xf0;
|
2011-11-06 21:12:50 +08:00
|
|
|
}
|
|
|
|
return h & 0xfffffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t index_from_internal_ip(ip_type internalip) {
|
|
|
|
ip_type tmp = internalip;
|
|
|
|
uint32_t ret;
|
2011-11-06 22:18:50 +08:00
|
|
|
ret = tmp.octet[3] + (tmp.octet[2] << 8) + (tmp.octet[1] << 16);
|
|
|
|
ret -= 1;
|
2011-11-06 21:12:50 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
char *string_from_internal_ip(ip_type internalip) {
|
|
|
|
char *res = NULL;
|
2011-11-07 00:46:11 +08:00
|
|
|
#ifdef THREAD_SAFE
|
|
|
|
pthread_mutex_lock(&internal_ips_lock);
|
|
|
|
#endif
|
|
|
|
uint32_t index = index_from_internal_ip(internalip);
|
|
|
|
if(index < internal_ips.counter)
|
|
|
|
res = internal_ips.list[index]->string;
|
|
|
|
#ifdef THREAD_SAFE
|
|
|
|
pthread_mutex_unlock(&internal_ips_lock);
|
|
|
|
#endif
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-11-06 21:12:50 +08:00
|
|
|
in_addr_t make_internal_ip(uint32_t index) {
|
|
|
|
ip_type ret;
|
2012-01-28 01:59:44 +08:00
|
|
|
index++; // so we can start at .0.0.1
|
|
|
|
if(index > 0xFFFFFF)
|
|
|
|
return (in_addr_t) - 1;
|
2012-01-26 19:44:42 +08:00
|
|
|
ret.octet[0] = remote_dns_subnet & 0xFF;
|
2011-11-06 21:12:50 +08:00
|
|
|
ret.octet[1] = (index & 0xFF0000) >> 16;
|
|
|
|
ret.octet[2] = (index & 0xFF00) >> 8;
|
|
|
|
ret.octet[3] = index & 0xFF;
|
|
|
|
return (in_addr_t) ret.as_int;
|
|
|
|
}
|
|
|
|
|
2012-01-28 03:00:22 +08:00
|
|
|
// stolen from libulz (C) rofl0r
|
|
|
|
void pc_stringfromipv4(unsigned char *ip_buf_4_bytes, char *outbuf_16_bytes) {
|
|
|
|
unsigned char *p;
|
|
|
|
char *o = outbuf_16_bytes;
|
|
|
|
unsigned char n;
|
|
|
|
for(p = ip_buf_4_bytes; p < ip_buf_4_bytes + 4; p++) {
|
|
|
|
n = *p;
|
|
|
|
if(*p >= 100) {
|
|
|
|
if(*p >= 200)
|
|
|
|
*(o++) = '2';
|
|
|
|
else
|
|
|
|
*(o++) = '1';
|
|
|
|
n %= 100;
|
|
|
|
}
|
|
|
|
if(*p >= 10) {
|
|
|
|
*(o++) = (n / 10) + '0';
|
|
|
|
n %= 10;
|
|
|
|
}
|
|
|
|
*(o++) = n + '0';
|
|
|
|
*(o++) = '.';
|
|
|
|
}
|
|
|
|
o[-1] = 0;
|
|
|
|
}
|
|
|
|
|
2011-02-25 17:40:11 +08:00
|
|
|
static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
static int poll_retry(struct pollfd *fds, nfds_t nfsd, int timeout) {
|
2011-11-06 21:12:50 +08:00
|
|
|
int ret;
|
|
|
|
int time_remain = timeout;
|
|
|
|
int time_elapsed = 0;
|
|
|
|
|
|
|
|
struct timeval start_time;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
gettimeofday(&start_time, NULL);
|
|
|
|
|
|
|
|
do {
|
|
|
|
//printf("Retry %d\n", time_remain);
|
|
|
|
ret = poll(fds, nfsd, time_remain);
|
|
|
|
gettimeofday(&tv, NULL);
|
2012-01-28 01:59:44 +08:00
|
|
|
time_elapsed = ((tv.tv_sec - start_time.tv_sec) * 1000 + (tv.tv_usec - start_time.tv_usec) / 1000);
|
2011-11-06 21:12:50 +08:00
|
|
|
//printf("Time elapsed %d\n", time_elapsed);
|
|
|
|
time_remain = timeout - time_elapsed;
|
2012-01-28 01:59:44 +08:00
|
|
|
} while(ret == -1 && errno == EINTR && time_remain > 0);
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-11-06 21:12:50 +08:00
|
|
|
//if (ret == -1)
|
|
|
|
//printf("Return %d %d %s\n", ret, errno, strerror(errno));
|
|
|
|
return ret;
|
2011-02-25 22:18:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
static void encode_base_64(char *src, char *dest, int max_len) {
|
|
|
|
int n, l, i;
|
|
|
|
l = strlen(src);
|
|
|
|
max_len = (max_len - 1) / 4;
|
|
|
|
for(i = 0; i < max_len; i++, src += 3, l -= 3) {
|
2011-02-25 17:40:11 +08:00
|
|
|
switch (l) {
|
2012-01-28 01:59:44 +08:00
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
n = src[0] << 16;
|
|
|
|
*dest++ = base64[(n >> 18) & 077];
|
|
|
|
*dest++ = base64[(n >> 12) & 077];
|
|
|
|
*dest++ = '=';
|
|
|
|
*dest++ = '=';
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
n = src[0] << 16 | src[1] << 8;
|
|
|
|
*dest++ = base64[(n >> 18) & 077];
|
|
|
|
*dest++ = base64[(n >> 12) & 077];
|
|
|
|
*dest++ = base64[(n >> 6) & 077];
|
|
|
|
*dest++ = '=';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
n = src[0] << 16 | src[1] << 8 | src[2];
|
|
|
|
*dest++ = base64[(n >> 18) & 077];
|
|
|
|
*dest++ = base64[(n >> 12) & 077];
|
|
|
|
*dest++ = base64[(n >> 6) & 077];
|
|
|
|
*dest++ = base64[n & 077];
|
2011-02-25 17:40:11 +08:00
|
|
|
}
|
2012-01-28 01:59:44 +08:00
|
|
|
if(l < 3)
|
|
|
|
break;
|
2011-02-25 17:40:11 +08:00
|
|
|
}
|
2012-01-28 01:59:44 +08:00
|
|
|
*dest++ = 0;
|
2011-02-25 17:40:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define LOG_BUFF 1024*20
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
int proxychains_write_log(char *str, ...) {
|
2011-11-06 21:12:50 +08:00
|
|
|
char buff[LOG_BUFF];
|
|
|
|
va_list arglist;
|
2012-01-28 01:59:44 +08:00
|
|
|
FILE *log_file;
|
|
|
|
log_file = stderr;
|
|
|
|
if(!proxychains_quiet_mode) {
|
|
|
|
va_start(arglist, str);
|
|
|
|
vsprintf(buff, str, arglist);
|
2011-11-06 21:12:50 +08:00
|
|
|
va_end(arglist);
|
2012-01-28 01:59:44 +08:00
|
|
|
fprintf(log_file, "%s", buff);
|
2011-11-06 21:12:50 +08:00
|
|
|
fflush(log_file);
|
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
2011-02-25 17:40:11 +08:00
|
|
|
}
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
static int write_n_bytes(int fd, char *buff, size_t size) {
|
|
|
|
int i = 0;
|
|
|
|
size_t wrote = 0;
|
|
|
|
for(;;) {
|
|
|
|
i = write(fd, &buff[wrote], size - wrote);
|
|
|
|
if(i <= 0)
|
2011-09-04 07:45:16 +08:00
|
|
|
return i;
|
|
|
|
wrote += i;
|
2012-01-28 01:59:44 +08:00
|
|
|
if(wrote == size)
|
2011-09-04 07:45:16 +08:00
|
|
|
return wrote;
|
|
|
|
}
|
2011-02-25 17:40:11 +08:00
|
|
|
}
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
static int read_n_bytes(int fd, char *buff, size_t size) {
|
2011-09-04 07:45:16 +08:00
|
|
|
int ready;
|
|
|
|
size_t i;
|
|
|
|
struct pollfd pfd[1];
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
pfd[0].fd = fd;
|
|
|
|
pfd[0].events = POLLIN;
|
|
|
|
for(i = 0; i < size; i++) {
|
2011-09-04 07:45:16 +08:00
|
|
|
pfd[0].revents = 0;
|
|
|
|
ready = poll_retry(pfd, 1, tcp_read_time_out);
|
2012-01-28 01:59:44 +08:00
|
|
|
if(ready != 1 || !(pfd[0].revents & POLLIN) || 1 != read(fd, &buff[i], 1))
|
2011-09-04 07:45:16 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return (int) size;
|
2011-02-25 17:40:11 +08:00
|
|
|
}
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
static int timed_connect(int sock, const struct sockaddr *addr, socklen_t len) {
|
2011-09-03 04:53:50 +08:00
|
|
|
int ret, value;
|
|
|
|
socklen_t value_len;
|
2012-01-28 01:59:44 +08:00
|
|
|
struct pollfd pfd[1];
|
2011-02-25 17:40:11 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
pfd[0].fd = sock;
|
|
|
|
pfd[0].events = POLLOUT;
|
2011-02-25 17:40:11 +08:00
|
|
|
fcntl(sock, F_SETFL, O_NONBLOCK);
|
2012-01-28 01:59:44 +08:00
|
|
|
ret = true_connect(sock, addr, len);
|
2011-09-04 07:45:16 +08:00
|
|
|
#ifdef DEBUG
|
2012-01-28 01:59:44 +08:00
|
|
|
if(ret == -1)
|
|
|
|
perror("true_connect");
|
|
|
|
printf("\nconnect ret=%d\n", ret);
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-09-04 07:45:16 +08:00
|
|
|
fflush(stdout);
|
|
|
|
#endif
|
2012-01-28 01:59:44 +08:00
|
|
|
if(ret == -1 && errno == EINPROGRESS) {
|
|
|
|
ret = poll_retry(pfd, 1, tcp_connect_time_out);
|
2011-09-04 07:45:16 +08:00
|
|
|
#ifdef DEBUG
|
2012-01-28 01:59:44 +08:00
|
|
|
printf("\npoll ret=%d\n", ret);
|
|
|
|
fflush(stdout);
|
2011-09-04 07:45:16 +08:00
|
|
|
#endif
|
2012-01-28 01:59:44 +08:00
|
|
|
if(ret == 1) {
|
|
|
|
value_len = sizeof(socklen_t);
|
|
|
|
getsockopt(sock, SOL_SOCKET, SO_ERROR, &value, &value_len);
|
2011-09-04 07:45:16 +08:00
|
|
|
#ifdef DEBUG
|
2012-01-28 01:59:44 +08:00
|
|
|
printf("\nvalue=%d\n", value);
|
|
|
|
fflush(stdout);
|
2011-09-04 07:45:16 +08:00
|
|
|
#endif
|
2012-01-28 01:59:44 +08:00
|
|
|
if(!value)
|
|
|
|
ret = 0;
|
2011-09-03 04:53:50 +08:00
|
|
|
else
|
2012-01-28 01:59:44 +08:00
|
|
|
ret = -1;
|
2011-02-25 22:18:29 +08:00
|
|
|
} else {
|
2012-01-28 01:59:44 +08:00
|
|
|
ret = -1;
|
2011-02-25 22:18:29 +08:00
|
|
|
}
|
|
|
|
} else {
|
2012-01-28 01:59:44 +08:00
|
|
|
if(ret != 0)
|
|
|
|
ret = -1;
|
2012-01-28 00:55:37 +08:00
|
|
|
}
|
2011-02-25 17:40:11 +08:00
|
|
|
|
2011-02-25 22:18:29 +08:00
|
|
|
fcntl(sock, F_SETFL, !O_NONBLOCK);
|
|
|
|
return ret;
|
2011-02-25 17:40:11 +08:00
|
|
|
}
|
|
|
|
|
2011-11-06 21:12:50 +08:00
|
|
|
|
|
|
|
#define INVALID_INDEX 0xFFFFFFFFU
|
2012-01-28 01:59:44 +08:00
|
|
|
static int tunnel_to(int sock, ip_type ip, unsigned short port, proxy_type pt, char *user, char *pass) {
|
|
|
|
char *dns_name = NULL;
|
2011-11-06 21:12:50 +08:00
|
|
|
size_t dns_len = 0;
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 02:14:17 +08:00
|
|
|
PDEBUG("tunnel_to()\n");
|
|
|
|
|
2011-11-06 21:12:50 +08:00
|
|
|
// we use ip addresses with 224.* to lookup their dns name in our table, to allow remote DNS resolution
|
|
|
|
// the range 224-255.* is reserved, and it won't go outside (unless the app does some other stuff with
|
|
|
|
// the results returned from gethostbyname et al.)
|
2012-01-26 19:44:42 +08:00
|
|
|
// the hardcoded number 224 can now be changed using the config option remote_dns_subnet to i.e. 127
|
|
|
|
if(ip.octet[0] == remote_dns_subnet) {
|
2011-11-07 00:46:11 +08:00
|
|
|
dns_name = string_from_internal_ip(ip);
|
2012-01-28 01:59:44 +08:00
|
|
|
if(!dns_name)
|
|
|
|
goto err;
|
2011-11-06 21:12:50 +08:00
|
|
|
dns_len = strlen(dns_name);
|
2012-01-28 01:59:44 +08:00
|
|
|
if(!dns_len)
|
|
|
|
goto err;
|
2011-11-06 21:12:50 +08:00
|
|
|
}
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-11-06 21:12:50 +08:00
|
|
|
size_t ulen = strlen(user);
|
|
|
|
size_t passlen = strlen(pass);
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-11-08 00:07:00 +08:00
|
|
|
if(ulen > 0xFF || passlen > 0xFF || dns_len > 0xFF) {
|
|
|
|
proxychains_write_log(LOG_PREFIX "error: maximum size of 255 for user/pass or domain name!\n");
|
|
|
|
goto err;
|
|
|
|
}
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
int len;
|
|
|
|
unsigned char buff[BUFF_SIZE];
|
2012-01-28 03:00:22 +08:00
|
|
|
char ip_buf[16];
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
//memset (buff, 0, sizeof(buff));
|
|
|
|
|
|
|
|
switch (pt) {
|
|
|
|
case HTTP_TYPE:{
|
2012-01-28 03:00:22 +08:00
|
|
|
if(!dns_len) {
|
|
|
|
pc_stringfromipv4(&ip.octet[0], ip_buf);
|
|
|
|
dns_name = ip_buf;
|
|
|
|
}
|
2012-01-28 01:59:44 +08:00
|
|
|
|
|
|
|
snprintf((char *) buff, sizeof(buff), "CONNECT %s:%d HTTP/1.0\r\n", dns_name,
|
|
|
|
ntohs(port));
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
if(user[0]) {
|
|
|
|
#define HTTP_AUTH_MAX ((0xFF * 2) + 1 + 1)
|
|
|
|
// 2 * 0xff: username and pass, plus 1 for ':' and 1 for zero terminator.
|
|
|
|
char src[HTTP_AUTH_MAX];
|
|
|
|
char dst[(4 * HTTP_AUTH_MAX)];
|
2011-02-25 17:40:11 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
memcpy(src, user, ulen);
|
|
|
|
memcpy(src + ulen, ":", 1);
|
|
|
|
memcpy(src + ulen + 1, pass, passlen);
|
|
|
|
src[ulen + 1 + passlen] = 0;
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
encode_base_64(src, dst, sizeof(dst));
|
|
|
|
strcat((char *) buff, "Proxy-Authorization: Basic ");
|
|
|
|
strcat((char *) buff, dst);
|
|
|
|
strcat((char *) buff, "\r\n\r\n");
|
|
|
|
} else
|
|
|
|
strcat((char *) buff, "\r\n");
|
|
|
|
|
|
|
|
len = strlen((char *) buff);
|
|
|
|
|
|
|
|
if(len != send(sock, buff, len, 0))
|
2011-11-06 21:12:50 +08:00
|
|
|
goto err;
|
2012-01-28 01:59:44 +08:00
|
|
|
|
|
|
|
len = 0;
|
|
|
|
// read header byte by byte.
|
|
|
|
while(len < BUFF_SIZE) {
|
|
|
|
if(1 == read_n_bytes(sock, (char *) (buff + len), 1))
|
|
|
|
len++;
|
|
|
|
else
|
|
|
|
goto err;
|
|
|
|
if(len > 4 &&
|
|
|
|
buff[len - 1] == '\n' &&
|
|
|
|
buff[len - 2] == '\r' && buff[len - 3] == '\n' && buff[len - 4] == '\r')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if not ok (200) or response greather than BUFF_SIZE return BLOCKED;
|
|
|
|
if(len == BUFF_SIZE || !(buff[9] == '2' && buff[10] == '0' && buff[11] == '0'))
|
|
|
|
return BLOCKED;
|
|
|
|
|
|
|
|
return SUCCESS;
|
2011-11-06 21:12:50 +08:00
|
|
|
}
|
2012-01-28 01:59:44 +08:00
|
|
|
break;
|
2011-11-06 21:12:50 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
case SOCKS4_TYPE:{
|
|
|
|
buff[0] = 4; // socks version
|
|
|
|
buff[1] = 1; // connect command
|
|
|
|
memcpy(&buff[2], &port, 2); // dest port
|
2011-11-06 21:12:50 +08:00
|
|
|
if(dns_len) {
|
|
|
|
ip.octet[0] = 0;
|
|
|
|
ip.octet[1] = 0;
|
|
|
|
ip.octet[2] = 0;
|
|
|
|
ip.octet[3] = 1;
|
|
|
|
}
|
2012-01-28 01:59:44 +08:00
|
|
|
memcpy(&buff[4], &ip, 4); // dest host
|
|
|
|
len = ulen + 1; // username
|
|
|
|
if(len > 1)
|
|
|
|
memcpy(&buff[8], user, len);
|
2011-11-07 07:09:46 +08:00
|
|
|
else {
|
|
|
|
buff[8] = 0;
|
|
|
|
}
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-11-06 21:12:50 +08:00
|
|
|
// do socksv4a dns resolution on the server
|
|
|
|
if(dns_len) {
|
|
|
|
memcpy(&buff[8 + len], dns_name, dns_len + 1);
|
|
|
|
len += dns_len + 1;
|
|
|
|
}
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
if((len + 8) != write_n_bytes(sock, (char *) buff, (8 + len)))
|
2011-11-06 21:12:50 +08:00
|
|
|
goto err;
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
if(8 != read_n_bytes(sock, (char *) buff, 8))
|
2011-11-06 21:12:50 +08:00
|
|
|
goto err;
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
if(buff[0] != 0 || buff[1] != 90)
|
2011-02-25 17:40:11 +08:00
|
|
|
return BLOCKED;
|
2011-11-06 21:12:50 +08:00
|
|
|
|
|
|
|
return SUCCESS;
|
2012-01-28 01:59:44 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SOCKS5_TYPE:{
|
|
|
|
if(user) {
|
|
|
|
buff[0] = 5; //version
|
|
|
|
buff[1] = 2; //nomber of methods
|
|
|
|
buff[2] = 0; // no auth method
|
|
|
|
buff[3] = 2; /// auth method -> username / password
|
|
|
|
if(4 != write_n_bytes(sock, (char *) buff, 4))
|
|
|
|
goto err;
|
|
|
|
} else {
|
|
|
|
buff[0] = 5; //version
|
|
|
|
buff[1] = 1; //nomber of methods
|
|
|
|
buff[2] = 0; // no auth method
|
|
|
|
if(3 != write_n_bytes(sock, (char *) buff, 3))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(2 != read_n_bytes(sock, (char *) buff, 2))
|
2011-11-06 21:12:50 +08:00
|
|
|
goto err;
|
2011-02-25 17:40:11 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
if(buff[0] != 5 || (buff[1] != 0 && buff[1] != 2)) {
|
|
|
|
if(buff[0] == 5 && buff[1] == 0xFF)
|
|
|
|
return BLOCKED;
|
|
|
|
else
|
|
|
|
goto err;
|
|
|
|
}
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
if(buff[1] == 2) {
|
|
|
|
// authentication
|
|
|
|
char in[2];
|
|
|
|
char out[515];
|
|
|
|
char *cur = out;
|
|
|
|
int c;
|
|
|
|
*cur++ = 1; // version
|
|
|
|
c = ulen & 0xFF;
|
|
|
|
*cur++ = c;
|
|
|
|
memcpy(cur, user, c);
|
|
|
|
cur += c;
|
|
|
|
c = passlen & 0xFF;
|
|
|
|
*cur++ = c;
|
|
|
|
memcpy(cur, pass, c);
|
|
|
|
cur += c;
|
|
|
|
|
|
|
|
if((cur - out) != write_n_bytes(sock, out, cur - out))
|
|
|
|
goto err;
|
2012-01-28 00:55:37 +08:00
|
|
|
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
if(2 != read_n_bytes(sock, in, 2))
|
2011-11-06 21:12:50 +08:00
|
|
|
goto err;
|
2012-01-28 01:59:44 +08:00
|
|
|
if(in[0] != 1 || in[1] != 0) {
|
|
|
|
if(in[0] != 1)
|
|
|
|
goto err;
|
|
|
|
else
|
|
|
|
return BLOCKED;
|
2011-02-25 17:40:11 +08:00
|
|
|
}
|
2012-01-28 01:59:44 +08:00
|
|
|
}
|
|
|
|
int buff_iter = 0;
|
|
|
|
buff[buff_iter++] = 5; // version
|
|
|
|
buff[buff_iter++] = 1; // connect
|
|
|
|
buff[buff_iter++] = 0; // reserved
|
|
|
|
|
|
|
|
if(!dns_len) {
|
|
|
|
buff[buff_iter++] = 1; // ip v4
|
|
|
|
memcpy(buff + buff_iter, &ip, 4); // dest host
|
|
|
|
buff_iter += 4;
|
|
|
|
} else {
|
|
|
|
buff[buff_iter++] = 3; //dns
|
|
|
|
buff[buff_iter++] = dns_len & 0xFF;
|
|
|
|
memcpy(buff + buff_iter, dns_name, dns_len);
|
|
|
|
buff_iter += dns_len;
|
|
|
|
}
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
memcpy(buff + buff_iter, &port, 2); // dest port
|
|
|
|
buff_iter += 2;
|
2011-02-25 17:40:11 +08:00
|
|
|
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
if(buff_iter != write_n_bytes(sock, (char *) buff, buff_iter))
|
|
|
|
goto err;
|
2011-02-25 17:40:11 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
if(4 != read_n_bytes(sock, (char *) buff, 4))
|
|
|
|
goto err;
|
2011-02-25 17:40:11 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
if(buff[0] != 5 || buff[1] != 0)
|
|
|
|
goto err;
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
switch (buff[3]) {
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
len = 4;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
len = 16;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
len = 0;
|
|
|
|
if(1 != read_n_bytes(sock, (char *) &len, 1))
|
|
|
|
goto err;
|
|
|
|
break;
|
|
|
|
default:
|
2011-11-06 21:12:50 +08:00
|
|
|
goto err;
|
2012-01-28 01:59:44 +08:00
|
|
|
}
|
2011-02-25 17:40:11 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
if(len + 2 != read_n_bytes(sock, (char *) buff, len + 2))
|
|
|
|
goto err;
|
2011-02-25 17:40:11 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
break;
|
2011-11-06 21:12:50 +08:00
|
|
|
}
|
2011-02-25 17:40:11 +08:00
|
|
|
|
2011-11-06 21:12:50 +08:00
|
|
|
err:
|
|
|
|
return SOCKET_ERROR;
|
2011-02-25 17:40:11 +08:00
|
|
|
}
|
|
|
|
|
2011-11-06 21:12:50 +08:00
|
|
|
#define TP " ... "
|
|
|
|
#define DT "Dynamic chain"
|
|
|
|
#define ST "Strict chain"
|
|
|
|
#define RT "Random chain"
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
static int start_chain(int *fd, proxy_data * pd, char *begin_mark) {
|
2011-02-25 17:40:11 +08:00
|
|
|
struct sockaddr_in addr;
|
2012-01-28 03:00:22 +08:00
|
|
|
char ip_buf[16];
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
*fd = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
if(*fd == -1)
|
2011-02-25 17:40:11 +08:00
|
|
|
goto error;
|
2012-01-28 03:00:22 +08:00
|
|
|
|
|
|
|
pc_stringfromipv4(&pd->ip.octet[0], ip_buf);
|
2012-01-28 01:59:44 +08:00
|
|
|
proxychains_write_log(LOG_PREFIX "%s " TP " %s:%d ",
|
2012-01-28 03:00:22 +08:00
|
|
|
begin_mark, ip_buf, htons(pd->port));
|
2012-01-28 01:59:44 +08:00
|
|
|
pd->ps = PLAY_STATE;
|
2011-09-03 01:55:50 +08:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
2011-02-25 17:40:11 +08:00
|
|
|
addr.sin_family = AF_INET;
|
2011-11-06 21:12:50 +08:00
|
|
|
addr.sin_addr.s_addr = (in_addr_t) pd->ip.as_int;
|
2011-02-25 17:40:11 +08:00
|
|
|
addr.sin_port = pd->port;
|
2012-01-28 01:59:44 +08:00
|
|
|
if(timed_connect(*fd, (struct sockaddr *) &addr, sizeof(addr))) {
|
|
|
|
pd->ps = DOWN_STATE;
|
2011-02-25 17:40:11 +08:00
|
|
|
goto error1;
|
|
|
|
}
|
2012-01-28 01:59:44 +08:00
|
|
|
pd->ps = BUSY_STATE;
|
2011-02-25 17:40:11 +08:00
|
|
|
return SUCCESS;
|
2012-01-28 01:59:44 +08:00
|
|
|
error1:
|
|
|
|
proxychains_write_log(TP " timeout\n");
|
|
|
|
error:
|
|
|
|
if(*fd != -1)
|
2011-02-25 17:40:11 +08:00
|
|
|
close(*fd);
|
|
|
|
return SOCKET_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
static proxy_data *select_proxy(select_type how, proxy_data * pd, unsigned int proxy_count, unsigned int *offset) {
|
|
|
|
unsigned int i = 0, k = 0;
|
2011-09-04 07:45:16 +08:00
|
|
|
if(*offset >= proxy_count)
|
2011-02-25 17:40:11 +08:00
|
|
|
return NULL;
|
2012-01-28 01:59:44 +08:00
|
|
|
switch (how) {
|
2011-02-25 17:40:11 +08:00
|
|
|
case RANDOMLY:
|
|
|
|
srand(time(NULL));
|
|
|
|
do {
|
|
|
|
k++;
|
2012-01-28 01:59:44 +08:00
|
|
|
i = 0 + (unsigned int) (proxy_count * 1.0 * rand() / (RAND_MAX + 1.0));
|
|
|
|
} while(pd[i].ps != PLAY_STATE && k < proxy_count * 100);
|
|
|
|
break;
|
2011-02-25 17:40:11 +08:00
|
|
|
case FIFOLY:
|
2012-01-28 01:59:44 +08:00
|
|
|
for(i = *offset; i < proxy_count; i++) {
|
2011-09-04 07:45:16 +08:00
|
|
|
if(pd[i].ps == PLAY_STATE) {
|
|
|
|
*offset = i;
|
2011-02-25 17:40:11 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2012-01-28 01:59:44 +08:00
|
|
|
break;
|
2011-02-25 17:40:11 +08:00
|
|
|
}
|
2012-01-28 01:59:44 +08:00
|
|
|
if(i >= proxy_count)
|
2011-09-04 07:45:16 +08:00
|
|
|
i = 0;
|
2012-01-28 01:59:44 +08:00
|
|
|
return (pd[i].ps == PLAY_STATE) ? &pd[i] : NULL;
|
2011-02-25 17:40:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
static void release_all(proxy_data * pd, unsigned int proxy_count) {
|
2011-09-04 07:45:16 +08:00
|
|
|
unsigned int i;
|
2012-01-28 01:59:44 +08:00
|
|
|
for(i = 0; i < proxy_count; i++)
|
2011-09-04 07:45:16 +08:00
|
|
|
pd[i].ps = PLAY_STATE;
|
2011-02-25 17:40:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
static void release_busy(proxy_data * pd, unsigned int proxy_count) {
|
2011-09-04 07:45:16 +08:00
|
|
|
unsigned int i;
|
2012-01-28 01:59:44 +08:00
|
|
|
for(i = 0; i < proxy_count; i++)
|
2011-09-04 07:45:16 +08:00
|
|
|
if(pd[i].ps == BUSY_STATE)
|
|
|
|
pd[i].ps = PLAY_STATE;
|
2011-02-25 17:40:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
static unsigned int calc_alive(proxy_data * pd, unsigned int proxy_count) {
|
2011-09-04 07:45:16 +08:00
|
|
|
unsigned int i;
|
2012-01-28 01:59:44 +08:00
|
|
|
int alive_count = 0;
|
2011-09-04 07:45:16 +08:00
|
|
|
release_busy(pd, proxy_count);
|
2012-01-28 01:59:44 +08:00
|
|
|
for(i = 0; i < proxy_count; i++)
|
2011-09-04 07:45:16 +08:00
|
|
|
if(pd[i].ps == PLAY_STATE)
|
2011-02-25 17:40:11 +08:00
|
|
|
alive_count++;
|
|
|
|
return alive_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
static int chain_step(int ns, proxy_data * pfrom, proxy_data * pto) {
|
|
|
|
int retcode = -1;
|
|
|
|
char *hostname;
|
2012-01-28 03:00:22 +08:00
|
|
|
char ip_buf[16];
|
2012-01-28 02:14:17 +08:00
|
|
|
|
2011-09-04 07:45:16 +08:00
|
|
|
PDEBUG("chain_step()\n");
|
2012-01-28 02:14:17 +08:00
|
|
|
|
2012-01-26 19:44:42 +08:00
|
|
|
if(pto->ip.octet[0] == remote_dns_subnet) {
|
2011-11-07 00:46:11 +08:00
|
|
|
hostname = string_from_internal_ip(pto->ip);
|
2012-01-28 01:59:44 +08:00
|
|
|
if(!hostname)
|
|
|
|
goto usenumericip;
|
2011-11-06 21:12:50 +08:00
|
|
|
} else {
|
2012-01-28 01:59:44 +08:00
|
|
|
usenumericip:
|
2012-01-28 03:00:22 +08:00
|
|
|
pc_stringfromipv4(&pto->ip.octet[0], ip_buf);
|
|
|
|
hostname = ip_buf;
|
2011-11-06 21:12:50 +08:00
|
|
|
}
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
proxychains_write_log(TP " %s:%d ", hostname, htons(pto->port));
|
|
|
|
retcode = tunnel_to(ns, pto->ip, pto->port, pfrom->pt, pfrom->user, pfrom->pass);
|
|
|
|
switch (retcode) {
|
2011-02-25 17:40:11 +08:00
|
|
|
case SUCCESS:
|
2012-01-28 01:59:44 +08:00
|
|
|
pto->ps = BUSY_STATE;
|
2011-02-25 17:40:11 +08:00
|
|
|
break;
|
|
|
|
case BLOCKED:
|
2012-01-28 01:59:44 +08:00
|
|
|
pto->ps = BLOCKED_STATE;
|
2011-02-25 17:40:11 +08:00
|
|
|
proxychains_write_log("<--denied\n");
|
|
|
|
close(ns);
|
|
|
|
break;
|
|
|
|
case SOCKET_ERROR:
|
2012-01-28 01:59:44 +08:00
|
|
|
pto->ps = DOWN_STATE;
|
2011-02-25 17:40:11 +08:00
|
|
|
proxychains_write_log("<--timeout\n");
|
|
|
|
close(ns);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return retcode;
|
|
|
|
}
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
int connect_proxy_chain(int sock, ip_type target_ip,
|
|
|
|
unsigned short target_port, proxy_data * pd,
|
|
|
|
unsigned int proxy_count, chain_type ct, unsigned int max_chain) {
|
2011-02-25 17:40:11 +08:00
|
|
|
proxy_data p4;
|
2012-01-28 01:59:44 +08:00
|
|
|
proxy_data *p1, *p2, *p3;
|
|
|
|
int ns = -1;
|
|
|
|
unsigned int offset = 0;
|
|
|
|
unsigned int alive_count = 0;
|
|
|
|
unsigned int curr_len = 0;
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
p3 = &p4;
|
2012-01-28 02:14:17 +08:00
|
|
|
|
2011-09-04 07:45:16 +08:00
|
|
|
PDEBUG("connect_proxy_chain\n");
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
again:
|
2011-09-04 07:45:16 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
switch (ct) {
|
2011-02-25 17:40:11 +08:00
|
|
|
case DYNAMIC_TYPE:
|
2012-01-28 01:59:44 +08:00
|
|
|
alive_count = calc_alive(pd, proxy_count);
|
|
|
|
offset = 0;
|
|
|
|
do {
|
|
|
|
if(!(p1 = select_proxy(FIFOLY, pd, proxy_count, &offset)))
|
|
|
|
goto error_more;
|
|
|
|
} while(SUCCESS != start_chain(&ns, p1, DT) && offset < proxy_count);
|
|
|
|
for(;;) {
|
|
|
|
p2 = select_proxy(FIFOLY, pd, proxy_count, &offset);
|
|
|
|
if(!p2)
|
|
|
|
break;
|
|
|
|
if(SUCCESS != chain_step(ns, p1, p2)) {
|
|
|
|
PDEBUG("GOTO AGAIN 1\n");
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
p1 = p2;
|
2011-09-04 07:45:16 +08:00
|
|
|
}
|
2012-01-28 01:59:44 +08:00
|
|
|
//proxychains_write_log(TP);
|
|
|
|
p3->ip = target_ip;
|
|
|
|
p3->port = target_port;
|
|
|
|
if(SUCCESS != chain_step(ns, p1, p3))
|
|
|
|
goto error;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STRICT_TYPE:
|
|
|
|
alive_count = calc_alive(pd, proxy_count);
|
|
|
|
offset = 0;
|
|
|
|
if(!(p1 = select_proxy(FIFOLY, pd, proxy_count, &offset))) {
|
|
|
|
PDEBUG("select_proxy failed\n");
|
2011-02-25 17:40:11 +08:00
|
|
|
goto error_strict;
|
2011-09-04 07:45:16 +08:00
|
|
|
}
|
2012-01-28 01:59:44 +08:00
|
|
|
if(SUCCESS != start_chain(&ns, p1, ST)) {
|
|
|
|
PDEBUG("start_chain failed\n");
|
|
|
|
goto error_strict;
|
|
|
|
}
|
|
|
|
while(offset < proxy_count) {
|
|
|
|
if(!(p2 = select_proxy(FIFOLY, pd, proxy_count, &offset)))
|
|
|
|
break;
|
|
|
|
if(SUCCESS != chain_step(ns, p1, p2)) {
|
|
|
|
PDEBUG("chain_step failed\n");
|
|
|
|
goto error_strict;
|
|
|
|
}
|
|
|
|
p1 = p2;
|
|
|
|
}
|
|
|
|
//proxychains_write_log(TP);
|
|
|
|
p3->ip = target_ip;
|
|
|
|
p3->port = target_port;
|
|
|
|
if(SUCCESS != chain_step(ns, p1, p3))
|
|
|
|
goto error;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RANDOM_TYPE:
|
|
|
|
alive_count = calc_alive(pd, proxy_count);
|
|
|
|
if(alive_count < max_chain)
|
2011-02-25 17:40:11 +08:00
|
|
|
goto error_more;
|
2012-01-28 01:59:44 +08:00
|
|
|
curr_len = offset = 0;
|
|
|
|
do {
|
|
|
|
if(!(p1 = select_proxy(RANDOMLY, pd, proxy_count, &offset)))
|
|
|
|
goto error_more;
|
|
|
|
} while(SUCCESS != start_chain(&ns, p1, RT) && offset < max_chain);
|
|
|
|
while(++curr_len < max_chain) {
|
|
|
|
if(!(p2 = select_proxy(RANDOMLY, pd, proxy_count, &offset)))
|
|
|
|
goto error_more;
|
|
|
|
if(SUCCESS != chain_step(ns, p1, p2)) {
|
|
|
|
PDEBUG("GOTO AGAIN 2\n");
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
p1 = p2;
|
2012-01-28 00:55:37 +08:00
|
|
|
}
|
2012-01-28 01:59:44 +08:00
|
|
|
//proxychains_write_log(TP);
|
|
|
|
p3->ip = target_ip;
|
|
|
|
p3->port = target_port;
|
|
|
|
if(SUCCESS != chain_step(ns, p1, p3))
|
|
|
|
goto error;
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-02-25 17:40:11 +08:00
|
|
|
}
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
proxychains_write_log(TP " OK\n");
|
|
|
|
dup2(ns, sock);
|
2011-02-25 17:40:11 +08:00
|
|
|
close(ns);
|
|
|
|
return 0;
|
2012-01-28 01:59:44 +08:00
|
|
|
error:
|
|
|
|
if(ns != -1)
|
2011-02-25 17:40:11 +08:00
|
|
|
close(ns);
|
2012-01-28 01:59:44 +08:00
|
|
|
errno = ECONNREFUSED; // for nmap ;)
|
2011-02-25 17:40:11 +08:00
|
|
|
return -1;
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
error_more:
|
2011-02-25 17:40:11 +08:00
|
|
|
proxychains_write_log("\n!!!need more proxies!!!\n");
|
2012-01-28 01:59:44 +08:00
|
|
|
error_strict:
|
2011-09-04 07:45:16 +08:00
|
|
|
PDEBUG("error\n");
|
2012-01-28 02:14:17 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
release_all(pd, proxy_count);
|
|
|
|
if(ns != -1)
|
2011-02-25 17:40:11 +08:00
|
|
|
close(ns);
|
|
|
|
errno = ETIMEDOUT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-11-07 00:46:11 +08:00
|
|
|
// TODO: all those buffers aren't threadsafe, but since no memory allocation happens there shouldnt be any segfaults
|
2011-02-25 17:40:11 +08:00
|
|
|
static struct hostent hostent_space;
|
|
|
|
static in_addr_t resolved_addr;
|
2012-01-28 01:59:44 +08:00
|
|
|
static char *resolved_addr_p[2];
|
|
|
|
static char addr_name[1024 * 8];
|
|
|
|
static const ip_type local_host = { {127, 0, 0, 1} };
|
|
|
|
struct hostent *proxy_gethostbyname(const char *name) {
|
2011-02-25 17:40:11 +08:00
|
|
|
char buff[256];
|
2011-11-06 21:12:50 +08:00
|
|
|
uint32_t i, hash;
|
2011-11-07 00:46:11 +08:00
|
|
|
// yep, new_mem never gets freed. once you passed a fake ip to the client, you can't "retreat" it
|
2012-01-28 01:59:44 +08:00
|
|
|
void *new_mem;
|
2011-11-06 21:12:50 +08:00
|
|
|
size_t l;
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
struct hostent *hp;
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
resolved_addr_p[0] = (char *) &resolved_addr;
|
2011-11-06 21:12:50 +08:00
|
|
|
resolved_addr_p[1] = NULL;
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-11-06 21:12:50 +08:00
|
|
|
hostent_space.h_addr_list = resolved_addr_p;
|
|
|
|
|
2011-02-25 17:40:11 +08:00
|
|
|
resolved_addr = 0;
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
gethostname(buff, sizeof(buff));
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-11-06 21:12:50 +08:00
|
|
|
if(!strcmp(buff, name)) {
|
|
|
|
resolved_addr = inet_addr(buff);
|
2012-01-28 01:59:44 +08:00
|
|
|
if(resolved_addr == (in_addr_t) (-1))
|
2011-11-06 21:12:50 +08:00
|
|
|
resolved_addr = (in_addr_t) (local_host.as_int);
|
|
|
|
return &hostent_space;
|
|
|
|
}
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-09-03 01:55:50 +08:00
|
|
|
memset(buff, 0, sizeof(buff));
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
while((hp = gethostent()))
|
|
|
|
if(!strcmp(hp->h_name, name))
|
2012-01-28 00:55:37 +08:00
|
|
|
return hp;
|
|
|
|
|
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
hash = dalias_hash((char *) name);
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-11-07 00:46:11 +08:00
|
|
|
#ifdef THREAD_SAFE
|
|
|
|
pthread_mutex_lock(&internal_ips_lock);
|
|
|
|
#endif
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-11-07 11:11:20 +08:00
|
|
|
// see if we already have this dns entry saved.
|
2011-11-06 21:12:50 +08:00
|
|
|
if(internal_ips.counter) {
|
2012-01-28 01:59:44 +08:00
|
|
|
for(i = 0; i < internal_ips.counter; i++) {
|
2011-11-07 11:11:20 +08:00
|
|
|
if(internal_ips.list[i]->hash == hash && !strcmp(name, internal_ips.list[i]->string)) {
|
2011-11-06 21:12:50 +08:00
|
|
|
resolved_addr = make_internal_ip(i);
|
2011-11-07 06:35:57 +08:00
|
|
|
PDEBUG("got cached ip for %s\n", name);
|
2011-11-06 21:12:50 +08:00
|
|
|
goto have_ip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-07 11:11:20 +08:00
|
|
|
// grow list if needed.
|
2011-11-06 21:12:50 +08:00
|
|
|
if(internal_ips.capa < internal_ips.counter + 1) {
|
2011-11-07 06:35:57 +08:00
|
|
|
PDEBUG("realloc\n");
|
2012-01-28 01:59:44 +08:00
|
|
|
new_mem = realloc(internal_ips.list, (internal_ips.capa + 16) * sizeof(void *));
|
2011-11-06 21:12:50 +08:00
|
|
|
if(new_mem) {
|
|
|
|
internal_ips.capa += 16;
|
|
|
|
internal_ips.list = new_mem;
|
|
|
|
} else {
|
2012-01-28 01:59:44 +08:00
|
|
|
oom:
|
2011-11-06 21:12:50 +08:00
|
|
|
proxychains_write_log("out of mem\n");
|
2011-11-07 00:46:11 +08:00
|
|
|
goto err_plus_unlock;
|
2011-11-06 21:12:50 +08:00
|
|
|
}
|
2011-02-25 17:40:11 +08:00
|
|
|
}
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-11-06 21:12:50 +08:00
|
|
|
resolved_addr = make_internal_ip(internal_ips.counter);
|
2012-01-28 01:59:44 +08:00
|
|
|
if(resolved_addr == (in_addr_t) - 1)
|
|
|
|
goto err_plus_unlock;
|
2011-11-06 21:12:50 +08:00
|
|
|
|
|
|
|
l = strlen(name);
|
|
|
|
new_mem = malloc(sizeof(string_hash_tuple) + l + 1);
|
2012-01-28 00:55:37 +08:00
|
|
|
if(!new_mem)
|
2011-11-06 21:12:50 +08:00
|
|
|
goto oom;
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-11-07 06:35:57 +08:00
|
|
|
PDEBUG("creating new entry %d for ip of %s\n", (int) internal_ips.counter, name);
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-11-06 21:12:50 +08:00
|
|
|
internal_ips.list[internal_ips.counter] = new_mem;
|
|
|
|
internal_ips.list[internal_ips.counter]->hash = hash;
|
2012-01-28 01:59:44 +08:00
|
|
|
internal_ips.list[internal_ips.counter]->string = (char *) new_mem + sizeof(string_hash_tuple);
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-11-06 21:12:50 +08:00
|
|
|
memcpy(internal_ips.list[internal_ips.counter]->string, name, l + 1);
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-11-06 21:12:50 +08:00
|
|
|
internal_ips.counter += 1;
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-11-06 21:12:50 +08:00
|
|
|
have_ip:
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-11-07 00:46:11 +08:00
|
|
|
#ifdef THREAD_SAFE
|
|
|
|
pthread_mutex_unlock(&internal_ips_lock);
|
|
|
|
#endif
|
|
|
|
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-11-07 00:46:11 +08:00
|
|
|
strncpy(addr_name, name, sizeof(addr_name));
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2011-11-06 21:12:50 +08:00
|
|
|
hostent_space.h_name = addr_name;
|
2012-01-28 01:59:44 +08:00
|
|
|
hostent_space.h_length = sizeof(in_addr_t);
|
2011-02-25 17:40:11 +08:00
|
|
|
return &hostent_space;
|
2011-11-07 00:46:11 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
err_plus_unlock:
|
2011-11-07 00:46:11 +08:00
|
|
|
#ifdef THREAD_SAFE
|
|
|
|
pthread_mutex_unlock(&internal_ips_lock);
|
|
|
|
#endif
|
2011-02-25 17:40:11 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-01-28 01:59:44 +08:00
|
|
|
int proxy_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res) {
|
2011-02-25 17:40:11 +08:00
|
|
|
struct servent *se = NULL;
|
|
|
|
struct hostent *hp = NULL;
|
2012-01-28 01:59:44 +08:00
|
|
|
struct sockaddr *sockaddr_space = NULL;
|
|
|
|
struct addrinfo *addrinfo_space = NULL;
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
// printf("proxy_getaddrinfo node %s service %s\n",node,service);
|
2011-02-25 17:40:11 +08:00
|
|
|
addrinfo_space = malloc(sizeof(struct addrinfo));
|
|
|
|
if(!addrinfo_space)
|
|
|
|
goto err1;
|
|
|
|
sockaddr_space = malloc(sizeof(struct sockaddr));
|
|
|
|
if(!sockaddr_space)
|
|
|
|
goto err2;
|
2011-09-03 01:55:50 +08:00
|
|
|
memset(sockaddr_space, 0, sizeof(*sockaddr_space));
|
|
|
|
memset(addrinfo_space, 0, sizeof(*addrinfo_space));
|
2012-01-28 01:59:44 +08:00
|
|
|
if(node && !inet_aton(node, &((struct sockaddr_in *) sockaddr_space)->sin_addr)) {
|
2011-02-25 17:40:11 +08:00
|
|
|
hp = proxy_gethostbyname(node);
|
2012-01-28 01:59:44 +08:00
|
|
|
if(hp)
|
|
|
|
memcpy(&((struct sockaddr_in *) sockaddr_space)->sin_addr,
|
|
|
|
*(hp->h_addr_list), sizeof(in_addr_t));
|
2011-02-25 17:40:11 +08:00
|
|
|
else
|
|
|
|
goto err3;
|
|
|
|
}
|
2012-01-28 01:59:44 +08:00
|
|
|
if(service)
|
2011-02-25 17:40:11 +08:00
|
|
|
se = getservbyname(service, NULL);
|
2012-01-28 00:55:37 +08:00
|
|
|
|
2012-01-28 01:59:44 +08:00
|
|
|
if(!se) {
|
|
|
|
((struct sockaddr_in *) sockaddr_space)->sin_port = htons(atoi(service ? : "0"));
|
2012-01-28 00:55:37 +08:00
|
|
|
} else
|
2012-01-28 01:59:44 +08:00
|
|
|
((struct sockaddr_in *) sockaddr_space)->sin_port = se->s_port;
|
2011-02-25 17:40:11 +08:00
|
|
|
|
|
|
|
*res = addrinfo_space;
|
|
|
|
(*res)->ai_addr = sockaddr_space;
|
2012-01-28 01:59:44 +08:00
|
|
|
if(node)
|
2011-02-25 17:40:11 +08:00
|
|
|
strcpy(addr_name, node);
|
|
|
|
(*res)->ai_canonname = addr_name;
|
|
|
|
(*res)->ai_next = NULL;
|
|
|
|
(*res)->ai_family = sockaddr_space->sa_family = AF_INET;
|
|
|
|
(*res)->ai_socktype = hints->ai_socktype;
|
|
|
|
(*res)->ai_flags = hints->ai_flags;
|
|
|
|
(*res)->ai_protocol = hints->ai_protocol;
|
|
|
|
(*res)->ai_addrlen = sizeof(*sockaddr_space);
|
|
|
|
goto out;
|
2012-01-28 01:59:44 +08:00
|
|
|
err3:
|
2011-02-25 17:40:11 +08:00
|
|
|
free(sockaddr_space);
|
2012-01-28 01:59:44 +08:00
|
|
|
err2:
|
2011-02-25 17:40:11 +08:00
|
|
|
free(addrinfo_space);
|
2012-01-28 01:59:44 +08:00
|
|
|
err1:
|
2011-02-25 17:40:11 +08:00
|
|
|
return 1;
|
2012-01-28 01:59:44 +08:00
|
|
|
out:
|
2011-02-25 17:40:11 +08:00
|
|
|
return 0;
|
|
|
|
}
|