Skip to content
Snippets Groups Projects
Commit 54a3c785 authored by Gregory Nutt's avatar Gregory Nutt
Browse files

First check-in of Lazlo's PF_PACKET 'raw' socket implementation

parent d416a8c8
No related branches found
No related tags found
No related merge requests found
......@@ -26,6 +26,7 @@ source "$APPSDIR/examples/mm/Kconfig"
source "$APPSDIR/examples/modbus/Kconfig"
source "$APPSDIR/examples/mount/Kconfig"
source "$APPSDIR/examples/mtdpart/Kconfig"
source "$APPSDIR/examples/netpkt/Kconfig"
source "$APPSDIR/examples/nettest/Kconfig"
source "$APPSDIR/examples/nrf24l01_term/Kconfig"
source "$APPSDIR/examples/nsh/Kconfig"
......
......@@ -130,6 +130,10 @@ ifeq ($(CONFIG_EXAMPLES_MTDPART),y)
CONFIGURED_APPS += examples/mtdpart
endif
ifeq ($(CONFIG_EXAMPLES_NETPKT),y)
CONFIGURED_APPS += examples/netpkt
endif
ifeq ($(CONFIG_EXAMPLES_NETTEST),y)
CONFIGURED_APPS += examples/nettest
endif
......
############################################################################
# apps/examples/Makefile
#
# Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
# Copyright (C) 2011-2014 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
......@@ -39,13 +39,12 @@
SUBDIRS = adc buttons can cc3000 cpuhog cxxtest dhcpd discover elf
SUBDIRS += flash_test ftpc ftpd hello helloxx hidkbd igmp i2schar json
SUBDIRS += keypadtest lcdrw mm modbus mount mtdpart nettest nrf24l01_term
SUBDIRS += nsh null nx nxconsole nxffs nxflat nxhello nximage nxlines
SUBDIRS += nxtext ostest pashello pipe poll posix_spawn pwm qencoder random
SUBDIRS += relays rgmp romfs sendmail serialblaster serloop serialrx slcd
SUBDIRS += smart smart_test tcpecho telnetd thttpd tiff touchscreen udp uip
SUBDIRS += usbserial usbterm watchdog wget wgetjson xmlrpc
SUBDIRS += keypadtest lcdrw mm modbus mount mtdpart netpkt nettest
SUBDIRS += nrf24l01_term nsh null nx nxconsole nxffs nxflat nxhello nximage
SUBDIRS += nxlines nxtext ostest pashello pipe poll posix_spawn pwm qencoder
SUBDIRS += random relays rgmp romfs sendmail serialblaster serloop serialrx
SUBDIRS += slcd smart smart_test tcpecho telnetd thttpd tiff touchscreen udp
SUBDIRS += uip usbserial usbterm watchdog wget wgetjson xmlrpc
# Sub-directories that might need context setup. Directories may need
# context setup for a variety of reasons, but the most common is because
......@@ -56,7 +55,7 @@ CNTXTDIRS = pwm
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
CNTXTDIRS += adc can cc3000 cpuhog cxxtest dhcpd discover flash_test ftpd
CNTXTDIRS += hello helloxx i2schar json keypadtestmodbus lcdrw mtdpart
CNTXTDIRS += nettest nx nxhello nximage nxlines nxtext nrf24l01_term
CNTXTDIRS += netpkt nettest nx nxhello nximage nxlines nxtext nrf24l01_term
CNTXTDIRS += ostest random relays qencoder serialblasterslcd serialrx
CNTXTDIRS += smart_test tcpecho telnetd tiff touchscreen usbterm watchdog
CNTXTDIRS += wgetjson
......
......@@ -658,6 +658,11 @@ examples/mtdpart
* CONFIG_EXAMPLES_MTDPART_NEBLOCKS - This value gives the nubmer of erase
blocks in MTD RAM device.
examples/netpkt
^^^^^^^^^^^^^^^
A test of AF_PACKET, "raw" sockets. Contributed by Lazlo Sitzer.
examples/nettest
^^^^^^^^^^^^^^^^
......
#
# For a description of the syntax of this configuration file,
# see misc/tools/kconfig-language.txt
#
config EXAMPLES_NETPKT
bool "Network packet socket example"
default n
depends on NET_PKT
---help---
Enable the network packet socket example
if EXAMPLES_NETPKT
endif
############################################################################
# apps/examples/netpkt/Makefile
#
# Copyright (C) 2014 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 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.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# Network packet socket example
APPNAME = netpkt
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
# MDIO tool
ASRCS =
CSRCS = netpkt_main.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(CONFIG_WINDOWS_NATIVE),y)
BIN = ..\..\libapps$(LIBEXT)
else
ifeq ($(WINTOOL),y)
BIN = ..\\..\\libapps$(LIBEXT)
else
BIN = ../../libapps$(LIBEXT)
endif
endif
ROOTDEPPATH = --dep-path .
# Common build
VPATH =
all: .built
.PHONY: clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
$(call ARCHIVE, $(BIN), $(OBJS))
@touch .built
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
$(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat: $(DEPCONFIG) Makefile
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
context: $(BUILTIN_REGISTRY)$(DELIM)$(APPNAME)_main.bdat
else
context:
endif
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
$(call DELFILE, .built)
$(call CLEAN)
distclean: clean
$(call DELFILE, Make.dep)
$(call DELFILE, .depend)
-include Make.dep
/****************************************************************************
* net/recvfrom.c
*
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
* Author: Lazlo <dlsitzer@gmail.comg>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netpacket/packet.h>
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: psock_create
****************************************************************************/
static int psock_create(void)
{
int sd;
struct sockaddr_ll addr;
int addrlen = sizeof(addr);
sd = socket(AF_PACKET, SOCK_RAW, 0);
if (sd < 0)
{
perror("ERROR: failed to create packet socket");
return -1;
}
/* Prepare sockaddr struct */
addr.sll_family = AF_PACKET;
addr.sll_ifindex = 0;
if (bind(sd, (const struct sockaddr *)&addr, addrlen) < 0)
{
perror("ERROR: binding socket failed");
close(sd);
return -1;
}
return sd;
}
/****************************************************************************
* Name: print_buf
****************************************************************************/
static void print_buf(const uint8_t *buf, int len)
{
int i;
for (i = 0; i < len; i++)
{
printf("%02X", buf[i]);
if ((i+1) % 16 == 0 || (i+1) == len)
{
printf("\n");
}
else if ((i+1) % 8 == 0)
{
printf(" ");
}
else
{
printf(" ");
}
}
}
/****************************************************************************
* Name: netpkt_usage
****************************************************************************/
static void netpkt_usage(void)
{
printf("usage: netpkt options\n");
printf("\n");
printf(" -a transmit and receive\n");
printf(" -r receive\n");
printf(" -t transmit\n");
printf(" -v verbose\n");
printf("\n");
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: netpkt_main
****************************************************************************/
int netpkt_main(int argc, char **argv)
{
int sd;
int i;
int txc;
int rxc;
uint8_t *buf;
const int buflen = 128;
const char da[6] = {0xf0, 0xde, 0xf1, 0x02, 0x43, 0x01};
const char sa[6] = {0x00, 0xe0, 0xde, 0xad, 0xbe, 0xef};
int opt;
int verbose = 0;
int do_rx = 0;
int do_rxtimes = 3;
int do_tx = 0;
int do_txtimes = 3;
if (argc == 1)
{
netpkt_usage();
return -1;
}
/* Parse arguments */
while ((opt = getopt(argc, argv, "artv")) != -1)
{
switch(opt)
{
case 'a':
do_rx = 1;
do_tx = 1;
break;
case 'r':
do_rx = 1;
break;
case 't':
do_tx = 1;
break;
case 'v':
verbose = 1;
break;
default:
netpkt_usage();
return -1;
}
}
sd = psock_create();
if (do_tx)
{
if (verbose)
{
printf("Testing write() (%d times)\n", do_txtimes);
}
buf = malloc(buflen);
memset(buf, 0, buflen);
memcpy(buf, da, 6);
memcpy(buf+6, sa, 6);
for (i = 0; i < do_txtimes; i++)
{
if ((txc = write(sd, buf, buflen)) < 0)
{
perror("ERROR: write failed");
free(buf);
close(sd);
return -1;
}
else
{
if (verbose)
{
printf("transmited %d octets\n", txc);
print_buf(buf, txc);
}
}
}
free(buf);
}
if (do_rx)
{
if (verbose)
{
printf("Testing read() (%d times)\n", do_rxtimes);
}
buf = malloc(buflen);
memset(buf, 0, buflen);
for (i = 0; i < do_rxtimes; i++)
{
rxc = read(sd, buf, buflen);
if (rxc < 0)
{
perror("ERROR: read failed");
free(buf);
close(sd);
return -1;
}
else if (rxc == 0)
{
/* ignore silently */
}
else
{
if (verbose)
{
printf("received %d octets\n", rxc);
print_buf(buf, rxc);
}
}
}
free(buf);
}
close(sd);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment