You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
6.5 KiB

/*
* Remote Laboratory FPGA Server
*
* 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* (c) 2012 Timothy Pearson
* Raptor Engineering
* http://www.raptorengineeringinc.com
*/
#include <stdio.h> /* perror() */
#include <stdlib.h> /* atoi() */
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h> /* read() */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <tqtimer.h>
#include <klocale.h>
#include "fpga_conn.h"
/* exception handling */
struct exit_exception {
int c;
exit_exception(int c):c(c) { }
};
/*
The FPGASocket class provides a socket that is connected with a client.
For every client that connects to the server, the server creates a new
instance of this class.
*/
FPGASocket::FPGASocket(int sock, TQObject *parent, const char *name) :
TDEKerberosServerSocket(parent, name), m_criticalSection(0), m_config(static_cast<FPGAServer*>(parent)->m_config) {
setServiceName("remotefpga");
line = 0;
connect(this, SIGNAL(connectionClosed()), SLOT(connectionClosedHandler()));
connect(this, SIGNAL(connectionClosed()), parent, SLOT(remoteConnectionClosed()));
setSocket(sock);
}
FPGASocket::~FPGASocket() {
//
}
void FPGASocket::close() {
if (state() == TQSocket::Connected) {
TDEKerberosServerSocket::close();
connectionClosedHandler();
TQTimer::singleShot(0, parent(), SLOT(remoteConnectionClosed()));
}
}
void FPGASocket::connectionClosedHandler() {
printf("[DEBUG] Connection from %s closed\n\r", m_remoteHost.ascii());
if (m_criticalSection > 0) {
throw exit_exception(-1);
}
}
int FPGASocket::initiateKerberosHandshake() {
if (setUsingKerberos(true) == 0) {
TQDataStream ds(this);
ds << TQString("OK");
return 0;
}
else {
return -1;
}
}
int FPGASocket::setupSerial() {
struct termios oldtio, newtio;
m_config->setGroup("FPGA");
TQString serialDevice = m_config->readEntry("serialdevice", "/dev/ttyS0");
TQString desiredBaudRate = m_config->readEntry("baudrate", "9600");
m_fd_tty = ::open(serialDevice.ascii(), O_RDWR | O_NOCTTY | O_NONBLOCK | O_APPEND);
if (m_fd_tty < 0) {
printf("[FAIL] Unable to open serial device %s\n\r", serialDevice.ascii()); fflush(stdout);
return 1;
}
tcgetattr(m_fd_tty, &oldtio); // Save current port settings
long serialBaud;
if (desiredBaudRate == "1200") {
serialBaud = B1200;
}
else if (desiredBaudRate == "9600") {
serialBaud = B9600;
}
else if (desiredBaudRate == "19200") {
serialBaud = B19200;
}
else if (desiredBaudRate == "115200") {
serialBaud = B115200;
}
else {
printf("[WARNING] Invalid baudrate %s specified, selecting 9600 instead\n\r", desiredBaudRate.ascii()); fflush(stdout);
serialBaud = B9600;
}
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = serialBaud | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
// Set input mode (non-canonical, no echo,...)
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0; // Inter-character timer unused
newtio.c_cc[VMIN] = 0; // Blocking read unused
tcflush(m_fd_tty, TCIFLUSH);
tcsetattr(m_fd_tty, TCSANOW, &newtio);
return 0;
}
int FPGASocket::enterCommandLoop() {
int cc;
char buffer[10000];
m_criticalSection++;
try {
while (state() == TQSocket::Connected) {
cc = read(m_fd_tty, buffer, 10000);
if (cc > 0) {
writeBlock(buffer, cc);
printf("[DEBUG] Got %d bytes from the serial port\n\r", cc); fflush(stdout);
}
if (canReadLine()) {
cc = readBlock(buffer, 10000);
if (cc > 0) {
if (write(m_fd_tty, buffer, cc) < 0) {
// ERROR
}
printf("[DEBUG] Got %d bytes from the network interface\n\r", cc); fflush(stdout);
}
}
}
m_criticalSection--;
return 0;
}
catch (...) {
m_criticalSection--;
return -1;
}
}
/*
The FPGAServer class handles new connections to the server. For every
client that connects, it creates a new FPGASocket -- that instance is now
responsible for the communication with that client.
*/
FPGAServer::FPGAServer(TQObject* parent, int port, KSimpleConfig* config) :
TQServerSocket( port, 1, parent ), m_config(config), m_numberOfConnections(0) {
if ( !ok() ) {
printf("[ERROR] Failed to bind to port %d\n\r", port);
exit(1);
}
printf("[INFO] Server started on port %d\n\r", port); fflush(stdout);
}
FPGAServer::~FPGAServer() {
//
}
void FPGAServer::newConnection(int socket) {
FPGASocket *s = new FPGASocket(socket, this);
s->m_remoteHost = s->peerAddress().toString();
printf("[DEBUG] New connection from %s\n\r", s->m_remoteHost.ascii());
if (m_numberOfConnections > 0) {
printf("[DEBUG] Connection from %s closed due to multiple access attempt\n\r", s->m_remoteHost.ascii());
s->close();
delete s;
s = NULL;
return;
}
if (s->initiateKerberosHandshake() != 0) {
printf("[DEBUG] Connection from %s closed due to Kerberos failure\n\r", s->m_remoteHost.ascii());
s->close();
delete s;
s = NULL;
return;
}
m_config->setGroup("Security");
TQString masterUser = m_config->readEntry("masteruser");
TQString masterRealm = m_config->readEntry("masterrealm");
if (masterRealm == "") {
masterRealm = "(NULL)";
}
if ((s->m_authenticatedUserName != masterUser) || (s->m_authenticatedRealmName != masterRealm)) {
printf("[DEBUG] Connection from %s closed due to authentication failure (attempted connection as user %s@%s)\n\r", s->m_remoteHost.ascii(), masterUser.ascii(), masterRealm.ascii());
s->close();
delete s;
s = NULL;
return;
}
if (s->setupSerial() != 0) {
printf("[DEBUG] Connection from %s closed due to serial port initialization failure\n\r", s->m_remoteHost.ascii());
s->close();
delete s;
s = NULL;
return;
}
else {
m_numberOfConnections++;
connect(s, SIGNAL(connectionClosed()), s, SLOT(deleteLater()));
emit newConnect(s);
s->enterCommandLoop();
}
}
void FPGAServer::remoteConnectionClosed() {
m_numberOfConnections--;
}