PERFORCE change 167146 for review
Gabor Pali
pgj at FreeBSD.org
Sun Aug 9 22:24:12 UTC 2009
http://perforce.freebsd.org/chv.cgi?CH=167146
Change 167146 by pgj at petymeg-current on 2009/08/09 22:23:51
Add the basic types and functions for the upcoming route_type, an
abstraction for FIB entries.
Affected files ...
.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/Makefile#17 edit
.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat.h#59 edit
.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_internal.h#56 edit
.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_route.c#1 add
.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_util.c#64 edit
Differences ...
==== //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/Makefile#17 (text+ko) ====
@@ -4,7 +4,7 @@
LIB= netstat
SRCS= netstat_socket.c netstat_mbuf.c netstat_if.c netstat_bpf.c \
- netstat_stat.c netstat_util.c
+ netstat_stat.c netstat_route.c netstat_util.c
INCS= netstat.h
==== //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat.h#59 (text+ko) ====
@@ -93,6 +93,31 @@
#define NETSTAT_IP6_MBUF_MAX 32
#define NETSTAT_IP6_IF_MAX 16
+/* Flags for netstat_route(): */
+#define NETSTAT_ROUTE_ALL 0x01 /* Return all routes. */
+#define NETSTAT_ROUTE_ALLFIBS 0x02 /* Return routes for all FIBs. */
+
+/* Testing flags for route_type: */
+#define NETSTAT_RT_UP 0x00001
+#define NETSTAT_RT_GATEWAY 0x00002
+#define NETSTAT_RT_HOST 0x00004
+#define NETSTAT_RT_REJECT 0x00008
+#define NETSTAT_RT_DYNAMIC 0x00010
+#define NETSTAT_RT_MODIFIED 0x00020
+#define NETSTAT_RT_DONE 0x00040
+#define NETSTAT_RT_XRESOLVE 0x00080
+#define NETSTAT_RT_STATIC 0x00100
+#define NETSTAT_RT_PROTO1 0x00200
+#define NETSTAT_RT_PROTO2 0x00400
+#define NETSTAT_RT_PROTO3 0x00800
+#define NETSTAT_RT_PRCLONING 0x01000
+#define NETSTAT_RT_BLACKHOLE 0x02000
+#define NETSTAT_RT_BROADCAST 0x04000
+#define NETSTAT_RT_LLINFO 0x08000
+#define NETSTAT_RT_WASCLONED 0x10000
+#define NETSTAT_RT_CLONING 0x20000
+#define NETSTAT_RT_EXPIRES 0x40000 /* has expire time */
+
/* Enum for TCP states: */
enum tcp_state {
tcps_Closed,
@@ -232,6 +257,10 @@
struct mroute6_stat;
#endif
+struct route_type;
+struct route_type_list;
+struct route_type_iterator;
+
__BEGIN_DECLS
const char *netstat_strerror(int);
const char *netstat_kvmerror(const struct session_type *);
@@ -1177,4 +1206,31 @@
u_int64_t netstat_mroute6s_get_pkt2large(const struct mroute6_stat *);
u_int64_t netstat_mroute6s_get_upq_sockfull(const struct mroute6_stat *);
#endif
+
+/* Routing: */
+struct route_type_list *netstat_rtl_alloc(void);
+void netstat_rtl_free(struct route_type_list *);
+int netstat_rtl_geterror(const struct route_type_list *);
+int netstat_rtl_length(const struct route_type_list *);
+
+int netstat_rti_alloc(struct route_type_list *list,
+ struct route_type_iterator **iterator);
+const struct route_type *netstat_rti_first(struct route_type_iterator *);
+const struct route_type *netstat_rti_next(struct route_type_iterator *);
+void netstat_rti_free(struct route_type_iterator *);
+
+int netstat_route(const struct session_type *, int fib, int domain,
+ struct route_type_list *list, int flags);
+
+int netstat_rt_get_fib(const struct route_type *);
+int netstat_rt_get_family(const struct route_type *);
+const struct routeaddr_type *netstat_rt_get_destination(const struct route_type *);
+const struct routeaddr_type *netstat_rt_get_gateway(const struct route_type *);
+int netstat_rt_get_flags(const struct route_type *);
+u_int64_t netstat_rt_get_refs(const struct route_type *);
+u_int64_t netstat_rt_get_used(const struct route_type *);
+const char *netstat_rt_get_interface(const struct route_type *);
+u_int64_t netstat_rt_get_expire(const struct route_type *);
+u_int32_t netstat_rt_get_mtu(const struct route_type *);
+u_int64_t netstat_rt_get_address(const struct route_type *);
#endif /* !_NETSTAT_H_ */
==== //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_internal.h#56 (text+ko) ====
@@ -439,6 +439,45 @@
const char *str;
};
+/* Route type. */
+struct routeaddr_type {
+ /* for printing purposes: */
+ char *rat_address; /* resolved hostname */
+ char rat_ni_address[20]; /* numeric */
+ /* for other: */
+ void *rat_data; /* raw socket information */
+ int rat_data_len;
+};
+
+struct route_type {
+ int rt_fib; /* FIB instance */
+ int rt_family; /* protocol family */
+ struct routeaddr_type *rt_destination; /* destination: subnet */
+ struct routeaddr_type *rt_gateway; /* gateway: node */
+ int rt_flags;
+ u_int64_t rt_refs; /* # references */
+ u_int64_t rt_used; /* # raw packets forwarded */
+ char *rt_interface; /* routing interface name */
+ u_int64_t rt_expire; /* sec */
+ u_int32_t rt_mtu; /* bytes */
+ u_int64_t rt_address; /* opaque pointer */
+
+ TAILQ_ENTRY(route_type) rt_list;
+};
+
+struct route_type_list {
+ TAILQ_HEAD(, route_type) rtl_list;
+ int rtl_length;
+ int rtl_error;
+};
+
+struct route_type_iterator {
+ struct route_type_list *rti_list;
+ struct route_type *rti_first;
+ struct route_type *rti_next;
+};
+
+
int kread_data(kvm_t *kvm, u_long kvm_pointer, void *address, size_t size);
int kread_string(kvm_t *kvm, u_long kvm_pointer, char *buffer, int buflen);
@@ -464,6 +503,12 @@
struct bpf_type *_netstat_bpt_allocate(struct bpf_type_list *list,
const char *ifname);
+void _netstat_rtl_empty(struct route_type_list *);
+void _netstat_rt_free(struct route_type *);
+void _netstat_rat_free(struct routeaddr_type *);
+struct route_type *_netstat_rt_allocate(struct route_type_list *);
+struct routeaddr_type *_netstat_rat_allocate(void *, size_t);
+
const char *resolve_val2str_name(int, const struct val2str *);
/* XXX: merge these into a common address resolution routine. */
const char *routename(in_addr_t in, int numeric);
==== //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_util.c#64 (text+ko) ====
@@ -2190,6 +2190,219 @@
#undef MRT6_ACC
#endif
+void
+_netstat_rtl_empty(struct route_type_list *list)
+{
+ struct route_type *rtp;
+
+ while ((rtp = TAILQ_FIRST(&list->rtl_list)) ) {
+ TAILQ_REMOVE(&list->rtl_list, rtp, rt_list);
+ _netstat_rt_free(rtp);
+ }
+
+ list->rtl_length = 0;
+}
+
+void
+_netstat_rt_free(struct route_type *rtp)
+{
+ _netstat_rat_free(rtp->rt_destination);
+ _netstat_rat_free(rtp->rt_gateway);
+ free(rtp->rt_interface);
+ free(rtp);
+}
+
+void
+_netstat_rat_free(struct routeaddr_type *ratp)
+{
+ free(ratp->rat_address);
+ free(ratp->rat_data);
+ free(ratp);
+}
+
+struct route_type *
+_netstat_rt_allocate(struct route_type_list *list)
+{
+ struct route_type *rtp;
+
+ rtp = malloc(sizeof(*rtp));
+ if (rtp == NULL)
+ return (NULL);
+
+ bzero(rtp, sizeof(*rtp));
+ TAILQ_INSERT_TAIL(&list->rtl_list, rtp, rt_list);
+ list->rtl_length += 1;
+ return (rtp);
+}
+
+struct routeaddr_type *
+_netstat_rat_allocate(void *address, size_t len)
+{
+ struct routeaddr_type *ratp;
+
+ ratp = malloc(sizeof(*ratp));
+ if (ratp == NULL)
+ return (NULL);
+
+ bzero(ratp, sizeof(*ratp));
+ if (address != NULL && len > 0) {
+ ratp->rat_data = malloc(len);
+ if (ratp->rat_data == NULL) {
+ free(ratp);
+ ratp = NULL;
+ } else {
+ memcpy(ratp->rat_data, address, len);
+ ratp->rat_data_len = len;
+ }
+ }
+ return (ratp);
+}
+
+struct route_type_list *
+netstat_rtl_alloc(void)
+{
+ struct route_type_list *rtlp;
+
+ rtlp = malloc(sizeof(*rtlp));
+ if (rtlp == NULL)
+ return (NULL);
+
+ TAILQ_INIT(&rtlp->rtl_list);
+ rtlp->rtl_error = NETSTAT_ERROR_UNDEFINED;
+ rtlp->rtl_length = 0;
+ return (rtlp);
+}
+
+void
+netstat_rtl_free(struct route_type_list *list)
+{
+ _netstat_rtl_empty(list);
+ free(list);
+}
+
+int
+netstat_rtl_geterror(const struct route_type_list *list)
+{
+ return (list->rtl_error);
+}
+
+int
+netstat_rtl_length(const struct route_type_list *list)
+{
+ return (list->rtl_length);
+}
+
+int
+netstat_rti_alloc(struct route_type_list *list,
+ struct route_type_iterator **iterator)
+{
+ struct route_type_iterator *rtip;
+
+ rtip = malloc(sizeof(*rtip));
+ if (rtip == NULL)
+ return (-1);
+
+ bzero(rtip, sizeof(*rtip));
+ rtip->rti_list = list;
+ rtip->rti_first = TAILQ_FIRST(&list->rtl_list);
+ if (rtip->rti_first != NULL)
+ rtip->rti_next = TAILQ_NEXT(rtip->rti_first, rt_list);
+ *iterator = rtip;
+ return (0);
+}
+
+const struct route_type *
+netstat_rti_first(struct route_type_iterator *rtip)
+{
+ if (rtip->rti_first != NULL)
+ rtip->rti_next = TAILQ_NEXT(rtip->rti_first, rt_list);
+ return (rtip->rti_first);
+}
+
+const struct route_type *
+netstat_rti_next(struct route_type_iterator *rtip)
+{
+ const struct route_type *rtp;
+
+ rtp = rtip->rti_next;
+ if (rtip->rti_next != NULL)
+ rtip->rti_next = TAILQ_NEXT(rtip->rti_next, rt_list);
+
+ return (rtp);
+}
+
+void
+netstat_rti_free(struct route_type_iterator *rtip)
+{
+ free(rtip);
+}
+
+int
+netstat_rt_get_fib(const struct route_type *rtp)
+{
+ return (rtp->rt_fib);
+}
+
+int
+netstat_rt_get_family(const struct route_type *rtp)
+{
+ return (rtp->rt_family);
+}
+
+const struct routeaddr_type *
+netstat_rt_get_destination(const struct route_type *rtp)
+{
+ return (rtp->rt_destination);
+}
+
+const struct routeaddr_type *
+netstat_rt_get_gateway(const struct route_type *rtp)
+{
+ return (rtp->rt_gateway);
+}
+
+int
+netstat_rt_get_flags(const struct route_type *rtp)
+{
+ return (rtp->rt_flags);
+}
+
+u_int64_t
+netstat_rt_get_refs(const struct route_type *rtp)
+{
+ return (rtp->rt_refs);
+}
+
+u_int64_t
+netstat_rt_get_used(const struct route_type *rtp)
+{
+ return (rtp->rt_used);
+}
+
+const char *
+netstat_rt_get_interface(const struct route_type *rtp)
+{
+ return (rtp->rt_interface);
+}
+
+u_int64_t
+netstat_rt_get_expire(const struct route_type *rtp)
+{
+ return (rtp->rt_expire);
+}
+
+u_int32_t
+netstat_rt_get_mtu(const struct route_type *rtp)
+{
+ return (rtp->rt_mtu);
+}
+
+u_int64_t
+netstat_rt_get_address(const struct route_type *rtp)
+{
+ return (rtp->rt_address);
+}
+
static const char *icmpnames[ICMP_MAXTYPE + 1] = {
"echo reply", /* RFC 792 */
"#1",
More information about the p4-projects
mailing list