]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
* bug 1070 fixed (call to undefined function ntpq_parsestring)
authorHeiko Gerstung <gerstung@ntp.org>
Thu, 4 Dec 2008 08:08:53 +0000 (09:08 +0100)
committerHeiko Gerstung <gerstung@ntp.org>
Thu, 4 Dec 2008 08:08:53 +0000 (09:08 +0100)
* added new string handler functions for dealing with ntpd responses
* fixed handling of SoftwareName entity (unreachable code)
* added function prototypes to reduce/remove compiler warnings

bk: 49379015tD6zgvGkzS_dYeC4LD5dPw

ntpsnmpd/ntpSnmpSubagentObject.c
ntpsnmpd/ntpSnmpSubagentObject.h

index 8e6883ae9bfc4898de78eb3b0d4b07d26958bddd..b4ead8834428733a0e0f53df28b6011d184a2457 100644 (file)
 
 static int      ntpSnmpSubagentObject = 3;
 
-
 char ntpvalue[NTPQ_BUFLEN];
 
+
+
+/*****************************************************************************
+ *
+ * ntpsnmpd_strip_string
+ *
+ *  This function removes white space characters and EOL chars 
+ *  from the beginning and end of a given NULL terminated string. 
+ *  Be aware that the parameter itself is altered.
+ *  
+ ****************************************************************************
+ * Parameters:
+ *     string          char*   The name of the string variable
+ *                                             NOTE: must be NULL terminated!
+ * Returns:
+ *     int             length of resulting string (i.e. w/o white spaces)
+ ****************************************************************************/
+
+int ntpsnmpd_strip_string(char *string)
+{
+       char newstring[2048] = { 0 };
+       int i = 0;
+       int j = 0;
+
+       if ( strlen(string) > 2047 ) 
+               string[2048]=0;
+
+       j = strlen(string);
+
+       for (i=0;i<strlen(string);i++)
+       {
+               switch(string[i])
+               {
+                       case 0x09:      // Tab
+                       case 0x0A:      // LF
+                       case 0x0D:      // CR
+                       case ' ':       // Space
+                         break;
+                       default:
+                         strncpy(newstring,(char *) &string[i], sizeof(newstring));
+                         i=2048;
+                         break;
+               }
+       }
+       strncpy(string, newstring, j);
+
+       return(strlen(string));
+}
+
+
+/*****************************************************************************
+ *
+ * ntpsnmpd_parse_string
+ *
+ *  This function will parse a given NULL terminated string and cut it
+ *  into a fieldname and a value part (using the '=' as the delimiter. 
+ *  The fieldname will be converted to uppercase and all whitespace 
+ *  characters are removed from it.
+ *  The value part is stripped, e.g. all whitespace characters are removed
+ *  from the beginning and end of the string.
+ *  If the value is started and ended with quotes ("), they will be removed
+ *  and everything between the quotes is left untouched (including 
+ *  whitespace)
+ *  Example:
+ *     server host name =   hello world!
+ *  will result in a field string "SERVERHOSTNAME" and a value
+ *  of "hello world!".
+ *     My first Parameter              =               "  is this!    "
+  * results in a field string "MYFIRSTPARAMETER" and a vaue " is this!    "
+ ****************************************************************************
+ * Parameters:
+ *     src                     char*   The name of the source string variable
+ *                                             NOTE: must be NULL terminated!
+ *     field                   char*   The name of the string which takes the
+ *                                             fieldname
+ *     fieldsize               int             The maximum size of the field name
+ *     value           char*   The name of the string which takes the
+ *                                             value part
+ *     valuesize               int             The maximum size of the value string
+ *
+ * Returns:
+ *     int                     length of value string 
+ ****************************************************************************/
+
+int ntpsnmpd_parse_string(char *src, char *field, int fieldsize, char *value, int valuesize)
+{
+       char string[2048];
+       int i = 0;
+       int j = 0;
+       int l = 0;
+       int a = 0;
+
+       strncpy(string,  src, sizeof(string));
+
+       a = strlen(string);
+
+       /* Parsing the field name */
+       for (i=0;l==0;i++)
+       {
+               if (i>=a)
+                  l=1;
+               else
+               {
+                       switch(string[i])
+                       {
+                               case 0x09:      // Tab
+                               case 0x0A:      // LF
+                               case 0x0D:      // CR
+                               case ' ':       // Space
+                                 break;
+                               case '=':
+                                 l=1;
+                                 break;
+                                 
+                               default:
+                                 if ( j < fieldsize ) 
+                                 {
+                                       if ( ( string[i] >= 'a' ) && ( string[i] <='z' ) )
+                                               field[j++]=( string[i] - 32 ); // convert to Uppercase
+                                       else
+                                               field[j++]=string[i]; 
+                                 }     
+
+                       }
+               }
+       }
+
+       field[j]=0; j=0; value[0]=0;
+
+
+       /* Now parsing the value */
+       for (l=0;i<a;i++)
+       {
+               if ( ( string[i] > 0x0D ) && ( string[i] != ' ' ) )
+                  l = j+1;
+               
+               if ( ( value[0] != 0 ) || ( ( string[i] > 0x0D ) && ( string[i] != ' ' ) ) )
+               {
+                       if (j < valuesize )
+                          value[j++]=string[i];
+               }
+       }
+
+       value[l]=0;
+
+       if ( value[0]=='"' )
+               strcpy(value, (char *) &value[1]);
+
+       if ( value[strlen(value)-1] == '"' ) 
+               value[strlen(value)-1]=0;
+
+       return (strlen(value));
+
+}
+
+
+/*****************************************************************************
+ *
+ * ntpsnmpd_cut_string
+ *
+ *  This function will parse a given NULL terminated string and cut it
+ *  into fields using the specified delimiter character. 
+ *  It will then copy the requested field into a destination buffer
+ *  Example:
+ *     ntpsnmpd_cut_string(read:my:lips:fool, RESULT, ':', 2, sizeof(RESULT))
+ *  will copy "lips" to RESULT.
+ ****************************************************************************
+ * Parameters:
+ *     src                     char*   The name of the source string variable
+ *                                             NOTE: must be NULL terminated!
+ *     dest                    char*   The name of the string which takes the
+ *                                             requested field content
+ *     delim                   char    The delimiter character
+ *     fieldnumber             int             The number of the required field
+ *                                             (start counting with 0)
+ *     maxsize                 int             The maximum size of dest
+ *
+ * Returns:
+ *     int                     length of resulting dest string 
+ ****************************************************************************/
+
+int ntpsnmpd_cut_string(char *src, char *dest, const char delim, int fieldnumber, int maxsize)
+{
+       char string[2048];
+       int i = 0;
+       int j = 0;
+       int l = 0;
+       int a = 0;
+
+       strncpy (string, src, sizeof(string));
+       
+       a = strlen(string);
+
+        memset (dest, 0, maxsize);
+
+       /* Parsing the field name */
+       for (i=0;l<=fieldnumber;i++)
+       {
+               if (i>=a)
+                  l=fieldnumber+1; /* terminate loop */
+               else
+               {
+                       if ( string[i] == delim )
+                       {
+                                 l++; /* next field */
+                       }
+                       else  if ( ( l == fieldnumber) && ( j < maxsize )  )
+                       {
+                               dest[j++]=string[i]; 
+                       }       
+
+               }
+       }
+
+       return (strlen(dest));
+
+}
+
+
 /*****************************************************************************
  *
  *  read_ntp_value
@@ -91,17 +309,15 @@ int get_ntpEntSoftwareName (netsnmp_mib_handler *handler,
    case MODE_GET:
    {
        if ( read_ntp_value("product", ntpvalue, NTPQ_BUFLEN) )
-    {
+       {
        snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                              (u_char *)ntpvalue,
                              strlen(ntpvalue)
                             );
-    }
-    break;
-
-    if ( read_ntp_value("version", ntpvalue, NTPQ_BUFLEN) )
+       } 
+    else  if ( read_ntp_value("version", ntpvalue, NTPQ_BUFLEN) )
     {
-       ntpq_parsestring(ntp_softwarename, ntpvalue, strlen(ntpvalue), NTPQ_BUFLEN, 'd', 1);
+       ntpsnmpd_cut_string(ntpvalue, ntp_softwarename, ' ', 0, sizeof(ntp_softwarename)-1);
        snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
                              (u_char *)ntp_softwarename,
                              strlen(ntp_softwarename)
@@ -485,6 +701,6 @@ init_ntpSnmpSubagentObject(void)
   _SETUP_OID_RO( ntpEntTimePrecision ,         NTPV4_OID , 1, 8, 0  );
   _SETUP_OID_RO( ntpEntTimePrecisionVal ,      NTPV4_OID , 1, 9, 0  );
   _SETUP_OID_RO( ntpEntTimeDistance ,                  NTPV4_OID , 1,10, 0  );
-       
+
 }
 
index 6dcab4e38230f8900e047e0cd25cdf08fa28cdb1..f568c73ecf88dab7f9b495c7fc22378cdc6b8c28 100644 (file)
 #define NTPSNMPSUBAGENTOBJECT_H
 
 /* Function Prototypes */
-
+int ntpsnmpd_strip_string(char *string);
+int ntpsnmpd_parse_string(char *src, char *field, int fieldsize, char *value, int valuesize);
+int ntpsnmpd_cut_string(char *src, char *dest, const char delim, int fieldnumber, int maxsize);
+unsigned int read_ntp_value(char *variable, char *rbuffer, unsigned int maxlength);
 
 /* Initialization */
 void            init_ntpSnmpSubagentObject(void);