📄 read_config.c
字号:
DEBUGMSGTL(("read_config_copy_word", "no end quote found in config string\n")); } else from++; } else { while (*from != 0 && !isspace(*from)) { if ((*from == '\\') && (*(from + 1) != 0)) { if (len > 0) { /* don't copy beyond len bytes */ *to++ = *(from + 1); if (--len == 0) *(to - 1) = '\0'; /* null protect the last spot */ } from = from + 2; } else { if (len > 0) { /* don't copy beyond len bytes */ *to++ = *from++; if (--len == 0) *(to - 1) = '\0'; /* null protect the last spot */ } else from++; } } } if (len > 0) *to = 0; from = skip_white(from); return (from);} /* copy_word *//* * copy_word * copies the next 'token' from 'from' into 'to'. * currently a token is anything seperate by white space * or within quotes (double or single) (i.e. "the red rose" * is one token, \"the red rose\" is three tokens) * a '\' character will allow a quote character to be treated * as a regular character * It returns a pointer to first non-white space after the end of the token * being copied or to 0 if we reach the end. */static int have_warned = 0;char *copy_word(char *from, char *to){ if (!have_warned) { snmp_log(LOG_INFO, "copy_word() called. Use copy_nword() instead.\n"); have_warned = 1; } return copy_nword(from, to, SPRINT_MAX_LEN);} /* copy_word *//* * read_config_save_octet_string(): saves an octet string as a length * followed by a string of hex */char *read_config_save_octet_string(char *saveto, u_char * str, size_t len){ int i; u_char *cp; /* * is everything easily printable */ for (i = 0, cp = str; i < (int) len && cp && (isalpha(*cp) || isdigit(*cp) || *cp == ' '); cp++, i++); if (len != 0 && i == (int) len) { *saveto++ = '"'; memcpy(saveto, str, len); saveto += len; *saveto++ = '"'; *saveto = '\0'; } else { if (str != NULL) { sprintf(saveto, "0x"); saveto += 2; for (i = 0; i < (int) len; i++) { sprintf(saveto, "%02x", str[i]); saveto = saveto + 2; } } else { sprintf(saveto, "\"\""); saveto += 2; } } return saveto;}/* * read_config_read_octet_string(): reads an octet string that was * saved by the read_config_save_octet_string() function */char *read_config_read_octet_string(char *readfrom, u_char ** str, size_t * len){ u_char *cptr = NULL; char *cptr1; u_int tmp; int i; if (readfrom == NULL || str == NULL) return NULL; if (strncasecmp(readfrom, "0x", 2) == 0) { /* * A hex string submitted. How long? */ readfrom += 2; cptr1 = skip_not_white(readfrom); if (cptr1) *len = (cptr1 - readfrom); else *len = strlen(readfrom); if (*len % 2) { DEBUGMSGTL(("read_config_read_octet_string", "invalid hex string: wrong length")); return NULL; } *len = *len / 2; /* * malloc data space if needed (+1 for good measure) */ if (*str == NULL) { if ((cptr = (u_char *) malloc(*len + 1)) == NULL) { return NULL; } *str = cptr; } else { cptr = *str; } /* * copy validated data */ for (i = 0; i < (int) *len; i++) { if (1 == sscanf(readfrom, "%2x", &tmp)) *cptr++ = (u_char) tmp; else { /* * we may lose memory, but don't know caller's buffer XX free(cptr); */ return (NULL); } readfrom += 2; } *cptr++ = '\0'; readfrom = skip_white(readfrom); } else { /* * Normal string */ /* * malloc string space if needed (including NULL terminator) */ if (*str == NULL) { char buf[SNMP_MAXBUF]; readfrom = copy_nword(readfrom, buf, sizeof(buf)); *len = strlen(buf); if ((cptr = (u_char *) malloc(*len + 1)) == NULL) return NULL; *str = cptr; if (cptr) { memcpy(cptr, buf, *len + 1); } } else { readfrom = copy_nword(readfrom, (char *) *str, *len); *len = strlen(*str); } } return readfrom;}/* * read_config_save_objid(): saves an objid as a numerical string */char *read_config_save_objid(char *saveto, oid * objid, size_t len){ int i; if (len == 0) { strcat(saveto, "NULL"); saveto += strlen(saveto); return saveto; } /* * in case len=0, this makes it easier to read it back in */ for (i = 0; i < (int) len; i++) { sprintf(saveto, ".%ld", objid[i]); saveto += strlen(saveto); } return saveto;}/* * read_config_read_objid(): reads an objid from a format saved by the above */char *read_config_read_objid(char *readfrom, oid ** objid, size_t * len){ if (objid == NULL || readfrom == NULL) return NULL; if (*objid == NULL) { *len = 0; if ((*objid = (oid *) malloc(MAX_OID_LEN * sizeof(oid))) == NULL) return NULL; *len = MAX_OID_LEN; } if (strncmp(readfrom, "NULL", 4) == 0) { /* * null length oid */ *len = 0; } else { /* * qualify the string for read_objid */ char buf[SPRINT_MAX_LEN]; copy_nword(readfrom, buf, sizeof(buf)); if (!read_objid(buf, *objid, len)) { DEBUGMSGTL(("read_config_read_objid", "Invalid OID")); *len = 0; return NULL; } } readfrom = skip_token(readfrom); return readfrom;}/* * read_config_read_data(): * reads data of a given type from a token(s) on a configuration line. * * Returns: character pointer to the next token in the configuration line. * NULL if none left. * NULL if an unknown type. * * dataptr is expected to match a pointer type being read * (int *, u_int *, char **, oid **) */char *read_config_read_data(int type, char *readfrom, void *dataptr, size_t * len){ int *intp; char **charpp; oid **oidpp; unsigned int *uintp; if (dataptr && readfrom) switch (type) { case ASN_INTEGER: intp = (int *) dataptr; *intp = atoi(readfrom); readfrom = skip_token(readfrom); return readfrom; case ASN_TIMETICKS: case ASN_UNSIGNED: uintp = (unsigned int *) dataptr; *uintp = strtoul(readfrom, NULL, 0); readfrom = skip_token(readfrom); return readfrom; case ASN_OCTET_STR: case ASN_BIT_STR: charpp = (char **) dataptr; return read_config_read_octet_string(readfrom, (u_char **) charpp, len); case ASN_OBJECT_ID: oidpp = (oid **) dataptr; return read_config_read_objid(readfrom, oidpp, len); default: DEBUGMSGTL(("read_config_read_data", "Fail: Unknown type: %d", type)); return NULL; } return NULL;}/* * read_config_read_memory(): * * similar to read_config_read_data, but expects a generic memory * pointer rather than a specific type of pointer. Len is expected to * be the amount of available memory. */char *read_config_read_memory(int type, char *readfrom, char *dataptr, size_t * len){ int *intp; unsigned int *uintp; if (!dataptr || !readfrom) return NULL; switch (type) { case ASN_INTEGER: if (*len < sizeof(int)) return NULL; intp = (int *) dataptr; *intp = atoi(readfrom); *len = sizeof(int); readfrom = skip_token(readfrom); return readfrom; case ASN_TIMETICKS: case ASN_UNSIGNED: if (*len < sizeof(unsigned int)) return NULL; uintp = (unsigned int *) dataptr; *uintp = strtoul(readfrom, NULL, 0); *len = sizeof(unsigned int); readfrom = skip_token(readfrom); return readfrom; case ASN_OCTET_STR: case ASN_BIT_STR: case ASN_PRIV_IMPLIED_OCTET_STR: return read_config_read_octet_string(readfrom, (u_char **) & dataptr, len); case ASN_PRIV_IMPLIED_OBJECT_ID: case ASN_OBJECT_ID: readfrom = read_config_read_objid(readfrom, (oid **) & dataptr, len); *len *= sizeof(oid); return readfrom; default: DEBUGMSGTL(("read_config_read_memory", "Fail: Unknown type: %d", type)); return NULL; } return NULL;}/* * read_config_store_data(): * stores data of a given type to a configuration line. * * Returns: character pointer to the end of the line. * NULL if an unknown type. */char *read_config_store_data(int type, char *storeto, void *dataptr, size_t * len){ return read_config_store_data_prefix(' ', type, storeto, dataptr, *len);}/* * read_config_store_data_prefix(): * stores data of a given type to a configuration line. * * Returns: character pointer to the end of the line. * NULL if an unknown type. */char *read_config_store_data_prefix(char prefix, int type, char *storeto, void *dataptr, size_t len){ int *intp; u_char **charpp; unsigned int *uintp; oid **oidpp; if (dataptr && storeto) switch (type) { case ASN_INTEGER: intp = (int *) dataptr; sprintf(storeto, "%c%d", prefix, *intp); return (storeto + strlen(storeto)); case ASN_TIMETICKS: case ASN_UNSIGNED: uintp = (unsigned int *) dataptr; sprintf(storeto, "%c%u", prefix, *uintp); return (storeto + strlen(storeto)); case ASN_OCTET_STR: case ASN_BIT_STR: *storeto++ = prefix; charpp = (u_char **) dataptr; return read_config_save_octet_string(storeto, *charpp, len); case ASN_OBJECT_ID: *storeto++ = prefix; oidpp = (oid **) dataptr; return read_config_save_objid(storeto, *oidpp, len); default: DEBUGMSGTL(("read_config_store_data_prefix", "Fail: Unknown type: %d", type)); return NULL; } return NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -