/** * * @retval 0 no errors * @retval !0 errors */ static int _load(netsnmp_container *container, u_int load_flags) { struct inpcbtable table; struct inpcb *head, *next, *prev; struct inpcb inpcb; struct tcpcb tcpcb; int StateMap[] = { 1, 2, 3, 4, 5, 8, 6, 10, 9, 7, 11 }; netsnmp_tcpconn_entry *entry; int state; int rc = 0; /* * Read in the buffer containing the TCP table data */ if (!auto_nlist(TCP_SYMBOL, (char *)&table, sizeof(table))) { DEBUGMSGTL(("tcp-mib/tcpConn_openbsd", "Failed to read tcp_symbol\n")); return -1; } prev = (struct inpcb *)&CIRCLEQ_FIRST(&table.inpt_queue); prev = NULL; head = next = CIRCLEQ_FIRST(&table.inpt_queue); while (next) { if (!NETSNMP_KLOOKUP(next, (char *)&inpcb, sizeof(inpcb))) { DEBUGMSGTL(("tcp-mib/data_access/tcpConn", "klookup inpcb failed\n")); break; } if (prev && CIRCLEQ_PREV(&inpcb, inp_queue) != prev) { snmp_log(LOG_ERR,"tcbtable link error\n"); break; } prev = next; next = CIRCLEQ_NEXT(&inpcb, inp_queue); if (!NETSNMP_KLOOKUP(inpcb.inp_ppcb, (char *)&tcpcb, sizeof(tcpcb))) { DEBUGMSGTL(("tcp-mib/data_access/tcpConn", "klookup tcpcb failed\n")); break; } state = StateMap[tcpcb.t_state]; if (load_flags) { if (state == TCPCONNECTIONSTATE_LISTEN) { if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_NOLISTEN) { DEBUGMSGT(("verbose:access:tcpconn:container", " skipping listen\n")); goto skip; } } else if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_ONLYLISTEN) { DEBUGMSGT(("verbose:access:tcpconn:container", " skipping non-listen\n")); goto skip; } } #if !defined(NETSNMP_ENABLE_IPV6) if (inpcb.inp_flags & INP_IPV6) goto skip; #endif entry = netsnmp_access_tcpconn_entry_create(); if(NULL == entry) { rc = -3; break; } /** oddly enough, these appear to already be in network order */ entry->loc_port = ntohs(inpcb.inp_lport); entry->rmt_port = ntohs(inpcb.inp_fport); entry->tcpConnState = state; entry->pid = 0; /** the addr string may need work */ if (inpcb.inp_flags & INP_IPV6) { entry->loc_addr_len = entry->rmt_addr_len = 16; memcpy(entry->loc_addr, &inpcb.inp_laddr6, 16); memcpy(entry->rmt_addr, &inpcb.inp_faddr6, 16); } else { entry->loc_addr_len = entry->rmt_addr_len = 4; memcpy(entry->loc_addr, &inpcb.inp_laddr, 4); memcpy(entry->rmt_addr, &inpcb.inp_faddr, 4); } DEBUGMSGTL(("tcp-mib/data_access", "tcp %d %d %d\n", entry->loc_addr_len, entry->loc_port, entry->rmt_port)); /* * add entry to container */ entry->arbitrary_index = CONTAINER_SIZE(container) + 1; CONTAINER_INSERT(container, entry); skip: if (head == next) break; } if(rc<0) return rc; return 0; }
int read_ns(void) { struct inpcbtable pcbtable; struct inpcb *head, *prev, *next; struct inpcb inpcb; struct socket sockb; struct tcpcb tcpcb; void *off; int istcp; if (kd == NULL) { return (0); } num_ns = 0; if (namelist[X_TCBTABLE].n_value == 0) return 0; if (protos & TCP) { off = NPTR(X_TCBTABLE); istcp = 1; } else if (protos & UDP) { off = NPTR(X_UDBTABLE); istcp = 0; } else { error("No protocols to display"); return 0; } again: KREAD(off, &pcbtable, sizeof (struct inpcbtable)); prev = head = (struct inpcb *)&((struct inpcbtable *)off)->inpt_queue; next = CIRCLEQ_FIRST(&pcbtable.inpt_queue); while (next != head) { KREAD(next, &inpcb, sizeof (inpcb)); if (CIRCLEQ_PREV(&inpcb, inp_queue) != prev) { error("Kernel state in transition"); return 0; } prev = next; next = CIRCLEQ_NEXT(&inpcb, inp_queue); if (!aflag) { if (!(inpcb.inp_flags & INP_IPV6) && inet_lnaof(inpcb.inp_faddr) == INADDR_ANY) continue; if ((inpcb.inp_flags & INP_IPV6) && IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6)) continue; } KREAD(inpcb.inp_socket, &sockb, sizeof (sockb)); if (istcp) { KREAD(inpcb.inp_ppcb, &tcpcb, sizeof (tcpcb)); if (!aflag && tcpcb.t_state <= TCPS_LISTEN) continue; enter(&inpcb, &sockb, tcpcb.t_state, "tcp"); } else enter(&inpcb, &sockb, 0, "udp"); } if (istcp && (protos & UDP)) { istcp = 0; off = NPTR(X_UDBTABLE); goto again; } num_disp = num_ns; return 0; }
/** * * @retval 0 no errors * @retval !0 errors */ static int _load(netsnmp_container *container, u_int load_flags) { struct inpcbtable table; struct inpcb *head, *next, *prev; struct inpcb inpcb; netsnmp_udp_endpoint_entry *entry; int rc = 0; /* * Read in the buffer containing the TCP table data */ if (!auto_nlist(UDB_SYMBOL, (char *) &table, sizeof(table))) { DEBUGMSGTL(("udp-mib/udp_endpoint_openbsd", "Failed to read udp_symbol\n")); return -1; } prev = (struct inpcb *)&CIRCLEQ_FIRST(&table.inpt_queue); prev = NULL; head = next = CIRCLEQ_FIRST(&table.inpt_queue); while (next) { NETSNMP_KLOOKUP(next, (char *)&inpcb, sizeof(inpcb)); if (prev && CIRCLEQ_PREV(&inpcb, inp_queue) != prev) { snmp_log(LOG_ERR,"udbtable link error\n"); break; } prev = next; next = CIRCLEQ_NEXT(&inpcb, inp_queue); #if !defined(NETSNMP_ENABLE_IPV6) if (inpcb.inp_flags & INP_IPV6) goto skip; #endif entry = netsnmp_access_udp_endpoint_entry_create(); if (NULL == entry) { rc = -3; break; } /** oddly enough, these appear to already be in network order */ entry->loc_port = ntohs(inpcb.inp_lport); entry->rmt_port = ntohs(inpcb.inp_fport); entry->pid = 0; /** the addr string may need work */ if (inpcb.inp_flags & INP_IPV6) { entry->loc_addr_len = entry->rmt_addr_len = 16; memcpy(entry->loc_addr, &inpcb.inp_laddr6, 16); memcpy(entry->rmt_addr, &inpcb.inp_faddr6, 16); } else { entry->loc_addr_len = entry->rmt_addr_len = 4; memcpy(entry->loc_addr, &inpcb.inp_laddr, 4); memcpy(entry->rmt_addr, &inpcb.inp_faddr, 4); } /* * add entry to container */ entry->index = CONTAINER_SIZE(container) + 1; CONTAINER_INSERT(container, entry); #if !defined(NETSNMP_ENABLE_IPV6) skip: #endif if (next == head) break; } if (rc < 0) return rc; return 0; }