Browse Source

New format now integrated, and it works.

Adam Ierymenko 9 years ago
parent
commit
901b75e756
3 changed files with 65 additions and 80 deletions
  1. 46 70
      node/Dictionary.hpp
  2. 15 6
      node/NetworkConfig.cpp
  3. 4 4
      node/NetworkConfig.hpp

+ 46 - 70
node/Dictionary.hpp

@@ -128,86 +128,64 @@ public:
 	inline int get(const char *key,char *dest,unsigned int destlen) const
 	{
 		const char *p = _d;
-		const char *k,*s;
+		const char *k;
 		bool esc;
 		int j;
 
-		for(;;) {
-			s = p;
-			for(;;) {
-				if ((*p == '\r')||(*p == '\n')||(*p == '=')||(!*p)) {
-					k = key;
-					while ((*k)&&(s != p)) {
-						if (*(k++) != *(s++))
-							break;
-					}
-					if (*k) {
+		if (!destlen) // sanity check
+			return -1;
+
+		while (*p) {
+			k = key;
+			while (*k) {
+				if (*p != *k)
+					break;
+				++k;
+				++p;
+			}
+
+			if ((!*k)&&(*p == '=')) {
+				j = 0;
+				esc = false;
+				++p;
+				while ((*p)&&(*p != '\r')&&(*p != '\n')) {
+					if (esc) {
 						esc = false;
-						for(;;) {
-							if (!*p) {
-								dest[0] = (char)0;
-								return -1;
-							} else if (esc) {
-								esc = false;
-							} else if (*p == '\\') {
-								esc = true;
-							} else if ((*p == '\r')||(*p == '\n')) {
-								++p;
-								break;
-							}
-							++p;
+						switch(*p) {
+							case 'r': dest[j++] = '\r'; break;
+							case 'n': dest[j++] = '\n'; break;
+							case '0': dest[j++] = (char)0; break;
+							case 'e': dest[j++] = '='; break;
+							default: dest[j++] = *p; break;
 						}
-						break;
+						if (j == (int)destlen) {
+							dest[j-1] = (char)0;
+							return j-1;
+						}
+					} else if (*p == '\\') {
+						esc = true;
 					} else {
-						if (*p == '=') ++p;
-						esc = false;
-						j = 0;
-						for(;;) {
-							if (esc) {
-								esc = false;
-								if (j >= destlen) {
-									dest[destlen-1] = (char)0;
-									return (int)(destlen-1);
-								}
-								switch(*p) {
-									case 'r':
-										dest[j++] = '\r';
-										break;
-									case 'n':
-										dest[j++] = '\n';
-										break;
-									case 't':
-										dest[j++] = '\t';
-										break;
-									case '0':
-										dest[j++] = (char)0;
-										break;
-									case 'e':
-										dest[j++] = '=';
-										break;
-									default:
-										dest[j++] = *p;
-								}
-							} else if (*p == '\\') {
-								esc = true;
-							} else if ((*p == '\r')||(*p == '\n')||(!*p)) {
-								dest[j] = (char)0;
-								return j;
-							} else {
-								if (j >= destlen) {
-									dest[destlen-1] = (char)0;
-									return (int)(destlen-1);
-								}
-								dest[j++] = *p;
-							}
-							++p;
+						dest[j++] = *p;
+						if (j == (int)destlen) {
+							dest[j-1] = (char)0;
+							return j-1;
 						}
 					}
-				} else {
 					++p;
 				}
+				dest[j] = (char)0;
+				return j;
+			} else {
+				while ((*p)&&(*p != '\r')&&(*p != '\n'))
+					++p;
+				if (*p)
+					++p;
+				else break;
 			}
 		}
+
+		dest[0] = (char)0;
+		return -1;
 	}
 
 	/**
@@ -310,7 +288,6 @@ public:
 						case 0:
 						case '\r':
 						case '\n':
-						case '\t':
 						case '\\':
 						case '=':
 							_d[j++] = '\\';
@@ -322,7 +299,6 @@ public:
 								case 0: _d[j++] = '0'; break;
 								case '\r': _d[j++] = 'r'; break;
 								case '\n': _d[j++] = 'n'; break;
-								case '\t': _d[j++] = 't'; break;
 								case '\\': _d[j++] = '\\'; break;
 								case '=': _d[j++] = 'e'; break;
 							}

+ 15 - 6
node/NetworkConfig.cpp

@@ -267,11 +267,9 @@ bool NetworkConfig::fromDictionary(const Dictionary &d)
 
 		memset(this,0,sizeof(NetworkConfig));
 
-		const uint64_t ver = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_VERSION,0);
-
 		// Fields that are always present, new or old
 		this->networkId = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,0);
-		if (this->networkId)
+		if (!this->networkId)
 			return false;
 		this->timestamp = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,0);
 		this->revision = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REVISION,0);
@@ -281,7 +279,7 @@ bool NetworkConfig::fromDictionary(const Dictionary &d)
 		this->multicastLimit = (unsigned int)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,0);
 		d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME,this->name,sizeof(this->name));
 
-		if (ver < ZT_NETWORKCONFIG_VERSION) {
+		if (d.getUI(ZT_NETWORKCONFIG_DICT_KEY_VERSION,0) < 6) {
 	#ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
 			// Decode legacy fields if version is old
 			if (d.getB(ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING_OLD))
@@ -294,14 +292,18 @@ bool NetworkConfig::fromDictionary(const Dictionary &d)
 				char *saveptr = (char *)0;
 				for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
 					if (this->staticIpCount >= ZT_MAX_ZT_ASSIGNED_ADDRESSES) break;
-					this->staticIps[this->staticIpCount++] = InetAddress(f);
+					InetAddress ip(f);
+					if (!ip.isNetwork())
+						this->staticIps[this->staticIpCount++] = ip;
 				}
 			}
 			if (d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC_OLD,tmp2,sizeof(tmp2)) > 0) {
 				char *saveptr = (char *)0;
 				for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
 					if (this->staticIpCount >= ZT_MAX_ZT_ASSIGNED_ADDRESSES) break;
-					this->staticIps[this->staticIpCount++] = InetAddress(f);
+					InetAddress ip(f);
+					if (!ip.isNetwork())
+						this->staticIps[this->staticIpCount++] = ip;
 				}
 			}
 
@@ -473,6 +475,13 @@ bool NetworkConfig::fromDictionary(const Dictionary &d)
 				}
 			}
 		}
+
+		/*
+		printf("~~~\n%s\n~~~\n",d.data());
+		dump();
+		printf("~~~\n");
+		*/
+
 		return true;
 	} catch ( ... ) {
 		return false;

+ 4 - 4
node/NetworkConfig.hpp

@@ -394,8 +394,8 @@ public:
 			printf("  specialists[%u]==%.16llx\n",i,specialists[i]);
 		printf("routeCount==%u\n",routeCount);
 		for(unsigned int i=0;i<routeCount;++i) {
-			printf("  routes[i].target==%s\n",reinterpret_cast<const struct sockaddr_storage *>(&(routes[i].target))->toString().c_str());
-			printf("  routes[i].via==%s\n",reinterpret_cast<const struct sockaddr_storage *>(&(routes[i].via))->toIpString().c_str());
+			printf("  routes[i].target==%s\n",reinterpret_cast<const InetAddress *>(&(routes[i].target))->toString().c_str());
+			printf("  routes[i].via==%s\n",reinterpret_cast<const InetAddress *>(&(routes[i].via))->toIpString().c_str());
 			printf("  routes[i].flags==%.4x\n",(unsigned int)routes[i].flags);
 			printf("  routes[i].metric==%u\n",(unsigned int)routes[i].metric);
 		}
@@ -404,8 +404,8 @@ public:
 			printf("  staticIps[i]==%s\n",staticIps[i].toString().c_str());
 		printf("pinnedCount==%u\n",pinnedCount);
 		for(unsigned int i=0;i<pinnedCount;++i) {
-			printf("  pinned[i].zt==%s\n",pinned[i].zt->toString().c_str());
-			printf("  pinned[i].phy==%s\n",pinned[i].zt->toString().c_str());
+			printf("  pinned[i].zt==%s\n",pinned[i].zt.toString().c_str());
+			printf("  pinned[i].phy==%s\n",pinned[i].phy.toString().c_str());
 		}
 		printf("ruleCount==%u\n",ruleCount);
 		printf("name==%s\n",name);