/*==MAINWIN.CPP=======================================================*/ /* */ /* Module : MAINWIN */ /* */ /* Project : FINGER */ /* */ /* Description: Main window of finger */ /* */ /*--------------------------------------------------------------------*/ /* */ /* (c) Enter AG, Zrich, 1993 */ /* */ /*--------------------------------------------------------------------*/ /* */ /* Author(s) : Hartwig Thomas */ /* */ /* Started : 24. March 1993 */ /* */ /* Finished : 29. March 1993 */ /* */ /*--------------------------------------------------------------------*/ /* */ /* Modifications: */ /* */ /* AUTHOR | DATE | COMMENT */ /* ----------------|--------------|-------------------------------- */ /* | | */ /* | | */ /* */ /*====================================================================*/ // basic OWL-Stuff #ifndef __WINDOWS_H #include "windows.h" #endif #ifndef __OWL_H #include #endif #pragma hdrstop #ifndef __DIR_H #include #endif #ifndef __STRING_H #include #endif #ifndef __ALLOC_H #include #endif // for (resource) constants #ifndef FINGER_DEF #include "finger.h" #endif // the Windows Sockets API #ifndef _WINSOCKAPI_ extern "C" { #include "winsock.h" }; #endif // the dialogs (About, Client, Server) #ifndef DIALOGS_DEF #include "dialogs.h" #endif // class definition #ifndef MAINWIN_DEF #include "mainwin.h" #endif /*====================================================================*/ /* Main window has a procedure for each menu item */ /*====================================================================*/ void TMainWin::InitErrorMsg(int iMsg) { char szTitle[STRSIZESHORT+1]; char szMessage[STRSIZELONG+1]; LoadString(GetApplication()->hInstance,IDS_INITERROR,(LPSTR)szTitle,STRSIZESHORT); LoadString(GetApplication()->hInstance,iMsg,(LPSTR)szMessage,STRSIZELONG); MessageBox(HWindow,(LPSTR)szMessage,(LPSTR)szTitle,MB_ICONEXCLAMATION | MB_OK | MB_APPLMODAL); CloseWindow(); } // InitErrorMsg /*--------------------------------------------------------------------*/ void TMainWin::ClientErrorMsg(int iMsg) { char szTitle[STRSIZESHORT+1]; char szMessage[STRSIZELONG+1]; LoadString(GetApplication()->hInstance,IDS_CLIENTERROR,(LPSTR)szTitle,STRSIZESHORT); LoadString(GetApplication()->hInstance,iMsg,(LPSTR)szMessage,STRSIZELONG); MessageBox(HWindow,(LPSTR)szMessage,(LPSTR)szTitle,MB_ICONEXCLAMATION | MB_OK | MB_APPLMODAL); if (fclient != INVALID_SOCKET) { closesocket(fclient); fclient = INVALID_SOCKET; } } // ClientErrorMsg /*--------------------------------------------------------------------*/ void TMainWin::ServerErrorMsg(int iMsg) { char szTitle[STRSIZESHORT+1]; char szMessage[STRSIZELONG+1]; LoadString(GetApplication()->hInstance,IDS_SERVERERROR,(LPSTR)szTitle,STRSIZESHORT); LoadString(GetApplication()->hInstance,iMsg,(LPSTR)szMessage,STRSIZELONG); MessageBox(HWindow,(LPSTR)szMessage,(LPSTR)szTitle,MB_ICONEXCLAMATION | MB_OK | MB_APPLMODAL); if (fserver != INVALID_SOCKET) { closesocket(fserver); fserver = INVALID_SOCKET; } } // ServerErrorMsg /*--------------------------------------------------------------------*/ void TMainWin::InputErrorMsg(int iMsg) { char szTitle[STRSIZESHORT+1]; char szMessage[STRSIZELONG+1]; LoadString(GetApplication()->hInstance,IDS_INPUTERROR,(LPSTR)szTitle,STRSIZESHORT); LoadString(GetApplication()->hInstance,iMsg,(LPSTR)szMessage,STRSIZELONG); MessageBox(HWindow,(LPSTR)szMessage,(LPSTR)szTitle,MB_ICONEXCLAMATION | MB_OK | MB_APPLMODAL); if (fconnect != INVALID_SOCKET) { closesocket(fconnect); fconnect = INVALID_SOCKET; } } // InputErrorMsg /*--------------------------------------------------------------------*/ TMainWin::TMainWin(LPSTR szTitle) : TWindow(NULL,szTitle) { char szTemp[STRSIZESHORT+1]; // initialize the window class name LoadString(GetApplication()->hInstance,IDS_MAINCLASS,(LPSTR)szClassName,STRSIZESHORT); // assign the menu LoadString(GetApplication()->hInstance,IDS_MAINMENU,(LPSTR)szTemp,STRSIZESHORT); AssignMenu(szTemp); // initially no sockets active fclient = INVALID_SOCKET; fserver = INVALID_SOCKET; fconnect = INVALID_SOCKET; // buffer is empty *szBuffer = '\0'; // no message defined *szFingerMessage = '\0'; } // TMainWin constructor /*--------------------------------------------------------------------*/ TMainWin::~TMainWin() { } // TMainWin destructor /*--------------------------------------------------------------------*/ LPSTR TMainWin::GetClassName() { return (LPSTR)szClassName; } // GetClassName /*--------------------------------------------------------------------*/ void TMainWin::GetWindowClass(WNDCLASS& WndClass) { char szTemp[STRSIZESHORT+1]; TWindow::GetWindowClass(WndClass); LoadString(GetApplication()->hInstance,IDS_MAINICON,(LPSTR)szTemp,STRSIZESHORT); WndClass.hIcon = LoadIcon(GetApplication()->hInstance,(LPSTR)szTemp); } // GetWindowClass /*--------------------------------------------------------------------*/ void TMainWin::SetupWindow() { int rc; WSADATA wskData; struct servent FAR *lpServerEntity; TWindow::SetupWindow(); // WSAStartup rc = WSAStartup(1,(LPWSADATA)&wskData); if (rc || (wskData.wVersion != 1)) { if (wskData.wVersion != 1) InitErrorMsg(IDS_VERSIONERROR); else if (rc) InitErrorMsg(IDS_STARTUPERROR); } // get finger port lpServerEntity = getservbyname("finger","tcp"); if (!lpServerEntity) { // message box CloseWindow(); } fingerport = htons(lpServerEntity->s_port); // put description of WSA to screen strcpy(szBuffer,"Windows Sockets API\nDescription: "); // from string resource later strcat(szBuffer,wskData.szDescription); strcat(szBuffer,"\nSystem Status: "); // ditto strcat(szBuffer,wskData.szSystemStatus); InvalidateRect(HWindow,NULL,TRUE); // menu items EnableMenuItems(); } // SetupWindow /*--------------------------------------------------------------------*/ void TMainWin::ShutDownWindow() { // close open sockets if (fclient != INVALID_SOCKET) closesocket(fclient); if (fserver != INVALID_SOCKET) closesocket(fserver); if (fconnect != INVALID_SOCKET) closesocket(fconnect); // WSACleanup WSACleanup(); TWindow::ShutDownWindow(); } // ShutDownWindow /*--------------------------------------------------------------------*/ void TMainWin::Paint(HDC PaintDC, PAINTSTRUCT _FAR & PaintInfo) { // display buffer SelectObject(PaintDC,GetStockObject(SYSTEM_FIXED_FONT)); DrawText(PaintDC,szBuffer,-1,(LPRECT)&PaintInfo.rcPaint,DT_LEFT | DT_EXPANDTABS); } // Paint /*--------------------------------------------------------------------*/ void TMainWin::EnableMenuItems() { HMENU hMenu; WORD wEnable; hMenu = GetMenu(HWindow); if (fclient == INVALID_SOCKET) wEnable = MF_ENABLED; else wEnable = MF_DISABLED | MF_GRAYED; CheckMenuItem(hMenu,IDM_CLIENT,wEnable); if (fserver == INVALID_SOCKET) wEnable = MF_BYCOMMAND | MF_UNCHECKED; else wEnable = MF_BYCOMMAND | MF_CHECKED; CheckMenuItem(hMenu,IDM_SERVER,wEnable); } // EnableMenuItems /*--------------------------------------------------------------------*/ void TMainWin::CMClient(RTMessage /* Msg */) { int length; char szDialogName[STRSIZESHORT+1]; char szHostName[STRSIZELONG+1]; char szUserName[STRSIZELONG+1]; PTClientDlg pClientDlg; struct hostent FAR *lpHostEntity; struct sockaddr_in anaddr; // prompt for host name *szUserName = '\0'; *szHostName = '\0'; LoadString(GetApplication()->hInstance,IDS_CLIENTNAME,(LPSTR)szDialogName,STRSIZESHORT); pClientDlg = new TClientDlg(this,(LPSTR)szDialogName, (LPSTR)szUserName,(LPSTR)szHostName); if (GetApplication()->ExecDialog(pClientDlg) != IDOK) return; // look up the host address lpHostEntity = gethostbyname(szHostName); if (!lpHostEntity) { ClientErrorMsg(IDS_UNKNOWNHOST); return; } // create socket fclient = socket(PF_INET,SOCK_STREAM,0); if (fclient == INVALID_SOCKET) { ClientErrorMsg(IDS_NOSOCKET); return; } // set up address for connect _fmemcpy((LPSTR)&anaddr.sin_addr.s_addr,lpHostEntity->h_addr,lpHostEntity->h_length); anaddr.sin_family = PF_INET; anaddr.sin_port = htons(fingerport); // connect socket to address if (connect(fclient,(struct sockaddr FAR *)&anaddr,sizeof(struct sockaddr_in))) { ClientErrorMsg(IDS_NOCONNECT); return; } // send the user name as a request to the finger server strcat(szUserName,"\r\n"); if (send(fclient,szUserName,strlen(szUserName),0) < strlen(szUserName)) { ClientErrorMsg(IDS_NOSEND); return; } // wait for answer and analyze it length = recv(fclient,szBuffer,BUFFERSIZE,0); if (length == SOCKET_ERROR) { ClientErrorMsg(IDS_NORECV); return; } if (length == 0) { ClientErrorMsg(IDS_NODATA); return; } // write it to the screen szBuffer[length] = '\0'; InvalidateRect(HWindow,NULL,TRUE); // close the socket closesocket(fclient); fclient = INVALID_SOCKET; } // CMClient /*--------------------------------------------------------------------*/ void TMainWin::CMServer(RTMessage /* Msg */) { char szDialogName[STRSIZESHORT+1]; struct sockaddr_in anaddr; PTServerDlg pServerDlg; // if server installed the stop it if (fserver != INVALID_SOCKET) { closesocket(fserver); fserver = INVALID_SOCKET; } else // otherwise start it { // prompt finger information to be served LoadString(GetApplication()->hInstance,IDS_SERVERNAME,(LPSTR)szDialogName,STRSIZESHORT); pServerDlg = new TServerDlg(this,(LPSTR)szDialogName, (LPSTR)szFingerMessage); if (GetApplication()->ExecDialog(pServerDlg) != IDOK) return; // create socket fserver = socket(PF_INET,SOCK_STREAM,0); if (fserver == INVALID_SOCKET) { ServerErrorMsg(IDS_NOSOCKET); return; } // bind the fserver socket to the port number anaddr.sin_port = htons(fingerport); anaddr.sin_addr.s_addr = INADDR_ANY; anaddr.sin_family = PF_INET; if (bind(fserver,(struct sockaddr FAR *)&anaddr,sizeof(anaddr))) { ServerErrorMsg(IDS_NOBIND); return; } // listen for connections and ask for a message when they come in if (listen(fserver,5)) { ServerErrorMsg(IDS_NOLISTEN); return; } // tell the Windows Sockets API that we wait for a UM_CONNECT message if (WSAAsyncSelect(fserver,HWindow,WM_USER + UM_CONNECT,FD_ACCEPT)) { ServerErrorMsg(IDS_NOWSAASYNC); return; } } // check/uncheck menu item EnableMenuItems(); } // CMServer /*--------------------------------------------------------------------*/ void TMainWin::CMCancel(RTMessage /* Msg */) { WSACancelBlockingCall(); strcpy(szBuffer,"Call cancelled"); // from string resource later InvalidateRect(HWindow,NULL,TRUE); } // CMCancel /*--------------------------------------------------------------------*/ void TMainWin::CMAbout(RTMessage /* Msg */) { char szTemp[STRSIZESHORT+1]; PTAboutDlg pAboutDialog; LoadString(GetApplication()->hInstance,IDS_ABOUTNAME,(LPSTR)szTemp,STRSIZESHORT); pAboutDialog = new TAboutDlg(this,(LPSTR)szTemp); GetApplication()->ExecDialog(pAboutDialog); } // CMAbout /*--------------------------------------------------------------------*/ void TMainWin::UMConnect(RTMessage Msg) { int length; struct sockaddr_in anaddr; // was there a problem? if (WSAGETSELECTERROR(Msg.LParam)) { InputErrorMsg(IDS_CONNLISTEN); return; } // accept the client socket from listening length = sizeof(struct sockaddr_in); fconnect = accept(fserver,(struct sockaddr FAR *)&anaddr,&length); if (fconnect == INVALID_SOCKET) { InputErrorMsg(IDS_NOACCEPT); return; } // now wait for request of client to read info if (WSAAsyncSelect(fconnect,HWindow,WM_USER + UM_DATA,FD_READ)) InputErrorMsg(IDS_NOWSAASYNC); } // UMConnect /*--------------------------------------------------------------------*/ void TMainWin::UMData(RTMessage /* Msg */) { int length; // we ignore the data from client length = recv(fconnect,szBuffer,BUFFERSIZE,0); *szBuffer = '\0'; if (length == SOCKET_ERROR) { InputErrorMsg(IDS_NORECV); return; } // send data to client if (send(fconnect,szFingerMessage,strlen(szFingerMessage),0) < (int) strlen(szFingerMessage)) { InputErrorMsg(IDS_NOSEND); return; } // close the connection closesocket(fconnect); fconnect = INVALID_SOCKET; } // UMData /*-End of MAINWIN.CPP-------------------------------------------------*/