PERFORCE change 163664 for review

Gabor Pali pgj at FreeBSD.org
Sat Jun 6 21:13:13 UTC 2009


http://perforce.freebsd.org/chv.cgi?CH=163664

Change 163664 by pgj at petymeg-current on 2009/06/06 21:12:40

	- Refine definition of socket_type:
	  - Add address_type as an abstraction for addresses of a socket
	  - Remove st_type, because st_family and st_protocol is possibly
	    enough for socket type identification
	  - Add some "mixed properties", purely for experimentation
	- Fix definitions for some internal functions
	- Add some accessor functions for socket_type
	- Add netstat.c (missed from the last submit):
	  - Add netstat_socket() for socket information retrieval:
	    - It is planned to be a common interface, and it can be
	      used to extract information via KVM (by using the
	      NETSTAT_SOCKET_KVM flag) -- tries to hide sysctl calls,
	      so they can be replaced by some direct access for live
	      monitoring
	    - Parameters are similar to socket()'s (domain/type/protocol),
	      so there can be different type of sockets can be queried in
	      one call (suggested by: rwatson)

Affected files ...

.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat.c#1 add
.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat.h#4 edit
.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_internal.h#4 edit
.. //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_util.c#4 edit

Differences ...

==== //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat.h#4 (text+ko) ====

@@ -13,23 +13,26 @@
 #define NETSTAT_ERROR_KVM_NOSYMBOL  4
 #define NETSTAT_ERROR_KVM_SHORTREAD 5
 
+/* Flags for netstat_socket(): */
+#define	NETSTAT_SOCKET_KVM	    1	/* Use KVM. */
+
 struct socket_type;
 struct socket_type_list;
 
 __BEGIN_DECLS
-const char	    *netstat_strerror(int);
+const char		    *netstat_strerror(int);
 
 struct socket_type_list	    *netstat_stl_alloc(void);
-struct socket_type	    *netstat_stl_first(struct socket_type_list *);
-struct socket_type	    *netstat_stl_next(struct socket_type *);
-struct socket_type	    *netstat_stl_find(struct socket_type_list *,
-				int, const char *);
+struct socket_type	    *netstat_stl_first(struct socket_type_list *list);
+struct socket_type	    *netstat_stl_next(struct socket_type *list);
+struct socket_type	    *netstat_stl_find(struct socket_type_list *list,
+				unsigned short family, const char *name);
 
-void	netstat_stl_free(struct socket_type_list *);
-int	netstat_stl_geterror(struct socket_type_list *);
+void	netstat_stl_free(struct socket_type_list *list);
+int	netstat_stl_geterror(struct socket_type_list *list);
 
-int	netstat_sysctl_socket(struct socket_type_list *, int);
-int	netstat_kvm_socket(struct socket_type_list *, void *);
+int	netstat_socket(int domain, int type, int protocol,
+	    struct socket_type_list *, int flags, void *kvm_handle);
 __END_DECLS
 
 #endif /* !_NETSTAT_H_ */

==== //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_internal.h#4 (text+ko) ====

@@ -3,26 +3,41 @@
 #define _NETSTAT_INTERNAL_H_
 
 #include <sys/queue.h>
+#include <sys/sockbuf.h>
 #include <sys/socket.h>
+#include <sys/socketvar.h>
+#include <sys/un.h>
+#include <sys/unpcb.h>
 #include <kvm.h>
 
+/* Address type:
+ *  local, foreign, node (Netgraph), raw (domain)
+ */
+struct address_type {
+	int			    at_type;
+	struct sockaddr_storage	    at_address;
+};
+
+/* Socket (PCB) type: a connection. */
 struct socket_type {
-	/* static properties */
+	/* Static properties. */
 	unsigned short	st_family;
 	unsigned short	st_protocol;
-	int		st_type;
 	int		st_flags;
 	char		st_name[SOCKTYPE_MAXNAME];
 
-	struct sockaddr_storage	    *st_local_address;
-	/* local address count, XXX: SCTP */
-	int			    st_la_cnt;
-	struct sockaddr_storage	    *st_remote_address;
-	/* remote address count */
-	int			    st_ra_cnt;
+	/* Mixed properties, needed for different reasons, to be refined
+	 * continuously.
+	 */
+	struct address_type	    *st_address;    /* address(es) */
+	int			    st_addrcnt;	    /* address count */
 
 	struct sockcred		    st_credentials;
+	struct xsockbuf		    st_rcv;	    /* receive queue */
+	struct xsockbuf		    st_snd;	    /* send queue */
 
+	struct xunpcb		    xup;	    /* xun */
+
 	/* list of types */
 	LIST_ENTRY(socket_type) st_list;
 };
@@ -32,12 +47,15 @@
 	int			    stl_error;
 };
 
-int kread(kvm_t *, void *, void *, size_t, size_t);
-int kread_string(kvm_t *, void *, char *, int);
+int kread(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);
+
+void		    _netstat_stl_empty(struct socket_type_list *list);
+struct socket_type  *_netstat_st_allocate(struct socket_type_list *list,
+			unsigned short family, unsigned short protocol,
+			const char *name);
+void		    _netstat_st_reset_stats(struct socket_type *list);
 
-void		    _netstat_stl_empty(struct socket_type_list *);
-struct socket_type  *_netstat_st_allocate(struct socket_type_list *,
-		      int, const char *);
-void		    _netstat_ns_reset_stats(struct socket_type *);
+int	sotoxsocket(struct socket *, struct xsocket *);
 
 #endif /* !_NETSTAT_INTERNAL_H_ */

==== //depot/projects/soc2009/pgj_libstat/src/lib/libnetstat/netstat_util.c#4 (text+ko) ====

@@ -1,5 +1,6 @@
 
 #include <sys/queue.h>
+#include <sys/socket.h>
 #include <sys/types.h>
 #include <kvm.h>
 #include <stdlib.h>
@@ -9,13 +10,11 @@
 #include "netstat_internal.h"
 
 int
-kread(kvm_t *kvm, void *kvm_pointer, void *address, size_t size,
-    size_t offset)
+kread(kvm_t *kvm, u_long kvm_pointer, void *address, size_t size)
 {
 	ssize_t ret;
 
-	ret = kvm_read(kvm, (unsigned long)kvm_pointer + offset, address,
-	    size);
+	ret = kvm_read(kvm, kvm_pointer, address, size);
 	if (ret < 0)
 		return (NETSTAT_ERROR_KVM);
 	if ((size_t)ret != size)
@@ -24,14 +23,14 @@
 }
 
 int
-kread_string(kvm_t *kvm, void *kvm_pointer, char *buffer, int buflen)
+kread_string(kvm_t *kvm, u_long kvm_pointer, char *buffer, int buflen)
 {
 	ssize_t ret;
 	int i;
 
 	for (i = 0; i < buflen; i++) {
-		ret = kvm_read(kvm, (unsigned long)kvm_pointer + i,
-		    &(buffer[i]), sizeof(char));
+		ret = kvm_read(kvm, kvm_pointer + i, &(buffer[i]),
+		    sizeof(char));
 		if (ret < 0)
 			return (NETSTAT_ERROR_KVM);
 		if ((size_t)ret != sizeof(char))
@@ -114,14 +113,14 @@
 }
 
 struct socket_type *
-netstat_stl_find(struct socket_type_list *list, int st_type,
+netstat_stl_find(struct socket_type_list *list, unsigned short family,
     const char *name)
 {
 	struct socket_type *stp;
 
-	/* XXX: no "any" */
 	LIST_FOREACH(stp, &list->stl_list, st_list) {
-		if ((stp->st_type == st_type) &&
+		if ((stp->st_family == family ||
+		    family == PF_UNSPEC) &&
 		    strcmp(stp->st_name, name) == 0)
 			return (stp);
 	}
@@ -129,8 +128,8 @@
 }
 
 struct socket_type *
-_netstat_st_allocate(struct socket_type_list *list, int st_type,
-    const char *name)
+_netstat_st_allocate(struct socket_type_list *list, unsigned short family,
+    unsigned short protocol, const char *name)
 {
 	struct socket_type *stp;
 
@@ -140,7 +139,9 @@
 
 	bzero(stp, sizeof(*stp));
 
-	stp->st_type = st_type;
+	stp->st_family = family;
+	stp->st_protocol = protocol;
+	stp->st_flags = 0;
 	strlcpy(stp->st_name, name, SOCKTYPE_MAXNAME);
 	LIST_INSERT_HEAD(&list->stl_list, stp, st_list);
 	return (stp);
@@ -149,6 +150,20 @@
 void
 _netstat_st_reset_stats(struct socket_type *stp)
 {
+	/* XXX: empty */
+}
+
+/* Accessor functions. */
+int
+netstat_st_get_family(const struct socket_type *stp)
+{
+	return (stp->st_family);
+}
+
+int
+netstat_st_get_protocol(const struct socket_type *stp)
+{
+	return (stp->st_protocol);
 }
 
 const char *
@@ -157,9 +172,9 @@
 	return (stp->st_name);
 }
 
-int
-netstat_st_get_type(const struct socket_type *stp)
+/* XXX: hack alert :) */
+void *
+netstat_st_get_pcb(const struct socket_type *stp)
 {
-	return (stp->st_type);
+	return (void *)(&stp->xup);
 }
-


More information about the p4-projects mailing list