Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 36 additions & 44 deletions SslSocket/basesock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,13 @@ int BaseSock::Delay(void)

// other functions

int BaseSock::_Connect(string host,int port)
int BaseSock::_Connect(string host,int port,bool &ipv6)
{

struct sockaddr_in adr;
struct sockaddr_in6 adr;
adr = GetIp(host,port);
ipv6 = !IN6_IS_ADDR_V4MAPPED(&adr.sin6_addr);

if(!setnonblocking(sock))
{
return 0;
Expand Down Expand Up @@ -260,9 +262,9 @@ int BaseSock::_Connect(string host,int port)
return 0;
}

int BaseSock::_Connect5(string ip, int port, string socksIp, int socksPort, string socksUser, string socksPass, bool socksSsl, int &status)
int BaseSock::_Connect5(string ip, int port, string socksIp, int socksPort, string socksUser, string socksPass, bool socksSsl, bool &ipv6, int &status)
{
if(!_Connect(socksIp, socksPort))
if(!_Connect(socksIp, socksPort, ipv6))
{
status = 1; // socks connect failed
return 0;
Expand Down Expand Up @@ -340,10 +342,10 @@ int BaseSock::_Connect5(string ip, int port, string socksIp, int socksPort, stri
buffer[2] = 0;
buffer[3] = 1;

struct sockaddr_in adr;
struct sockaddr_in6 adr;
adr = GetIp(ip,port);
//memcpy(&adr.sin_addr.s_addr,buffer+4,4);
memcpy(buffer+4,&adr.sin_addr.s_addr,4);
memcpy(buffer+4,&adr.sin6_addr.s6_addr,4); //doesn't work in v6, need proto update
buffer[8] = (char)(port / 256);
buffer[9] = (char)(port % 256);

Expand Down Expand Up @@ -433,7 +435,7 @@ int BaseSock::CanRead(int timeout)

int BaseSock::GetSock(int &sock)
{
if((sock = socket(AF_INET,SOCK_STREAM,0)) == -1)
if((sock = socket(AF_INET6,SOCK_STREAM,0)) == -1)
{
sock = -1;

Expand Down Expand Up @@ -594,38 +596,26 @@ int BaseSock::setreuse(int &socket)
return SocketOption(socket, SO_REUSEADDR);
}

struct sockaddr_in BaseSock::GetIp(string ip,int port)
struct sockaddr_in6 BaseSock::GetIp(string ip,int port)
{
struct sockaddr_in addr;
addr.sin_family = AF_INET;
unsigned long lip = inet_addr(ip.c_str());
if(lip == INADDR_NONE)
{
struct hostent *he;

if((he = gethostbyname(ip.c_str())) == NULL)
{
lip = inet_addr("0.0.0.0");
addr.sin_addr.s_addr = lip;
}
else
{
addr.sin_addr = *(struct in_addr*)he->h_addr;
}
}
else
{
addr.sin_addr.s_addr = lip;
}
addr.sin_port = htons(port);
memset(&(addr.sin_zero), '\0', 8);
return addr;
struct sockaddr_in6 addr;
struct addrinfo hints, *res;
int n, sockfd;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET6;
hints.ai_flags = AI_V4MAPPED | AI_ADDRCONFIG | AI_PASSIVE;
n = getaddrinfo(ip.c_str(), to_string(port).c_str(), &hints, &res);
memcpy(&addr, res->ai_addr, sizeof(addr));
freeaddrinfo(res);
return addr;
}

string BaseSock::GetIpStr(string ip)
{
sockaddr_in adr = GetIp(ip,0);
string tmpip(inet_ntoa(adr.sin_addr));
sockaddr_in6 adr = GetIp(ip,0);
char buf[64];
inet_ntop(AF_INET6, &(adr.sin6_addr), buf, 64);
string tmpip(buf);
return tmpip;
}

Expand Down Expand Up @@ -943,20 +933,20 @@ void BaseSock::setquit(int quit)

int BaseSock::Bind(string ip,int port)
{
struct sockaddr_in adr;
struct sockaddr_in6 adr;
if (ip != "")
{
adr = GetIp(ip,port);
}
else
{
adr.sin_addr.s_addr = INADDR_ANY;
adr.sin_port = htons(port);
adr.sin_family = AF_INET;
memset(&(adr.sin_zero), '\0', 8);
{
adr.sin6_addr = in6addr_any;
adr.sin6_port = htons(port);
adr.sin6_family = AF_INET6;
adr.sin6_flowinfo = 0;
}
if(!setreuse(sock)) return 0;
if(bind(sock,(struct sockaddr *)&adr, sizeof(struct sockaddr)) != 0)
if(bind(sock,(struct sockaddr *)&adr, sizeof(adr)) != 0)
{
return 0;
}
Expand Down Expand Up @@ -1265,7 +1255,7 @@ int BaseSock::_Accept(int listensock,int &newsock,string &clientip,int &clientpo
{
fd_set readfds;
fd_set errorfds;
struct sockaddr_in adr;
struct sockaddr_in6 adr;

if(sec > 0)
{
Expand Down Expand Up @@ -1327,8 +1317,10 @@ int BaseSock::_Accept(int listensock,int &newsock,string &clientip,int &clientpo
return 0;
}

clientip = inet_ntoa(adr.sin_addr);
clientport = ntohs(adr.sin_port);
char charbuf[INET6_ADDRSTRLEN];
clientip = inet_ntop(adr.sin6_family, &adr.sin6_addr, charbuf, INET6_ADDRSTRLEN);
clientport = ntohs(adr.sin6_port);


if(!setnonblocking(newsock))
{
Expand Down
7 changes: 3 additions & 4 deletions SslSocket/basesock.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ friend class ServerSock;

virtual int Init(void) {return 0;} // make this class abstract
int Bind(string ip, int port);
int _Connect(string ip, int port);
int _Connect5(string ip, int port, string socksIp, int socksPort, string socksUser, string socksPass, bool socksSsl, int &status);
int _Connect(string ip, int port, bool &ipv6);
int _Connect5(string ip, int port, string socksIp, int socksPort, string socksUser, string socksPass, bool socksSsl, bool &ipv6, int &status);

int CanRead(int timeout);
int ReadLine(string &str);
Expand All @@ -57,7 +57,7 @@ friend class ServerSock;
int FastWrite(char *data,int nrbytes);
int BlockWrite(char *data,int nrbytes);
int BlockRead(char *buffer, int &nrbytes);
struct sockaddr_in GetIp(string ip, int port);
struct sockaddr_in6 GetIp(string ip, int port);
string GetIpStr(string ip);

void setquit(int quit);
Expand All @@ -83,7 +83,6 @@ friend class ServerSock;
int _shouldquit;
SSL *ssl;


int SocketOption(int &,int);
int setnonblocking(int);
int setblocking(int);
Expand Down
10 changes: 8 additions & 2 deletions SslSocket/clientsock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ClientSock::ClientSock(void)
{
buffer = NULL;
ssl = NULL;
_ipv6 = false;
_socksIp = "";
_socksPort = 0;
_socksUser = "";
Expand Down Expand Up @@ -290,15 +291,20 @@ void ClientSock::useSocks(bool useSocks)
_useSocks = useSocks;
}

bool ClientSock::ipv6(void)
{
return _ipv6;
}

int ClientSock::Connect(string ip, int port)
{
if(!useSocks())
{
return _Connect(ip, port);
return _Connect(ip, port, _ipv6);
}
else
{
int status;
return _Connect5(ip, port, socksIp(), socksPort(), socksUser(), socksPass(), socksSsl(), status);
return _Connect5(ip, port, socksIp(), socksPort(), socksUser(), socksPass(), socksSsl(), _ipv6, status);
}
}
2 changes: 2 additions & 0 deletions SslSocket/clientsock.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class DLL ClientSock: public BaseSock
void socksSsl(bool socksSsl);
bool useSocks(void);
void useSocks(bool useSocks);
bool ipv6(void);


protected:
Expand All @@ -46,6 +47,7 @@ class DLL ClientSock: public BaseSock
string _socksUser;
bool _socksSsl;
bool _useSocks;
bool _ipv6;
};


Expand Down
1 change: 1 addition & 0 deletions SslSocket/serversock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ int ServerSock::Init()
if(!GetSock(sock)) return 0;
buffer = new char [_buffersize];
if(buffer == NULL) return 0;
if(!setreuse(sock)) return 0;
return 1;
}

Expand Down
14 changes: 14 additions & 0 deletions SslSocket/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,20 @@ bool parsePasvCmd(string cmd, string &ip, int &port)
return true;
}

bool parseEpsvCmd(string cmd, string &ip, int &port) {
int pos1 = cmd.find("(");
int pos2 = cmd.find(")",pos1);
if(pos1 == string::npos || pos2 == string::npos) return false;
string tmp = cmd.substr(pos1 + 1, pos2 - 2);
vector<string> vec;
if(split(vec,tmp,'|',false) == 2) { //rfc response
ip = "";
port = atoi(vec[0].c_str());
return true;
}
return false;
}

bool parsePortCmd(string cmd, string &ip, int &port)
{
size_t pos1 = cmd.find(" ");
Expand Down
1 change: 1 addition & 0 deletions SslSocket/strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ DLL void getpassword(string prompt, string &pass);
DLL int MatchIp(const string& ip1, const string& ip2);
DLL int ftpCode(string reply);
DLL bool parsePasvCmd(string cmd, string &ip, int &port);
DLL bool parseEpsvCmd(string cmd, string &ip, int &port);
DLL bool parsePortCmd(string cmd, string &ip, int &port);
DLL int random_range(int lowest_number, int highest_number);
DLL string int2str(int);
Expand Down
Loading