⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmp_core.c

📁 -
💻 C
📖 第 1 页 / 共 3 页
字号:
			nextoid = NULL;		    }		} else {		    nextoid = mibTreeEntry;		    mibTreeEntry = NULL;		}	    }	}    }    while ((mibTreeEntry) && (!mibTreeEntry->parsefunction)) {	mibTreeEntry = mibTreeEntry->leaves[0];    }    if (mibTreeEntry) {	*NextLen = mibTreeEntry->len;	*Next = (*mibTreeEntry->instancefunction) (mibTreeEntry->name, NextLen, mibTreeEntry, &Fn);    }    return (Fn);}oid *static_Inst(oid * name, snint * len, mib_tree_entry * current, oid_ParseFn ** Fn){    oid *instance = NULL;    if (*len <= current->len) {	instance = xmalloc(sizeof(name) * (*len + 1));	xmemcpy(instance, name, (sizeof(name) * *len));	instance[*len] = 0;	*len += 1;    }    *Fn = current->parsefunction;    return (instance);}oid *time_Inst(oid * name, snint * len, mib_tree_entry * current, oid_ParseFn ** Fn){    oid *instance = NULL;    int identifier = 0, loop = 0;    int index[TIME_INDEX_LEN] =    {TIME_INDEX};    if (*len <= current->len) {	instance = xmalloc(sizeof(name) * (*len + 1));	xmemcpy(instance, name, (sizeof(name) * *len));	instance[*len] = *index;	*len += 1;    } else {	identifier = name[*len - 1];	while ((identifier != index[loop]) && (loop < TIME_INDEX_LEN))	    loop++;	if (loop < TIME_INDEX_LEN - 1) {	    instance = xmalloc(sizeof(name) * (*len));	    xmemcpy(instance, name, (sizeof(name) * *len));	    instance[*len - 1] = index[++loop];	}    }    *Fn = current->parsefunction;    return (instance);}oid *peer_Inst(oid * name, snint * len, mib_tree_entry * current, oid_ParseFn ** Fn){    oid *instance = NULL;    u_char *cp = NULL;    peer *peers = Config.peers;    struct in_addr *laddr = NULL;    char *host_addr = NULL, *current_addr = NULL, *last_addr = NULL;    if (peers == NULL) {	current = current->parent->parent->parent->leaves[1];	while ((current) && (!current->parsefunction))	    current = current->leaves[0];	instance = client_Inst(current->name, len, current, Fn);    } else if (*len <= current->len) {	instance = xmalloc(sizeof(name) * (*len + 4));	xmemcpy(instance, name, (sizeof(name) * *len));	cp = (u_char *) & (peers->in_addr.sin_addr.s_addr);	instance[*len] = *cp++;	instance[*len + 1] = *cp++;	instance[*len + 2] = *cp++;	instance[*len + 3] = *cp++;	*len += 4;    } else {	laddr = oid2addr(&name[*len - 4]);	host_addr = inet_ntoa(*laddr);	last_addr = xmalloc(strlen(host_addr));	strncpy(last_addr, host_addr, strlen(host_addr));	current_addr = inet_ntoa(peers->in_addr.sin_addr);	while ((peers) && (strncmp(last_addr, current_addr, strlen(current_addr)))) {	    if (peers->next) {		peers = peers->next;		current_addr = inet_ntoa(peers->in_addr.sin_addr);	    } else {		peers = NULL;	    }	}	xfree(last_addr);	if (peers) {	    if (peers->next) {		peers = peers->next;		instance = xmalloc(sizeof(name) * (*len));		xmemcpy(instance, name, (sizeof(name) * *len));		cp = (u_char *) & (peers->in_addr.sin_addr.s_addr);		instance[*len - 4] = *cp++;		instance[*len - 3] = *cp++;		instance[*len - 2] = *cp++;		instance[*len - 1] = *cp++;	    } else {		return (instance);	    }	} else {	    return (instance);	}    }    *Fn = current->parsefunction;    return (instance);}oid *client_Inst(oid * name, snint * len, mib_tree_entry * current, oid_ParseFn ** Fn){    oid *instance = NULL;    u_char *cp = NULL;    struct in_addr *laddr = NULL;    if (*len <= current->len) {	instance = xmalloc(sizeof(name) * (*len + 4));	xmemcpy(instance, name, (sizeof(name) * *len));	laddr = client_entry(NULL);	if (laddr) {	    cp = (u_char *) & (laddr->s_addr);	    instance[*len] = *cp++;	    instance[*len + 1] = *cp++;	    instance[*len + 2] = *cp++;	    instance[*len + 3] = *cp++;	    *len += 4;	}    } else {	laddr = oid2addr(&name[*len - 4]);	laddr = client_entry(laddr);	if (laddr) {	    instance = xmalloc(sizeof(name) * (*len));	    xmemcpy(instance, name, (sizeof(name) * *len));	    cp = (u_char *) & (laddr->s_addr);	    instance[*len - 4] = *cp++;	    instance[*len - 3] = *cp++;	    instance[*len - 2] = *cp++;	    instance[*len - 1] = *cp++;	}    }    *Fn = current->parsefunction;    return (instance);}/* * Utility functions *//* * Tree utility functions.  *//*  * Returns a the sibling object in the tree */mib_tree_entry *snmpTreeSiblingEntry(oid entry, snint len, mib_tree_entry * current){    mib_tree_entry *next = NULL;    int count = 0;    while ((!next) && (count < current->children)) {	if (current->leaves[count]->name[len] == entry) {	    next = current->leaves[count];	}	count++;    }    if (count < current->children) {	next = current->leaves[count];    } else {	next = NULL;    }    return (next);}/*  * Returns the requested child object or NULL if it does not exist */mib_tree_entry *snmpTreeEntry(oid entry, snint len, mib_tree_entry * current){    mib_tree_entry *next = NULL;    int count = 0;    while ((!next) && (count < current->children)) {	if (current->leaves[count]->name[len] == entry) {	    next = current->leaves[count];	}	count++;    }    return (next);}/* * Adds a node to the MIB tree structure and adds the appropriate children */mib_tree_entry *#if STDC_HEADERSsnmpAddNode(oid * name, int len, oid_ParseFn * parsefunction, instance_Fn * instancefunction, int children,...)#elsesnmpAddNode(va_alist)     va_dcl#endif{#if STDC_HEADERS    va_list args;    int loop;    mib_tree_entry *entry = NULL;    va_start(args, children);#else    va_list args;    oid *name = NULL;    int len = 0, children = 0, loop;    oid_ParseFn *parsefunction = NULL;    instance_Fn *instancefunction = NULL;    mib_tree_entry *entry = NULL;    va_start(args);    name = va_arg(args, oid *);    len = va_arg(args, int);    parsefunction = va_arg(args, oid_ParseFn *);    instancefunction = va_arg(args, instance_Fn *);    children = va_arg(args, int);#endif    debug(49, 6) ("snmpAddNode: Children : %d, Oid : \n", children);    snmpDebugOid(6, name, len);    va_start(args, children);    entry = xmalloc(sizeof(mib_tree_entry));    entry->name = snmpOidDup(name, len);    entry->len = len;    entry->parsefunction = parsefunction;    entry->instancefunction = instancefunction;    entry->children = children;    if (children > 0) {	entry->leaves = xmalloc(sizeof(mib_tree_entry *) * children);	for (loop = 0; loop < children; loop++) {	    entry->leaves[loop] = va_arg(args, mib_tree_entry *);	    entry->leaves[loop]->parent = entry;	}    }    return (entry);}/* End of tree utility functions *//*  * Returns the list of parameters in an oid */oid *#if STDC_HEADERSsnmpCreateOid(int length,...)#elsesnmpCreateOid(va_alist)     va_dcl#endif{#if STDC_HEADERS    va_list args;    oid *new_oid;    int loop;    va_start(args, length);#else    va_list args;    int length = 0, loop;    oid *new_oid;    va_start(args);    length va_arg(args, int);#endif    new_oid = xmalloc(sizeof(oid) * length);    if (length > 0) {	for (loop = 0; loop < length; loop++) {	    new_oid[loop] = va_arg(args, int);	}    }    return (new_oid);}/* * Allocate space for, and copy, an OID.  Returns new oid. */oid *snmpOidDup(oid * A, snint ALen){    oid *Ans = xmalloc(sizeof(oid) * ALen);    xmemcpy(Ans, A, (sizeof(oid) * ALen));    return Ans;}/* * Debug calls, prints out the OID for debugging purposes. */voidsnmpDebugOid(int lvl, oid * Name, snint Len){    char mbuf[16], objid[1024];    int x;    objid[0] = '\0';    for (x = 0; x < Len; x++) {	snprintf(mbuf, sizeof(mbuf), ".%u", (unsigned int) Name[x]);	strncat(objid, mbuf, sizeof(objid));    }    debug(49, lvl) ("   oid = %s\n", objid);}static voidsnmpSnmplibDebug(int lvl, char *buf){    debug(49, lvl) ("%s", buf);}voidaddr2oid(struct in_addr addr, oid * Dest){    u_char *cp;    cp = (u_char *) & (addr.s_addr);    Dest[0] = *cp++;    Dest[1] = *cp++;    Dest[2] = *cp++;    Dest[3] = *cp++;}struct in_addr       *oid2addr(oid * id){    static struct in_addr laddr;    u_char *cp = (u_char *) & (laddr.s_addr);    cp[0] = id[0];    cp[1] = id[1];    cp[2] = id[2];    cp[3] = id[3];    return &laddr;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -