Mercurial > hg > waaad
view extensions/radius_gw/rg_common.h @ 388:1a4902b216f8
Improved initial handling of RADIUS messages
author | Sebastien Decugis <sdecugis@nict.go.jp> |
---|---|
date | Fri, 29 May 2009 12:57:43 +0900 |
parents | 03b512313cc1 |
children | 3a8e91184d4d |
line wrap: on
line source
/********************************************************************************************************* * Software License Agreement (BSD License) * * Author: Sebastien Decugis <sdecugis@nict.go.jp> * * * * Copyright (c) 2009, WIDE Project and NICT * * All rights reserved. * * * * Redistribution and use of this software in source and binary forms, with or without modification, are * * permitted provided that the following conditions are met: * * * * * Redistributions of source code must retain the above * * copyright notice, this list of conditions and the * * following disclaimer. * * * * * Redistributions in binary form must reproduce the above * * copyright notice, this list of conditions and the * * following disclaimer in the documentation and/or other * * materials provided with the distribution. * * * * * Neither the name of the WIDE Project or NICT nor the * * names of its contributors may be used to endorse or * * promote products derived from this software without * * specific prior written permission of WIDE Project and * * NICT. * * * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * *********************************************************************************************************/ /* This file contains definitions for both radius_gw extension and subextensions. */ #ifndef _RG_COMMON_H #define _RG_COMMON_H /* Some headers that are useful in many files */ #include <pthread.h> #include <string.h> #include <stdlib.h> #include <errno.h> #include <stdint.h> #include <dlfcn.h> #include <sys/socket.h> #include <netdb.h> /* This should be overwritten before including this file */ #ifndef DEFINE_DEBUG_MACRO # define DEFINE_DEBUG_MACRO rg_common # ifndef RG_COMMON_VERBO # define RG_COMMON_VERBO 0 # endif /* RG_COMMON_VERBO */ static int rg_common_verbosity = RG_COMMON_VERBO; #endif /* DEFINE_DEBUG_MACRO */ /* Include the waaad API definition */ #define IN_EXTENSION #include <waaad/waaad.h> /* Include host-specific defines */ #include "rg_host.h" /* Include hostap files for RADIUS processings */ #include "hostap_compat.h" #include "md5.h" #include "radius.h" /* Include the radius_gw API definition */ #include "rg_api.h" /* Type of message / server */ #define RGW_EXT_TYPE_AUTH 1 #define RGW_EXT_TYPE_ACCT 2 /**************************************************************/ /* Functions exported by the common library extension */ /**************************************************************/ /* List management */ struct rg_list { struct rg_list *next; struct rg_list *prev; struct rg_list *head; }; void rg_list_init(struct rg_list * plist); int rg_list_is_empty(struct rg_list * plist); void rg_list_insert_after(struct rg_list * ref, struct rg_list * item); void rg_list_insert_before(struct rg_list * ref, struct rg_list * item); void rg_list_unlink(struct rg_list * plist); /* Others */ int rg_thread_term(pthread_t * th); void rg_cleanup_mutex(void * mtx); int rg_pointers_init(void ** hdl); void rg_pointers_fini(void **hdl); #define rg_pointers_resolve( ptr, hdl, fct, ret ) { \ ptr = dlsym(hdl, #fct); \ if (!ptr) { \ TRACE_DEBUG(INFO, "Error in dlsym(" #fct "): %s", dlerror()); \ return ret; \ } \ } /****************************************/ /* Debug and related stuff */ /****************************************/ #include <assert.h> #ifndef ASSERT #define ASSERT(x) assert(x) #endif /* ASSERT */ /* CHECK_* macro from the daemon */ /* Helper for tracing the CHECK_* macros bellow */ #define TRACE_DEBUG_ALL( str ) \ TRACE_DEBUG(CALL, str ); /* Macros to check a return value and branch out in case of error. * These macro are to be used only when errors are highly improbable, not for expected errors. */ /* Check the return value of a system function and execute fallback in case of error */ #define CHECK_SYS_DO( __call__, __fallback__ ) { \ int __ret__; \ TRACE_DEBUG_ALL( "Check SYS: " #__call__ ); \ __ret__ = (__call__); \ if (__ret__ < 0) { \ int __err__ = errno; /* We may handle EINTR here */ \ log_error("An unexpected error occured (%s), turn on debug for detail\n",\ strerror(__err__)); \ TRACE_DEBUG(INFO, "Error in '" #__call__ "': %s", strerror(__err__)); \ __fallback__; \ } \ } /* Check the return value of a system function, return error code on error */ #define CHECK_SYS( __call__ ) { \ int __ret__; \ TRACE_DEBUG_ALL( "Check SYS: " #__call__ ); \ __ret__ = (__call__); \ if (__ret__ < 0) { \ int __err__ = errno; /* We may handle EINTR here */ \ log_error("An unexpected error occured (%s), turn on debug for detail\n",\ strerror(__err__)); \ TRACE_DEBUG(INFO, "Error in '" #__call__ "': %s", strerror(__err__)); \ return __err__; \ } \ } /* Check the return value of a POSIX function and execute fallback in case of error or special value */ #define CHECK_POSIX_DO2( __call__, __val__, __fallback1__, __fallback2__ ) { \ int __ret__; \ TRACE_DEBUG_ALL( "Check POSIX: " #__call__ ); \ __ret__ = (__call__); \ if (__ret__ != 0) { \ if (__ret__ == (__val__)) { \ __fallback1__; \ } else { \ log_error("An unexpected error, see log for detail\n"); \ TRACE_DEBUG(INFO, "Error in '" #__call__ "': %s", strerror(__ret__)); \ __fallback2__; \ } \ } \ } /* Check the return value of a POSIX function and execute fallback in case of error */ #define CHECK_POSIX_DO( __call__, __fallback__ ) \ CHECK_POSIX_DO2( (__call__), 0, , __fallback__ ); /* Check the return value of a POSIX function and return it if error */ #define CHECK_POSIX( __call__ ) { \ int __v__; \ CHECK_POSIX_DO( __v__ = (__call__), return __v__ ); \ } /* Check that a memory allocator did not return NULL, otherwise log an error and execute fallback */ #define CHECK_MALLOC_DO( __call__, __fallback__ ) { \ void * __ret__; \ TRACE_DEBUG_ALL( "Check MALLOC: " #__call__ ); \ __ret__ = (void *)( __call__ ); \ if (__ret__ == NULL) { \ int __err__ = errno; \ log_error("Memory allocation failed: %s\n", strerror(__err__)); \ TRACE_DEBUG(INFO, "Error in '" #__call__ "': %s", strerror(__err__)); \ __fallback__; \ } \ } /* Check that a memory allocator did not return NULL, otherwise return ENOMEM */ #define CHECK_MALLOC( __call__ ) \ CHECK_MALLOC_DO( __call__, return ENOMEM ); /* The next functions can be used also for expected errors */ /* Check parameters at function entry, execute fallback on error */ #define CHECK_PARAMS_DO( __bool__, __fallback__ ) \ TRACE_DEBUG_ALL( "Check PARAMS: " #__bool__ ); \ if ( ! (__bool__) ) { \ TRACE_DEBUG(INFO, "Invalid parameter received in " #__bool__ ); \ __fallback__; \ } /* Check parameters at function entry, return EINVAL if the boolean is false (similar to assert) */ #define CHECK_PARAMS( __bool__ ) \ CHECK_PARAMS_DO( __bool__, return EINVAL ); /* Check the return value of an internal function, log and propagate */ #define CHECK_FCT_DO( __call__, __fallback__ ) { \ int __ret__; \ TRACE_DEBUG_ALL( "Check FCT: " #__call__ ); \ __ret__ = (__call__); \ if (__ret__ != 0) { \ TRACE_DEBUG(INFO, "Error in '" #__call__ "': %s", strerror(__ret__)); \ __fallback__; \ } \ } /* Check the return value of a function call, return any error code */ #define CHECK_FCT( __call__ ) { \ int __v__; \ CHECK_FCT_DO( __v__ = (__call__), return __v__ ); \ } #endif /* _RG_COMMON_H */