65,186
社区成员




#include <stdlib.h>
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <string.h>
#define HAVE_REMOTE
#include <pcap.h>
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "IPHLPAPI.lib")
#pragma comment(lib, "Packet.lib")
#pragma comment(lib, "wpcap.lib")
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
#if WIN32
#define snprintf sprintf_s
#endif
struct packet_t {
unsigned char dest_mac[6];
unsigned char src_mac[6];
unsigned short type;
unsigned char data[32];
};
static int get_adapter_info(unsigned char *mac, char *name, int len);
void print_mac(unsigned char* mac, int len);
int string2mac(const char *buf, unsigned char* mac);
int main(int argc, char **argv)
{
pcap_t *fp;
char errbuf[PCAP_ERRBUF_SIZE];
char dev_name[512];
struct packet_t packet;
int i=0;
#if 1
/* 检查命令行参数的合法性 */
if (argc != 2)
{
printf("usage: %s mac", argv[0]);
return -1;
}
#endif
memset((void*)&packet, 0, sizeof(struct packet_t));
string2mac(argv[1], &(packet.dest_mac[0]));
printf("mac: %s\n", argv[1]);
printf("dest mac: ");
print_mac(packet.dest_mac, 6);
printf("\n");
snprintf(dev_name, sizeof(dev_name), "rpcap://\\Device\\NPF_");
get_adapter_info(&(packet.src_mac[0]), dev_name+strlen(dev_name), sizeof(dev_name)-strlen(dev_name));
printf("src mac: ");
print_mac(packet.src_mac, 6);
printf("\n");
printf("adapter name: %s\n", dev_name);
/* 打开输出设备 */
if ((fp = pcap_open(dev_name, // 设备名
100, // 要捕获的部分 (只捕获前100个字节)
PCAP_OPENFLAG_PROMISCUOUS, // 混杂模式
1000, // 读超时时间
NULL, // 远程机器验证
errbuf // 错误缓冲
)) == NULL)
{
fprintf(stderr, "\nUnable to open the adapter. %s is not supported by WinPcap\n", argv[1]);
return -1;
}
packet.type = htons(0x0800);
packet.data[0] = 0xFF;
for (i = 0; i < 31; i++)
{
packet.data[i + 1] = i;
}
/* 发送数据包 */
if (pcap_sendpacket(fp, (unsigned char *)&packet, sizeof(struct packet_t)) != 0)
{
fprintf(stderr, "\nError sending the packet: %s\n", pcap_geterr(fp));
return -1;
}
return 0;
}
static int get_adapter_info(unsigned char *mac, char *name, int len)
{
int i;
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
if (mac == NULL || name == NULL || len <= 0 )
{
printf("[get_adapter_info:%d] Invalid parameter\n", __LINE__);
return -1;
}
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(sizeof(IP_ADAPTER_INFO));
if (pAdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
return 1;
}
// Make an initial call to GetAdaptersInfo to get
// the necessary size into the ulOutBufLen variable
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
FREE(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(ulOutBufLen);
if (pAdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
return 1;
}
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
pAdapter = pAdapterInfo;
while (pAdapter) {
printf("\tComboIndex: \t%d\n", pAdapter->ComboIndex);
printf("\tAdapter Name: \t%s\n", pAdapter->AdapterName);
printf("\tAdapter Desc: \t%s\n", pAdapter->Description);
printf("\tAdapter Addr: \t");
for (i = 0; i < pAdapter->AddressLength; i++) {
if (i == (pAdapter->AddressLength - 1))
printf("%.2X\n", (int)pAdapter->Address[i]);
else
printf("%.2X-", (int)pAdapter->Address[i]);
}
printf("\tIndex: \t%d\n", pAdapter->Index);
if (pAdapter->Type == MIB_IF_TYPE_ETHERNET)
{
memcpy(mac, pAdapter->Address, pAdapter->AddressLength);
snprintf(name, len, "%s", pAdapter->AdapterName);
}
pAdapter = pAdapter->Next;
}
}
else {
printf("GetAdaptersInfo failed with error: %d\n", dwRetVal);
}
if (pAdapterInfo)
FREE(pAdapterInfo);
return 0;
}
void print_mac(UCHAR* mac, int len)
{
int i;
for (i = 0; i < len; i++)
{
if (i == len - 1)
printf("%02X", mac[i]);
else
printf("%02X-", mac[i]);
}
}
int string2mac(const char *buf, unsigned char* mac)
{
int i,j,len;
unsigned char temp_buf[12] = { 0 };
len = strlen(buf);
if (buf == NULL || mac == NULL || len < 12)
return -1;
for (i=0, j=0; j<12&&i<len; i++)
{
if (buf[i] >= 'a' && buf[i] <= 'f')
{
temp_buf[j++] = buf[i] - 'a' + 0xA;
}
else if (buf[i] >= 'A' && buf[i] <= 'F')
{
temp_buf[j++] = buf[i] - 'A' + 0xA;
}
else if (buf[i] >= '0' && buf[i] <= '9')
{
temp_buf[j++] = buf[i] - '0';
}
else
{
if (!(buf[i] == '-' || buf[i] == ':'))
{
printf("Invalid mac format[%s]\n", buf);
return -1;
}
}
}
mac[0] = ((temp_buf[0] << 4) & 0xF0) + temp_buf[1];
mac[1] = ((temp_buf[2] << 4) & 0xF0) + temp_buf[3];
mac[2] = ((temp_buf[4] << 4) & 0xF0) + temp_buf[5];
mac[3] = ((temp_buf[6] << 4) & 0xF0) + temp_buf[7];
mac[4] = ((temp_buf[8] << 4) & 0xF0) + temp_buf[9];
mac[5] = ((temp_buf[10] << 4) & 0xF0) + temp_buf[11];
return 0;
}