2011-02-25 09:40:11 +00:00
|
|
|
/***************************************************************************
|
|
|
|
libproxychains.c - description
|
|
|
|
-------------------
|
|
|
|
begin : Tue May 14 2002
|
|
|
|
copyright : netcreature (C) 2002
|
|
|
|
email : netcreature@users.sourceforge.net
|
|
|
|
***************************************************************************/
|
|
|
|
/* GPL */
|
|
|
|
/***************************************************************************
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************/
|
2011-09-03 23:45:16 +00:00
|
|
|
|
|
|
|
#undef _GNU_SOURCE
|
2011-02-25 09:40:11 +00:00
|
|
|
#define _GNU_SOURCE
|
2011-09-03 23:45:16 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2011-09-02 17:55:50 +00:00
|
|
|
#include <string.h>
|
2011-02-25 09:40:11 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2011-09-02 17:55:50 +00:00
|
|
|
#include <fcntl.h>
|
2011-02-25 09:40:11 +00:00
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "core.h"
|
2011-09-10 20:32:27 +00:00
|
|
|
#include "common.h"
|
2011-02-25 09:40:11 +00:00
|
|
|
|
|
|
|
#define satosin(x) ((struct sockaddr_in *) &(x))
|
|
|
|
#define SOCKADDR(x) (satosin(x)->sin_addr.s_addr)
|
|
|
|
#define SOCKADDR_2(x) (satosin(x)->sin_addr)
|
|
|
|
#define SOCKPORT(x) (satosin(x)->sin_port)
|
|
|
|
#define SOCKFAMILY(x) (satosin(x)->sin_family)
|
|
|
|
#define MAX_CHAIN 30*1024
|
|
|
|
|
|
|
|
int tcp_read_time_out;
|
|
|
|
int tcp_connect_time_out;
|
|
|
|
chain_type proxychains_ct;
|
|
|
|
proxy_data proxychains_pd[MAX_CHAIN];
|
2011-09-03 23:45:16 +00:00
|
|
|
unsigned int proxychains_proxy_count = 0;
|
2011-02-25 09:40:11 +00:00
|
|
|
int proxychains_got_chain_data = 0;
|
2011-09-03 23:45:16 +00:00
|
|
|
unsigned int proxychains_max_chain = 1;
|
2011-02-25 09:40:11 +00:00
|
|
|
int proxychains_quiet_mode = 0;
|
|
|
|
int proxychains_resolver = 0;
|
|
|
|
static int init_l = 0;
|
2011-02-25 15:16:58 +00:00
|
|
|
localaddr_arg localnet_addr[MAX_LOCALNET];
|
|
|
|
size_t num_localnet_addr = 0;
|
2012-01-26 11:44:42 +00:00
|
|
|
unsigned int remote_dns_subnet = 224;
|
2011-02-25 15:16:58 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
static inline void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_type * ct);
|
2011-02-25 15:16:58 +00:00
|
|
|
static void init_lib(void);
|
2011-02-25 09:40:11 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
static void init_lib(void) {
|
2012-01-27 20:30:42 +00:00
|
|
|
MUTEX_INIT(&internal_ips_lock, NULL);
|
2012-01-24 07:26:37 +00:00
|
|
|
/* read the config file */
|
|
|
|
get_chain_data(proxychains_pd, &proxychains_proxy_count, &proxychains_ct);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-11-06 13:12:50 +00:00
|
|
|
proxychains_write_log(LOG_PREFIX "DLL init\n");
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
true_connect = (connect_t) dlsym(RTLD_NEXT, "connect");
|
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
if(!true_connect) {
|
2011-02-25 09:40:11 +00:00
|
|
|
fprintf(stderr, "Cannot load symbol 'connect' %s\n", dlerror());
|
|
|
|
exit(1);
|
|
|
|
} else {
|
2012-01-27 17:59:44 +00:00
|
|
|
PDEBUG("loaded symbol 'connect'" " real addr %p wrapped addr %p\n", true_connect, connect);
|
2011-02-25 09:40:11 +00:00
|
|
|
}
|
2012-01-27 17:59:44 +00:00
|
|
|
if(connect == true_connect) {
|
2011-09-03 23:45:16 +00:00
|
|
|
PDEBUG("circular reference detected, aborting!\n");
|
|
|
|
abort();
|
|
|
|
}
|
2012-01-27 16:55:37 +00:00
|
|
|
|
|
|
|
true_gethostbyname = (gethostbyname_t)
|
2012-01-27 17:59:44 +00:00
|
|
|
dlsym(RTLD_NEXT, "gethostbyname");
|
2011-02-25 09:40:11 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
if(!true_gethostbyname) {
|
|
|
|
fprintf(stderr, "Cannot load symbol 'gethostbyname' %s\n", dlerror());
|
2011-02-25 09:40:11 +00:00
|
|
|
exit(1);
|
|
|
|
} else {
|
2012-01-27 17:59:44 +00:00
|
|
|
PDEBUG("loaded symbol 'gethostbyname'"
|
|
|
|
" real addr %p wrapped addr %p\n", true_gethostbyname, gethostbyname);
|
2011-02-25 09:40:11 +00:00
|
|
|
}
|
2012-01-27 16:55:37 +00:00
|
|
|
true_getaddrinfo = (getaddrinfo_t)
|
2012-01-27 17:59:44 +00:00
|
|
|
dlsym(RTLD_NEXT, "getaddrinfo");
|
2011-02-25 09:40:11 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
if(!true_getaddrinfo) {
|
|
|
|
fprintf(stderr, "Cannot load symbol 'getaddrinfo' %s\n", dlerror());
|
2011-02-25 09:40:11 +00:00
|
|
|
exit(1);
|
|
|
|
} else {
|
2012-01-27 17:59:44 +00:00
|
|
|
PDEBUG("loaded symbol 'getaddrinfo'" " real addr %p wrapped addr %p\n", true_getaddrinfo, getaddrinfo);
|
2011-02-25 09:40:11 +00:00
|
|
|
}
|
2012-01-27 16:55:37 +00:00
|
|
|
true_freeaddrinfo = (freeaddrinfo_t)
|
2012-01-27 17:59:44 +00:00
|
|
|
dlsym(RTLD_NEXT, "freeaddrinfo");
|
2011-02-25 09:40:11 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
if(!true_freeaddrinfo) {
|
|
|
|
fprintf(stderr, "Cannot load symbol 'freeaddrinfo' %s\n", dlerror());
|
2011-02-25 09:40:11 +00:00
|
|
|
exit(1);
|
|
|
|
} else {
|
2012-01-27 17:59:44 +00:00
|
|
|
PDEBUG("loaded symbol 'freeaddrinfo'"
|
|
|
|
" real addr %p wrapped addr %p\n", true_freeaddrinfo, freeaddrinfo);
|
2011-02-25 09:40:11 +00:00
|
|
|
}
|
2012-01-27 16:55:37 +00:00
|
|
|
true_gethostbyaddr = (gethostbyaddr_t)
|
2012-01-27 17:59:44 +00:00
|
|
|
dlsym(RTLD_NEXT, "gethostbyaddr");
|
2011-02-25 09:40:11 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
if(!true_gethostbyaddr) {
|
|
|
|
fprintf(stderr, "Cannot load symbol 'gethostbyaddr' %s\n", dlerror());
|
2011-02-25 09:40:11 +00:00
|
|
|
exit(1);
|
|
|
|
} else {
|
2012-01-27 17:59:44 +00:00
|
|
|
PDEBUG("loaded symbol 'gethostbyaddr'"
|
|
|
|
" real addr %p wrapped addr %p\n", true_gethostbyaddr, gethostbyaddr);
|
2011-02-25 09:40:11 +00:00
|
|
|
}
|
2012-01-27 16:55:37 +00:00
|
|
|
true_getnameinfo = (getnameinfo_t)
|
2012-01-27 17:59:44 +00:00
|
|
|
dlsym(RTLD_NEXT, "getnameinfo");
|
2011-02-25 09:40:11 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
if(!true_getnameinfo) {
|
|
|
|
fprintf(stderr, "Cannot load symbol 'getnameinfo' %s\n", dlerror());
|
2011-02-25 09:40:11 +00:00
|
|
|
exit(1);
|
|
|
|
} else {
|
2012-01-27 17:59:44 +00:00
|
|
|
PDEBUG("loaded symbol 'getnameinfo'" " real addr %p wrapped addr %p\n", true_getnameinfo, getnameinfo);
|
2011-02-25 09:40:11 +00:00
|
|
|
}
|
|
|
|
init_l = 1;
|
|
|
|
}
|
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
static inline void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_type * ct) {
|
|
|
|
int count = 0, port_n = 0, list = 0;
|
|
|
|
char buff[1024], type[1024], host[1024], user[1024];
|
2011-02-25 12:21:34 +00:00
|
|
|
char *env;
|
2011-03-22 14:04:52 +00:00
|
|
|
char local_in_addr_port[32];
|
|
|
|
char local_in_addr[32], local_in_port[32], local_netmask[32];
|
2012-01-27 17:59:44 +00:00
|
|
|
FILE *file;
|
2011-02-25 09:40:11 +00:00
|
|
|
|
|
|
|
if(proxychains_got_chain_data)
|
2012-01-27 17:59:44 +00:00
|
|
|
return;
|
2011-02-25 09:40:11 +00:00
|
|
|
|
|
|
|
//Some defaults
|
2012-01-27 17:59:44 +00:00
|
|
|
tcp_read_time_out = 4 * 1000;
|
|
|
|
tcp_connect_time_out = 10 * 1000;
|
2011-09-03 23:45:16 +00:00
|
|
|
*ct = DYNAMIC_TYPE;
|
2011-02-25 09:40:11 +00:00
|
|
|
|
2011-02-25 12:21:34 +00:00
|
|
|
/*
|
|
|
|
* Get path to configuration file from env this file has priority
|
|
|
|
* if it's defined.
|
|
|
|
*/
|
2011-09-10 20:32:27 +00:00
|
|
|
env = getenv(PROXYCHAINS_CONF_FILE_ENV_VAR);
|
2011-02-25 12:21:34 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
snprintf(buff, 256, "%s/.proxychains/proxychains.conf", getenv("HOME"));
|
2011-02-25 09:40:11 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
if(!env || (!(file = fopen(env, "r"))))
|
|
|
|
if(!(file = fopen("./proxychains.conf", "r")))
|
|
|
|
if(!(file = fopen(buff, "r")))
|
|
|
|
if(!(file = fopen("/etc/proxychains.conf", "r"))) {
|
|
|
|
perror("Can't locate proxychains.conf");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-01-24 07:26:37 +00:00
|
|
|
env = getenv(PROXYCHAINS_QUIET_MODE_ENV_VAR);
|
2012-01-27 17:59:44 +00:00
|
|
|
if(env && *env == '1')
|
|
|
|
proxychains_quiet_mode = 1;
|
2011-02-25 09:40:11 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
while(fgets(buff, sizeof(buff), file)) {
|
|
|
|
if(buff[0] != '\n' && buff[strspn(buff, " ")] != '#') {
|
2011-02-25 09:40:11 +00:00
|
|
|
if(list) {
|
2011-09-02 17:55:50 +00:00
|
|
|
memset(&pd[count], 0, sizeof(proxy_data));
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-11-06 13:12:50 +00:00
|
|
|
pd[count].ps = PLAY_STATE;
|
|
|
|
port_n = 0;
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
sscanf(buff, "%s %s %d %s %s", type, host, &port_n, pd[count].user, pd[count].pass);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-11-06 13:12:50 +00:00
|
|
|
pd[count].ip.as_int = (uint32_t) inet_addr(host);
|
2012-01-27 17:59:44 +00:00
|
|
|
pd[count].port = htons((unsigned short) port_n);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
if(!strcmp(type, "http")) {
|
2011-11-06 13:12:50 +00:00
|
|
|
pd[count].pt = HTTP_TYPE;
|
2012-01-27 17:59:44 +00:00
|
|
|
} else if(!strcmp(type, "socks4")) {
|
2011-11-06 13:12:50 +00:00
|
|
|
pd[count].pt = SOCKS4_TYPE;
|
2012-01-27 17:59:44 +00:00
|
|
|
} else if(!strcmp(type, "socks5")) {
|
2011-11-06 13:12:50 +00:00
|
|
|
pd[count].pt = SOCKS5_TYPE;
|
2012-01-27 16:55:37 +00:00
|
|
|
} else
|
2011-11-06 13:12:50 +00:00
|
|
|
continue;
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
if(pd[count].ip.as_int && port_n && pd[count].ip.as_int != (uint32_t) - 1)
|
|
|
|
if(++count == MAX_CHAIN)
|
2011-02-25 09:40:11 +00:00
|
|
|
break;
|
2012-01-27 17:59:44 +00:00
|
|
|
} else {
|
|
|
|
if(strstr(buff, "[ProxyList]")) {
|
|
|
|
list = 1;
|
2012-01-26 11:44:42 +00:00
|
|
|
} else if(strstr(buff, "random_chain")) {
|
2012-01-27 17:59:44 +00:00
|
|
|
*ct = RANDOM_TYPE;
|
2012-01-26 11:44:42 +00:00
|
|
|
} else if(strstr(buff, "strict_chain")) {
|
2012-01-27 17:59:44 +00:00
|
|
|
*ct = STRICT_TYPE;
|
2012-01-26 11:44:42 +00:00
|
|
|
} else if(strstr(buff, "dynamic_chain")) {
|
2012-01-27 17:59:44 +00:00
|
|
|
*ct = DYNAMIC_TYPE;
|
|
|
|
} else if(strstr(buff, "tcp_read_time_out")) {
|
2012-01-26 11:44:42 +00:00
|
|
|
sscanf(buff, "%s %d", user, &tcp_read_time_out);
|
2012-01-27 17:59:44 +00:00
|
|
|
} else if(strstr(buff, "tcp_connect_time_out")) {
|
2012-01-26 11:44:42 +00:00
|
|
|
sscanf(buff, "%s %d", user, &tcp_connect_time_out);
|
2012-01-27 17:59:44 +00:00
|
|
|
} else if(strstr(buff, "remote_dns_subnet")) {
|
2012-01-26 11:44:42 +00:00
|
|
|
sscanf(buff, "%s %d", user, &remote_dns_subnet);
|
|
|
|
if(remote_dns_subnet >= 256) {
|
2012-01-27 17:59:44 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"remote_dns_subnet: invalid value. requires a number between 0 and 255.\n");
|
2012-01-26 11:44:42 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else if(strstr(buff, "localnet")) {
|
2012-01-27 17:59:44 +00:00
|
|
|
if(sscanf(buff, "%s %21[^/]/%15s", user, local_in_addr_port, local_netmask) < 3) {
|
2011-03-22 14:04:52 +00:00
|
|
|
fprintf(stderr, "localnet format error");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/* clean previously used buffer */
|
2012-01-27 17:59:44 +00:00
|
|
|
memset(local_in_port, 0, sizeof(local_in_port) / sizeof(local_in_port[0]));
|
2011-03-22 14:04:52 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
if(sscanf(local_in_addr_port, "%15[^:]:%5s", local_in_addr, local_in_port) < 2) {
|
|
|
|
PDEBUG("added localnet: netaddr=%s, port=%s\n",
|
|
|
|
local_in_addr, local_netmask);
|
2011-03-22 14:04:52 +00:00
|
|
|
} else {
|
2012-01-27 17:59:44 +00:00
|
|
|
PDEBUG("added localnet: netaddr=%s, port=%s, netmask=%s\n",
|
|
|
|
local_in_addr, local_in_port, local_netmask);
|
2011-03-22 14:04:52 +00:00
|
|
|
}
|
2012-01-27 17:59:44 +00:00
|
|
|
if(num_localnet_addr < MAX_LOCALNET) {
|
2011-02-25 15:16:58 +00:00
|
|
|
int error;
|
2012-01-27 17:59:44 +00:00
|
|
|
error =
|
|
|
|
inet_pton(AF_INET, local_in_addr,
|
|
|
|
&localnet_addr[num_localnet_addr].in_addr);
|
|
|
|
if(error <= 0) {
|
2011-02-25 15:16:58 +00:00
|
|
|
fprintf(stderr, "localnet address error\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-01-27 17:59:44 +00:00
|
|
|
error =
|
|
|
|
inet_pton(AF_INET, local_netmask,
|
|
|
|
&localnet_addr[num_localnet_addr].netmask);
|
|
|
|
if(error <= 0) {
|
2011-03-22 14:04:52 +00:00
|
|
|
fprintf(stderr, "localnet netmask error\n");
|
2011-02-25 15:16:58 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2012-01-27 17:59:44 +00:00
|
|
|
if(local_in_port[0]) {
|
|
|
|
localnet_addr[num_localnet_addr].port =
|
|
|
|
(short) atoi(local_in_port);
|
2011-03-22 14:04:52 +00:00
|
|
|
} else {
|
|
|
|
localnet_addr[num_localnet_addr].port = 0;
|
|
|
|
}
|
2011-02-25 15:16:58 +00:00
|
|
|
++num_localnet_addr;
|
2012-01-27 17:59:44 +00:00
|
|
|
} else {
|
2011-02-25 15:16:58 +00:00
|
|
|
fprintf(stderr, "# of localnet exceed %d.\n", MAX_LOCALNET);
|
|
|
|
}
|
2012-01-27 17:59:44 +00:00
|
|
|
} else if(strstr(buff, "chain_len")) {
|
|
|
|
char *pc;
|
|
|
|
int len;
|
|
|
|
pc = strchr(buff, '=');
|
|
|
|
len = atoi(++pc);
|
|
|
|
proxychains_max_chain = (len ? len : 1);
|
|
|
|
} else if(strstr(buff, "quiet_mode")) {
|
|
|
|
proxychains_quiet_mode = 1;
|
|
|
|
} else if(strstr(buff, "proxy_dns")) {
|
|
|
|
proxychains_resolver = 1;
|
2011-02-25 09:40:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(file);
|
2011-09-03 23:45:16 +00:00
|
|
|
*proxy_count = count;
|
|
|
|
proxychains_got_chain_data = 1;
|
2011-02-25 09:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
int connect(int sock, const struct sockaddr *addr, unsigned int len) {
|
|
|
|
int socktype = 0, flags = 0, ret = 0;
|
2011-09-03 23:45:16 +00:00
|
|
|
socklen_t optlen = 0;
|
2011-11-06 13:12:50 +00:00
|
|
|
ip_type dest_ip;
|
2011-09-03 23:45:16 +00:00
|
|
|
#ifdef DEBUG
|
2011-02-25 15:16:58 +00:00
|
|
|
char str[256];
|
2011-09-03 23:45:16 +00:00
|
|
|
#endif
|
2011-02-25 15:16:58 +00:00
|
|
|
struct in_addr *p_addr_in;
|
2011-03-22 14:04:52 +00:00
|
|
|
unsigned short port;
|
2011-02-25 15:16:58 +00:00
|
|
|
size_t i;
|
2011-02-25 09:40:11 +00:00
|
|
|
|
|
|
|
if(!init_l)
|
|
|
|
init_lib();
|
2012-01-27 17:59:44 +00:00
|
|
|
optlen = sizeof(socktype);
|
|
|
|
getsockopt(sock, SOL_SOCKET, SO_TYPE, &socktype, &optlen);
|
|
|
|
if(!(SOCKFAMILY(*addr) == AF_INET && socktype == SOCK_STREAM))
|
|
|
|
return true_connect(sock, addr, len);
|
2011-02-25 15:16:58 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
p_addr_in = &((struct sockaddr_in *) addr)->sin_addr;
|
|
|
|
port = ntohs(((struct sockaddr_in *) addr)->sin_port);
|
2011-02-25 15:16:58 +00:00
|
|
|
|
2011-09-03 23:45:16 +00:00
|
|
|
#ifdef DEBUG
|
2012-01-27 17:59:44 +00:00
|
|
|
// PDEBUG("localnet: %s; ", inet_ntop(AF_INET,&in_addr_localnet, str, sizeof(str)));
|
|
|
|
// PDEBUG("netmask: %s; " , inet_ntop(AF_INET, &in_addr_netmask, str, sizeof(str)));
|
2011-09-03 23:45:16 +00:00
|
|
|
PDEBUG("target: %s\n", inet_ntop(AF_INET, p_addr_in, str, sizeof(str)));
|
|
|
|
PDEBUG("port: %d\n", port);
|
|
|
|
#endif
|
2012-01-27 17:59:44 +00:00
|
|
|
for(i = 0; i < num_localnet_addr; i++) {
|
|
|
|
if((localnet_addr[i].in_addr.s_addr & localnet_addr[i].netmask.s_addr)
|
|
|
|
== (p_addr_in->s_addr & localnet_addr[i].netmask.s_addr)) {
|
|
|
|
if(localnet_addr[i].port || localnet_addr[i].port == port) {
|
2011-03-22 14:04:52 +00:00
|
|
|
PDEBUG("accessing localnet using true_connect\n");
|
2012-01-27 17:59:44 +00:00
|
|
|
return true_connect(sock, addr, len);
|
2011-03-22 14:04:52 +00:00
|
|
|
}
|
2011-02-25 15:16:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-06 13:12:50 +00:00
|
|
|
flags = fcntl(sock, F_GETFL, 0);
|
2011-02-25 09:40:11 +00:00
|
|
|
if(flags & O_NONBLOCK)
|
2012-01-27 17:59:44 +00:00
|
|
|
fcntl(sock, F_SETFL, !O_NONBLOCK);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-11-06 13:12:50 +00:00
|
|
|
dest_ip.as_int = SOCKADDR(*addr);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
ret = connect_proxy_chain(sock,
|
|
|
|
dest_ip,
|
|
|
|
SOCKPORT(*addr),
|
|
|
|
proxychains_pd, proxychains_proxy_count, proxychains_ct, proxychains_max_chain);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
fcntl(sock, F_SETFL, flags);
|
2011-11-06 13:12:50 +00:00
|
|
|
if(ret != SUCCESS)
|
2012-01-27 17:59:44 +00:00
|
|
|
errno = ECONNREFUSED;
|
2011-02-25 09:40:11 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
struct hostent *gethostbyname(const char *name) {
|
2011-02-25 09:40:11 +00:00
|
|
|
if(!init_l)
|
|
|
|
init_lib();
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
PDEBUG("gethostbyname: %s\n", name);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
if(proxychains_resolver)
|
|
|
|
return proxy_gethostbyname(name);
|
|
|
|
else
|
|
|
|
return true_gethostbyname(name);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-09-03 23:45:16 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res) {
|
2011-02-25 09:40:11 +00:00
|
|
|
int ret = 0;
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
if(!init_l)
|
|
|
|
init_lib();
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
PDEBUG("getaddrinfo: %s %s\n", node, service);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
if(proxychains_resolver)
|
|
|
|
ret = proxy_getaddrinfo(node, service, hints, res);
|
|
|
|
else
|
|
|
|
ret = true_getaddrinfo(node, service, hints, res);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2011-09-03 23:45:16 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
void freeaddrinfo(struct addrinfo *res) {
|
2011-02-25 09:40:11 +00:00
|
|
|
if(!init_l)
|
|
|
|
init_lib();
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
PDEBUG("freeaddrinfo %p \n", res);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
if(!proxychains_resolver)
|
|
|
|
true_freeaddrinfo(res);
|
|
|
|
else {
|
|
|
|
free(res->ai_addr);
|
|
|
|
free(res);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2012-01-27 17:59:44 +00:00
|
|
|
|
2011-09-04 16:13:30 +00:00
|
|
|
// work around a buggy prototype in GLIBC. according to the bugtracker it has been fixed in git at 02 May 2011.
|
|
|
|
// 2.14 came out in June 2011 so that should be the first fixed version
|
|
|
|
#if defined(__GLIBC__) && (__GLIBC__ < 3) && (__GLIBC_MINOR__ < 14)
|
2012-01-27 17:59:44 +00:00
|
|
|
int getnameinfo(const struct sockaddr *sa,
|
|
|
|
socklen_t salen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, unsigned int flags)
|
2011-09-03 23:45:16 +00:00
|
|
|
#else
|
2012-01-27 17:59:44 +00:00
|
|
|
int getnameinfo(const struct sockaddr *sa,
|
|
|
|
socklen_t salen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags)
|
2011-09-03 23:45:16 +00:00
|
|
|
#endif
|
2011-02-25 09:40:11 +00:00
|
|
|
{
|
2012-01-27 19:00:22 +00:00
|
|
|
char ip_buf[16];
|
2011-02-25 09:40:11 +00:00
|
|
|
int ret = 0;
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
if(!init_l)
|
|
|
|
init_lib();
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-09-04 00:03:47 +00:00
|
|
|
PDEBUG("getnameinfo: %s %s\n", host, serv);
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
if(!proxychains_resolver) {
|
2012-01-27 17:59:44 +00:00
|
|
|
ret = true_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
|
2011-02-25 09:40:11 +00:00
|
|
|
} else {
|
2012-01-27 19:00:22 +00:00
|
|
|
if(hostlen) {
|
|
|
|
pc_stringfromipv4((unsigned char*) &(SOCKADDR_2(*sa)), ip_buf);
|
|
|
|
strncpy(host, ip_buf, hostlen);
|
|
|
|
}
|
2012-01-27 16:55:37 +00:00
|
|
|
if(servlen)
|
2012-01-27 17:59:44 +00:00
|
|
|
snprintf(serv, servlen, "%d", ntohs(SOCKPORT(*sa)));
|
2011-02-25 09:40:11 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2011-02-25 15:16:58 +00:00
|
|
|
|
2012-01-27 17:59:44 +00:00
|
|
|
struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type) {
|
2011-09-03 23:45:16 +00:00
|
|
|
static char buf[16];
|
|
|
|
static char ipv4[4];
|
2012-01-27 17:59:44 +00:00
|
|
|
static char *list[2];
|
2011-09-03 23:45:16 +00:00
|
|
|
static struct hostent he;
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
if(!init_l)
|
|
|
|
init_lib();
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-09-04 00:03:47 +00:00
|
|
|
PDEBUG("TODO: proper gethostbyaddr hook\n");
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-02-25 09:40:11 +00:00
|
|
|
if(!proxychains_resolver)
|
2012-01-27 17:59:44 +00:00
|
|
|
return true_gethostbyaddr(addr, len, type);
|
2011-09-03 23:45:16 +00:00
|
|
|
else {
|
2012-01-27 16:55:37 +00:00
|
|
|
|
2011-09-04 00:03:47 +00:00
|
|
|
PDEBUG("len %u\n", len);
|
2012-01-27 17:59:44 +00:00
|
|
|
if(len != 4)
|
|
|
|
return NULL;
|
2011-09-03 23:45:16 +00:00
|
|
|
he.h_name = buf;
|
|
|
|
memcpy(ipv4, addr, 4);
|
|
|
|
list[0] = ipv4;
|
|
|
|
list[1] = NULL;
|
|
|
|
he.h_addr_list = list;
|
|
|
|
he.h_addrtype = AF_INET;
|
|
|
|
he.h_aliases = NULL;
|
|
|
|
he.h_length = 4;
|
2012-01-27 17:59:44 +00:00
|
|
|
pc_stringfromipv4((unsigned char *) addr, buf);
|
2011-09-03 23:45:16 +00:00
|
|
|
return &he;
|
|
|
|
}
|
2011-02-25 09:40:11 +00:00
|
|
|
return NULL;
|
|
|
|
}
|