00001 /* 00002 * $Id: hashTable.h 4764 2008-08-28 14:41:06Z henningw $ 00003 * 00004 * SNMPStats Module 00005 * Copyright (C) 2006 SOMA Networks, INC. 00006 * Written by: Jeffrey Magder (jmagder@somanetworks.com) 00007 * 00008 * This file is part of Kamailio, a free SIP server. 00009 * 00010 * Kamailio is free software; you can redistribute it and/or modify it 00011 * under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version 00014 * 00015 * Kamailio is distributed in the hope that it will be useful, but 00016 * WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU General Public License 00021 * along with this program; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00023 * USA 00024 * 00025 * History: 00026 * -------- 00027 * 2006-11-23 initial version (jmagder) 00028 */ 00029 00030 /*! 00031 * \file 00032 * \brief SNMP statistic module, hash table 00033 * 00034 * This file describes several structure. In general, it was necessary to map 00035 * between OpenSER's "aor" (Address of Record) and string indexing mechanisms, 00036 * and the SNMPStats modules integer indexing scheme for users and contacts. 00037 * While it would have been a more natural fit to use string indexes in the 00038 * SNMPStats module, SNMP limitations precluded this. 00039 * 00040 * aorToIndexStruct: maps an aor to: 00041 * - a userIndex, to uniquely identify each RegUserTable SNMP row 00042 * - a contactList, containing all contacts for the user specified by 00043 * userIndex. 00044 * 00045 * The aorToIndexStruct also contains a numContacts counter. Each time a new 00046 * contact is associated with the aor (user), the counter is incremented. Each 00047 * time a contact is dissasociated (due to an expiration), the counter is 00048 * decremented. When the counter reaches zero the structure will be deleted. 00049 * 00050 * contactToIndexStruct: maps a contact name to: 00051 * - a contactIndex, used to uniquely identify each ContactTable SNMP row. 00052 * \ingroup snmpstats 00053 * - Module: \ref snmpstats 00054 */ 00055 00056 00057 #ifndef HASHSLOT_H 00058 #define HASHSLOT_H 00059 00060 /*! 00061 * Used to map between a 'contact' name (OpenSER's index) and a contact index. 00062 * (SNMPStats Index) 00063 */ 00064 typedef struct contactToIndexStruct 00065 { 00066 char *contactName; 00067 00068 int contactIndex; 00069 00070 struct contactToIndexStruct *next; 00071 00072 } contactToIndexStruct_t; 00073 00074 00075 /*! 00076 * Used to map between an 'aor' (OpenSER index) and a user index. (SNMPStats 00077 * index). Since each user can have multiple contacts, the structure also has a 00078 * 'contactIndex', and a reference to the contactToIndexStruct list. 00079 */ 00080 typedef struct aorToIndexStruct 00081 { 00082 /* Pointer to the actual address record in the given SNMP row. */ 00083 char *aor; 00084 int aorLength; 00085 00086 /* Points to the user index, which is used to uniquely identify each 00087 * SNMP row in a table. */ 00088 int userIndex; 00089 00090 /* Each contact needs a unique index, for each user. This value should 00091 * be incremented each time a contact is added. This way, we can know 00092 * what index to use for the next addition to the contactList. */ 00093 int contactIndex; 00094 00095 /* Pointer to the contact list. */ 00096 contactToIndexStruct_t *contactList; 00097 00098 struct aorToIndexStruct *prev; 00099 00100 /* The structure is part of a hash table, so this element is needed so 00101 * that we can point to the next element in the colission slot. */ 00102 struct aorToIndexStruct *next; 00103 00104 /* This counter will be incremented when a new contact is associated 00105 * with this user record, and will be decremented each time an 00106 * associated contact is removed. When the count reaches 0, it is safe 00107 * to remove this record. */ 00108 int numContacts; 00109 00110 } aorToIndexStruct_t; 00111 00112 00113 typedef struct hashSlot 00114 { 00115 /*! Number of elements in this list. */ 00116 int numberOfElements; 00117 00118 /*! First element in the list. */ 00119 struct aorToIndexStruct* first; 00120 00121 /*! Last element in the list. This is here for optimization purposes. 00122 * It stands to reason that things added later will need to be deleted 00123 * later. So they should be added to the end of the list. This way, 00124 * things that are to be deleted sooner will be at the front of the 00125 * list. */ 00126 struct aorToIndexStruct* last; 00127 00128 } hashSlot_t; 00129 00130 /******************************************************************* 00131 * More detailed function definitions can be found in hashTable.c */ 00132 00133 00134 /*! Returns a aorToIndexStruct_t, holding the given 'userIndex' and 'aor'. The 00135 * structure is used to map between the "aor" (OpenSER's way of indexing 00136 * users/contacts), and the SNMPStats user and contact integer indexes. 00137 * 00138 * \note This record does not make a copy of aor, but instead points 00139 * directly to the parameter. Therefore make sure that aor is not on the stack, 00140 * and is not going to disappear before this record is deleted. 00141 */ 00142 aorToIndexStruct_t *createHashRecord(int userIndex, char *aor); 00143 00144 00145 /*! Returns a chunk of memory large enough to store 'size' hashSlot's. The 00146 * table will contain mappings between OpenSER's "aor" user/contact indexing 00147 * scheme, and SNMPStats integer indexing scheme */ 00148 hashSlot_t *createHashTable(int size); 00149 00150 00151 /*! Calculates and returns a hash index to a hash table. The index is calculated 00152 * by summing up all the characters specified with theString, and using the 00153 * hashTableSize as the modulus. */ 00154 int calculateHashSlot(char *theString, int hashTableSize); 00155 00156 00157 /*! Searches the hash table specified as theTable, of size 'size', for a record 00158 * indexed with 'aor'. If a match is found, then an aorToIndextStruct_t 00159 * structure is returned. 00160 * 00161 * This function is called to discover the map between OpenSER's "aor" 00162 * (Address of Records) indexing scheme, and the SNMPStats modules integer 00163 * indexing scheme for its contact/user data. 00164 * 00165 * Returns: the aorToIndexStruct_t mapping structure if a match was found, 00166 * or NULL otherwise. 00167 */ 00168 aorToIndexStruct_t *findHashRecord(hashSlot_t *theTable, char *aor, int size); 00169 00170 00171 /*! Inserts theRecord into an appropriate place in theTable, when size is given. */ 00172 void insertHashRecord(hashSlot_t *theTable, aorToIndexStruct_t *theRecord, int size); 00173 00174 00175 /*! Debugging function. Prints off an entire hash slot. */ 00176 void printHashSlot(hashSlot_t *theTable, int index); 00177 00178 00179 /*! If a record is found with string aor in theTable, it is deleted and its 00180 * SNMPStats user integer index is returned. */ 00181 int deleteHashRecord(hashSlot_t *theTable, char *aor, int hashTableSize); 00182 00183 /*! 00184 * This function will search the provided hash table for an entry indexed by 00185 * 'aor'. If an entry is found then: 00186 * 00187 * - Its numContacts counter will be decremented. 00188 * - If its numContacts counter reaches zero, then the entry will be removed 00189 * from the hash table. 00190 * 00191 */ 00192 void deleteUser(hashSlot_t *theTable, char *aor, int hashTableSize); 00193 00194 #endif
1.5.6