From: Heiko Gerstung Date: Thu, 4 Dec 2008 08:08:53 +0000 (+0100) Subject: * bug 1070 fixed (call to undefined function ntpq_parsestring) X-Git-Tag: NTP_4_2_5P148~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1023fe72bf35cd9fe2a56878d071031e6ac43014;p=thirdparty%2Fntp.git * bug 1070 fixed (call to undefined function ntpq_parsestring) * 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 --- diff --git a/ntpsnmpd/ntpSnmpSubagentObject.c b/ntpsnmpd/ntpSnmpSubagentObject.c index 8e6883ae9..b4ead8834 100644 --- a/ntpsnmpd/ntpSnmpSubagentObject.c +++ b/ntpsnmpd/ntpSnmpSubagentObject.c @@ -26,9 +26,227 @@ 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 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 ); - + } diff --git a/ntpsnmpd/ntpSnmpSubagentObject.h b/ntpsnmpd/ntpSnmpSubagentObject.h index 6dcab4e38..f568c73ec 100644 --- a/ntpsnmpd/ntpSnmpSubagentObject.h +++ b/ntpsnmpd/ntpSnmpSubagentObject.h @@ -11,7 +11,10 @@ #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);