w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
Rw11VirtEthTap.cpp
Go to the documentation of this file.
1// $Id: Rw11VirtEthTap.cpp 1186 2019-07-12 17:49:59Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2014-2019 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2019-02-23 1114 1.0.4 use std::bind instead of lambda
8// 2018-12-15 1082 1.0.3 use lambda instead of boost::bind
9// 2018-11-30 1075 1.0.2 use list-init
10// 2018-10-27 1059 1.0.1 coverity fixup (uncaught exception in dtor)
11// BUGFIX: coverity (buffer not null terminated)
12// 2017-04-15 875 1.0 Initial version
13// 2014-06-09 561 0.1 First draft
14// ---------------------------------------------------------------------------
15
19#define _XOPEN_SOURCE 600
20
21#include <stdlib.h>
22#include <fcntl.h>
23#include <errno.h>
24#include <unistd.h>
25#include <string.h>
26
27#include <sys/ioctl.h>
28#include <net/if.h>
29#include <linux/if_tun.h>
30
31#include <functional>
32
33#include "librtools/RosFill.hpp"
34#include "librtools/Rtools.hpp"
35
36#include "Rw11VirtEthTap.hpp"
37
38using namespace std;
39using namespace std::placeholders;
40
46// all method definitions in namespace Retro
47namespace Retro {
48
49//------------------------------------------+-----------------------------------
51
53 : Rw11VirtEth(punit),
54 fFd(-1)
55{}
56
57//------------------------------------------+-----------------------------------
59
61{
62 if (fFd>=2) {
63 Rtools::Catch2Cerr(__func__,
64 [this](){ Server().RemovePollHandler(fFd); } );
65 ::close(fFd);
66 }
67}
68
69//------------------------------------------+-----------------------------------
71
72bool Rw11VirtEthTap::Open(const std::string& url, RerrMsg& emsg)
73{
74 if (!fUrl.Set(url, "", "tap", emsg)) return false;
75
76 if (fUrl.Path().size() >= IFNAMSIZ-1) {
77 emsg.Init("Rw11VirtEthTap::Open()",
78 string("device name '") + fUrl.Path() + "' too long");
79 return false;
80 }
81
82 int fd = ::open("/dev/net/tun", O_RDWR);
83 if (fd < 0) {
84 emsg.InitErrno("Rw11VirtEthTap::Open()",
85 "open(/dev/net/tun) failed: ", errno);
86 return false;
87 }
88
89 struct ifreq ifr = {};
90 ::strncpy(ifr.ifr_name, fUrl.Path().c_str(), IFNAMSIZ-1);
91 ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
92
93 if (::ioctl(fd, TUNSETIFF, &ifr) < 0) {
94 emsg.InitErrno("Rw11VirtEthTap::Open()",
95 string("ioctl for '") + fUrl.Path() + "' failed:", errno);
96 ::close(fd);
97 return false;
98 }
99
100 fFd = fd;
101
103 fFd, POLLIN);
104 return true;
105}
106
107//------------------------------------------+-----------------------------------
109
110bool Rw11VirtEthTap::Snd(const RethBuf& ebuf, RerrMsg& emsg)
111{
113 ssize_t irc = ebuf.Write(fFd);
114 if (irc != ssize_t(ebuf.Size())) {
115 emsg.InitErrno("Rw11VirtEthTap::Snd", "write() failed: ", errno);
116 return false;
117 }
118 fStats.Inc(kStatNVTSndByt, double(ebuf.Size()));
119 return true;
120}
121
122//------------------------------------------+-----------------------------------
124
125int Rw11VirtEthTap::RcvPollHandler(const pollfd& pfd)
126{
128 // bail-out and cancel handler if poll returns an error event
129 if (pfd.revents & (~pfd.events)) return -1;
130
131 RethBuf::pbuf_t pbuf(new RethBuf());
132 ssize_t irc = pbuf->Read(fFd);
133
134 if (irc > 0) {
135 pbuf->SetTime();
136 fRcvCb(pbuf);
137 fStats.Inc(kStatNVTRcvByt, double(irc));
138 }
139
140 return 0;
141}
142
143//------------------------------------------+-----------------------------------
145
146void Rw11VirtEthTap::Dump(std::ostream& os, int ind, const char* text,
147 int detail) const
148{
149 RosFill bl(ind);
150 os << bl << (text?text:"--") << "Rw11VirtEthTap @ " << this << endl;
151
152 os << bl << " fFd: " << fFd << endl;
153 Rw11VirtEth::Dump(os, ind+2, "", detail);
154 return;
155}
156
157
158} // end namespace Retro
FIXME_docs.
Definition: RerrMsg.hpp:25
void Init(const std::string &meth, const std::string &text)
FIXME_docs.
Definition: RerrMsg.cpp:74
void InitErrno(const std::string &meth, const std::string &text, int errnum)
FIXME_docs.
Definition: RerrMsg.cpp:84
FIXME_docs.
Definition: RethBuf.hpp:27
std::shared_ptr< RethBuf > pbuf_t
Definition: RethBuf.hpp:29
uint16_t Size() const
FIXME_docs.
Definition: RethBuf.ipp:60
ssize_t Write(int fd) const
FIXME_docs.
Definition: RethBuf.cpp:81
void RemovePollHandler(int fd, short events, bool nothrow=false)
FIXME_docs.
void AddPollHandler(pollhdl_t &&pollhdl, int fd, short events=POLLIN)
FIXME_docs.
I/O appicator to generate fill characters.
Definition: RosFill.hpp:24
bool Set(const std::string &url, const std::string &optlist, RerrMsg &emsg)
FIXME_docs.
Definition: RparseUrl.cpp:55
const std::string & Path() const
FIXME_docs.
Definition: RparseUrl.ipp:45
void Inc(size_t ind, double val=1.)
FIXME_docs.
Definition: Rstats.ipp:29
FIXME_docs.
Definition: Rw11Unit.hpp:39
virtual void Dump(std::ostream &os, int ind=0, const char *text=0, int detail=0) const
FIXME_docs.
int fFd
fd for pty master side
Rw11VirtEthTap(Rw11Unit *punit)
Default constructor.
virtual bool Snd(const RethBuf &ebuf, RerrMsg &emsg)
FIXME_docs.
~Rw11VirtEthTap()
Destructor.
int RcvPollHandler(const pollfd &pfd)
FIXME_docs.
virtual bool Open(const std::string &url, RerrMsg &emsg)
FIXME_docs.
virtual void Dump(std::ostream &os, int ind=0, const char *text=0, int detail=0) const
FIXME_docs.
Definition: Rw11VirtEth.cpp:78
rcvcbfo_t fRcvCb
receive callback fobj
Definition: Rw11VirtEth.hpp:60
RparseUrl fUrl
Definition: Rw11Virt.hpp:66
Rstats fStats
statistics
Definition: Rw11Virt.hpp:68
RlinkServer & Server() const
FIXME_docs.
Definition: Rw11Virt.ipp:55
void Catch2Cerr(const char *msg, std::function< void()> func)
FIXME_docs.
Definition: Rtools.cpp:170
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47