📄 snmpudpdomain.c
字号:
if (!*cp && atoi(peername) != 0) { /* * Okay, it looks like just a port number. */ DEBUGMSGTL(("netsnmp_sockaddr_in", "totally numeric: %d\n", atoi(peername))); addr->sin_port = htons(atoi(peername)); } else if (inet_addr(peername) != INADDR_NONE) { /* * It looks like an IP address. */ DEBUGMSGTL(("netsnmp_sockaddr_in", "IP address\n")); addr->sin_addr.s_addr = inet_addr(peername); } else { /* * Well, it must be a hostname then. */#ifdef HAVE_GETHOSTBYNAME struct hostent *hp = gethostbyname(peername); if (hp == NULL) { DEBUGMSGTL(("netsnmp_sockaddr_in", "hostname (couldn't resolve)\n")); free(peername); return 0; } else { if (hp->h_addrtype != AF_INET) { DEBUGMSGTL(("netsnmp_sockaddr_in", "hostname (not AF_INET!)\n")); free(peername); return 0; } else { DEBUGMSGTL(("netsnmp_sockaddr_in", "hostname (resolved okay)\n")); memcpy(&(addr->sin_addr), hp->h_addr, hp->h_length); } }#else /*HAVE_GETHOSTBYNAME */ DEBUGMSGTL(("netsnmp_sockaddr_in", "hostname (no gethostbyname)\n")); free(peername); return 0;#endif /*HAVE_GETHOSTBYNAME */ } } else { DEBUGMSGTL(("netsnmp_sockaddr_in", "NULL peername")); return 0; } DEBUGMSGTL(("netsnmp_sockaddr_in", "return { AF_INET, %s:%hu }\n", inet_ntoa(addr->sin_addr), ntohs(addr->sin_port))); free(peername); return 1;}/* * The following functions provide the "com2sec" configuration token * functionality for compatibility. */#define EXAMPLE_NETWORK "NETWORK"#define EXAMPLE_COMMUNITY "COMMUNITY"typedef struct _com2SecEntry { char community[VACMSTRINGLEN]; unsigned long network; unsigned long mask; char secName[VACMSTRINGLEN]; struct _com2SecEntry *next;} com2SecEntry;com2SecEntry *com2SecList = NULL, *com2SecListLast = NULL;voidnetsnmp_udp_parse_security(const char *token, char *param){ char *secName = NULL, *community = NULL, *source = NULL; char *cp = NULL; const char *strmask = NULL; com2SecEntry *e = NULL; unsigned long network = 0, mask = 0; /* * Get security, source address/netmask and community strings. */ secName = strtok(param, "\t\n "); if (secName == NULL) { config_perror("missing NAME parameter"); return; } else if (strlen(secName) > (VACMSTRINGLEN - 1)) { config_perror("security name too long"); return; } source = strtok(NULL, "\t\n "); if (source == NULL) { config_perror("missing SOURCE parameter"); return; } else if (strncmp(source, EXAMPLE_NETWORK, strlen(EXAMPLE_NETWORK)) == 0) { config_perror("example config NETWORK not properly configured"); return; } community = strtok(NULL, "\t\n "); if (community == NULL) { config_perror("missing COMMUNITY parameter\n"); return; } else if (strncmp (community, EXAMPLE_COMMUNITY, strlen(EXAMPLE_COMMUNITY)) == 0) { config_perror("example config COMMUNITY not properly configured"); return; } else if (strlen(community) > (VACMSTRINGLEN - 1)) { config_perror("community name too long"); return; } /* * Process the source address/netmask string. */ cp = strchr(source, '/'); if (cp != NULL) { /* * Mask given. */ *cp = '\0'; strmask = cp + 1; } /* * Deal with the network part first. */ if ((strcmp(source, "default") == 0) || (strcmp(source, "0.0.0.0") == 0)) { network = 0; strmask = "0.0.0.0"; } else { /* * Try interpreting as a dotted quad. */ network = inet_addr(source); if (network == (unsigned long) -1) { /* * Nope, wasn't a dotted quad. Must be a hostname. */#ifdef HAVE_GETHOSTBYNAME struct hostent *hp = gethostbyname(source); if (hp == NULL) { config_perror("bad source address"); return; } else { if (hp->h_addrtype != AF_INET) { config_perror("no IP address for source hostname"); return; } network = *((unsigned long *) hp->h_addr); }#else /*HAVE_GETHOSTBYNAME */ /* * Oh dear. */ config_perror("cannot resolve source hostname"); return;#endif /*HAVE_GETHOSTBYNAME */ } } /* * Now work out the mask. */ if (strmask == NULL || *strmask == '\0') { /* * No mask was given. Use 255.255.255.255. */ mask = 0xffffffffL; } else { if (strchr(strmask, '.')) { /* * Try to interpret mask as a dotted quad. */ mask = inet_addr(strmask); if (mask == (unsigned long) -1 && strncmp(strmask, "255.255.255.255", 15) != 0) { config_perror("bad mask"); return; } } else { /* * Try to interpret mask as a "number of 1 bits". */ int maskLen = atoi(strmask), maskBit = 0x80000000L; if (maskLen <= 0 || maskLen > 32) { config_perror("bad mask length"); return; } while (maskLen--) { mask |= maskBit; maskBit >>= 1; } mask = htonl(mask); } } /* * Check that the network and mask are consistent. */ if (network & ~mask) { config_perror("source/mask mismatch"); return; } e = (com2SecEntry *) malloc(sizeof(com2SecEntry)); if (e == NULL) { config_perror("memory error"); return; } /* * Everything is okay. Copy the parameters to the structure allocated * above and add it to END of the list. */ DEBUGMSGTL(("netsnmp_udp_parse_security", "<\"%s\", 0x%08x/0x%08x> => \"%s\"\n", community, network, mask, secName)); strcpy(e->secName, secName); strcpy(e->community, community); e->network = network; e->mask = mask; e->next = NULL; if (com2SecListLast != NULL) { com2SecListLast->next = e; com2SecListLast = e; } else { com2SecListLast = com2SecList = e; }}voidnetsnmp_udp_com2SecList_free(void){ com2SecEntry *e = com2SecList; while (e != NULL) { com2SecEntry *tmp = e; e = e->next; free(tmp); } com2SecList = com2SecListLast = NULL;}voidnetsnmp_udp_agent_config_tokens_register(void){ register_app_config_handler("com2sec", netsnmp_udp_parse_security, netsnmp_udp_com2SecList_free, "name IPv4-network-address[/netmask] community");}/* * Return 0 if there are no com2sec entries, or return 1 if there ARE com2sec * entries. On return, if a com2sec entry matched the passed parameters, * then *secName points at the appropriate security name, or is NULL if the * parameters did not match any com2sec entry. */intnetsnmp_udp_getSecName(void *opaque, int olength, const char *community, size_t community_len, char **secName){ com2SecEntry *c; struct sockaddr_in *from = (struct sockaddr_in *) opaque; char *ztcommunity = NULL; /* * Special case if there are NO entries (as opposed to no MATCHING * entries). */ if (com2SecList == NULL) { DEBUGMSGTL(("netsnmp_udp_getSecName", "no com2sec entries\n")); if (secName != NULL) { *secName = NULL; } return 0; } /* * If there is no IPv4 source address, then there can be no valid security * name. */ if (opaque == NULL || olength != sizeof(struct sockaddr_in) || from->sin_family != AF_INET) { DEBUGMSGTL(("netsnmp_udp_getSecName", "no IPv4 source address in PDU?\n")); if (secName != NULL) { *secName = NULL; } return 1; } DEBUGIF("netsnmp_udp_getSecName") { ztcommunity = (char *)malloc(community_len + 1); if (ztcommunity != NULL) { memcpy(ztcommunity, community, community_len); ztcommunity[community_len] = '\0'; } DEBUGMSGTL(("netsnmp_udp_getSecName", "resolve <\"%s\", 0x%08x>\n", ztcommunity ? ztcommunity : "<malloc error>", from->sin_addr.s_addr)); } for (c = com2SecList; c != NULL; c = c->next) { DEBUGMSGTL(("netsnmp_udp_getSecName","compare <\"%s\", 0x%08x/0x%08x>", c->community, c->network, c->mask)); if ((community_len == strlen(c->community)) && (memcmp(community, c->community, community_len) == 0) && ((from->sin_addr.s_addr & c->mask) == c->network)) { DEBUGMSG(("netsnmp_udp_getSecName", "... SUCCESS\n")); if (secName != NULL) { *secName = c->secName; } break; } DEBUGMSG(("netsnmp_udp_getSecName", "... nope\n")); } if (ztcommunity != NULL) { free(ztcommunity); } return 1;}netsnmp_transport *netsnmp_udp_create_tstring(const char *string, int local){ struct sockaddr_in addr; if (netsnmp_sockaddr_in(&addr, string, 0)) { return netsnmp_udp_transport(&addr, local); } else { return NULL; }}netsnmp_transport *netsnmp_udp_create_ostring(const u_char * o, size_t o_len, int local){ struct sockaddr_in addr; if (o_len == 6) { addr.sin_family = AF_INET; memcpy((u_char *) & (addr.sin_addr.s_addr), o, 4); addr.sin_port = ntohs((o[4] << 8) + o[5]); return netsnmp_udp_transport(&addr, local); } return NULL;}voidnetsnmp_udp_ctor(void){ udpDomain.name = netsnmpUDPDomain; udpDomain.name_length = netsnmpUDPDomain_len; udpDomain.prefix = calloc(2, sizeof(char *)); udpDomain.prefix[0] = "udp"; udpDomain.f_create_from_tstring = netsnmp_udp_create_tstring; udpDomain.f_create_from_ostring = netsnmp_udp_create_ostring; netsnmp_tdomain_register(&udpDomain);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -