arp.c
来自「一个类似windows」· C语言 代码 · 共 622 行 · 第 1/2 页
C
622 行
/*
* 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
* PROJECT: ReactOS arp utility
* FILE: apps/utils/net/arp/arp.c
* PURPOSE: view and manipulate the ARP cache
* PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
* REVISIONS:
* GM 27/06/05 Created
*
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <string.h>
#include <ctype.h>
#include <winsock2.h>
#include <iphlpapi.h>
#define UNICODE
#define _UNICODE
/*
* Globals
*/
const char SEPERATOR = '-';
int _CRT_glob = 0; // stop * from listing dir files in arp -d *
/*
* function declerations
*/
DWORD DoFormatMessage(VOID);
INT PrintEntries(PMIB_IPNETROW pIpAddRow);
INT DisplayArpEntries(PTCHAR pszInetAddr, PTCHAR pszIfAddr);
INT Addhost(PTCHAR pszInetAddr, PTCHAR pszEthAddr, PTCHAR pszIfAddr);
INT Deletehost(PTCHAR pszInetAddr, PTCHAR pszIfAddr);
VOID Usage(VOID);
/*
* convert error code into meaningful message
*/
DWORD DoFormatMessage(VOID)
{
LPVOID lpMsgBuf;
DWORD RetVal;
DWORD ErrorCode = GetLastError();
if (ErrorCode != ERROR_SUCCESS)
{
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 );
if (RetVal != 0)
{
_tprintf(_T("%s"), (LPTSTR)lpMsgBuf);
LocalFree(lpMsgBuf);
/* return number of TCHAR's stored in output buffer
* excluding '\0' - as FormatMessage does*/
return RetVal;
}
}
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;
}
/*
*
* Takes optional parameters of an internet address and interface address.
* Retrieve all entries in the ARP cache. If an internet address is
* specified, display the ARP entry relating to that address. If an
* interface address is specified, display all entries relating to
* that interface.
*
*/
/* FIXME: allow user to specify an interface address, via pszIfAddr */
INT DisplayArpEntries(PTCHAR pszInetAddr, PTCHAR pszIfAddr)
{
INT iRet;
UINT i, k;
PMIB_IPNETTABLE pIpNetTable = NULL;
PMIB_IPADDRTABLE pIpAddrTable = NULL;
DWORD Size = 0;
struct in_addr inaddr, inaddr2;
PTCHAR pszIpAddr;
TCHAR szIntIpAddr[20];
/* retrieve the IP-to-physical address mapping table */
/* get table size */
GetIpNetTable(pIpNetTable, &Size, 0);
/* allocate memory for ARP address table */
pIpNetTable = (PMIB_IPNETTABLE) HeapAlloc(GetProcessHeap(), 0, Size);
if (pIpNetTable == NULL)
goto cleanup;
ZeroMemory(pIpNetTable, sizeof(*pIpNetTable));
iRet = GetIpNetTable(pIpNetTable, &Size, TRUE);
if (iRet != NO_ERROR)
{
_tprintf(_T("failed to allocate memory for GetIpNetTable\n"));
DoFormatMessage();
goto cleanup;
}
/* check there are entries in the table */
if (pIpNetTable->dwNumEntries == 0)
{
_tprintf(_T("No ARP entires found\n"));
goto cleanup;
}
/* Retrieve the interface-to-ip address mapping
* table to get the IP address for adapter */
/* get table size */
Size = 0;
GetIpAddrTable(pIpAddrTable, &Size, 0);
pIpAddrTable = (MIB_IPADDRTABLE *) HeapAlloc(GetProcessHeap(), 0, Size);
if (pIpAddrTable == NULL)
goto cleanup;
ZeroMemory(pIpAddrTable, sizeof(*pIpAddrTable));
iRet = GetIpAddrTable(pIpAddrTable, &Size, TRUE);
if (iRet != NO_ERROR)
{
_tprintf(_T("GetIpAddrTable failed: %d\n"), iRet);
DoFormatMessage();
goto cleanup;
}
for (k=0; k < pIpAddrTable->dwNumEntries; k++)
{
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;
pszIpAddr = inet_ntoa(inaddr2);
strcpy(szIntIpAddr, pszIpAddr);
}
}
/* print header, including interface IP address and index number */
_tprintf(_T("\nInterface: %s --- 0x%lx \n"), szIntIpAddr, pIpNetTable->table[0].dwIndex);
_tprintf(_T(" Internet Address Physical Address Type\n"));
/* go through all ARP entries */
for (i=0; i < pIpNetTable->dwNumEntries; i++)
{
/* if the user has supplied their own internet addesss *
* only print the arp entry which matches that */
if (pszInetAddr)
{
inaddr.S_un.S_addr = pIpNetTable->table[i].dwAddr;
pszIpAddr = inet_ntoa(inaddr);
/* check if it matches, print it */
if (strcmp(pszIpAddr, pszInetAddr) == 0)
PrintEntries(&pIpNetTable->table[i]);
}
else
/* if an address is not supplied, print all entries */
PrintEntries(&pIpNetTable->table[i]);
}
return EXIT_SUCCESS;
cleanup:
if (pIpNetTable != NULL)
HeapFree(GetProcessHeap(), 0, pIpNetTable);
if (pIpAddrTable != NULL)
HeapFree(GetProcessHeap(), 0, pIpAddrTable);
return EXIT_FAILURE;
}
/*
*
* Takes an internet address, a MAC address and an optional interface
* address as arguments and checks their validity.
* Fill out an MIB_IPNETROW structure and insert the data into the
* ARP cache as a static entry.
*
*/
INT Addhost(PTCHAR pszInetAddr, PTCHAR pszEthAddr, PTCHAR pszIfAddr)
{
PMIB_IPNETROW pAddHost = NULL;
PMIB_IPNETTABLE pIpNetTable = NULL;
DWORD dwIpAddr = 0;
ULONG Size = 0;
INT iRet, i, val, c;
/* error checking */
/* check IP address */
if (pszInetAddr != NULL)
{
if ((dwIpAddr = inet_addr(pszInetAddr)) == INADDR_NONE)
{
_tprintf(_T("ARP: bad IP address: %s\n"), pszInetAddr);
return EXIT_FAILURE;
}
}
else
{
Usage();
return EXIT_FAILURE;
}
/* check MAC address */
if (strlen(pszEthAddr) != 17)
{
_tprintf(_T("ARP: bad argument: %s\n"), pszEthAddr);
return 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);
return EXIT_FAILURE;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?