mainwnd.ui.h

来自「linux 下通过802.1认证的安装包」· C头文件 代码 · 共 620 行 · 第 1/2 页

H
620
字号
/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you want to add, delete, or rename functions or slots, use
** Qt Designer to update this file, preserving your code.
**
** You should not define a constructor or destructor in this file.
** Instead, write your code in functions called init() and destroy().
** These will automatically be called by the form's constructor and
** destructor.
*****************************************************************************/

#include <qdir.h>
#include <qregexp.h>
#include <qmessagebox.h>
#include <qfiledialog.h>

void mainWnd::destroy()
{
    config_destroy();
    xsupgui_disconnect(sockfd);
}

void mainWnd::reqState()
{
    char buffer[50];
    int bufptr = 0;

    xsupgui_get_state(buffer, &bufptr);

    if (xsupgui_send_packet(sockfd, buffer, bufptr) != 0)
     {
	QMessageBox::critical(this, QString("Error!"), QString("Error sending packet to Xsupplicant!"),1,0,0);

          return;
     }
}

void mainWnd::setAuthState( char state )
{
    switch (state)
    {
        case LOGOFF:
            authStatelbl->setText(QString("LOGOFF"));
            break;

        case DISCONNECTED:
            authStatelbl->setText(QString("DISCONNECTED"));
            break;

        case CONNECTING:
            authStatelbl->setText(QString("CONNECTING"));
            break;

        case ACQUIRED:
            // No longer used.  But we will put it here anyway.
            authStatelbl->setText(QString("ACQUIRED"));
            break;

        case AUTHENTICATING:
            authStatelbl->setText(QString("AUTHENTICATING"));
            break;

        case HELD:
            authStatelbl->setText(QString("HELD"));
            break;

        case AUTHENTICATED:
            authStatelbl->setText(QString("AUTHENTICATED"));
            break;

        case RESTART:
            authStatelbl->setText(QString("RESTART"));
            break;

        case S_FORCE_AUTH:
            authStatelbl->setText(QString("Forced Authenticated"));
            break;

        case S_FORCE_UNAUTH:
            authStatelbl->setText(QString("Forced UN-Authenticated"));
            break;

        default:
            authStatelbl->setText(QString("UNKNOWN"));
            break;
   }
}

void mainWnd::handleLogMsg(char *buf, int buflen)
{
    int bufptr = 0, cur;
    
    // The first byte is the type of log information that this is.  For now, we ignore it.
    bufptr++;
    cur = bufptr;
  
    // Since QT doesn't know what to do with \n, we need to figure it out for it.
    while (cur < buflen)
    {
	if (buf[cur] == '\n')
	{
	    buf[cur]= 0x00;
	    
	    logdata->insertItem(QString(&buf[bufptr]), -1);
	    logdata->setCurrentItem(logdata->numRows()-1);
	    logdata->ensureCurrentVisible();
	    bufptr = cur+1;
	}
	cur++;
    }

    if ((bufptr < buflen) && (buf[bufptr] != 0x00))
    {
	// We have one that we didn't do yet.
	logdata->insertItem(QString(&buf[bufptr]), -1);
	logdata->setCurrentItem(logdata->numRows()-1);
	logdata->ensureCurrentVisible();
    }
}

void mainWnd::handleNotify(char *data, int len)
{
    QMessageBox::information(this, QString("EAP Notification"), QString(data), 1, 0, 0);
    len = len;   // Shut up compiler.
}

void mainWnd::handlePassword(char *data, int len)
{
    pwdPrompt pwd;
    char *mypwd = NULL;
    char buffer[1500];
    int bufptr = 0;
    
    pwd.setPwd(&mypwd);
    pwd.setDisplay(data, len);
    pwd.exec();
    
    if (mypwd != NULL)
    {
	printf("Sending password.\n");
	xsupgui_set_password(buffer, &bufptr, mypwd);

	if (xsupgui_send_packet(sockfd, buffer, bufptr) != 0)
	{	
	    QMessageBox::critical(this, QString("Error!"), QString("Error sending packet to Xsupplicant!"),1,0,0);

	    return;
	}
    }
}

void mainWnd::dataReceived()
{
    int size = 1500, datalen = 1500, bufptr;
    char buffer[1500], data[1500];

    memset(buffer, 0, 1500);
    if (xsupgui_get_packet(sockfd, (char *)&buffer, &size, FALSE)  != ERR_NONE)
    {
	QMessageBox::critical(this, QString("Communication Error"), 
			      QString("Recieved an event that indicated a message was available, but something went wrong trying to get it."), 1, 0, 0);

	return;
    }
    else
    {
        if (size == 0)
        {
	    if (msgdisplayed != 1)
	    {
		msgdisplayed = 1;
		QMessageBox::warning(this, QString("Xsupplicant terminated"),
				     QString("This instance of Xsupplicant has terminated.  Please select another instance from the drop down box."),1,0,0);
		
		authStatelbl->setText(QString("Not Running"));
	    }
	    return;
        }

        msgdisplayed = 0;
        memset(data, 0, 1500);

        bufptr = 0;

        while (bufptr < size)
        {
            switch (xsupgui_parse_packet(buffer, &bufptr, data, &datalen))
            {
	    case AUTH_STATE:
		setAuthState(data[0]);
		break;
		
	    case LOG_MSG:
		handleLogMsg(data, datalen);
		break;
		
	    case NOTIFY:
		handleNotify(data, datalen);
		break;
		
	    case PASSWORD:
		handlePassword(data, datalen);
		break;
/*
            case TNCENABLE:
                if (data[0] == 0)
                {
                    tncState->setText(QString("DISABLED"));
                }
                else
                {
                    tncState->setText(QString("ENABLED"));
                }*/
	    case ERR_PKT_BAD:
		bufptr = size;
		break;
            }
        }
    }
}

void mainWnd::hookInt()
{
    if (interface != NULL)
    {
	free(interface);
	interface = NULL;
    }
    
    interface = strdup(intList->currentText().ascii());

    if (sockfd > 0)
    {
	// Close out the existing socket.
	xsupgui_disconnect(sockfd);	
    }
    
    sockfd = xsupgui_connect(interface);

    if (sockfd <= 0)
    {
	// Update this to be a message box instead.
	printf("Interface %s appeared to be running Xsupplicant.  However, we were "
	       "unable to obtain a socket to communicate with it.\n", interface);
	exit(1);
    }    
    
    if (sn != NULL)
    {
	delete sn;
    }
    
    // Let us know when there is something to read.
    sn = new QSocketNotifier(  sockfd, QSocketNotifier::Read, this, NULL );

    QObject::connect( sn, SIGNAL(activated(int)), this, SLOT(dataReceived()) );

    // Request initial state.
    reqState();
}

void mainWnd::init()
{
    statBar = statusBar();
     
    findInts();
    
    // So that hookInt() doesn't get confused and think we have something already open.
    sockfd = -1;
    interface = NULL;
    sn = NULL;
    
    hookInt();
    
    conf_file = QString("/etc/xsupplicant.conf");
    
    statBar->message(QString("Using configuration : ") + conf_file);
    statBar->setSizeGripEnabled(false);
    statBar->show();
    
    // Set our log window to disallow selections.
    logdata->setSelectionMode(QListBox::NoSelection);

    // When a message is displayed that is likely to be triggered repeatedly, set this to 1, and use it to block
    // displaying the message over and over again.
    msgdisplayed = 0;
    
    loadConfigAndPopulate();
}

void mainWnd::findInts()
{
    int foundints =0;
    QDir d;
    d.setFilter( QDir::AccessMask);
    d.setSorting( QDir::Size | QDir::Reversed );
    d.setPath(QString("/tmp"));
    d.setNameFilter(QString("xsupplicant.sock.*"));
    
    const QFileInfoList *list = d.entryInfoList();
    QFileInfoListIterator it( *list );
    QFileInfo *fi;
  
    QRegExp q("xsupplicant.sock.(\\w+)");
  
    while ( (fi = it.current()) != 0 ) {
	q.search(fi->fileName());

	// We should only ever catch one thing.  If we get more, or less than that, it isn't valid.

⌨️ 快捷键说明

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