From e894bd703a2010e57d49b6cdd8ce7611d4eb1369 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 27 Jan 2015 13:41:45 -0600 Subject: [PATCH] Add a simple test of the Unix domain sockets --- examples/Kconfig | 1 + examples/Make.defs | 4 + examples/Makefile | 4 +- examples/README.txt | 14 ++ examples/ustream/.gitignore | 14 ++ examples/ustream/Kconfig | 19 +++ examples/ustream/Makefile | 173 ++++++++++++++++++++++++ examples/ustream/ustream.h | 53 ++++++++ examples/ustream/ustream_client.c | 191 +++++++++++++++++++++++++++ examples/ustream/ustream_server.c | 212 ++++++++++++++++++++++++++++++ 10 files changed, 683 insertions(+), 2 deletions(-) create mode 100644 examples/ustream/.gitignore create mode 100644 examples/ustream/Kconfig create mode 100644 examples/ustream/Makefile create mode 100644 examples/ustream/ustream.h create mode 100644 examples/ustream/ustream_client.c create mode 100644 examples/ustream/ustream_server.c diff --git a/examples/Kconfig b/examples/Kconfig index 6714e6351..329077071 100644 --- a/examples/Kconfig +++ b/examples/Kconfig @@ -75,6 +75,7 @@ source "$APPSDIR/examples/discover/Kconfig" source "$APPSDIR/examples/webserver/Kconfig" source "$APPSDIR/examples/usbserial/Kconfig" source "$APPSDIR/examples/usbterm/Kconfig" +source "$APPSDIR/examples/ustream/Kconfig" source "$APPSDIR/examples/watchdog/Kconfig" source "$APPSDIR/examples/wget/Kconfig" source "$APPSDIR/examples/wgetjson/Kconfig" diff --git a/examples/Make.defs b/examples/Make.defs index 2c23a68e6..520ba8cc0 100644 --- a/examples/Make.defs +++ b/examples/Make.defs @@ -318,6 +318,10 @@ ifeq ($(CONFIG_EXAMPLES_USBTERM),y) CONFIGURED_APPS += examples/usbterm endif +ifeq ($(CONFIG_EXAMPLES_USTREAM),y) +CONFIGURED_APPS += examples/ustream +endif + ifeq ($(CONFIG_EXAMPLES_WATCHDOG),y) CONFIGURED_APPS += examples/watchdog endif diff --git a/examples/Makefile b/examples/Makefile index 8d39c1a5f..ddcea687c 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -45,7 +45,7 @@ SUBDIRS += nxffs nxflat nxhello nximage nxlines nxtext ostest pashello pipe SUBDIRS += poll posix_spawn pwm qencoder random relays rgmp romfs sendmail SUBDIRS += serialblaster serloop serialrx slcd smart smart_test tcpecho SUBDIRS += telnetd thttpd tiff timer touchscreen udp usbserial usbterm -SUBDIRS += watchdog webserver wget wgetjson xmlrpc +SUBDIRS += ustream watchdog webserver 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 @@ -59,7 +59,7 @@ CNTXTDIRS += djoystick flash_test ftpd hello helloxx i2schar json keypadtest CNTXTDIRS += ltdc modbus lcdrw mtdpart mtdrwb netpkt nettest nx nxhello CNTXTDIRS += nximage nxlines nxtext nrf24l01_term ostest random relays CNTXTDIRS += qencoder serialblasters lcd serialrx smart_test tcpecho telnetd -CNTXTDIRS += tiff timer touchscreen usbterm watchdog wgetjson +CNTXTDIRS += tiff timer touchscreen usbterm ustream watchdog wgetjson endif all: nothing diff --git a/examples/README.txt b/examples/README.txt index 13586419b..c9436df91 100644 --- a/examples/README.txt +++ b/examples/README.txt @@ -1976,6 +1976,20 @@ examples/usbterm Prolifics emulation (not defined) and the CDC serial implementation (when defined). CONFIG_USBDEV_TRACE_INITIALIDSET. +examples/ustream +^^^^^^^^^^^^^^^^ + + This is the same test as examples/nettest, but using Unix domain + sockets. + + Dependencies: + CONFIG_NET_LOCAL - Depends on support for Unix domain sockets + + Configuration: + CONFIG_EXAMPLES_USTREAM - Enables the Unix domain socket example. + CONFIG_EXAMPLES_USTREAM_ADDR - Specifics the Unix domain address. + Default "/var/fifo/fifo". + examples/watchdog ^^^^^^^^^^^^^^^^^ diff --git a/examples/ustream/.gitignore b/examples/ustream/.gitignore new file mode 100644 index 000000000..52ae9aa90 --- /dev/null +++ b/examples/ustream/.gitignore @@ -0,0 +1,14 @@ +/Make.dep +/.depend +/.built +/*.asm +/*.obj +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src +/*.hobj +/*.exe +/*.dSYM diff --git a/examples/ustream/Kconfig b/examples/ustream/Kconfig new file mode 100644 index 000000000..5731a7c5a --- /dev/null +++ b/examples/ustream/Kconfig @@ -0,0 +1,19 @@ +# +# For a description of the syntax of this configuration file, +# see misc/tools/kconfig-language.txt. +# + +config EXAMPLES_USTREAM + bool "Unix domain socket example" + default n + depends on NET_LOCAL + ---help--- + Enable the Unix domain SOCK_STREAM test example + +if EXAMPLES_USTREAM + +config EXAMPLES_USTREAM_ADDR + string "Unix domain address" + default "/var/fifo/fifo" + +endif # EXAMPLES_USTREAM diff --git a/examples/ustream/Makefile b/examples/ustream/Makefile new file mode 100644 index 000000000..1b399860c --- /dev/null +++ b/examples/ustream/Makefile @@ -0,0 +1,173 @@ +############################################################################ +# examples/ustream/Makefile +# +# Copyright (C) 2015 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# 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 + +# Basic Unix domain networking test + +SERVER_ASRCS = +SERVER_AOBJS = $(SERVER_ASRCS:.S=$(OBJEXT)) + +SERVER_CSRCS = +SERVER_COBJS = $(SERVER_CSRCS:.c=$(OBJEXT)) + +SERVER_MAINSRC = ustream_server.c +SERVER_MAINOBJ = $(SERVER_MAINSRC:.c=$(OBJEXT)) + +SERVER_SRCS = $(SERVER_ASRCS) $(SERVER_CSRCS) $(SERVER_MAINSRC) +SERVER_OBJS = $(SERVER_AOBJS) $(SERVER_COBJS) + +SERVER_PROGNAME = server$(EXEEXT) + +SERVER_APPNAME = server +SERVER_PRIORITY = SCHED_PRIORITY_DEFAULT +SERVER_STACKSIZE = 2048 + +CLIENT_ASRCS = +CLIENT_AOBJS = $(CLIENT_ASRCS:.S=$(OBJEXT)) + +CLIENT_CSRCS = +CLIENT_COBJS = $(CLIENT_CSRCS:.c=$(OBJEXT)) + +CLIENT_MAINSRC = ustream_client.c +CLIENT_MAINOBJ = $(CLIENT_MAINSRC:.c=$(OBJEXT)) + +CLIENT_SRCS = $(CLIENT_ASRCS) $(CLIENT_CSRCS) $(CLIENT_MAINSRC) +CLIENT_OBJS = $(CLIENT_AOBJS) $(CLIENT_COBJS) + +CLIENT_PROGNAME = server$(EXEEXT) + +CLIENT_APPNAME = client +CLIENT_PRIORITY = SCHED_PRIORITY_DEFAULT +CLIENT_STACKSIZE = 2048 + +AOBJS = $(CLIENT_AOBJS) $(SERVER_AOBJS) +COBJS = $(CLIENT_COBJS) $(CLIENT_MAINOBJ) $(SERVER_COBJS) $(SERVER_MAINOBJ) + +SRCS = $(CLIENT_SRCS) $(SERVER_SRCS) +OBJS = $(AOBJS) $(COBJS) + +ifneq ($(CONFIG_BUILD_KERNEL),y) + SERVER_OBJS += $(SERVER_MAINOBJ) + CLIENT_OBJS += $(CLIENT_MAINOBJ) +endif + +ifeq ($(CONFIG_WINDOWS_NATIVE),y) + USTREAM_BIN = ..\..\libapps$(LIBEXT) +else +ifeq ($(WINTOOL),y) + USTREAM_BIN = ..\\..\\libapps$(LIBEXT) +else + USTREAM_BIN = ../../libapps$(LIBEXT) +endif +endif + +ifeq ($(WINTOOL),y) + INSTALL_DIR = "${shell cygpath -w $(BIN_DIR)}" +else + INSTALL_DIR = $(BIN_DIR) +endif + +# Common build + +VPATH = + +all: .built +.PHONY: clean depend distclean + +$(AOBJS): %$(OBJEXT): %.S + $(call ASSEMBLE, $<, $@) + +$(COBJS): %$(OBJEXT): %.c + $(call COMPILE, $<, $@) + +.built: $(OBJS) + $(call ARCHIVE, $(USTREAM_BIN), $(SERVER_OBJS)) + $(Q) touch .built + +ifeq ($(CONFIG_BUILD_KERNEL),y) + +$(BIN_DIR)$(DELIM)$(CLIENT_PROGNAME): $(CLIENT_OBJS) + @echo "LD: $(CLIENT_PROGNAME)" + $(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(CLIENT_PROGNAME) $(ARCHCRT0OBJ) $(CLIENT_MAINOBJ) $(LDLIBS) + $(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(CLIENT_PROGNAME) + +$(BIN_DIR)$(DELIM)$(SERVER_PROGNAME): $(OBJS) $(SERVER_MAINOBJ) + @echo "LD: $(SERVER_PROGNAME)" + $(Q) $(LD) $(LDELFFLAGS) $(LDLIBPATH) -o $(INSTALL_DIR)$(DELIM)$(SERVER_PROGNAME) $(ARCHCRT0OBJ) $(SERVER_MAINOBJ) $(LDLIBS) + $(Q) $(NM) -u $(INSTALL_DIR)$(DELIM)$(SERVER_PROGNAME) + +install: $(BIN_DIR)$(DELIM)$(CLIENT_PROGNAME) $(BIN_DIR)$(DELIM)$(SERVER_PROGNAME) + +else +install: + +endif + +ifeq ($(CONFIG_NSH_BUILTIN_APPS),y) + +$(BUILTIN_REGISTRY)$(DELIM)$(CLIENT_APPNAME)_main.bdat: $(DEPCONFIG) Makefile + $(call REGISTER,$(CLIENT_APPNAME),$(CLIENT_PRIORITY),$(CLIENT_STACKSIZE),$(CLIENT_APPNAME)_main) + +$(BUILTIN_REGISTRY)$(DELIM)$(SERVER_APPNAME)_main.bdat: $(DEPCONFIG) Makefile + $(call REGISTER,$(SERVER_APPNAME),$(SERVER_PRIORITY),$(SERVER_STACKSIZE),$(SERVER_APPNAME)_main) + +context: $(BUILTIN_REGISTRY)$(DELIM)$(CLIENT_APPNAME)_main.bdat $(BUILTIN_REGISTRY)$(DELIM)$(SERVER_APPNAME)_main.bdat + +else + +context: + +endif + +.depend: Makefile $(SERVER_SRCS) + @$(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SERVER_SRCS) >Make.dep + @touch $@ + +depend: .depend + +clean: + $(call DELFILE, .built) + $(call DELFILE, *.dSYM) + $(call CLEAN) + +distclean: clean + $(call DELFILE, Make.dep) + $(call DELFILE, .depend) + +-include Make.dep + diff --git a/examples/ustream/ustream.h b/examples/ustream/ustream.h new file mode 100644 index 000000000..2d4533c05 --- /dev/null +++ b/examples/ustream/ustream.h @@ -0,0 +1,53 @@ +/**************************************************************************** + * examples/ustream/ustream.h + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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. + * + ****************************************************************************/ + +#ifndef __APPS_EXAMPLES_USTREAM_H +#define __APPS_EXAMPLES_USTREAM_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define SENDSIZE 4096 + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#endif /* __APPS_EXAMPLES_USTREAM_H */ diff --git a/examples/ustream/ustream_client.c b/examples/ustream/ustream_client.c new file mode 100644 index 000000000..797a5a18c --- /dev/null +++ b/examples/ustream/ustream_client.c @@ -0,0 +1,191 @@ +/**************************************************************************** + * examples/ustream/ustream_client.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 Gregory Nutt 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 + +#include +#include + +#include +#include +#include +#include +#include + +#include "ustream.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int main(int argc, FAR char *argv[]) +#else +int client_main(int argc, char *argv[]) +#endif +{ + struct sockaddr_un myaddr; + socklen_t addrlen; + FAR char *outbuf; + FAR char *inbuf; + int sockfd; + int nbytessent; + int nbytesrecvd; + int totalbytesrecvd; + int ch; + int ret; + int i; + + /* Allocate buffers */ + + outbuf = (char*)malloc(SENDSIZE); + inbuf = (char*)malloc(SENDSIZE); + if (!outbuf || !inbuf) + { + printf("client: failed to allocate buffers\n"); + exit(1); + } + + /* Create a new Unix domain socket */ + + sockfd = socket(PF_LOCAL, SOCK_STREAM, 0); + if (sockfd < 0) + { + printf("client socket failure %d\n", errno); + goto errout_with_buffers; + } + + /* Connect the socket to the server */ + + addrlen = strlen(CONFIG_EXAMPLES_USTREAM_ADDR); + if (addrlen > UNIX_PATH_MAX - 1) + { + addrlen = UNIX_PATH_MAX - 1; + } + + myaddr.sun_family = AF_LOCAL; + strncpy(myaddr.sun_path, CONFIG_EXAMPLES_USTREAM_ADDR, addrlen); + myaddr.sun_path[addrlen] = '\0'; + + printf("client: Connecting...\n"); + addrlen += sizeof(sa_family_t) + 1; + ret = connect( sockfd, (struct sockaddr *)&myaddr, addrlen); + if (ret < 0) + { + printf("client: connect failure: %d\n", errno); + goto errout_with_socket; + } + + printf("client: Connected\n"); + + /* Initialize the buffer */ + + ch = 0x20; + for (i = 0; i < SENDSIZE; i++ ) + { + outbuf[i] = ch; + if (++ch > 0x7e) + { + ch = 0x20; + } + } + + /* Then send and receive one message */ + + printf("client: Sending %d bytes\n", SENDSIZE); + nbytessent = send(sockfd, outbuf, SENDSIZE, 0); + printf("client: Sent %d bytes\n", nbytessent); + + if (nbytessent < 0) + { + printf("client: send failed: %d\n", errno); + goto errout_with_socket; + } + else if (nbytessent != SENDSIZE) + { + printf("client: Bad send length: %d Expected: %d\n", nbytessent, SENDSIZE); + goto errout_with_socket; + } + + totalbytesrecvd = 0; + do + { + printf("client: Receiving...\n"); + nbytesrecvd = recv(sockfd, &inbuf[totalbytesrecvd], SENDSIZE - totalbytesrecvd, 0); + + if (nbytesrecvd < 0) + { + printf("client: recv failed: %d\n", errno); + goto errout_with_socket; + } + else if (nbytesrecvd == 0) + { + printf("client: The server closed the connection\n"); + goto errout_with_socket; + } + + totalbytesrecvd += nbytesrecvd; + printf("client: Received %d of %d bytes\n", totalbytesrecvd, SENDSIZE); + } + while (totalbytesrecvd < SENDSIZE); + + if (totalbytesrecvd != SENDSIZE) + { + printf("client: Bad recv length: %d Expected: %d\n", totalbytesrecvd, SENDSIZE); + goto errout_with_socket; + } + else if (memcmp(inbuf, outbuf, SENDSIZE) != 0) + { + printf("client: Received buffer does not match sent buffer\n"); + goto errout_with_socket; + } + + close(sockfd); + free(outbuf); + free(inbuf); + return 0; + +errout_with_socket: + close(sockfd); + +errout_with_buffers: + free(outbuf); + free(inbuf); + return 1; +} diff --git a/examples/ustream/ustream_server.c b/examples/ustream/ustream_server.c new file mode 100644 index 000000000..4432019a4 --- /dev/null +++ b/examples/ustream/ustream_server.c @@ -0,0 +1,212 @@ +/**************************************************************************** + * examples/ustream/ustream_server.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 Gregory Nutt 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 + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "ustream.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int main(int argc, FAR char *argv[]) +#else +int server_main(int argc, char *argv[]) +#endif +{ + struct sockaddr_un myaddr; + socklen_t addrlen; + FAR char *buffer; + int listensd; + int acceptsd; + int nbytesread; + int totalbytesread; + int nbytessent; + int ch; + int ret; + int i; + + /* Allocate a BIG buffer */ + + buffer = (char*)malloc(2*SENDSIZE); + if (!buffer) + { + printf("server: failed to allocate buffer\n"); + exit(1); + } + + /* Create a new Unix domain socket */ + + listensd = socket(PF_LOCAL, SOCK_STREAM, 0); + if (listensd < 0) + { + printf("server: socket failure: %d\n", errno); + goto errout_with_buffer; + } + + /* Bind the socket to a local address */ + + addrlen = strlen(CONFIG_EXAMPLES_USTREAM_ADDR); + if (addrlen > UNIX_PATH_MAX - 1) + { + addrlen = UNIX_PATH_MAX - 1; + } + + myaddr.sun_family = AF_LOCAL; + strncpy(myaddr.sun_path, CONFIG_EXAMPLES_USTREAM_ADDR, addrlen); + myaddr.sun_path[addrlen] = '\0'; + + addrlen += sizeof(sa_family_t) + 1; + ret = bind(listensd, (struct sockaddr*)&myaddr, addrlen); + if (ret < 0) + { + printf("server: bind failure: %d\n", errno); + goto errout_with_listensd; + } + + /* Listen for connections on the bound socket */ + + if (listen(listensd, 5) < 0) + { + printf("server: listen failure %d\n", errno); + goto errout_with_listensd; + } + + /* Accept only one connection */ + + printf("server: Accepting connections on %s\n", CONFIG_EXAMPLES_USTREAM_ADDR); + acceptsd = accept(listensd, (struct sockaddr*)&myaddr, &addrlen); + if (acceptsd < 0) + { + printf("server: accept failure: %d\n", errno); + goto errout_with_listensd; + } + + printf("server: Connection accepted -- receiving\n"); + + /* Receive canned message */ + + totalbytesread = 0; + while (totalbytesread < SENDSIZE) + { + printf("server: Reading...\n"); + nbytesread = recv(acceptsd, &buffer[totalbytesread], 2*SENDSIZE - totalbytesread, 0); + if (nbytesread < 0) + { + printf("server: recv failed: %d\n", errno); + goto errout_with_acceptsd; + } + else if (nbytesread == 0) + { + printf("server: The client broke the connection\n"); + goto errout_with_acceptsd; + } + + totalbytesread += nbytesread; + printf("server: Received %d of %d bytes\n", totalbytesread, SENDSIZE); + } + + /* Verify the message */ + + if (totalbytesread != SENDSIZE) + { + printf("server: Received %d / Expected %d bytes\n", totalbytesread, SENDSIZE); + goto errout_with_acceptsd; + } + + ch = 0x20; + for (i = 0; i < SENDSIZE; i++ ) + { + if (buffer[i] != ch) + { + printf("server: Byte %d is %02x / Expected %02x\n", i, buffer[i], ch); + goto errout_with_acceptsd; + } + + if (++ch > 0x7e) + { + ch = 0x20; + } + } + + /* Then send the same data back to the client */ + + printf("server: Sending %d bytes\n", totalbytesread); + nbytessent = send(acceptsd, buffer, totalbytesread, 0); + if (nbytessent <= 0) + { + printf("server: send failed: %d\n", errno); + goto errout_with_acceptsd; + } + + printf("server: Sent %d bytes\n", nbytessent); + + /* If this platform only does abortive disconnects, then wait a bit to get the + * client side a change to receive the data. + */ + + printf("server: Wait before closing\n"); + sleep(60); + + close(listensd); + close(acceptsd); + free(buffer); + return 0; + +errout_with_acceptsd: + close(acceptsd); + +errout_with_listensd: + close(listensd); + +errout_with_buffer: + free(buffer); + return 1; +}