Skip to content

Commit ab2393c

Browse files
committed
[IFMON] Implement the 'add dns' command in the 'interface ip' context
The index argument is not supported yet.
1 parent aaf231f commit ab2393c

3 files changed

Lines changed: 206 additions & 4 deletions

File tree

dll/win32/ifmon/ip.c

Lines changed: 183 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#define REGISTER_BOTH 3
3333

3434
static FN_HANDLE_CMD IpAddAddress;
35+
static FN_HANDLE_CMD IpAddDns;
3536
static FN_HANDLE_CMD IpSetAddress;
3637
static FN_HANDLE_CMD IpSetDns;
3738
static FN_HANDLE_CMD IpShowAddresses;
@@ -43,7 +44,8 @@ static
4344
CMD_ENTRY
4445
IpAddCommands[] =
4546
{
46-
{L"address", IpAddAddress, IDS_HLP_IP_ADD_ADDRESS, IDS_HLP_IP_ADD_ADDRESS_EX, 0}
47+
{L"address", IpAddAddress, IDS_HLP_IP_ADD_ADDRESS, IDS_HLP_IP_ADD_ADDRESS_EX, 0},
48+
{L"dns", IpAddDns, IDS_HLP_IP_ADD_DNS, IDS_HLP_IP_ADD_DNS_EX, 0}
4749
};
4850

4951
static
@@ -709,6 +711,186 @@ IpAddAddress(
709711
}
710712

711713

714+
static
715+
DWORD
716+
WINAPI
717+
IpAddDns(
718+
LPCWSTR pwszMachine,
719+
LPWSTR *argv,
720+
DWORD dwCurrentIndex,
721+
DWORD dwArgCount,
722+
DWORD dwFlags,
723+
LPCVOID pvData,
724+
BOOL *pbDone)
725+
{
726+
TAG_TYPE pttTags[] = {{L"name", NS_REQ_ZERO, FALSE},
727+
{L"addr", NS_REQ_ZERO, FALSE},
728+
{L"index", NS_REQ_ZERO, FALSE}};
729+
GUID InterfaceGUID;
730+
PDWORD pdwTagType = NULL;
731+
DWORD i;
732+
BOOL bHaveName = FALSE, bHaveAddress = FALSE/*, bHaveIndex = FALSE*/;
733+
IN_ADDR Address;
734+
PWSTR pszName = NULL, pszAddress = NULL;
735+
PWSTR pszNewParameters = NULL;
736+
DWORD dwIndex, dwLength;
737+
PCWSTR Term;
738+
PTCPIP_PROPERTIES pProperties = NULL;
739+
TCPIP_PROPERTIES NewProperties;
740+
HRESULT hr;
741+
NTSTATUS Status;
742+
DWORD dwError = ERROR_SUCCESS;
743+
744+
DPRINT1("IpAddDns()\n");
745+
746+
pdwTagType = HeapAlloc(GetProcessHeap(),
747+
0,
748+
(dwArgCount - dwCurrentIndex) * sizeof(DWORD));
749+
if (pdwTagType == NULL)
750+
{
751+
return ERROR_NOT_ENOUGH_MEMORY;
752+
}
753+
754+
dwError = MatchTagsInCmdLine(hDllInstance,
755+
argv,
756+
dwCurrentIndex,
757+
dwArgCount,
758+
pttTags,
759+
ARRAYSIZE(pttTags),
760+
pdwTagType);
761+
if (dwError != ERROR_SUCCESS)
762+
{
763+
DPRINT1("MatchTagsInCmdLine() failed (Error %lu)\n", dwError);
764+
HeapFree(GetProcessHeap(), 0, pdwTagType);
765+
return dwError;
766+
}
767+
768+
for (i = 0; i < (dwArgCount - dwCurrentIndex); i++)
769+
{
770+
DPRINT1("Tag %lu: %lu\n", i, pdwTagType[i]);
771+
772+
switch (pdwTagType[i])
773+
{
774+
case 0: /* name */
775+
DPRINT1("Tag: name (%S)\n", argv[i + dwCurrentIndex]);
776+
dwError = NhGetGuidFromInterfaceName(argv[i + dwCurrentIndex],
777+
&InterfaceGUID,
778+
0, 0);
779+
if (dwError != ERROR_SUCCESS)
780+
{
781+
DPRINT1("NhGetGuidFromInterfaceName() failed (Error %lu)\n", dwError);
782+
PrintMessageFromModule(hDllInstance,
783+
IDS_ERROR_INVALID_INTERFACE,
784+
argv[i + dwCurrentIndex]);
785+
dwError = ERROR_SUPPRESS_OUTPUT;
786+
break;
787+
}
788+
pszName = argv[i + dwCurrentIndex];
789+
DPRINT1("Interface: {%08lx-%04hx-%04hx-%02x%02x-%02x%02x%02x%02x%02x%02x}\n",
790+
InterfaceGUID.Data1, InterfaceGUID.Data2, InterfaceGUID.Data3, InterfaceGUID.Data4[0], InterfaceGUID.Data4[1],
791+
InterfaceGUID.Data4[2], InterfaceGUID.Data4[3], InterfaceGUID.Data4[4], InterfaceGUID.Data4[5], InterfaceGUID.Data4[6], InterfaceGUID.Data4[7]);
792+
bHaveName = TRUE;
793+
break;
794+
795+
case 1: /* addr */
796+
DPRINT1("Tag: addr (%S)\n", argv[i + dwCurrentIndex]);
797+
Status = RtlIpv4StringToAddressW(argv[i + dwCurrentIndex],
798+
TRUE,
799+
&Term,
800+
&Address);
801+
if (Status != 0 /*STATUS_SUCCESS*/)
802+
{
803+
DPRINT1("RtlIpv4StringToAddressW() failed (Status 0x%08lx)\n", Status);
804+
PrintMessageFromModule(hDllInstance,
805+
IDS_ERROR_BAD_VALUE,
806+
argv[i + dwCurrentIndex],
807+
pttTags[pdwTagType[i]].pwszTag);
808+
dwError = ERROR_SUPPRESS_OUTPUT;
809+
break;
810+
}
811+
DPRINT1("IP Address: %u.%u.%u.%u\n",
812+
Address.S_un.S_un_b.s_b1, Address.S_un.S_un_b.s_b2, Address.S_un.S_un_b.s_b3, Address.S_un.S_un_b.s_b4);
813+
pszAddress = argv[i + dwCurrentIndex];
814+
DPRINT1("IP Address: %S\n", pszAddress);
815+
bHaveAddress = TRUE;
816+
break;
817+
818+
case 2: /* index */
819+
DPRINT1("Tag: index (%S)\n", argv[i + dwCurrentIndex]);
820+
dwIndex = wcstoul(argv[i + dwCurrentIndex],
821+
(wchar_t**)&Term,
822+
10);
823+
if ((dwIndex == 0) || (dwIndex > 999))
824+
{
825+
dwError = ERROR_INVALID_PARAMETER;
826+
break;
827+
}
828+
DPRINT1("Index: %lu\n", dwIndex);
829+
// bHaveIndex = TRUE;
830+
break;
831+
832+
default:
833+
DPRINT1("Unknown tag type %lu\n", pdwTagType[i]);
834+
break;
835+
}
836+
}
837+
838+
if (pdwTagType)
839+
HeapFree(GetProcessHeap(), 0, pdwTagType);
840+
841+
if (dwError != ERROR_SUCCESS)
842+
return dwError;
843+
844+
/* Check parameters */
845+
846+
/* The interface name is mandatory */
847+
if (bHaveName == FALSE)
848+
return ERROR_INVALID_SYNTAX;
849+
850+
/* The address is mandatory */
851+
if (!bHaveAddress)
852+
return ERROR_INVALID_SYNTAX;
853+
854+
hr = GetInterfaceProperties(&InterfaceGUID, &pProperties);
855+
if (FAILED(hr))
856+
{
857+
PrintMessageFromModule(hDllInstance,
858+
IDS_ERROR_GET_PROPERTIES,
859+
pszName);
860+
return ERROR_SUPPRESS_OUTPUT;
861+
}
862+
863+
dwLength = wcslen(pProperties->pszParameters) + wcslen(pszAddress) + 2;
864+
865+
pszNewParameters = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwLength * sizeof(WCHAR));
866+
if (pszNewParameters == NULL)
867+
{
868+
dwError = ERROR_NOT_ENOUGH_MEMORY;
869+
goto done;
870+
}
871+
872+
wcscpy(pszNewParameters, pProperties->pszParameters);
873+
AppendParameterValue(pszNewParameters, L"DNS", pszAddress);
874+
875+
NewProperties.dwDhcp = 0;
876+
NewProperties.pszIpAddress = pProperties->pszIpAddress;
877+
NewProperties.pszSubnetMask = pProperties->pszSubnetMask;
878+
NewProperties.pszParameters = pszNewParameters;
879+
880+
SetInterfaceProperties(&InterfaceGUID, &NewProperties);
881+
882+
done:
883+
if (pszNewParameters)
884+
HeapFree(GetProcessHeap(), 0, pszNewParameters);
885+
886+
CoTaskMemFree(pProperties);
887+
pProperties = NULL;
888+
889+
DPRINT("IpAddDns() done (Error %lu)\n", dwError);
890+
return dwError;
891+
}
892+
893+
712894
static
713895
DWORD
714896
WINAPI

dll/win32/ifmon/lang/en-US.rc

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,25 @@ Examples:\n\n\
2424
The first command adds a static IP address of 10.0.0.2 with a subnet\n\
2525
mask of 255.0.0.0 to the Local Area Connection interface. The second\n\
2626
command adds the IP address of 10.0.0.3 as a second default gateway\n\
27-
for this interface with a gateway metric of 2.\n\n"
27+
for this interface with a gateway metric of 2.\n"
28+
29+
IDS_HLP_IP_ADD_DNS "Adds a static DNS server address.\n"
30+
IDS_HLP_IP_ADD_DNS_EX "\nUsage: %1!s! [name=]<string> [addr=]<IP address> [[index=]<integer>]\n\n\
31+
Parameters:\n\n\
32+
Tag Value\n\
33+
name - The name of the interface where DNS servers are added.\n\
34+
addr - The IP address for the DNS server you are adding.\n\
35+
index - Specifies the index (preference) for the specified\n\
36+
DNS server address.\n\n\
37+
Remarks: Adds a new DNS server IP address to the statically-configured list.\n\
38+
By default, the DNS server is added to the end of the list. If an\n\
39+
index is specified, the DNS server will be placed in that position\n\
40+
in the list, with other servers being moved down to make room.\n\
41+
If DNS servers were previously obtained through DHCP, the new\n\
42+
address will replace the old list.\n\n\
43+
Examples:\n\n\
44+
%1!s! ""Local Area Connection"" 10.0.0.1\n\
45+
%1!s! ""Local Area Connection"" 10.0.0.3 index=2\n"
2846

2947
IDS_HLP_IP_SET "Sets configuration information.\n"
3048
IDS_HLP_IP_SET_ADDRESS "Sets the IP address or default gateway to the specified interface.\n"
@@ -54,7 +72,7 @@ Remarks: Used to change the IP address configuration mode from either DHCP to\n\
5472
interface with static IP address or adds default gateways.\n\n\
5573
Examples:\n\n\
5674
%1!s! name=""Local Area Connection"" source=dhcp\n\
57-
%1!s! local static 10.0.0.9 255.0.0.0 10.0.0.1 1\n\n"
75+
%1!s! local static 10.0.0.9 255.0.0.0 10.0.0.1 1\n"
5876

5977
IDS_HLP_IP_SET_DNS "Sets DNS server mode and addresses.\n"
6078
IDS_HLP_IP_SET_DNS_EX "\nUsage: %1!s! [name=]<string> [source=]dhcp|static [addr=]<IP address>|none\n\
@@ -81,7 +99,7 @@ Remarks: Sets DNS server configuration to either DHCP or static mode. Only\n\
8199
specified interface.\n\n\
82100
Examples:\n\n\
83101
%1!s! name=""Local Area Connection"" source=dhcp\n\
84-
%1!s! ""Local Area Connection"" static 10.0.0.1 primary\n\n"
102+
%1!s! ""Local Area Connection"" static 10.0.0.1 primary\n"
85103

86104
IDS_HLP_IP_SHOW "Displays IP information.\n"
87105
IDS_HLP_ADDRESSES "Displays IP address configuration.\n"

dll/win32/ifmon/resource.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#define IDS_HLP_IP_ADD 200
77
#define IDS_HLP_IP_ADD_ADDRESS 201
88
#define IDS_HLP_IP_ADD_ADDRESS_EX 202
9+
#define IDS_HLP_IP_ADD_DNS 203
10+
#define IDS_HLP_IP_ADD_DNS_EX 204
911

1012
#define IDS_HLP_IP_SET 205
1113
#define IDS_HLP_IP_SET_ADDRESS 206

0 commit comments

Comments
 (0)