From b35ee2d2eadf991f42c43e0ea16242278363679c Mon Sep 17 00:00:00 2001 From: correy <112426112@qq.com> Date: Wed, 31 Jan 2024 21:28:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0reactos=E7=9A=84whois.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NetTool/NetTool.cpp | 4 + NetTool/NetTool.vcxproj.user | 2 +- inc/libnet.h | 3 + libnet.sln | 3 + libnet/libnet.vcxproj | 1 + libnet/libnet.vcxproj.filters | 3 + libnet/whois.cpp | 189 ++++++++++++++++++++++++++++++++++ 7 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 libnet/whois.cpp diff --git a/NetTool/NetTool.cpp b/NetTool/NetTool.cpp index d4f13b1..7223488 100644 --- a/NetTool/NetTool.cpp +++ b/NetTool/NetTool.cpp @@ -91,6 +91,10 @@ int _cdecl main(_In_ int argc, _In_reads_(argc) CHAR * argv[]) tracert(--argc, ++argv); } + else if (_wcsicmp(Arglist[1], L"whois") == 0) { + whois(--argc, ++argv); + } + else { Usage(Arglist[0]); } diff --git a/NetTool/NetTool.vcxproj.user b/NetTool/NetTool.vcxproj.user index bf30e1d..7ff49db 100644 --- a/NetTool/NetTool.vcxproj.user +++ b/NetTool/NetTool.vcxproj.user @@ -1,7 +1,7 @@  - pathping baidu.com + whois baidu.com WindowsLocalDebugger \ No newline at end of file diff --git a/inc/libnet.h b/inc/libnet.h index a5e099c..ea09a26 100644 --- a/inc/libnet.h +++ b/inc/libnet.h @@ -198,6 +198,9 @@ int WINAPI pathping(int argc, char ** argv); __declspec(dllimport) int WINAPI ping(int argc, char ** argv); +__declspec(dllimport) +int WINAPI whois(int argc, char ** argv); + ////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/libnet.sln b/libnet.sln index bc18c11..0694d94 100644 --- a/libnet.sln +++ b/libnet.sln @@ -11,6 +11,9 @@ EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libnet", "libnet\libnet.vcxproj", "{77427D08-54F8-46F4-80E4-A147B852E1BF}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NetTool", "NetTool\NetTool.vcxproj", "{08366C20-CB1B-4B79-BE74-ED450AEFBCD3}" + ProjectSection(ProjectDependencies) = postProject + {77427D08-54F8-46F4-80E4-A147B852E1BF} = {77427D08-54F8-46F4-80E4-A147B852E1BF} + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/libnet/libnet.vcxproj b/libnet/libnet.vcxproj index 03302c8..8f8f603 100644 --- a/libnet/libnet.vcxproj +++ b/libnet/libnet.vcxproj @@ -246,6 +246,7 @@ + diff --git a/libnet/libnet.vcxproj.filters b/libnet/libnet.vcxproj.filters index 3691b9b..27a1832 100644 --- a/libnet/libnet.vcxproj.filters +++ b/libnet/libnet.vcxproj.filters @@ -194,6 +194,9 @@ 源文件 + + 源文件 + diff --git a/libnet/whois.cpp b/libnet/whois.cpp new file mode 100644 index 0000000..fc86213 --- /dev/null +++ b/libnet/whois.cpp @@ -0,0 +1,189 @@ +/* + * Copyright (c) 1980, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * 8/1/97 - Ted Felix + * Ported to Win32 from 4.4-BSDLITE2 from wcarchive. + * Added WSAStartup()/WSACleanup() and switched from the + * more convenient fdopen()/fprintf() to send()/recv(). + */ + +//#include +//#define WIN32_NO_STATUS +#include +#define _INC_WINDOWS +#include +/* #include */ +/* #include */ +/* #include */ +#include +#include +#include "pch.h" +/* #include */ +/* #include */ +/* #include */ + +#pragma warning(disable:6011) //ȡ NULL ָ롰XXX + +static char NICHOST[] = "whois.internic.net"; +char *host = NULL; +int optset = 0; +static void usage(); +static void cleanup(int iExitCode); + +void getwhoisserver(int argc, char **argv) +{ + int i = 1; + + while(i < argc) + { + if (!strcmp(argv[i], "-h")) + { + if (i + 2 < argc) + { + host = argv[i +1]; + optset = i + 1; + } + else + { + optset = argc; + } + return; + } + i++; + } + host = NICHOST; + optset = 1; +} + +EXTERN_C +__declspec(dllexport) +int WINAPI whois(int argc, char **argv) +{ + char ch; + struct sockaddr_in sin; + struct hostent *hp; + struct servent *sp; + SOCKET s; + + WORD wVersionRequested; + WSADATA wsaData; + int err; + + getwhoisserver(argc, argv); + argc -= optset; + argv += optset; + + if (!host || !argc) + usage(); + + /* Start winsock */ + wVersionRequested = MAKEWORD( 1, 1 ); + err = WSAStartup( wVersionRequested, &wsaData ); + if ( err != 0 ) + { + /* Tell the user that we couldn't find a usable */ + /* WinSock DLL. */ + perror("whois: WSAStartup failed"); + cleanup(1); + } + + hp = gethostbyname(host); + if (hp == NULL) { + (void)fprintf(stderr, "whois: %s: ", host); + cleanup(1); + } + host = hp->h_name; + + s = socket(hp->h_addrtype, SOCK_STREAM, 0); + if (s == INVALID_SOCKET) { + perror("whois: socket"); + cleanup(1); + } + + memset(/*(caddr_t)*/&sin, 0, sizeof(sin)); + sin.sin_family = hp->h_addrtype; + if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { + perror("whois: bind"); + cleanup(1); + } + + memcpy((char *)&sin.sin_addr, hp->h_addr, hp->h_length); + sp = getservbyname("nicname", "tcp"); + if (sp == NULL) { + (void)fprintf(stderr, "whois: nicname/tcp: unknown service\n"); + cleanup(1); + } + + sin.sin_port = sp->s_port; + + /* have network connection; identify the host connected with */ + (void)printf("[%s]\n", hp->h_name); + + if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { + fprintf(stderr, "whois: connect error = %d\n", WSAGetLastError()); + cleanup(1); + } + + /* WinSock doesn't allow using a socket as a file descriptor. */ + /* Have to use send() and recv(). whois will drop connection. */ + + /* For each request */ + while (argc-- > 1) + { + /* Send the request */ + send(s, *argv, (int)strlen(*argv), 0); + send(s, " ", 1, 0); + argv++; + } + /* Send the last request */ + send(s, *argv, (int)strlen(*argv), 0); + send(s, "\r\n", 2, 0); + + /* Receive anything and print it */ + while (recv(s, &ch, 1, 0) == 1) + putchar(ch); + + cleanup(0); + return 0; +} + +static void usage() +{ + (void)fprintf(stderr, "usage: whois [-h hostname] name ...\n"); + cleanup(1); +} + +static void cleanup(int iExitCode) +{ + WSACleanup(); + exit(iExitCode); +}