Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

hi_ui_server_lookup.c

Go to the documentation of this file.
00001 /**
00002 **  @file       hi_ui_server_lookup.c
00003 **
00004 **  @author     Daniel Roelker <droelker@sourcefire.com>
00005 **
00006 **  @brief      This file contains functions to access the SERVER_LOOKUP
00007 **              structure.
00008 **
00009 **  We wrap the access to SERVER_LOOKUP so changing the lookup algorithms
00010 **  are more modular and independent.  This is the only file that would need
00011 **  to be changed to change the algorithmic lookup.
00012 **
00013 **  NOTES:
00014 **
00015 **  - 2.24.03:  Initial Develpoment. DJR
00016 */
00017 #include <stdlib.h>
00018 #include <stdio.h>
00019 #include <string.h>
00020 
00021 #include "hi_util_xmalloc.h"
00022 #include "hi_util_kmap.h"
00023 #include "hi_ui_config.h"
00024 #include "hi_return_codes.h"
00025 
00026 /*
00027 **  NAME
00028 **    hi_ui_server_lookup_init::
00029 */
00030 /**
00031 **  Initialize the server_lookup structure.
00032 **
00033 **  We need to initialize the server_lookup structure for the server
00034 **  configuration access.  Don't want a NULL pointer flying around, when
00035 **  we have to look for server configs.
00036 **
00037 **  @param ServerLookup pointer to the pointer of the server lookup structure.
00038 **
00039 **  @return integer
00040 **
00041 **  @retval HI_SUCCESS function successful.
00042 **  @retval HI_MEM_ALLOC_FAIL memory allocation failed
00043 */
00044 int hi_ui_server_lookup_init(SERVER_LOOKUP **ServerLookup)
00045 {
00046     *ServerLookup = KMapNew(NULL); 
00047     if(*ServerLookup == NULL)
00048     {
00049         return HI_MEM_ALLOC_FAIL;
00050     }
00051 
00052     return HI_SUCCESS;
00053 }
00054 
00055 /*
00056 **  NAME
00057 **    hi_ui_server_lookup_add::
00058 */
00059 /**
00060 **  Add a server configuration to the list.
00061 **
00062 **  We add these keys like you would normally think to add them, because
00063 **  on low endian machines the least significant byte is compared first.
00064 **  This is what we want to compare IPs backward, doesn't work on high
00065 **  endian machines, but oh well.  Our platform is Intel.
00066 **
00067 **  @param ServerLookup a pointer to the lookup structure
00068 **  @param Ip           the IP address of the server (the key)
00069 **  @param ServerConf   a pointer to the server configuration
00070 **
00071 **  @return integer
00072 **
00073 **  @retval HI_SUCCESS        function successful
00074 **  @retval HI_INVALID_ARG    invalid argument, most likely NULL pointer 
00075 **  @retval HI_MEM_ALLOC_FAIL memory allocation failed 
00076 **  @retval HI_NONFATAL_ERR   key is already in table, don't overwrite
00077 **                            configuration.
00078 */
00079 int hi_ui_server_lookup_add(SERVER_LOOKUP *ServerLookup, unsigned long Ip,
00080                             HTTPINSPECT_CONF *ServerConf)
00081 {
00082     int iRet;
00083 
00084     if(!ServerLookup || !ServerConf)
00085     {
00086         return HI_INVALID_ARG;
00087     }
00088 
00089     if((iRet = KMapAdd(ServerLookup, (void *)&Ip, 4, (void *)ServerConf)))
00090     {
00091         /*
00092         **  This means the key has already been added.
00093         */
00094         if(iRet == 1)
00095         {
00096             return HI_NONFATAL_ERR;
00097         }
00098         else
00099         {
00100             return HI_MEM_ALLOC_FAIL;
00101         }
00102     }
00103 
00104     return HI_SUCCESS;
00105 }
00106 
00107 /*
00108 **  NAME
00109 **    hi_ui_server_lookup_find::
00110 */
00111 /**
00112 **  Find a server configuration given a IP.
00113 **
00114 **  We look up a server configuration given an IP and return a pointer
00115 **  to that server configuration if found.
00116 **
00117 **  @param ServerLookup pointer to the server lookup structure
00118 **  @param Ip           the IP to lookup
00119 **  @param iError       the error return code
00120 **
00121 **  @return integer
00122 **
00123 **  @retval HI_SUCCESS function sucessful
00124 **  @retval HI_INVALID_ARG argument(s) are invalid
00125 **  @retval HI_NOT_FOUND IP not found
00126 */
00127 HTTPINSPECT_CONF  *hi_ui_server_lookup_find(SERVER_LOOKUP *ServerLookup, 
00128                                             unsigned long Ip, int *iError)
00129 {
00130     HTTPINSPECT_CONF *ServerConf;
00131 
00132     if(!iError)
00133     {
00134         return NULL;
00135     }
00136 
00137     if(!ServerLookup)
00138     {
00139         *iError = HI_INVALID_ARG;
00140         return NULL;
00141     }
00142 
00143     *iError = HI_SUCCESS;
00144 
00145     if(!(ServerConf = (HTTPINSPECT_CONF *)KMapFind(ServerLookup,(void *)&Ip,4)))
00146     {
00147         *iError = HI_NOT_FOUND;
00148     }
00149 
00150     return ServerConf;
00151 }
00152 
00153 /*
00154 **  NAME
00155 **    hi_ui_server_lookup_first::
00156 */
00157 /**
00158 **  This lookups the first server configuration, so we can iterate
00159 **  through the configurations.
00160 **
00161 **  @param ServerLookup pointer to the server lookup structure
00162 **  @param iError       pointer to the integer to set for errors
00163 **
00164 **  @return integer
00165 **
00166 **  @retval HI_INVALID_ARG invalid argument
00167 **  @retval HI_NOT_FOUND   configuration not found (no first config)
00168 **  @retval HI_SUCCESS     function successful
00169 */
00170 HTTPINSPECT_CONF *hi_ui_server_lookup_first(SERVER_LOOKUP *ServerLookup,
00171                                             int *iError)
00172 {
00173     HTTPINSPECT_CONF *ServerConf;
00174 
00175     if(!iError)
00176     {
00177         return NULL;
00178     }
00179 
00180     if(!ServerLookup)
00181     {
00182         *iError = HI_INVALID_ARG;
00183         return NULL;
00184     }
00185 
00186     *iError = HI_SUCCESS;
00187 
00188     if(!(ServerConf = (HTTPINSPECT_CONF *)KMapFindFirst(ServerLookup)))
00189     {
00190         *iError = HI_NOT_FOUND;
00191     }
00192 
00193     return ServerConf;
00194 }
00195 
00196 /*
00197 **  NAME
00198 **    hi_ui_server_lookup_next::
00199 */
00200 /**
00201 **  Iterates to the next configuration, like a list it just returns
00202 **  the next config in the config list.
00203 **
00204 **  @param ServerLookup pointer to the server lookup structure
00205 **  @param iError       pointer to the integer to set for errors
00206 **
00207 **  @return integer
00208 **
00209 **  @retval HI_INVALID_ARG invalid argument
00210 **  @retval HI_NOT_FOUND   configuration not found (no first config)
00211 **  @retval HI_SUCCESS     function successful
00212 */
00213 HTTPINSPECT_CONF *hi_ui_server_lookup_next(SERVER_LOOKUP *ServerLookup,
00214                                            int *iError)
00215 {
00216     HTTPINSPECT_CONF *ServerConf;
00217 
00218     if(!iError)
00219     {
00220         return NULL;
00221     }
00222 
00223     if(!ServerLookup)
00224     {
00225         *iError = HI_INVALID_ARG;
00226         return NULL;
00227     }
00228 
00229     *iError = HI_SUCCESS;
00230 
00231     if(!(ServerConf = (HTTPINSPECT_CONF *)KMapFindNext(ServerLookup)))
00232     {
00233         *iError = HI_NOT_FOUND;
00234     }
00235 
00236     return ServerConf;
00237 }
00238     
00239 
00240             
00241 

Generated on Sun May 14 14:51:15 2006 by  doxygen 1.4.2