From owner-p4-projects@FreeBSD.ORG Fri May 9 18:26:36 2008 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 1E62A106567E; Fri, 9 May 2008 18:26:36 +0000 (UTC) Delivered-To: perforce@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id D4842106567A for ; Fri, 9 May 2008 18:26:35 +0000 (UTC) (envelope-from rpaulo@FreeBSD.org) Received: from repoman.freebsd.org (repoman.freebsd.org [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id C26268FC13 for ; Fri, 9 May 2008 18:26:35 +0000 (UTC) (envelope-from rpaulo@FreeBSD.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.1/8.14.1) with ESMTP id m49IQZTO025188 for ; Fri, 9 May 2008 18:26:35 GMT (envelope-from rpaulo@FreeBSD.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.1/8.14.1/Submit) id m49IQZsN025186 for perforce@freebsd.org; Fri, 9 May 2008 18:26:35 GMT (envelope-from rpaulo@FreeBSD.org) Date: Fri, 9 May 2008 18:26:35 GMT Message-Id: <200805091826.m49IQZsN025186@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to rpaulo@FreeBSD.org using -f From: Rui Paulo To: Perforce Change Reviews Cc: Subject: PERFORCE change 141377 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 09 May 2008 18:26:36 -0000 http://perforce.freebsd.org/chv.cgi?CH=141377 Change 141377 by rpaulo@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 ==== //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 +#include #include #include #include #include #include -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 #include +#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 #include #include #include #include +#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); + } +}