Date: Wed, 10 Jun 2009 10:37:26 GMT From: Gabor Pali <pgj@FreeBSD.org> To: Perforce Change Reviews <perforce@FreeBSD.org> Subject: PERFORCE change 163965 for review Message-ID: <200906101037.n5AAbQfA037260@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=163965 Change 163965 by pgj@petymeg-current on 2009/06/10 10:37:14 Some code re-organization Affected files ... .. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat.c#11 edit Differences ... ==== //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat.c#11 (text+ko) ==== @@ -99,6 +99,74 @@ } static int +net_inet_pcblist_sysctl(int protocol, struct socket_type_list *list) +{ + char *buf; + size_t len; + char mibvar[64]; + + struct xinpgen *xig, *oxig; + struct tcpcb *tp = NULL; + struct inpcb *inp; + struct xsocket *so; + + struct socket_type *stp; + + sprintf(mibvar, "net.inet.%s.pcblist", ipproto[protocol]); + + len = 0; + if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) { + if (errno != ENOENT) + warn("sysctl: %s", mibvar); + return (-1); + } + if ((buf = malloc(len)) == 0) { + warnx("malloc %lu bytes", (u_long)len); + return (-2); + } + if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) { + warn("sysctl: %s", mibvar); + free(buf); + return (-2); + } + + oxig = xig = (struct xinpgen *)buf; + for (xig = (struct xinpgen *)((char *)xig + xig->xig_len); + xig->xig_len > sizeof(struct xinpgen); + xig = (struct xinpgen *)((char *)xig + xig->xig_len)) { + if (protocol == IPPROTO_TCP) { + tp = &((struct xtcpcb *)xig)->xt_tp; + inp = &((struct xtcpcb *)xig)->xt_inp; + so = &((struct xtcpcb *)xig)->xt_socket; + } else { + inp = &((struct xinpcb *)xig)->xi_inp; + so = &((struct xinpcb *)xig)->xi_socket; + } + + /* Ignore sockets for protocols other than the desired one. + XXX: is this needed? */ + if (so->xso_protocol != protocol) + continue; + + if (inp->inp_gencnt <= oxig->xig_gen) { + stp = _netstat_st_allocate(list, PF_INET, protocol, + ipproto[protocol]); + extract_inet_data(tp, inp, so, stp); + } + } + + free(buf); + return (0); +} + +#define KREAD(off, buf, len) do { \ + if (kread(kvm, (uintptr_t)(off), (buf), (len)) != 0) { \ + list->stl_error = NETSTAT_ERROR_UNDEFINED; \ + return (-1); \ + } \ +} while (0); + +static int net_local_pcblist_kvm(int type, struct socket_type_list *list, kvm_t *kvm, struct nlist *nlistp) { @@ -131,13 +199,6 @@ return (-1); } -#define KREAD(off, buf, len) do { \ - if (kread(kvm, (uintptr_t)(off), (buf), (len)) != 0) { \ - list->stl_error = NETSTAT_ERROR_UNDEFINED; \ - return (-1); \ - } \ -} while (0); - KREAD(count_off, &unp_count, sizeof(unp_count)); KREAD(gencnt_off, &unp_gencnt, sizeof(unp_gencnt)); KREAD(head_off, &head, sizeof(head)); @@ -171,71 +232,9 @@ } return (0); -#undef KREAD } static int -net_inet_pcblist_sysctl(int protocol, struct socket_type_list *list) -{ - char *buf; - size_t len; - char mibvar[64]; - - struct xinpgen *xig, *oxig; - struct tcpcb *tp = NULL; - struct inpcb *inp; - struct xsocket *so; - - struct socket_type *stp; - - sprintf(mibvar, "net.inet.%s.pcblist", ipproto[protocol]); - - len = 0; - if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) { - if (errno != ENOENT) - warn("sysctl: %s", mibvar); - return (-1); - } - if ((buf = malloc(len)) == 0) { - warnx("malloc %lu bytes", (u_long)len); - return (-2); - } - if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) { - warn("sysctl: %s", mibvar); - free(buf); - return (-2); - } - - oxig = xig = (struct xinpgen *)buf; - for (xig = (struct xinpgen *)((char *)xig + xig->xig_len); - xig->xig_len > sizeof(struct xinpgen); - xig = (struct xinpgen *)((char *)xig + xig->xig_len)) { - if (protocol == IPPROTO_TCP) { - tp = &((struct xtcpcb *)xig)->xt_tp; - inp = &((struct xtcpcb *)xig)->xt_inp; - so = &((struct xtcpcb *)xig)->xt_socket; - } else { - inp = &((struct xinpcb *)xig)->xi_inp; - so = &((struct xinpcb *)xig)->xi_socket; - } - - /* Ignore sockets for protocols other than the desired one. - XXX: is this needed? */ - if (so->xso_protocol != protocol) - continue; - - if (inp->inp_gencnt <= oxig->xig_gen) { - stp = _netstat_st_allocate(list, PF_INET, protocol, - ipproto[protocol]); - extract_inet_data(tp, inp, so, stp); - } - } - - free(buf); - return (0); -} - -static int net_inet_pcblist_kvm(int protocol, struct socket_type_list *list, kvm_t *kvm, struct nlist *nlistp) { @@ -243,6 +242,8 @@ return (0); } +#undef KREAD + int netstat_socket(int domain, int type, int protocol, struct socket_type_list *list, int flags, void *kvm_handle)
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200906101037.n5AAbQfA037260>