utilities.c

Go to the documentation of this file.
00001 /* 
00002  * $Id$
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 
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 #include "../../lib/kcore/kstats_wrapper.h"
00050 
00059 int convertStrToCharString(str *strToConvert, char **copiedString) 
00060 {
00061         /* We want enough space for the string, plus 1 for the '\0' character. */
00062         *copiedString = shm_malloc(sizeof(char) * (strToConvert->len + 1));
00063 
00064         if (*copiedString == NULL)
00065         {
00066                 return 0;
00067         }
00068 
00069         memcpy(*copiedString, strToConvert->s, strToConvert->len);
00070         (*copiedString)[strToConvert->len] = '\0';
00071 
00072         return 1;
00073 }
00074 
00075 
00078 int stringHandlerSanityCheck( modparam_t type, void *val, char *parameterName) 
00079 {
00080         char *theString = (char *)val;
00081 
00082         /* Make sure the function was called correctly. */
00083         if (PARAM_TYPE_MASK(type) != STR_PARAM) {
00084                 LM_ERR("the %s parameter was assigned a type %d instead of %d\n",
00085                                 parameterName, type, STR_PARAM);
00086                 return 0;
00087         }
00088 
00089         /* An empty string was supplied.  We consider this illegal */
00090         if (theString==0 || (theString[0])==0) {
00091                 LM_ERR("the %s parameter was specified  with an empty string\n",
00092                                 parameterName); 
00093                 return 0;
00094         }
00095 
00096         return 1;
00097 }
00098 
00099 
00100 
00106 int get_statistic(char *statName)
00107 {
00108         long result = 0;
00109 
00110         str theStr;
00111 
00112         theStr.s   = statName;
00113         theStr.len = strlen(statName);
00114 
00115         stat_var *theVar = get_stat(&theStr);
00116         
00117         if (theVar==0) {
00118                 LM_INFO("failed to retrieve statistics for %s\n", statName);
00119         } else {
00120                 result = get_stat_val(theVar);
00121         }
00122 
00123         return (int)result;
00124 }
00125 
00129 char * convertTMToSNMPDateAndTime(struct tm *timeStructure) 
00130 {
00131         static char dateAndTime[8];
00132 
00133         /* The tm structure stores the number of years since 1900.  We need to
00134          * change the offset. */
00135         int currentYear = timeStructure->tm_year + 1900;
00136 
00137         /* See SNMPv2-TC for the conversion details */
00138         dateAndTime[0] = (char) ((currentYear & 0xFF00) >> 8);
00139         dateAndTime[1] = (char)  currentYear & 0xFF;
00140         dateAndTime[2] = (char) timeStructure->tm_mon + 1;
00141         dateAndTime[3] = (char) timeStructure->tm_mday;
00142         dateAndTime[4] = (char) timeStructure->tm_hour;
00143         dateAndTime[5] = (char) timeStructure->tm_min;
00144         dateAndTime[6] = (char) timeStructure->tm_sec;
00145         dateAndTime[7] = 0;
00146 
00147         return dateAndTime;
00148 }