#include #include #include #include #include #include #define TCP_SOCK SOCK_STREAM /* How to build with Visual C++ 5.0: * * cl -nologo -o w.exe /W3 /Ox /TC /G5 /Ot /MT waiter.c /link user32.lib kernel32.lib gdi32.lib ws2_32.lib * */ int port = 7; #define ClassName "Waiter_Class" static char AppName[64] = "Half-Life fake authorization server"; static char Welcome[128]; static int Clients=0; static int Total =0; HINSTANCE g_hInst = NULL; HWND g_hWnd = NULL; void SafeSend(int Socket, void *Buf, int Bytes) { char *a = (char *)Buf; for(;;) { int b = send(Socket, a, Bytes, 0); if(b <= 0)return; a += b; Bytes -= b; } } static void SetTitle(void) { char Title[128]; sprintf(Title, "%s - %d clients - %d total", AppName, Clients, Total); SetWindowTextA(g_hWnd, Title); } static unsigned __stdcall DataThread(void *tmp) { int Sock = *(int *)tmp; Clients++; Total++; SetTitle(); for(;;) { unsigned char c; int i = recv(Sock, &c, 1, 0); if(i==1) SafeSend(Sock, &c, 1); else if(i<0) { // Gprintf("Error receiving: %d\n", WSAGetLastError()); break; } else { // Gprintf("Lost one\n"); break; } } Clients--; SetTitle(); return 0; } long FAR PASCAL WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_KEYDOWN: switch (wParam) { case VK_ESCAPE: PostMessage(g_hWnd, WM_CLOSE, 0, 0); break; } break; #if 0 case WM_SYSCOMMAND: switch (wParam) { // Trap ALT so it doesn't pause the app case SC_KEYMENU: return 0; break; } break; #endif case WM_CLOSE: DestroyWindow(g_hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProc(hWnd, message, wParam, lParam); } BOOL InitClass(HINSTANCE hInst) { WNDCLASS wndClass; // Fill out WNDCLASS info wndClass.style = CS_HREDRAW | CS_VREDRAW; wndClass.lpfnWndProc = WndProc; wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hInstance = hInst; wndClass.hIcon = NULL; wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); wndClass.hbrBackground = GetStockObject(BLACK_BRUSH); wndClass.lpszMenuName = NULL; wndClass.lpszClassName = ClassName; return RegisterClass(&wndClass); } BOOL InitWindow(HINSTANCE hInst, LPSTR lpCmdLine, int nCmdShow) { RECT r,cr; GetWindowRect(GetDesktopWindow(), &r); g_hWnd = CreateWindowA( ClassName, AppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, (r.right-r.left)/3,64, NULL, NULL, hInst, NULL); if(!g_hWnd)return FALSE; GetClientRect(g_hWnd, &cr); SetWindowPos(g_hWnd, HWND_TOP, r.right - (cr.right-cr.left+8), 0, cr.right - cr.left, 0, SWP_NOMOVE | SWP_NOZORDER); ShowWindow(g_hWnd, nCmdShow); UpdateWindow(g_hWnd); return TRUE; } static WSADATA wsaData; static unsigned __stdcall Main(void *tmp) { #if 0 int a, p=0; #endif unsigned temp; unsigned long handle; struct sockaddr_in They; int Plug, Sock, AddrLen; struct sockaddr_in MyAddr; // Gprintf("Waiting for connection in port %d...\n", port); TextOutA(g_hWnd, 4,4, "Made by Bisqwit", 4+1+2+1+7); if((Plug = socket(AF_INET, TCP_SOCK, 0)) < 0) { // Error("socket"); return 0; } MyAddr.sin_port = htons((unsigned short)port);/* network order */ MyAddr.sin_family = AF_INET; /* host byte order */ MyAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* auto-fill with my IP, network long */ memset((void *)&(MyAddr.sin_zero), 0, 8); /* zero the rest of the struct */ if(bind(Plug, (struct sockaddr *)&MyAddr, sizeof(struct sockaddr))) { // Error("bind"); return 0; } Redo: if(listen(Plug, 10) == -1) { // Error("listen"); close(Plug); return 0; } AddrLen = sizeof(struct sockaddr_in); Sock = accept(Plug, (struct sockaddr *)&They, &AddrLen); if(Sock < 0) { Vihre: // Gprintf("Connection failed.\n"); // Gprintf("Windows error code: %d\n", WSAGetLastError()); // Gprintf(" errno: %d (%s)\n", errno, sys_errlist[errno]); return 0; } // Gprintf("Hahaa, got another one :-)\n"); SafeSend(Sock, &Welcome, strlen(Welcome)); handle = _beginthreadex(NULL, 0, DataThread, &Sock, 0, &temp); if(!handle) { // Gprintf("Can't make new thread.\n"); goto Vihre; } goto Redo; } int FAR PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow) { unsigned temp; unsigned long handle; MSG msg; if(!InitClass(hInst) || !InitWindow(hInst, lpCmdLine, nCmdShow)) return 1; if(WSAStartup(0x202,&wsaData) == SOCKET_ERROR) { // Error("WSAStartup failed with error %d\n", WSAGetLastError()); WSACleanup(); return -1; } SetTitle(); sprintf(Welcome, "%s\r\n", AppName); #if 0 /* FIXME to use lpCmdLine */ for(a=1; a