+ subnet_t *p, subnet = {0};
+
+ cp();
+
+ subnet.type = SUBNET_IPV6;
+ subnet.net.ipv6.address = *address;
+ subnet.net.ipv6.prefixlength = 128;
+ subnet.owner = NULL;
+
+ do {
+ /* Go find subnet */
+
+ p = splay_search_closest_smaller(subnet_tree, &subnet);
+
+ /* Check if the found subnet REALLY matches */
+
+ if(p) {
+ if(p->type != SUBNET_IPV6)
+ return NULL;
+
+ if(!maskcmp(address, &p->net.ipv6.address, p->net.ipv6.prefixlength))
+ break;
+ else {
+ /* Otherwise, see if there is a bigger enclosing subnet */
+
+ subnet.net.ipv6.prefixlength = p->net.ipv6.prefixlength - 1;
+ if(subnet.net.ipv6.prefixlength < 0 || subnet.net.ipv6.prefixlength > 128)
+ return NULL;
+ maskcpy(&subnet.net.ipv6.address, &p->net.ipv6.address, subnet.net.ipv6.prefixlength, sizeof subnet.net.ipv6.address);
+ }
+ }
+ } while(p);
+
+ return p;
+}
+
+void subnet_update(node_t *owner, subnet_t *subnet, bool up) {
+ splay_node_t *node;
+ int i;
+ char *envp[8];
+ char netstr[MAXNETSTR + 7] = "SUBNET=";
+ char *name, *address, *port;
+
+ asprintf(&envp[0], "NETNAME=%s", netname ? : "");
+ asprintf(&envp[1], "DEVICE=%s", device ? : "");
+ asprintf(&envp[2], "INTERFACE=%s", iface ? : "");
+ asprintf(&envp[3], "NODE=%s", owner->name);
+
+ if(owner != myself) {
+ sockaddr2str(&owner->address, &address, &port);
+ asprintf(&envp[4], "REMOTEADDRESS=%s", address);
+ asprintf(&envp[5], "REMOTEPORT=%s", port);
+ envp[6] = netstr;
+ envp[7] = NULL;
+ } else {
+ envp[4] = netstr;
+ envp[5] = NULL;
+ }
+
+ name = up ? "subnet-up" : "subnet-down";
+
+ if(!subnet) {
+ for(node = owner->subnet_tree->head; node; node = node->next) {
+ subnet = node->data;
+ if(!net2str(netstr + 7, sizeof netstr - 7, subnet))
+ continue;
+ execute_script(name, envp);
+ }
+ } else {
+ if(net2str(netstr + 7, sizeof netstr - 7, subnet))
+ execute_script(name, envp);
+ }
+
+ for(i = 0; i < (owner != myself ? 6 : 4); i++)
+ free(envp[i]);
+
+ if(owner != myself) {
+ free(address);
+ free(port);
+ }