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
|
|
|
|
2023-12-25 22:20:15 -04:00
|
|
|
#ifndef SOCKET_CLASS_NAME
|
|
|
|
#define SOCKET_CLASS_NAME SocketAPM
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef IN_SOCKET_NATIVE_CPP
|
|
|
|
#include "Socket.hpp"
|
|
|
|
#endif
|
|
|
|
|
2023-12-12 04:02:42 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_CHIBIOS || AP_NETWORKING_BACKEND_PPP
|
|
|
|
#include <lwip/sockets.h>
|
|
|
|
#else
|
|
|
|
// SITL or Linux
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
|
2023-10-19 19:56:31 -03:00
|
|
|
#include <errno.h>
|
2015-05-05 08:27:33 -03:00
|
|
|
|
2023-12-12 04:02:42 -04:00
|
|
|
#if AP_NETWORKING_BACKEND_CHIBIOS || AP_NETWORKING_BACKEND_PPP
|
2023-11-25 15:05:47 -04:00
|
|
|
#define CALL_PREFIX(x) ::lwip_##x
|
|
|
|
#else
|
|
|
|
#define CALL_PREFIX(x) ::x
|
|
|
|
#endif
|
|
|
|
|
2023-12-29 17:29:23 -04:00
|
|
|
#ifndef MSG_NOSIGNAL
|
|
|
|
#define MSG_NOSIGNAL 0
|
|
|
|
#endif
|
|
|
|
|
2015-05-05 08:27:33 -03:00
|
|
|
/*
|
|
|
|
constructor
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
SOCKET_CLASS_NAME::SOCKET_CLASS_NAME(bool _datagram) :
|
|
|
|
SOCKET_CLASS_NAME(_datagram,
|
2023-11-25 15:05:47 -04:00
|
|
|
CALL_PREFIX(socket)(AF_INET, _datagram?SOCK_DGRAM:SOCK_STREAM, 0))
|
2023-12-16 23:40:46 -04:00
|
|
|
{
|
2023-12-25 22:20:15 -04:00
|
|
|
static_assert(sizeof(SOCKET_CLASS_NAME::last_in_addr) >= sizeof(struct sockaddr_in), "last_in_addr must be at least sockaddr_in size");
|
2023-12-16 23:40:46 -04:00
|
|
|
}
|
2015-07-28 20:07:41 -03:00
|
|
|
|
2023-12-25 22:20:15 -04:00
|
|
|
SOCKET_CLASS_NAME::SOCKET_CLASS_NAME(bool _datagram, int _fd) :
|
2015-07-28 20:07:41 -03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-12-25 22:20:15 -04:00
|
|
|
SOCKET_CLASS_NAME::~SOCKET_CLASS_NAME()
|
2015-06-11 18:48:24 -03:00
|
|
|
{
|
2023-12-25 17:18:41 -04:00
|
|
|
if (fd != -1) {
|
|
|
|
CALL_PREFIX(close)(fd);
|
|
|
|
}
|
|
|
|
if (fd_in != -1) {
|
|
|
|
CALL_PREFIX(close)(fd_in);
|
|
|
|
}
|
2015-06-11 18:48:24 -03:00
|
|
|
}
|
|
|
|
|
2023-12-25 22:20:15 -04:00
|
|
|
void SOCKET_CLASS_NAME::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;
|
2023-12-25 17:18:41 -04:00
|
|
|
sockaddr.sin_addr.s_addr = htonl(inet_str_to_addr(address));
|
2015-05-22 02:53:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
connect the socket
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
bool SOCKET_CLASS_NAME::connect(const char *address, uint16_t port)
|
2015-05-22 02:53:33 -03:00
|
|
|
{
|
2023-12-03 19:11:48 -04:00
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-22 02:53:33 -03:00
|
|
|
struct sockaddr_in sockaddr;
|
2023-11-25 15:05:47 -04:00
|
|
|
int ret;
|
2023-11-27 18:22:54 -04:00
|
|
|
int one = 1;
|
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;
|
|
|
|
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) {
|
2023-11-27 18:22:54 -04:00
|
|
|
goto fail_multi;
|
2023-11-25 15:05:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-11-27 18:22:54 -04:00
|
|
|
goto fail_multi;
|
2023-11-25 15:05:47 -04:00
|
|
|
}
|
2023-11-27 18:22:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (datagram && sockaddr.sin_addr.s_addr == INADDR_BROADCAST) {
|
|
|
|
// setup for bi-directional UDP broadcast
|
2023-11-25 17:41:26 -04:00
|
|
|
set_broadcast();
|
2023-11-27 18:22:54 -04:00
|
|
|
reuseaddress();
|
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;
|
2023-11-27 18:22:54 -04:00
|
|
|
|
|
|
|
if (datagram && sockaddr.sin_addr.s_addr == INADDR_BROADCAST) {
|
|
|
|
// for bi-directional UDP broadcast we need 2 sockets
|
|
|
|
struct sockaddr_in send_addr;
|
|
|
|
socklen_t send_len = sizeof(send_addr);
|
|
|
|
ret = CALL_PREFIX(getsockname)(fd, (struct sockaddr *)&send_addr, &send_len);
|
|
|
|
fd_in = CALL_PREFIX(socket)(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (fd_in == -1) {
|
|
|
|
goto fail_multi;
|
|
|
|
}
|
|
|
|
CALL_PREFIX(setsockopt)(fd_in, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
|
|
|
|
// 2nd socket needs to be bound to wildcard
|
|
|
|
send_addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
ret = CALL_PREFIX(bind)(fd_in, (struct sockaddr *)&send_addr, sizeof(send_addr));
|
|
|
|
if (ret == -1) {
|
|
|
|
goto fail_multi;
|
|
|
|
}
|
|
|
|
}
|
2015-05-05 08:27:33 -03:00
|
|
|
return true;
|
2023-11-25 15:05:47 -04:00
|
|
|
|
2023-11-27 18:22:54 -04:00
|
|
|
fail_multi:
|
2023-11-25 15:05:47 -04:00
|
|
|
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
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
bool SOCKET_CLASS_NAME::connect_timeout(const char *address, uint16_t port, uint32_t timeout_ms)
|
2023-10-19 19:56:31 -03:00
|
|
|
{
|
2023-12-03 19:11:48 -04:00
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-10-19 19:56:31 -03:00
|
|
|
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);
|
2023-12-12 04:02:42 -04:00
|
|
|
if (CALL_PREFIX(getsockopt)(fd, SOL_SOCKET, SO_ERROR, (void*)&sock_error, &len) != 0) {
|
2023-10-19 19:56:31 -03:00
|
|
|
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
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
bool SOCKET_CLASS_NAME::bind(const char *address, uint16_t port)
|
2015-05-05 08:27:33 -03:00
|
|
|
{
|
2023-12-03 19:11:48 -04:00
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-05 08:27:33 -03:00
|
|
|
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-12-03 23:37:54 -04:00
|
|
|
reuseaddress();
|
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
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
bool SOCKET_CLASS_NAME::reuseaddress(void) const
|
2015-05-05 08:27:33 -03:00
|
|
|
{
|
2023-12-03 19:11:48 -04:00
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
bool SOCKET_CLASS_NAME::set_blocking(bool blocking) const
|
2015-05-05 08:27:33 -03:00
|
|
|
{
|
2023-12-03 19:11:48 -04:00
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
bool SOCKET_CLASS_NAME::set_cloexec() const
|
2018-06-08 01:12:35 -03:00
|
|
|
{
|
2023-12-03 19:11:48 -04:00
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
ssize_t SOCKET_CLASS_NAME::send(const void *buf, size_t size) const
|
2015-05-05 08:27:33 -03:00
|
|
|
{
|
2023-12-03 19:11:48 -04:00
|
|
|
if (fd == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2023-12-03 23:37:54 -04:00
|
|
|
return CALL_PREFIX(send)(fd, buf, size, MSG_NOSIGNAL);
|
2015-05-05 08:27:33 -03:00
|
|
|
}
|
|
|
|
|
2015-05-22 02:53:33 -03:00
|
|
|
/*
|
|
|
|
send some data
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
ssize_t SOCKET_CLASS_NAME::sendto(const void *buf, size_t size, const char *address, uint16_t port)
|
2015-05-22 02:53:33 -03:00
|
|
|
{
|
2023-12-03 19:11:48 -04:00
|
|
|
if (fd == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
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
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
ssize_t SOCKET_CLASS_NAME::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;
|
|
|
|
}
|
2023-12-16 23:40:46 -04:00
|
|
|
socklen_t len = sizeof(struct sockaddr_in);
|
2023-11-25 15:05:47 -04:00
|
|
|
int fin = get_read_fd();
|
|
|
|
ssize_t ret;
|
2023-12-16 23:40:46 -04:00
|
|
|
ret = CALL_PREFIX(recvfrom)(fin, buf, size, MSG_DONTWAIT, (sockaddr *)&last_in_addr[0], &len);
|
2023-11-25 15:05:47 -04:00
|
|
|
if (ret <= 0) {
|
2023-12-04 22:51:03 -04:00
|
|
|
if (!datagram && connected && ret == 0) {
|
|
|
|
// remote host has closed connection
|
|
|
|
connected = false;
|
|
|
|
}
|
2023-11-25 15:05:47 -04:00
|
|
|
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;
|
|
|
|
}
|
2023-12-16 23:40:46 -04:00
|
|
|
const struct sockaddr_in &sin = *(struct sockaddr_in *)&last_in_addr[0];
|
|
|
|
if (sin.sin_port == send_addr.sin_port &&
|
|
|
|
sin.sin_family == send_addr.sin_family &&
|
|
|
|
sin.sin_addr.s_addr == send_addr.sin_addr.s_addr) {
|
2023-11-25 15:05:47 -04:00
|
|
|
// 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
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
void SOCKET_CLASS_NAME::last_recv_address(const char *&ip_addr, uint16_t &port) const
|
2015-07-29 03:46:33 -03:00
|
|
|
{
|
2023-12-25 22:20:40 -04:00
|
|
|
// 16 bytes for aaa.bbb.ccc.ddd with null term
|
2023-12-12 04:02:42 -04:00
|
|
|
static char buf[16];
|
|
|
|
auto *str = last_recv_address(buf, sizeof(buf), port);
|
|
|
|
ip_addr = str;
|
2015-07-29 03:46:33 -03:00
|
|
|
}
|
|
|
|
|
2023-11-25 20:44:52 -04:00
|
|
|
/*
|
|
|
|
return the IP address and port of the last received packet, using caller supplied buffer
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
const char *SOCKET_CLASS_NAME::last_recv_address(char *ip_addr_buf, uint8_t buflen, uint16_t &port) const
|
2023-11-25 20:44:52 -04:00
|
|
|
{
|
2023-12-16 23:40:46 -04:00
|
|
|
const struct sockaddr_in &sin = *(struct sockaddr_in *)&last_in_addr[0];
|
|
|
|
|
2023-12-25 17:18:41 -04:00
|
|
|
const char *ret = inet_addr_to_str(ntohl(sin.sin_addr.s_addr), ip_addr_buf, buflen);
|
2023-11-25 20:44:52 -04:00
|
|
|
if (ret == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2023-12-16 23:40:46 -04:00
|
|
|
port = ntohs(sin.sin_port);
|
2023-11-25 20:44:52 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-12-25 22:20:15 -04:00
|
|
|
void SOCKET_CLASS_NAME::set_broadcast(void) const
|
2015-07-29 03:46:33 -03:00
|
|
|
{
|
2023-12-03 19:11:48 -04:00
|
|
|
if (fd == -1) {
|
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
bool SOCKET_CLASS_NAME::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();
|
2023-12-03 19:11:48 -04:00
|
|
|
if (fin == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-25 15:05:47 -04:00
|
|
|
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
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
bool SOCKET_CLASS_NAME::pollout(uint32_t timeout_ms)
|
2015-07-29 01:04:25 -03:00
|
|
|
{
|
2023-12-03 19:11:48 -04:00
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-29 01:04:25 -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;
|
|
|
|
|
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
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
bool SOCKET_CLASS_NAME::listen(uint16_t backlog) const
|
2015-07-28 20:07:41 -03:00
|
|
|
{
|
2023-12-03 19:11:48 -04:00
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
SOCKET_CLASS_NAME *SOCKET_CLASS_NAME::accept(uint32_t timeout_ms)
|
2015-07-28 20:07:41 -03:00
|
|
|
{
|
2023-12-03 19:11:48 -04:00
|
|
|
if (fd == -1) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-07-28 20:07:41 -03:00
|
|
|
if (!pollin(timeout_ms)) {
|
2016-10-30 02:24:21 -03:00
|
|
|
return nullptr;
|
2015-07-28 20:07:41 -03:00
|
|
|
}
|
|
|
|
|
2023-12-16 23:40:46 -04:00
|
|
|
struct sockaddr_in &sin = *(struct sockaddr_in *)&last_in_addr[0];
|
|
|
|
socklen_t len = sizeof(sin);
|
|
|
|
int newfd = CALL_PREFIX(accept)(fd, (sockaddr *)&sin, &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
|
|
|
}
|
2023-12-25 22:20:15 -04:00
|
|
|
auto *ret = new SOCKET_CLASS_NAME(false, newfd);
|
2023-12-03 23:37:54 -04:00
|
|
|
if (ret != nullptr) {
|
|
|
|
ret->connected = true;
|
|
|
|
ret->reuseaddress();
|
|
|
|
}
|
|
|
|
return ret;
|
2015-07-28 20:07:41 -03:00
|
|
|
}
|
|
|
|
|
2023-11-25 15:05:47 -04:00
|
|
|
/*
|
|
|
|
return true if an address is in the multicast range
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
bool SOCKET_CLASS_NAME::is_multicast_address(struct sockaddr_in &addr) const
|
2023-11-25 15:05:47 -04:00
|
|
|
{
|
|
|
|
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-12-25 22:20:15 -04:00
|
|
|
void SOCKET_CLASS_NAME::close(void)
|
2023-12-03 19:11:48 -04:00
|
|
|
{
|
|
|
|
if (fd != -1) {
|
|
|
|
CALL_PREFIX(close)(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
if (fd_in != -1) {
|
|
|
|
CALL_PREFIX(close)(fd_in);
|
|
|
|
fd_in = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 14:29:48 -04:00
|
|
|
/*
|
|
|
|
duplicate a socket, giving a new object with the same contents,
|
|
|
|
the fd in the old object is set to -1
|
|
|
|
*/
|
2023-12-25 22:20:15 -04:00
|
|
|
SOCKET_CLASS_NAME *SOCKET_CLASS_NAME::duplicate(void)
|
2023-12-07 14:29:48 -04:00
|
|
|
{
|
2023-12-25 22:20:15 -04:00
|
|
|
auto *ret = new SOCKET_CLASS_NAME(datagram, fd);
|
2023-12-07 14:29:48 -04:00
|
|
|
if (ret == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
ret->fd_in = fd_in;
|
|
|
|
ret->connected = connected;
|
|
|
|
fd = -1;
|
|
|
|
fd_in = -1;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-12-25 17:18:41 -04:00
|
|
|
// access to inet_ntop, takes host order ipv4 as uint32_t
|
2023-12-25 22:20:15 -04:00
|
|
|
const char *SOCKET_CLASS_NAME::inet_addr_to_str(uint32_t addr, char *dst, uint16_t len)
|
2023-12-16 23:40:46 -04:00
|
|
|
{
|
2023-12-25 17:18:41 -04:00
|
|
|
addr = htonl(addr);
|
|
|
|
return CALL_PREFIX(inet_ntop)(AF_INET, (void*)&addr, dst, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
// access to inet_pton, returns host order ipv4 as uint32_t
|
2023-12-25 22:20:15 -04:00
|
|
|
uint32_t SOCKET_CLASS_NAME::inet_str_to_addr(const char *ipstr)
|
2023-12-25 17:18:41 -04:00
|
|
|
{
|
|
|
|
uint32_t ret = 0;
|
|
|
|
CALL_PREFIX(inet_pton)(AF_INET, ipstr, &ret);
|
|
|
|
return ntohl(ret);
|
|
|
|
|
2023-12-16 23:40:46 -04:00
|
|
|
}
|
|
|
|
|
2023-11-12 20:38:58 -04:00
|
|
|
#endif // AP_NETWORKING_BACKEND_ANY
|