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
|
|
|
|
2023-11-25 15:05:47 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_CHIBIOS
|
|
|
|
#define CALL_PREFIX(x) ::lwip_##x
|
|
|
|
#else
|
|
|
|
#define CALL_PREFIX(x) ::x
|
|
|
|
#endif
|
|
|
|
|
2015-05-05 08:27:33 -03:00
|
|
|
/*
|
|
|
|
constructor
|
|
|
|
*/
|
|
|
|
SocketAPM::SocketAPM(bool _datagram) :
|
2015-07-28 20:07:41 -03:00
|
|
|
SocketAPM(_datagram,
|
2023-11-25 15:05:47 -04:00
|
|
|
CALL_PREFIX(socket)(AF_INET, _datagram?SOCK_DGRAM:SOCK_STREAM, 0))
|
2015-07-28 20:07:41 -03:00
|
|
|
{}
|
|
|
|
|
|
|
|
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
|
2023-11-25 15:05:47 -04:00
|
|
|
CALL_PREFIX(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;
|
2023-11-25 15:05:47 -04:00
|
|
|
CALL_PREFIX(setsockopt)(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
|
2015-07-28 20:07:41 -03:00
|
|
|
}
|
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-25 15:05:47 -04:00
|
|
|
CALL_PREFIX(close)(fd);
|
2015-07-28 19:39:16 -03:00
|
|
|
fd = -1;
|
2015-06-11 18:48:24 -03:00
|
|
|
}
|
2023-11-25 15:05:47 -04:00
|
|
|
if (fd_in != -1) {
|
|
|
|
CALL_PREFIX(close)(fd_in);
|
|
|
|
fd_in = -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;
|
2023-11-25 15:05:47 -04:00
|
|
|
int ret;
|
2015-05-22 02:53:33 -03:00
|
|
|
make_sockaddr(address, port, sockaddr);
|
2015-05-05 08:27:33 -03:00
|
|
|
|
2023-11-25 15:05:47 -04:00
|
|
|
if (datagram && is_multicast_address(sockaddr)) {
|
|
|
|
/*
|
|
|
|
connect fd_in as a multicast UDP socket
|
|
|
|
*/
|
|
|
|
fd_in = CALL_PREFIX(socket)(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (fd_in == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
struct sockaddr_in sockaddr_mc = sockaddr;
|
|
|
|
int one = 1;
|
|
|
|
struct ip_mreq mreq {};
|
|
|
|
#ifdef FD_CLOEXEC
|
|
|
|
CALL_PREFIX(fcntl)(fd_in, F_SETFD, FD_CLOEXEC);
|
2023-11-12 20:38:58 -04:00
|
|
|
#endif
|
2023-11-25 15:05:47 -04:00
|
|
|
IGNORE_RETURN(CALL_PREFIX(setsockopt)(fd_in, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)));
|
|
|
|
|
|
|
|
#if defined(__CYGWIN__) || defined(__CYGWIN64__) || defined(CYGWIN_BUILD)
|
|
|
|
/*
|
|
|
|
on cygwin you need to bind to INADDR_ANY then use the multicast
|
|
|
|
IP_ADD_MEMBERSHIP to get on the right address
|
|
|
|
*/
|
|
|
|
sockaddr_mc.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ret = CALL_PREFIX(bind)(fd_in, (struct sockaddr *)&sockaddr_mc, sizeof(sockaddr));
|
|
|
|
if (ret == -1) {
|
|
|
|
goto fail_mc;
|
|
|
|
}
|
|
|
|
|
|
|
|
mreq.imr_multiaddr.s_addr = sockaddr.sin_addr.s_addr;
|
|
|
|
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
ret = CALL_PREFIX(setsockopt)(fd_in, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
|
|
|
|
if (ret == -1) {
|
|
|
|
goto fail_mc;
|
|
|
|
}
|
2023-11-25 17:41:26 -04:00
|
|
|
} else if (datagram && sockaddr.sin_addr.s_addr == INADDR_BROADCAST) {
|
|
|
|
set_broadcast();
|
2023-11-25 15:05:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = CALL_PREFIX(connect)(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
|
|
|
|
if (ret != 0) {
|
2015-05-05 08:27:33 -03:00
|
|
|
return false;
|
|
|
|
}
|
2023-11-25 15:05:47 -04:00
|
|
|
connected = true;
|
2015-05-05 08:27:33 -03:00
|
|
|
return true;
|
2023-11-25 15:05:47 -04:00
|
|
|
|
|
|
|
fail_mc:
|
|
|
|
CALL_PREFIX(close)(fd_in);
|
|
|
|
fd_in = -1;
|
|
|
|
return false;
|
2015-05-05 08:27:33 -03:00
|
|
|
}
|
|
|
|
|
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-25 15:05:47 -04:00
|
|
|
int ret = CALL_PREFIX(connect)(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
|
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;
|
|
|
|
}
|
2023-11-25 15:05:47 -04:00
|
|
|
connected = sock_error == 0;
|
|
|
|
return connected;
|
2023-10-19 19:56:31 -03:00
|
|
|
}
|
|
|
|
|
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-25 15:05:47 -04:00
|
|
|
if (CALL_PREFIX(bind)(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0) {
|
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;
|
2023-11-25 15:05:47 -04:00
|
|
|
return (CALL_PREFIX(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) {
|
2023-11-25 15:05:47 -04:00
|
|
|
fcntl_ret = CALL_PREFIX(fcntl)(fd, F_SETFL, CALL_PREFIX(fcntl)(fd, F_GETFL, 0) & ~O_NONBLOCK);
|
|
|
|
if (fd_in != -1) {
|
|
|
|
fcntl_ret |= CALL_PREFIX(fcntl)(fd_in, F_SETFL, CALL_PREFIX(fcntl)(fd_in, F_GETFL, 0) & ~O_NONBLOCK);
|
|
|
|
}
|
2015-05-05 08:27:33 -03:00
|
|
|
} else {
|
2023-11-25 15:05:47 -04:00
|
|
|
fcntl_ret = CALL_PREFIX(fcntl)(fd, F_SETFL, CALL_PREFIX(fcntl)(fd, F_GETFL, 0) | O_NONBLOCK);
|
|
|
|
if (fd_in != -1) {
|
|
|
|
fcntl_ret |= CALL_PREFIX(fcntl)(fd_in, F_SETFL, CALL_PREFIX(fcntl)(fd_in, 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
|
2023-11-25 15:05:47 -04:00
|
|
|
return (CALL_PREFIX(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-25 15:05:47 -04:00
|
|
|
return CALL_PREFIX(send)(fd, buf, size, 0);
|
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-25 15:05:47 -04:00
|
|
|
return CALL_PREFIX(sendto)(fd, buf, size, 0, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
|
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)) {
|
2023-11-25 20:44:52 -04:00
|
|
|
errno = EWOULDBLOCK;
|
2015-07-28 19:58:02 -03:00
|
|
|
return -1;
|
|
|
|
}
|
2015-07-29 03:46:33 -03:00
|
|
|
socklen_t len = sizeof(in_addr);
|
2023-11-25 15:05:47 -04:00
|
|
|
int fin = get_read_fd();
|
|
|
|
ssize_t ret;
|
|
|
|
ret = CALL_PREFIX(recvfrom)(fin, buf, size, MSG_DONTWAIT, (sockaddr *)&in_addr, &len);
|
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (fd_in != -1) {
|
|
|
|
/*
|
|
|
|
for multicast check we are not receiving from ourselves
|
|
|
|
*/
|
|
|
|
struct sockaddr_in send_addr;
|
|
|
|
socklen_t send_len = sizeof(send_addr);
|
|
|
|
if (CALL_PREFIX(getsockname)(fd, (struct sockaddr *)&send_addr, &send_len) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (in_addr.sin_port == send_addr.sin_port &&
|
|
|
|
in_addr.sin_family == send_addr.sin_family &&
|
|
|
|
in_addr.sin_addr.s_addr == send_addr.sin_addr.s_addr) {
|
|
|
|
// discard packets from ourselves
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
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);
|
|
|
|
}
|
|
|
|
|
2023-11-25 20:44:52 -04:00
|
|
|
/*
|
|
|
|
return the IP address and port of the last received packet, using caller supplied buffer
|
|
|
|
*/
|
|
|
|
const char *SocketAPM::last_recv_address(char *ip_addr_buf, uint8_t buflen, uint16_t &port) const
|
|
|
|
{
|
|
|
|
const char *ret = inet_ntop(AF_INET, (void*)&in_addr.sin_addr, ip_addr_buf, buflen);
|
|
|
|
if (ret == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
port = ntohs(in_addr.sin_port);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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;
|
2023-11-25 15:05:47 -04:00
|
|
|
CALL_PREFIX(setsockopt)(fd,SOL_SOCKET,SO_BROADCAST,(char *)&one,sizeof(one));
|
2015-07-29 03:46:33 -03:00
|
|
|
}
|
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);
|
2023-11-25 15:05:47 -04:00
|
|
|
int fin = get_read_fd();
|
|
|
|
FD_SET(fin, &fds);
|
2015-05-05 08:27:33 -03:00
|
|
|
|
|
|
|
tv.tv_sec = timeout_ms / 1000;
|
|
|
|
tv.tv_usec = (timeout_ms % 1000) * 1000UL;
|
|
|
|
|
2023-11-25 15:05:47 -04:00
|
|
|
if (CALL_PREFIX(select)(fin+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;
|
|
|
|
|
2023-11-25 15:05:47 -04:00
|
|
|
if (CALL_PREFIX(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-25 15:05:47 -04:00
|
|
|
return CALL_PREFIX(listen)(fd, (int)backlog) == 0;
|
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-25 20:44:52 -04:00
|
|
|
socklen_t len = sizeof(in_addr);
|
|
|
|
int newfd = CALL_PREFIX(accept)(fd, (sockaddr *)&in_addr, &len);
|
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
|
|
|
}
|
|
|
|
return new SocketAPM(false, newfd);
|
|
|
|
}
|
|
|
|
|
2023-11-25 15:05:47 -04:00
|
|
|
/*
|
|
|
|
return true if an address is in the multicast range
|
|
|
|
*/
|
|
|
|
bool SocketAPM::is_multicast_address(struct sockaddr_in &addr) const
|
|
|
|
{
|
|
|
|
const uint32_t mc_lower = 0xE0000000; // 224.0.0.0
|
|
|
|
const uint32_t mc_upper = 0xEFFFFFFF; // 239.255.255.255
|
|
|
|
const uint32_t haddr = ntohl(addr.sin_addr.s_addr);
|
|
|
|
return haddr >= mc_lower && haddr <= mc_upper;
|
|
|
|
}
|
|
|
|
|
2023-11-12 20:38:58 -04:00
|
|
|
#endif // AP_NETWORKING_BACKEND_ANY
|