static int conn_read(void) { struct kinfo_file *kf; int i, fcnt; conn_reset_port_entry(); kf = kvm_getfiles(kvmd, KERN_FILE_BYFILE, DTYPE_SOCKET, sizeof(*kf), &fcnt); if (kf == NULL) { ERROR("tcpconns plugin: kvm_getfiles failed."); return (-1); } for (i = 0; i < fcnt; i++) { if (kf[i].so_protocol != IPPROTO_TCP) continue; if (kf[i].inp_fport == 0) continue; conn_handle_ports(ntohs(kf[i].inp_lport), ntohs(kf[i].inp_fport), kf[i].t_state); } conn_submit_all(); return (0); }
void unixpr(u_long off, u_long pcbaddr) { struct file *fp; struct socket sock, *so = &sock; char *filebuf; struct protosw *unixsw = (struct protosw *)off; filebuf = kvm_getfiles(kvmd, KERN_FILE, 0, &fcnt); if (filebuf == NULL) { printf("Out of memory (file table).\n"); return; } file = (struct file *)(filebuf + sizeof(fp)); fileNFILE = file + fcnt; for (fp = file; fp < fileNFILE; fp++) { if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET) continue; if (kread((u_long)fp->f_data, so, sizeof (*so))) continue; /* kludge */ if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2) if (so->so_pcb) unixdomainpr(so, fp->f_data, pcbaddr); } }
/** * * @retval 0 no errors * @retval !0 errors */ static int _kvmload(netsnmp_container *container, u_int load_flags) { netsnmp_udp_endpoint_entry *entry; struct kinfo_file *kf; int count; int rc = 0; kf = kvm_getfiles(kd, KERN_FILE_BYFILE, DTYPE_SOCKET, sizeof(struct kinfo_file), &count); while (count--) { if (kf->so_protocol != IPPROTO_UDP) goto skip; #if !defined(NETSNMP_ENABLE_IPV6) if (kf->so_family == AF_INET6) 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(kf->inp_lport); entry->rmt_port = ntohs(kf->inp_fport); entry->pid = kf->p_pid; /** the addr string may need work */ if (kf->so_family == AF_INET6) { entry->loc_addr_len = entry->rmt_addr_len = 16; memcpy(entry->loc_addr, &kf->inp_laddru, 16); memcpy(entry->rmt_addr, &kf->inp_faddru, 16); } else { entry->loc_addr_len = entry->rmt_addr_len = 4; memcpy(entry->loc_addr, &kf->inp_laddru[0], 4); memcpy(entry->rmt_addr, &kf->inp_faddru[0], 4); } DEBUGMSGTL(("udp-mib/data_access", "udp %d %d %d\n", entry->loc_addr_len, entry->loc_port, entry->rmt_port)); /* * add entry to container */ entry->index = CONTAINER_SIZE(container) + 1; CONTAINER_INSERT(container, entry); skip: kf++; } if (rc < 0) return rc; return 0; }
void unixpr(u_long off) { struct file *fp; struct socket sock, *so = &sock; char *filebuf; struct protosw *unixsw = (struct protosw *)off; if (use_sysctl) { struct kinfo_pcb *pcblist; int mib[8]; size_t namelen = 0, size = 0, i; const char *mibnames[] = { "net.local.stream.pcblist", "net.local.dgram.pcblist", "net.local.seqpacket.pcblist", NULL, }; const char **mibname; static int first = 1; for (mibname = mibnames; *mibname; mibname++) { memset(mib, 0, sizeof(mib)); if (sysctlnametomib(*mibname, mib, &namelen) == -1) err(1, "sysctlnametomib: %s", *mibname); if (prog_sysctl(mib, sizeof(mib) / sizeof(*mib), NULL, &size, NULL, 0) == -1) err(1, "sysctl (query)"); if ((pcblist = malloc(size)) == NULL) err(1, "malloc"); memset(pcblist, 0, size); mib[6] = sizeof(*pcblist); mib[7] = size / sizeof(*pcblist); if (prog_sysctl(mib, sizeof(mib) / sizeof(*mib), pcblist, &size, NULL, 0) == -1) err(1, "sysctl (copy)"); for (i = 0; i < size / sizeof(*pcblist); i++) { struct kinfo_pcb *ki = &pcblist[i]; struct sockaddr_un *sun; int remote = 0; if (first) { unixdomainprhdr(); first = 0; } sun = (struct sockaddr_un *)&ki->ki_dst; if (sun->sun_path[0] != '\0') { remote = 1; } else { sun = (struct sockaddr_un *)&ki->ki_src; } unixdomainpr0(ki->ki_pcbaddr, ki->ki_type, ki->ki_rcvq, ki->ki_sndq, ki->ki_vnode, ki->ki_conn, ki->ki_refs, ki->ki_nextref, ki->ki_sockaddr, sun, remote); } free(pcblist); } } else { filebuf = (char *)kvm_getfiles(get_kvmd(), KERN_FILE, 0, &ns_nfiles); if (filebuf == 0) { printf("file table read error: %s", kvm_geterr(get_kvmd())); return; } file = (struct file *)(filebuf + sizeof(fp)); fileNFILE = file + ns_nfiles; for (fp = file; fp < fileNFILE; fp++) { if (fp->f_count == 0 || fp->f_type != DTYPE_SOCKET) continue; if (kread((u_long)fp->f_data, (char *)so, sizeof (*so))) continue; /* kludge */ if (so->so_proto >= unixsw && so->so_proto <= unixsw + 2) if (so->so_pcb) unixdomainpr(so, fp->f_data); } } }