aboutsummaryrefslogtreecommitdiffstats
path: root/src/UdpSocket.cpp
blob: 6d2728bb791b2ff54075463e8f3ac5b245bbd6ce (plain)
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Her Majesty the
   Queen in Right of Canada (Communications Research Center Canada)
   */
/*
   This file is part of ODR-DabMux.

   ODR-DabMux 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.

   ODR-DabMux 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 ODR-DabMux.  If not, see <http://www.gnu.org/licenses/>.
   */

#include "UdpSocket.h"

#include <iostream>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>

#ifdef TRACE_ON
# ifndef TRACE_CLASS
#  define TRACE_CLASS(class, func) cout <<"-" <<(class) <<"\t(" <<this <<")::" <<(func) <<endl
#  define TRACE_STATIC(class, func) cout <<"-" <<(class) <<"\t(static)::" <<(func) <<endl
# endif
#else
# ifndef TRACE_CLASS
#  define TRACE_CLASS(class, func)
#  define TRACE_STATIC(class, func)
# endif
#endif


/// Must be call once before doing any operation on sockets
int UdpSocket::init()
{
#ifdef _WIN32
  WSADATA wsaData;
  WORD wVersionRequested = wVersionRequested = MAKEWORD( 2, 2 );
  
  int res = WSAStartup( wVersionRequested, &wsaData );
  if (res) {
    setInetError("Can't initialize winsock");
    return -1;
  }
#endif
  return 0;
}


/// Must be call once before leaving application
int UdpSocket::clean()
{
#ifdef _WIN32
  int res = WSACleanup();
  if (res) {
    setInetError("Can't initialize winsock");
    return -1;
  }
#endif
  return 0;
}


/**
 *  Two step constructor. Create must be called prior to use this
 *  socket.
 */
UdpSocket::UdpSocket() :
  listenSocket(INVALID_SOCKET)
{
  TRACE_CLASS("UdpSocket", "UdpSocket()");
}


/**
 *  One step constructor.
 *  @param port The port number on which the socket will be bind
 *  @param name The IP address on which the socket will be bind.
 *              It is used to bind the socket on a specific interface if
 *              the computer have many NICs.
 */
UdpSocket::UdpSocket(int port, char *name) :
  listenSocket(INVALID_SOCKET)
{
  TRACE_CLASS("UdpSocket", "UdpSocket(int, char*)");
  create(port, name);
}


/**
 *  This functin set blocking mode. The socket can be blocking or not,
 *  depending of the parametre. By default, the socket is blocking.
 *  @param block If true, set the socket blocking, otherwise set non-blocking
 *  @return 0  if ok
 *          -1 if error
 */
int UdpSocket::setBlocking(bool block)
{
#ifdef _WIN32
	unsigned long res = block ? 0 : 1;
	if (ioctlsocket(listenSocket, FIONBIO, &res) != 0) {
        setInetError("Can't change blocking state of socket");
        return -1;
	}
    return 0;
#else
  int res;
  if (block)
    res = fcntl(listenSocket, F_SETFL, 0);
  else
    res = fcntl(listenSocket, F_SETFL, O_NONBLOCK);
  if (res == SOCKET_ERROR) {
    setInetError("Can't change blocking state of socket");
    return -1;
  }
  return 0;
#endif
}


/**
 *  Two step initializer. This function must be called after the constructor
 *  without argument as been called.
 *  @param port The port number on which the socket will be bind
 *  @param name The IP address on which the socket will be bind.
 *              It is used to bind the socket on a specific interface if
 *              the computer have many NICs.
 *  @return 0  if ok
 *          -1 if error
 */
int UdpSocket::create(int port, char *name)
{
  TRACE_CLASS("UdpSocket", "create(int, char*)");
  if (listenSocket != INVALID_SOCKET)
    closesocket(listenSocket);
  address.setAddress(name);
  address.setPort(port);
  if ((listenSocket = socket(PF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
    setInetError("Can't create socket");
    return -1;
  }
  reuseopt_t reuse = 1;
  if (setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))
      == SOCKET_ERROR) {
    setInetError("Can't reuse address");
    return -1;
  }
  if (bind(listenSocket, address.getAddress(), sizeof(sockaddr_in)) == SOCKET_ERROR) {
    setInetError("Can't bind socket");
    closesocket(listenSocket);
    listenSocket = INVALID_SOCKET;
    return -1;
  }
  return 0;
}


/// Destructor
UdpSocket::~UdpSocket() {
  TRACE_CLASS("UdpSocket", "~UdpSocket()");
  if (listenSocket != INVALID_SOCKET)
    closesocket(listenSocket);
}


/**
 *  Receive an UDP packet.
 *  @param packet The packet that will receive data. The address will be set
 *                to the source address.
 *  @return 0 if ok, -1 if error
 */
int UdpSocket::receive(UdpPacket &packet)
{
  TRACE_CLASS("UdpSocket", "receive(UdpPacket)");
  socklen_t addrSize;
  addrSize = sizeof(*packet.getAddress().getAddress());
  int ret = recvfrom(listenSocket, packet.getData(), packet.getSize() - packet.getOffset(), 0,
		     packet.getAddress().getAddress(), &addrSize);
  if (ret == SOCKET_ERROR) {
    packet.setLength(0);
#ifndef _WIN32
  if (errno == EAGAIN)
    return 0;
#endif
    setInetError("Can't receive UDP packet");
    return -1;
  }
  packet.setLength(ret);
  if (ret == (long)packet.getSize()) {
    packet.setSize(packet.getSize() << 1);
  }
  return 0;
}


/**
 *  Send an UDP packet.
 *  @param packet The UDP packet to be sent. It includes the data and the
 *                destination address
 *  return 0 if ok, -1 if error
 */
int UdpSocket::send(UdpPacket &packet)
{
#ifdef DUMP
  TRACE_CLASS("UdpSocket", "send(UdpPacket)");
#endif
  int ret = sendto(listenSocket, packet.getData(), packet.getLength(), 0,
		   packet.getAddress().getAddress(), sizeof(*packet.getAddress().getAddress()));
  if (ret == SOCKET_ERROR
#ifndef _WIN32
      && errno != ECONNREFUSED
#endif
      ) {
    setInetError("Can't send UDP packet");
    return -1;
  }
  return 0;
}


/**
 *  Must be called to receive data on a multicast address.
 *  @param groupname The multica
st address to join.
 *  @return 0 if ok, -1 if error
 */
int UdpSocket::joinGroup(char* groupname)
{
  TRACE_CLASS("UdpSocket", "joinGroup(char*)");
#ifdef _WIN32
  ip_mreq group;
#else
  ip_mreqn group;
#endif
  if ((group.imr_multiaddr.s_addr = inet_addr(groupname)) == INADDR_NONE) {
    setInetError(groupname);
    return -1;
  }
  if (!IN_MULTICAST(ntohl(group.imr_multiaddr.s_addr))) {
    setInetError("Not a multicast address");
    return -1;
  }
#ifdef _WIN32
  group.imr_interface.s_addr = 0;
  if (setsockopt(listenSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&group, sizeof(group))
      == SOCKET_ERROR) {
    setInetError("Can't join multicast group");
    return -1;
  }
#else
  group.imr_address.s_addr = htons(INADDR_ANY);;
  group.imr_ifindex = 0;
  if (setsockopt(listenSocket, SOL_IP, IP_ADD_MEMBERSHIP, &group, sizeof(group))
      == SOCKET_ERROR) {
    setInetError("Can't join multicast group");
  }
#endif
  return 0;
}

  
/**
 *  Constructs an UDP packet.
 *  @param initSize The initial size of the data buffer
 */
UdpPacket::UdpPacket(unsigned int initSize) :
  dataBuf(new char[initSize]),
  length(0),
  size(initSize),
  offset(0)
{
  TRACE_CLASS("UdpPacket", "UdpPacket(unsigned int)");
  if (dataBuf == NULL)
    size = 0;
}


/// Destructor
UdpPacket::~UdpPacket()
{
  TRACE_CLASS("UdpPacket", "~UdpPacket()");
  if (dataBuf != NULL) {
    delete []dataBuf;
    dataBuf = NULL;
  }
}
  

/**
 *  Changes size of the data buffer size. \a Length + \a offset data will be copied
 *  in the new buffer.
 *  @warning The pointer to data will be changed
 *  @param newSize The new data buffer size
 */
void UdpPacket::setSize(unsigned newSize)
{
  TRACE_CLASS("UdpPacket", "setSize(unsigned)");
  char *tmp = new char[newSize];
  if (length > newSize)
    length = newSize;
  if (tmp) {
    memcpy(tmp, dataBuf, length);
    delete []dataBuf;
    dataBuf = tmp;
    size = newSize;
  }
}


/**
 *  Give the pointer to data. It is ajusted with the \a offset.
 *  @warning This pointer change. when the \a size of the buffer and the \a offset change.
 *  @return The pointer
 */
char *UdpPacket::getData()
{
  return dataBuf + offset;
}


/**
 *  Add some data at the end of data buffer and adjust size.
 *  @param data Pointer to the data to add
 *  @param size Size in bytes of new data
 */
void UdpPacket::addData(void *data, unsigned size)
{
  if (length + size > this->size) {
    setSize(this->size << 1);
  }
  memcpy(dataBuf + length, data, size);
  length += size;
}


/**
 *  Returns the length of useful data. Data before the \a offset are ignored.
 *  @return The data length
 */
unsigned long UdpPacket::getLength()
{
  return length - offset;
}


/**
 *  Returns the size of the data buffer.
 *  @return The data buffer size
 */
unsigned long UdpPacket::getSize()
{
  return size;
}


/**
 *  Returns the offset value.
 *  @return The offset value
 */
unsigned long UdpPacket::getOffset()
{
  return offset;
}


/**
 *  Sets the data length value. Data before the \a offset are ignored.
 *  @param len The new length of data
 */
void UdpPacket::setLength(unsigned long len)
{
  length = len + offset;
}


/**
 *  Sets the data offset. Data length is ajusted to ignore data before the \a offset.
 *  @param val The new data offset.
 */
void UdpPacket::setOffset(unsigned long val)
{
  offset = val;
  if (offset > length)
    length = offset;
}


/**
 *  Returns the UDP address of the data.
 *  @return The UDP address
 */
InetAddress &UdpPacket::getAddress()
{
  return address;
}

/*
WSAEINTR
WSAEBADF
WSAEACCES
WSAEFAULT
WSAEINVAL
WSAEMFILE
WSAEWOULDBLOCK
WSAEINPROGRESS
WSAEALREADY
WSAENOTSOCK
WSAEDESTADDRREQ
WSAEMSGSIZE
WSAEPROTOTYPE
WSAENOPROTOOPT
WSAEPROTONOSUPPORT
WSAESOCKTNOSUPPORT
WSAEOPNOTSUPP
WSAEPFNOSUPPORT
WSAEAFNOSUPPORT
WSAEADDRINUSE
WSAEADDRNOTAVAIL
WSAENETDOWN
WSAENETUNREACH
WSAENETRESET
WSAECONNABORTED
WSAECONNRESET
WSAENOBUFS
WSAEISCONN
WSAENOTCONN
WSAESHUTDOWN
WSAETOOMANYREFS
WSAETIMEDOUT
WSAECONNREFUSED
WSAELOOP
WSAENAMETOOLONG
WSAEHOSTDOWN
WSAEHOSTUNREACH
WSAENOTEMPTY
WSAEPROCLIM
WSAEUSERS
WSAEDQUOT
WSAESTALE
WSAEREMOTE
WSAEDISCON
WSASYSNOTREADY
WSAVERNOTSUPPORTED
WSANOTINITIALISED
*/