/*
U2FDevice - A program to allow Raspberry Pi Zeros to act as U2F tokens
Copyright (C) 2018 Michael Kuc
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 3 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, see .
*/
#include "Streams.hpp"
#include
#include
#include
#include
#include
#include
using namespace std;
FILE* initHTML(FILE *fPtr, const string &title);
void closeHTML(FILE *fPtr);
shared_ptr getHostDescriptor()
{
static shared_ptr descriptor{};
descriptor.reset(new int{ open("/dev/hidg0", O_RDWR | O_NONBLOCK | O_APPEND) }, [](int* fd){
close(*fd);
delete fd;
});
if (*descriptor == -1)
throw runtime_error{ "Descriptor is unavailable" };
return descriptor;
}
shared_ptr getComHostStream()
{
static shared_ptr stream{ fopen("/tmp/comhost.txt", "wb"), [](FILE *f){
clog << "Closing comhost stream" << endl;
fclose(f);
} };
if (!stream)
clog << "Stream is unavailable" << endl;
return stream;
}
shared_ptr getHostPacketStream()
{
static shared_ptr stream{ initHTML(fopen("/tmp/hostpackets.html", "wb"), "Host Packets"), [](FILE *f){
clog << "Closing hostPackets stream" << endl;
closeHTML(f);
} };
if (!stream)
clog << "Stream is unavailable" << endl;
return stream;
}
shared_ptr getHostAPDUStream()
{
static shared_ptr stream{ initHTML(fopen("/tmp/hostAPDU.html", "wb"), "Host APDU"), [](FILE *f){
clog << "Closing host APDU stream" << endl;
closeHTML(f);
} };
if (!stream)
clog << "Stream is unavailable" << endl;
return stream;
}
shared_ptr getComDevStream()
{
static shared_ptr stream{ fopen("/tmp/comdev.txt", "wb"), [](FILE *f){
clog << "Closing comdev stream" << endl;
fclose(f);
} };
if (!stream)
clog << "Stream is unavailable" << endl;
return stream;
}
shared_ptr getDevPacketStream()
{
static shared_ptr stream{ initHTML(fopen("/tmp/devpackets.html", "wb"), "Dev Packets"), [](FILE *f){
clog << "Closing devPackets stream" << endl;
closeHTML(f);
} };
if (!stream)
clog << "Stream is unavailable" << endl;
return stream;
}
shared_ptr getDevAPDUStream()
{
static shared_ptr stream{ initHTML(fopen("/tmp/devAPDU.html", "wb"), "Dev APDU"), [](FILE *f){
clog << "Closing dev APDU stream" << endl;
closeHTML(f);
} };
if (!stream)
clog << "Stream is unavailable" << endl;
return stream;
}
FILE* initHTML(FILE *fPtr, const string &title)
{
fprintf(fPtr, "\n"
"\t\n"
"\t\t%s\n"
"\t\t\n"
"\t\n"
"\n"
"\t", title.c_str());
return fPtr;
}
void closeHTML(FILE *fPtr)
{
fprintf(fPtr, "\t\n"
"");
fclose(fPtr);
}