home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / EXTRAS / WINWHOIS / WINWHOIS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-26  |  25.1 KB  |  791 lines

  1. #define WINDOWS
  2. #include <windows.h>
  3. #include <winsock.h>
  4. #include <sys\types.h>
  5. #include <sys\stat.h>
  6. #include <time.h>
  7. #include <stdio.h>
  8. #include <memory.h>
  9. #include "winwhois.h"
  10. #define NICHOST "rs.internic.net"
  11.  
  12. HANDLE hInst;
  13. HANDLE saveinst;
  14.  
  15. char szQueryhost[128] = NICHOST;
  16. char szHost [256];
  17. char szServHost[128];
  18. REQUEST FAR *HeadReq = NULL;
  19. HANDLE hHeadReq = NULL;
  20. HWND hDialog;
  21.  //
  22. int APIENTRY WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  23.     LPSTR lpCmdLine, int nCmdShow)
  24.  
  25. {
  26.     MSG msg;
  27.     HWND hMainWindow;
  28.  
  29.     saveinst = hInstance;
  30.  
  31.     if (!hPrevInstance)
  32.     if (!InitApplication (hInstance))
  33.         return FALSE;
  34.  
  35.     if (!InitInstance (hInstance, nCmdShow, &hMainWindow))
  36.     return FALSE;
  37.  
  38.     LoadString (hInstance, IDC_WHOISLISTSERVER,
  39.                 (LPSTR)szServHost, 128);
  40.  
  41.     SendMessage (hMainWindow, WM_COMMAND, IDC_STARTDIALOG, 0);
  42.  
  43.     while (GetMessage (&msg, 0, 0, 0))
  44.     {
  45.     TranslateMessage (&msg);
  46.     DispatchMessage (&msg);
  47.     }
  48.     return (msg.wParam);
  49. }
  50.  
  51. BOOL InitApplication (HANDLE hInstCurrent)
  52. {
  53.     WNDCLASS wc;
  54.     WNDCLASS windc;
  55.  
  56.     memset(&windc, 0x00, sizeof(WNDCLASS));
  57.     memset(&wc, 0x00, sizeof(WNDCLASS));
  58.  
  59.     windc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  60.     windc.lpfnWndProc = QueryWndProc;
  61.  
  62.     windc.cbClsExtra = 0;
  63.     windc.cbWndExtra = 0;
  64.     windc.hInstance = hInstCurrent;
  65.     windc.hIcon = LoadIcon (saveinst, "QMARK");
  66.     windc.hCursor = LoadCursor (NULL, IDC_ARROW);
  67.     windc.hbrBackground = GetStockObject (WHITE_BRUSH);
  68.  
  69.     windc.lpszMenuName = NULL;
  70.     windc.lpszClassName = "PurposelessWindow";
  71.     if (!RegisterClass (&windc))
  72.     {
  73.         MessageBox (NULL, "Cannot start program",
  74.                     "Whois", MB_OK|MB_ICONHAND);
  75.         return 0;
  76.     }
  77.  
  78.     wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  79.     wc.lpfnWndProc = MainWndProc;
  80.  
  81.     wc.cbClsExtra = 0;
  82.     wc.cbWndExtra = 0;
  83.     wc.hInstance = hInstCurrent;
  84.     wc.hIcon = LoadIcon (saveinst, "QMARK");
  85.     wc.hCursor = LoadCursor (NULL, IDC_ARROW);
  86.     wc.hbrBackground = GetStockObject (WHITE_BRUSH);
  87.    // Create brush for erasing background
  88.  
  89.     wc.lpszMenuName = NULL;
  90.     wc.lpszClassName = "UselessWindow";
  91.  
  92.     return (RegisterClass (&wc));
  93. }
  94.  
  95. BOOL InitInstance (HANDLE hInstCurrent, int nCmdShow,
  96.            LPHANDLE lphMainWindow )
  97. {
  98.     HWND hWnd;
  99.  
  100.     hInst = hInstCurrent;
  101.     hWnd = CreateWindow ("UselessWindow",
  102.              "whois for Windows",
  103.              WS_OVERLAPPEDWINDOW,
  104.              CW_USEDEFAULT,
  105.              CW_USEDEFAULT,
  106.              CW_USEDEFAULT,
  107.              CW_USEDEFAULT,
  108.              NULL,
  109.              NULL,
  110.              hInstCurrent,
  111.              NULL);
  112.     if (hWnd == NULL)
  113.     return FALSE;
  114.  
  115.     *lphMainWindow = hWnd;
  116.  
  117.     return TRUE;
  118.  
  119. }
  120.  
  121.  
  122.  
  123.  
  124.  
  125. LONG FAR APIENTRY MainWndProc (HWND hWnd, UINT message, WPARAM wParam,
  126.                 LPARAM lParam)
  127.  
  128. {
  129.     FARPROC lpfnProcDialog;
  130.     WORD suword; WSADATA WSAData;
  131.    
  132.     switch (message) {
  133.     case WM_COMMAND:
  134.         if (LOWORD(wParam) != IDC_STARTDIALOG)
  135.         return (DefWindowProc (hWnd, message, wParam, lParam));
  136.         else
  137.         {
  138.             suword = MAKEWORD (1,1);
  139.             WSAStartup (suword, &WSAData);
  140.             lpfnProcDialog = MakeProcInstance ((FARPROC) MainDialog,
  141.                              hInst);
  142.             DialogBox (hInst,
  143.                    "WinWhoIs",
  144.                    hWnd,
  145.                    lpfnProcDialog);
  146.             FreeProcInstance (lpfnProcDialog);
  147.             PostMessage (hWnd, WM_DESTROY, 0, 0);
  148.             break;
  149.         }
  150.     case WM_DESTROY:
  151.         WSACleanup ();
  152.         PostQuitMessage (0);
  153.         break;
  154.  
  155.     default:
  156.         return (DefWindowProc (hWnd, message, wParam, lParam));
  157.      }
  158.     return 0;
  159.  
  160. }
  161.  
  162. BOOL FAR PASCAL ChangeHost (HWND hDlg, WORD message, WPARAM wParam, LPARAM lParam)
  163.  
  164. {
  165.     char szHost [128];
  166.     switch (message) {
  167.     case WM_INITDIALOG:
  168.         strcpy (szHost, szQueryhost);
  169.         SendDlgItemMessage (hDlg, IDC_HOSTINPUT, (UINT) WM_SETTEXT,
  170.                    (WPARAM) 0,
  171.                    (LPARAM) ((LPSTR) szHost));
  172.         SendDlgItemMessage (hDlg, IDC_HOSTINPUT, (UINT) EM_SETSEL,            
  173.                    (WPARAM) 0,
  174.                    MAKELPARAM (0, -1));
  175.         SendMessage (GetDlgItem (hDlg, IDC_HOSTINPUT), EM_LIMITTEXT, 128, 0L);
  176.         return TRUE;
  177.  
  178.     case WM_COMMAND:
  179.         switch (LOWORD(wParam))
  180.         {
  181.         case IDC_HOSTINPUT:
  182.             if (HIWORD (lParam) == EN_CHANGE) {
  183.             if (SendDlgItemMessage (hDlg, IDC_HOSTINPUT,
  184.                         EM_LINELENGTH, 0, 0L))
  185.                 EnableWindow (GetDlgItem (hDlg, IDC_CHANGEHOST), TRUE);
  186.             else
  187.                 EnableWindow (GetDlgItem (hDlg, IDC_CHANGEHOST), FALSE);
  188.             return TRUE;
  189.             }
  190.             else return FALSE;
  191.         case IDC_CHANGEHOST:
  192.                 SendDlgItemMessage (hDlg, IDC_HOSTINPUT, (UINT) WM_GETTEXT,
  193.                         (WPARAM) 128,
  194.                         (LPARAM) ((LPSTR) szHost));
  195.                 SendDlgItemMessage (hDlg, IDC_HOSTINPUT, (UINT) EM_SETSEL,
  196.                         0, MAKELPARAM (0,-1));
  197.                 if (*szHost)
  198.                 {
  199.                     strcpy (szQueryhost, szHost);
  200.                 }
  201.                 else {
  202.                 MessageBox (NULL, "No hostname specified!",
  203.                         "WinWhoIs", MB_OK | MB_ICONHAND);
  204.                 }
  205.                 EndDialog (hDlg, TRUE);
  206.                 return TRUE;
  207.         case IDCANCEL:
  208.             EndDialog (hDlg, TRUE);
  209.         }
  210.     }
  211.     return FALSE;
  212. }
  213.  
  214. BOOL FAR PASCAL About (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  215.  
  216. {
  217.     switch (message) {
  218.     case WM_INITDIALOG:
  219.         return TRUE;
  220.  
  221.     case WM_COMMAND:
  222.         if (wParam == IDOK
  223.         || wParam == IDCANCEL)
  224.         {
  225.         EndDialog (hDlg, TRUE);
  226.         return (TRUE);
  227.         }
  228.         break;
  229.     }
  230.     return FALSE;
  231. }
  232.  
  233.  
  234. void DoWhoIsQuery (HWND hDlg, LPSTR lpszName, LPSTR lpszQHost)
  235. {
  236.     struct sockaddr_in;
  237.     HANDLE hWind;
  238.     REQUEST FAR *thisreq;
  239.     
  240.     hWind = CreateWindow ("PurposelessWindow",
  241.          "Whois/Win query",
  242.          WS_OVERLAPPEDWINDOW,
  243.          CW_USEDEFAULT,
  244.          CW_USEDEFAULT,
  245.          CW_USEDEFAULT,
  246.          CW_USEDEFAULT,
  247.          hDlg,
  248.          NULL,
  249.          hInst,
  250.          NULL);
  251.     if (hWind == NULL)
  252.     {    
  253.         MessageBox (NULL, "Could not start query",
  254.                        "WinWhois", MB_OK|MB_ICONHAND);
  255.         return;
  256.     }
  257.  
  258.     thisreq = HeadReq;
  259.     if (thisreq != NULL)
  260.     {
  261.         while (thisreq->NextReq != NULL)
  262.         {
  263.             thisreq = thisreq->NextReq;
  264.         }
  265.         thisreq->hNextReq = GlobalAlloc (GHND, sizeof (REQUEST));
  266.         thisreq->NextReq = (REQUEST FAR*)GlobalLock (thisreq->hNextReq);
  267.         thisreq = thisreq->NextReq;
  268.     }
  269.     else
  270.     {
  271.         hHeadReq = GlobalAlloc (GHND, sizeof (REQUEST));
  272.         HeadReq = (REQUEST FAR*)GlobalLock (hHeadReq);
  273.         thisreq = HeadReq;
  274.     }
  275.     thisreq->hWin = hWind;
  276.     thisreq->hHost = GlobalAlloc (GHND, 256);
  277.     thisreq->lpHost = GlobalLock (thisreq->hHost);
  278.     lstrcpy (thisreq->lpHost, lpszName);
  279.     thisreq->hQuery = GlobalAlloc (GHND, 128);
  280.     thisreq->lpQuery = GlobalLock (thisreq->hQuery);
  281.     lstrcpy (thisreq->lpQuery, lpszQHost);
  282.     thisreq->NextReq = NULL;
  283.     thisreq->hNextReq = NULL;
  284.     PostMessage (hWind, START_QUERY, 0, 0);
  285.     
  286.     return;
  287. }
  288.  
  289. BOOL FAR PASCAL MainDialog (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  290.  
  291. {
  292.  
  293.     switch (message)
  294.     {
  295.     case WM_INITDIALOG:
  296.         {
  297.         hDialog = hDlg;
  298.         SendDlgItemMessage (hDlg, IDC_RESPONSES, EM_LIMITTEXT,0,0);
  299.         SendDlgItemMessage (hDlg, IDC_RESPONSES, EM_FMTLINES, 1, 0L);
  300.         EnableWindow (GetDlgItem (hDlg, IDC_MAKEQUERY), FALSE);
  301.         return TRUE;
  302.         }
  303.     case WM_CLOSE: {
  304.         EndDialog (hDlg, 0);
  305.         break;
  306.         }
  307.  
  308.     case WM_COMMAND:
  309.         switch (LOWORD(wParam)) {
  310.         case IDM_ABOUT:
  311.             {
  312.             FARPROC lpfnAboutDialog;
  313.             lpfnAboutDialog = MakeProcInstance (About, hInst);
  314.             DialogBox (hInst,
  315.                    "AboutBox",
  316.                    hDlg,
  317.                    lpfnAboutDialog);
  318.             FreeProcInstance (lpfnAboutDialog);
  319.             return TRUE;
  320.             }
  321.         case IDM_CHANGEHOST:
  322.         {
  323.             FARPROC lpfnChangeDialog;
  324.             lpfnChangeDialog = MakeProcInstance (ChangeHost, hInst);
  325.             DialogBox (hInst, "GetHostBox", hDlg, lpfnChangeDialog);
  326.             FreeProcInstance (lpfnChangeDialog);
  327.             return TRUE;
  328.         }
  329.         case IDM_CUT:
  330.         case IDM_COPY:
  331.         case IDM_CLEAR:
  332.             /* if the selection is empty, make it all... */
  333.             {
  334.             DWORD dwStartAndEnd;
  335.             WORD wStartChar;
  336.             WORD wEndChar;
  337.  
  338.             dwStartAndEnd = SendDlgItemMessage (hDlg,
  339.             IDC_RESPONSES, EM_GETSEL, 0, 0L);
  340.             wStartChar = LOWORD (dwStartAndEnd);
  341.             wEndChar = HIWORD (dwStartAndEnd);
  342.  
  343.             if (wStartChar == wEndChar)
  344.             {
  345.                 SendDlgItemMessage (hDlg, IDC_RESPONSES,
  346.                         EM_SETSEL, 0,
  347.                         MAKELPARAM (0, -1));
  348.                 dwStartAndEnd = SendDlgItemMessage (hDlg,
  349.                 IDC_RESPONSES, EM_GETSEL, 0, 0L);
  350.                 wStartChar = LOWORD (dwStartAndEnd);
  351.                 wEndChar = HIWORD (dwStartAndEnd);
  352.                 if (wStartChar == wEndChar)
  353.                 break;
  354.             }
  355.  
  356.             if (wParam == IDM_CUT)
  357.             SendDlgItemMessage (hDlg, IDC_RESPONSES,
  358.                         WM_CUT, 0,0);
  359.             else if (wParam == IDM_COPY)
  360.             SendDlgItemMessage (hDlg, IDC_RESPONSES,
  361.                         WM_COPY, 0, 0);
  362.             else
  363.             SendDlgItemMessage (hDlg, IDC_RESPONSES,
  364.                         WM_CLEAR, 0, 0);
  365.             SetFocus (GetDlgItem (hDlg, IDC_NAMEINPUT));
  366.             return TRUE;
  367.             }
  368.         case IDC_HELP:
  369.             {
  370.             MessageBox (NULL, "Enter the name you wish to query in the "
  371.                       "Name to Query field, and press return.  "
  372.                       "For help about whois, query on the name "
  373.                       "\"help\".",
  374.                     "WinWhoIs", MB_ICONINFORMATION | MB_OK);
  375.             SetFocus (GetDlgItem (hDlg, IDC_NAMEINPUT));
  376.             return TRUE;
  377.             }
  378.         case IDC_CLEAR:
  379.                 ClearText (hDlg, IDC_RESPONSES);
  380.                 SetFocus (GetDlgItem (hDlg, IDC_NAMEINPUT));
  381.                 return TRUE;
  382.         case IDC_NAMEINPUT:
  383.             if (HIWORD (wParam) == EN_CHANGE) {
  384.             if (SendDlgItemMessage (hDlg, IDC_NAMEINPUT,
  385.                         EM_LINELENGTH, 0, 0L))
  386.                 EnableWindow (GetDlgItem (hDlg, IDC_MAKEQUERY),
  387.                       TRUE);
  388.             else
  389.                 EnableWindow (GetDlgItem (hDlg, IDC_MAKEQUERY),
  390.                       FALSE);
  391.             return TRUE;
  392.             }
  393.             else
  394.             return FALSE;
  395.  
  396.         case IDC_MAKEQUERY:                                                   
  397.                 SendDlgItemMessage (hDlg, IDC_NAMEINPUT, (UINT) WM_GETTEXT,
  398.                         (WPARAM) 255,
  399.                         (LPARAM) ((LPSTR) szHost));
  400.                 SendDlgItemMessage (hDlg, IDC_NAMEINPUT, (UINT) EM_SETSEL,
  401.                         0, MAKELPARAM (0,-1));
  402.                 if (*szHost)
  403.                 {
  404.                     char szStatus[144];
  405.                     strcpy (szStatus, "Querying server '");
  406.                     strcat (szStatus, szQueryhost);
  407.                     strcat (szStatus, "'.");
  408.                     strcat (szHost, "\r\n");
  409.                     SendDlgItemMessage (hDlg, IDC_STATUS, WM_SETTEXT,
  410.                             0, (LPARAM) (LPCSTR)
  411.                             szStatus);
  412.             DoWhoIsQuery(hDlg,(LPSTR)szHost,(LPSTR)szQueryhost);
  413.                     return TRUE;
  414.                 }
  415.                 else {
  416.                 MessageBox (NULL, "No name to query specified!",
  417.                         "WinWhoIs", MB_OK | MB_ICONHAND);
  418.                 return TRUE;
  419.                 }
  420.  
  421.         case IDC_GETLIST:
  422.             {
  423.                     char szStatus[144];
  424.                     strcpy (szStatus, "Querying server '");
  425.                     strcat (szStatus, szServHost);
  426.                     strcat (szStatus, "'.");
  427.                     strcat (szHost, "\r\n");
  428.                     SendDlgItemMessage (hDlg, IDC_STATUS, WM_SETTEXT,
  429.                             0, (LPARAM) (LPCSTR)
  430.                             szStatus);
  431.                     DoWhoIsQuery (hDlg, (LPSTR)"whois-servers\r\n", 
  432.                                         (LPSTR)szServHost);
  433.                     return TRUE;
  434.             }
  435.         
  436.         case IDC_EXIT:
  437.                 while (hHeadReq != NULL)
  438.                 {
  439.                     HWND hWindow;
  440.                     hWindow = HeadReq->hWin;
  441.                     closesocket (HeadReq->s);
  442.                     CleanRequest (hWindow);
  443.                     DestroyWindow (hWindow);
  444.                 }
  445.                 EndDialog (hDlg, 0);
  446.                 return TRUE;
  447.         }
  448.         return FALSE;
  449.  
  450.     }
  451.     return FALSE;
  452. }
  453.  
  454.  
  455.  
  456. void CopyText (HWND hDlg, WORD wIDControl)
  457. {
  458.  
  459.     HANDLE hWholeString;
  460.     HANDLE hCutString;
  461.     LPSTR lpszWholeString;
  462.     LPSTR lpszCutString;
  463.     DWORD ccControl;
  464.     WORD wStartChar;
  465.     WORD wEndChar;
  466.     DWORD dwStartAndEnd;
  467.  
  468.     dwStartAndEnd = SendDlgItemMessage (hDlg, wIDControl, EM_GETSEL, 0, 0L);
  469.     wStartChar = LOWORD (dwStartAndEnd);
  470.     wEndChar = HIWORD (dwStartAndEnd);
  471.  
  472.     if (wStartChar == wEndChar)
  473.     {
  474.     SendDlgItemMessage (hDlg, wIDControl, EM_SETSEL, 0,
  475.                 MAKELPARAM (0, -1));
  476.     dwStartAndEnd = SendDlgItemMessage (hDlg, wIDControl,
  477.                         EM_GETSEL, 0, 0L);
  478.     wStartChar = LOWORD (dwStartAndEnd);
  479.     wEndChar = HIWORD (dwStartAndEnd);
  480.     if (wStartChar == wEndChar)
  481.         return;
  482.     }
  483.  
  484.     hWholeString = GlobalAlloc (GHND, ccControl =
  485.                  SendDlgItemMessage (hDlg, wIDControl,
  486.                  WM_GETTEXTLENGTH, 0,0) + 256);
  487.     lpszWholeString = GlobalLock (hWholeString);
  488.  
  489.     SendDlgItemMessage (hDlg, wIDControl, WM_GETTEXT, (WPARAM) ccControl,
  490.                (LPARAM) lpszWholeString);
  491.  
  492.  
  493.     hCutString = GlobalAlloc (GHND, wEndChar - wStartChar + 256);
  494.     lpszCutString = GlobalLock (hCutString);
  495.  
  496.     memcpy (lpszCutString, lpszWholeString + wStartChar * sizeof (char),
  497.         (wEndChar - wStartChar) * sizeof (char));
  498.  
  499.     GlobalUnlock (hCutString);
  500.  
  501.     OpenClipboard (hDlg);
  502.     EmptyClipboard ();
  503.     SetClipboardData (CF_TEXT, hCutString);
  504.     CloseClipboard ();
  505.     GlobalFree (hCutString);
  506.  
  507.  
  508.     GlobalUnlock (hWholeString);
  509.     GlobalFree (hWholeString);
  510.  
  511. }
  512.  
  513. void ClearText (HWND hDlg, WORD wIDControl)
  514. {
  515.     DWORD dwStartAndEnd;
  516.     WORD wStartChar;
  517.     WORD wEndChar;
  518.  
  519.     dwStartAndEnd = SendDlgItemMessage (hDlg, wIDControl, EM_GETSEL, 0, 0L);
  520.     wStartChar = LOWORD (dwStartAndEnd);
  521.     wEndChar = HIWORD (dwStartAndEnd);
  522.  
  523.     if (wStartChar == wEndChar)
  524.     SendDlgItemMessage (hDlg, wIDControl, EM_SETSEL, 0,
  525.                 MAKELPARAM (0, -1));
  526.  
  527.     SendDlgItemMessage (hDlg, wIDControl, EM_REPLACESEL, 0,
  528.             (LPARAM) (LPCSTR) "");
  529.  
  530. }
  531.  
  532.  
  533. LONG FAR PASCAL QueryWndProc (HWND hWind, UINT message, WPARAM wParam, LPARAM lParam)
  534. {
  535.     int s, i, cc, iOutPos;
  536.     struct sockaddr_in;
  537.     DWORD addr;
  538.     HANDLE hszLine, hszOutput;
  539.     LPSTR lpszLine;
  540.     LPSTR lpszOutput;
  541.     REQUEST FAR *thisreq;
  542.         
  543.     switch (message) {
  544.     
  545.         case START_QUERY:
  546.             {
  547.                 thisreq = FindRequest (hWind);
  548.                 thisreq->hbuffer = GlobalAlloc (GHND, MAXGETHOSTSTRUCT);
  549.                 thisreq->lpbuffer = (struct hostent FAR *)GlobalLock (thisreq->hbuffer);
  550.                 WSAAsyncGetHostByName (hWind, IDC_GOTADDR, 
  551.                     thisreq->lpQuery, (LPSTR)thisreq->lpbuffer, MAXGETHOSTSTRUCT);
  552.                 return TRUE;
  553.             }
  554.         case IDC_GOTADDR:
  555.             {
  556.                 unsigned long ii;
  557.                 ii = 1;
  558.                 thisreq = FindRequest (hWind);
  559.  
  560.                 if (WSAGETASYNCERROR (lParam) != 0) {
  561.                 MessageBox (NULL, "Could not resolve host",
  562.                         "WinWhoIs", MB_OK | MB_ICONHAND);
  563.                 GRelease (&thisreq->hbuffer);
  564.                 return FALSE;
  565.                 }
  566.                 memcpy (&addr, thisreq->lpbuffer->h_addr_list[0],
  567.                             thisreq->lpbuffer->h_length);
  568.                 thisreq->s = socket (PF_INET, SOCK_STREAM, 0);
  569.                 if (thisreq->s < 0) {
  570.                     MessageBox (NULL, "Could not create socket for communication",
  571.                         "WinWhoIs", MB_OK | MB_ICONHAND);
  572.                     GRelease (&thisreq->hbuffer);
  573.                     PostMessage (hWind, IDC_RESETDISPLAY, 0, 0);
  574.                 return FALSE;
  575.                 }
  576.                 ioctlsocket (thisreq->s, FIONBIO, (unsigned long FAR *)&ii);
  577.                 thisreq->sock_in.sin_family = PF_INET;
  578.                 thisreq->sock_in.sin_port = 0;
  579.                 thisreq->sock_in.sin_addr.s_addr = INADDR_ANY;
  580.                 if (bind (thisreq->s, (struct sockaddr FAR*) &(thisreq->sock_in),
  581.                      sizeof (thisreq->sock_in)) < 0) {
  582.                     MessageBox (NULL, "Could not bind socket.",
  583.                         "WinWhoIs", MB_OK | MB_ICONHAND);
  584.                     closesocket (thisreq->s);
  585.                     GRelease (&thisreq->hbuffer);
  586.                     PostMessage (hWind, IDC_RESETDISPLAY, 0, 0);
  587.                 return FALSE;
  588.                 }
  589.                 GRelease (&thisreq->hbuffer);
  590.                 thisreq->sphandle = GlobalAlloc (GHND, MAXGETHOSTSTRUCT);
  591.                 thisreq->sp = (struct servent FAR *)GlobalLock (thisreq->sphandle);
  592.                 memcpy ((char FAR *)&(thisreq->sock_in.sin_addr), (char FAR *)&addr, 4);
  593.                 WSAAsyncGetServByName (hWind, IDC_GOTSERVER, "whois", "tcp", 
  594.                     (LPSTR)thisreq->sp, MAXGETHOSTSTRUCT);
  595.                 return TRUE;
  596.             }
  597.         case IDC_GOTSERVER:
  598.             {
  599.                 thisreq = FindRequest (hWind);
  600.                 if (WSAGETASYNCERROR (lParam) != 0) {
  601.                     MessageBox (NULL, "Found no service: whois / tcp",
  602.                         "WinWhoIs", MB_OK | MB_ICONHAND);
  603.                     closesocket (thisreq->s);
  604.                     GRelease (&thisreq->sphandle);
  605.                     PostMessage (hWind, IDC_RESETDISPLAY, 0, 0);
  606.                     return FALSE;
  607.                 }
  608.                 if (!thisreq->sp) {
  609.                     MessageBox (NULL, "Can't lock service: whois / tcp",
  610.                         "WinWhoIs", MB_OK | MB_ICONHAND);
  611.                     closesocket (thisreq->s);
  612.                     GRelease (&thisreq->sphandle);
  613.                     PostMessage (hWind, IDC_RESETDISPLAY, 0, 0);
  614.                     return FALSE;
  615.                 }
  616.                 thisreq->sock_in.sin_port = thisreq->sp -> s_port;
  617.  
  618.                 if (connect (thisreq->s, (struct sockaddr FAR *) &(thisreq->sock_in),
  619.                      sizeof (thisreq->sock_in)) != 0) {
  620.                     int lerror;
  621.                     lerror = WSAGetLastError ();
  622.                     if (lerror == WSAEWOULDBLOCK)
  623.                     {
  624.                         WSAAsyncSelect (thisreq->s, hWind, IDC_GOTCONNECT, FD_CONNECT);
  625.                         GRelease (&thisreq->sphandle);
  626.                         return FALSE;
  627.                     }
  628.                     else
  629.                     {
  630.                         wsprintf (lpszLine,"Could not connect; error #%d", lerror);
  631.                         MessageBox (NULL, lpszLine,
  632.                             "WinWhoIs", MB_OK | MB_ICONHAND);
  633.                         closesocket (thisreq->s);
  634.                         GRelease (&thisreq->sphandle);
  635.                         PostMessage (hWind, IDC_RESETDISPLAY, 0, 0);
  636.                         return FALSE;
  637.                     }
  638.                 }
  639.             
  640.                 GRelease (&thisreq->sphandle);
  641.                 /*  Fall Through  */
  642.             }
  643.         case IDC_GOTCONNECT:
  644.             {
  645.                 thisreq = FindRequest (hWind);
  646.                 if (send (thisreq->s, (LPSTR)thisreq->lpHost, 
  647.                         lstrlen ((LPSTR)thisreq->lpHost), 0) == SOCKET_ERROR)    {
  648.                     int lerror;
  649.                     lerror = WSAGetLastError ();
  650.                     if (lerror == WSAEWOULDBLOCK)
  651.                     {
  652.                         WSAAsyncSelect (thisreq->s, hWind, IDC_GOTCONNECT, FD_WRITE);
  653.                         return FALSE;
  654.                     }
  655.                     else
  656.                     {
  657.                         MessageBox (NULL, "Could not send request",
  658.                             "WinWhoIs", MB_OK | MB_ICONHAND);
  659.                         closesocket (thisreq->s);
  660.                         PostMessage (hWind, IDC_RESETDISPLAY, 0, 0);
  661.                         return FALSE;
  662.                     }
  663.                 }
  664.                 WSAAsyncSelect (thisreq->s, hWind, IDC_GOTRESPONSE, FD_READ | FD_CLOSE);
  665.             return TRUE;
  666.             }                       
  667.         case IDC_GOTRESPONSE:
  668.             {
  669.                 hszLine = GlobalAlloc (GHND, 256);
  670.                 lpszLine = GlobalLock (hszLine);
  671.                 hszOutput = GlobalAlloc (GHND, 512);
  672.                 lpszOutput = GlobalLock (hszOutput);
  673.                 s = (int) wParam;
  674.  
  675.                 if ((cc = recv (s, lpszLine, 256, 0)) > 0) {
  676.                     for (i = 0, iOutPos = 0; i < cc; ++i)
  677.                         if (lpszLine [i] == '\n') {
  678.                         lpszOutput [iOutPos++] = '\r';
  679.                         lpszOutput [iOutPos++] = '\n';
  680.                         }
  681.                         else
  682.                         lpszOutput [iOutPos++] = lpszLine [i];
  683.                     
  684.                     lpszOutput [iOutPos] = '\0';
  685.                     SendDlgItemMessage (hDialog, IDC_RESPONSES, EM_REPLACESEL,
  686.                         0, (LPARAM) ((LPCSTR) lpszOutput));
  687.                     GlobalUnlock (hszLine);
  688.                     GlobalFree (hszLine);
  689.                     GlobalUnlock (hszOutput);
  690.                     GlobalFree (hszOutput);
  691.  
  692.                     return FALSE;  /* If there's more, WSASelect() will get it... */
  693.                 }
  694.                 else if (cc != 0)
  695.                 {
  696.                     int ii;
  697.                     if (ii = WSAGetLastError () == WSAEWOULDBLOCK)
  698.                         return FALSE;
  699.                     MessageBox (NULL, "Receiving error occurred.",
  700.                                 "WinWhois", MB_OK | MB_ICONHAND);
  701.                 }
  702.         /*  Now we're done;  clean up  */
  703.  
  704.                 WSAAsyncSelect (s, hWind, 0, 0);
  705.                 if (closesocket (s))
  706.                     MessageBox (NULL, "Socket did not close!", "WinWhois",
  707.                             MB_OK | MB_ICONHAND);
  708.                 GlobalUnlock (hszLine);
  709.                 GlobalFree (hszLine);
  710.                 GlobalUnlock (hszOutput);
  711.                 GlobalFree (hszOutput);
  712.                             SendDlgItemMessage (hDialog, IDC_STATUS, WM_SETTEXT,
  713.                             0, (LPARAM) (LPCSTR)
  714.                             "Waiting for input...");
  715.                     SetFocus (GetDlgItem (hDialog, IDC_NAMEINPUT));
  716.                 /* Fall Through */
  717.             }
  718.             case IDC_RESETDISPLAY:
  719.             {
  720.                 CleanRequest (hWind);
  721.                 DestroyWindow (hWind);
  722.                 return TRUE;
  723.             }
  724.  
  725.        default:
  726.             return (DefWindowProc (hWind, message, wParam, lParam));
  727.       } /*switch*/
  728.     return FALSE;
  729. }
  730.  
  731. REQUEST FAR *FindRequest (HWND hWind)
  732. {
  733.     REQUEST FAR *CurrReq;
  734.     int flag;
  735.     
  736.     flag = TRUE;
  737.     CurrReq = HeadReq;
  738.     while ((CurrReq != NULL) && (flag))
  739.     {
  740.         if (CurrReq->hWin == hWind)
  741.             flag = FALSE;
  742.         else
  743.         {
  744.             CurrReq = CurrReq->NextReq;
  745.         }
  746.     }
  747.     return CurrReq;
  748. }
  749.  
  750. void GRelease (HANDLE FAR *h)
  751. {
  752.     if (*h)
  753.     {
  754.         GlobalUnlock (*h);
  755.         GlobalFree (*h);
  756.         *h=NULL;
  757.     }
  758.     return;
  759. }
  760.  
  761. void CleanRequest (HWND hWind)
  762. {
  763.     REQUEST FAR *thisreq;
  764.     REQUEST FAR *tempreq;
  765.     HANDLE temph;
  766.     
  767.     thisreq = FindRequest (hWind);
  768.     GRelease (&thisreq->hHost);
  769.     GRelease (&thisreq->hQuery);
  770.     GRelease (&thisreq->hbuffer);
  771.     GRelease (&thisreq->sphandle);
  772.     if (thisreq == HeadReq)
  773.     {
  774.         tempreq = thisreq->NextReq;
  775.         temph = thisreq->hNextReq;
  776.         GRelease (&hHeadReq);
  777.         hHeadReq = temph;
  778.         HeadReq = tempreq;
  779.         return;
  780.     }
  781.     tempreq = HeadReq;
  782.     while (tempreq->NextReq != thisreq)
  783.         tempreq = tempreq->NextReq;
  784.     temph = tempreq->hNextReq;
  785.     tempreq->hNextReq = thisreq->hNextReq;
  786.     tempreq->NextReq = thisreq->NextReq;
  787.     GRelease (&temph);
  788.     return;
  789. }
  790.  
  791.