2015-05-05 08:27:33 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
simple socket handling class for systems with BSD socket API
|
|
|
|
*/
|
|
|
|
|
2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2023-11-12 20:38:58 -04:00
|
|
|
#include <AP_Networking/AP_Networking_Config.h>
|
|
|
|
#if AP_NETWORKING_SOCKETS_ENABLED
|
2015-05-05 08:27:33 -03:00
|
|
|
|
|
|
|
#include "Socket.h"
|
2023-10-19 19:56:31 -03:00
|
|
|
#include <errno.h>
|
2015-05-05 08:27:33 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
constructor
|
|
|
|
*/
|
|
|
|
SocketAPM::SocketAPM(bool _datagram) :
|
2015-07-28 20:07:41 -03:00
|
|
|
SocketAPM(_datagram,
|
|
|
|
socket(AF_INET, _datagram?SOCK_DGRAM:SOCK_STREAM, 0))
|
|
|
|
{}
|
|
|
|
|
|
|
|
SocketAPM::SocketAPM(bool _datagram, int _fd) :
|
|
|
|
datagram(_datagram),
|
|
|
|
fd(_fd)
|
2015-05-05 08:27:33 -03:00
|
|
|
{
|
2023-11-12 20:38:58 -04:00
|
|
|
#ifdef FD_CLOEXEC
|
2015-05-10 19:05:51 -03:00
|
|
|
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
2023-11-12 20:38:58 -04:00
|
|
|
#endif
|
2015-07-28 20:07:41 -03:00
|
|
|
if (!datagram) {
|
|
|
|
int one = 1;
|
|
|
|
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
|
|
|
|
}
|
2015-05-05 08:27:33 -03:00
|
|
|
}
|
|
|
|
|
2015-06-11 18:48:24 -03:00
|
|
|
SocketAPM::~SocketAPM()
|
|
|
|
{
|
2015-07-28 19:39:16 -03:00
|
|
|
if (fd != -1) {
|
2023-11-12 20:38:58 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_CHIBIOS
|
|
|
|
::lwip_close(fd);
|
|
|
|
#else
|
2015-07-28 19:39:16 -03:00
|
|
|
::close(fd);
|
2023-11-12 20:38:58 -04:00
|
|
|
#endif
|
2015-07-28 19:39:16 -03:00
|
|
|
fd = -1;
|
2015-06-11 18:48:24 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 02:53:33 -03:00
|
|
|
void SocketAPM::make_sockaddr(const char *address, uint16_t port, struct sockaddr_in &sockaddr)
|
2015-05-05 08:27:33 -03:00
|
|
|
{
|
|
|
|
memset(&sockaddr, 0, sizeof(sockaddr));
|
|
|
|
|
|
|
|
#ifdef HAVE_SOCK_SIN_LEN
|
|
|
|
sockaddr.sin_len = sizeof(sockaddr);
|
|
|
|
#endif
|
|
|
|
sockaddr.sin_port = htons(port);
|
|
|
|
sockaddr.sin_family = AF_INET;
|
|
|
|
sockaddr.sin_addr.s_addr = inet_addr(address);
|
2015-05-22 02:53:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
connect the socket
|
|
|
|
*/
|
|
|
|
bool SocketAPM::connect(const char *address, uint16_t port)
|
|
|
|
{
|
|
|
|
struct sockaddr_in sockaddr;
|
|
|
|
make_sockaddr(address, port, sockaddr);
|
2015-05-05 08:27:33 -03:00
|
|
|
|
2023-11-12 20:38:58 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_CHIBIOS
|
|
|
|
if (::lwip_connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0) {
|
|
|
|
#else
|
2015-05-05 08:27:33 -03:00
|
|
|
if (::connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0) {
|
2023-11-12 20:38:58 -04:00
|
|
|
#endif
|
2015-05-05 08:27:33 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-19 19:56:31 -03:00
|
|
|
/*
|
|
|
|
connect the socket with a timeout
|
|
|
|
*/
|
|
|
|
bool SocketAPM::connect_timeout(const char *address, uint16_t port, uint32_t timeout_ms)
|
|
|
|
{
|
|
|
|
struct sockaddr_in sockaddr;
|
|
|
|
make_sockaddr(address, port, sockaddr);
|
|
|
|
|
|
|
|
set_blocking(false);
|
|
|
|
|
2023-11-12 20:38:58 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_CHIBIOS
|
|
|
|
int ret = ::lwip_connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
|
|
|
|
#else
|
2023-10-19 19:56:31 -03:00
|
|
|
int ret = ::connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
|
2023-11-12 20:38:58 -04:00
|
|
|
#endif
|
2023-10-19 19:56:31 -03:00
|
|
|
if (ret == 0) {
|
|
|
|
// instant connect?
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (errno != EINPROGRESS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool pollret = pollout(timeout_ms);
|
|
|
|
if (!pollret) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int sock_error = 0;
|
|
|
|
socklen_t len = sizeof(sock_error);
|
|
|
|
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&sock_error, &len) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return sock_error == 0;
|
|
|
|
}
|
|
|
|
|
2015-05-05 08:27:33 -03:00
|
|
|
/*
|
|
|
|
bind the socket
|
|
|
|
*/
|
|
|
|
bool SocketAPM::bind(const char *address, uint16_t port)
|
|
|
|
{
|
|
|
|
struct sockaddr_in sockaddr;
|
2015-05-22 02:53:33 -03:00
|
|
|
make_sockaddr(address, port, sockaddr);
|
2015-05-05 08:27:33 -03:00
|
|
|
|
2023-11-12 20:38:58 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_CHIBIOS
|
|
|
|
if (::lwip_bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0) {
|
|
|
|
#else
|
2015-05-05 08:27:33 -03:00
|
|
|
if (::bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0) {
|
2023-11-12 20:38:58 -04:00
|
|
|
#endif
|
2015-05-05 08:27:33 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
set SO_REUSEADDR
|
|
|
|
*/
|
2021-02-01 12:26:28 -04:00
|
|
|
bool SocketAPM::reuseaddress(void) const
|
2015-05-05 08:27:33 -03:00
|
|
|
{
|
|
|
|
int one = 1;
|
2018-12-13 22:36:50 -04:00
|
|
|
return (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) != -1);
|
2015-05-05 08:27:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
set blocking state
|
|
|
|
*/
|
2021-02-01 12:26:28 -04:00
|
|
|
bool SocketAPM::set_blocking(bool blocking) const
|
2015-05-05 08:27:33 -03:00
|
|
|
{
|
2018-12-13 22:36:50 -04:00
|
|
|
int fcntl_ret;
|
2015-05-05 08:27:33 -03:00
|
|
|
if (blocking) {
|
2018-12-13 22:36:50 -04:00
|
|
|
fcntl_ret = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK);
|
2015-05-05 08:27:33 -03:00
|
|
|
} else {
|
2018-12-13 22:36:50 -04:00
|
|
|
fcntl_ret = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
|
2015-05-05 08:27:33 -03:00
|
|
|
}
|
2018-12-13 22:36:50 -04:00
|
|
|
return fcntl_ret != -1;
|
2015-05-05 08:27:33 -03:00
|
|
|
}
|
|
|
|
|
2018-06-08 01:12:35 -03:00
|
|
|
/*
|
|
|
|
set cloexec state
|
|
|
|
*/
|
2021-02-01 12:26:28 -04:00
|
|
|
bool SocketAPM::set_cloexec() const
|
2018-06-08 01:12:35 -03:00
|
|
|
{
|
2023-11-12 20:38:58 -04:00
|
|
|
#ifdef FD_CLOEXEC
|
2018-12-13 22:36:50 -04:00
|
|
|
return (fcntl(fd, F_SETFD, FD_CLOEXEC) != -1);
|
2023-11-12 20:38:58 -04:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2018-06-08 01:12:35 -03:00
|
|
|
}
|
|
|
|
|
2015-05-05 08:27:33 -03:00
|
|
|
/*
|
|
|
|
send some data
|
|
|
|
*/
|
2021-02-01 12:26:28 -04:00
|
|
|
ssize_t SocketAPM::send(const void *buf, size_t size) const
|
2015-05-05 08:27:33 -03:00
|
|
|
{
|
2023-11-12 20:38:58 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_CHIBIOS
|
|
|
|
return ::lwip_send(fd, buf, size, 0);
|
|
|
|
#else
|
2015-05-05 08:27:33 -03:00
|
|
|
return ::send(fd, buf, size, 0);
|
2023-11-12 20:38:58 -04:00
|
|
|
#endif
|
2015-05-05 08:27:33 -03:00
|
|
|
}
|
|
|
|
|
2015-05-22 02:53:33 -03:00
|
|
|
/*
|
|
|
|
send some data
|
|
|
|
*/
|
2015-06-15 17:54:08 -03:00
|
|
|
ssize_t SocketAPM::sendto(const void *buf, size_t size, const char *address, uint16_t port)
|
2015-05-22 02:53:33 -03:00
|
|
|
{
|
|
|
|
struct sockaddr_in sockaddr;
|
|
|
|
make_sockaddr(address, port, sockaddr);
|
2023-11-12 20:38:58 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_CHIBIOS
|
|
|
|
return ::lwip_sendto(fd, buf, size, 0, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
|
|
|
|
#else
|
2015-05-22 02:53:33 -03:00
|
|
|
return ::sendto(fd, buf, size, 0, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
|
2023-11-12 20:38:58 -04:00
|
|
|
#endif
|
2015-05-22 02:53:33 -03:00
|
|
|
}
|
|
|
|
|
2015-05-05 08:27:33 -03:00
|
|
|
/*
|
|
|
|
receive some data
|
|
|
|
*/
|
|
|
|
ssize_t SocketAPM::recv(void *buf, size_t size, uint32_t timeout_ms)
|
2015-07-28 19:58:02 -03:00
|
|
|
{
|
|
|
|
if (!pollin(timeout_ms)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-07-29 03:46:33 -03:00
|
|
|
socklen_t len = sizeof(in_addr);
|
2023-11-12 20:38:58 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_CHIBIOS
|
|
|
|
return ::lwip_recvfrom(fd, buf, size, MSG_DONTWAIT, (sockaddr *)&in_addr, &len);
|
|
|
|
#else
|
2015-07-29 03:46:33 -03:00
|
|
|
return ::recvfrom(fd, buf, size, MSG_DONTWAIT, (sockaddr *)&in_addr, &len);
|
2023-11-12 20:38:58 -04:00
|
|
|
#endif
|
2015-07-28 19:58:02 -03:00
|
|
|
}
|
|
|
|
|
2015-07-29 03:46:33 -03:00
|
|
|
/*
|
|
|
|
return the IP address and port of the last received packet
|
|
|
|
*/
|
2021-02-01 12:26:28 -04:00
|
|
|
void SocketAPM::last_recv_address(const char *&ip_addr, uint16_t &port) const
|
2015-07-29 03:46:33 -03:00
|
|
|
{
|
|
|
|
ip_addr = inet_ntoa(in_addr.sin_addr);
|
|
|
|
port = ntohs(in_addr.sin_port);
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:26:28 -04:00
|
|
|
void SocketAPM::set_broadcast(void) const
|
2015-07-29 03:46:33 -03:00
|
|
|
{
|
|
|
|
int one = 1;
|
|
|
|
setsockopt(fd,SOL_SOCKET,SO_BROADCAST,(char *)&one,sizeof(one));
|
|
|
|
}
|
2015-07-28 19:58:02 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
return true if there is pending data for input
|
|
|
|
*/
|
|
|
|
bool SocketAPM::pollin(uint32_t timeout_ms)
|
2015-05-05 08:27:33 -03:00
|
|
|
{
|
|
|
|
fd_set fds;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(fd, &fds);
|
|
|
|
|
|
|
|
tv.tv_sec = timeout_ms / 1000;
|
|
|
|
tv.tv_usec = (timeout_ms % 1000) * 1000UL;
|
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
if (select(fd+1, &fds, nullptr, nullptr, &tv) != 1) {
|
2015-07-28 19:58:02 -03:00
|
|
|
return false;
|
2015-05-05 08:27:33 -03:00
|
|
|
}
|
2015-07-28 19:58:02 -03:00
|
|
|
return true;
|
2015-05-05 08:27:33 -03:00
|
|
|
}
|
|
|
|
|
2015-07-29 01:04:25 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
return true if there is room for output data
|
|
|
|
*/
|
|
|
|
bool SocketAPM::pollout(uint32_t timeout_ms)
|
|
|
|
{
|
|
|
|
fd_set fds;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(fd, &fds);
|
|
|
|
|
|
|
|
tv.tv_sec = timeout_ms / 1000;
|
|
|
|
tv.tv_usec = (timeout_ms % 1000) * 1000UL;
|
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
if (select(fd+1, nullptr, &fds, nullptr, &tv) != 1) {
|
2015-07-29 01:04:25 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-28 20:07:41 -03:00
|
|
|
/*
|
|
|
|
start listening for new tcp connections
|
|
|
|
*/
|
2021-02-01 12:26:28 -04:00
|
|
|
bool SocketAPM::listen(uint16_t backlog) const
|
2015-07-28 20:07:41 -03:00
|
|
|
{
|
2023-11-12 20:38:58 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_CHIBIOS
|
|
|
|
return ::lwip_listen(fd, (int)backlog) == 0;
|
|
|
|
#else
|
2015-07-28 20:07:41 -03:00
|
|
|
return ::listen(fd, (int)backlog) == 0;
|
2023-11-12 20:38:58 -04:00
|
|
|
#endif
|
2015-07-28 20:07:41 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
accept a new connection. Only valid for TCP connections after
|
|
|
|
listen has been used. A new socket is returned
|
|
|
|
*/
|
|
|
|
SocketAPM *SocketAPM::accept(uint32_t timeout_ms)
|
|
|
|
{
|
|
|
|
if (!pollin(timeout_ms)) {
|
2016-10-30 02:24:21 -03:00
|
|
|
return nullptr;
|
2015-07-28 20:07:41 -03:00
|
|
|
}
|
|
|
|
|
2023-11-12 20:38:58 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_CHIBIOS
|
|
|
|
int newfd = ::lwip_accept(fd, nullptr, nullptr);
|
|
|
|
#else
|
2016-10-30 02:24:21 -03:00
|
|
|
int newfd = ::accept(fd, nullptr, nullptr);
|
2023-11-12 20:38:58 -04:00
|
|
|
#endif
|
2015-07-28 20:07:41 -03:00
|
|
|
if (newfd == -1) {
|
2016-10-30 02:24:21 -03:00
|
|
|
return nullptr;
|
2015-07-28 20:07:41 -03:00
|
|
|
}
|
2015-07-29 01:04:25 -03:00
|
|
|
// turn off nagle for lower latency
|
|
|
|
int one = 1;
|
|
|
|
setsockopt(newfd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
|
2015-07-28 20:07:41 -03:00
|
|
|
return new SocketAPM(false, newfd);
|
|
|
|
}
|
|
|
|
|
2023-11-12 20:38:58 -04:00
|
|
|
#endif // AP_NETWORKING_BACKEND_ANY
|