2013-07-04 20:56:19 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Global Peer to Peer Ethernet
|
2014-02-16 20:40:22 +00:00
|
|
|
* Copyright (C) 2011-2014 ZeroTier Networks LLC
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
|
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
*
|
|
|
|
* If you would like to embed ZeroTier into a commercial application or
|
|
|
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
|
|
* LLC. Start here: http://www.zerotier.com/
|
|
|
|
*/
|
|
|
|
|
2013-12-07 00:49:20 +00:00
|
|
|
#ifndef ZT_DEMARC_HPP
|
|
|
|
#define ZT_DEMARC_HPP
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2013-09-17 19:53:59 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2013-09-17 19:53:59 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Mutex.hpp"
|
|
|
|
#include "InetAddress.hpp"
|
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
class RuntimeEnvironment;
|
|
|
|
class UdpSocket;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Local demarcation point
|
|
|
|
*
|
|
|
|
* This holds and provides unique identifiers for all local communication
|
|
|
|
* endpoints, such as UDP sockets, raw Ethernet sockets, tunnels to a relay
|
|
|
|
* server, etc. It permits other code to refer to these via Port and forget
|
|
|
|
* about what they actually are.
|
|
|
|
*
|
|
|
|
* All ports are closed when this class is destroyed.
|
2013-07-18 22:07:43 +00:00
|
|
|
*
|
|
|
|
* Its name "demarcation point" comes from the telco/cable terminology for
|
|
|
|
* the box where wires terminate at a customer's property.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
class Demarc
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Local demarcation port
|
2013-12-24 18:39:29 +00:00
|
|
|
*
|
|
|
|
* NULL_PORT is zero so this can be used in if(port) to check for
|
|
|
|
* a valid/known port.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
typedef uint64_t Port;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Port identifier used to refer to any port
|
|
|
|
*/
|
2013-08-14 15:19:21 +00:00
|
|
|
static const Port ANY_PORT;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Port identifier used to refer to null port / port not found
|
|
|
|
*/
|
2013-08-14 15:19:21 +00:00
|
|
|
static const Port NULL_PORT;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
Demarc(const RuntimeEnvironment *renv);
|
|
|
|
~Demarc();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Describe a port
|
|
|
|
*
|
|
|
|
* This can describe even ports that are not bound, e.g. from serialized
|
|
|
|
* data.
|
|
|
|
*
|
|
|
|
* @param p Port
|
|
|
|
* @return Human-readable description of port
|
|
|
|
*/
|
2013-08-30 21:05:43 +00:00
|
|
|
static std::string describe(Port p);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param p Port to check
|
|
|
|
* @return True if this port is bound/connected/etc.
|
|
|
|
*/
|
|
|
|
bool has(Port p) const
|
|
|
|
throw();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind local UDP port for both IPv4 and IPv6 traffic
|
|
|
|
*
|
|
|
|
* @param localPort Local IP port
|
|
|
|
* @return True if successfully bound, or if already bound
|
|
|
|
*/
|
2013-10-18 18:16:53 +00:00
|
|
|
bool bindLocalUdp(unsigned int localPort);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pick a port to send to an address of a given type
|
|
|
|
*
|
|
|
|
* @param to Destination address
|
|
|
|
* @return Port or NULL_PORT if none
|
|
|
|
*/
|
|
|
|
Port pick(const InetAddress &to) const
|
|
|
|
throw();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a packet
|
|
|
|
*
|
|
|
|
* If fromPort is ANY_PORT or if the port is not found, a random port is
|
|
|
|
* chosen from those available matching the characteristics of the address
|
|
|
|
* in 'to'.
|
|
|
|
*
|
|
|
|
* @param fromPort Port to send from
|
|
|
|
* @param to Destination IP/port
|
|
|
|
* @param data Data to send
|
|
|
|
* @param len Length of data in bytes
|
|
|
|
* @param hopLimit IP hop limit for UDP packets or -1 for max/unlimited
|
|
|
|
* @return Port actually sent from or NULL_PORT on failure
|
|
|
|
*/
|
|
|
|
Port send(Port fromPort,const InetAddress &to,const void *data,unsigned int len,int hopLimit) const
|
|
|
|
throw();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param p Port
|
|
|
|
* @return 64-bit integer suitable for serialization
|
|
|
|
*/
|
|
|
|
static inline uint64_t portToInt(const Port p) throw() { return (uint64_t)p; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param p 64-bit integer from serialized representation
|
|
|
|
* @return Port suitable for use in code
|
|
|
|
*/
|
|
|
|
static inline Port intToPort(const uint64_t p) throw() { return (Port)p; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const RuntimeEnvironment *_r;
|
|
|
|
|
|
|
|
static void _CBudpSocketPacketHandler(UdpSocket *sock,void *arg,const InetAddress &from,const void *data,unsigned int len);
|
|
|
|
|
|
|
|
enum DemarcPortType
|
|
|
|
{
|
|
|
|
PORT_TYPE_UDP_SOCKET_V4 = 1,
|
|
|
|
PORT_TYPE_UDP_SOCKET_V6 = 2,
|
|
|
|
PORT_TYPE_LOCAL_ETHERNET = 3,
|
|
|
|
PORT_TYPE_RELAY_TUNNEL = 4
|
|
|
|
};
|
|
|
|
|
|
|
|
// Variant holding instances of UdpSocket, etc.
|
|
|
|
struct DemarcPortObj
|
|
|
|
{
|
|
|
|
Demarc::Port port;
|
|
|
|
Demarc *parent;
|
|
|
|
void *obj;
|
|
|
|
DemarcPortType type;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::map< Port,DemarcPortObj > _ports;
|
|
|
|
Mutex _ports_m;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|