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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AP_HAL.h>
|
|
|
|
#if HAL_OS_SOCKETS
|
|
|
|
|
|
|
|
#include "Socket.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
constructor
|
|
|
|
*/
|
|
|
|
SocketAPM::SocketAPM(bool _datagram) :
|
|
|
|
datagram(_datagram)
|
|
|
|
{
|
|
|
|
fd = socket(AF_INET, datagram?SOCK_DGRAM:SOCK_STREAM, 0);
|
2015-05-10 19:05:51 -03:00
|
|
|
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
2015-05-12 05:00:34 -03:00
|
|
|
int one = 1;
|
|
|
|
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
|
2015-05-05 08:27:33 -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
|
|
|
|
|
|
|
if (::connect(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
|
|
|
|
|
|
if (::bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
set SO_REUSEADDR
|
|
|
|
*/
|
|
|
|
void SocketAPM::reuseaddress(void)
|
|
|
|
{
|
|
|
|
int one = 1;
|
|
|
|
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
set blocking state
|
|
|
|
*/
|
|
|
|
void SocketAPM::set_blocking(bool blocking)
|
|
|
|
{
|
|
|
|
if (blocking) {
|
|
|
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) & ~O_NONBLOCK);
|
|
|
|
} else {
|
|
|
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
send some data
|
|
|
|
*/
|
2015-06-15 17:54:08 -03:00
|
|
|
ssize_t SocketAPM::send(const void *buf, size_t size)
|
2015-05-05 08:27:33 -03:00
|
|
|
{
|
|
|
|
return ::send(fd, buf, size, 0);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
return ::sendto(fd, buf, size, 0, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
|
|
|
|
}
|
|
|
|
|
2015-05-05 08:27:33 -03:00
|
|
|
/*
|
|
|
|
receive some data
|
|
|
|
*/
|
|
|
|
ssize_t SocketAPM::recv(void *buf, size_t size, 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;
|
|
|
|
|
|
|
|
if (select(fd+1, &fds, NULL, NULL, &tv) != 1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ::recv(fd, buf, size, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // HAL_OS_SOCKETS
|