Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NetTool/NetTool.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="finger.cpp" />
<ClCompile Include="common_utils.cpp" />
<ClCompile Include="IPArp.Cpp" />
<ClCompile Include="IPConfig.cpp" />
<ClCompile Include="IPRoute.Cpp" />
Expand All @@ -169,6 +170,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="finger.h" />
<ClInclude Include="common_utils.h" />
<ClInclude Include="IPArp.h" />
<ClInclude Include="IPConfig.h" />
<ClInclude Include="iphdr.h" />
Expand Down
13 changes: 7 additions & 6 deletions NetTool/Ping.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ping.h"
#include "iphdr.h"
#include "common_utils.h"


int gAddressFamily = AF_UNSPEC, // Address family to use
Expand All @@ -12,7 +13,7 @@ recvbuf[MAX_RECV_BUF_LEN]; // For received packets
int recvbuflen = MAX_RECV_BUF_LEN; // Length of received packets.


#pragma warning(disable : 28159) //����ʹ�á�GetTickCount64�������ǡ�GetTickCount��
#pragma warning(disable : 28159) //����ʹ�á�GetTickCount64�������ǡ�GetTickCount��


//////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -505,7 +506,6 @@ int ping(int argc, char ** argv)
\Windows-classic-samples\Samples\Win7Samples\netds\winsock\ping\Ping.cpp
*/
{
WSADATA wsd;
WSAOVERLAPPED recvol;
SOCKET s = INVALID_SOCKET;
char * icmpbuf = NULL;
Expand All @@ -524,9 +524,10 @@ int ping(int argc, char ** argv)
goto EXIT;
}

// Load Winsock
if ((rc = WSAStartup(MAKEWORD(2, 2), &wsd)) != 0) {
printf("WSAStartup() failed: %d\n", rc);
// Load Winsock using RAII wrapper
WinsockInitializer winsock(MAKEWORD(2, 2));
if (!winsock.IsInitialized()) {
printf("WSAStartup() failed: %d\n", winsock.GetErrorCode());
status = -1;
goto EXIT;
}
Expand Down Expand Up @@ -687,7 +688,7 @@ int ping(int argc, char ** argv)
if (icmpbuf)
HeapFree(GetProcessHeap(), 0, icmpbuf);

WSACleanup();
// Winsock cleanup is handled automatically by WinsockInitializer destructor

EXIT:
return status;
Expand Down
118 changes: 118 additions & 0 deletions NetTool/common_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include "common_utils.h"
#include <errno.h>

// Global variables for getopt
int opterr = 1; // if error message should be printed
int optind = 1; // index into parent argv vector
int optopt; // character checked for validity
const char* optarg; // argument associated with option

#pragma warning(disable : 28182)

int getopt(int nargc, char* const* nargv, const char* ostr)
{
static const char* place = EMSG; // option letter processing
register char* oli; // option letter list index
char* p;

if (!*place) { // update scanning pointer
if (optind >= nargc || *(place = nargv[optind]) != '-') {
place = EMSG;
return (EOF);
}
if (place[1] && *++place == '-') { // found "--"
++optind;
place = EMSG;
return (EOF);
}
} // option letter okay?
if ((optopt = (int)*place++) == (int)':' || !(oli = (char*)strchr(ostr, optopt))) {
/*
* if the user didn't specify '-' as an option, assume it means EOF.
*/
if (optopt == (int)'-')
return (EOF);
if (!*place)
++optind;
if (opterr) {
if (!(p = strrchr(*nargv, '/')))
p = *nargv;
else
++p;
(void)fprintf(stderr, "%s: illegal option -- %c\n", p, optopt);
}
return (BADCH);
}
if (*++oli != ':') { // don't need argument
optarg = NULL;
if (!*place)
++optind;
}
else { // need an argument
if (*place) // no white space
optarg = place;
else if (nargc <= ++optind) { // no arg
place = EMSG;
if (!(p = strrchr(*nargv, '/')))
p = *nargv;
else
++p;
if (opterr)
(void)fprintf(stderr, "%s: option requires an argument -- %c\n", p, optopt);
return (BADCH);
}
else // white space
optarg = nargv[optind];

place = EMSG;
++optind;
}

return (optopt); // dump back option letter
}

void verr(int eval, const char* fmt, va_list ap)
{
int sverrno;
extern char* __progname;

sverrno = errno;
(void)fprintf(stderr, "%s: ", __progname);
if (fmt != NULL) {
(void)vfprintf(stderr, fmt, ap);
(void)fprintf(stderr, ": ");
}

#pragma warning(push)
#pragma warning(disable : 4996)
(void)fprintf(stderr, "%s\n", strerror(sverrno));
#pragma warning(pop)

exit(eval);
}

void err(int eval, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verr(eval, fmt, ap);
va_end(ap);
}

void verrx(int eval, const char* fmt, va_list ap)
{
extern char* __progname;
(void)fprintf(stderr, "%s: ", __progname);
if (fmt != NULL)
(void)vfprintf(stderr, fmt, ap);
(void)fprintf(stderr, "\n");
exit(eval);
}

void errx(int eval, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verrx(eval, fmt, ap);
va_end(ap);
}
52 changes: 52 additions & 0 deletions NetTool/common_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include "pch.h"

// RAII wrapper for Winsock initialization
class WinsockInitializer {
private:
BOOL initialized;
int errorCode;

public:
WinsockInitializer(WORD versionRequested = MAKEWORD(2, 2)) : initialized(FALSE), errorCode(0) {
WSADATA wsaData;
errorCode = WSAStartup(versionRequested, &wsaData);
initialized = (errorCode == 0);
}

~WinsockInitializer() {
if (initialized) {
WSACleanup();
}
}

BOOL IsInitialized() const {
return initialized;
}

int GetErrorCode() const {
return errorCode;
}

// Delete copy constructor and assignment operator
WinsockInitializer(const WinsockInitializer&) = delete;
WinsockInitializer& operator=(const WinsockInitializer&) = delete;
};

// Common getopt implementation (from BSD)
// Global variables for getopt
extern int opterr, optind, optopt;
extern const char* optarg;

#define BADCH (int)'?'
#define EMSG ""

// getopt function declaration
int getopt(int nargc, char* const* nargv, const char* ostr);

// Error handling functions (from BSD)
void verr(int eval, const char* fmt, va_list ap);
void err(int eval, const char* fmt, ...);
void verrx(int eval, const char* fmt, va_list ap);
void errx(int eval, const char* fmt, ...);
Loading