1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
///////////////////////////////////////////////////////////////////////////////////
// SDRdaemon - send I/Q samples read from a SDR device over the network via UDP. //
// //
// Copyright (C) 2015 Edouard Griffiths, F4EXB //
// //
// 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 as version 3 of the License, or //
// //
// 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 V3 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/>. //
///////////////////////////////////////////////////////////////////////////////////
// Original code is posted at: https://cppcodetips.wordpress.com/2014/01/29/udp-socket-class-in-c/
#include "UDPSocket.h"
#include <errno.h>
#include <cstring>
#include <fcntl.h>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <pthread.h>
#include <unistd.h>
#include <net/if.h>
CSocketException::CSocketException( const string &sMessage, bool blSysMsg /*= false*/ ) : m_sMsg(sMessage)
{
if (blSysMsg) {
m_sMsg.append(": ");
m_sMsg.append(strerror(errno));
}
}
CSocketException::~CSocketException() throw()
{
}
CSocket::~CSocket()
{
::close(m_sockDesc);
m_sockDesc = -1;
}
CSocket::CSocket( SocketType type, NetworkLayerProtocol protocol ) : m_sockDesc(-1)
{
m_sockDesc = socket(protocol, type, 0);
if (m_sockDesc < 0)
{
throw CSocketException("Socket creation failed (socket())", true);
}
}
CSocket::CSocket( int sockDesc )
{
m_sockDesc = sockDesc;
}
CSocket::CSocket( const CSocket &sock __attribute__((unused)))
{
}
void CSocket::operator=( const CSocket &sock __attribute__((unused)))
{
}
std::string CSocket::GetLocalAddress()
{
sockaddr_in addr;
unsigned int addr_len = sizeof(addr);
if (getsockname(m_sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) {
throw CSocketException("Fetch of local address failed (getsockname())", true);
}
return inet_ntoa(addr.sin_addr);
}
unsigned short CSocket::GetLocalPort()
{
sockaddr_in addr;
unsigned int addr_len = sizeof(addr);
if (getsockname(m_sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) {
throw CSocketException("Fetch of local port failed (getsockname())", true);
}
return ntohs(addr.sin_port);
}
void CSocket::BindLocalPort( unsigned short localPort )
{
// Bind the socket to its port
sockaddr_in localAddr;
memset(&localAddr, 0, sizeof(localAddr));
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(localPort);
if (::bind(m_sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) {
throw CSocketException("Set of local port failed (bind())", true);
}
}
void CSocket::BindLocalAddressAndPort( const string &localAddress, unsigned short localPort /*= 0*/ )
{
// Get the address of the requested host
sockaddr_in localAddr;
FillAddr(localAddress, localPort, localAddr);
if (::bind(m_sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) {
throw CSocketException("Set of local address and port failed (bind())", true);
}
}
void CSocket::FillAddr( const string & localAddress, unsigned short localPort, sockaddr_in& localAddr )
{
////cout<<"\n Inside Fille addr:"<<localAddress <<" port:"<<localPort;
memset(&localAddr, 0, sizeof(localAddr)); // Zero out address structure
localAddr.sin_family = AF_INET; // Internet address
hostent *host; // Resolve name
if ((host = gethostbyname(localAddress.c_str())) == NULL) {
// strerror() will not work for gethostbyname() and hstrerror()
// is supposedly obsolete
throw CSocketException("Failed to resolve name (gethostbyname())");
}
localAddr.sin_addr.s_addr = *((unsigned long *) host->h_addr_list[0]);
localAddr.sin_port = htons(localPort); // Assign port in network byte order
////cout<<"\n returning from Fille addr";
}
unsigned long int CSocket::GetReadBufferSize()
{
unsigned long int nSize;
socklen_t n = sizeof(nSize);
getsockopt(m_sockDesc,SOL_SOCKET,SO_RCVBUF,(void *)&nSize, (&n));
// now the variable nSize will have the socket size
return nSize;
}
void CSocket::SetReadBufferSize( unsigned int nSize )
{
if (setsockopt(m_sockDesc, SOL_SOCKET, SO_RCVBUF, &nSize, sizeof(nSize)) == -1)
{
throw CSocketException("Error in setting socket buffer size ", true);
}
}
void CSocket::SetNonBlocking( bool bBlocking )
{
int opts;
opts = fcntl ( m_sockDesc, F_GETFL );
if ( opts < 0 )
{
return;
}
if ( bBlocking )
opts = ( opts | O_NONBLOCK );
else
opts = ( opts & ~O_NONBLOCK );
fcntl ( m_sockDesc, F_SETFL,opts );
}
void CSocket::ConnectToHost( const string &foreignAddress, unsigned short foreignPort )
{
//cout<<"\nstart Connect to host";
// Get the address of the requested host
sockaddr_in destAddr;
//cout<<"\ninside Connect to host";
FillAddr(foreignAddress, foreignPort, destAddr);
//cout<<"trying to connect to host";
// Try to connect to the given port
if (::connect(m_sockDesc, (sockaddr *) &destAddr, sizeof(destAddr)) < 0) {
throw CSocketException("Connect failed (connect())", true);
}
//cout<<"\n after connecting";
}
void CSocket::Send( const void *buffer, int bufferLen )
{
if (::send(m_sockDesc, (void *) buffer, bufferLen, 0) < 0) {
throw CSocketException("Send failed (send())", true);
}
}
int CSocket::Recv( void *buffer, int bufferLen )
{
int nBytes;
if ((nBytes = ::recv(m_sockDesc, (void *) buffer, bufferLen, 0)) < 0) {
throw CSocketException("Received failed (recv())", true);
}
char* sData = static_cast<char *>(buffer);
sData[nBytes] = '\0';
return nBytes;
}
std::string CSocket::GetPeerAddress()
{
sockaddr_in addr;
unsigned int addr_len = sizeof(addr);
if (getpeername(m_sockDesc, (sockaddr *) &addr,(socklen_t *) &addr_len) < 0) {
throw CSocketException("Fetch of foreign address failed (getpeername())", true);
}
return inet_ntoa(addr.sin_addr);
}
unsigned short CSocket::GetPeerPort()
{
sockaddr_in addr;
unsigned int addr_len = sizeof(addr);
if (getpeername(m_sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) {
throw CSocketException("Fetch of foreign port failed (getpeername())", true);
}
return ntohs(addr.sin_port);
}
CSocket& CSocket::operator<<(const string& sStr )
{
Send(sStr.c_str(), sStr.length());
return *this;
}
CSocket& CSocket::operator>>( string& sStr )
{
char *buff = new char[GetReadBufferSize()];
Recv(buff, GetReadBufferSize());
sStr.append(buff);
delete [] buff;
return *this;
}
int CSocket::OnDataRead(unsigned long timeToWait)
{
/* master file descriptor list */
fd_set master;
//struct timeval *ptimeout = NULL;
/* temp file descriptor list for select() */
fd_set read_fds;
/* maximum file descriptor number */
int fdmax;
/* clear the master and temp sets */
FD_ZERO(&master);
FD_ZERO(&read_fds);
/* add the listener to the master set */
FD_SET(m_sockDesc, &master);
/* keep track of the biggest file descriptor */
fdmax = m_sockDesc; /* so far, it's this one*/
/* copy it */
read_fds = master;
//cout<<"Waiting for select";
int nRet;
if (timeToWait == ULONG_MAX)
{
nRet = select(fdmax+1, &read_fds, NULL, NULL, NULL);
if (nRet == -1)
nRet = DATA_EXCEPTION;
else if (nRet > 0)
nRet = DATA_ARRIVED;
}
else
{
struct timeval timeout;
timeout.tv_sec = timeToWait;
timeout.tv_usec = 0;
nRet = select(fdmax+1, &read_fds, NULL, NULL, &timeout);
if (nRet == -1)
nRet = DATA_EXCEPTION;
else if (nRet > 0)
nRet = DATA_ARRIVED;
else if(nRet == 0)
nRet = DATA_TIMED_OUT;
}
return nRet;
}
void CSocket::SetBindToDevice( const string& sInterface )
{
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", sInterface.c_str());
//Todo:SO_BINDTODEVICE not declared error comes in CygWin, need to compile in Linux.
/*int nRet = ::setsockopt(m_sockDesc, SOL_SOCKET, SO_BINDTODEVICE, (void*)&ifr, sizeof(ifr));
if (nRet < 0)
{
throw CSocketException("Error in binding to device ", true);
}*/
}
UDPSocket::UDPSocket() : CSocket(UdpSocket,IPv4Protocol)
{
SetBroadcast();
}
UDPSocket::UDPSocket( unsigned short localPort ) :
CSocket(UdpSocket,IPv4Protocol)
{
BindLocalPort(localPort);
SetBroadcast();
}
UDPSocket::UDPSocket( const string &localAddress, unsigned short localPort ) :
CSocket(UdpSocket,IPv4Protocol)
{
BindLocalAddressAndPort(localAddress, localPort);
SetBroadcast();
}
void UDPSocket::DisconnectFromHost()
{
sockaddr_in nullAddr;
memset(&nullAddr, 0, sizeof(nullAddr));
nullAddr.sin_family = AF_UNSPEC;
// Try to disconnect
if (::connect(m_sockDesc, (sockaddr *) &nullAddr, sizeof(nullAddr)) < 0)
{
if (errno != EAFNOSUPPORT)
{
throw CSocketException("Disconnect failed (connect())", true);
}
}
}
void UDPSocket::SendDataGram( const void *buffer, int bufferLen, const string &foreignAddress,
unsigned short foreignPort )
{
//cout<<"Befor Fill addr";
sockaddr_in destAddr;
FillAddr(foreignAddress, foreignPort, destAddr);
//cout<<"Befor socket send";
// Write out the whole buffer as a single message.
if (sendto(m_sockDesc, (void *) buffer, bufferLen, 0,(sockaddr *) &destAddr, sizeof(destAddr)) != bufferLen)
{
throw CSocketException("Send failed (sendto())", true);
}
}
int UDPSocket::RecvDataGram( void *buffer, int bufferLen, string &sourceAddress, unsigned short &sourcePort )
{
sockaddr_in clntAddr;
socklen_t addrLen = sizeof(clntAddr);
int nBytes;
if ((nBytes = recvfrom(m_sockDesc, (void *) buffer, bufferLen, 0, (sockaddr *) &clntAddr,
(socklen_t *) &addrLen)) < 0)
{
throw CSocketException("Receive failed (recvfrom())", true);
}
sourceAddress = inet_ntoa(clntAddr.sin_addr);
sourcePort = ntohs(clntAddr.sin_port);
char* sData = static_cast<char *>(buffer);
sData[nBytes] = '\0';
return nBytes;
}
void UDPSocket::SetMulticastTTL( unsigned char multicastTTL )
{
if (setsockopt(m_sockDesc, IPPROTO_IP, IP_MULTICAST_TTL, (void *) &multicastTTL, sizeof(multicastTTL)) < 0)
{
throw CSocketException("Multicast TTL set failed (setsockopt())", true);
}
}
void UDPSocket::JoinGroup( const string &multicastGroup )
{
struct ip_mreq multicastRequest;
multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str());
multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(m_sockDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(void *) &multicastRequest,
sizeof(multicastRequest)) < 0)
{
throw CSocketException("Multicast group join failed (setsockopt())", true);
}
}
void UDPSocket::LeaveGroup( const string &multicastGroup )
{
struct ip_mreq multicastRequest;
multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str());
multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(m_sockDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP,
(void *) &multicastRequest,
sizeof(multicastRequest)) < 0)
{
throw CSocketException("Multicast group leave failed (setsockopt())", true);
}
}
void UDPSocket::SetBroadcast()
{
// If this fails, we'll hear about it when we try to send. This will allow
// system that cannot broadcast to continue if they don't plan to broadcast
int broadcastPermission = 1;
setsockopt(m_sockDesc, SOL_SOCKET, SO_BROADCAST,
(void *) &broadcastPermission, sizeof(broadcastPermission));
}
|