|
Revision 1985, 1.6 KB
(checked in by ahu, 2 years ago)
|
|
namespaces.hh includes
|
-
Property svn:eol-style set to
native
-
Property svn:keywords set to
author date id revision
|
| Line | |
|---|
| 1 | /* ippreftree.hh |
|---|
| 2 | * Copyright (C) 2004 Mark Bergsma <mark@nedworks.org> |
|---|
| 3 | * This software is licensed under the terms of the GPL, version 2. |
|---|
| 4 | * |
|---|
| 5 | * $Id$ |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | #include <string> |
|---|
| 9 | #include <sys/types.h> |
|---|
| 10 | #include <cstdlib> |
|---|
| 11 | #include <stdint.h> |
|---|
| 12 | |
|---|
| 13 | #include "pdns/namespaces.hh" |
|---|
| 14 | |
|---|
| 15 | // Use old style C structs for efficiency |
|---|
| 16 | typedef struct node_t { |
|---|
| 17 | node_t *child[2]; |
|---|
| 18 | short value; |
|---|
| 19 | } node_t; |
|---|
| 20 | |
|---|
| 21 | class IPPrefTree{ |
|---|
| 22 | |
|---|
| 23 | public: |
|---|
| 24 | IPPrefTree(); |
|---|
| 25 | ~IPPrefTree(); |
|---|
| 26 | |
|---|
| 27 | void add(const string &prefix, const short value); |
|---|
| 28 | void add(const uint32_t ip, const int preflen, const short value); |
|---|
| 29 | |
|---|
| 30 | short lookup(const string &prefix) const; |
|---|
| 31 | short lookup(const uint32_t ip, const int preflen) const; |
|---|
| 32 | |
|---|
| 33 | void clear(); |
|---|
| 34 | |
|---|
| 35 | int getNodeCount() const; |
|---|
| 36 | int getMemoryUsage() const; |
|---|
| 37 | |
|---|
| 38 | private: |
|---|
| 39 | node_t *root; // root of the tree |
|---|
| 40 | int nodecount; // total number of nodes in the tree |
|---|
| 41 | |
|---|
| 42 | void addNode(node_t * node, const uint32_t ip, const uint32_t mask, const short value); |
|---|
| 43 | node_t * allocateNode(); |
|---|
| 44 | const node_t * findDeepestFilledNode(const node_t *root, const uint32_t ip, const uint32_t mask) const; |
|---|
| 45 | void removeNode(node_t * node); |
|---|
| 46 | |
|---|
| 47 | inline uint32_t preflenToNetmask(const int preflen) const; |
|---|
| 48 | inline void parsePrefix(const string &prefix, uint32_t &ip, int &preflen) const; |
|---|
| 49 | }; |
|---|
| 50 | |
|---|
| 51 | class ParsePrefixException |
|---|
| 52 | { |
|---|
| 53 | public: |
|---|
| 54 | ParsePrefixException() { reason = ""; }; |
|---|
| 55 | ParsePrefixException(string r) { reason = r; }; |
|---|
| 56 | |
|---|
| 57 | string reason; |
|---|
| 58 | }; |
|---|