-
-
Save cleverca22/79bcd06b01ba825f65416f7bd85f2855 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QString> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <QDebug> | |
#include <sys/un.h> | |
#include <QTimer> | |
#include <QTime> | |
#include "wpa_client.h" | |
int connect_proxy(int fd,sockaddr *addr,int size) { | |
return connect(fd,addr,size); | |
} | |
wpa_client::wpa_client(QString interface) { | |
this->interface = interface; | |
pinger = new QTimer(this); | |
connect(pinger,SIGNAL(timeout()),this,SLOT(ping_it())); | |
pinger->start(5000); | |
connected = false; | |
} | |
void wpa_client::ping_it() { | |
if ((!connected) && (fd > 0)) { | |
if (try_connect()) return; | |
} else send_msg("PING"); | |
} | |
void wpa_client::connect_wpa() { | |
fd = socket(PF_FILE,SOCK_DGRAM,0); | |
if (!fd) return; | |
struct sockaddr_un self; | |
self.sun_family = AF_FILE; | |
strncpy(self.sun_path,"/tmp/wpa_client",100); | |
unlink("/tmp/wpa_client"); | |
if (bind(fd,(sockaddr*)&self,sizeof(self))) { | |
qDebug("err1"); | |
return; | |
} | |
start(); | |
if (try_connect()) return; | |
} | |
bool wpa_client::try_connect() { | |
struct sockaddr_un target; | |
target.sun_family = AF_FILE; | |
strncpy(target.sun_path,qPrintable(QString("/var/run/wpa_supplicant/wlan0")),100); | |
if (connect_proxy(fd,(sockaddr*)&target,sizeof(target))) { | |
qDebug() << QString("err2 %1").arg(fd); | |
return true; | |
} | |
qDebug() << "connected?"; | |
if (send_msg("ATTACH")) { | |
return true; | |
} | |
qDebug() << "attached"; | |
connected = true; | |
return false; | |
} | |
bool wpa_client::send_msg(QString msg) { | |
if (fd < 0) return true; | |
const char *buffer; | |
buffer = qPrintable(msg); | |
int length = strlen(buffer); | |
int ret = send(fd,buffer,length,0); | |
if (ret == -1) { | |
qDebug("connection lost, reconn"); | |
connected = false; | |
return true; | |
} | |
if (ret != (int)length) { | |
qDebug() << QString("send of %1 was partial %2 %3").arg(msg).arg(ret).arg(length); | |
return true; | |
} | |
return false; | |
} | |
void wpa_client::run() { | |
char buffer[1024]; | |
int size; | |
while (1) { | |
size = recv(fd,buffer,1024,0); | |
buffer[size] = 0; | |
pinger->start(10000); | |
QString line = buffer; | |
if (line == "PONG\n") { | |
} else { | |
qDebug() << QTime::currentTime() << QString(buffer); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment