Skip to content

Commit

Permalink
添加reactos的whois.
Browse files Browse the repository at this point in the history
  • Loading branch information
kouzhudong committed Jan 31, 2024
1 parent a79dd8a commit b35ee2d
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 1 deletion.
4 changes: 4 additions & 0 deletions NetTool/NetTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
2 changes: 1 addition & 1 deletion NetTool/NetTool.vcxproj.user
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerCommandArguments>pathping baidu.com</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>whois baidu.com</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>
3 changes: 3 additions & 0 deletions inc/libnet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);


//////////////////////////////////////////////////////////////////////////////////////////////////

Expand Down
3 changes: 3 additions & 0 deletions libnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions libnet/libnet.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@
<ClCompile Include="udp.cpp" />
<ClCompile Include="WebBrowser.cpp" />
<ClCompile Include="wfp.cpp" />
<ClCompile Include="whois.cpp" />
<ClCompile Include="Winhttp.cpp" />
<ClCompile Include="WinINet.cpp" />
<ClCompile Include="Wlan.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions libnet/libnet.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@
<ClCompile Include="resolve.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="whois.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Text Include="readme.txt" />
Expand Down
189 changes: 189 additions & 0 deletions libnet/whois.cpp
Original file line number Diff line number Diff line change
@@ -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 <tfelix@fred.net>
* Ported to Win32 from 4.4-BSDLITE2 from wcarchive.
* Added WSAStartup()/WSACleanup() and switched from the
* more convenient fdopen()/fprintf() to send()/recv().
*/

//#include <sys/types.h>
//#define WIN32_NO_STATUS
#include <windef.h>
#define _INC_WINDOWS
#include <winsock2.h>
/* #include <sys/socket.h> */
/* #include <netinet/in.h> */
/* #include <netdb.h> */
#include <stdio.h>
#include <stdlib.h>
#include "pch.h"
/* #include <various.h> */
/* #include <getopt.h> */
/* #include <io.h> */

#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);
}

0 comments on commit b35ee2d

Please sign in to comment.