📄 route.c
字号:
struct iflist { int index; char name[64]; struct iflist *next;} *Iflist = NULL;voidget_ifname(char *name, int ifIndex){ netsnmp_pdu *pdu, *response; netsnmp_variable_list *vp; struct iflist *ip; oid varname[MAX_OID_LEN]; int status; for (ip = Iflist; ip; ip = ip->next) { if (ip->index == ifIndex) break; } if (ip) { strcpy(name, ip->name); return; } ip = (struct iflist *) malloc(sizeof(struct iflist)); if (ip == NULL) return; ip->next = Iflist; Iflist = ip; ip->index = ifIndex; pdu = snmp_pdu_create(SNMP_MSG_GET); memmove(varname, oid_ifdescr, sizeof(oid_ifdescr)); varname[10] = (oid) ifIndex; snmp_add_null_var(pdu, varname, sizeof(oid_ifdescr) / sizeof(oid) + 1); status = snmp_synch_response(Session, pdu, &response); if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { vp = response->variables; if (vp->val_len >= sizeof(ip->name)) vp->val_len = sizeof(ip->name) - 1; memmove(ip->name, vp->val.string, vp->val_len); ip->name[vp->val_len] = '\0'; snmp_free_pdu(response); } else { sprintf(ip->name, "if%d", ifIndex); } strcpy(name, ip->name);}static u_longforgemask(u_long a){ u_long m; if (IN_CLASSA(a)) m = IN_CLASSA_NET; else if (IN_CLASSB(a)) m = IN_CLASSB_NET; else m = IN_CLASSC_NET; return m;}static voiddomask(char *dst, u_long addr, u_long mask){ int b, i; if (!mask || forgemask(addr) == mask) { *dst = '\0'; return; } i = 0; for (b = 0; b < 32; b++) if (mask & (1 << b)) { int bb; i = b; for (bb = b + 1; bb < 32; bb++) if (!(mask & (1 << bb))) { i = -1; /* non-contig */ break; } break; } if (i == -1) sprintf(dst, "&0x%lx", mask); else sprintf(dst, "/%d", 32 - i);}char *routename(struct in_addr in){ register char *cp; static char line[MAXHOSTNAMELEN + 1]; struct hostent *hp; static char domain[MAXHOSTNAMELEN + 1]; static int first = 1; if (first) { first = 0; if (gethostname(domain, MAXHOSTNAMELEN) == 0 && (cp = (char *) strchr(domain, '.'))) (void) strcpy(domain, cp + 1); else domain[0] = 0; } cp = 0; if (!nflag) { hp = gethostbyaddr((char *) &in, sizeof(struct in_addr), AF_INET); if (hp) { if ((cp = (char *) strchr(hp->h_name, '.')) && !strcmp(cp + 1, domain)) *cp = 0; cp = (char *) hp->h_name; } } if (cp) strncpy(line, cp, sizeof(line) - 1); else {#define C(x) (unsigned)((x) & 0xff) in.s_addr = ntohl(in.s_addr); sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24), C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr)); } return (line);}/* * Return the name of the network whose address is given. * The address is assumed to be that of a net or subnet, not a host. */char *netname(struct in_addr in, u_long mask){ char *cp = NULL; static char line[MAXHOSTNAMELEN + 1]; struct netent *np = 0; u_long net, omask; u_long i; int subnetshift; i = ntohl(in.s_addr); omask = mask = ntohl(mask); if (!nflag && i != INADDR_ANY) { if (mask == INADDR_ANY) { if (IN_CLASSA(i)) { mask = IN_CLASSA_NET; subnetshift = 8; } else if (IN_CLASSB(i)) { mask = IN_CLASSB_NET; subnetshift = 8; } else { mask = IN_CLASSC_NET; subnetshift = 4; } /* * If there are more bits than the standard mask * would suggest, subnets must be in use. * Guess at the subnet mask, assuming reasonable * width subnet fields. */ while (i & ~mask) mask = (long) mask >> subnetshift; } net = i & mask; while ((mask & 1) == 0) mask >>= 1, net >>= 1; np = getnetbyaddr(net, AF_INET); if (np) cp = np->n_name; } if (cp) strncpy(line, cp, sizeof(line) - 1); else if ((i & 0xffffff) == 0) sprintf(line, "%u", C(i >> 24)); else if ((i & 0xffff) == 0) sprintf(line, "%u.%u", C(i >> 24), C(i >> 16)); else if ((i & 0xff) == 0) sprintf(line, "%u.%u.%u", C(i >> 24), C(i >> 16), C(i >> 8)); else sprintf(line, "%u.%u.%u.%u", C(i >> 24), C(i >> 16), C(i >> 8), C(i)); domask(line + strlen(line), i, omask); return (line);}/* * Print routing statistics */voidrt_stats(void){ netsnmp_variable_list *var; printf("routing:\n"); var = getvarbyname(Session, oid_ipnoroutes, sizeof(oid_ipnoroutes) / sizeof(oid)); if (var) { printf("\t%lu destination%s found unreachable\n", *var->val.integer, plural((int) *var->val.integer)); snmp_free_var(var); } else { printf("\tCouldn't get ipOutNoRoutes variable\n"); }}/* * Request a variable with a GET REQUEST message on the given * session. If the variable is found, a * pointer to a netsnmp_variable_list object will be returned. * Otherwise, NULL is returned. The caller must free the returned * variable_list object when done with it. */netsnmp_variable_list *getvarbyname(netsnmp_session * sp, oid * name, size_t len){ netsnmp_pdu *request, *response; netsnmp_variable_list *var = NULL, *vp; int status; request = snmp_pdu_create(SNMP_MSG_GET); snmp_add_null_var(request, name, len); status = snmp_synch_response(sp, request, &response); if (status == STAT_SUCCESS) { if (response->errstat == SNMP_ERR_NOERROR) { for (var = response->variables; var; var = var->next_variable) { if (var->name_length == len && !memcmp(name, var->name, len * sizeof(oid))) break; /* found our match */ } if (var != NULL) { /* * Now unlink this var from pdu chain so it doesn't get freed. * The caller will free the var. */ if (response->variables == var) { response->variables = var->next_variable; } else { for (vp = response->variables; vp; vp = vp->next_variable) { if (vp->next_variable == var) { vp->next_variable = var->next_variable; break; } } } if (var->type == SNMP_NOSUCHOBJECT || var->type == SNMP_NOSUCHINSTANCE || var->type == SNMP_ENDOFMIBVIEW) { snmp_free_var(var); var = NULL; } } } } else if (status != STAT_TIMEOUT) snmp_sess_perror("snmpnetstat", sp); if (response) snmp_free_pdu(response); return var;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -