1
0
mirror of https://github.com/wfjm/w11.git synced 2026-04-28 04:56:06 +00:00

backend support classes for networking

This commit is contained in:
Walter F.J. Mueller
2017-04-17 21:05:42 +02:00
parent d466304530
commit 726377722c
11 changed files with 1071 additions and 0 deletions

View File

@@ -0,0 +1,152 @@
// $Id: RethBuf.cpp 881 2017-04-17 18:52:26Z mueller $
//
// Copyright 2017- by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// This program is free software; you may redistribute and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 2, 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 complete details.
//
// Revision History:
// Date Rev Version Comment
// 2017-04-16 880 1.0 Initial version
// 2017-02-12 850 0.1 First draft
// ---------------------------------------------------------------------------
/*!
\file
\version $Id: RethBuf.cpp 881 2017-04-17 18:52:26Z mueller $
\brief Implemenation of RethBuf.
*/
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sstream>
#include "librtools/RosFill.hpp"
#include "librtools/RosPrintf.hpp"
#include "librtools/RosPrintBvi.hpp"
#include "RethBuf.hpp"
#include "RethTools.hpp"
using namespace std;
/*!
\class Retro::RethBuf
\brief FIXME_docs
*/
// all method definitions in namespace Retro
namespace Retro {
//------------------------------------------+-----------------------------------
// constants definitions
const size_t RethBuf::kMaxSize;
const size_t RethBuf::kMinSize;
const size_t RethBuf::kCrcSize;
const size_t RethBuf::kWOffDstMac;
const size_t RethBuf::kWOffSrcMac;
const size_t RethBuf::kWOffTyp;
//------------------------------------------+-----------------------------------
//! Default constructor
RethBuf::RethBuf()
: fTime(),
fSize(0)
{}
//------------------------------------------+-----------------------------------
//! Destructor
RethBuf::~RethBuf()
{}
//------------------------------------------+-----------------------------------
//! FIXME_docs
uint16_t RethBuf::Type() const
{
return ntohs(Buf16()[kWOffTyp]);
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
ssize_t RethBuf::Read(int fd)
{
ssize_t irc = ::read(fd, fBuf, kMaxSize);
fSize = (irc > 0) ? irc : 0;
return irc;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
ssize_t RethBuf::Write(int fd) const
{
return ::write(fd, fBuf, fSize);
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
std::string RethBuf::FrameInfo() const
{
std::ostringstream sos;
sos << RethTools::Mac2String(MacSource())
<< " > " << RethTools::Mac2String(MacDestination())
<< " typ: " << RosPrintBvi(Type(),16)
<< " siz: " << RosPrintf(Size(),"d",4);
return sos.str();
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
void RethBuf::Dump(std::ostream& os, int ind, const char* text,
int detail) const
{
RosFill bl(ind);
os << bl << (text?text:"--") << "RethBuf @ " << this << endl;
os << bl << " fTime: " << fTime << endl;
os << bl << " fBuf: " << FrameInfo() << endl;
if (detail < 0) return; // detail<0 --> info only
int ibeg = 0;
int imax = int(Size())-1;
for (int iline=0; ; iline++) {
if (ibeg > imax) break;
if (detail <= 0 && iline >= 6) break; // detail>1 --> full buffer
int iend = ibeg + 15;
int igap = 0;
if (iend > imax) {
igap = iend - imax;
iend = imax;
}
os << bl << " " << RosPrintf(ibeg,"x0", 4) << ":";
// print hex
for (int i=ibeg; i<=iend; i++) os << " " << RosPrintf(Buf8()[i],"x0", 2);
for (int i=0; i<igap; i++) os << " ";
// print ascii
os << " ";
for (int i=ibeg; i<=iend; i++) {
char c = char(Buf8()[i]);
if (c < 0x20 || c >= 0x7F) c = '.';
os << RosPrintf(char(c),"c");
}
os << endl;
ibeg += 16;
}
return;
}
} // end namespace Retro

View File

@@ -0,0 +1,95 @@
// $Id: RethBuf.hpp 881 2017-04-17 18:52:26Z mueller $
//
// Copyright 2017- by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// This program is free software; you may redistribute and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 2, 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 complete details.
//
// Revision History:
// Date Rev Version Comment
// 2017-04-17 880 1.0 Initial version
// 2017-02-12 850 0.1 First draft
// ---------------------------------------------------------------------------
/*!
\file
\version $Id: RethBuf.hpp 881 2017-04-17 18:52:26Z mueller $
\brief Declaration of class RethBuf.
*/
#ifndef included_Retro_RethBuf
#define included_Retro_RethBuf 1
#include <memory>
#include <string>
#include "librtools/Rtime.hpp"
namespace Retro {
class RethBuf {
public:
typedef std::shared_ptr<RethBuf> pbuf_t;
RethBuf();
~RethBuf();
void Clear();
void SetSize(uint16_t size);
void SetTime();
void SetTime(const Rtime& time);
uint16_t Size() const;
const Rtime& Time() const;
const uint8_t* Buf8() const;
const uint16_t* Buf16() const;
const uint32_t* Buf32() const;
uint8_t* Buf8();
uint16_t* Buf16();
uint32_t* Buf32();
void SetMacDestination(uint64_t mac);
void SetMacSource(uint64_t mac);
uint64_t MacDestination() const;
uint64_t MacSource() const;
uint16_t Type() const;
bool IsMcast() const;
bool IsBcast() const;
ssize_t Read(int fd);
ssize_t Write(int fd) const;
std::string FrameInfo() const;
virtual void Dump(std::ostream& os, int ind=0, const char* text=0,
int detail=0) const;
// some constants
static const size_t kMaxSize = 1514; //!< max ethernet frame size
static const size_t kMinSize = 60; //!< min ethernet frame size
static const size_t kCrcSize = 4; //!< size of ethernet CRC
static const size_t kWOffDstMac = 0; //!< offset dst mac in 16 bit wrds
static const size_t kWOffSrcMac = 3; //!< offset src mac in 16 bit wrds
static const size_t kWOffTyp = 6; //!< offset type in 16 bit wrds
protected:
Rtime fTime;
uint16_t fSize;
uint8_t fBuf[kMaxSize+kCrcSize];
};
} // end namespace Retro
#include "RethBuf.ipp"
#endif

View File

@@ -0,0 +1,182 @@
// $Id: RethBuf.ipp 859 2017-03-11 22:36:45Z mueller $
//
// Copyright 2017- by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// This program is free software; you may redistribute and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 2, 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 complete details.
//
// Revision History:
// Date Rev Version Comment
// 2017-02-25 856 1.0 Initial version
// 2017-02-12 850 0.1 First draft
// ---------------------------------------------------------------------------
/*!
\file
\version $Id: RethBuf.ipp 859 2017-03-11 22:36:45Z mueller $
\brief Implemenation (inline) of RethBuf.
*/
#include "RethTools.hpp"
// all method definitions in namespace Retro
namespace Retro {
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline void RethBuf::Clear()
{
fSize = 0;
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline void RethBuf::SetSize(uint16_t size)
{
fSize = size;
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline void RethBuf::SetTime()
{
fTime.GetClock(CLOCK_REALTIME);
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline void RethBuf::SetTime(const Rtime& time)
{
fTime = time;
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline uint16_t RethBuf::Size() const
{
return fSize;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline const Rtime& RethBuf::Time() const
{
return fTime;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline const uint8_t* RethBuf::Buf8() const
{
return fBuf;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline const uint16_t* RethBuf::Buf16() const
{
return (uint16_t*) fBuf;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline const uint32_t* RethBuf::Buf32() const
{
return (uint32_t*) fBuf;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline uint8_t* RethBuf::Buf8()
{
return fBuf;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline uint16_t* RethBuf::Buf16()
{
return (uint16_t*) fBuf;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline uint32_t* RethBuf::Buf32()
{
return (uint32_t*) fBuf;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline void RethBuf::SetMacDestination(uint64_t mac)
{
RethTools::Mac2WList(mac, Buf16()+kWOffDstMac);
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline void RethBuf::SetMacSource(uint64_t mac)
{
RethTools::Mac2WList(mac, Buf16()+kWOffSrcMac);
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline uint64_t RethBuf::MacDestination() const
{
return RethTools::WList2Mac(Buf16()+kWOffDstMac);
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline uint64_t RethBuf::MacSource() const
{
return RethTools::WList2Mac(Buf16()+kWOffSrcMac);
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline bool RethBuf::IsMcast() const
{
return fBuf[0] & 0x1; // odd first byte destination MAC
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline bool RethBuf::IsBcast() const
{
return Buf16()[0] == 0xffff && Buf16()[1] == 0xffff && Buf16()[2] == 0xffff;
}
} // end namespace Retro

View File

@@ -0,0 +1,98 @@
// $Id: RethTools.cpp 875 2017-04-15 21:58:50Z mueller $
//
// Copyright 2017- by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// This program is free software; you may redistribute and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 2, 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 complete details.
//
// Revision History:
// Date Rev Version Comment
// 2017-04-15 875 1.0 Initial version
// 2017-02-04 849 0.1 First draft
// ---------------------------------------------------------------------------
/*!
\file
\version $Id: RethTools.cpp 875 2017-04-15 21:58:50Z mueller $
\brief Implemenation of RethTools .
*/
#include "librtools/Rexception.hpp"
#include "librtools/Rtools.hpp"
#include "RethTools.hpp"
using namespace std;
/*!
\namespace Retro::RethTools
\brief FIXME_docs
*/
// all method definitions in namespace Retro
namespace Retro {
namespace RethTools {
//------------------------------------------+-----------------------------------
//! FIXME_docs
std::string Mac2String(uint64_t mac)
{
const char* codes = "0123456789abcdef";
string rval;
rval.reserve(3*6-1); // reserve expected length
for (int ib=0; ib<6; ib++) {
uint64_t byte = mac & 0xff;
mac = mac >> 8;
if (ib > 0) rval += ':';
rval += codes[(byte>>4) & 0x0f];
rval += codes[ byte & 0x0f];
}
return rval;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
bool String2Mac(const std::string& str, uint64_t& mac, RerrMsg& emsg)
{
if (str.length() != 17) {
emsg.Init("RethTools::String2Mac",
string("invalid string size for '") + str +"'");
return false;
}
mac = 0;
for (int ib=0; ib<6; ib++) {
unsigned long byte;
if (!Rtools::String2Long(str.substr(3*ib,2), byte, emsg, 16)) return false;
mac |= uint64_t(byte&0xff)<<(8*ib);
if (ib > 0) {
char delim = str[3*ib-1];
if (delim != ':' && delim != '-') {
emsg.Init("RethTools::String2Mac",
string("invalid delimiter '") + delim +"'");
return false;
}
}
}
return true;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
uint64_t String2Mac(const std::string& str)
{
uint64_t mac;
RerrMsg emsg;
if (!String2Mac(str, mac, emsg)) throw Rexception(emsg);
return mac;
}
} // end namespace RethTools
} // end namespace Retro

View File

@@ -0,0 +1,50 @@
// $Id: RethTools.hpp 875 2017-04-15 21:58:50Z mueller $
//
// Copyright 2017- by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// This program is free software; you may redistribute and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 2, 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 complete details.
//
// Revision History:
// Date Rev Version Comment
// 2017-04-15 875 1.0 Initial version
// 2017-02-04 849 0.1 First draft
// ---------------------------------------------------------------------------
/*!
\file
\version $Id: RethTools.hpp 875 2017-04-15 21:58:50Z mueller $
\brief Declaration of class RethTools .
*/
#ifndef included_Retro_RethTools
#define included_Retro_RethTools 1
#include <cstdint>
#include <string>
#include "librtools/RerrMsg.hpp"
namespace Retro {
namespace RethTools {
std::string Mac2String(uint64_t mac);
bool String2Mac(const std::string& str, uint64_t& mac,
RerrMsg& emsg);
uint64_t String2Mac(const std::string& str);
void Mac2WList(uint64_t mac, uint16_t wlist[3]);
uint64_t WList2Mac(const uint16_t wlist[3]);
} // end namespace RethTools
} // end namespace Retro
#include "RethTools.ipp"
#endif

View File

@@ -0,0 +1,51 @@
// $Id: RethTools.ipp 850 2017-02-12 22:51:19Z mueller $
//
// Copyright 2017- by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// This program is free software; you may redistribute and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 2, 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 complete details.
//
// Revision History:
// Date Rev Version Comment
// 2017-02-04 849 1.0 Initial version
// ---------------------------------------------------------------------------
/*!
\file
\version $Id: RethTools.ipp 850 2017-02-12 22:51:19Z mueller $
\brief Implemenation (inline) of Rw11.
*/
// all method definitions in namespace Retro
namespace Retro {
namespace RethTools {
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline void Mac2WList(uint64_t mac, uint16_t wlist[3])
{
wlist[0] = mac & 0xffff;
wlist[1] = (mac>>16) & 0xffff;
wlist[2] = (mac>>32) & 0xffff;
return;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline uint64_t WList2Mac(const uint16_t wlist[3])
{
return uint64_t(wlist[0]) |
(uint64_t(wlist[1])<<16) |
(uint64_t(wlist[2])<<32);
}
} // end namespace RethTools
} // end namespace Retro

View File

@@ -0,0 +1,98 @@
// $Id: Rw11VirtEth.cpp 868 2017-04-07 20:09:33Z mueller $
//
// Copyright 2014-2017 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// This program is free software; you may redistribute and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 2, 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 complete details.
//
// Revision History:
// Date Rev Version Comment
// 2017-04-07 868 1.0 Initial version
// 2014-06-09 561 0.1 First draft
// ---------------------------------------------------------------------------
/*!
\file
\version $Id: Rw11VirtEth.cpp 868 2017-04-07 20:09:33Z mueller $
\brief Implemenation of Rw11VirtEth.
*/
#include <memory>
#include "librtools/RparseUrl.hpp"
#include "librtools/RosFill.hpp"
#include "Rw11VirtEthTap.hpp"
#include "Rw11VirtEth.hpp"
using namespace std;
/*!
\class Retro::Rw11VirtEth
\brief FIXME_docs
*/
// all method definitions in namespace Retro
namespace Retro {
//------------------------------------------+-----------------------------------
//! Default constructor
Rw11VirtEth::Rw11VirtEth(Rw11Unit* punit)
: Rw11Virt(punit),
fChannelId(),
fRcvCb()
{
fStats.Define(kStatNVTRcvPoll, "NVTRcvPoll", "VT RcvPollHandler() calls");
fStats.Define(kStatNVTSnd, "NVTSnd", "VT Snd() calls");
fStats.Define(kStatNVTRcvByt, "NVTRcvByt", "VT bytes received");
fStats.Define(kStatNVTSndByt, "NVTSndByt", "VT bytes send");
}
//------------------------------------------+-----------------------------------
//! Destructor
Rw11VirtEth::~Rw11VirtEth()
{}
//------------------------------------------+-----------------------------------
//! FIXME_docs
Rw11VirtEth* Rw11VirtEth::New(const std::string& url, Rw11Unit* punit,
RerrMsg& emsg)
{
string scheme = RparseUrl::FindScheme(url, "tcp");
unique_ptr<Rw11VirtEth> p;
if (scheme == "tap") { // scheme -> tap:
p.reset(new Rw11VirtEthTap(punit));
if (p->Open(url, emsg)) return p.release();
} else { // scheme -> no match
emsg.Init("Rw11VirtEth::New", string("Scheme '") + scheme +
"' is not supported");
}
return 0;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
void Rw11VirtEth::Dump(std::ostream& os, int ind, const char* text,
int detail) const
{
RosFill bl(ind);
os << bl << (text?text:"--") << "Rw11VirtEth @ " << this << endl;
os << bl << " fChannelId: " << fChannelId << endl;
Rw11Virt::Dump(os, ind, " ^", detail);
return;
}
} // end namespace Retro

View File

@@ -0,0 +1,75 @@
// $Id: Rw11VirtEth.hpp 868 2017-04-07 20:09:33Z mueller $
//
// Copyright 2014-2017 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// This program is free software; you may redistribute and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 2, 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 complete details.
//
// Revision History:
// Date Rev Version Comment
// 2017-04-07 868 1.0 Initial version
// 2014-06-09 561 0.1 First draft
// ---------------------------------------------------------------------------
/*!
\file
\version $Id: Rw11VirtEth.hpp 868 2017-04-07 20:09:33Z mueller $
\brief Declaration of class Rw11VirtEth.
*/
#ifndef included_Retro_Rw11VirtEth
#define included_Retro_Rw11VirtEth 1
#include <memory>
#include "boost/function.hpp"
#include "RethBuf.hpp"
#include "Rw11Virt.hpp"
namespace Retro {
class Rw11VirtEth : public Rw11Virt {
public:
typedef boost::function<bool(std::shared_ptr<RethBuf>&)> rcvcbfo_t;
explicit Rw11VirtEth(Rw11Unit* punit);
~Rw11VirtEth();
virtual const std::string& ChannelId() const;
void SetupRcvCallback(const rcvcbfo_t& rcvcbfo);
virtual bool Snd(const RethBuf& ebuf, RerrMsg& emsg) = 0;
virtual void Dump(std::ostream& os, int ind=0, const char* text=0,
int detail=0) const;
static Rw11VirtEth* New(const std::string& url, Rw11Unit* punit,
RerrMsg& emsg);
// statistics counter indices
enum stats {
kStatNVTRcvPoll = Rw11Virt::kDimStat,
kStatNVTSnd,
kStatNVTRcvByt,
kStatNVTSndByt,
kDimStat
};
protected:
std::string fChannelId; //!< channel id
rcvcbfo_t fRcvCb; //!< receive callback fobj
};
} // end namespace Retro
#include "Rw11VirtEth.ipp"
#endif

View File

@@ -0,0 +1,47 @@
// $Id: Rw11VirtEth.ipp 847 2017-01-29 22:38:42Z mueller $
//
// Copyright 2014-2017 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// This program is free software; you may redistribute and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 2, 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 complete details.
//
// Revision History:
// Date Rev Version Comment
// 2017-01-29 847 1.0 Initial version
// 2014-06-09 561 0.1 First draft
// ---------------------------------------------------------------------------
/*!
\file
\version $Id: Rw11VirtEth.ipp 847 2017-01-29 22:38:42Z mueller $
\brief Implemenation (inline) of Rw11VirtEth.
*/
// all method definitions in namespace Retro
namespace Retro {
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline const std::string& Rw11VirtEth::ChannelId() const
{
return fChannelId;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
inline void Rw11VirtEth::SetupRcvCallback(const rcvcbfo_t& rcvcbfo)
{
fRcvCb = rcvcbfo;
return;
}
} // end namespace Retro

View File

@@ -0,0 +1,163 @@
// $Id: Rw11VirtEthTap.cpp 875 2017-04-15 21:58:50Z mueller $
//
// Copyright 2014-2017 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// This program is free software; you may redistribute and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 2, 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 complete details.
//
// Revision History:
// Date Rev Version Comment
// 2017-04-15 875 1.0 Initial version
// 2014-06-09 561 0.1 First draft
// ---------------------------------------------------------------------------
/*!
\file
\version $Id: Rw11VirtEthTap.cpp 875 2017-04-15 21:58:50Z mueller $
\brief Implemenation of Rw11VirtEthTap.
*/
#define _XOPEN_SOURCE 600
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_tun.h>
#include "boost/bind.hpp"
#include "librtools/RosFill.hpp"
#include "Rw11VirtEthTap.hpp"
using namespace std;
/*!
\class Retro::Rw11VirtEthTap
\brief FIXME_docs
*/
// all method definitions in namespace Retro
namespace Retro {
//------------------------------------------+-----------------------------------
//! Default constructor
Rw11VirtEthTap::Rw11VirtEthTap(Rw11Unit* punit)
: Rw11VirtEth(punit),
fFd(-1)
{}
//------------------------------------------+-----------------------------------
//! Destructor
Rw11VirtEthTap::~Rw11VirtEthTap()
{
if (fFd>=2) {
Server().RemovePollHandler(fFd);
close(fFd);
}
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
bool Rw11VirtEthTap::Open(const std::string& url, RerrMsg& emsg)
{
if (!fUrl.Set(url, "", "tap", emsg)) return false;
if (fUrl.Path().size() >= IFNAMSIZ-1) {
emsg.Init("Rw11VirtEthTap::Open()",
string("device name '") + fUrl.Path() + "' too long");
return false;
}
int fd = ::open("/dev/net/tun", O_RDWR);
if (fd < 0) {
emsg.InitErrno("Rw11VirtEthTap::Open()",
"open(/dev/net/tun) failed: ", errno);
return false;
}
struct ifreq ifr;
::memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, fUrl.Path().c_str(), IFNAMSIZ);
ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
if (::ioctl(fd, TUNSETIFF, &ifr) < 0) {
emsg.InitErrno("Rw11VirtEthTap::Open()",
string("ioctl for '") + fUrl.Path() + "' failed:", errno);
::close(fd);
return false;
}
fFd = fd;
Server().AddPollHandler(boost::bind(&Rw11VirtEthTap::RcvPollHandler,
this, _1),
fFd, POLLIN);
return true;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
bool Rw11VirtEthTap::Snd(const RethBuf& ebuf, RerrMsg& emsg)
{
fStats.Inc(kStatNVTSnd);
ssize_t irc = ebuf.Write(fFd);
if (irc != ssize_t(ebuf.Size())) {
emsg.InitErrno("Rw11VirtEthTap::Snd", "write() failed: ", errno);
return false;
}
fStats.Inc(kStatNVTSndByt, double(ebuf.Size()));
return true;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
int Rw11VirtEthTap::RcvPollHandler(const pollfd& pfd)
{
fStats.Inc(kStatNVTRcvPoll);
// bail-out and cancel handler if poll returns an error event
if (pfd.revents & (~pfd.events)) return -1;
RethBuf::pbuf_t pbuf(new RethBuf());
ssize_t irc = pbuf->Read(fFd);
if (irc > 0) {
pbuf->SetTime();
fRcvCb(pbuf);
fStats.Inc(kStatNVTRcvByt, double(irc));
}
return 0;
}
//------------------------------------------+-----------------------------------
//! FIXME_docs
void Rw11VirtEthTap::Dump(std::ostream& os, int ind, const char* text,
int detail) const
{
RosFill bl(ind);
os << bl << (text?text:"--") << "Rw11VirtEthTap @ " << this << endl;
os << bl << " fFd: " << fFd << endl;
Rw11VirtEth::Dump(os, ind+2, "", detail);
return;
}
} // end namespace Retro

View File

@@ -0,0 +1,60 @@
// $Id: Rw11VirtEthTap.hpp 875 2017-04-15 21:58:50Z mueller $
//
// Copyright 2014-2017 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// This program is free software; you may redistribute and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 2, 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 complete details.
//
// Revision History:
// Date Rev Version Comment
// 2017-04-15 875 1.0 Initial version
// 2014-06-09 561 0.1 First draft
// ---------------------------------------------------------------------------
/*!
\file
\version $Id: Rw11VirtEthTap.hpp 875 2017-04-15 21:58:50Z mueller $
\brief Declaration of class Rw11VirtEthTap.
*/
#ifndef included_Retro_Rw11VirtEthTap
#define included_Retro_Rw11VirtEthTap 1
#include <poll.h>
#include "Rw11VirtEth.hpp"
namespace Retro {
class Rw11VirtEthTap : public Rw11VirtEth {
public:
explicit Rw11VirtEthTap(Rw11Unit* punit);
~Rw11VirtEthTap();
virtual bool Open(const std::string& url, RerrMsg& emsg);
virtual bool Snd(const RethBuf& ebuf, RerrMsg& emsg);
virtual void Dump(std::ostream& os, int ind=0, const char* text=0,
int detail=0) const;
protected:
int RcvPollHandler(const pollfd& pfd);
protected:
int fFd; //<! fd for pty master side
};
} // end namespace Retro
//#include "Rw11VirtEthTap.ipp"
#endif