npcap收数据包,libnet发数据包

 

#pragma comment(lib,"libnet.lib")
#pragma comment(lib,"wsock32.lib")
#pragma comment(lib,"ws2_32.lib") 
#pragma comment(lib,"wpcap.lib")

#ifdef _MSC_VER
/*
 * we do not want the warnings about the old deprecated and unsecure CRT functions
 * since these examples can be compiled under *nix as well
 */
#define _CRT_SECURE_NO_WARNINGS
#endif

//#include <stdio.h>
//#include <stdlib.h>
#include <string>
#include <pcap.h>
#include <libnet.h>
#include <QDebug>
#include "protocal/EAPMD5/EAPMD5.h"
#include "protocal/EAPTLS/EAPTLS.h"
#include "common/utils.h"
using namespace std;

#define AUTHWAY_EAPMD5 0
#define AUTHWAY_EAPTLS 1

#define STATUS_DISCONNECTION 0
#define STATUS_CONNECTION    1

#define MAC_ADDRESS_LEN 6


class SendAndRecv
{
public:
	SendAndRecv(string nic);
	~SendAndRecv();
	int proccess();
	int connect(string nic, uint8_t authWay, string username, string passward);
	int disconnect();

private:
	int init();
	int destroy();
	int initNpcap();
	int initLibnet();
	int destroyNpcap();
	int destroyLibnet();
	bool LoadNpcapDlls();
	int setFilter(char* str);
	//int sendData(char* data, int dataLen);
	//char* recvData();


	int getMAC();
	int analysisPackage(const u_char* pkt_data);
	int analysisMD5(const u_char* pkt_data);
	int analysisTLS(const u_char* pkt_data);
	int sendStart();
	int sendResIdentity(const u_char* pkt_data);
	int sendResMD5(const u_char* pkt_data);
	int sendResNAK(const u_char* pkt_data);
	int sendLogoff();
	int EAPstructToPtr(EAP* eap, uint8_t* payload);

	int analysisTLSResquest(const u_char* pkt_data);
	int processEAPTLS(const u_char* pkt_data);

	pcap_if_t* alldevs;
	pcap_if_t* d;
	pcap_if_t * m_d;
	pcap_t * m_adhandle;
	string m_strNic;
	string m_strMAC;
	uint8_t* m_mac;
	string m_nicName;
	u_int m_netmask;

	libnet_t* l;
	string m_username;
	string m_passward;
	UINT8 m_iAuthWay;
	UINT8 m_iConnectionStatus;
	EAPTLS* eaptls;

};






#include "SendAndRecv.h"
#include <tchar.h>

SendAndRecv::SendAndRecv(string nic)
{
	qDebug("init SendAndRecv");
	m_strNic = nic;	
	m_iAuthWay = AUTHWAY_EAPMD5;//md5
	m_iConnectionStatus = STATUS_DISCONNECTION;

	qDebug("m_strNic:");
	qDebug((char*)m_strNic.c_str());
	m_mac = NULL;
	m_mac = Utils::getMAC((char*)m_strNic.c_str(), m_nicName, m_netmask);
	qDebug("m_mac:");
	char buf[50] = { 0 };
	sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x\n", m_mac[0], m_mac[1], m_mac[2], m_mac[3], m_mac[4], m_mac[5]);
	qDebug(buf);

	init();
	eaptls = new EAPTLS();
	if (NULL == eaptls)
	{
		return;
	}

	qDebug("init SendAndRecv over");
}
SendAndRecv::~SendAndRecv()
{
	destroy();

	if (NULL != m_mac)
	{
		delete[]m_mac;
		m_mac = NULL;
	}
}

int SendAndRecv::init()
{
	initNpcap();
	initLibnet();
	
	return 0;
}

int SendAndRecv::destroy()
{
	destroyNpcap();
	destroyLibnet();

	return 0;
}

int SendAndRecv::initNpcap()
{
	qDebug("init Npcap\n");
	int inum;
	int i = 0;
	//pcap_t* adhandle;
	char errbuf[PCAP_ERRBUF_SIZE];

	/* Load Npcap and its functions. */
	qDebug("1");
	if (!LoadNpcapDlls())
	{
		fprintf(stderr, "Couldn't load Npcap\n");
		qDebug("Couldn't load Npcap\n");
		exit(1);
	}

	/* Retrieve the device list on the local machine */
	qDebug("2");
	if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
	{
		fprintf(stderr, "Error in pcap_findalldevs: %s\n", errbuf);
		qDebug("Error in pcap_findalldevs: %s\n");
		exit(1);
	}

	qDebug("3");
	string nic = "Network adapter '" + m_strNic + "' on local host";
	for (d = alldevs; d; d = d->next)
	{
		qDebug(d->description);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值