0
0
mirror of https://github.com/OpenVPN/openvpn3.git synced 2024-09-20 20:13:05 +02:00
openvpn3/openvpn/common/scoped_fd.hpp
James Yonan be3a573f66 Core change: provide logic for tunPerist that works with iOS-style
tun semantics, however this code has not been enabled yet on iOS
because it breaks in several ways:

1. network available/unavailable detection appears to break when
   tun interface is kept alive across transport connection sessions.

2. plugin session persistence appears to fail when these lines are not
   executed immediately after transport pause/resume:

     VPNTunnelSetStatus(tunnelRef, kVPNTunnelStatusReasserting, 0);
     VPNTunnelClearConfiguration(tunnelRef)

iOS Core change: change pause/reconnect delay to 3 seconds (from 2)
to reduce flapping.
2013-02-19 06:38:10 +00:00

84 lines
1.4 KiB
C++

//
// scoped_fd.hpp
// OpenVPN
//
// Copyright (c) 2012 OpenVPN Technologies, Inc. All rights reserved.
//
// A scoped file descriptor that is automatically closed by its destructor.
#ifndef OPENVPN_COMMON_SCOPED_FD_H
#define OPENVPN_COMMON_SCOPED_FD_H
#include <unistd.h> // for close()
#include <boost/noncopyable.hpp>
namespace openvpn {
class ScopedFD : boost::noncopyable
{
public:
ScopedFD() : fd(-1) {}
explicit ScopedFD(const int fd_arg)
: fd(fd_arg) {}
int release()
{
const int ret = fd;
fd = -1;
//OPENVPN_LOG("**** SFD RELEASE=" << ret);
return ret;
}
bool defined() const
{
return fd >= 0;
}
int operator()() const
{
return fd;
}
void reset(const int fd_arg)
{
close();
fd = fd_arg;
//OPENVPN_LOG("**** SFD RESET=" << fd);
}
// unusual semantics: replace fd without closing it first
void replace(const int fd_arg)
{
//OPENVPN_LOG("**** SFD REPLACE " << fd << " -> " << fd_arg);
fd = fd_arg;
}
int close()
{
if (defined())
{
const int ret = ::close(fd);
fd = -1;
//OPENVPN_LOG("**** SFD CLOSE=" << ret);
return ret;
}
else
return 0;
}
~ScopedFD()
{
close();
}
private:
int fd;
};
} // namespace openvpn
#endif // OPENVPN_COMMON_SCOPED_FD_H