⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wire1xdlg.cpp

📁 source_code 实现无线局域网中的802.1x功能
💻 CPP
字号:
/**************************************************************************/
/* WIRE1x Version 1.0: A client-side 802.1x implementation                */
/* based on xsupplicant of Open1x for Windows XP, 2000, 98, and Me        */
/*                                                                        */
/* This code is released under both the GPL version 2 and BSD licenses.   */
/* Either license may be used.  The respective licenses are found below.  */
/*                                                                        */
/* Copyright (C) 2004, WIRE Lab, National Tsing Hua Univ., Hsinchu, Taiwan*/
/* All Rights Reserved                                                    */
/**************************************************************************/

/*
 * --- GPL Version 2 License ---
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * --- BSD License ---
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  - All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *       This product includes software developed by the University of
 *       Maryland at College Park and its contributors.
 *  - Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
// wire1xDlg.cpp : implementation file
//

#include "stdafx.h"
#include "wire1x.h"
#include "wire1xDlg.h"
#include <process.h>
#include <stdio.h>
#include <pcap.h>
#include <string.h>
#include "dot1x_globals.h"
#include "os_frame_funcs.h"
#include "eapol.h"
#include "eap.h"
#include "userconf.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
	int ret;
	char *netid = NULL, *device = NULL, *config = NULL;
	char *reterr = NULL;
	u_char *auth_addr = NULL;
	u_char dstAddr[ETH_ADDR_LEN];
	u_char *frame_ptr = NULL;
	pcap_if_t *alldev,*d;
	char errbuf[PCAP_ERRBUF_SIZE],a;
	u_char *srcmac = NULL;
	CString m_Temp;
	CString text;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWire1xDlg dialog
//803
UINT CWire1xDlg::run(LPVOID p)
    {
     CWire1xDlg * me = (CWire1xDlg *)p;
     me->run();
     return 0;
    }

//803
void CWire1xDlg::exit_cleanly(int the_signal) 
{		
	eapol_shutdown();
	clean_user_conf();
	exit(0);
}
//803
void CWire1xDlg::OnStop()
   {
    running = FALSE;
   }
void CWire1xDlg::run()
   {
	 while (eapol_authenticate() == 0)  // Loop through, and try to authenticate.
     {
		text = get_current_state();
		m_STA.SetWindowText(text);
	 }
   }
CWire1xDlg::CWire1xDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWire1xDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWire1xDlg)
	m_Password = _T("");
	m_Username = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}

void CWire1xDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWire1xDlg)
	DDX_Control(pDX, IDC_STA_STATIC, m_STA);
	DDX_Control(pDX, IDC_COMBO1, m_IF);
	DDX_Text(pDX, IDC_PASSWORD_EDIT, m_Password);
	DDX_Text(pDX, IDC_USERNAME_EDIT1, m_Username);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWire1xDlg, CDialog)
	//{{AFX_MSG_MAP(CWire1xDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_SELCHANGE(IDC_COMBO1, OnSelchangeCombo1)
	ON_BN_CLICKED(IDc_LOGIN_BUTTON, OnLoginButton)
	ON_BN_CLICKED(ID_LOGOFF_BUTTON, OnLogoffButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWire1xDlg message handlers

BOOL CWire1xDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	if(pcap_findalldevs(&alldev,errbuf)==-1)
	MessageBox("Error find alldev!!","Login error",MB_ICONEXCLAMATION);
	int i=0;
	for(d=alldev;d;d=d->next)
	{
		char Interface[200]="";
		sprintf(Interface,"%s",d->description);
		m_IF.AddString(Interface);
	}
	
	m_IF.SetCurSel(0);
	OnSelchangeCombo1();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CWire1xDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CWire1xDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWire1xDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CWire1xDlg::OnSelchangeCombo1() 
{
	// TODO: Add your control notification handler code here
	int i,ifSelect;
	char tmp[1000];

	m_IF.GetLBText(m_IF.GetCurSel(),tmp);//get string data variable
	ifSelect=m_IF.GetCurSel();
	ifSelect=tmp[0]-48;
	d=alldev;
	for(i=0; i< ifSelect;i++){
		if(!strcmp(tmp,d->description))
		break;
		d=d->next;
	}
	m_Temp=d->name;
}

void CWire1xDlg::OnLoginButton() 
{
	// TODO: Add your control notification handler code here
	CEdit *pUsername;
	CEdit *pPassword;
	
	pUsername = (CEdit *) GetDlgItem (IDC_USERNAME_EDIT1);
	pUsername->GetWindowText(m_Username);
	
	pPassword = (CEdit *) GetDlgItem (IDC_PASSWORD_EDIT);
	pPassword->GetWindowText(m_Password);

	char name[100],pw[100],face[100];
	initalize_user_conf();  // Set up our user information structure.  
	strcpy(name,m_Username);
	strcpy(pw,m_Password);
	strcpy(face,m_Temp);
  	set_username(name);
	set_password(pw);
	
	device=face;
	
	auth_addr = (u_char *)malloc(sizeof(u_char) * ETH_ADDR_LEN);
	memcpy(auth_addr,dstAddr,ETH_ADDR_LEN);


	  /**** Begin EAPOL Conversation ****/

	if ((ret = init_eapol(device, netid, auth_addr, config)) == -1) 
	{
		free(auth_addr);
		auth_addr = NULL;
		exit_cleanly(-1);
	}

	running = TRUE;                  //803
	AfxBeginThread(run, this);       //803
}

void CWire1xDlg::OnLogoffButton() 
{
	// TODO: Add your control notification handler code here
	OnStop();
	if(eapol_get_current_state() == AUTHENTICATED)
    {
      set_userLogoff(1);
      eapol_pae_transition_state();
      eapol_pae_do_state();
      set_userLogoff(0);
    }
	EndDialog(IDOK);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -