#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);