2011-03-20 19:18:19 +01:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/poll/net_reader.c
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2011-03-20 19:18:19 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/socket.h>
|
2015-02-15 22:18:35 +01:00
|
|
|
#include <sys/time.h>
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/netlib.h"
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
#include "poll_internal.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-09-03 00:49:17 +02:00
|
|
|
* Pre-processor Definitions
|
2011-03-20 19:18:19 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define IOBUFFER_SIZE 80
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_configure
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void net_configure(void)
|
|
|
|
{
|
|
|
|
struct in_addr addr;
|
2012-10-04 19:36:07 +02:00
|
|
|
#if defined(CONFIG_EXAMPLES_POLL_NOMAC)
|
2011-03-20 19:18:19 +01:00
|
|
|
uint8_t mac[IFHWADDRLEN];
|
|
|
|
#endif
|
|
|
|
|
2015-09-03 00:49:17 +02:00
|
|
|
/* Configure the network */
|
2011-03-20 19:18:19 +01:00
|
|
|
/* Many embedded network interfaces must have a software assigned MAC */
|
|
|
|
|
2012-10-04 19:36:07 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_POLL_NOMAC
|
2011-03-20 19:18:19 +01:00
|
|
|
mac[0] = 0x00;
|
|
|
|
mac[1] = 0xe0;
|
2012-07-20 00:32:19 +02:00
|
|
|
mac[2] = 0xde;
|
|
|
|
mac[3] = 0xad;
|
|
|
|
mac[4] = 0xbe;
|
|
|
|
mac[5] = 0xef;
|
2014-07-03 00:52:02 +02:00
|
|
|
netlib_setmacaddr("eth0", mac);
|
2011-03-20 19:18:19 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set up our host address */
|
|
|
|
|
2012-10-04 19:36:07 +02:00
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_POLL_IPADDR);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4addr("eth0", &addr);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* Set up the default router address */
|
|
|
|
|
2012-10-04 19:36:07 +02:00
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_POLL_DRIPADDR);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_dripv4addr("eth0", &addr);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* Setup the subnet mask */
|
|
|
|
|
2012-10-04 19:36:07 +02:00
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLES_POLL_NETMASK);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4netmask("eth0", &addr);
|
2017-05-19 17:30:26 +02:00
|
|
|
|
|
|
|
/* New versions of netlib_set_ipvXaddr will not bring the network up,
|
2017-05-19 17:50:26 +02:00
|
|
|
* So ensure the network is really up at this point.
|
|
|
|
*/
|
2017-05-19 17:30:26 +02:00
|
|
|
|
|
|
|
netlib_ifup("eth0");
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_receive
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void net_receive(int sd)
|
|
|
|
{
|
|
|
|
struct timeval timeout;
|
|
|
|
char buffer[IOBUFFER_SIZE];
|
|
|
|
char *ptr;
|
|
|
|
fd_set readset;
|
|
|
|
int nbytes;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Set up the timeout */
|
|
|
|
|
|
|
|
timeout.tv_sec = NET_LISTENER_DELAY;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
/* Loop while we have the connection */
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* Wait for incoming message */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
FD_ZERO(&readset);
|
|
|
|
FD_SET(sd, &readset);
|
|
|
|
ret = select(sd + 1, (FAR fd_set*)&readset, (FAR fd_set*)NULL, (FAR fd_set*)NULL, &timeout);
|
|
|
|
}
|
|
|
|
while (ret < 0 && errno == EINTR);
|
|
|
|
|
|
|
|
/* Something has happened */
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("net_reader: select failed: %d\n", errno);
|
2011-03-20 19:18:19 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (ret == 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("net_reader: Timeout\n");
|
2011-03-20 19:18:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("net_reader: Read data from sd=%d\n", sd);
|
2011-03-20 19:18:19 +01:00
|
|
|
memset(buffer, '?', IOBUFFER_SIZE); /* Just to make sure we really receive something */
|
|
|
|
ret = recv(sd, buffer, IOBUFFER_SIZE, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
if (errno != EINTR)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("net_reader: recv failed sd=%d: %d\n", sd, errno);
|
2011-03-20 19:18:19 +01:00
|
|
|
if (errno != EAGAIN)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ret == 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("net_reader: Client connection lost sd=%d\n", sd);
|
2011-03-20 19:18:19 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer[ret]='\0';
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("net_reader: Read '%s' (%d bytes)\n", buffer, ret);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* Echo the data back to the client */
|
|
|
|
|
|
|
|
for (nbytes = ret, ptr = buffer; nbytes > 0; )
|
|
|
|
{
|
|
|
|
ret = send(sd, ptr, nbytes, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
if (errno != EINTR)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("net_reader: Send failed sd=%d: %d\n", sd, errno);
|
2011-03-20 19:18:19 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nbytes -= ret;
|
|
|
|
ptr += ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_reader
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void *net_reader(pthread_addr_t pvarg)
|
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
#ifdef POLL_HAVE_SOLINGER
|
|
|
|
struct linger ling;
|
|
|
|
#endif
|
|
|
|
int listensd;
|
|
|
|
int acceptsd;
|
|
|
|
socklen_t addrlen;
|
|
|
|
int optval;
|
|
|
|
|
2015-09-03 00:49:17 +02:00
|
|
|
/* Configure the network */
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
net_configure();
|
|
|
|
|
|
|
|
/* Create a new TCP socket */
|
|
|
|
|
|
|
|
listensd = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (listensd < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("net_reader: socket failure: %d\n", errno);
|
2011-03-20 19:18:19 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set socket to reuse address */
|
|
|
|
|
|
|
|
optval = 1;
|
|
|
|
if (setsockopt(listensd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("net_reader: setsockopt SO_REUSEADDR failure: %d\n", errno);
|
2011-03-20 19:18:19 +01:00
|
|
|
goto errout_with_listensd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bind the socket to a local address */
|
|
|
|
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = HTONS(LISTENER_PORT);
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
|
|
if (bind(listensd, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("net_reader: bind failure: %d\n", errno);
|
2011-03-20 19:18:19 +01:00
|
|
|
goto errout_with_listensd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Listen for connections on the bound TCP socket */
|
|
|
|
|
|
|
|
if (listen(listensd, 5) < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("net_reader: listen failure %d\n", errno);
|
2011-03-20 19:18:19 +01:00
|
|
|
goto errout_with_listensd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Connection loop */
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* Accept only one connection */
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("net_reader: Accepting new connections on port %d\n", LISTENER_PORT);
|
2011-03-20 19:18:19 +01:00
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
|
|
|
acceptsd = accept(listensd, (struct sockaddr*)&addr, &addrlen);
|
|
|
|
if (acceptsd < 0)
|
|
|
|
{
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("net_reader: accept failure: %d\n", errno);
|
2011-03-20 19:18:19 +01:00
|
|
|
continue;
|
|
|
|
}
|
2015-09-03 00:49:17 +02:00
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("net_reader: Connection accepted on sd=%d\n", acceptsd);
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
/* Configure to "linger" until all data is sent when the socket is closed */
|
|
|
|
|
|
|
|
#ifdef POLL_HAVE_SOLINGER
|
|
|
|
ling.l_onoff = 1;
|
|
|
|
ling.l_linger = 30; /* timeout is seconds */
|
2015-09-03 00:49:17 +02:00
|
|
|
|
2011-03-20 19:18:19 +01:00
|
|
|
if (setsockopt(acceptsd, SOL_SOCKET, SO_LINGER, &ling, sizeof(struct linger)) < 0)
|
|
|
|
{
|
2015-09-03 00:49:17 +02:00
|
|
|
printf("net_reader: setsockopt SO_LINGER failure: %d\n", errno);
|
|
|
|
goto errout_with_acceptsd;
|
|
|
|
}
|
2011-03-20 19:18:19 +01:00
|
|
|
#endif
|
|
|
|
|
2020-02-23 05:51:44 +01:00
|
|
|
/* Handle incoming messages on the connection. */
|
2011-03-20 19:18:19 +01:00
|
|
|
|
|
|
|
net_receive(acceptsd);
|
|
|
|
|
2014-10-08 16:33:00 +02:00
|
|
|
printf("net_reader: Closing sd=%d\n", acceptsd);
|
2011-03-20 19:18:19 +01:00
|
|
|
close(acceptsd);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef POLL_HAVE_SOLINGER
|
|
|
|
errout_with_acceptsd:
|
|
|
|
close(acceptsd);
|
|
|
|
#endif
|
|
|
|
errout_with_listensd:
|
|
|
|
close(listensd);
|
|
|
|
errout:
|
|
|
|
return NULL;
|
|
|
|
}
|