2011-03-19 22:04:13 +01:00
|
|
|
/****************************************************************************
|
2021-06-15 09:09:58 +02:00
|
|
|
* apps/netutils/netlib/netlib_server.c
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
2021-06-11 06:22:42 +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-19 22:04:13 +01:00
|
|
|
*
|
2021-06-11 06:22:42 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
2021-06-11 06:22:42 +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-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2012-09-17 20:18:44 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2012-09-17 20:18:44 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/netlib.h"
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-07-03 00:52:02 +02:00
|
|
|
* Name: netlib_server
|
2011-03-19 22:04:13 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Implement basic server logic
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* portno The port to listen on (in network byte order)
|
|
|
|
* handler The entrypoint of the task to spawn when a new connection is
|
|
|
|
* accepted.
|
|
|
|
* stacksize The stack size needed by the spawned task
|
|
|
|
*
|
|
|
|
* Return:
|
|
|
|
* Does not return unless an error occurs.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-11 12:06:42 +02:00
|
|
|
void netlib_server(uint16_t portno,
|
|
|
|
pthread_startroutine_t handler, int stacksize)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
struct sockaddr_in myaddr;
|
2014-07-11 20:30:23 +02:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
2011-03-19 22:04:13 +01:00
|
|
|
struct linger ling;
|
|
|
|
#endif
|
|
|
|
pthread_t child;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
socklen_t addrlen;
|
|
|
|
int listensd;
|
|
|
|
int acceptsd;
|
2012-09-26 00:47:11 +02:00
|
|
|
int ret;
|
2011-03-19 22:04:13 +01:00
|
|
|
|
|
|
|
/* Create a new TCP socket to use to listen for connections */
|
|
|
|
|
2014-07-03 00:52:02 +02:00
|
|
|
listensd = netlib_listenon(portno);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (listensd < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-17 20:18:44 +02:00
|
|
|
/* Begin serving connections */
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-06-11 12:06:42 +02:00
|
|
|
for (; ; )
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2012-09-26 00:47:11 +02:00
|
|
|
/* Accept the next connectin */
|
2014-04-14 00:24:28 +02:00
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
2021-06-11 12:06:42 +02:00
|
|
|
acceptsd = accept(listensd, (struct sockaddr *)&myaddr, &addrlen);
|
2011-03-19 22:04:13 +01:00
|
|
|
if (acceptsd < 0)
|
|
|
|
{
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: accept failure: %d\n", errno);
|
2012-09-26 00:47:11 +02:00
|
|
|
break;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
2012-09-17 20:18:44 +02:00
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
ninfo("Connection accepted -- spawning sd=%d\n", acceptsd);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2012-09-26 00:47:11 +02:00
|
|
|
/* Configure to "linger" until all data is sent when the socket is
|
|
|
|
* closed.
|
|
|
|
*/
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2014-07-11 20:30:23 +02:00
|
|
|
#ifdef CONFIG_NET_SOLINGER
|
2011-03-19 22:04:13 +01:00
|
|
|
ling.l_onoff = 1;
|
|
|
|
ling.l_linger = 30; /* timeout is seconds */
|
2012-09-26 00:47:11 +02:00
|
|
|
|
2021-06-11 12:06:42 +02:00
|
|
|
ret = setsockopt(acceptsd, SOL_SOCKET,
|
|
|
|
SO_LINGER, &ling, sizeof(struct linger));
|
2012-09-26 00:47:11 +02:00
|
|
|
if (ret < 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
|
|
|
close(acceptsd);
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: setsockopt SO_LINGER failure: %d\n", errno);
|
2012-09-17 20:18:44 +02:00
|
|
|
break;
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Create a thread to handle the connection. The socket descriptor is
|
|
|
|
* provided in as the single argument to the new thread.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_setstacksize(&attr, stacksize);
|
2011-03-19 22:04:13 +01:00
|
|
|
|
2021-06-11 12:06:42 +02:00
|
|
|
ret = pthread_create(&child, &attr,
|
|
|
|
handler, (pthread_addr_t)((uintptr_t)acceptsd));
|
2012-09-26 00:47:11 +02:00
|
|
|
if (ret != 0)
|
2011-03-19 22:04:13 +01:00
|
|
|
{
|
2012-09-26 00:47:11 +02:00
|
|
|
/* Close the connection */
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
close(acceptsd);
|
2016-06-15 01:53:47 +02:00
|
|
|
nerr("ERROR: pthread_create failed\n");
|
2012-09-26 00:47:11 +02:00
|
|
|
|
|
|
|
if (ret == EAGAIN)
|
|
|
|
{
|
|
|
|
/* Lacked resources to create a new thread. This is a temporary
|
|
|
|
* condition, so we close this peer, but keep serving for
|
|
|
|
* other connections.
|
|
|
|
*/
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Something is very wrong... Break out and stop serving */
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't care when/how the child thread exits so detach from it now
|
|
|
|
* in order to avoid memory leaks.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
pthread_detach(child);
|
2011-03-19 22:04:13 +01:00
|
|
|
}
|
|
|
|
|
2012-09-26 00:47:11 +02:00
|
|
|
/* Close the listerner socket */
|
|
|
|
|
2011-03-19 22:04:13 +01:00
|
|
|
close(listensd);
|
|
|
|
}
|