1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
#define WEBSITE "server.x10host.com" // Full website URL, withouth HTTP (raises exe size)
#define WEBPAGE "/Server.php" // The page of the Server Script (proceed with '\')
#define cAddIP 0 // Send this to the server, to add the IP to the list.
#define cRemIP 1 // Send this to the server, to remove the IP from the list.
#define cGetIPs 2 // Send this to the server, to get the list of all IPs.
SOCKET ListeningSocket;
SOCKADDR_IN ServerAddr;
int Port = 7171;
char* MyIP; // Hold's the computers external IP (EG on an NAT)
// -------------------------
char* WebPost(char Website[], char Webpage[], char Request[], int RetLen) {
// Sends an HTTP Post request with POST Data...
// Absolutly NOT error checking, which needs fixing!
SOCKET WebSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent *WebHost;
WebHost = gethostbyname(Website);
if (WebHost == NULL) {
if (WSAGetLastError() == WSANOTINITIALISED)
printf("Error Not Connected!");
else
printf("Error: %d", WSAGetLastError());
Sleep(1000);
exit(0);
}
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)WebHost->h_addr);
connect(WebSocket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr));
char PostRequest[1024];
sprintf(PostRequest,
"POST %s HTTP/1.1\r\n"
"Host: %s\r\n"
"Content-Length: %hu\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"\r\nD=%s\0",
Webpage, Website,
strlen(Request)+2, Request
);
send(WebSocket, PostRequest, strlen(PostRequest), 0);
// Get return data ----------
char* Data = new char[RetLen];
recv(WebSocket, Data, 4, 0);
for (;;) { // Skip HTTP headers...
Data[0] = Data[1];
Data[1] = Data[2];
Data[2] = Data[3];
recv(WebSocket, &Data[3], 1, 0);
if (Data[0] == '\r' && Data[1] == '\n'
&& Data[2] == '\r' && Data[3] == '\n')
break;
}
int DataLen = recv(WebSocket, Data, RetLen, 0);
Data[DataLen] = '\0'; // Return the data...
shutdown(WebSocket, 2);
closesocket(WebSocket);
return Data;
}
void ServStart() {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
printf("Server: WSAStartup failed with error %ld.\n", WSAGetLastError());
exit(0);
}
printf("Server: The Winsock DLL found!\n");
printf("Server: The current status is %s.\n\n", wsaData.szSystemStatus);
if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2 ) {
printf("Server: The dll do not support the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
WSACleanup();
exit(0);
}
printf("Server: The dll supports the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
// Start listening -----------------------------------------------
ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ListeningSocket == INVALID_SOCKET) {
printf("Server: Error at socket, error code: %ld.\n", WSAGetLastError());
WSACleanup();
exit(0);
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(ListeningSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR) {
printf("Server: bind failed! Error code: %ld.\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
exit(0);
}
if (listen(ListeningSocket, 5) == SOCKET_ERROR) {
printf("Server: listen: Error listening on socket %ld.\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
exit(0);
}
//MyIP = WebPost("api.externalip.net", "/ip/", "", 16); // Get my IP
MyIP = inet_ntoa(ServerAddr.sin_addr);
char SendBuf[32];
sprintf(SendBuf, "%hhu|%s", cAddIP, MyIP); // Send the server the IP
WebPost(WEBSITE, WEBPAGE, SendBuf, 0);
printf("Server: listening for connections...\n\n");
}
void ShutDown() { // Shut down the server (tells the web server I am offline)
char SendBuf[32]; // Remove my IP from the list of online servers...
sprintf(SendBuf, "%hhu|%s", cRemIP, MyIP);
WebPost(WEBSITE, WEBPAGE, SendBuf, 0);
printf("Successful shutdown\n");
Sleep(1000);
WSACleanup();
}
//
void ServLoop() {
SOCKADDR_IN SenderInfo;
SOCKET NewConnection;
int ByteReceived, nlen;
char recvbuff[1024];
for (;;) { // Main program loop
NewConnection = SOCKET_ERROR;
while(NewConnection == SOCKET_ERROR) {
NewConnection = accept(ListeningSocket, NULL, NULL); // (this is a blocking function)
printf("Server: New client got connected, ready to receive and send data...\n\n");
ByteReceived = recv(NewConnection, recvbuff, sizeof(recvbuff), 0);
if (ByteReceived > 0) {
getsockname(ListeningSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));
printf("Server: IP(s) used by Server: %s\n", inet_ntoa(ServerAddr.sin_addr));
printf("Server: port used by Server: %d\n\n", htons(ServerAddr.sin_port));
memset(&SenderInfo, 0, sizeof(SenderInfo));
nlen = sizeof(SenderInfo);
getpeername(NewConnection, (SOCKADDR *)&SenderInfo, &nlen);
printf("Server: IP used by Client: %s\n", inet_ntoa(SenderInfo.sin_addr));
printf("Server: Port used by Client: %d\n", htons(SenderInfo.sin_port));
printf("Server: Bytes received: %d\n", ByteReceived);
printf("Server: Those bytes are: \n\"");
for(int i = 0; i < ByteReceived; ++i)
printf("%c", recvbuff[i]);
printf("\"");
} else if ( ByteReceived == 0 )
printf("Server: Connection closed!\n");
else
printf("Server: recv failed with error code: %d\n", WSAGetLastError());
}
if (shutdown(NewConnection, 2) != 0)
printf("\n\nServer: there is something wrong with the shutdown. The error code: %ld\n", WSAGetLastError());
else
printf("\nServer: shutdown is working...\n");
}
}
// --------------------------------------------
BOOL ConsoleProc(DWORD Msg) {
switch (Msg) {
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
ShutDown();
return false;
}
return false;
}
int main(/*int argc, char **argv*/) {
// Handle console events
SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleProc, TRUE);
ServStart(); // Main loop is in another thread
ServLoop(); // The never returning server loop
}
|