| 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 | |
|---|
| 12 | using namespace std; |
|---|
| 13 | |
|---|
| 14 | // Use old style C structs for efficiency |
|---|
| 15 | typedef struct node_t { |
|---|
| 16 | node_t *child[2]; |
|---|
| 17 | short value; |
|---|
| 18 | } node_t; |
|---|
| 19 | |
|---|
| 20 | class IPPrefTree{ |
|---|
| 21 | |
|---|
| 22 | public: |
|---|
| 23 | IPPrefTree(); |
|---|
| 24 | ~IPPrefTree(); |
|---|
| 25 | |
|---|
| 26 | void add(const string &prefix, const short value); |
|---|
| 27 | void add(const uint32_t ip, const int preflen, const short value); |
|---|
| 28 | |
|---|
| 29 | short lookup(const string &prefix) const; |
|---|
| 30 | short lookup(const uint32_t ip, const int preflen) const; |
|---|
| 31 | |
|---|
| 32 | void clear(); |
|---|
| 33 | |
|---|
| 34 | int getNodeCount() const; |
|---|
| 35 | int getMemoryUsage() const; |
|---|
| 36 | |
|---|
| 37 | private: |
|---|
| 38 | node_t *root; // root of the tree |
|---|
| 39 | int nodecount; // total number of nodes in the tree |
|---|
| 40 | |
|---|
| 41 | void addNode(node_t * node, const uint32_t ip, const uint32_t mask, const short value); |
|---|
| 42 | node_t * allocateNode(); |
|---|
| 43 | const node_t * IPPrefTree::findDeepestFilledNode(const node_t *root, const uint32_t ip, const uint32_t mask) const; |
|---|
| 44 | void removeNode(node_t * node); |
|---|
| 45 | |
|---|
| 46 | inline uint32_t preflenToNetmask(const int preflen) const; |
|---|
| 47 | inline void parsePrefix(const string &prefix, uint32_t &ip, int &preflen) const; |
|---|
| 48 | }; |
|---|
| 49 | |
|---|
| 50 | class ParsePrefixException |
|---|
| 51 | { |
|---|
| 52 | public: |
|---|
| 53 | ParsePrefixException() { reason = ""; }; |
|---|
| 54 | ParsePrefixException(string r) { reason = r; }; |
|---|
| 55 | |
|---|
| 56 | string reason; |
|---|
| 57 | }; |
|---|