2013-07-04 20:56:19 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Global Peer to Peer Ethernet
|
|
|
|
* Copyright (C) 2012-2013 ZeroTier Networks LLC
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
|
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
*
|
|
|
|
* If you would like to embed ZeroTier into a commercial application or
|
|
|
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
|
|
* LLC. Start here: http://www.zerotier.com/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
2013-08-12 20:18:35 +00:00
|
|
|
#include <sys/stat.h>
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-08-13 01:25:36 +00:00
|
|
|
#include "Constants.hpp"
|
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
2013-08-12 20:18:35 +00:00
|
|
|
#include <WinSock2.h>
|
|
|
|
#include <WS2tcpip.h>
|
2013-08-13 01:25:36 +00:00
|
|
|
#include <Windows.h>
|
2013-07-04 20:56:19 +00:00
|
|
|
#else
|
2013-08-12 20:18:35 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <arpa/inet.h>
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "UdpSocket.hpp"
|
|
|
|
#include "RuntimeEnvironment.hpp"
|
|
|
|
#include "Logger.hpp"
|
|
|
|
#include "Switch.hpp"
|
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
UdpSocket::UdpSocket(
|
2013-07-17 18:10:44 +00:00
|
|
|
bool localOnly,
|
2013-07-04 20:56:19 +00:00
|
|
|
int localPort,
|
|
|
|
bool ipv6,
|
|
|
|
void (*packetHandler)(UdpSocket *,void *,const InetAddress &,const void *,unsigned int),
|
|
|
|
void *arg)
|
|
|
|
throw(std::runtime_error) :
|
|
|
|
_packetHandler(packetHandler),
|
|
|
|
_arg(arg),
|
|
|
|
_localPort(localPort),
|
|
|
|
_sock(0),
|
|
|
|
_v6(ipv6)
|
|
|
|
{
|
2013-08-12 20:18:35 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
BOOL yes,no;
|
|
|
|
#else
|
2013-07-04 20:56:19 +00:00
|
|
|
int yes,no;
|
2013-08-12 20:18:35 +00:00
|
|
|
#endif
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
if ((localPort <= 0)||(localPort > 0xffff))
|
|
|
|
throw std::runtime_error("port is out of range");
|
|
|
|
|
|
|
|
if (ipv6) {
|
|
|
|
_sock = socket(AF_INET6,SOCK_DGRAM,0);
|
|
|
|
if (_sock <= 0)
|
|
|
|
throw std::runtime_error("unable to create IPv6 SOCK_DGRAM socket");
|
|
|
|
|
2013-08-12 20:18:35 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
yes = TRUE; setsockopt(_sock,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&yes,sizeof(yes));
|
|
|
|
no = FALSE; setsockopt(_sock,SOL_SOCKET,SO_REUSEADDR,(const char *)&no,sizeof(no));
|
|
|
|
no = FALSE; setsockopt(_sock,IPPROTO_IPV6,IPV6_DONTFRAG,(const char *)&no,sizeof(no));
|
|
|
|
#else
|
2013-07-04 20:56:19 +00:00
|
|
|
yes = 1; setsockopt(_sock,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&yes,sizeof(yes));
|
|
|
|
no = 0; setsockopt(_sock,SOL_SOCKET,SO_REUSEADDR,(void *)&no,sizeof(no));
|
|
|
|
#ifdef IP_DONTFRAG
|
|
|
|
no = 0; setsockopt(_sock,IPPROTO_IP,IP_DONTFRAG,&no,sizeof(no));
|
|
|
|
#endif
|
|
|
|
#ifdef IP_MTU_DISCOVER
|
|
|
|
no = 0; setsockopt(_sock,IPPROTO_IP,IP_MTU_DISCOVER,&no,sizeof(no));
|
|
|
|
#endif
|
|
|
|
#ifdef IPV6_MTU_DISCOVER
|
|
|
|
no = 0; setsockopt(_sock,IPPROTO_IPV6,IPV6_MTU_DISCOVER,&no,sizeof(no));
|
2013-08-12 20:18:35 +00:00
|
|
|
#endif
|
2013-07-04 20:56:19 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
memset(&sin6,0,sizeof(sin6));
|
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
sin6.sin6_port = htons(localPort);
|
2013-07-17 18:10:44 +00:00
|
|
|
if (localOnly)
|
2013-07-17 18:39:34 +00:00
|
|
|
memcpy(&(sin6.sin6_addr.s6_addr),InetAddress::LO6.rawIpData(),16);
|
2013-07-17 18:10:44 +00:00
|
|
|
else memcpy(&(sin6.sin6_addr),&in6addr_any,sizeof(struct in6_addr));
|
2013-07-04 20:56:19 +00:00
|
|
|
if (::bind(_sock,(const struct sockaddr *)&sin6,sizeof(sin6))) {
|
2013-08-12 20:18:35 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
::closesocket(_sock);
|
|
|
|
#else
|
2013-07-04 20:56:19 +00:00
|
|
|
::close(_sock);
|
2013-08-12 20:18:35 +00:00
|
|
|
#endif
|
2013-07-04 20:56:19 +00:00
|
|
|
throw std::runtime_error("unable to bind to port");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_sock = socket(AF_INET,SOCK_DGRAM,0);
|
|
|
|
if (_sock <= 0)
|
|
|
|
throw std::runtime_error("unable to create IPv4 SOCK_DGRAM socket");
|
|
|
|
|
2013-08-12 20:18:35 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
no = FALSE; setsockopt(_sock,SOL_SOCKET,SO_REUSEADDR,(const char *)&no,sizeof(no));
|
|
|
|
no = FALSE; setsockopt(_sock,IPPROTO_IP,IP_DONTFRAGMENT,(const char *)&no,sizeof(no));
|
|
|
|
#else
|
2013-07-04 20:56:19 +00:00
|
|
|
no = 0; setsockopt(_sock,SOL_SOCKET,SO_REUSEADDR,(void *)&no,sizeof(no));
|
|
|
|
#ifdef IP_DONTFRAG
|
|
|
|
no = 0; setsockopt(_sock,IPPROTO_IP,IP_DONTFRAG,&no,sizeof(no));
|
|
|
|
#endif
|
|
|
|
#ifdef IP_MTU_DISCOVER
|
|
|
|
no = 0; setsockopt(_sock,IPPROTO_IP,IP_MTU_DISCOVER,&no,sizeof(no));
|
2013-08-12 20:18:35 +00:00
|
|
|
#endif
|
2013-07-04 20:56:19 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
memset(&sin,0,sizeof(sin));
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = htons(localPort);
|
2013-07-17 18:10:44 +00:00
|
|
|
if (localOnly)
|
2013-07-17 18:39:34 +00:00
|
|
|
memcpy(&(sin.sin_addr.s_addr),InetAddress::LO4.rawIpData(),4);
|
2013-07-17 18:10:44 +00:00
|
|
|
else sin.sin_addr.s_addr = INADDR_ANY;
|
2013-07-04 20:56:19 +00:00
|
|
|
if (::bind(_sock,(const struct sockaddr *)&sin,sizeof(sin))) {
|
2013-08-12 20:18:35 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
::closesocket(_sock);
|
|
|
|
#else
|
2013-07-04 20:56:19 +00:00
|
|
|
::close(_sock);
|
2013-08-12 20:18:35 +00:00
|
|
|
#endif
|
2013-07-04 20:56:19 +00:00
|
|
|
throw std::runtime_error("unable to bind to port");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 13:19:36 +00:00
|
|
|
_thread = Thread::start(this);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UdpSocket::~UdpSocket()
|
|
|
|
{
|
2013-08-05 21:44:39 +00:00
|
|
|
int s = _sock;
|
|
|
|
_sock = 0;
|
|
|
|
if (s > 0) {
|
2013-08-12 20:18:35 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
::shutdown(s,SD_BOTH);
|
|
|
|
::closesocket(s);
|
|
|
|
#else
|
2013-08-05 21:44:39 +00:00
|
|
|
::shutdown(s,SHUT_RDWR);
|
|
|
|
::close(s);
|
2013-08-12 20:18:35 +00:00
|
|
|
#endif
|
2013-08-05 21:44:39 +00:00
|
|
|
}
|
2013-08-08 13:19:36 +00:00
|
|
|
Thread::join(_thread);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UdpSocket::send(const InetAddress &to,const void *data,unsigned int len,int hopLimit)
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_sendLock);
|
|
|
|
if (to.isV6()) {
|
|
|
|
if (!_v6)
|
|
|
|
return false;
|
2013-08-12 20:18:35 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
DWORD hltmp = (DWORD)hopLimit;
|
|
|
|
setsockopt(_sock,IPPROTO_IPV6,IPV6_UNICAST_HOPS,(const char *)&hltmp,sizeof(hltmp));
|
|
|
|
return ((int)sendto(_sock,(const char *)data,len,0,to.saddr(),to.saddrLen()) == (int)len);
|
|
|
|
#else
|
2013-07-04 20:56:19 +00:00
|
|
|
setsockopt(_sock,IPPROTO_IPV6,IPV6_UNICAST_HOPS,&hopLimit,sizeof(hopLimit));
|
|
|
|
return ((int)sendto(_sock,data,len,0,to.saddr(),to.saddrLen()) == (int)len);
|
2013-08-12 20:18:35 +00:00
|
|
|
#endif
|
2013-07-04 20:56:19 +00:00
|
|
|
} else {
|
|
|
|
if (_v6)
|
|
|
|
return false;
|
2013-08-12 20:18:35 +00:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
DWORD hltmp = (DWORD)hopLimit;
|
|
|
|
setsockopt(_sock,IPPROTO_IP,IP_TTL,(const char *)&hltmp,sizeof(hltmp));
|
|
|
|
return ((int)sendto(_sock,(const char *)data,len,0,to.saddr(),to.saddrLen()) == (int)len);
|
|
|
|
#else
|
2013-07-04 20:56:19 +00:00
|
|
|
setsockopt(_sock,IPPROTO_IP,IP_TTL,&hopLimit,sizeof(hopLimit));
|
|
|
|
return ((int)sendto(_sock,data,len,0,to.saddr(),to.saddrLen()) == (int)len);
|
2013-08-12 20:18:35 +00:00
|
|
|
#endif
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 20:06:16 +00:00
|
|
|
void UdpSocket::threadMain()
|
2013-07-04 20:56:19 +00:00
|
|
|
throw()
|
|
|
|
{
|
2013-08-05 21:44:39 +00:00
|
|
|
char buf[65536];
|
2013-07-04 20:56:19 +00:00
|
|
|
InetAddress from;
|
|
|
|
socklen_t salen;
|
|
|
|
int n;
|
|
|
|
|
2013-08-05 21:44:39 +00:00
|
|
|
while (_sock > 0) {
|
2013-07-04 20:56:19 +00:00
|
|
|
salen = from.saddrSpaceLen();
|
|
|
|
n = (int)recvfrom(_sock,buf,sizeof(buf),0,from.saddr(),&salen);
|
|
|
|
if (n < 0) {
|
|
|
|
if ((errno != EINTR)&&(errno != ETIMEDOUT))
|
|
|
|
break;
|
2013-08-05 21:44:39 +00:00
|
|
|
} else if (n > 0) {
|
|
|
|
try {
|
|
|
|
_packetHandler(this,_arg,from,buf,(unsigned int)n);
|
|
|
|
} catch ( ... ) {} // should never be thrown from here anyway...
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|