openserSIPPortTable.c

Go to the documentation of this file.
00001 /*
00002  * $Id: openserSIPPortTable.c 5769 2009-03-26 21:47:59Z osas $
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  * Originally Generated with mib2c using mib2c.array-user.conf
00030  *
00031  * This file implements the openserSIPPortTable.  For a full description of the table,
00032  * please see the OPENSER-SIP-COMMON-MIB.
00033  *
00034  */
00035 
00036 
00037 #include <net-snmp/net-snmp-config.h>
00038 #include <net-snmp/net-snmp-includes.h>
00039 #include <net-snmp/agent/net-snmp-agent-includes.h>
00040 #include <net-snmp/library/snmp_assert.h>
00041 
00042 #include "../../statistics.h"
00043 #include "../../mem/mem.h"
00044 
00045 #include "snmpstats_globals.h"
00046 #include "openserSIPPortTable.h"
00047 
00048 static     netsnmp_handler_registration *my_handler = NULL;
00049 static     netsnmp_table_array_callbacks cb;
00050 
00051 oid    openserSIPPortTable_oid[]   = { openserSIPPortTable_TABLE_OID };
00052 size_t openserSIPPortTable_oid_len = OID_LENGTH(openserSIPPortTable_oid);
00053 
00054 
00055 /* Returns a new OID with the following structure:
00056  *
00057  *    ipType.NUM_IP_OCTETS.ipAddress[0].ipAddress[1]...ipAddress[NUM_IP_OCTETS].portNumber
00058  *
00059  * sizeOfOID will be assigned the length of the oid.  
00060  * 
00061  * Note: This function returns a newly allocated block of memory.  Make sure to
00062  * deallocate the memory when you no longer need it. 
00063  */
00064 oid *createIndex(int ipType, int *ipAddress, int *sizeOfOID) 
00065 {
00066    oid *currentOIDIndex;
00067    int i;
00068 
00069    /* The size needs to be large enough such that it can store the ipType
00070     * (one octet), the prefixed length (one octet), the number of
00071     * octets to the IP Address (NUM_IP_OCTETS), and the port. */
00072    *sizeOfOID = NUM_IP_OCTETS + 3;
00073 
00074    /* Allocate space for the OID Index.  */
00075    currentOIDIndex = pkg_malloc((*sizeOfOID) * sizeof(oid));
00076 
00077    if (currentOIDIndex == NULL) {
00078       LM_ERR("failed to create a row for openserSIPPortTable\n");
00079       *sizeOfOID = 0;
00080       return NULL;
00081    }
00082 
00083    /* Assign the OID Index */
00084    currentOIDIndex[0] = ipType;
00085    currentOIDIndex[1] = NUM_IP_OCTETS;
00086       
00087    for (i = 0; i < NUM_IP_OCTETS; i++) {
00088       currentOIDIndex[i+2] = ipAddress[i];
00089    }
00090 
00091    /* Extract out the port number */
00092    currentOIDIndex[NUM_IP_OCTETS+2] = ipAddress[NUM_IP_OCTETS];
00093 
00094    return currentOIDIndex;
00095 }
00096 
00097 
00098 /* Will return an existing row indexed by the parameter list if one exists, and
00099  * return a new one otherwise.  If the row is new, then the provided index will be
00100  * assigned to the new row.
00101  *
00102  * Note: NULL will be returned on an error 
00103  */
00104 openserSIPPortTable_context *getRow(int ipType, int *ipAddress) 
00105 {
00106    int lengthOfOID;
00107    oid *currentOIDIndex = createIndex(ipType, ipAddress, &lengthOfOID);
00108 
00109    if (currentOIDIndex == NULL)
00110    {
00111       return NULL;
00112    }
00113 
00114    netsnmp_index theIndex;
00115 
00116    theIndex.oids = currentOIDIndex;
00117    theIndex.len  = lengthOfOID;
00118 
00119    openserSIPPortTable_context *rowToReturn;
00120 
00121    /* Lets check to see if there is an existing row. */
00122    rowToReturn = CONTAINER_FIND(cb.container, &theIndex);
00123    
00124    /* We found an existing row, so there is no need to create a new one.
00125     * Let's return it to the caller. */
00126    if (rowToReturn != NULL) 
00127    {
00128       /* We don't need the index we allocated anymore, because the
00129        * existing row already has its own copy, so free the memory */
00130       pkg_free(currentOIDIndex);
00131 
00132       return rowToReturn;
00133    }
00134    
00135    /* If we are here then the row doesn't exist yet.  So lets create it. */
00136    rowToReturn = SNMP_MALLOC_TYPEDEF(openserSIPPortTable_context);
00137 
00138    /* Not enough memory to create the new row. */
00139    if (rowToReturn == NULL) {
00140       pkg_free(currentOIDIndex);
00141       return NULL;
00142    }
00143 
00144    /* Assign the Container Index. */
00145    rowToReturn->index.len  = lengthOfOID;
00146    rowToReturn->index.oids = currentOIDIndex;
00147 
00148    memcpy(rowToReturn->openserSIPStringIndex, currentOIDIndex, NUM_IP_OCTETS + 3);
00149    rowToReturn->openserSIPStringIndex_len = NUM_IP_OCTETS + 3;
00150 
00151    /* Insert the new row into the table */
00152    CONTAINER_INSERT(cb.container, rowToReturn);
00153 
00154    return rowToReturn;
00155 }
00156 
00157 
00158 /*
00159  * Will create rows for this table from theList.  The final parameter snmpIndex
00160  * can point to any integer >= zero.  All rows created by this function will be
00161  * indexed starting at snmpIndex++.  The parameter is implemented as a pointer
00162  * to an integer so that if the function is called again with another
00163  * 'protocol', we can continue from the last index. 
00164  */
00165 void createRowsFromIPList(int *theList, int listSize, int protocol, 
00166       int *snmpIndex) {
00167 
00168    openserSIPPortTable_context *currentRow;
00169    
00170    int curIndexOfIP;
00171    int curSocketIdx;
00172    int valueToAssign;
00173 
00174    if (protocol == PROTO_UDP) 
00175    {
00176       valueToAssign = TC_TRANSPORT_PROTOCOL_UDP;
00177    }
00178    else if (protocol == PROTO_TCP) 
00179    {
00180       valueToAssign = TC_TRANSPORT_PROTOCOL_TCP;
00181    }
00182    else if (protocol == PROTO_TLS)
00183    {
00184       valueToAssign = TC_TRANSPORT_PROTOCOL_TLS;
00185    }
00186    else 
00187    {
00188       valueToAssign = TC_TRANSPORT_PROTOCOL_OTHER;
00189    }
00190    
00191    /* Create all rows with respect to the given protocol */
00192    for (curSocketIdx=0; curSocketIdx < listSize; curSocketIdx++) {
00193 
00194       curIndexOfIP   = (NUM_IP_OCTETS + 1) * curSocketIdx;
00195       
00196       /* Retrieve an existing row, or a new row if one doesn't
00197        * allready exist. */
00198       currentRow = getRow(1, &theList[curIndexOfIP]);
00199 
00200       if (currentRow == NULL) {
00201          LM_ERR("failed to create all the "
00202                "rows for the openserSIPPortTable\n");
00203          return;
00204       }
00205 
00206       currentRow->openserSIPTransportRcv[0]  |= valueToAssign;
00207       currentRow->openserSIPTransportRcv_len = 1;
00208    }
00209 }
00210 
00211 /*
00212  * Initializes the openserSIPPortTable module.  
00213  *
00214  * Specifically, this function will define the tables structure, and then
00215  * populate it with the ports and transports that OpenSER is listening on.
00216  *
00217  */
00218 void init_openserSIPPortTable(void)
00219 {
00220    int curSNMPIndex = 0;
00221 
00222    initialize_table_openserSIPPortTable();
00223 
00224    int *UDPList = NULL;
00225    int *TCPList = NULL;
00226    int *TLSList = NULL;
00227 
00228    int numUDPSockets;
00229    int numTCPSockets; 
00230    int numTLSSockets;
00231    
00232    /* Retrieve the list of the number of UDP and TCP sockets. */
00233    numUDPSockets = get_socket_list_from_proto(&UDPList, PROTO_UDP);
00234    numTCPSockets = get_socket_list_from_proto(&TCPList, PROTO_TCP);
00235    numTLSSockets = get_socket_list_from_proto(&TLSList, PROTO_TLS);
00236 
00237    /* Generate all rows, using all retrieved interfaces. */
00238    createRowsFromIPList(UDPList, numUDPSockets, PROTO_UDP, &curSNMPIndex);
00239 
00240    curSNMPIndex = 0;
00241    
00242    createRowsFromIPList(TCPList, numTCPSockets, PROTO_TCP, &curSNMPIndex);
00243 
00244    curSNMPIndex = 0;
00245    createRowsFromIPList(TLSList, numTLSSockets, PROTO_TLS, &curSNMPIndex);
00246 }
00247 
00248  
00249 /* Initialize the openserSIPPortTable table by defining how it is structured */
00250 void initialize_table_openserSIPPortTable(void)
00251 {
00252    netsnmp_table_registration_info *table_info;
00253 
00254    if(my_handler) {
00255       snmp_log(LOG_ERR, "initialize_table_openserSIPPortTable_handler"
00256             "called again\n");
00257       return;
00258    }
00259 
00260    memset(&cb, 0x00, sizeof(cb));
00261 
00262    /* create the table structure itself */
00263    table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
00264 
00265    my_handler = netsnmp_create_handler_registration("openserSIPPortTable",
00266          netsnmp_table_array_helper_handler,
00267          openserSIPPortTable_oid,
00268          openserSIPPortTable_oid_len,
00269          HANDLER_CAN_RONLY);
00270     
00271    if (!my_handler || !table_info) {
00272       snmp_log(LOG_ERR, "malloc failed in "
00273           "initialize_table_openserSIPPortTable_handler\n");
00274       return; /** mallocs failed */
00275    }
00276 
00277    /* Set up the table's structural definition */
00278    
00279    /* index: openserSIPPortIndex */
00280    netsnmp_table_helper_add_index(table_info, ASN_OCTET_STR);
00281 
00282    table_info->min_column = openserSIPPortTable_COL_MIN;
00283    table_info->max_column = openserSIPPortTable_COL_MAX;
00284 
00285    /* register the table with the master agent */
00286    cb.get_value = openserSIPPortTable_get_value;
00287    cb.container = netsnmp_container_find("openserSIPPortTable_primary:"
00288          "openserSIPPortTable:"
00289          "table_container");
00290 
00291    
00292    DEBUGMSGTL(("initialize_table_openserSIPPortTable",
00293             "Registering table openserSIPPortTable "
00294             "as a table array\n"));
00295    
00296    netsnmp_table_container_register(my_handler, table_info, &cb,
00297          cb.container, 1);
00298 }
00299 
00300 /*
00301  * This routine is called to process get requests for elements of the table.
00302  *
00303  * The function is mostly left in its auto-generated form 
00304  */
00305 int openserSIPPortTable_get_value(netsnmp_request_info *request, 
00306       netsnmp_index *item,
00307       netsnmp_table_request_info *table_info )
00308 {
00309    netsnmp_variable_list *var = request->requestvb;
00310 
00311    openserSIPPortTable_context *context = 
00312       (openserSIPPortTable_context *)item;
00313 
00314    switch(table_info->colnum) 
00315    {
00316       
00317       case COLUMN_OPENSERSIPTRANSPORTRCV:
00318          /** OpenSERSIPTransportProtocol = ASN_OCTET_STR */
00319          snmp_set_var_typed_value(var, ASN_OCTET_STR,
00320                (unsigned char *)
00321                &context->openserSIPTransportRcv,
00322                context->openserSIPTransportRcv_len );
00323          break;
00324 
00325       default: /** We shouldn't get here */
00326          snmp_log(LOG_ERR, "unknown column in "
00327                "openserSIPPortTable_get_value\n");
00328          return SNMP_ERR_GENERR;
00329    }
00330 
00331    return SNMP_ERR_NOERROR;
00332 }
00333 
00334 /* Auto-generated function */
00335 const openserSIPPortTable_context *
00336 openserSIPPortTable_get_by_idx(netsnmp_index * hdr)
00337 {
00338    return (const openserSIPPortTable_context *)
00339       CONTAINER_FIND(cb.container, hdr );
00340 }
00341 
00342 

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