utilities.c

Go to the documentation of this file.
00001 /* 
00002  * $Id: utilities.c 5299 2008-12-04 18:12:33Z 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, utilities
00033  *
00034  * This file was created to group together utility functions that were useful
00035  * throughout the SNMPStats module, without belonging to any file in particular.
00036  * \ingroup snmpstats
00037  * - Module: \ref snmpstats
00038  */
00039 
00040 #include <stdlib.h>
00041 #include <string.h>
00042 
00043 #include "utilities.h"
00044 
00045 #include "../../str.h"
00046 #include "../../locking.h"
00047 #include "../../mem/mem.h"
00048 
00049 /*!
00050  * This function copies an OpenSER "str" datatype into a '\\0' terminated char*
00051  * string. 
00052  *
00053  * \note Make sure to free the memory allocated to *copiedString, when you no
00054  *       longer have any use for it. (It is allocated with shm_malloc(), so make
00055  *       sure to deallocate it with shm_free()) 
00056  */
00057 int convertStrToCharString(str *strToConvert, char **copiedString) 
00058 {
00059    /* We want enough space for the string, plus 1 for the '\0' character. */
00060    *copiedString = shm_malloc(sizeof(char) * (strToConvert->len + 1));
00061 
00062    if (*copiedString == NULL)
00063    {
00064       return 0;
00065    }
00066 
00067    memcpy(*copiedString, strToConvert->s, strToConvert->len);
00068    (*copiedString)[strToConvert->len] = '\0';
00069 
00070    return 1;
00071 }
00072 
00073 
00074 /*! Silently returns 1 if the supplied parameters are sane.  Otherwise, an error
00075  * message is logged for parameterName, and 0 returned. */
00076 int stringHandlerSanityCheck( modparam_t type, void *val, char *parameterName) 
00077 {
00078    char *theString = (char *)val;
00079 
00080    /* Make sure the function was called correctly. */
00081    if (type != STR_PARAM) {
00082       LM_ERR("the %s parameter was assigned a type %d instead of %d\n",
00083             parameterName, type, STR_PARAM);
00084       return 0;
00085    }
00086 
00087    /* An empty string was supplied.  We consider this illegal */
00088    if (theString==0 || (theString[0])==0) {
00089       LM_ERR("the %s parameter was specified  with an empty string\n",
00090             parameterName); 
00091       return 0;
00092    }
00093 
00094    return 1;
00095 }
00096 
00097 
00098 
00099 /*!
00100  * This function is a wrapper around the standard statistic framework.  It will
00101  * return the value of the statistic denoted with statName, or zero if the
00102  * statistic was not found. 
00103  */
00104 int get_statistic(char *statName) 
00105 {
00106    int result = 0;
00107 
00108    str theStr;
00109 
00110    theStr.s   = statName;
00111    theStr.len = strlen(statName);
00112 
00113    stat_var *theVar = get_stat(&theStr);
00114    
00115    if (theVar==0) {
00116       LM_INFO("failed to retrieve statistics for %s\n", statName);
00117    } else {
00118       result = get_stat_val(theVar);
00119    }
00120 
00121    return result;
00122 }
00123 
00124 /*! Returns a pointer to an SNMP DateAndTime OCTET STRING representation of the
00125  * time structure.  Note that the pointer is to static data, so it shouldn't be
00126  * counted on to be around if this function is called again. */
00127 char * convertTMToSNMPDateAndTime(struct tm *timeStructure) 
00128 {
00129    static char dateAndTime[8];
00130 
00131    /* The tm structure stores the number of years since 1900.  We need to
00132     * change the offset. */
00133    int currentYear = timeStructure->tm_year + 1900;
00134 
00135    /* See SNMPv2-TC for the conversion details */
00136    dateAndTime[0] = (char) ((currentYear & 0xFF00) >> 8);
00137    dateAndTime[1] = (char)  currentYear & 0xFF;
00138    dateAndTime[2] = (char) timeStructure->tm_mon + 1;
00139    dateAndTime[3] = (char) timeStructure->tm_mday;
00140    dateAndTime[4] = (char) timeStructure->tm_hour;
00141    dateAndTime[5] = (char) timeStructure->tm_min;
00142    dateAndTime[6] = (char) timeStructure->tm_sec;
00143    dateAndTime[7] = 0;
00144 
00145    return dateAndTime;
00146 }

Generated on Thu May 24 22:00:35 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6