2
* Copyright (c) 2010, Psiphon Inc.
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21
#include "locallistener.h"
22
#include "psiclient.h"
24
LocalListener::LocalListener(const string& IP, unsigned short port) :
25
m_listenIP(IP), m_listenPort(port)
30
LocalListener::~LocalListener(void)
32
// close listening socket
33
closesocket(m_listenSocket);
36
void LocalListener::Initialize(void)
40
m_listenSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
41
m_sin.sin_family = AF_INET;
42
m_sin.sin_port = htons(m_listenPort);
43
if (INADDR_NONE == (m_sin.sin_addr.s_addr = inet_addr(m_listenIP.c_str())))
45
my_print(false, _T("inet_addr error: %s"), _tcserror(errno));
50
setsockopt(m_listenSocket, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt));
53
// TODO: I don't think this is needed
54
unsigned long opt = 1;
55
if (ioctlsocket(m_listenSocket, FIONBIO, &opt) == SOCKET_ERROR)
57
my_print(false, _T("ioctlsocket error: %s"), _tcserror(errno));
62
m_sinlen = sizeof(m_sin);
63
if (-1 == bind(m_listenSocket, (struct sockaddr *)&m_sin, m_sinlen))
65
my_print(false, _T("bind error: %s"), _tcserror(errno));
69
if (-1 == listen(m_listenSocket, 2))
71
my_print(false, _T("listen error: %s"), _tcserror(errno));
75
const char* x = inet_ntoa(m_sin.sin_addr);
77
_T("waiting for TCP connection on %hs:%d..."),
78
inet_ntoa(m_sin.sin_addr), ntohs(m_sin.sin_port));
81
void LocalListener::Accept(int& forwardSocket)
83
forwardSocket = accept(m_listenSocket, (struct sockaddr *)&m_sin, &m_sinlen);
84
if (-1 == forwardSocket)
86
my_print(false, _T("accept error: %d"), WSAGetLastError());
91
int LocalListener::ListenSocket(void)
93
return m_listenSocket;