2015-12-29 23:45:47 +01:00
|
|
|
//
|
|
|
|
|
// cudpsocket.cpp
|
|
|
|
|
// xlxd
|
|
|
|
|
//
|
|
|
|
|
// Created by Jean-Luc Deltombe (LX3JL) on 31/10/2015.
|
|
|
|
|
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
// This file is part of xlxd.
|
|
|
|
|
//
|
|
|
|
|
// xlxd 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.
|
|
|
|
|
//
|
|
|
|
|
// xlxd 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include "creflector.h"
|
|
|
|
|
#include "cudpsocket.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// constructor
|
|
|
|
|
|
|
|
|
|
CUdpSocket::CUdpSocket()
|
|
|
|
|
{
|
2019-10-09 03:29:21 +02:00
|
|
|
for ( int i = 0; i < UDP_SOCKET_MAX; i++ )
|
|
|
|
|
{
|
|
|
|
|
m_Socket[i] = -1;
|
|
|
|
|
}
|
2015-12-29 23:45:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// destructor
|
|
|
|
|
|
|
|
|
|
CUdpSocket::~CUdpSocket()
|
|
|
|
|
{
|
2019-10-09 03:29:21 +02:00
|
|
|
Close();
|
2015-12-29 23:45:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// open & close
|
|
|
|
|
|
|
|
|
|
bool CUdpSocket::Open(uint16 uiPort)
|
|
|
|
|
{
|
|
|
|
|
bool open = false;
|
2019-10-09 03:29:21 +02:00
|
|
|
struct sockaddr_storage *ss;
|
|
|
|
|
socklen_t ss_len;
|
2015-12-29 23:45:47 +01:00
|
|
|
|
2019-10-09 03:29:21 +02:00
|
|
|
for ( int i = 0; i < UDP_SOCKET_MAX; i++ ) {
|
|
|
|
|
m_Ip[i] = CIp(g_Reflector.GetListenIp(i), uiPort);
|
|
|
|
|
ss = m_Ip[i].GetSockAddr(ss_len);
|
2015-12-29 23:45:47 +01:00
|
|
|
|
2019-10-09 03:29:21 +02:00
|
|
|
// create socket
|
|
|
|
|
// (avoid INADDR_ANY on secondary and later IP address)
|
|
|
|
|
m_Socket[i] = ( i != 0 && m_Ip[i] == CIp() ) ?
|
|
|
|
|
-1 : socket(ss->ss_family, SOCK_DGRAM, 0);
|
|
|
|
|
if ( m_Socket[i] != -1 )
|
2015-12-29 23:45:47 +01:00
|
|
|
{
|
2019-10-09 03:29:21 +02:00
|
|
|
if ( bind(m_Socket[i], (struct sockaddr *)ss, ss_len) == 0 )
|
|
|
|
|
{
|
|
|
|
|
fcntl(m_Socket[i], F_SETFL, O_NONBLOCK);
|
|
|
|
|
open |= true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
close(m_Socket[i]);
|
|
|
|
|
m_Socket[i] = -1;
|
|
|
|
|
}
|
2015-12-29 23:45:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// done
|
|
|
|
|
return open;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CUdpSocket::Close(void)
|
|
|
|
|
{
|
2019-10-09 03:29:21 +02:00
|
|
|
for ( int i = 0; i < UDP_SOCKET_MAX; i++ )
|
|
|
|
|
{
|
|
|
|
|
if ( m_Socket[i] != -1 )
|
|
|
|
|
{
|
|
|
|
|
close(m_Socket[i]);
|
|
|
|
|
m_Socket[i] = -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CUdpSocket::GetSocket(const CIp &Ip)
|
|
|
|
|
{
|
|
|
|
|
CIp temp(Ip);
|
|
|
|
|
socklen_t ss_len;
|
|
|
|
|
struct sockaddr_storage *ss = temp.GetSockAddr(ss_len);
|
|
|
|
|
sa_family_t ss_family = ss->ss_family;
|
|
|
|
|
|
|
|
|
|
for ( int i = 0; i < UDP_SOCKET_MAX; i++ )
|
2015-12-29 23:45:47 +01:00
|
|
|
{
|
2019-10-09 03:29:21 +02:00
|
|
|
ss = m_Ip[i].GetSockAddr(ss_len);
|
|
|
|
|
if ( ss_family == ss->ss_family )
|
|
|
|
|
{
|
|
|
|
|
return m_Socket[i];
|
|
|
|
|
}
|
2015-12-29 23:45:47 +01:00
|
|
|
}
|
2019-10-09 03:29:21 +02:00
|
|
|
|
|
|
|
|
return -1;
|
2015-12-29 23:45:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// read
|
|
|
|
|
|
|
|
|
|
int CUdpSocket::Receive(CBuffer *Buffer, CIp *Ip, int timeout)
|
|
|
|
|
{
|
2019-10-09 03:29:21 +02:00
|
|
|
struct sockaddr_storage Sin;
|
|
|
|
|
struct pollfd pfd[UDP_SOCKET_MAX];
|
|
|
|
|
socklen_t uiFromLen = sizeof(Sin);
|
|
|
|
|
int i, socks, index, iRecvLen = -1;
|
2015-12-29 23:45:47 +01:00
|
|
|
|
|
|
|
|
// socket valid ?
|
2019-10-09 03:29:21 +02:00
|
|
|
for ( i = 0, socks = 0; i < UDP_SOCKET_MAX; i++ )
|
|
|
|
|
{
|
|
|
|
|
if ( m_Socket[i] != -1 )
|
|
|
|
|
{
|
|
|
|
|
pfd[socks].fd = m_Socket[i];
|
|
|
|
|
pfd[socks].events = POLLIN;
|
|
|
|
|
socks++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( socks != 0 )
|
2015-12-29 23:45:47 +01:00
|
|
|
{
|
|
|
|
|
// control socket
|
2019-10-09 03:29:21 +02:00
|
|
|
poll(pfd, socks, timeout);
|
2015-12-29 23:45:47 +01:00
|
|
|
|
2019-10-09 03:29:21 +02:00
|
|
|
for ( i = 0; i < socks; i++ )
|
2015-12-29 23:45:47 +01:00
|
|
|
{
|
2019-10-09 03:29:21 +02:00
|
|
|
// round robin
|
|
|
|
|
index = (i + m_Counter) % socks;
|
2015-12-29 23:45:47 +01:00
|
|
|
|
2019-10-09 03:29:21 +02:00
|
|
|
if ( pfd[index].revents & POLLIN )
|
|
|
|
|
{
|
|
|
|
|
// allocate buffer
|
|
|
|
|
Buffer->resize(UDP_BUFFER_LENMAX);
|
|
|
|
|
|
|
|
|
|
// read
|
|
|
|
|
iRecvLen = (int)recvfrom(pfd[index].fd,
|
|
|
|
|
(void *)Buffer->data(), UDP_BUFFER_LENMAX,
|
|
|
|
|
0, (struct sockaddr *)&Sin, &uiFromLen);
|
|
|
|
|
|
|
|
|
|
// handle
|
|
|
|
|
if ( iRecvLen != -1 )
|
|
|
|
|
{
|
|
|
|
|
// adjust buffer size
|
|
|
|
|
Buffer->resize(iRecvLen);
|
|
|
|
|
|
|
|
|
|
// get IP
|
|
|
|
|
Ip->SetSockAddr(&Sin, uiFromLen);
|
|
|
|
|
|
|
|
|
|
m_Counter++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-29 23:45:47 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-10-09 03:29:21 +02:00
|
|
|
|
2015-12-29 23:45:47 +01:00
|
|
|
// done
|
|
|
|
|
return iRecvLen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// write
|
|
|
|
|
|
|
|
|
|
int CUdpSocket::Send(const CBuffer &Buffer, const CIp &Ip)
|
|
|
|
|
{
|
|
|
|
|
CIp temp(Ip);
|
2019-10-09 03:29:21 +02:00
|
|
|
socklen_t ss_len;
|
|
|
|
|
struct sockaddr_storage *ss = temp.GetSockAddr(ss_len);
|
|
|
|
|
return (int)::sendto(GetSocket(Ip),
|
2015-12-29 23:45:47 +01:00
|
|
|
(void *)Buffer.data(), Buffer.size(),
|
2019-10-09 03:29:21 +02:00
|
|
|
0, (struct sockaddr *)ss, ss_len);
|
2015-12-29 23:45:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CUdpSocket::Send(const char *Buffer, const CIp &Ip)
|
|
|
|
|
{
|
|
|
|
|
CIp temp(Ip);
|
2019-10-09 03:29:21 +02:00
|
|
|
socklen_t ss_len;
|
|
|
|
|
struct sockaddr_storage *ss = temp.GetSockAddr(ss_len);
|
|
|
|
|
return (int)::sendto(GetSocket(Ip),
|
2015-12-29 23:45:47 +01:00
|
|
|
(void *)Buffer, ::strlen(Buffer),
|
2019-10-09 03:29:21 +02:00
|
|
|
0, (struct sockaddr *)ss, ss_len);
|
2015-12-29 23:45:47 +01:00
|
|
|
}
|
2016-02-11 18:51:12 +01:00
|
|
|
|
|
|
|
|
int CUdpSocket::Send(const CBuffer &Buffer, const CIp &Ip, uint16 destport)
|
|
|
|
|
{
|
2019-10-09 03:29:21 +02:00
|
|
|
CIp temp(Ip, destport);
|
|
|
|
|
socklen_t ss_len;
|
|
|
|
|
struct sockaddr_storage *ss = temp.GetSockAddr(ss_len);
|
|
|
|
|
return (int)::sendto(GetSocket(Ip),
|
2016-02-11 18:51:12 +01:00
|
|
|
(void *)Buffer.data(), Buffer.size(),
|
2019-10-09 03:29:21 +02:00
|
|
|
0, (struct sockaddr *)ss, ss_len);
|
2016-02-11 18:51:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int CUdpSocket::Send(const char *Buffer, const CIp &Ip, uint16 destport)
|
|
|
|
|
{
|
2019-10-09 03:29:21 +02:00
|
|
|
CIp temp(Ip, destport);
|
|
|
|
|
socklen_t ss_len;
|
|
|
|
|
struct sockaddr_storage *ss = temp.GetSockAddr(ss_len);
|
|
|
|
|
return (int)::sendto(GetSocket(Ip),
|
2016-02-11 18:51:12 +01:00
|
|
|
(void *)Buffer, ::strlen(Buffer),
|
2019-10-09 03:29:21 +02:00
|
|
|
0, (struct sockaddr *)ss, ss_len);
|
2016-02-11 18:51:12 +01:00
|
|
|
}
|