- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我写了一些代码(当然有 mdsm 手动帮助),可以在 arp 表中添加新记录。但我无法理解几行代码。我在开始标记和停止标记之间标记了这些线。我不知道这部分代码的工作原理。如果我删除标记的段落并替换为
的事件GetIpAddrTable(pIpAddrtable, &dwSize, 0)
GetIpAddrTable(pIpAddrtable, &dwSize, 0)
程序“正确”运行,但一定有什么地方出错了,我想了解是什么以及为什么?我认为这与内存分配有关。
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2ipdef.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
ULONG ulOutBufLen;
DWORD dwRetVal;
PIP_ADAPTER_INFO pAdapterInfo;
ulOutBufLen = sizeof(IP_ADAPTER_INFO);
pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
ulOutBufLen = sizeof(IP_ADAPTER_INFO);
if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) != ERROR_SUCCESS) {
free (pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) malloc ( ulOutBufLen );
}
if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) != ERROR_SUCCESS) {
printf("GetAdaptersInfo call failed with %d\n", dwRetVal);
}
PMIB_IPADDRTABLE pIpAddrtable;
DWORD dwSize = 0;
DWORD dwRetVal2 = 0;
IN_ADDR IPAddr;
free(pIpAddrtable);
pIpAddrtable = (MIB_IPADDRTABLE *) malloc(dwSize*2);
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
PMIB_IPNETROW pArpEntry;
DWORD ip = inet_addr("182.221.231.1");
//start
if (pIpAddrtable)
{
if (GetIpAddrTable(pIpAddrtable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER)
{
free(pIpAddrtable);
cout <<"Za mała ilosc pamięci";
}
if (pIpAddrtable == NULL) {
printf("Memory allocation failed for GetIpAddrTable\n");
exit(1);
}
if (dwRetVal2 = GetIpAddrTable(pIpAddrtable, &dwSize, 0) != NO_ERROR)
{
printf("Mamy error %s", dwRetVal2);
}
}
//stop
pArpEntry->dwIndex = pIpAddrtable->table[0].dwIndex;
pArpEntry->dwPhysAddrLen = 6;
pArpEntry->bPhysAddr[0] = '0x01';
pArpEntry->bPhysAddr[1] = '0xb2';
pArpEntry->bPhysAddr[2] = '0xd3';
pArpEntry->bPhysAddr[3] = '0xd4';
pArpEntry->bPhysAddr[4] = '0x05';
pArpEntry->bPhysAddr[5] = '0x16';
pArpEntry->dwType = MIB_IPNET_TYPE_STATIC;
pArpEntry->dwAddr = ip;
if (CreateIpNetEntry(pArpEntry) == ERROR_ACCESS_DENIED)
{
cout <<"Dostęp zabroniony ";
}
while (pAdapter)
{
printf(TEXT("Nazwa adaptera: %s \n"), pAdapter->AdapterName);
printf("Adres adaptera: %s \n", pAdapter->IpAddressList.IpAddress.String);
printf("Maska: %s \n ", pAdapter->IpAddressList.IpMask.String);
printf("Opis: %s \n ", pAdapter->Description);
printf("Serwer DHCP %s \n ", pAdapter->DhcpServer.IpAddress.String);
printf("Indeks: %5d \n ", pAdapter->Index);
cout <<endl;
pAdapter = pAdapter->Next;
}
return 0;
}
最佳答案
正如我的评论所建议的那样,不要逐字记录 C
示例的实现,您应该对其进行调整,使其不使用 malloc
分配原始内存, 而不是使用 std::vector
.
此外,您的样本中有几个错误,主要的错误是您使用的是未初始化的 pArpEntry
。指针。我稍后会解决这个问题。
这是一个正确的示例,它可以正常工作并且不使用动态内存分配。
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <winsock2.h>
#include <ws2ipdef.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
ULONG ulOutBufLen;
DWORD dwRetVal;
ulOutBufLen = 0;
// create a vector we will use for the PIP_ADAPTER_INFO data
std::vector<char> adapterInfo;
// call the INET API function with the vector contents serving
// as the PIP_ADAPTER_INFO
if (GetAdaptersInfo(reinterpret_cast<PIP_ADAPTER_INFO>(adapterInfo.data()), &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
// resize the buffer
adapterInfo.resize(ulOutBufLen);
注意最后两行代码。 GetAdaptersInfo
首先调用缓冲区大小为 0 的函数。这将失败并返回 ERROR_BUFFER_OVERFLOW
。错误(希望如此)。一旦它这样做了,我们就调整 adapterInfo
的大小。 vector 到 ulOutBufLen
的大小,而不是使用 malloc
,但只需调用 std::vector::resize
功能。没有动态内存分配,没有指针等。
请注意,我们必须 reinterpret_cast
API 调用中的指针,因为这是调用要求的指针类型。
进行中:
if ((dwRetVal = GetAdaptersInfo(reinterpret_cast<PIP_ADAPTER_INFO>(adapterInfo.data()), &ulOutBufLen) != ERROR_SUCCESS))
{
std::cout << "GetAdaptersInfo call failed with " << dwRetVal;
return -1;
}
PIP_ADAPTER_INFO pAdapter = reinterpret_cast<PIP_ADAPTER_INFO>(adapterInfo.data());
如果在调整大小后出现错误,我们将停止程序。如果成功,我们通过将适配器信息的地址分配给 pAdapter
来简化我们的代码。通过 reinterpret_cast
关于返回的适配器信息。
进行中:
DWORD dwSize = 0;
DWORD dwRetVal2 = 0;
std::vector<char> pIpAddrtable;
if (GetIpAddrTable(reinterpret_cast<PMIB_IPADDRTABLE>(pIpAddrtable.data()), &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER)
{
pIpAddrtable.resize(dwSize);
if (dwRetVal2 = GetIpAddrTable(reinterpret_cast<PMIB_IPADDRTABLE>(pIpAddrtable.data()), &dwSize, 0) != NO_ERROR)
{
std::cout << "Many error " << dwRetVal2;
return -1;
}
}
这基本上与 PIP_ADAPTER_INFO
相同的模式以前的代码。我们创建一个 vector ,调用 IP 函数获取大小,使用返回的大小调整 vector 的大小。
进行中:
MIB_IPNETROW arpEntry;
if (CreateIpNetEntry(&arpEntry) == ERROR_ACCESS_DENIED)
{
cout << "Access denied\n";
}
我们不需要在对 CreateIpNetEntry
的调用中分配任何东西.我们需要做的就是传递现有 MIB_IONETROW
的地址实例。这消除了您在原始代码中遇到的未初始化指针错误。
进行中:
PMIB_IPADDRTABLE theTable = reinterpret_cast<PMIB_IPADDRTABLE>(pIpAddrtable.data());
arpEntry.dwIndex = theTable->table[0].dwIndex;
arpEntry.dwPhysAddrLen = 6;
arpEntry.bPhysAddr[0] = 0x01;
arpEntry.bPhysAddr[1] = 0xb2;
arpEntry.bPhysAddr[2] = 0xd3;
arpEntry.bPhysAddr[3] = 0xd4;
arpEntry.bPhysAddr[4] = 0x05;
arpEntry.bPhysAddr[5] = 0x16;
arpEntry.dwType = MIB_IPNET_TYPE_STATIC;
arpEntry.dwAddr = ip;
我们得到一个指向 PMIB_ADDRTABLE
的指针通过 reinterpret_cast
-ing pIpAddrtable
中的数据 vector (我们称指针为 theTable
)。
进行中:
while (pAdapter)
{
std::cout << pAdapter->AdapterName << "\n";
std::cout << pAdapter->IpAddressList.IpAddress.String << "\n";
std::cout << pAdapter->IpAddressList.IpMask.String << "\n";
std::cout << pAdapter->Description << "\n";
std::cout << pAdapter->DhcpServer.IpAddress.String << "\n";
std::cout << pAdapter->Index << "\n";
std::cout << endl;
pAdapter = pAdapter->Next;
}
return 0;
}
这会输出适配器信息。
同样,为什么这有效的神奇之处在于我们使用了 std::vector<char>
并在 Windows IP 函数返回给我们正确的缓冲区大小时调整 vector 的大小。这消除了 malloc
的使用, 而不是我们简单地使用 std::vector::resize
.
我们需要做的另一件事是使用 reinterpret_cast
,因为 API 函数实际上需要正确的指针类型才能使代码正确编译。这很丑,但这就是C
的样子接口(interface)被编码,所以C++
做同样的事情需要等价物。
没有调用 malloc
, 没有调用 free
,没有要处理的指针(reinterpret_cast
除外),没有内存泄漏。
下面是使用在线 Visual Studio 2015 编译器的完整实现:
关于c++ - 代码arp数组、指针和内存分配的不可理解部分(Windows IP函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41051269/
我需要在本地网络中查找 MAC 地址并且我使用的是 Linux。我所做的是使用 libnet 发送一个 ARP 请求,但是在我发送 3 个请求后,缓存仍然不会更新。我使用 wireshark 来捕获数
我正在尝试学习 ARP 协议(protocol)。我对 ARP 的看法是,它是一种有助于将 IP 寻址到确切 MAC 地址的协议(protocol)。 但是当我尝试这样做时: arp -a 我认为它将
因此,在我玩过 Digitalsquid 开发的 Network Spoofer (http://digitalsquid.co.uk/netspoof/) 之后,我一直试图通过编写一个执行某些操作的
我目前正在编写一个发送 arp 请求的小程序,该程序似乎可以工作,因为 wireshark 捕获了数据包,但有一些奇怪的地方;首先,目标主机没有收到数据包(我没有收到任何响应,甚至在目标上运行 wir
我们有一个运行 Windows XP Embedded SP1 的产品。我们在 ARP 表中为假 IP 配置假(不存在)MAC 地址。当发生通信时,Windows 将数据包发送到伪造的 MAC 和伪造
我试图在 Linux 中将 ARP 绑定(bind)添加到 ARP 表中,我的 arp 表如下所示: IP address HW type Flags HW addre
使用 ARP 表,我们可以访问 Android 9 及更早版本的热点连接设备的 IP 和 MAC。现在来自 Android 10 的权限被拒绝。请建议我如何在 Android 10 中访问连接设备的
我正在尝试在 Linux 上用 c 语言制作一个简单的 arp 欺骗程序(主要是为了更好地理解低级网络)。到目前为止,我成功地创建了一个 arp 请求并获得了带有目标和网关 mac 地址的 arp 回
我需要在任意 IP 网络上存储对第三方设备的持久引用,其中设备的 IP 地址可能是静态的或由 DHCP 随机分配的。我不控制网络上的设备,我不能依赖 DNS 和其他现有的或与设备一起使用的临时网络协议
Scapy 文档给出了以下 ARP 缓存中毒的例子: send(Ether(dst=clientMAC)/ARP(op="who-has", psrc=gateway, pdst=client)) 问
我正在尝试在我的 android 设备上调用“arp -a”,但由于它仅适用于 pc - 我该如何更改?有没有办法访问 arp -a 给你的 IP 地址列表,但是在 android 上? 我的代码在a
我正在从事一个网络安全项目,我注意到一些我无法解释的事情: 为什么我们需要arp中的源硬件地址字段?它不是已经包含在以太网 header 中了吗? 最佳答案 ARP 是这样设计的,因此它可以在其他硬件
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我正在通过 PC1 向 PC2 发送数据,两者都是 Linux 2.6 内核机器。此传输将需要几个小时。 PC1 中的 ARP 缓存过时超时设置为 50 秒。因此在数据传输期间,PC1 每隔 50 秒
我需要编写一个java程序,获取所有计算机网络接口(interface)并扫描子网内的IP地址和MAC地址。 我不太确定该怎么做,但我发现有一个方法叫做: Arping.scan(deviceName
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我需要一些可在 Linux 下编译的 C 或 C++ 代码,以便能够获取一些任意数量的远程主机的 IP 地址列表,并为每个主机获取一个以太网 MAC 地址。这些主机可能在同一个子网上,也可能在路由器后
我一直在尝试找出将 Linux ARP 表复制到数组中的最佳方法,我只需要 IP 和 MAC 地址。我试过复制/proc/net/arp 文件,但我发现这不是最好的方法,我读到除了 ARP(IPv6
我需要编写一个程序来显示这些信息: 网络统计 TCP/UDP 连接 有关IP的信息 ipconfig/all arp-a 路线图 我已经有了其中的大部分,但是我在 route print 和 arp
我正在为 WiFi direct 编写一个应用程序,它使用我通过另一种方法(有效)传输的 mac 地址,客户端将使用 ARP 表查找主机的 IP,因为 groupownerintent 在全部在安卓上
我是一名优秀的程序员,十分优秀!