📄 winwhois.c
字号:
SendDlgItemMessage (hDlg, IDC_NAMEINPUT, (UINT) WM_GETTEXT,
(WPARAM) 255,
(LPARAM) ((LPSTR) szHost));
SendDlgItemMessage (hDlg, IDC_NAMEINPUT, (UINT) EM_SETSEL,
0, MAKELPARAM (0,-1));
if (*szHost)
{
char szStatus[144];
strcpy (szStatus, "Querying server '");
strcat (szStatus, szQueryhost);
strcat (szStatus, "'.");
strcat (szHost, "\r\n");
SendDlgItemMessage (hDlg, IDC_STATUS, WM_SETTEXT,
0, (LPARAM) (LPCSTR)
szStatus);
DoWhoIsQuery(hDlg,(LPSTR)szHost,(LPSTR)szQueryhost);
return TRUE;
}
else {
MessageBox (NULL, "No name to query specified!",
"WinWhoIs", MB_OK | MB_ICONHAND);
return TRUE;
}
case IDC_GETLIST:
{
char szStatus[144];
strcpy (szStatus, "Querying server '");
strcat (szStatus, szServHost);
strcat (szStatus, "'.");
strcat (szHost, "\r\n");
SendDlgItemMessage (hDlg, IDC_STATUS, WM_SETTEXT,
0, (LPARAM) (LPCSTR)
szStatus);
DoWhoIsQuery (hDlg, (LPSTR)"whois-servers\r\n",
(LPSTR)szServHost);
return TRUE;
}
case IDC_EXIT:
while (hHeadReq != NULL)
{
HWND hWindow;
hWindow = HeadReq->hWin;
closesocket (HeadReq->s);
CleanRequest (hWindow);
DestroyWindow (hWindow);
}
EndDialog (hDlg, 0);
return TRUE;
}
return FALSE;
}
return FALSE;
}
void CopyText (HWND hDlg, WORD wIDControl)
{
HANDLE hWholeString;
HANDLE hCutString;
LPSTR lpszWholeString;
LPSTR lpszCutString;
DWORD ccControl;
WORD wStartChar;
WORD wEndChar;
DWORD dwStartAndEnd;
dwStartAndEnd = SendDlgItemMessage (hDlg, wIDControl, EM_GETSEL, 0, 0L);
wStartChar = LOWORD (dwStartAndEnd);
wEndChar = HIWORD (dwStartAndEnd);
if (wStartChar == wEndChar)
{
SendDlgItemMessage (hDlg, wIDControl, EM_SETSEL, 0,
MAKELPARAM (0, -1));
dwStartAndEnd = SendDlgItemMessage (hDlg, wIDControl,
EM_GETSEL, 0, 0L);
wStartChar = LOWORD (dwStartAndEnd);
wEndChar = HIWORD (dwStartAndEnd);
if (wStartChar == wEndChar)
return;
}
hWholeString = GlobalAlloc (GHND, ccControl =
SendDlgItemMessage (hDlg, wIDControl,
WM_GETTEXTLENGTH, 0,0) + 256);
lpszWholeString = GlobalLock (hWholeString);
SendDlgItemMessage (hDlg, wIDControl, WM_GETTEXT, (WPARAM) ccControl,
(LPARAM) lpszWholeString);
hCutString = GlobalAlloc (GHND, wEndChar - wStartChar + 256);
lpszCutString = GlobalLock (hCutString);
memcpy (lpszCutString, lpszWholeString + wStartChar * sizeof (char),
(wEndChar - wStartChar) * sizeof (char));
GlobalUnlock (hCutString);
OpenClipboard (hDlg);
EmptyClipboard ();
SetClipboardData (CF_TEXT, hCutString);
CloseClipboard ();
GlobalFree (hCutString);
GlobalUnlock (hWholeString);
GlobalFree (hWholeString);
}
void ClearText (HWND hDlg, WORD wIDControl)
{
DWORD dwStartAndEnd;
WORD wStartChar;
WORD wEndChar;
dwStartAndEnd = SendDlgItemMessage (hDlg, wIDControl, EM_GETSEL, 0, 0L);
wStartChar = LOWORD (dwStartAndEnd);
wEndChar = HIWORD (dwStartAndEnd);
if (wStartChar == wEndChar)
SendDlgItemMessage (hDlg, wIDControl, EM_SETSEL, 0,
MAKELPARAM (0, -1));
SendDlgItemMessage (hDlg, wIDControl, EM_REPLACESEL, 0,
(LPARAM) (LPCSTR) "");
}
LONG FAR PASCAL QueryWndProc (HWND hWind, UINT message, WPARAM wParam, LPARAM lParam)
{
int s, i, cc, iOutPos;
struct sockaddr_in;
DWORD addr;
HANDLE hszLine, hszOutput;
LPSTR lpszLine;
LPSTR lpszOutput;
REQUEST FAR *thisreq;
switch (message) {
case START_QUERY:
{
thisreq = FindRequest (hWind);
thisreq->hbuffer = GlobalAlloc (GHND, MAXGETHOSTSTRUCT);
thisreq->lpbuffer = (struct hostent FAR *)GlobalLock (thisreq->hbuffer);
WSAAsyncGetHostByName (hWind, IDC_GOTADDR,
thisreq->lpQuery, (LPSTR)thisreq->lpbuffer, MAXGETHOSTSTRUCT);
return TRUE;
}
case IDC_GOTADDR:
{
unsigned long ii;
ii = 1;
thisreq = FindRequest (hWind);
if (WSAGETASYNCERROR (lParam) != 0) {
MessageBox (NULL, "Could not resolve host",
"WinWhoIs", MB_OK | MB_ICONHAND);
GRelease (&thisreq->hbuffer);
return FALSE;
}
memcpy (&addr, thisreq->lpbuffer->h_addr_list[0],
thisreq->lpbuffer->h_length);
thisreq->s = socket (PF_INET, SOCK_STREAM, 0);
if (thisreq->s < 0) {
MessageBox (NULL, "Could not create socket for communication",
"WinWhoIs", MB_OK | MB_ICONHAND);
GRelease (&thisreq->hbuffer);
PostMessage (hWind, IDC_RESETDISPLAY, 0, 0);
return FALSE;
}
ioctlsocket (thisreq->s, FIONBIO, (unsigned long FAR *)&ii);
thisreq->sock_in.sin_family = PF_INET;
thisreq->sock_in.sin_port = 0;
thisreq->sock_in.sin_addr.s_addr = INADDR_ANY;
if (bind (thisreq->s, (struct sockaddr FAR*) &(thisreq->sock_in),
sizeof (thisreq->sock_in)) < 0) {
MessageBox (NULL, "Could not bind socket.",
"WinWhoIs", MB_OK | MB_ICONHAND);
closesocket (thisreq->s);
GRelease (&thisreq->hbuffer);
PostMessage (hWind, IDC_RESETDISPLAY, 0, 0);
return FALSE;
}
GRelease (&thisreq->hbuffer);
thisreq->sphandle = GlobalAlloc (GHND, MAXGETHOSTSTRUCT);
thisreq->sp = (struct servent FAR *)GlobalLock (thisreq->sphandle);
memcpy ((char FAR *)&(thisreq->sock_in.sin_addr), (char FAR *)&addr, 4);
WSAAsyncGetServByName (hWind, IDC_GOTSERVER, "whois", "tcp",
(LPSTR)thisreq->sp, MAXGETHOSTSTRUCT);
return TRUE;
}
case IDC_GOTSERVER:
{
thisreq = FindRequest (hWind);
if (WSAGETASYNCERROR (lParam) != 0) {
MessageBox (NULL, "Found no service: whois / tcp",
"WinWhoIs", MB_OK | MB_ICONHAND);
closesocket (thisreq->s);
GRelease (&thisreq->sphandle);
PostMessage (hWind, IDC_RESETDISPLAY, 0, 0);
return FALSE;
}
if (!thisreq->sp) {
MessageBox (NULL, "Can't lock service: whois / tcp",
"WinWhoIs", MB_OK | MB_ICONHAND);
closesocket (thisreq->s);
GRelease (&thisreq->sphandle);
PostMessage (hWind, IDC_RESETDISPLAY, 0, 0);
return FALSE;
}
thisreq->sock_in.sin_port = thisreq->sp -> s_port;
if (connect (thisreq->s, (struct sockaddr FAR *) &(thisreq->sock_in),
sizeof (thisreq->sock_in)) != 0) {
int lerror;
lerror = WSAGetLastError ();
if (lerror == WSAEWOULDBLOCK)
{
WSAAsyncSelect (thisreq->s, hWind, IDC_GOTCONNECT, FD_CONNECT);
GRelease (&thisreq->sphandle);
return FALSE;
}
else
{
wsprintf (lpszLine,"Could not connect; error #%d", lerror);
MessageBox (NULL, lpszLine,
"WinWhoIs", MB_OK | MB_ICONHAND);
closesocket (thisreq->s);
GRelease (&thisreq->sphandle);
PostMessage (hWind, IDC_RESETDISPLAY, 0, 0);
return FALSE;
}
}
GRelease (&thisreq->sphandle);
/* Fall Through */
}
case IDC_GOTCONNECT:
{
thisreq = FindRequest (hWind);
if (send (thisreq->s, (LPSTR)thisreq->lpHost,
lstrlen ((LPSTR)thisreq->lpHost), 0) == SOCKET_ERROR) {
int lerror;
lerror = WSAGetLastError ();
if (lerror == WSAEWOULDBLOCK)
{
WSAAsyncSelect (thisreq->s, hWind, IDC_GOTCONNECT, FD_WRITE);
return FALSE;
}
else
{
MessageBox (NULL, "Could not send request",
"WinWhoIs", MB_OK | MB_ICONHAND);
closesocket (thisreq->s);
PostMessage (hWind, IDC_RESETDISPLAY, 0, 0);
return FALSE;
}
}
WSAAsyncSelect (thisreq->s, hWind, IDC_GOTRESPONSE, FD_READ | FD_CLOSE);
return TRUE;
}
case IDC_GOTRESPONSE:
{
hszLine = GlobalAlloc (GHND, 256);
lpszLine = GlobalLock (hszLine);
hszOutput = GlobalAlloc (GHND, 512);
lpszOutput = GlobalLock (hszOutput);
s = (int) wParam;
if ((cc = recv (s, lpszLine, 256, 0)) > 0) {
for (i = 0, iOutPos = 0; i < cc; ++i)
if (lpszLine [i] == '\n') {
lpszOutput [iOutPos++] = '\r';
lpszOutput [iOutPos++] = '\n';
}
else
lpszOutput [iOutPos++] = lpszLine [i];
lpszOutput [iOutPos] = '\0';
SendDlgItemMessage (hDialog, IDC_RESPONSES, EM_REPLACESEL,
0, (LPARAM) ((LPCSTR) lpszOutput));
GlobalUnlock (hszLine);
GlobalFree (hszLine);
GlobalUnlock (hszOutput);
GlobalFree (hszOutput);
return FALSE; /* If there's more, WSASelect() will get it... */
}
else if (cc != 0)
{
int ii;
if (ii = WSAGetLastError () == WSAEWOULDBLOCK)
return FALSE;
MessageBox (NULL, "Receiving error occurred.",
"WinWhois", MB_OK | MB_ICONHAND);
}
/* Now we're done; clean up */
WSAAsyncSelect (s, hWind, 0, 0);
if (closesocket (s))
MessageBox (NULL, "Socket did not close!", "WinWhois",
MB_OK | MB_ICONHAND);
GlobalUnlock (hszLine);
GlobalFree (hszLine);
GlobalUnlock (hszOutput);
GlobalFree (hszOutput);
SendDlgItemMessage (hDialog, IDC_STATUS, WM_SETTEXT,
0, (LPARAM) (LPCSTR)
"Waiting for input...");
SetFocus (GetDlgItem (hDialog, IDC_NAMEINPUT));
/* Fall Through */
}
case IDC_RESETDISPLAY:
{
CleanRequest (hWind);
DestroyWindow (hWind);
return TRUE;
}
default:
return (DefWindowProc (hWind, message, wParam, lParam));
} /*switch*/
return FALSE;
}
REQUEST FAR *FindRequest (HWND hWind)
{
REQUEST FAR *CurrReq;
int flag;
flag = TRUE;
CurrReq = HeadReq;
while ((CurrReq != NULL) && (flag))
{
if (CurrReq->hWin == hWind)
flag = FALSE;
else
{
CurrReq = CurrReq->NextReq;
}
}
return CurrReq;
}
void GRelease (HANDLE FAR *h)
{
if (*h)
{
GlobalUnlock (*h);
GlobalFree (*h);
*h=NULL;
}
return;
}
void CleanRequest (HWND hWind)
{
REQUEST FAR *thisreq;
REQUEST FAR *tempreq;
HANDLE temph;
thisreq = FindRequest (hWind);
GRelease (&thisreq->hHost);
GRelease (&thisreq->hQuery);
GRelease (&thisreq->hbuffer);
GRelease (&thisreq->sphandle);
if (thisreq == HeadReq)
{
tempreq = thisreq->NextReq;
temph = thisreq->hNextReq;
GRelease (&hHeadReq);
hHeadReq = temph;
HeadReq = tempreq;
return;
}
tempreq = HeadReq;
while (tempreq->NextReq != thisreq)
tempreq = tempreq->NextReq;
temph = tempreq->hNextReq;
tempreq->hNextReq = thisreq->hNextReq;
tempreq->NextReq = thisreq->NextReq;
GRelease (&temph);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -