mirror of
https://github.com/reactos/reactos.git
synced 2024-12-28 10:04:49 +00:00
added more functionality to arp
- display correct interface index - delete all hosts with -d * - display informative error messages - better argument control - clean up formatting svn path=/trunk/; revision=18057
This commit is contained in:
parent
4f9b4be8fb
commit
b1c57949ff
3 changed files with 348 additions and 322 deletions
|
@ -1,3 +1,22 @@
|
||||||
|
/*
|
||||||
|
* ReactOS Win32 Applications
|
||||||
|
* Copyright (C) 2005 ReactOS Team
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* COPYRIGHT: See COPYING in the top level directory
|
* COPYRIGHT: See COPYING in the top level directory
|
||||||
* PROJECT: ReactOS arp utility
|
* PROJECT: ReactOS arp utility
|
||||||
|
@ -9,7 +28,6 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -19,24 +37,99 @@
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <iphlpapi.h>
|
#include <iphlpapi.h>
|
||||||
|
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
|
#define UNICODE
|
||||||
|
#define _UNICODE
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Globals
|
* Globals
|
||||||
*/
|
*/
|
||||||
const char SEPERATOR = '-';
|
const char SEPERATOR = '-';
|
||||||
|
int _CRT_glob = 0; // stop * from listing dir files in arp -d *
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* function declerations
|
* function declerations
|
||||||
*/
|
*/
|
||||||
INT DisplayArpEntries(PTCHAR pszInetAddr, PTCHAR pszIfAddr);
|
DWORD DoFormatMessage(DWORD ErrorCode);
|
||||||
INT PrintEntries(PMIB_IPNETROW pIpAddRow);
|
INT PrintEntries(PMIB_IPNETROW pIpAddRow);
|
||||||
|
INT DisplayArpEntries(PTCHAR pszInetAddr, PTCHAR pszIfAddr);
|
||||||
INT Addhost(PTCHAR pszInetAddr, PTCHAR pszEthAddr, PTCHAR pszIfAddr);
|
INT Addhost(PTCHAR pszInetAddr, PTCHAR pszEthAddr, PTCHAR pszIfAddr);
|
||||||
INT Deletehost(PTCHAR pszInetAddr, PTCHAR pszIfAddr);
|
INT Deletehost(PTCHAR pszInetAddr, PTCHAR pszIfAddr);
|
||||||
VOID Usage(VOID);
|
VOID Usage(VOID);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* convert error code into meaningful message
|
||||||
|
*/
|
||||||
|
DWORD DoFormatMessage(DWORD ErrorCode)
|
||||||
|
{
|
||||||
|
LPVOID lpMsgBuf;
|
||||||
|
DWORD RetVal;
|
||||||
|
/* double brackets to silence the assignment warning message */
|
||||||
|
if ((RetVal = FormatMessage(
|
||||||
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||||
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||||
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
|
NULL,
|
||||||
|
ErrorCode,
|
||||||
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
|
||||||
|
(LPTSTR) &lpMsgBuf,
|
||||||
|
0,
|
||||||
|
NULL ))) {
|
||||||
|
_tprintf(_T("%s"), (LPTSTR)lpMsgBuf);
|
||||||
|
|
||||||
|
LocalFree(lpMsgBuf);
|
||||||
|
/* return number of TCHAR's stored in output buffer
|
||||||
|
* excluding '\0' - as FormatMessage does*/
|
||||||
|
return RetVal;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* Takes an ARP entry and prints the IP address,
|
||||||
|
* the MAC address and the entry type to screen
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
INT PrintEntries(PMIB_IPNETROW pIpAddRow)
|
||||||
|
{
|
||||||
|
IN_ADDR inaddr;
|
||||||
|
TCHAR cMacAddr[20];
|
||||||
|
|
||||||
|
/* print IP addresses */
|
||||||
|
inaddr.S_un.S_addr = pIpAddRow->dwAddr;
|
||||||
|
_tprintf(_T(" %-22s"), inet_ntoa(inaddr));
|
||||||
|
|
||||||
|
/* print MAC address */
|
||||||
|
_stprintf(cMacAddr, _T("%02x-%02x-%02x-%02x-%02x-%02x"),
|
||||||
|
pIpAddRow->bPhysAddr[0],
|
||||||
|
pIpAddRow->bPhysAddr[1],
|
||||||
|
pIpAddRow->bPhysAddr[2],
|
||||||
|
pIpAddRow->bPhysAddr[3],
|
||||||
|
pIpAddRow->bPhysAddr[4],
|
||||||
|
pIpAddRow->bPhysAddr[5]);
|
||||||
|
_tprintf(_T("%-22s"), cMacAddr);
|
||||||
|
|
||||||
|
/* print cache type */
|
||||||
|
switch (pIpAddRow->dwType)
|
||||||
|
{
|
||||||
|
case MIB_IPNET_TYPE_DYNAMIC : _tprintf(_T("dynamic\n"));
|
||||||
|
break;
|
||||||
|
case MIB_IPNET_TYPE_STATIC : _tprintf(_T("static\n"));
|
||||||
|
break;
|
||||||
|
case MIB_IPNET_TYPE_INVALID : _tprintf(_T("invalid\n"));
|
||||||
|
break;
|
||||||
|
case MIB_IPNET_TYPE_OTHER : _tprintf(_T("other\n"));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
|
@ -68,19 +161,21 @@ INT DisplayArpEntries(PTCHAR pszInetAddr, PTCHAR pszIfAddr)
|
||||||
ZeroMemory(pIpNetTable, sizeof(*pIpNetTable));
|
ZeroMemory(pIpNetTable, sizeof(*pIpNetTable));
|
||||||
|
|
||||||
/* get Arp address table */
|
/* get Arp address table */
|
||||||
if (pIpNetTable != NULL) {
|
if (pIpNetTable != NULL)
|
||||||
GetIpNetTable(pIpNetTable, &ulSize, TRUE);
|
GetIpNetTable(pIpNetTable, &ulSize, TRUE);
|
||||||
} else {
|
else
|
||||||
|
{
|
||||||
_tprintf(_T("failed to allocate memory for GetIpNetTable\n"));
|
_tprintf(_T("failed to allocate memory for GetIpNetTable\n"));
|
||||||
free(pIpNetTable);
|
free(pIpNetTable);
|
||||||
return -1;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* check there are entries in the table */
|
/* check there are entries in the table */
|
||||||
if (pIpNetTable->dwNumEntries == 0) {
|
if (pIpNetTable->dwNumEntries == 0)
|
||||||
|
{
|
||||||
_tprintf(_T("No ARP entires found\n"));
|
_tprintf(_T("No ARP entires found\n"));
|
||||||
free(pIpNetTable);
|
free(pIpNetTable);
|
||||||
return -1;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -95,15 +190,18 @@ INT DisplayArpEntries(PTCHAR pszInetAddr, PTCHAR pszIfAddr)
|
||||||
pIpAddrTable = (MIB_IPADDRTABLE *) malloc(dwSize);
|
pIpAddrTable = (MIB_IPADDRTABLE *) malloc(dwSize);
|
||||||
//ZeroMemory(pIpAddrTable, sizeof(*pIpAddrTable));
|
//ZeroMemory(pIpAddrTable, sizeof(*pIpAddrTable));
|
||||||
|
|
||||||
if ((iRet = GetIpAddrTable(pIpAddrTable, &dwSize, TRUE)) != NO_ERROR) { // NO_ERROR = 0
|
if ((iRet = GetIpAddrTable(pIpAddrTable, &dwSize, TRUE)) != NO_ERROR)
|
||||||
|
{
|
||||||
_tprintf(_T("GetIpAddrTable failed: %d\n"), iRet);
|
_tprintf(_T("GetIpAddrTable failed: %d\n"), iRet);
|
||||||
_tprintf(_T("error: %d\n"), WSAGetLastError());
|
_tprintf(_T("error: %d\n"), WSAGetLastError());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
for (k=0; k < pIpAddrTable->dwNumEntries; k++) {
|
for (k=0; k < pIpAddrTable->dwNumEntries; k++)
|
||||||
if (pIpNetTable->table[0].dwIndex == pIpAddrTable->table[k].dwIndex) {
|
{
|
||||||
//printf("printing pIpAddrTable->table[?].dwIndex = %lx\n", pIpNetTable->table[k].dwIndex);
|
if (pIpNetTable->table[0].dwIndex == pIpAddrTable->table[k].dwIndex)
|
||||||
|
{
|
||||||
|
//printf("debug print: pIpAddrTable->table[?].dwIndex = %lx\n", pIpNetTable->table[k].dwIndex);
|
||||||
inaddr2.s_addr = pIpAddrTable->table[k].dwAddr;
|
inaddr2.s_addr = pIpAddrTable->table[k].dwAddr;
|
||||||
pszIpAddr = inet_ntoa(inaddr2);
|
pszIpAddr = inet_ntoa(inaddr2);
|
||||||
strcpy(szIntIpAddr, pszIpAddr);
|
strcpy(szIntIpAddr, pszIpAddr);
|
||||||
|
@ -116,74 +214,32 @@ INT DisplayArpEntries(PTCHAR pszInetAddr, PTCHAR pszIfAddr)
|
||||||
_tprintf(_T(" Internet Address Physical Address Type\n"));
|
_tprintf(_T(" Internet Address Physical Address Type\n"));
|
||||||
|
|
||||||
/* go through all ARP entries */
|
/* go through all ARP entries */
|
||||||
for (i=0; i < pIpNetTable->dwNumEntries; i++) {
|
for (i=0; i < pIpNetTable->dwNumEntries; i++)
|
||||||
|
{
|
||||||
|
|
||||||
/* if the user has supplied their own internet addesss *
|
/* if the user has supplied their own internet addesss *
|
||||||
* only print the arp entry which matches that */
|
* only print the arp entry which matches that */
|
||||||
if (pszInetAddr) {
|
if (pszInetAddr)
|
||||||
|
{
|
||||||
inaddr.S_un.S_addr = pIpNetTable->table[i].dwAddr;
|
inaddr.S_un.S_addr = pIpNetTable->table[i].dwAddr;
|
||||||
pszIpAddr = inet_ntoa(inaddr);
|
pszIpAddr = inet_ntoa(inaddr);
|
||||||
|
|
||||||
/* check if it matches, print it */
|
/* check if it matches, print it */
|
||||||
if (strcmp(pszIpAddr, pszInetAddr) == 0) {
|
if (strcmp(pszIpAddr, pszInetAddr) == 0)
|
||||||
PrintEntries(&pIpNetTable->table[i]);
|
PrintEntries(&pIpNetTable->table[i]);
|
||||||
}
|
}
|
||||||
} else {
|
else
|
||||||
/* if an address is not supplied, print all entries */
|
/* if an address is not supplied, print all entries */
|
||||||
PrintEntries(&pIpNetTable->table[i]);
|
PrintEntries(&pIpNetTable->table[i]);
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(pIpNetTable);
|
free(pIpNetTable);
|
||||||
free(pIpAddrTable);
|
free(pIpAddrTable);
|
||||||
|
|
||||||
return 0;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
*
|
|
||||||
* Takes an ARP entry and prints the IP address,
|
|
||||||
* the MAC address and the entry type to screen
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
INT PrintEntries(PMIB_IPNETROW pIpAddRow)
|
|
||||||
{
|
|
||||||
IN_ADDR inaddr;
|
|
||||||
TCHAR cMacAddr[20];
|
|
||||||
|
|
||||||
/* print IP addresses */
|
|
||||||
inaddr.S_un.S_addr = pIpAddRow->dwAddr;
|
|
||||||
_tprintf(_T(" %-22s"), inet_ntoa(inaddr)); //error checking
|
|
||||||
|
|
||||||
/* print MAC address */
|
|
||||||
_stprintf(cMacAddr, _T("%02x-%02x-%02x-%02x-%02x-%02x"),
|
|
||||||
pIpAddRow->bPhysAddr[0],
|
|
||||||
pIpAddRow->bPhysAddr[1],
|
|
||||||
pIpAddRow->bPhysAddr[2],
|
|
||||||
pIpAddRow->bPhysAddr[3],
|
|
||||||
pIpAddRow->bPhysAddr[4],
|
|
||||||
pIpAddRow->bPhysAddr[5]);
|
|
||||||
_tprintf(_T("%-22s"), cMacAddr);
|
|
||||||
|
|
||||||
/* print cache type */
|
|
||||||
switch (pIpAddRow->dwType) {
|
|
||||||
case MIB_IPNET_TYPE_DYNAMIC : _tprintf(_T("dynamic\n"));
|
|
||||||
break;
|
|
||||||
case MIB_IPNET_TYPE_STATIC : _tprintf(_T("static\n"));
|
|
||||||
break;
|
|
||||||
case MIB_IPNET_TYPE_INVALID : _tprintf(_T("invalid\n"));
|
|
||||||
break;
|
|
||||||
case MIB_IPNET_TYPE_OTHER : _tprintf(_T("other\n"));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
* Takes an internet address, a MAC address and an optional interface
|
* Takes an internet address, a MAC address and an optional interface
|
||||||
|
@ -196,63 +252,90 @@ INT Addhost(PTCHAR pszInetAddr, PTCHAR pszEthAddr, PTCHAR pszIfAddr)
|
||||||
{
|
{
|
||||||
PMIB_IPNETROW pAddHost;
|
PMIB_IPNETROW pAddHost;
|
||||||
PMIB_IPADDRTABLE pIpAddrTable;
|
PMIB_IPADDRTABLE pIpAddrTable;
|
||||||
|
PMIB_IPNETTABLE pIpNetTable;
|
||||||
DWORD dwIpAddr;
|
DWORD dwIpAddr;
|
||||||
DWORD dwSize = 0;
|
DWORD dwSize = 0;
|
||||||
|
ULONG ulSize = 0;
|
||||||
INT iRet, i, val;
|
INT iRet, i, val;
|
||||||
TCHAR c;
|
TCHAR c;
|
||||||
|
|
||||||
/* error checking */
|
/* error checking */
|
||||||
|
|
||||||
/* check IP address */
|
/* check IP address */
|
||||||
if (pszInetAddr != NULL) {
|
if (pszInetAddr != NULL)
|
||||||
if ((dwIpAddr = inet_addr(pszInetAddr)) == INADDR_NONE) {
|
{
|
||||||
|
if ((dwIpAddr = inet_addr(pszInetAddr)) == INADDR_NONE)
|
||||||
|
{
|
||||||
_tprintf(_T("ARP: bad IP address: %s\n"), pszInetAddr);
|
_tprintf(_T("ARP: bad IP address: %s\n"), pszInetAddr);
|
||||||
return -1;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Usage();
|
Usage();
|
||||||
return -1;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* check MAC address */
|
/* check MAC address */
|
||||||
if (strlen(pszEthAddr) != 17) {
|
if (strlen(pszEthAddr) != 17)
|
||||||
|
{
|
||||||
_tprintf(_T("ARP: bad argument: %s\n"), pszEthAddr);
|
_tprintf(_T("ARP: bad argument: %s\n"), pszEthAddr);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
for (i=0; i<17; i++)
|
||||||
|
{
|
||||||
|
if (pszEthAddr[i] == SEPERATOR)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!isxdigit(pszEthAddr[i]))
|
||||||
|
{
|
||||||
|
_tprintf(_T("ARP: bad argument: %s\n"), pszEthAddr);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* We need the IpNetTable to get the adapter index */
|
||||||
|
/* Return required buffer size */
|
||||||
|
GetIpNetTable(NULL, &ulSize, 0);
|
||||||
|
/* allocate memory for ARP address table */
|
||||||
|
pIpNetTable = (PMIB_IPNETTABLE) malloc(ulSize * sizeof(BYTE));
|
||||||
|
ZeroMemory(pIpNetTable, sizeof(*pIpNetTable));
|
||||||
|
/* get Arp address table */
|
||||||
|
if (pIpNetTable != NULL)
|
||||||
|
GetIpNetTable(pIpNetTable, &ulSize, TRUE);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_tprintf(_T("failed to allocate memory for GetIpNetTable\n"));
|
||||||
|
free(pIpNetTable);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
for (i=0; i<17; i++) {
|
|
||||||
if (pszEthAddr[i] == SEPERATOR) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!isxdigit(pszEthAddr[i])) {
|
|
||||||
_tprintf(_T("ARP: bad argument: %s\n"), pszEthAddr);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* reserve memory on heap and zero */
|
/* reserve memory on heap and zero */
|
||||||
pAddHost = (MIB_IPNETROW *) malloc(sizeof(MIB_IPNETROW));
|
pAddHost = (MIB_IPNETROW *) malloc(sizeof(MIB_IPNETROW));
|
||||||
ZeroMemory(pAddHost, sizeof(MIB_IPNETROW));
|
ZeroMemory(pAddHost, sizeof(MIB_IPNETROW));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* set dwIndex field to the index of a local IP address to
|
/* set dwIndex field to the index of a local IP address to
|
||||||
* indicate the network on which the ARP entry applies */
|
* indicate the network on which the ARP entry applies */
|
||||||
if (pszIfAddr) {
|
if (pszIfAddr)
|
||||||
sscanf(pszIfAddr, "%lx", &pAddHost->dwIndex);
|
sscanf(pszIfAddr, "%lx", &pAddHost->dwIndex);
|
||||||
} else {
|
else
|
||||||
|
{
|
||||||
/* map the IP to the index */
|
/* map the IP to the index */
|
||||||
pIpAddrTable = (MIB_IPADDRTABLE *) malloc(dwSize);
|
pIpAddrTable = (MIB_IPADDRTABLE *) malloc(dwSize);
|
||||||
GetIpAddrTable(pIpAddrTable, &dwSize, 0);
|
GetIpAddrTable(pIpAddrTable, &dwSize, 0);
|
||||||
|
|
||||||
pIpAddrTable = (MIB_IPADDRTABLE *) malloc(dwSize);
|
pIpAddrTable = (MIB_IPADDRTABLE *) malloc(dwSize);
|
||||||
|
|
||||||
if ((iRet = GetIpAddrTable(pIpAddrTable, &dwSize, TRUE)) != NO_ERROR) { // NO_ERROR = 0
|
if ((iRet = GetIpAddrTable(pIpAddrTable, &dwSize, TRUE)) != NO_ERROR)
|
||||||
|
{
|
||||||
_tprintf(_T("GetIpAddrTable failed: %d\n"), iRet);
|
_tprintf(_T("GetIpAddrTable failed: %d\n"), iRet);
|
||||||
_tprintf(_T("error: %d\n"), WSAGetLastError());
|
_tprintf(_T("error: %d\n"), WSAGetLastError());
|
||||||
}
|
}
|
||||||
printf("printing pIpAddrTable->table[0].dwIndex = %lx\n", pIpAddrTable->table[0].dwIndex);
|
//printf("debug print: pIpNetTable->table[0].dwIndex = %lx\n", pIpNetTable->table[0].dwIndex);
|
||||||
pAddHost->dwIndex = 4;
|
/* needs testing. I get the correct index on my machine, but need others
|
||||||
|
* to test their card index. Any problems and we can use GetAdaptersInfo instead */
|
||||||
|
pAddHost->dwIndex = pIpNetTable->table[0].dwIndex;
|
||||||
|
|
||||||
free(pIpAddrTable);
|
free(pIpAddrTable);
|
||||||
}
|
}
|
||||||
|
@ -262,7 +345,8 @@ INT Addhost(PTCHAR pszInetAddr, PTCHAR pszEthAddr, PTCHAR pszIfAddr)
|
||||||
|
|
||||||
|
|
||||||
/* Encode bPhysAddr into correct byte array */
|
/* Encode bPhysAddr into correct byte array */
|
||||||
for (i=0; i<6; i++) {
|
for (i=0; i<6; i++)
|
||||||
|
{
|
||||||
val =0;
|
val =0;
|
||||||
c = toupper(pszEthAddr[i*3]);
|
c = toupper(pszEthAddr[i*3]);
|
||||||
c = c - (isdigit(c) ? '0' : ('A' - 10));
|
c = c - (isdigit(c) ? '0' : ('A' - 10));
|
||||||
|
@ -272,7 +356,6 @@ INT Addhost(PTCHAR pszInetAddr, PTCHAR pszEthAddr, PTCHAR pszIfAddr)
|
||||||
c = c - (isdigit(c) ? '0' : ('A' - 10));
|
c = c - (isdigit(c) ? '0' : ('A' - 10));
|
||||||
val += c;
|
val += c;
|
||||||
pAddHost->bPhysAddr[i] = val;
|
pAddHost->bPhysAddr[i] = val;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -285,44 +368,71 @@ INT Addhost(PTCHAR pszInetAddr, PTCHAR pszEthAddr, PTCHAR pszIfAddr)
|
||||||
|
|
||||||
|
|
||||||
/* Add the ARP entry */
|
/* Add the ARP entry */
|
||||||
if ((iRet = SetIpNetEntry(pAddHost)) != NO_ERROR) {
|
if ((iRet = SetIpNetEntry(pAddHost)) != NO_ERROR)
|
||||||
_tprintf(_T("The ARP entry addition failed: %d\n"), iRet);
|
{
|
||||||
return -1;
|
DoFormatMessage(iRet);
|
||||||
|
free(pAddHost);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(pAddHost);
|
free(pAddHost);
|
||||||
|
|
||||||
return 0;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
* Takes an internet address and an optional interface address as
|
* Takes an internet address and an optional interface address as
|
||||||
* arguments and checks their validity.
|
* arguments and checks their validity.
|
||||||
* Add the interface number and IP to an MIB_IPNETROW structure
|
* Add the interface number and IP to an MIB_IPNETROW structure
|
||||||
* and remove the entrty from the ARP cache.
|
* and remove the entry from the ARP cache.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
INT Deletehost(PTCHAR pszInetAddr, PTCHAR pszIfAddr)
|
INT Deletehost(PTCHAR pszInetAddr, PTCHAR pszIfAddr)
|
||||||
{
|
{
|
||||||
PMIB_IPNETROW pDelHost;
|
PMIB_IPNETROW pDelHost;
|
||||||
PMIB_IPADDRTABLE pIpAddrTable;
|
PMIB_IPADDRTABLE pIpAddrTable;
|
||||||
|
PMIB_IPNETTABLE pIpNetTable;
|
||||||
|
ULONG ulSize = 0;
|
||||||
DWORD dwIpAddr;
|
DWORD dwIpAddr;
|
||||||
DWORD dwSize = 0;
|
DWORD dwSize = 0;
|
||||||
INT iret;
|
INT iRet;
|
||||||
|
BOOL bFlushTable = FALSE;
|
||||||
|
|
||||||
/* error checking */
|
/* error checking */
|
||||||
|
|
||||||
/* check IP address */
|
/* check IP address */
|
||||||
if (pszInetAddr != NULL) {
|
if (pszInetAddr != NULL)
|
||||||
if ((dwIpAddr = inet_addr(pszInetAddr)) == INADDR_NONE) {
|
{
|
||||||
|
/* if wildcard is given, set flag to delete all hosts */
|
||||||
|
if (strncmp(pszInetAddr, "*", 1) == 0)
|
||||||
|
bFlushTable = TRUE;
|
||||||
|
else if ((dwIpAddr = inet_addr(pszInetAddr)) == INADDR_NONE)
|
||||||
|
{
|
||||||
_tprintf(_T("ARP: bad IP address: %s\n"), pszInetAddr);
|
_tprintf(_T("ARP: bad IP address: %s\n"), pszInetAddr);
|
||||||
return -1;
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
Usage();
|
Usage();
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* We need the IpNetTable to get the adapter index */
|
||||||
|
/* Return required buffer size */
|
||||||
|
GetIpNetTable(NULL, &ulSize, 0);
|
||||||
|
/* allocate memory for ARP address table */
|
||||||
|
pIpNetTable = (PMIB_IPNETTABLE) malloc(ulSize * sizeof(BYTE));
|
||||||
|
ZeroMemory(pIpNetTable, sizeof(*pIpNetTable));
|
||||||
|
/* get Arp address table */
|
||||||
|
if (pIpNetTable != NULL)
|
||||||
|
GetIpNetTable(pIpNetTable, &ulSize, TRUE);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_tprintf(_T("failed to allocate memory for GetIpNetTable\n"));
|
||||||
|
free(pIpNetTable);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -333,33 +443,59 @@ INT Deletehost(PTCHAR pszInetAddr, PTCHAR pszIfAddr)
|
||||||
ZeroMemory(pDelHost, sizeof(MIB_IPNETROW));
|
ZeroMemory(pDelHost, sizeof(MIB_IPNETROW));
|
||||||
/* set dwIndex field to the index of a local IP address to
|
/* set dwIndex field to the index of a local IP address to
|
||||||
* indicate the network on which the ARP entry applies */
|
* indicate the network on which the ARP entry applies */
|
||||||
if (pszIfAddr) {
|
if (pszIfAddr)
|
||||||
sscanf(pszIfAddr, "%lx", &pDelHost->dwIndex);
|
sscanf(pszIfAddr, "%lx", &pDelHost->dwIndex);
|
||||||
} else {
|
else
|
||||||
|
{
|
||||||
/* map the IP to the index */
|
/* map the IP to the index */
|
||||||
if (GetIpAddrTable(pIpAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
|
if (GetIpAddrTable(pIpAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER)
|
||||||
pIpAddrTable = (MIB_IPADDRTABLE *) malloc(dwSize);
|
pIpAddrTable = (MIB_IPADDRTABLE *) malloc(dwSize);
|
||||||
}
|
|
||||||
if ((iret = GetIpAddrTable(pIpAddrTable, &dwSize, TRUE)) != NO_ERROR) {
|
if ((iRet = GetIpAddrTable(pIpAddrTable, &dwSize, TRUE)) != NO_ERROR)
|
||||||
_tprintf(_T("GetIpAddrTable failed: %d\n"), iret);
|
{
|
||||||
|
_tprintf(_T("GetIpAddrTable failed: %d\n"), iRet);
|
||||||
_tprintf(_T("error: %d\n"), WSAGetLastError());
|
_tprintf(_T("error: %d\n"), WSAGetLastError());
|
||||||
}
|
}
|
||||||
pDelHost->dwIndex = 4; //pIpAddrTable->table[0].dwIndex;
|
/* needs testing. I get the correct index on my machine, but need others
|
||||||
|
* to test their card index. Any problems and we can use GetAdaptersInfo instead */
|
||||||
|
pDelHost->dwIndex = pIpNetTable->table[0].dwIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* copy converted IP address */
|
if (bFlushTable == TRUE)
|
||||||
pDelHost->dwAddr = dwIpAddr;
|
{
|
||||||
|
/* delete arp cache */
|
||||||
|
if ((iRet = FlushIpNetTable(pDelHost->dwIndex)) != NO_ERROR)
|
||||||
|
{
|
||||||
|
DoFormatMessage(iRet);
|
||||||
|
free(pIpAddrTable);
|
||||||
|
free(pDelHost);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
free(pIpAddrTable);
|
||||||
|
free(pDelHost);
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
/* copy converted IP address */
|
||||||
|
pDelHost->dwAddr = dwIpAddr;
|
||||||
|
|
||||||
/* Add the ARP entry */
|
/* Add the ARP entry */
|
||||||
if ((iret = DeleteIpNetEntry(pDelHost)) != NO_ERROR) {
|
if ((iRet = DeleteIpNetEntry(pDelHost)) != NO_ERROR)
|
||||||
_tprintf(_T("The ARP entry deletion failed: %d\n"), iret);
|
{
|
||||||
return -1;
|
|
||||||
|
DoFormatMessage(iRet);
|
||||||
|
free(pIpAddrTable);
|
||||||
|
free(pDelHost);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(pIpAddrTable);
|
free(pIpAddrTable);
|
||||||
free(pDelHost);
|
free(pDelHost);
|
||||||
|
|
||||||
return 0;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -412,35 +548,37 @@ VOID Usage(VOID)
|
||||||
*/
|
*/
|
||||||
INT main(int argc, char* argv[])
|
INT main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
const char N[] = "-N";
|
|
||||||
|
|
||||||
if ((argc < 2) || (argc > 5))
|
if ((argc < 2) || (argc > 5))
|
||||||
{
|
{
|
||||||
Usage();
|
Usage();
|
||||||
return FALSE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (argv[1][0] == '-')
|
||||||
if (argv[1][0] == '-') {
|
{
|
||||||
switch (argv[1][1]) {
|
switch (argv[1][1])
|
||||||
/* FIX ME */
|
{
|
||||||
/* need better control for -a, as -N might not be arg 4 */
|
case 'a': /* fall through */
|
||||||
case 'a': if (argc == 2)
|
case 'g':
|
||||||
|
if (argc == 2)
|
||||||
DisplayArpEntries(NULL, NULL);
|
DisplayArpEntries(NULL, NULL);
|
||||||
else if (argc == 3)
|
else if (argc == 3)
|
||||||
DisplayArpEntries(argv[2], NULL);
|
DisplayArpEntries(argv[2], NULL);
|
||||||
else if ((argc == 5) && ((strcmp(argv[3], N)) == 0))
|
else if ((argc == 4) && ((strcmp(argv[2], "-N")) == 0))
|
||||||
|
DisplayArpEntries(NULL, argv[3]);
|
||||||
|
else if ((argc == 5) && ((strcmp(argv[3], "-N")) == 0))
|
||||||
DisplayArpEntries(argv[2], argv[4]);
|
DisplayArpEntries(argv[2], argv[4]);
|
||||||
else
|
else
|
||||||
Usage();
|
Usage();
|
||||||
|
return EXIT_FAILURE;
|
||||||
break;
|
break;
|
||||||
case 'g': break;
|
|
||||||
case 'd': if (argc == 3)
|
case 'd': if (argc == 3)
|
||||||
Deletehost(argv[2], NULL);
|
Deletehost(argv[2], NULL);
|
||||||
else if (argc == 4)
|
else if (argc == 4)
|
||||||
Deletehost(argv[2], argv[3]);
|
Deletehost(argv[2], argv[3]);
|
||||||
else
|
else
|
||||||
Usage();
|
Usage();
|
||||||
|
return EXIT_FAILURE;
|
||||||
break;
|
break;
|
||||||
case 's': if (argc == 4)
|
case 's': if (argc == 4)
|
||||||
Addhost(argv[2], argv[3], NULL);
|
Addhost(argv[2], argv[3], NULL);
|
||||||
|
@ -448,15 +586,17 @@ INT main(int argc, char* argv[])
|
||||||
Addhost(argv[2], argv[3], argv[4]);
|
Addhost(argv[2], argv[3], argv[4]);
|
||||||
else
|
else
|
||||||
Usage();
|
Usage();
|
||||||
|
return EXIT_FAILURE;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
Usage();
|
Usage();
|
||||||
return -1;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
Usage();
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
Usage();
|
||||||
|
|
||||||
return 0;
|
return EXIT_SUCCESS;
|
||||||
}
|
} /*
|
||||||
|
warning: suggest parentheses around assignment used as truth value
|
||||||
|
warning: char format, void arg (arg 2)*/
|
||||||
|
|
|
@ -1,53 +0,0 @@
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Diagnostic Trace
|
|
||||||
//
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#define WIN32_LEAN_AND_MEAN
|
|
||||||
#include <windows.h>
|
|
||||||
#include <tchar.h>
|
|
||||||
#include "trace.h"
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef _DEBUG
|
|
||||||
|
|
||||||
#undef THIS_FILE
|
|
||||||
static char THIS_FILE[] = __FILE__;
|
|
||||||
|
|
||||||
void _DebugBreak(void)
|
|
||||||
{
|
|
||||||
DebugBreak();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Trace(TCHAR* lpszFormat, ...)
|
|
||||||
{
|
|
||||||
va_list args;
|
|
||||||
int nBuf;
|
|
||||||
TCHAR szBuffer[512];
|
|
||||||
|
|
||||||
va_start(args, lpszFormat);
|
|
||||||
nBuf = _vsntprintf(szBuffer, sizeof(szBuffer)/sizeof(TCHAR), lpszFormat, args);
|
|
||||||
OutputDebugString(szBuffer);
|
|
||||||
// was there an error? was the expanded string too long?
|
|
||||||
//ASSERT(nBuf >= 0);
|
|
||||||
va_end(args);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Assert(void* assert, TCHAR* file, int line, void* msg)
|
|
||||||
{
|
|
||||||
if (msg == NULL) {
|
|
||||||
printf("ASSERT -- %s occured on line %u of file %s.\n",
|
|
||||||
assert, line, file);
|
|
||||||
} else {
|
|
||||||
printf("ASSERT -- %s occured on line %u of file %s: Message = %s.\n",
|
|
||||||
assert, line, file, msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
void Trace(TCHAR* lpszFormat, ...) { };
|
|
||||||
void Assert(void* assert, TCHAR* file, int line, void* msg) { };
|
|
||||||
|
|
||||||
#endif //_DEBUG
|
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
|
|
@ -1,61 +0,0 @@
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
|
||||||
// Diagnostic Trace
|
|
||||||
//
|
|
||||||
#ifndef __TRACE_H__
|
|
||||||
#define __TRACE_H__
|
|
||||||
|
|
||||||
#ifdef _DEBUG
|
|
||||||
|
|
||||||
#ifdef _X86_
|
|
||||||
#define BreakPoint() _asm { int 3h }
|
|
||||||
#else
|
|
||||||
#define BreakPoint() _DebugBreak()
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef ASSERT
|
|
||||||
#define ASSERT(exp) \
|
|
||||||
{ \
|
|
||||||
if (!(exp)) { \
|
|
||||||
Assert(#exp, __FILE__, __LINE__, NULL); \
|
|
||||||
BreakPoint(); \
|
|
||||||
} \
|
|
||||||
} \
|
|
||||||
|
|
||||||
#define ASSERTMSG(exp, msg) \
|
|
||||||
{ \
|
|
||||||
if (!(exp)) { \
|
|
||||||
Assert(#exp, __FILE__, __LINE__, msg); \
|
|
||||||
BreakPoint(); \
|
|
||||||
} \
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
//=============================================================================
|
|
||||||
// MACRO: TRACE()
|
|
||||||
//=============================================================================
|
|
||||||
|
|
||||||
#define TRACE Trace
|
|
||||||
|
|
||||||
|
|
||||||
#else // _DEBUG
|
|
||||||
|
|
||||||
//=============================================================================
|
|
||||||
// Define away MACRO's ASSERT() and TRACE() in non debug builds
|
|
||||||
//=============================================================================
|
|
||||||
|
|
||||||
#ifndef ASSERT
|
|
||||||
#define ASSERT(exp)
|
|
||||||
#define ASSERTMSG(exp, msg)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define TRACE 0 ? (void)0 : Trace
|
|
||||||
|
|
||||||
#endif // !_DEBUG
|
|
||||||
|
|
||||||
|
|
||||||
void Assert(void* assert, TCHAR* file, int line, void* msg);
|
|
||||||
void Trace(TCHAR* lpszFormat, ...);
|
|
||||||
|
|
||||||
|
|
||||||
#endif // __TRACE_H__
|
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
|
Loading…
Reference in a new issue