WarptenLe 14/12/2012 à 19:21
On avance, plus qu'à trouver pourquoi recv renvoie SOCKET_ERROR
void Proxy::Initialize()
{
if (!_canStart)
return;
// Initialize socket to handle server messages
struct hostent* remoteHost = gethostbyname(_conf.remoteHost);
if (remoteHost == NULL) {
printf("[ERROR] Realm not found.\n");
return;
}
struct in_addr addressList = {0};
if (remoteHost->h_addrtype == AF_INET) {
int i = 0;
while (remoteHost->h_addr_list[i] != 0)
addressList.s_addr = *(u_long*)remoteHost->h_addr_list[i++];
}
// Fill infos for the socket
SOCKADDR_IN socketOut;
socketOut.sin_addr.s_addr = inet_addr(inet_ntoa(addressList));
socketOut.sin_family = AF_INET;
socketOut.sin_port = htons(_conf.remotePort);
remoteSocket = socket(AF_INET, SOCK_STREAM, 0);
printf("[INFO] Connecting to %s:%u (%s)...\n", _conf.remoteHost, _conf.remotePort, inet_ntoa(addressList));
int errorCode = connect(remoteSocket, (SOCKADDR*)&socketOut, sizeof(socketOut));
if (errorCode == SOCKET_ERROR) {
printf("[ERROR] Error #%u when connecting to remote server: %s", errorCode, WSAGetLastError());
return;
} else printf("[INFO] Connected to remote server.\n");
// Initialize socket to handle client messages
SOCKADDR_IN socketIn;
socketIn.sin_addr.s_addr = inet_addr("127.0.0.1");
socketIn.sin_family = AF_INET;
socketIn.sin_port = htons(_conf.localPort);
clientSocket = socket(AF_INET, SOCK_STREAM, 0);
errorCode = bind(clientSocket, (SOCKADDR*)&socketIn, sizeof(socketIn));
if (errorCode == -1) {
printf("[ERROR] Error when binding on port %u.\n", _conf.localPort);
return;
}
// Start listening
errorCode = listen(clientSocket, 1);
if (errorCode == SOCKET_ERROR) {
printf("[ERROR] Unable to listen on port %u.\n", _conf.localPort);
return;
}
printf("[INFO] Proxy listening on 127.0.0.1:%u and proxying to %s:%u.\n", _conf.localPort, _conf.remoteHost, _conf.remotePort);
while (true) { // Main polling loop
char dataBuffer[4096];
FD_SET socketSet;
struct timeval timeoutDelay = { 1, 0 };
FD_ZERO(&socketSet);
FD_SET(clientSocket, &socketSet); // Insert client socket
FD_SET(remoteSocket, &socketSet); // Insert server socket
if (select(0, &socketSet, NULL, NULL, &timeoutDelay) == 0) // No socket active
continue;
if (!FD_ISSET(clientSocket, &socketSet)) // No new connection
continue;
errorCode = accept(clientSocket, NULL, 0);
if (errorCode == SOCKET_ERROR) {
printf("[ERROR] Error #%u when accepting a client.\n", errno);
break;
} else printf("[INFO] Client connected.\n"); //! TODO: Origin?
bool closeNeeded = false;
int byteCount = 0;
if (FD_ISSET(clientSocket, &socketSet)) {
byteCount = recv(clientSocket, dataBuffer, sizeof(dataBuffer), 0);
// Handle packet (CMSG).
if (byteCount <= 0 || send(remoteSocket, dataBuffer, byteCount, 0) == SOCKET_ERROR)
closeNeeded = true;
}
if (FD_ISSET(remoteSocket, &socketSet)) {
byteCount = recv(remoteSocket, dataBuffer, sizeof(dataBuffer), 0);
// Handle packet (SMSG).
if (byteCount <= 0 || send(clientSocket, dataBuffer, byteCount, 0) == SOCKET_ERROR)
closeNeeded = true;
}
if (!closeNeeded)
continue;
printf("[INFO] Closing link with the client.\n");
DestroySockets();
return;
}
DestroySockets();
}