Browse Source

Stub out a protocol field in Endpoint INETADDR types for future use.

Adam Ierymenko 5 years ago
parent
commit
8e4d7c56d8
2 changed files with 36 additions and 13 deletions
  1. 11 9
      node/Endpoint.cpp
  2. 25 4
      node/Endpoint.hpp

+ 11 - 9
node/Endpoint.cpp

@@ -25,7 +25,7 @@ bool Endpoint::operator==(const Endpoint &ep) const
 			case TYPE_URL:         return (strcmp(_v.url,ep._v.url) == 0);
 			case TYPE_ETHERNET:    return (_v.eth == ep._v.eth);
 			case TYPE_INETADDR_V4:
-			case TYPE_INETADDR_V6: return (inetAddr() == ep.inetAddr());
+			case TYPE_INETADDR_V6: return ((asInetAddress(_v.in.sa) == asInetAddress(ep._v.in.sa))&&(_v.in.proto == ep._v.in.proto));
 		}
 	}
 	return false;
@@ -45,7 +45,7 @@ bool Endpoint::operator<(const Endpoint &ep) const
 			case TYPE_URL:         return (strcmp(_v.url,ep._v.url) < 0);
 			case TYPE_ETHERNET:    return (_v.eth < ep._v.eth);
 			case TYPE_INETADDR_V4:
-			case TYPE_INETADDR_V6: return (inetAddr() < ep.inetAddr());
+			case TYPE_INETADDR_V6: return ((_v.in.proto < ep._v.in.proto)||((_v.in.proto == ep._v.in.proto)&&(asInetAddress(_v.in.sa) < asInetAddress(ep._v.in.sa))));
 			default:               return false;
 		}
 	}
@@ -100,10 +100,11 @@ int Endpoint::marshal(uint8_t data[ZT_ENDPOINT_MARSHAL_SIZE_MAX]) const noexcept
 			return 13;
 		case TYPE_INETADDR_V4:
 		case TYPE_INETADDR_V6:
-			p = asInetAddress(_v.sa).marshal(data + 7);
-			if (p < 0)
-				return p;
-			return 7 + p;
+			p = 7 + asInetAddress(_v.in.sa).marshal(data + 7);
+			if (p <= 7)
+				return -1;
+			data[p++] = _v.in.proto;
+			return p;
 		default:
 			data[0] = (uint8_t)TYPE_NIL;
 			return 7;
@@ -174,10 +175,11 @@ int Endpoint::unmarshal(const uint8_t *restrict data,const int len) noexcept
 		  return 13;
 		case TYPE_INETADDR_V4:
 		case TYPE_INETADDR_V6:
-			p = asInetAddress(_v.sa).unmarshal(data + 7,len - 7);
-			if (p <= 0)
+			p = 7 + asInetAddress(_v.in.sa).unmarshal(data + 7,len - 7);
+			if ((p <= 7)||(p >= len))
 				return -1;
-			return 7 + p;
+			_v.in.proto = data[p++];
+			return p;
 		default:
 			// Unrecognized endpoint types not yet specified must start with a 16-bit
 			// length so that older versions of ZeroTier can skip them.

+ 25 - 4
node/Endpoint.hpp

@@ -55,9 +55,21 @@ public:
 		TYPE_INETADDR_V6 =  ZT_TRACE_EVENT_PATH_TYPE_INETADDR_V6
 	};
 
+	/**
+	 * Protocol identifiers for INETADDR endpoint types
+	 *
+	 * Most of these are reserved for future use.
+	 */
+	enum Protocol
+	{
+		PROTO_UDP_ZT =      0,
+		PROTO_TCP_ZT =      1,
+		PROTO_IP_ZT =       2
+	};
+
 	ZT_ALWAYS_INLINE Endpoint() noexcept { memoryZero(this); }
 
-	explicit ZT_ALWAYS_INLINE Endpoint(const InetAddress &sa)
+	explicit ZT_ALWAYS_INLINE Endpoint(const InetAddress &sa,const Protocol proto = PROTO_UDP_ZT)
 	{
 		switch (sa.family()) {
 			case AF_INET:
@@ -69,7 +81,8 @@ public:
 				_t = TYPE_NIL;
 				return;
 		}
-		asInetAddress(_v.sa) = sa;
+		asInetAddress(_v.in.sa) = sa;
+		_v.in.proto = (uint8_t)proto;
 	}
 
 	ZT_ALWAYS_INLINE Endpoint(const Address &zt,const uint8_t identityHash[ZT_IDENTITY_HASH_SIZE]) :
@@ -95,7 +108,12 @@ public:
 	/**
 	 * @return InetAddress or NIL if not of this type
 	 */
-	ZT_ALWAYS_INLINE const InetAddress &inetAddr() const noexcept { return ((_t == TYPE_INETADDR_V4)||(_t == TYPE_INETADDR_V6)) ? asInetAddress(_v.sa) : InetAddress::NIL; }
+	ZT_ALWAYS_INLINE const InetAddress &inetAddr() const noexcept { return ((_t == TYPE_INETADDR_V4)||(_t == TYPE_INETADDR_V6)) ? asInetAddress(_v.in.sa) : InetAddress::NIL; }
+
+	/**
+	 * @return Protocol for INETADDR types, undefined for other endpoint types
+	 */
+	ZT_ALWAYS_INLINE Protocol inetAddrProto() const noexcept { return (Protocol)_v.in.proto; }
 
 	/**
 	 * @return DNS name or empty string if not of this type
@@ -149,7 +167,10 @@ private:
 	Type _t;
 	int _l[3]; // X,Y,Z location in kilometers from the nearest gravitational center of mass
 	union {
-		struct sockaddr_storage sa;
+		struct {
+			sockaddr_storage sa;
+			uint8_t proto;
+		} in;
 		struct {
 			uint16_t port;
 			char name[ZT_ENDPOINT_MAX_NAME_SIZE];