PERFORCE change 141377 for review

Rui Paulo rpaulo at FreeBSD.org
Fri May 9 18:26:36 UTC 2008


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

Change 141377 by rpaulo at rpaulo_epsilon on 2008/05/09 18:25:44

	Groundwork checkpoint.
	* Add P4 ids.
	* Enable WARNS=5.
	* Setup pcap initialization. We are now capturing packets.
	* Add some debugging output.
	* Add more command line options.

Affected files ...

.. //depot/projects/soc2008/rpaulo-tcpad/Makefile#2 edit
.. //depot/projects/soc2008/rpaulo-tcpad/device.c#2 edit
.. //depot/projects/soc2008/rpaulo-tcpad/device.h#1 add
.. //depot/projects/soc2008/rpaulo-tcpad/handler.c#1 add
.. //depot/projects/soc2008/rpaulo-tcpad/handler.h#1 add
.. //depot/projects/soc2008/rpaulo-tcpad/linkhdr.c#2 edit
.. //depot/projects/soc2008/rpaulo-tcpad/linkhdr.h#1 add
.. //depot/projects/soc2008/rpaulo-tcpad/main.c#2 edit

Differences ...

==== //depot/projects/soc2008/rpaulo-tcpad/Makefile#2 (text+ko) ====

@@ -1,5 +1,9 @@
+# $P4: //depot/projects/soc2008/rpaulo-tcpad/Makefile#2 $
+
 PROG=tcpad
-SRCS=main.c device.c linkhdr.c
+SRCS=main.c device.c linkhdr.c handler.c
+WARNS=5
+CFLAGS+=-DDEBUG
 LDADD=-lpcap
 
 .include <bsd.prog.mk>

==== //depot/projects/soc2008/rpaulo-tcpad/device.c#2 (text+ko) ====

@@ -22,20 +22,19 @@
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $P4: //depot/projects/soc2008/rpaulo-tcpad/device.c#2 $
  */
 
+#include <err.h>
+#include <stdio.h>
 #include <sys/param.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <pcap.h>
 
-int
-device_lookup(const char *dev)
-{
-	
-}
-
+#include "device.h"
 /**
  * @brief
  * List all devices found by pcap.
@@ -43,19 +42,21 @@
  * @param verbose	Verbose toggle
  */
 void
-device_listall(int verbose)
+device_listall(void)
 {
 	pcap_if_t *iface, *ifaceFirst;
 	pcap_addr_t *ifaddr;
 	struct sockaddr *addr;
 	struct sockaddr_in *sin;
 	struct sockaddr_in6 *sin6;
-	struct bpf_program fp;
 	char addrbuf[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
 	char errbuf[PCAP_ERRBUF_SIZE];
 
 	if (pcap_findalldevs(&iface, errbuf) == -1)
 		errx(1, "pcap_findalldevs: %s", errbuf);
+
+	if (!iface)
+		errx(1, "no suitable interfaces found");
 	
 	ifaceFirst = iface;
 	for (; iface; iface = iface->next) {
@@ -63,28 +64,23 @@
 		if (iface->description)
 			printf(", description %s", iface->description);
 		printf("\n");
-		if (verbose) {
-			for (ifaddr = iface->addresses; ifaddr; 
-			    ifaddr = ifaddr->next) {
-				addr = iface->addresses->addr;
-				switch (addr->sa_family) {
-				case AF_INET:
-					sin = (struct sockaddr_in *)addr;
-					inet_ntop(AF_INET, &sin->sin_addr,
+		for (ifaddr = iface->addresses; ifaddr; ifaddr = ifaddr->next) {
+			addr = ifaddr->addr;
+			switch (addr->sa_family) {
+			case AF_INET:
+				sin = (struct sockaddr_in *)addr;
+				inet_ntop(AF_INET, &sin->sin_addr,
+				    addrbuf, sizeof(addrbuf));
+				printf("\tIPv4 address: %s\n", addrbuf);
+				break;
+			case AF_INET6:
+				sin6 = (struct sockaddr_in6 *)addr;
+				inet_ntop(AF_INET6, &sin6->sin6_addr,
 					    addrbuf, sizeof(addrbuf));
-					printf("\tIPv4 address: %s\n",
-					    addrbuf);
-					break;
-				case AF_INET6:
-					sin6 = (struct sockaddr_in6 *)addr;
-					inet_ntop(AF_INET6, &sin6->sin6_addr,
-					    addrbuf, sizeof(addrbuf));
-					printf("\tIPv6 address: %s\n", 
-						addrbuf);
-					break;
-				default:
-					break;
-				}
+				printf("\tIPv6 address: %s\n", addrbuf);
+				break;
+			default:
+				break;
 			}
 		}
 	}

==== //depot/projects/soc2008/rpaulo-tcpad/linkhdr.c#2 (text+ko) ====

@@ -22,11 +22,15 @@
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $P4: //depot/projects/soc2008/rpaulo-tcpad/linkhdr.c#2 $
  */
 
 #include <net/ethernet.h>
 #include <pcap.h>
 
+#include "linkhdr.h"
+
 struct linktypes {
 	int type;
 	unsigned int skip;
@@ -35,6 +39,7 @@
 static struct linktypes linktypes[] = {
     { DLT_NULL,		0 },
     { DLT_EN10MB,	ETHER_HDR_LEN },	/* from ethernet.h */
+    { DLT_PPP,		4 },			/* XXX */
 
     { -1, 0 }
 };
@@ -42,7 +47,7 @@
 
 /**
  * @brief
- * Find the number link layer header length.
+ * Find the link layer header length.
  *
  * @param dlt	Data-link level type code
  *
@@ -50,7 +55,7 @@
  *
  * @return 	Bytes to skip
  */
-static int
+int
 linkhdr_headerlen(int dlt)
 {
 	int i;
@@ -64,15 +69,11 @@
 }
 
 
-unsigned char *
-linkhdr_remove(unsigned char *bytes, int dlt)
+const unsigned char *
+linkhdr_remove(const unsigned char *bytes, unsigned int skip)
 {
-	int skip;
-	
-	skip = linkhdr_headerlen(dlt);
-	
 	/* XXX: more computation needed for some interfaces, e.g.: SLIP, 
 	  PPP, etc. */
 	
 	return (bytes + skip);
-}+}

==== //depot/projects/soc2008/rpaulo-tcpad/main.c#2 (text+ko) ====

@@ -22,28 +22,59 @@
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * $P4: //depot/projects/soc2008/rpaulo-tcpad/main.c#2 $
  */
 
+#include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <pcap.h>
 
+#include "device.h"
+#include "linkhdr.h"
+#include "handler.h"
+
 static void
 usage(void)
 {
 	fprintf(stderr, "%s\n", pcap_lib_version());
-	fprintf(stderr, "%s: [-pD] [-i interface]", getprogname());
+	fprintf(stderr, "%s: [-pD] [-i interface] [-s snaplen]\n",
+	    getprogname());
+	exit(1);
 }
 
 int
-main(int argc, char *argv[], char *envp[])
+main(int argc, char *argv[])
 {
+	int promisc;
+	int snaplen;
 	int ch;
+	char *interface;
+	char errbuf[PCAP_ERRBUF_SIZE];
+	pcap_t *p;
+	struct bpf_program fp;
+	char filter[] = "ip proto \\tcp";
 	
-	while ((ch = getopt(argc, argv, "pDi:")) != -1) {
+	promisc = 1;
+	snaplen = 100;
+	interface = NULL;
+	while ((ch = getopt(argc, argv, "pDi:s:l")) != -1) {
 		switch (ch) {
-			/* TODO: option processing */
+		case 'p':
+			promisc = 0;
+			break;
+		case 'i':
+			interface = optarg;
+			break;
+		case 's':
+			snaplen = atoi(optarg);
+			break;
+		case 'l':
+			device_listall();
+			exit(0);
+			break;
 		case '?':
 		default:
 			usage();
@@ -52,4 +83,21 @@
 	}
 	argc -= optind;
 	argv += optind;
-}+
+	if (interface == NULL)
+		errx(1, "interface not specified");
+	
+	p = pcap_open_live(interface, snaplen, promisc, 100, errbuf);
+	if (p == NULL)
+		err(1, "pcap_open_live");
+
+	/* XXX: check for ICMP too */
+	if (pcap_compile(p, &fp, filter, 1, 0) == -1)
+		errx(1, "pcap_compile: %s", pcap_geterr(p));
+
+	pcap_setfilter(p, &fp);
+
+	for (;;) {
+		pcap_dispatch(p, -1, tcpad_pcaphandler, NULL);
+	}
+}


More information about the p4-projects mailing list