void addClntThrdCD::run( void * ) { HAB thab = WinInitialize( 0 ); HMQ hmq = WinCreateMsgQueue( thab , 0 ); // Attempt to create a new socket VSocket *tmpsock; tmpsock = new VSocket; if (tmpsock!=NULL) { // Connect out to the specified host on the VNCviewer listen port // To be really good, we should allow a display number here but // for now we'll just assume we're connecting to display zero tmpsock->Create(); if (tmpsock->Connect(m_host, m_port)) { // Add the new client to this server m_server->AddClient(tmpsock, TRUE, TRUE); } else { // Print up an error message WinMessageBox( HWND_DESKTOP , HWND_DESKTOP , (PSZ)"Failed to connect to listening VNC viewer", (PSZ)"Outgoing Connection", 10001 , MB_ICONEXCLAMATION | MB_OK ); delete tmpsock; } } WinDestroyMsgQueue( hmq ); WinTerminate( thab ); }
VBool VSocket::Bind(const VCard port, const VBool localOnly, const VBool checkIfInUse) { struct sockaddr_in addr; // Check that the socket is open! if (sock < 0) return VFalse; // If a specific port is being set then check it's not already used! if (port != 0 && checkIfInUse) { VSocket dummy; if (dummy.Create()) { // If we're able to connect then the port number is in use... if (dummy.Connect("localhost", port)) return VFalse; } } // Set up the address to bind the socket to addr.sin_family = AF_INET; addr.sin_port = htons(port); if (localOnly) addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); else addr.sin_addr.s_addr = htonl(INADDR_ANY); // And do the binding if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) return VFalse; return VTrue; }
vncSockConnect::~vncSockConnect() { m_socket.Shutdown(); // Join with our lovely thread if (m_thread != NULL) { // *** This is a hack to force the listen thread out of the accept call, // because Winsock accept semantics are broken ((vncSockConnectThread *)m_thread)->m_shutdown = TRUE; VSocket socket; socket.Create(); socket.Bind(0); socket.Connect("localhost", m_port); socket.Close(); void *returnval; m_thread->join(&returnval); m_thread = NULL; m_socket.Close(); } }
BOOL CALLBACK vncConnDialog::vncConnDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { // This is a static method, so we don't know which instantiation we're // dealing with. But we can get a pseudo-this from the parameter to // WM_INITDIALOG, which we therafter store with the window and retrieve // as follows: vncConnDialog *_this = helper::SafeGetWindowUserData<vncConnDialog>(hwnd); switch (uMsg) { case WM_PAINT: { //adzm 2009-06-20 if (GetUpdateRect(hwnd, NULL, TRUE)) { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); { RECT rcIcon; rcIcon.top = 0; rcIcon.left = 0; rcIcon.bottom = 48; rcIcon.right = 48; RECT rcClient; if (GetClientRect(hwnd, &rcClient)) { int nDifference = (rcClient.bottom - rcIcon.bottom) / 2; if (nDifference > 0) { rcIcon.top += nDifference; rcIcon.bottom += nDifference; } } RECT rcLabel; HWND hwndLabel = GetDlgItem(hwnd, IDC_CONNECTION_NUMBER_STATIC); if (GetWindowRect(hwndLabel, &rcLabel)) { LPRECT lprcLabel = &rcLabel; ScreenToClient(hwnd, (LPPOINT)lprcLabel); ScreenToClient(hwnd, ((LPPOINT)lprcLabel)+1); int nAdjustment = (rcLabel.left - rcIcon.right) / 2; rcIcon.left += nAdjustment; rcIcon.right += nAdjustment; } RECT rcIntersect; if (IntersectRect(&rcIntersect, &rcIcon, &(ps.rcPaint))) { if (_this->m_hicon == NULL) { _this->m_hicon = (HICON)LoadImage(hInstResDLL, MAKEINTRESOURCE(IDI_WINVNC), IMAGE_ICON, 48, 48, 0); } if (_this->m_hicon) { HBRUSH hbr = (HBRUSH)SendMessage(hwnd, WM_CTLCOLORDLG, (WPARAM)hdc, (WPARAM)hwnd); DrawIconEx(hdc, rcIcon.left, rcIcon.top, _this->m_hicon, 48, 48, 0, hbr, DI_NORMAL); } } } EndPaint(hwnd, &ps); } return 0; } // Dialog has just been created case WM_INITDIALOG: { // Save the lParam into our user data so that subsequent calls have // access to the parent C++ object helper::SafeSetWindowUserData(hwnd, lParam); vncConnDialog *_this = (vncConnDialog *) lParam; //adzm 2009-06-20 if (g_szRepeaterHost) { SetDlgItemText(hwnd, IDC_HOSTNAME_EDIT, g_szRepeaterHost); //adzm 2009-06-20 if (SPECIAL_SC_PROMPT) { HWND hwndChild = GetDlgItem(hwnd, IDC_HOSTNAME_EDIT); if (hwndChild) { ShowWindow(hwndChild, SW_HIDE); } hwndChild = GetDlgItem(hwnd, IDC_HOSTNAME_STATIC); if (hwndChild) { ShowWindow(hwndChild, SW_HIDE); } HWND hwndEdit = GetDlgItem(hwnd, IDC_IDCODE); HWND hwndLabel = GetDlgItem(hwnd, IDC_CONNECTION_NUMBER_STATIC); RECT rcEdit; RECT rcLabel; GetWindowRect(hwndEdit, &rcEdit); GetWindowRect(hwndLabel, &rcLabel); LPRECT lprcEdit = &rcEdit; LPRECT lprcLabel = &rcLabel; ScreenToClient(hwnd, (LPPOINT)lprcEdit); ScreenToClient(hwnd, ((LPPOINT)lprcEdit)+1); ScreenToClient(hwnd, (LPPOINT)lprcLabel); ScreenToClient(hwnd, ((LPPOINT)lprcLabel)+1); RECT rcClient; GetClientRect(hwnd, &rcClient); long nTotalHeight = rcEdit.bottom - rcLabel.top; long nAdjustedTop = (rcClient.bottom - nTotalHeight) / 2; long nAdjustment = nAdjustedTop - rcLabel.top; MoveWindow(hwndLabel, rcLabel.left, rcLabel.top + nAdjustment, rcLabel.right - rcLabel.left, rcLabel.bottom - rcLabel.top, TRUE); MoveWindow(hwndEdit, rcEdit.left, rcEdit.top + nAdjustment, rcEdit.right - rcEdit.left, rcEdit.bottom - rcEdit.top, TRUE); HWND hwndCaption = GetDlgItem(hwnd, IDC_CAPTION_STATIC); HFONT hFont = (HFONT)SendMessage(hwndCaption, WM_GETFONT, 0, 0); if (hFont) { LOGFONT lf; if (GetObject(hFont, sizeof(LOGFONT), &lf)) { lf.lfWidth = 0; lf.lfHeight = (lf.lfHeight * 6) / 4; _this->m_hfont = CreateFontIndirect(&lf); if (_this->m_hfont) { SendMessage(hwndCaption, WM_SETFONT, (WPARAM)_this->m_hfont, (LPARAM)TRUE); } } } SetWindowText(hwndCaption, "Connect to Technical Support"); ShowWindow(hwndCaption, SW_SHOWNA); } SetFocus(GetDlgItem(hwnd, IDC_IDCODE)); } else { // Make the text entry box active SetFocus(GetDlgItem(hwnd, IDC_HOSTNAME_EDIT)); } SetForegroundWindow(hwnd); // Return success! return TRUE; } // Dialog has just received a command case WM_COMMAND: switch (LOWORD(wParam)) { // User clicked OK or pressed return case IDOK: { // sf@2002 - host:num & host::num analyse. // Compatible with both RealVNC and TightVNC methods char hostname[_MAX_PATH]; char actualhostname[_MAX_PATH]; char idcode[_MAX_PATH]; char *portp; int port; bool id; // Get the hostname of the VNCviewer GetDlgItemText(hwnd, IDC_HOSTNAME_EDIT, hostname, _MAX_PATH); GetDlgItemText(hwnd, IDC_IDCODE, idcode, _MAX_PATH); if (strcmp(idcode,"")==0) id=false; else id=true; strcpy(actualhostname, hostname); //adzm 2010-02-15 - Multiple repeaters chosen by modulo of ID char finalidcode[_MAX_PATH]; //adzm 2010-08 - this was sending uninitialized data over the wire ZeroMemory(finalidcode, sizeof(finalidcode)); //adzm 2009-06-20 if (id) { size_t i = 0; for (i = 0; i < strlen(idcode); i++) { finalidcode[i] = toupper(idcode[i]); } finalidcode[i] = 0; if (0 != strncmp("ID:", idcode, 3)) { strcpy(finalidcode, "ID:"); for (i = 0; i < strlen(idcode); i++) { finalidcode[i+3] = toupper(idcode[i]); } finalidcode[i+3] = 0; } //adzm 2010-02-15 - At this point, finalidcode is of the form "ID:#####" int numericId = atoi(finalidcode + 3); int numberOfHosts = 1; for (i = 0; i < strlen(hostname); i++) { if (hostname[i] == ';') { numberOfHosts++; } } if (numberOfHosts <= 1) { // then hostname == actualhostname } else { int modulo = numericId % numberOfHosts; char* szToken = strtok(hostname, ";"); while (szToken) { if (modulo == 0) { strcpy(actualhostname, szToken); break; } modulo--; szToken = strtok(NULL, ";"); } } } // Calculate the Display and Port offset. port = INCOMING_PORT_OFFSET; portp = strchr(actualhostname, ':'); if (portp) { *portp++ = '\0'; if (*portp == ':') // Tight127 method { port = atoi(++portp); // Port number after "::" } else // RealVNC method { if (atoi(portp) < 100) // If < 100 after ":" -> display number port += atoi(portp); else port = atoi(portp); // If > 100 after ":" -> Port number } } // Attempt to create a new socket VSocket *tmpsock; tmpsock = new VSocket; if (!tmpsock) { return TRUE; } // Connect out to the specified host on the VNCviewer listen port // To be really good, we should allow a display number here but // for now we'll just assume we're connecting to display zero tmpsock->Create(); if (tmpsock->Connect(actualhostname, port)) { if (id) { tmpsock->Send(finalidcode,250); tmpsock->SetTimeout(0); /* if (strncmp(hostname,"ID",2)!=0) { while (true) { char result[1]; tmpsock->Read(result,1); if (strcmp(result,"2")==0) break; tmpsock->Send("1",1); } }*/ // adzm 2009-07-05 - repeater IDs // Add the new client to this server // adzm 2009-08-02 _this->m_server->AddClient(tmpsock, TRUE, TRUE, 0, NULL, finalidcode, actualhostname, port); } else { // Add the new client to this server // adzm 2009-08-02 _this->m_server->AddClient(tmpsock, TRUE, TRUE, 0, NULL, NULL, actualhostname, port); } // And close the dialog EndDialog(hwnd, TRUE); } else { // Print up an error message MessageBoxSecure(NULL, sz_ID_FAILED_CONNECT_LISTING_VIEW, sz_ID_OUTGOING_CONNECTION, MB_OK | MB_ICONEXCLAMATION ); delete tmpsock; } return TRUE; } // Cancel the dialog case IDCANCEL: EndDialog(hwnd, FALSE); return TRUE; }; break; case WM_DESTROY: EndDialog(hwnd, FALSE); if (_this->m_hicon != NULL) { DestroyIcon(_this->m_hicon); _this->m_hicon = NULL; } if (_this->m_hfont != NULL) { DeleteObject(_this->m_hfont); _this->m_hfont = NULL; } return TRUE; } return 0; }
// Process window messages LRESULT CALLBACK vncMenu::WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { // This is a static method, so we don't know which instantiation we're // dealing with. We use Allen Hadden's ([email protected]) suggestion // from a newsgroup to get the pseudo-this. vncMenu *_this = (vncMenu *) GetWindowLong(hwnd, GWL_USERDATA); // Beep(100,10); // vnclog.Print(LL_INTINFO, VNCLOG("iMsg 0x%x \n"),iMsg); switch (iMsg) { // Every five seconds, a timer message causes the icon to update case WM_TIMER: // *** HACK for running servicified if (vncService::RunningAsService()) { // Attempt to add the icon if it's not already there _this->AddTrayIcon(); // Trigger a check of the current user PostMessage(hwnd, WM_USERCHANGED, 0, 0); } if (nport!=0) { PostMessage(hwnd, RETRY_MESSAGE, 0, 0); } // Update the icon _this->FlashTrayIcon(_this->m_server->AuthClientCount() != 0); break; // DEAL WITH NOTIFICATIONS FROM THE SERVER: case WM_SRV_CLIENT_AUTHENTICATED: case WM_SRV_CLIENT_DISCONNECT: // Adjust the icon accordingly _this->FlashTrayIcon(_this->m_server->AuthClientCount() != 0); if (_this->m_server->AuthClientCount() != 0) { if (_this->m_server->RemoveWallpaperEnabled()) KillWallpaper(); } else { if (_this->m_server->RemoveWallpaperEnabled()) RestoreWallpaper(); } return 0; // STANDARD MESSAGE HANDLING case WM_CREATE: return 0; case WM_COMMAND: // User has clicked an item on the tray menu switch (LOWORD(wParam)) { case ID_DEFAULT_PROPERTIES: /* // Show the default properties dialog, unless it is already displayed vnclog.Print(LL_INTINFO, VNCLOG("show default properties requested\n")); _this->m_properties.ShowAdmin(TRUE, FALSE); _this->FlashTrayIcon(_this->m_server->AuthClientCount() != 0);*/ break; case ID_PROPERTIES: // Show the properties dialog, unless it is already displayed vnclog.Print(LL_INTINFO, VNCLOG("show user properties requested\n")); _this->m_propertiesPoll.Show(TRUE, TRUE); _this->FlashTrayIcon(_this->m_server->AuthClientCount() != 0); break; case ID_ABOUT: // Show the About box _this->m_about.Show(TRUE); break; case ID_CLOSE: // User selected Close from the tray menu _this->m_server->KillAuthClients(); PostMessage(hwnd, WM_CLOSE, 0, 0); break; } return 0; case WM_TRAYNOTIFY: // User has clicked on the tray icon or the menu { // Get the submenu to use as a pop-up menu HMENU submenu = GetSubMenu(_this->m_hmenu, 0); // What event are we responding to, RMB click? if (lParam==WM_RBUTTONUP) { if (submenu == NULL) { vnclog.Print(LL_INTERR, VNCLOG("no submenu available\n")); return 0; } // Make the first menu item the default (bold font) SetMenuDefaultItem(submenu, 0, TRUE); // Get the current cursor position, to display the menu at POINT mouse; GetCursorPos(&mouse); // There's a "bug" // (Microsoft calls it a feature) in Windows 95 that requires calling // SetForegroundWindow. To find out more, search for Q135788 in MSDN. // SetForegroundWindow(_this->m_nid.hWnd); // Display the menu at the desired position TrackPopupMenu(submenu, 0, mouse.x, mouse.y, 0, _this->m_nid.hWnd, NULL); return 0; } // Or was there a LMB double click? if (lParam==WM_LBUTTONDBLCLK) { // double click: execute first menu item SendMessage(_this->m_nid.hWnd, WM_COMMAND, GetMenuItemID(submenu, 0), 0); } return 0; } case WM_CLOSE: // Only accept WM_CLOSE if the logged on user has AllowShutdown set if (!_this->m_properties.AllowShutdown()) { return 0; } // tnatsni Wallpaper fix RestoreWallpaper(); break; case WM_DESTROY: // The user wants WinVNC to quit cleanly... vnclog.Print(LL_INTINFO, VNCLOG("quitting from WM_DESTROY\n")); PostQuitMessage(0); return 0; case WM_QUERYENDSESSION: vnclog.Print(LL_INTERR, VNCLOG("WM_QUERYENDSESSION\n")); break; case WM_ENDSESSION: vnclog.Print(LL_INTERR, VNCLOG("WM_ENDSESSION\n")); break; case WM_USERCHANGED: // The current user may have changed. { char newuser[UNLEN+1]; if (vncService::CurrentUser((char *) &newuser, sizeof(newuser))) { vnclog.Print(LL_INTINFO, // VNCLOG("usernames : old=\"%s\", new=\"%s\"\n"), _this->m_username, newuser); // Check whether the user name has changed! if (strcmp(newuser, _this->m_username) != 0) { vnclog.Print(LL_INTINFO, VNCLOG("user name has changed\n")); // User has changed! strcpy(_this->m_username, newuser); // Redraw the tray icon and set it's state _this->DelTrayIcon(); _this->AddTrayIcon(); _this->FlashTrayIcon(_this->m_server->AuthClientCount() != 0); } } } return 0; default: if (iMsg == MENU_DEFAULT_PROPERTIES_SHOW) { // External request to show our Properties dialog PostMessage(hwnd, WM_COMMAND, MAKELONG(ID_DEFAULT_PROPERTIES, 0), 0); return 0; } if (iMsg == MENU_ABOUTBOX_SHOW) { // External request to show our About dialog PostMessage(hwnd, WM_COMMAND, MAKELONG(ID_ABOUT, 0), 0); return 0; } if (iMsg == MENU_SERVICEHELPER_MSG) { // External ServiceHelper message. // This message holds a process id which we can use to // impersonate a specific user. In doing so, we can load their // preferences correctly // vncService::ProcessUserHelperMessage(wParam, lParam); // Modif Jeremy C. vncService::ProcessUserHelperMessage((WPARAM)&_this->m_server->m_impersonationtoken, lParam); // - Trigger a check of the current user PostMessage(hwnd, WM_USERCHANGED, 0, 0); return 0; } if (iMsg == RETRY_MESSAGE) { connect_counter++; if (connect_counter%5==0) { // Attempt to create a new socket // Beep(1000,100); VSocket *tmpsock; tmpsock = new VSocket; if (tmpsock) { // Connect out to the specified host on the VNCviewer listen port tmpsock->Create(); if (tmpsock->Connect(szAdrName, nport)) { // Add the new client to this server if (strcmp(g_idcode,"")==NULL) g_id=false; else g_id=true; char sendID[_MAX_PATH]; strcpy(sendID,"ID:"); strcat(sendID,g_idcode); if (g_id) tmpsock->Send(sendID,250); _this->m_server->AddClient(tmpsock, TRUE, TRUE); nport=0; connected=1; _this->FlashTrayIcon(_this->m_server->AuthClientCount() != 0); return 0; } else { delete tmpsock; connect_counter++; if (connect_counter>25) // Sleep(5000); PostMessage(hwnd, WM_CLOSE, 0, 0); } } } return 0; } if (iMsg == MENU_ADD_CLIENT_MSG) { // sf@2003 - Autoreconnect // Dirty trick to avoid to add a new MSG... no time if (lParam == 999) { _this->m_server->AutoReconnect(true); return 0; } /* // sf@2005 - FTNoUserImpersonation // Dirty trick to avoid to add a new MSG... no time if (lParam == 998) { _this->m_server->FTUserImpersonation(false); return 0; } */ // Add Client message. This message includes an IP address // of a listening client, to which we should connect. char *nameDup = 0; // sf@2003 - Values are already converted if (_this->m_server->AutoReconnect() && strlen(_this->m_server->AutoReconnectAdr()) > 0) { nport = _this->m_server->AutoReconnectPort(); strcpy(szAdrName, _this->m_server->AutoReconnectAdr()); } else { // Get the IP address stringified struct in_addr address; address.S_un.S_addr = lParam; char *name = inet_ntoa(address); if (name == 0) return 0; nameDup = strdup(name); if (nameDup == 0) return 0; strcpy(szAdrName, nameDup); // Free the duplicate name if (nameDup != 0) free(nameDup); // Get the port number nport = (unsigned short)wParam; if (nport == 0) nport = INCOMING_PORT_OFFSET; } // sf@2003 // Stores the client adr/ports the first time we try to connect // This way we can call this message again later to reconnect with the same values if (_this->m_server->AutoReconnect() && strlen(_this->m_server->AutoReconnectAdr()) == 0) { _this->m_server->AutoReconnectAdr(szAdrName); _this->m_server->AutoReconnectPort(nport); } // Attempt to create a new socket _this->FlashTrayIcon(_this->m_server->AuthClientCount() != 0); VSocket *tmpsock; tmpsock = new VSocket; if (tmpsock) { // Connect out to the specified host on the VNCviewer listen port tmpsock->Create(); if (tmpsock->Connect(szAdrName, nport)) { // Add the new client to this server if (strcmp(g_idcode,"")==NULL) g_id=false; else g_id=true; char sendID[_MAX_PATH]; strcpy(sendID,"ID:"); strcat(sendID,g_idcode); if (g_id) tmpsock->Send(sendID,250); _this->m_server->AddClient(tmpsock, TRUE, TRUE); nport=0; connected=1; _this->FlashTrayIcon(_this->m_server->AuthClientCount() != 0); return 0; } else { delete tmpsock; // Sleep(5000); // PostMessage(hwnd, WM_CLOSE, 0, 0); } } return 0; } // Process FileTransfer asynchronous Send Packet Message if (iMsg == FileTransferSendPacketMessage) { vncClient* pClient = (vncClient*) wParam; if (_this->m_server->IsClient(pClient)) pClient->SendFileChunk(); } } // Message not recognised return DefWindowProc(hwnd, iMsg, wParam, lParam); }
BOOL CALLBACK vncConnDialog::vncConnDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { // This is a static method, so we don't know which instantiation we're // dealing with. But we can get a pseudo-this from the parameter to // WM_INITDIALOG, which we therafter store with the window and retrieve // as follows: vncConnDialog *_this = (vncConnDialog *) GetWindowLong(hwnd, GWL_USERDATA); switch (uMsg) { // Dialog has just been created case WM_INITDIALOG: { // Save the lParam into our user data so that subsequent calls have // access to the parent C++ object SetWindowLong(hwnd, GWL_USERDATA, lParam); vncConnDialog *_this = (vncConnDialog *) lParam; // Make the text entry box active SetFocus(GetDlgItem(hwnd, IDC_HOSTNAME_EDIT)); // Return success! return TRUE; } // Dialog has just received a command case WM_COMMAND: switch (LOWORD(wParam)) { // User clicked OK or pressed return case IDOK: { char viewer[256]; char hostname[256]; VCard display_or_port; // Get the viewer to connect to GetDlgItemText(hwnd, IDC_HOSTNAME_EDIT, viewer, 256); // Process the supplied viewer address int result = sscanf(viewer, "%255[^:]:%u", hostname, &display_or_port); if (result == 1) { display_or_port = 0; result = 2; } if (result == 2) { // Correct a display number to a port number if required if (display_or_port < 100) { display_or_port += INCOMING_PORT_OFFSET; } // Attempt to create a new socket VSocket *tmpsock; tmpsock = new VSocket; if (!tmpsock) return TRUE; // Connect out to the specified host on the VNCviewer listen port // To be really good, we should allow a display number here but // for now we'll just assume we're connecting to display zero tmpsock->Create(); if (tmpsock->Connect(hostname, display_or_port)) { // Add the new client to this server _this->m_server->AddClient(tmpsock, TRUE, TRUE); // And close the dialog EndDialog(hwnd, TRUE); } else { // Print up an error message MessageBox(NULL, "Failed to connect to listening VNC viewer", "Outgoing Connection", MB_OK | MB_ICONEXCLAMATION ); delete tmpsock; } } else { // We couldn't process the machine specification MessageBox(NULL, "Unable to process specified hostname and display/port", "Outgoing Connection", MB_OK | MB_ICONEXCLAMATION); } } return TRUE; // Cancel the dialog case IDCANCEL: EndDialog(hwnd, FALSE); return TRUE; }; break; case WM_DESTROY: EndDialog(hwnd, FALSE); return TRUE; } return 0; }
// Socket connection handling BOOL vncServer::SockConnect(BOOL On) { // Are we being asked to switch socket connects on or off? if (On) { // Is there a listening socket? if (m_socketConn == NULL) { m_socketConn = new vncSockConnect(); if (m_socketConn == NULL) return FALSE; // Are we to use automatic port selection? if (m_autoportselect) { BOOL ok = FALSE; // Yes, so cycle through the ports, looking for a free one! for (int i=0; i < 99; i++) { m_port = DISPLAY_TO_PORT(i); vnclog.Print(LL_CLIENTS, VNCLOG("trying port number %d\n"), m_port); // Attempt to connect to the port VSocket tempsock; if (tempsock.Create()) { if (!tempsock.Connect("localhost", m_port)) { // Couldn't connect, so this port is probably usable! if (m_socketConn->Init(this, m_port)) { ok = TRUE; break; } } } } if (!ok) { delete m_socketConn; m_socketConn = NULL; return FALSE; } } else { // No autoportselect if (!m_socketConn->Init(this, m_port)) { delete m_socketConn; m_socketConn = NULL; return FALSE; } } } } else { // *** JNW - Trying to fix up a lock-up when the listening socket closes KillAuthClients(); KillUnauthClients(); WaitUntilAuthEmpty(); WaitUntilUnauthEmpty(); // Is there a listening socket? if (m_socketConn != NULL) { // Close the socket delete m_socketConn; m_socketConn = NULL; } } return TRUE; }
// Process window messages LRESULT CALLBACK vncMenu::WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam) { // This is a static method, so we don't know which instantiation we're // dealing with. We use Allen Hadden's ([email protected]) suggestion // from a newsgroup to get the pseudo-this. vncMenu *_this = helper::SafeGetWindowUserData<vncMenu>(hwnd); // Beep(100,10); // vnclog.Print(LL_INTINFO, VNCLOG("iMsg 0x%x \n"),iMsg); if (iMsg==WM_TASKBARCREATED) { if (_this->m_server->RunningFromExternalService()) { Sleep(1000); vnclog.Print(LL_INTINFO, VNCLOG("WM_TASKBARCREATED \n")); // User has changed! strcpy(_this->m_username, newuser); vnclog.Print(LL_INTINFO, VNCLOG("############## Kill vncMenu thread\n")); // Order impersonation thread killing KillTimer(hwnd,1); PostQuitMessage(0); } } switch (iMsg) { // Every five seconds, a timer message causes the icon to update case WM_TIMER: // sf@2007 - Can't get the WTS_CONSOLE_CONNECT message work properly for now.. // So use a hack instead // jdp reread some ini settings _this->m_properties.ReloadDynamicSettings(); // G_1111==true --> reconnect if (G_1111==true) { if (_this->IsIconSet==true) { vnclog.Print(LL_INTERR, VNCLOG("Add client reconnect from timer\n")); G_1111=false; PostMessage(hwnd,MENU_ADD_CLIENT_MSG,1111,1111); } } vnclog.Print(LL_INTERR, VNCLOG("########### vncMenu::TIMER TrayIcon 5s hack\n")); if (_this->m_server->RunningFromExternalService()) { strcpy(newuser,""); if (vncService::CurrentUser((char *) &newuser, sizeof(newuser))) { // Check whether the user name has changed! if (_stricmp(newuser, _this->m_username) != 0 || _this->IconFaultCounter>2) { Sleep(1000); vnclog.Print(LL_INTINFO, VNCLOG("user name has changed\n")); // User has changed! strcpy(_this->m_username, newuser); vnclog.Print(LL_INTINFO, VNCLOG("############## Kill vncMenu thread\n")); // Order impersonation thread killing PostQuitMessage(0); break; } } } // *** HACK for running servicified if (vncService::RunningAsService()) { vnclog.Print(LL_INTERR, VNCLOG("########### vncMenu::TIMER TrayIcon 5s hack call - Runningasservice\n")); // Attempt to add the icon if it's not already there _this->AddTrayIcon(); // Trigger a check of the current user PostMessage(hwnd, WM_USERCHANGED, 0, 0); } // Update the icon _this->FlashTrayIcon(_this->m_server->AuthClientCount() != 0); break; // DEAL WITH NOTIFICATIONS FROM THE SERVER: case WM_SRV_CLIENT_AUTHENTICATED: case WM_SRV_CLIENT_DISCONNECT: // Adjust the icon accordingly _this->FlashTrayIcon(_this->m_server->AuthClientCount() != 0); if (_this->m_server->AuthClientCount() != 0) { if (_this->m_server->RemoveWallpaperEnabled()) KillWallpaper(); if (_this->m_server->RemoveAeroEnabled()) // Moved, redundant if //PGM @ Advantig DisableAero(); // Moved, redundant if //PGM @ Advantig } else { if (_this->m_server->RemoveAeroEnabled()) // Moved, redundant if //PGM @ Advantig ResetAero(); // Moved, redundant if //PGM @ Advantig if (_this->m_server->RemoveWallpaperEnabled()) { // Added { //PGM @ Advantig Sleep(2000); // Added 2 second delay to help wallpaper restore //PGM @ Advantig RestoreWallpaper(); } //PGM @ Advantig } //PGM @ Advantig if (_this->m_server->AuthClientCount() != 0) { //PGM @ Advantig if (_this->m_server->RemoveAeroEnabled()) //PGM @ Advantig DisableAero(); //PGM @ Advantig } else { //PGM @ Advantig if (_this->m_server->RemoveAeroEnabled()) //PGM @ Advantig ResetAero(); //PGM @ Advantig } return 0; // STANDARD MESSAGE HANDLING case WM_CREATE: WM_TASKBARCREATED = RegisterWindowMessage("TaskbarCreated"); return 0; case WM_COMMAND: // User has clicked an item on the tray menu switch (LOWORD(wParam)) { case ID_DEFAULT_PROPERTIES: // Show the default properties dialog, unless it is already displayed vnclog.Print(LL_INTINFO, VNCLOG("show default properties requested\n")); _this->m_properties.ShowAdmin(TRUE, FALSE); _this->FlashTrayIcon(_this->m_server->AuthClientCount() != 0); break; case ID_PROPERTIES: // Show the properties dialog, unless it is already displayed vnclog.Print(LL_INTINFO, VNCLOG("show user properties requested\n")); _this->m_propertiesPoll.Show(TRUE, TRUE); _this->FlashTrayIcon(_this->m_server->AuthClientCount() != 0); break; case ID_ADMIN_PROPERTIES: // Show the properties dialog, unless it is already displayed vnclog.Print(LL_INTINFO, VNCLOG("show user properties requested\n")); _this->m_properties.ShowAdmin(TRUE, TRUE); _this->FlashTrayIcon(_this->m_server->AuthClientCount() != 0); break; case ID_OUTGOING_CONN: // Connect out to a listening VNC viewer { vncConnDialog *newconn = new vncConnDialog(_this->m_server); if (newconn) { newconn->DoDialog(); // delete newconn; // NO ! Already done in vncConnDialog. } } break; case ID_KILLCLIENTS: // Disconnect all currently connected clients vnclog.Print(LL_INTINFO, VNCLOG("KillAuthClients() ID_KILLCLIENTS \n")); _this->m_server->KillAuthClients(); break; // sf@2002 case ID_LISTCLIENTS: _this->m_ListDlg.Display(); break; case ID_ABOUT: // Show the About box _this->m_about.Show(TRUE); break; case ID_VISITUSONLINE_HOMEPAGE: { HANDLE hProcess,hPToken; DWORD id=GetExplorerLogonPid(); if (id!=0) { hProcess = OpenProcess(MAXIMUM_ALLOWED,FALSE,id); if(!OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY |TOKEN_DUPLICATE|TOKEN_ASSIGN_PRIMARY|TOKEN_ADJUST_SESSIONID |TOKEN_READ|TOKEN_WRITE,&hPToken)) break; char dir[MAX_PATH]; char exe_file_name[MAX_PATH]; GetModuleFileName(0, exe_file_name, MAX_PATH); strcpy(dir, exe_file_name); strcat(dir, " -openhomepage"); { STARTUPINFO StartUPInfo; PROCESS_INFORMATION ProcessInfo; ZeroMemory(&StartUPInfo,sizeof(STARTUPINFO)); ZeroMemory(&ProcessInfo,sizeof(PROCESS_INFORMATION)); StartUPInfo.wShowWindow = SW_SHOW; StartUPInfo.lpDesktop = "Winsta0\\Default"; StartUPInfo.cb = sizeof(STARTUPINFO); CreateProcessAsUser(hPToken,NULL,dir,NULL,NULL,FALSE,DETACHED_PROCESS,NULL,NULL,&StartUPInfo,&ProcessInfo); DWORD error=GetLastError(); if (ProcessInfo.hThread) CloseHandle(ProcessInfo.hThread); if (ProcessInfo.hProcess) CloseHandle(ProcessInfo.hProcess); //if (error==1314) // { // Open_homepage(); // } } } } break; case ID_VISITUSONLINE_FORUM: { HANDLE hProcess,hPToken; DWORD id=GetExplorerLogonPid(); if (id!=0) { hProcess = OpenProcess(MAXIMUM_ALLOWED,FALSE,id); if(!OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY |TOKEN_DUPLICATE|TOKEN_ASSIGN_PRIMARY|TOKEN_ADJUST_SESSIONID |TOKEN_READ|TOKEN_WRITE,&hPToken)) break; char dir[MAX_PATH]; char exe_file_name[MAX_PATH]; GetModuleFileName(0, exe_file_name, MAX_PATH); strcpy(dir, exe_file_name); strcat(dir, " -openforum"); { STARTUPINFO StartUPInfo; PROCESS_INFORMATION ProcessInfo; ZeroMemory(&StartUPInfo,sizeof(STARTUPINFO)); ZeroMemory(&ProcessInfo,sizeof(PROCESS_INFORMATION)); StartUPInfo.wShowWindow = SW_SHOW; StartUPInfo.lpDesktop = "Winsta0\\Default"; StartUPInfo.cb = sizeof(STARTUPINFO); CreateProcessAsUser(hPToken,NULL,dir,NULL,NULL,FALSE,DETACHED_PROCESS,NULL,NULL,&StartUPInfo,&ProcessInfo); DWORD error=GetLastError(); if (ProcessInfo.hThread) CloseHandle(ProcessInfo.hThread); if (ProcessInfo.hProcess) CloseHandle(ProcessInfo.hProcess); //if (error==1314) // { // Open_forum(); // } } } } break; case ID_CLOSE: // User selected Close from the tray menu fShutdownOrdered=TRUE; Sleep(1000); vnclog.Print(LL_INTINFO, VNCLOG("KillAuthClients() ID_CLOSE \n")); _this->m_server->KillAuthClients(); PostMessage(hwnd, WM_CLOSE, 0, 0); break; case ID_UNINSTALL_SERVICE: { HANDLE hProcess,hPToken; DWORD id=GetExplorerLogonPid(); if (id!=0) { hProcess = OpenProcess(MAXIMUM_ALLOWED,FALSE,id); if(!OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY |TOKEN_DUPLICATE|TOKEN_ASSIGN_PRIMARY|TOKEN_ADJUST_SESSIONID |TOKEN_READ|TOKEN_WRITE,&hPToken)) break; char dir[MAX_PATH]; char exe_file_name[MAX_PATH]; GetModuleFileName(0, exe_file_name, MAX_PATH); strcpy(dir, exe_file_name); strcat(dir, " -uninstallhelper"); { STARTUPINFO StartUPInfo; PROCESS_INFORMATION ProcessInfo; HANDLE Token=NULL; HANDLE process=NULL; ZeroMemory(&StartUPInfo,sizeof(STARTUPINFO)); ZeroMemory(&ProcessInfo,sizeof(PROCESS_INFORMATION)); StartUPInfo.wShowWindow = SW_SHOW; StartUPInfo.lpDesktop = "Winsta0\\Default"; StartUPInfo.cb = sizeof(STARTUPINFO); CreateProcessAsUser(hPToken,NULL,dir,NULL,NULL,FALSE,DETACHED_PROCESS,NULL,NULL,&StartUPInfo,&ProcessInfo); DWORD errorcode=GetLastError(); if (process) CloseHandle(process); if (Token) CloseHandle(Token); if (errorcode==1314) { Set_uninstall_service_as_admin(); } } fShutdownOrdered=TRUE; vnclog.Print(LL_INTINFO, VNCLOG("KillAuthClients() ID_CLOSE \n")); _this->m_server->KillAuthClients(); PostMessage(hwnd, WM_CLOSE, 0, 0); } } break; case ID_RUNASSERVICE: { HANDLE hProcess,hPToken; DWORD id=GetExplorerLogonPid(); if (id!=0) { hProcess = OpenProcess(MAXIMUM_ALLOWED,FALSE,id); if(!OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY |TOKEN_DUPLICATE|TOKEN_ASSIGN_PRIMARY|TOKEN_ADJUST_SESSIONID |TOKEN_READ|TOKEN_WRITE,&hPToken)) break; char dir[MAX_PATH]; char exe_file_name[MAX_PATH]; GetModuleFileName(0, exe_file_name, MAX_PATH); strcpy(dir, exe_file_name); strcat(dir, " -installhelper"); STARTUPINFO StartUPInfo; PROCESS_INFORMATION ProcessInfo; HANDLE Token=NULL; HANDLE process=NULL; ZeroMemory(&StartUPInfo,sizeof(STARTUPINFO)); ZeroMemory(&ProcessInfo,sizeof(PROCESS_INFORMATION)); StartUPInfo.wShowWindow = SW_SHOW; StartUPInfo.lpDesktop = "Winsta0\\Default"; StartUPInfo.cb = sizeof(STARTUPINFO); CreateProcessAsUser(hPToken,NULL,dir,NULL,NULL,FALSE,DETACHED_PROCESS,NULL,NULL,&StartUPInfo,&ProcessInfo); DWORD error=GetLastError(); if (process) CloseHandle(process); if (Token) CloseHandle(Token); if (error==1314) { Set_install_service_as_admin(); } } fShutdownOrdered=TRUE; Sleep(1000); vnclog.Print(LL_INTINFO, VNCLOG("KillAuthClients() ID_CLOSE \n")); _this->m_server->KillAuthClients(); PostMessage(hwnd, WM_CLOSE, 0, 0); } break; case ID_CLOSE_SERVICE: { HANDLE hProcess,hPToken; DWORD id=GetExplorerLogonPid(); if (id!=0) { hProcess = OpenProcess(MAXIMUM_ALLOWED,FALSE,id); if(!OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY |TOKEN_DUPLICATE|TOKEN_ASSIGN_PRIMARY|TOKEN_ADJUST_SESSIONID |TOKEN_READ|TOKEN_WRITE,&hPToken)) { CloseHandle(hProcess); break; } char dir[MAX_PATH]; char exe_file_name[MAX_PATH]; GetModuleFileName(0, exe_file_name, MAX_PATH); strcpy(dir, exe_file_name); strcat(dir, " -stopservicehelper"); STARTUPINFO StartUPInfo; PROCESS_INFORMATION ProcessInfo; HANDLE Token=NULL; HANDLE process=NULL; ZeroMemory(&StartUPInfo,sizeof(STARTUPINFO)); ZeroMemory(&ProcessInfo,sizeof(PROCESS_INFORMATION)); StartUPInfo.wShowWindow = SW_SHOW; StartUPInfo.lpDesktop = "Winsta0\\Default"; StartUPInfo.cb = sizeof(STARTUPINFO); CreateProcessAsUser(hPToken,NULL,dir,NULL,NULL,FALSE,DETACHED_PROCESS,NULL,NULL,&StartUPInfo,&ProcessInfo); DWORD error=GetLastError(); if (process) CloseHandle(process); if (Token) CloseHandle(Token); if (hProcess) CloseHandle(hProcess); if (error==1314) { Set_stop_service_as_admin(); } } } break; case ID_START_SERVICE: { HANDLE hProcess,hPToken; DWORD id=GetExplorerLogonPid(); if (id!=0) { hProcess = OpenProcess(MAXIMUM_ALLOWED,FALSE,id); if(!OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY |TOKEN_DUPLICATE|TOKEN_ASSIGN_PRIMARY|TOKEN_ADJUST_SESSIONID |TOKEN_READ|TOKEN_WRITE,&hPToken)) { CloseHandle(hProcess); break; } char dir[MAX_PATH]; char exe_file_name[MAX_PATH]; GetModuleFileName(0, exe_file_name, MAX_PATH); strcpy(dir, exe_file_name); strcat(dir, " -startservicehelper"); STARTUPINFO StartUPInfo; PROCESS_INFORMATION ProcessInfo; HANDLE Token=NULL; HANDLE process=NULL; ZeroMemory(&StartUPInfo,sizeof(STARTUPINFO)); ZeroMemory(&ProcessInfo,sizeof(PROCESS_INFORMATION)); StartUPInfo.wShowWindow = SW_SHOW; StartUPInfo.lpDesktop = "Winsta0\\Default"; StartUPInfo.cb = sizeof(STARTUPINFO); CreateProcessAsUser(hPToken,NULL,dir,NULL,NULL,FALSE,DETACHED_PROCESS,NULL,NULL,&StartUPInfo,&ProcessInfo); DWORD error=GetLastError(); if (hPToken) CloseHandle(hPToken); if (process) CloseHandle(process); if (Token) CloseHandle(Token); if (hProcess) CloseHandle(hProcess); if (error==1314) { Set_start_service_as_admin(); } fShutdownOrdered=TRUE; Sleep(1000); vnclog.Print(LL_INTINFO, VNCLOG("KillAuthClients() ID_CLOSE \n")); _this->m_server->KillAuthClients(); PostMessage(hwnd, WM_CLOSE, 0, 0); } } break; } return 0; case WM_TRAYNOTIFY: // User has clicked on the tray icon or the menu { // Get the submenu to use as a pop-up menu HMENU submenu = GetSubMenu(_this->m_hmenu, 0); // What event are we responding to, RMB click? if (lParam==WM_RBUTTONUP) { if (submenu == NULL) { vnclog.Print(LL_INTERR, VNCLOG("no submenu available\n")); return 0; } // Make the first menu item the default (bold font) SetMenuDefaultItem(submenu, 0, TRUE); // Get the current cursor position, to display the menu at POINT mouse; GetCursorPos(&mouse); // There's a "bug" // (Microsoft calls it a feature) in Windows 95 that requires calling // SetForegroundWindow. To find out more, search for Q135788 in MSDN. // SetForegroundWindow(_this->m_nid.hWnd); // Display the menu at the desired position TrackPopupMenu(submenu, 0, mouse.x, mouse.y, 0, _this->m_nid.hWnd, NULL); PostMessage(hwnd, WM_NULL, 0, 0); return 0; } // Or was there a LMB double click? if (lParam==WM_LBUTTONDBLCLK) { // double click: execute first menu item SendMessage(_this->m_nid.hWnd, WM_COMMAND, GetMenuItemID(submenu, 0), 0); } return 0; } case WM_CLOSE: // Only accept WM_CLOSE if the logged on user has AllowShutdown set if (!_this->m_properties.AllowShutdown()) { return 0; } // tnatsni Wallpaper fix if (_this->m_server->RemoveWallpaperEnabled()) RestoreWallpaper(); if (_this->m_server->RemoveAeroEnabled()) ResetAero(); vnclog.Print(LL_INTERR, VNCLOG("vncMenu WM_CLOSE call - All cleanup done\n")); Sleep(2000); DestroyWindow(hwnd); break; case WM_DESTROY: // The user wants WinVNC to quit cleanly... vnclog.Print(LL_INTINFO, VNCLOG("quitting from WM_DESTROY\n")); PostQuitMessage(0); return 0; case WM_QUERYENDSESSION: { //shutdown or reboot if((lParam & ENDSESSION_LOGOFF) != ENDSESSION_LOGOFF) { fShutdownOrdered=TRUE; Sleep(1000); vnclog.Print(LL_INTERR, VNCLOG("SHUTDOWN OS detected\n")); vnclog.Print(LL_INTINFO, VNCLOG("KillAuthClients() ID_CLOSE \n")); _this->m_server->KillAuthClients(); PostMessage(hwnd, WM_CLOSE, 0, 0); break; } DWORD SessionID; SessionID=GetCurrentSessionID(); vnclog.Print(LL_INTERR, VNCLOG("Session ID %i\n"),SessionID); if (SessionID!=0) { fShutdownOrdered=TRUE; Sleep(1000); vnclog.Print(LL_INTERR, VNCLOG("WM_QUERYENDSESSION session!=0\n")); vnclog.Print(LL_INTINFO, VNCLOG("KillAuthClients() ID_CLOSE \n")); _this->m_server->KillAuthClients(); PostMessage(hwnd, WM_CLOSE, 0, 0); } } break; case WM_ENDSESSION: vnclog.Print(LL_INTERR, VNCLOG("WM_ENDSESSION\n")); break; case WM_USERCHANGED: // The current user may have changed. { strcpy(newuser,""); if (vncService::CurrentUser((char *) &newuser, sizeof(newuser))) { vnclog.Print(LL_INTINFO, VNCLOG("############### Usernames change: old=\"%s\", new=\"%s\"\n"), _this->m_username, newuser); // Check whether the user name has changed! if (_stricmp(newuser, _this->m_username) != 0) { vnclog.Print(LL_INTINFO, VNCLOG("user name has changed\n")); // User has changed! strcpy(_this->m_username, newuser); // Redraw the tray icon and set it's state _this->DelTrayIcon(); _this->AddTrayIcon(); _this->FlashTrayIcon(_this->m_server->AuthClientCount() != 0); // We should load in the prefs for the new user if (_this->m_properties.m_fUseRegistry) { _this->m_properties.Load(TRUE); _this->m_propertiesPoll.Load(TRUE); } else { _this->m_properties.LoadFromIniFile(); _this->m_propertiesPoll.LoadFromIniFile(); } } } } return 0; // [v1.0.2-jp1 fix] Don't show IME toolbar on right click menu. case WM_INITMENU: case WM_INITMENUPOPUP: SendMessage(ImmGetDefaultIMEWnd(hwnd), WM_IME_CONTROL, IMC_CLOSESTATUSWINDOW, 0); return 0; default: // Deal with any of our custom message types // wa@2005 -- added support for the AutoReconnectId // removed the previous code that used 999,999 if ( iMsg == MENU_AUTO_RECONNECT_MSG ) { char szId[MAX_PATH] = {0}; UINT ret = 0; if ( lParam != NULL ) { ret = GlobalGetAtomName( (ATOM)lParam, szId, sizeof( szId ) ); GlobalDeleteAtom( (ATOM)lParam ); } _this->m_server->AutoReconnect(true); if ( ret > 0 ) _this->m_server->AutoReconnectId(szId); return 0; } if ( iMsg == MENU_REPEATER_ID_MSG ) { char szId[MAX_PATH] = {0}; UINT ret = 0; if ( lParam != NULL ) { ret = GlobalGetAtomName( (ATOM)lParam, szId, sizeof( szId ) ); GlobalDeleteAtom( (ATOM)lParam ); } _this->m_server->IdReconnect(true); if ( ret > 0 ) _this->m_server->AutoReconnectId(szId); return 0; } if (iMsg == MENU_ADD_CLIENT_MSG) { /* // sf@2005 - FTNoUserImpersonation // Dirty trick to avoid to add a new MSG... no time if (lParam == 998) { _this->m_server->FTUserImpersonation(false); return 0; } */ // Add Client message. This message includes an IP address // of a listening client, to which we should connect. //adzm 2009-06-20 - Check for special add repeater client message if (wParam == 0xFFFFFFFF && lParam == 0xFFFFFFFF) { vncConnDialog *newconn = new vncConnDialog(_this->m_server); if (newconn) { if (IDOK != newconn->DoDialog()) { if (SPECIAL_SC_PROMPT && _this->m_server->AuthClientCount() == 0 && _this->m_server->UnauthClientCount() == 0) { PostMessage(hwnd, WM_COMMAND, ID_CLOSE, 0); } } } return 0; } // If there is no IP address then show the connection dialog if (!lParam) { vncConnDialog *newconn = new vncConnDialog(_this->m_server); if (newconn) { newconn->DoDialog(); // winvnc -connect fixed //CHECH memeory leak // delete newconn; } return 0; } unsigned short nport = 0; char *nameDup = 0; char szAdrName[64]; char szId[MAX_PATH] = {0}; // sf@2003 - Values are already converted if ((_this->m_server->AutoReconnect()|| _this->m_server->IdReconnect() )&& strlen(_this->m_server->AutoReconnectAdr()) > 0) { nport = _this->m_server->AutoReconnectPort(); strcpy(szAdrName, _this->m_server->AutoReconnectAdr()); } else { // Get the IP address stringified struct in_addr address; address.S_un.S_addr = lParam; char *name = inet_ntoa(address); if (name == 0) return 0; nameDup = _strdup(name); if (nameDup == 0) return 0; strcpy(szAdrName, nameDup); // Free the duplicate name if (nameDup != 0) free(nameDup); // Get the port number nport = (unsigned short)wParam; if (nport == 0) nport = INCOMING_PORT_OFFSET; } // wa@2005 -- added support for the AutoReconnectId // (but it's not required) bool bId = ( strlen(_this->m_server->AutoReconnectId() ) > 0); if ( bId ) strcpy( szId, _this->m_server->AutoReconnectId() ); // sf@2003 // Stores the client adr/ports the first time we try to connect // This way we can call this message again later to reconnect with the same values if ((_this->m_server->AutoReconnect() || _this->m_server->IdReconnect())&& strlen(_this->m_server->AutoReconnectAdr()) == 0) { _this->m_server->AutoReconnectAdr(szAdrName); _this->m_server->AutoReconnectPort(nport); } // Attempt to create a new socket VSocket *tmpsock; tmpsock = new VSocket; if (tmpsock) { // Connect out to the specified host on the VNCviewer listen port tmpsock->Create(); if (tmpsock->Connect(szAdrName, nport)) { if ( bId ) { // wa@2005 -- added support for the AutoReconnectId // Set the ID for this client -- code taken from vncconndialog.cpp (ln:142) tmpsock->Send(szId,250); tmpsock->SetTimeout(0); // adzm 2009-07-05 - repeater IDs // Add the new client to this server // adzm 2009-08-02 _this->m_server->AddClient(tmpsock, TRUE, TRUE, 0, NULL, szId, szAdrName, nport); } else { // Add the new client to this server // adzm 2009-08-02 _this->m_server->AddClient(tmpsock, TRUE, TRUE, 0, NULL, NULL, szAdrName, nport); } } else { delete tmpsock; _this->m_server->AutoConnectRetry(); } } return 0; } // Process FileTransfer asynchronous Send Packet Message if (iMsg == FileTransferSendPacketMessage) { vncClient* pClient = (vncClient*) wParam; if (_this->m_server->IsClient(pClient)) pClient->SendFileChunk(); } // adzm 2009-07-05 - Tray icon balloon tips if (iMsg == MENU_TRAYICON_BALLOON_MSG) { omni_mutex_lock sync(_this->m_mutexTrayIcon); // adzm 2009-07-05 - Tray icon balloon tips if (_this->m_BalloonInfo) { free(_this->m_BalloonInfo); _this->m_BalloonInfo = NULL; } if (_this->m_BalloonTitle) { free(_this->m_BalloonTitle); _this->m_BalloonTitle = NULL; } char* szInfo = (char*)wParam; char* szTitle = (char*)lParam; if (szInfo && (strlen(szInfo) > 0) ) { _this->m_BalloonInfo = _strdup(szInfo); } if (szTitle && (strlen(szTitle) > 0) ) { _this->m_BalloonTitle = _strdup(szTitle); } if (szInfo) { free(szInfo); } if (szTitle) { free(szTitle); } if (_this->IsIconSet) { _this->SendTrayMsg(NIM_MODIFY, _this->m_nid.hIcon == _this->m_winvnc_icon ? FALSE : TRUE); } } } // Message not recognised return DefWindowProc(hwnd, iMsg, wParam, lParam); }
extern "C" DLLEXPORT /*export without name mangling*/ int WinVNCDll_ListenForClient(PSTR szRemoteClient, PSTR szIdCode) { // Compatible with both RealVNC and TightVNC methods char hostname[_MAX_PATH]; char actualhostname[_MAX_PATH]; char idcode[_MAX_PATH]; char *portp; int port; bool id; // Get the hostname of the VNCviewer strcpy(hostname, szRemoteClient); strcpy(idcode, szIdCode); //GetDlgItemText(hwnd, IDC_HOSTNAME_EDIT, hostname, _MAX_PATH); //GetDlgItemText(hwnd, IDC_IDCODE, idcode, _MAX_PATH); if (strcmp(idcode,"")==0) id=false; else id=true; strcpy(actualhostname, hostname); vnclog.Print(LL_STATE, VNCLOG("WinVNCDll_ListenForClient(%s %s)\n"),hostname,idcode); //adzm 2010-02-15 - Multiple repeaters chosen by modulo of ID char finalidcode[_MAX_PATH]; //adzm 2010-08 - this was sending uninitialized data over the wire ZeroMemory(finalidcode, sizeof(finalidcode)); //adzm 2009-06-20 if (id) { size_t i = 0; for (i = 0; i < strlen(idcode); i++) { finalidcode[i] = toupper(idcode[i]); } finalidcode[i] = 0; if (0 != strncmp("ID:", idcode, 3)) { strcpy(finalidcode, "ID:"); for (i = 0; i < strlen(idcode); i++) { finalidcode[i+3] = toupper(idcode[i]); } finalidcode[i+3] = 0; } //adzm 2010-02-15 - At this point, finalidcode is of the form "ID:#####" int numericId = atoi(finalidcode + 3); int numberOfHosts = 1; for (i = 0; i < strlen(hostname); i++) { if (hostname[i] == ';') { numberOfHosts++; } } if (numberOfHosts <= 1) { // then hostname == actualhostname } else { int modulo = numericId % numberOfHosts; char* szToken = strtok(hostname, ";"); while (szToken) { if (modulo == 0) { strcpy(actualhostname, szToken); break; } modulo--; szToken = strtok(NULL, ";"); } } } // Calculate the Display and Port offset. port = INCOMING_PORT_OFFSET; portp = strchr(actualhostname, ':'); if (portp) { *portp++ = '\0'; if (*portp == ':') // Tight127 method { port = atoi(++portp); // Port number after "::" } else // RealVNC method { if (atoi(portp) < 100) // If < 100 after ":" -> display number port += atoi(portp); else port = atoi(portp); // If > 100 after ":" -> Port number } } // Attempt to create a new socket VSocket *tmpsock; tmpsock = new VSocket; if (!tmpsock) { vnclog.Print(LL_STATE, VNCLOG("Could not create socket\n")); return -1; } // Connect out to the specified host on the VNCviewer listen port // To be really good, we should allow a display number here but // for now we'll just assume we're connecting to display zero tmpsock->Create(); vnclog.Print(LL_STATE, VNCLOG("Connecting to: %s %d\n"),actualhostname,port); if (tmpsock->Connect(actualhostname, port)) { vnclog.Print(LL_STATE, VNCLOG("Adding client: %s %d\n"),actualhostname,port); if (id) { tmpsock->Send(finalidcode,250); tmpsock->SetTimeout(0); // adzm 2009-07-05 - repeater IDs // Add the new client to this server // adzm 2009-08-02 return m_server->AddClient(tmpsock, TRUE, TRUE, 0, NULL, finalidcode, actualhostname, port); } else { // Add the new client to this server // adzm 2009-08-02 return m_server->AddClient(tmpsock, TRUE, TRUE, 0, NULL, NULL, actualhostname, port); } } }
// Socket connection handling BOOL vncServer::SockConnect(BOOL On) { // Are we being asked to switch socket connects on or off? if (On) { // Is there a listening socket? if (m_socketConn == NULL) { m_socketConn = new vncSockConnect(); if (m_socketConn == NULL) return FALSE; // Are we to use automatic port selection? if (m_autoportselect) { BOOL ok = FALSE; // Yes, so cycle through the ports, looking for a free one! for (int i = 0; i < 99; i++) { m_port = DISPLAY_TO_PORT(i); m_port_http = DISPLAY_TO_HPORT(i); vnclog.Print(LL_CLIENTS, VNCLOG("trying port number %d\n"), m_port); // Attempt to connect to the port VSocket tempsock; if (tempsock.Create()) { if (!tempsock.Connect("localhost", m_port)) { // Couldn't connect, so this port is probably usable! if (m_socketConn->Init(this, m_port)) { ok = TRUE; break; } } } } if (!ok) { delete m_socketConn; m_socketConn = NULL; return FALSE; } } else { // No autoportselect if (!m_socketConn->Init(this, m_port)) { delete m_socketConn; m_socketConn = NULL; return FALSE; } } // Now let's start the HTTP connection stuff if (m_port_http == m_port) { vnclog.Print(LL_INTERR, VNCLOG("cannot start both RFB and HTTP servers " "on the same port\n")); } if (m_httpConn == NULL && m_httpd_enabled && m_port_http != m_port) { m_httpConn = new vncHTTPConnect; if (m_httpConn != NULL) { // Start up the HTTP server if (!m_httpConn->Init(this, m_port_http, m_httpd_params_enabled)) { delete m_httpConn; m_httpConn = NULL; return FALSE; } } } } } else { // *** JNW - Trying to fix up a lock-up when the listening socket closes #ifndef HORIZONLIVE KillAuthClients(); KillUnauthClients(); WaitUntilAuthEmpty(); WaitUntilUnauthEmpty(); #endif // Is there a listening socket? if (m_socketConn != NULL) { // Close the socket delete m_socketConn; m_socketConn = NULL; } // Is there an HTTP socket active? if (m_httpConn != NULL) { // Close the socket delete m_httpConn; m_httpConn = NULL; } } return TRUE; }