// public static CIDRNetwork IPAddress::createNetwork(StringPiece ipSlashCidr, int defaultCidr, /* = -1 */ bool applyMask /* = true */) { if (defaultCidr > std::numeric_limits<uint8_t>::max()) { throw std::range_error("defaultCidr must be <= UINT8_MAX"); } vector<string> vec; split("/", ipSlashCidr, vec); vector<string>::size_type elemCount = vec.size(); if (elemCount == 0 || // weird invalid string elemCount > 2) { // invalid string (IP/CIDR/extras) throw IPAddressFormatException("Invalid ipSlashCidr specified. ", "Expected IP/CIDR format, got ", "'", ipSlashCidr, "'"); } IPAddress subnet(vec.at(0)); uint8_t cidr = (defaultCidr > -1) ? defaultCidr : (subnet.isV4() ? 32 : 128); if (elemCount == 2) { try { cidr = to<uint8_t>(vec.at(1)); } catch (...) { throw IPAddressFormatException("Mask value ", "'", vec.at(1), "' not a valid mask"); } } if (cidr > subnet.bitCount()) { throw IPAddressFormatException("CIDR value '", cidr, "' ", "is > network bit count ", "'", subnet.bitCount(), "'"); } return std::make_pair(applyMask ? subnet.mask(cidr) : subnet, cidr); }
Net::Net(){ // Initialize Ethernet byte mac[]={ 0x90, 0xA2, 0xDA, 0x0D, 0x02, 0xD0 }; IPAddress ip(192,168,1, 77); IPAddress dns(192,168,1, 1); IPAddress gateway(192,168,1, 1); IPAddress subnet(255,255,255,0); Ethernet.begin(mac, ip, dns, gateway, subnet); // Processors processor=NULL; processorCount=0; // Enable interrupts EthernetInterrupt::begin(0); }
/** Initialize ACL list for HTTP server */ static bool InitHTTPAllowList() { rpc_allow_subnets.clear(); rpc_allow_subnets.push_back(CSubNet("127.0.0.0/8")); // always allow IPv4 local subnet rpc_allow_subnets.push_back(CSubNet("::1")); // always allow IPv6 localhost if (mapMultiArgs.count("-rpcallowip")) { const std::vector<std::string>& vAllow = mapMultiArgs["-rpcallowip"]; BOOST_FOREACH (std::string strAllow, vAllow) { CSubNet subnet(strAllow); if (!subnet.IsValid()) { uiInterface.ThreadSafeMessageBox( strprintf("Invalid -rpcallowip subnet specification: %s. Valid are a single IP (e.g. 1.2.3.4), a network/netmask (e.g. 1.2.3.4/255.255.255.0) or a network/CIDR (e.g. 1.2.3.4/24).", strAllow), "", CClientUIInterface::MSG_ERROR); return false; } rpc_allow_subnets.push_back(subnet); } }
//............................................................................... // start accesspoint //............................................................................... void WIFI::startAP(int state) { if (state) { logging.debug("starting access point"); //Dl; WiFi.mode(WIFI_AP); //Dl; IPAddress apIP(192,168,4,1); IPAddress gateway(192,168,4,1); IPAddress subnet(255,255,255,0); //Dl; String apSSID = ffs.cfg.readItem("ap_ssid"); String apPSK = ffs.cfg.readItem("ap_password"); //Dl; logging.info("configuring access point"); logging.debug(" IP address: " + apIP.toString()); logging.debug(" gateway: " + gateway.toString()); logging.debug(" subnet: " + subnet.toString()); if (WiFi.softAPConfig(apIP, gateway, subnet)){ if (WiFi.softAP(apSSID.c_str(), apPSK.c_str())) { logging.info("access point is now on"); } else { logging.error("starting access point failed"); } } else { logging.error("access point configuration failed"); } if (on_ap_no_stations_connected != nullptr) on_ap_no_stations_connected(); } else { logging.debug("stopping access point"); WiFi.mode(WIFI_STA); WiFi.softAPdisconnect(true); logging.info("access point is now off"); } }
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway) { IPAddress subnet(255, 255, 255, 0); begin(mac_address, local_ip, dns_server, gateway, subnet); }
void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway) { IPAddress subnet(255, 255, 255, 0); begin(local_ip, dns_server, gateway, subnet); }
/* * fill in all the requested attributes for a system. * if the system's entry doesn't have all required, * walk through successively more inclusive networks * for inherited attributes. */ Ndbtuple* ndbipinfo(Ndb *db, char *attr, char *val, char **alist, int n) { Ndbtuple *t, *nt, *f; Ndbs s; char *ipstr; uint8_t net[IPaddrlen], ip[IPaddrlen]; int prefix, smallestprefix, force; int64_t r; /* just in case */ fmtinstall('I', eipfmt); fmtinstall('M', eipfmt); /* get needed attributes */ f = mkfilter(n, alist); /* * first look for a matching entry with an ip address */ t = nil; ipstr = ndbgetvalue(db, &s, attr, val, "ip", &nt); if(ipstr == nil){ /* none found, make one up */ if(strcmp(attr, "ip") != 0) { ndbfree(f); return nil; } t = ndbnew("ip", val); t->line = t; t->entry = nil; r = parseip(net, val); if(r == -1) ndbfree(t); } else { /* found one */ while(nt != nil){ nt = ndbreorder(nt, s.t); t = ndbconcatenate(t, nt); nt = ndbsnext(&s, attr, val); } r = parseip(net, ipstr); free(ipstr); } if(r < 0){ ndbfree(f); return nil; } ipmove(ip, net); t = filter(db, t, f); /* * now go through subnets to fill in any missing attributes */ if(isv4(net)){ prefix = 127; smallestprefix = 100; force = 0; } else { /* in v6, the last 8 bytes have no structure (we hope) */ prefix = 64; smallestprefix = 2; memset(net+8, 0, 8); force = 1; } /* * to find a containing network, keep turning off * the lower bit and look for a network with * that address and a shorter mask. tedius but * complete, we may need to find a trick to speed this up. */ for(; prefix >= smallestprefix; prefix--){ if(filtercomplete(f)) break; if(!force && (net[prefix/8] & (1<<(7-(prefix%8)))) == 0) continue; force = 0; net[prefix/8] &= ~(1<<(7-(prefix%8))); t = ndbconcatenate(t, subnet(db, net, f, prefix)); } /* * if there's an unfulfilled ipmask, make one up */ nt = ndbfindattr(f, f, "ipmask"); if(nt && !(nt->ptr & Fignore)){ char x[64]; snprint(x, sizeof(x), "%M", defmask(ip)); t = ndbconcatenate(t, ndbnew("ipmask", x)); } ndbfree(f); ndbsetmalloctag(t, getcallerpc(&db)); return t; }
// read in a list linking IPs, subnets & IP ranges to filter groups bool IPList::readIPMelangeList(const char *filename) { // load in the list file std::ifstream input ( filename ); if (!input) { if (!is_daemonised) { std::cerr << "Error reading file (does it exist?): " << filename << std::endl; } syslog(LOG_ERR, "%s%s","Error reading file (does it exist?): ",filename); return false; } // compile regexps for determining whether a list entry is an IP, a subnet (IP + mask), or a range RegExp matchIP, matchSubnet, matchRange; #ifdef HAVE_PCRE matchIP.comp("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"); matchSubnet.comp("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"); matchRange.comp("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}-\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"); #else matchIP.comp("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"); matchSubnet.comp("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"); matchRange.comp("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}-[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"); #endif // read in the file String line; char buffer[ 2048 ]; while (input) { if (!input.getline(buffer, sizeof( buffer ))) { break; } // ignore comments if (buffer[0] == '#') continue; // ignore blank lines if (strlen(buffer) < 7) continue; line = buffer; #ifdef DGDEBUG std::cout << "line: " << line << std::endl; #endif // store the IP address (numerically, not as a string) and filter group in either the IP list, subnet list or range list if (matchIP.match(line.toCharArray())) { struct in_addr address; if (inet_aton(line.toCharArray(), &address)) { uint32_t addr = ntohl(address.s_addr); iplist.push_back(addr); } } else if (matchSubnet.match(line.toCharArray())) { struct in_addr address; struct in_addr addressmask; String subnet(line.before("/")); String mask(line.after("/")); if (inet_aton(subnet.toCharArray(), &address) && inet_aton(mask.toCharArray(), &addressmask)) { ipl_subnetstruct s; uint32_t addr = ntohl(address.s_addr); s.mask = ntohl(addressmask.s_addr); // pre-mask the address for quick comparison s.maskedaddr = addr & s.mask; ipsubnetlist.push_back(s); } } else if (matchRange.match(line.toCharArray())) { struct in_addr addressstart; struct in_addr addressend; String start(line.before("-")); String end(line.after("-")); if (inet_aton(start.toCharArray(), &addressstart) && inet_aton(end.toCharArray(), &addressend)) { ipl_rangestruct r; r.startaddr = ntohl(addressstart.s_addr); r.endaddr = ntohl(addressend.s_addr); iprangelist.push_back(r); } } // hmmm. the line didn't match any of our regular expressions. // assume it's a hostname. else { line.toLower(); hostlist.push_back(line); } } input.close(); #ifdef DGDEBUG std::cout << "starting sort" << std::endl; #endif std::sort(iplist.begin(), iplist.end()); std::sort(hostlist.begin(), hostlist.end()); #ifdef DGDEBUG std::cout << "sort complete" << std::endl; std::cout << "ip list dump:" << std::endl; std::vector<uint32_t>::iterator i = iplist.begin(); while (i != iplist.end()) { std::cout << "IP: " << *i << std::endl; ++i; } std::cout << "subnet list dump:" << std::endl; std::list<ipl_subnetstruct>::iterator j = ipsubnetlist.begin(); while (j != ipsubnetlist.end()) { std::cout << "Masked IP: " << j->maskedaddr << " Mask: " << j->mask << std::endl; ++j; } std::cout << "range list dump:" << std::endl; std::list<ipl_rangestruct>::iterator k = iprangelist.begin(); while (k != iprangelist.end()) { std::cout << "Start IP: " << k->startaddr << " End IP: " << k->endaddr << std::endl; ++k; } std::cout << "host list dump:" << std::endl; std::vector<String>::iterator l = hostlist.begin(); while (l != hostlist.end()) { std::cout << "Hostname: " << *l << std::endl; ++l; } #endif return true; }
void EthernetShieldClass::begin(uint8_t *mac_address, IPAddressShield local_ip, IPAddressShield dns_server, IPAddressShield gateway) { IPAddressShield subnet(255, 255, 255, 0); begin(mac_address, local_ip, dns_server, gateway, subnet); }
// read in a list linking IPs, subnets & IP ranges to filter groups // return 0 for success, -1 for failure, 1 for warning int ipinstance::readIPMelangeList(const char *filename) { // load in the list file std::ifstream input ( filename ); if (!input) { if (!is_daemonised) { std::cerr << "Error reading file (does it exist?): " << filename << std::endl; } syslog(LOG_ERR, "%s%s","Error reading file (does it exist?): ",filename); return -1; } // compile regexps for determining whether a list entry is an IP, a subnet (IP + mask), or a range RegExp matchIP, matchSubnet, matchRange; #ifdef HAVE_PCRE matchIP.comp("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"); matchSubnet.comp("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"); matchRange.comp("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}-\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"); #else matchIP.comp("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"); matchSubnet.comp("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"); matchRange.comp("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}-[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"); #endif // read in the file String line; String key, value; char buffer[ 2048 ]; bool warn = false; while (input) { if (!input.getline(buffer, sizeof( buffer ))) { break; } // ignore comments if (buffer[0] == '#') continue; // ignore blank lines if (strlen(buffer) < 10) continue; line = buffer; // split into key & value if (line.contains("=")) { key = line.before("="); key.removeWhiteSpace(); value = line.after("filter"); } else { if (!is_daemonised) std::cerr << "No filter group given; entry " << line << " in " << filename << std::endl; syslog(LOG_ERR, "No filter group given; entry %s in %s", line.toCharArray(), filename); warn = true; continue; } #ifdef DGDEBUG std::cout << "key: " << key << std::endl; std::cout << "value: " << value.toInteger() << std::endl; #endif if ((value.toInteger() < 1) || (value.toInteger() > o.filter_groups)) { if (!is_daemonised) std::cerr << "Filter group out of range; entry " << line << " in " << filename << std::endl; syslog(LOG_ERR, "Filter group out of range; entry %s in %s", line.toCharArray(), filename); warn = true; continue; } // store the IP address (numerically, not as a string) and filter group in either the IP list, subnet list or range list if (matchIP.match(key.toCharArray())) { struct in_addr address; if (inet_aton(key.toCharArray(), &address)) { iplist.push_back(ip(ntohl(address.s_addr),value.toInteger()-1)); } } else if (matchSubnet.match(key.toCharArray())) { struct in_addr address; struct in_addr addressmask; String subnet(key.before("/")); String mask(key.after("/")); if (inet_aton(subnet.toCharArray(), &address) && inet_aton(mask.toCharArray(), &addressmask)) { subnetstruct s; int addr = ntohl(address.s_addr); s.mask = ntohl(addressmask.s_addr); // pre-mask the address for quick comparison s.maskedaddr = addr & s.mask; s.group = value.toInteger()-1; ipsubnetlist.push_back(s); } } else if (matchRange.match(key.toCharArray())) { struct in_addr addressstart; struct in_addr addressend; String start(key.before("-")); String end(key.after("-")); if (inet_aton(start.toCharArray(), &addressstart) && inet_aton(end.toCharArray(), &addressend)) { rangestruct r; r.startaddr = ntohl(addressstart.s_addr); r.endaddr = ntohl(addressend.s_addr); r.group = value.toInteger()-1; iprangelist.push_back(r); } } // hmmm. the key didn't match any of our regular expressions. output message & return a warning value. else { if (!is_daemonised) std::cerr << "Entry " << line << " in " << filename << " was not recognised as an IP address, subnet or range" << std::endl; syslog(LOG_ERR, "Entry %s in %s was not recognised as an IP address, subnet or range", line.toCharArray(), filename); warn = true; } } input.close(); #ifdef DGDEBUG std::cout << "starting sort" << std::endl; #endif std::sort(iplist.begin(), iplist.end()); #ifdef DGDEBUG std::cout << "sort complete" << std::endl; std::cout << "ip list dump:" << std::endl; std::vector<ip>::const_iterator i = iplist.begin(); while (i != iplist.end()) { std::cout << "IP: " << i->addr << " Group: " << i->group << std::endl; ++i; } std::cout << "subnet list dump:" << std::endl; std::list<subnetstruct>::const_iterator j = ipsubnetlist.begin(); while (j != ipsubnetlist.end()) { std::cout << "Masked IP: " << j->maskedaddr << " Mask: " << j->mask << " Group: " << j->group << std::endl; ++j; } std::cout << "range list dump:" << std::endl; std::list<rangestruct>::const_iterator k = iprangelist.begin(); while (k != iprangelist.end()) { std::cout << "Start IP: " << k->startaddr << " End IP: " << k->endaddr << " Group: " << k->group << std::endl; ++k; } #endif // return either warning or success return warn ? 1 : 0; }