Exemple #1
0
void cidr(unsigned long first, unsigned long last) {
  if (first < last) {
    unsigned int idx1=31,idx2=0,prefix;
    while (bit(first,idx1) == bit(last,idx1)) idx1--;
    prefix=(first>>idx1+1)<<idx1+1;
    while (idx2 <= idx1 && bit(first,idx2) == 0 && bit(last,idx2) == 1) idx2++;
    if (idx2 <= idx1) {
      cidr(first,prefix|(unsigned long)mpow(2,idx1)-1);
      cidr(prefix|1<<idx1,last);
    }
    else printf("%s/%d\n",inet_ntoa(htonl(prefix)),32-idx2);
  }
Exemple #2
0
bool Entry::Matches(User *u, bool full) const
{
	/* First check if this mode has defined any matches (usually for extbans). */
	if (IRCD->IsExtbanValid(this->mask))
	{
		ChannelMode *cm = ModeManager::FindChannelModeByName(this->name);
		if (cm != NULL && cm->type == MODE_LIST)
		{
			ChannelModeList *cml = anope_dynamic_static_cast<ChannelModeList *>(cm);
			if (cml->Matches(u, this))
				return true;
		}
	}

	/* If the user's displayed host is their real host, then we can do a full match without
	 * having to worry about exposing a user's IP
	 */
	full |= u->GetDisplayedHost() == u->host;

	bool ret = true;

	if (!this->nick.empty() && !Anope::Match(u->nick, this->nick))
		ret = false;

	if (!this->user.empty() && !Anope::Match(u->GetVIdent(), this->user) && (!full || !Anope::Match(u->GetIdent(), this->user)))
		ret = false;

	if (this->cidr_len && full)
	{
		try
		{
			if (!cidr(this->host, this->cidr_len).match(u->ip))
				ret = false;
		}
		catch (const SocketException &)
		{
			ret = false;
		}
	}
	else if (!this->host.empty() && !Anope::Match(u->GetDisplayedHost(), this->host) && !Anope::Match(u->GetCloakedHost(), this->host) &&
		(!full || (!Anope::Match(u->host, this->host) && !Anope::Match(u->ip.addr(), this->host))))
		ret = false;
	
	if (!this->real.empty() && !Anope::Match(u->realname, this->real))
		ret = false;
	
	return ret;
}
Exemple #3
0
int main(int argc, char *argv[])
{
    unsigned int i = 0;
    unsigned int iSmallIP;
    unsigned int iBigIP;
    CIDR_S stCIDR[32] = {0};

    if (argc != 3) {
        printf("arg invalid\r\n");
        return -1;
    }

    iSmallIP = ntohl(inet_addr(argv[1]));
    iBigIP = ntohl(inet_addr(argv[2]));

    cidr(iSmallIP, iBigIP, stCIDR);

    for (; 0 != stCIDR[i].uiIP; i++) {
        printf("IP: %u, Mask: %u\r\n", stCIDR[i].uiIP, stCIDR[i].uiMask);
    }

    return 0;    
}
Exemple #4
0
// input host based link
// output ip based link
char *wins_resolve(char *link) {
    char *iplink = NULL;
    static char *nbtscan_outfile = NULL;
    static char *workgroup = NULL;
    static int updated_wins_file=0;

    HTML_LOG(0,"wins_resolve[%s]",link);
    if (workgroup == NULL ) {
       workgroup = setting_val("workgroup");
    }
    HTML_LOG(0,"wins_resolve workgroup[%s]",workgroup);

    if (nbtscan_outfile  == NULL ) {
        ovs_asprintf(&nbtscan_outfile,"%s/conf/wins.txt",appDir());
    }
    HTML_LOG(0,"wins_resolve nbtscan_outfile[%s]",nbtscan_outfile);

    if (!updated_wins_file && ( !exists(nbtscan_outfile) || file_age(nbtscan_outfile) > 60*60*24 ) ) {
        char *cmd;
        int c = cidr(setting_val("eth_netmask"));
        // Avoid scanning too many ips 
        // we can just scan a /21 subnet in about 25 secs.
        if (c < 21 ) c = 21; 
        ovs_asprintf(&cmd,"nbtscan -T 1 %s/%d > '%s/conf/wins.txt' && chown nmt:nmt '%s/conf/wins.txt'",
                setting_val("eth_gateway"),
                c,
                appDir(),appDir());
        if (system(cmd) == 0) {
            updated_wins_file=1;
        } else {
            HTML_LOG(0,"ERROR wins_resolve running [%s]",cmd);
        }
    }

    char *host = link + 6;
    char *hostend = strchr(host,'/');
    *hostend = '\0';


    FILE *fp = util_open(nbtscan_outfile,"r");
    if (fp) {
#define WINS_BUFSIZE 200
        char buf[WINS_BUFSIZE];

        PRE_CHECK_FGETS(buf,WINS_BUFSIZE);

        while(fgets(buf,WINS_BUFSIZE,fp)) {

            CHECK_FGETS(buf,WINS_BUFSIZE);

            HTML_LOG(0,"Check wins %s",buf);
            char *p=NULL;

#define IGNORE_WORKGROUP 1
#ifdef IGNORE_WORKGROUP
            // Ignore the workgroup
            p = strchr(buf,'\\');
            if (p != NULL) {
                p++;
            }
#else
            // Look for host in output of nbtscan (which is run by the catalog process)
            // 1.1.1.1<space>WORKGROUP\host<space>
            if ((p=delimited_substring(buf," ",workgroup,"\\",0,0)) != NULL) {
                p += 1+strlen(workgroup);
            }
#endif
            if (p) {
                if (util_starts_with_ignore_case(p,host) && p[strlen(host)] == ' ' ) {
                    // found it - get ip address from the start.
                    char *sp = strchr(buf,' ');
                    if (sp) {
                        ovs_asprintf(&iplink,"smb://%.*s/%s",sp-buf,buf,hostend+1);
                        break;
                    }
                }
            }
        }
        fclose(fp);
    }

    *hostend = '/';
    HTML_LOG(0,"New ip based link = [%s]",iplink);
    return iplink;
}
Exemple #5
0
 bool IPAddress::operator== ( const std::string& p_ip ) const {
   return cidr() == p_ip;
 }
Exemple #6
0
 bool IPAddress::operator== ( const IPAddress& p_ip ) const {
   return cidr() == p_ip.cidr();
 }
Exemple #7
0
bool IPList::ifsreadIPMelangeList(std::ifstream *input, bool checkendstring, const char *endstring)
{
    // compile regexps for determining whether a list entry is an IP, a subnet (IP + mask), or a range
    RegExp matchIP, matchSubnet, matchRange, matchCIDR;
#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}$");
    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}$");
    matchCIDR.comp("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/\\d{1,2}$");
    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}$");
    matchCIDR.comp("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/[0-9]{1,2}$");
    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
    RegResult Rre;

    // read in the file
    String line;
    char buffer[2048];
    while (input) {
        if (!input->getline(buffer, sizeof(buffer))) {
            break;
        }
        line = buffer;
        if (checkendstring && line.startsWith(endstring)) {
            break;
        }

        // ignore comments
        if (buffer[0] == '#')
            continue;
        // ignore blank lines
        if (strlen(buffer) < 7)
            continue;
#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(),Rre)) {
            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(),Rre)) {
            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 (matchCIDR.match(line.toCharArray(),Rre)) {
            struct in_addr address;
            struct in_addr addressmask;
            String subnet(line.before("/"));
            String cidr(line.after("/"));
            int m = cidr.toInteger();
            int host_part = 32 - m;
            if (host_part > -1) {
                String mask = (0xFFFFFFFF << host_part);
                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(),Rre)) {
            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);
        }
    }
#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;
}