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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
|
/**
* @file net.cpp
* @brief Cross-platform routines for sending and receiving packets.
*
* $LicenseInfo:firstyear=2000&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "net.h"
// system library includes
#include <stdexcept>
#if LL_WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <errno.h>
#endif
// linden library includes
#include "llerror.h"
#include "llhost.h"
#include "lltimer.h"
#include "indra_constants.h"
#include "llsocks5.h"
// Globals
#if LL_WINDOWS
SOCKADDR_IN stDstAddr;
SOCKADDR_IN stSrcAddr;
SOCKADDR_IN stLclAddr;
static WSADATA stWSAData;
#else
struct sockaddr_in stDstAddr;
struct sockaddr_in stSrcAddr;
struct sockaddr_in stLclAddr;
#if LL_DARWIN
#ifndef _SOCKLEN_T
#define _SOCKLEN_T
typedef int socklen_t;
#endif
#endif
#endif
static U32 gsnReceivingIFAddr = INVALID_HOST_IP_ADDRESS; // Address to which datagram was sent
const char* LOOPBACK_ADDRESS_STRING = "127.0.0.1";
const char* BROADCAST_ADDRESS_STRING = "255.255.255.255";
#if LL_DARWIN
// Mac OS X returns an error when trying to set these to 400000. Smaller values succeed.
const int SEND_BUFFER_SIZE = 200000;
const int RECEIVE_BUFFER_SIZE = 200000;
#else // LL_DARWIN
const int SEND_BUFFER_SIZE = 400000;
const int RECEIVE_BUFFER_SIZE = 400000;
#endif // LL_DARWIN
// universal functions (cross-platform)
LLHost get_sender()
{
return LLHost(stSrcAddr.sin_addr.s_addr, ntohs(stSrcAddr.sin_port));
}
U32 get_sender_ip(void)
{
return stSrcAddr.sin_addr.s_addr;
}
U32 get_sender_port()
{
return ntohs(stSrcAddr.sin_port);
}
LLHost get_receiving_interface()
{
return LLHost(gsnReceivingIFAddr, INVALID_PORT);
}
U32 get_receiving_interface_ip(void)
{
return gsnReceivingIFAddr;
}
const char* u32_to_ip_string(U32 ip)
{
static char buffer[MAXADDRSTR]; /* Flawfinder: ignore */
// Convert the IP address into a string
in_addr in;
in.s_addr = ip;
char* result = inet_ntoa(in);
// NULL indicates error in conversion
if (result != NULL)
{
strncpy( buffer, result, MAXADDRSTR ); /* Flawfinder: ignore */
buffer[MAXADDRSTR-1] = '\0';
return buffer;
}
else
{
return "(bad IP addr)";
}
}
// Returns ip_string if successful, NULL if not. Copies into ip_string
char *u32_to_ip_string(U32 ip, char *ip_string)
{
char *result;
in_addr in;
// Convert the IP address into a string
in.s_addr = ip;
result = inet_ntoa(in);
// NULL indicates error in conversion
if (result != NULL)
{
//the function signature needs to change to pass in the lengfth of first and last.
strcpy(ip_string, result); /*Flawfinder: ignore*/
return ip_string;
}
else
{
return NULL;
}
}
// Wrapper for inet_addr()
U32 ip_string_to_u32(const char* ip_string)
{
// *NOTE: Windows doesn't support inet_aton(), so we are using
// inet_addr(). Unfortunately, INADDR_NONE == INADDR_BROADCAST, so
// we have to check whether the input is a broadcast address before
// deciding that @ip_string is invalid.
//
// Also, our definition of INVALID_HOST_IP_ADDRESS doesn't allow us to
// use wildcard addresses. -Ambroff
U32 ip = inet_addr(ip_string);
if (ip == INADDR_NONE
&& strncmp(ip_string, BROADCAST_ADDRESS_STRING, MAXADDRSTR) != 0)
{
llwarns << "ip_string_to_u32() failed, Error: Invalid IP string '" << ip_string << "'" << llendl;
return INVALID_HOST_IP_ADDRESS;
}
return ip;
}
//////////////////////////////////////////////////////////////////////////////////////////
// Windows Versions
//////////////////////////////////////////////////////////////////////////////////////////
#if LL_WINDOWS
int tcp_handshake(S32 handle, char * dataout, int outlen, char * datain, int maxinlen)
{
int result;
result = send(handle, dataout, outlen, 0);
if (result != outlen)
{
S32 err = WSAGetLastError();
llwarns << "Error sending data to proxy control channel, number of bytes sent were " << result << " error code was " << err << llendl;
return -1;
}
result = recv(handle, datain, maxinlen, 0);
if (result != maxinlen)
{
S32 err = WSAGetLastError();
llwarns << "Error receiving data from proxy control channel, number of bytes received were " << result << " error code was " << err << llendl;
return -1;
}
return 0;
}
S32 tcp_open_channel(LLHost host)
{
// Open a TCP channel
// Jump through some hoops to ensure that if the request hosts is down
// or not reachable connect() does not block
S32 handle;
handle = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == handle)
{
llwarns << "Error opening TCP control socket, socket() returned "
<< WSAGetLastError() << ", " << DecodeError(WSAGetLastError()) << llendl;
return -1;
}
struct sockaddr_in address;
address.sin_port = htons(host.getPort());
address.sin_family = AF_INET;
address.sin_addr.s_addr = host.getAddress();
// Non blocking
WSAEVENT hEvent = WSACreateEvent();
WSAEventSelect(handle, hEvent, FD_CONNECT) ;
connect(handle, (struct sockaddr*)&address, sizeof(address)) ;
// Wait for 5 seconds, if we can't get a TCP channel open in this
// time frame then there is something badly wrong.
WaitForSingleObject(hEvent, 1000 * 5); // 5 seconds time out
WSANETWORKEVENTS netevents;
WSAEnumNetworkEvents(handle, hEvent, &netevents);
// Check the async event status to see if we connected
if ((netevents.lNetworkEvents & FD_CONNECT) == FD_CONNECT)
{
if (netevents.iErrorCode[FD_CONNECT_BIT] != 0)
{
llwarns << "Unable to open TCP channel, WSA returned an error code of " << netevents.iErrorCode[FD_CONNECT_BIT] << llendl;
WSACloseEvent(hEvent);
tcp_close_channel(handle);
return -1;
}
// Now we are connected disable non blocking
// we don't need support an async interface as
// currently our only consumer (socks5) will make one round
// of packets then just hold the connection open
WSAEventSelect(handle, hEvent, NULL) ;
unsigned long NonBlock = 0;
ioctlsocket(handle, FIONBIO, &NonBlock);
return handle;
}
llwarns << "Unable to open TCP channel, Timeout is the host up?" << netevents.iErrorCode[FD_CONNECT_BIT] << llendl;
tcp_close_channel(handle);
return -1;
}
void tcp_close_channel(S32 handle)
{
llinfos << "Closing TCP channel" << llendl;
shutdown(handle, SD_BOTH);
closesocket(handle);
}
S32 start_net(S32& socket_out, int& nPort)
{
// Create socket, make non-blocking
// Init WinSock
int nRet;
int hSocket;
int snd_size = SEND_BUFFER_SIZE;
int rec_size = RECEIVE_BUFFER_SIZE;
int buff_size = 4;
// Initialize windows specific stuff
if (WSAStartup(0x0202, &stWSAData))
{
S32 err = WSAGetLastError();
WSACleanup();
LL_WARNS("AppInit") << "Windows Sockets initialization failed, err " << err << LL_ENDL;
return 1;
}
// Get a datagram socket
hSocket = (int)socket(AF_INET, SOCK_DGRAM, 0);
if (hSocket == INVALID_SOCKET)
{
S32 err = WSAGetLastError();
WSACleanup();
LL_WARNS("AppInit") << "socket() failed, err " << err << LL_ENDL;
return 2;
}
// Name the socket (assign the local port number to receive on)
stLclAddr.sin_family = AF_INET;
stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY);
stLclAddr.sin_port = htons(nPort);
S32 attempt_port = nPort;
LL_DEBUGS("AppInit") << "attempting to connect on port " << attempt_port << LL_ENDL;
nRet = bind(hSocket, (struct sockaddr*) &stLclAddr, sizeof(stLclAddr));
if (nRet == SOCKET_ERROR)
{
// If we got an address in use error...
if (WSAGetLastError() == WSAEADDRINUSE)
{
// Try all ports from PORT_DISCOVERY_RANGE_MIN to PORT_DISCOVERY_RANGE_MAX
for(attempt_port = PORT_DISCOVERY_RANGE_MIN;
attempt_port <= PORT_DISCOVERY_RANGE_MAX;
attempt_port++)
{
stLclAddr.sin_port = htons(attempt_port);
LL_DEBUGS("AppInit") << "trying port " << attempt_port << LL_ENDL;
nRet = bind(hSocket, (struct sockaddr*) &stLclAddr, sizeof(stLclAddr));
if (!(nRet == SOCKET_ERROR &&
WSAGetLastError() == WSAEADDRINUSE))
{
break;
}
}
if (nRet == SOCKET_ERROR)
{
LL_WARNS("AppInit") << "startNet() : Couldn't find available network port." << LL_ENDL;
// Fail gracefully here in release
return 3;
}
}
else
// Some other socket error
{
LL_WARNS("AppInit") << llformat("bind() port: %d failed, Err: %d\n", nPort, WSAGetLastError()) << LL_ENDL;
// Fail gracefully in release.
return 4;
}
}
sockaddr_in socket_address;
S32 socket_address_size = sizeof(socket_address);
getsockname(hSocket, (SOCKADDR*) &socket_address, &socket_address_size);
attempt_port = ntohs(socket_address.sin_port);
LL_INFOS("AppInit") << "connected on port " << attempt_port << LL_ENDL;
nPort = attempt_port;
// Set socket to be non-blocking
unsigned long argp = 1;
nRet = ioctlsocket (hSocket, FIONBIO, &argp);
if (nRet == SOCKET_ERROR)
{
printf("Failed to set socket non-blocking, Err: %d\n",
WSAGetLastError());
}
// set a large receive buffer
nRet = setsockopt(hSocket, SOL_SOCKET, SO_RCVBUF, (char *)&rec_size, buff_size);
if (nRet)
{
LL_INFOS("AppInit") << "Can't set receive buffer size!" << LL_ENDL;
}
nRet = setsockopt(hSocket, SOL_SOCKET, SO_SNDBUF, (char *)&snd_size, buff_size);
if (nRet)
{
LL_INFOS("AppInit") << "Can't set send buffer size!" << LL_ENDL;
}
getsockopt(hSocket, SOL_SOCKET, SO_RCVBUF, (char *)&rec_size, &buff_size);
getsockopt(hSocket, SOL_SOCKET, SO_SNDBUF, (char *)&snd_size, &buff_size);
LL_DEBUGS("AppInit") << "startNet - receive buffer size : " << rec_size << LL_ENDL;
LL_DEBUGS("AppInit") << "startNet - send buffer size : " << snd_size << LL_ENDL;
// Setup a destination address
stDstAddr.sin_family = AF_INET;
stDstAddr.sin_addr.s_addr = INVALID_HOST_IP_ADDRESS;
stDstAddr.sin_port = htons(nPort);
socket_out = hSocket;
return 0;
}
void end_net(S32& socket_out)
{
if (socket_out >= 0)
{
shutdown(socket_out, SD_BOTH);
closesocket(socket_out);
}
WSACleanup();
}
S32 receive_packet(int hSocket, char * receiveBuffer)
{
// Receives data asynchronously from the socket set by initNet().
// Returns the number of bytes received into dataReceived, or zero
// if there is no data received.
int nRet;
int addr_size = sizeof(struct sockaddr_in);
nRet = recvfrom(hSocket, receiveBuffer, NET_BUFFER_SIZE, 0, (struct sockaddr*)&stSrcAddr, &addr_size);
if (nRet == SOCKET_ERROR )
{
if (WSAEWOULDBLOCK == WSAGetLastError())
return 0;
if (WSAECONNRESET == WSAGetLastError())
return 0;
llinfos << "receivePacket() failed, Error: " << WSAGetLastError() << llendl;
}
return nRet;
}
// Returns TRUE on success.
BOOL send_packet(int hSocket, const char *sendBuffer, int size, U32 recipient, int nPort)
{
// Sends a packet to the address set in initNet
//
int nRet = 0;
U32 last_error = 0;
stDstAddr.sin_addr.s_addr = recipient;
stDstAddr.sin_port = htons(nPort);
do
{
nRet = sendto(hSocket, sendBuffer, size, 0, (struct sockaddr*)&stDstAddr, sizeof(stDstAddr));
if (nRet == SOCKET_ERROR )
{
last_error = WSAGetLastError();
if (last_error != WSAEWOULDBLOCK)
{
// WSAECONNRESET - I think this is caused by an ICMP "connection refused"
// message being sent back from a Linux box... I'm not finding helpful
// documentation or web pages on this. The question is whether the packet
// actually got sent or not. Based on the structure of this code, I would
// assume it is. JNC 2002.01.18
if (WSAECONNRESET == WSAGetLastError())
{
return TRUE;
}
llinfos << "sendto() failed to " << u32_to_ip_string(recipient) << ":" << nPort
<< ", Error " << last_error << llendl;
}
}
} while ( (nRet == SOCKET_ERROR)
&&(last_error == WSAEWOULDBLOCK));
return (nRet != SOCKET_ERROR);
}
//////////////////////////////////////////////////////////////////////////////////////////
// Linux Versions
//////////////////////////////////////////////////////////////////////////////////////////
#else
int tcp_handshake(S32 handle, char * dataout, int outlen, char * datain, int maxinlen)
{
if (send(handle, dataout, outlen, 0) != outlen)
{
llwarns << "Error sending data to proxy control channel" << llendl;
return -1;
}
if (recv(handle, datain, maxinlen, 0) != maxinlen)
{
llwarns << "Error receiving data to proxy control channel" << llendl;
return -1;
}
return 0;
}
S32 tcp_open_channel(LLHost host)
{
S32 handle;
handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (-1 == handle)
{
llwarns << "Error opening TCP control socket, socket() returned " << handle << "error code: " << errno << llendl;
return -1;
}
struct sockaddr_in address;
address.sin_port = htons(host.getPort());
address.sin_family = AF_INET;
address.sin_addr.s_addr = host.getAddress();
// Set the socket to non blocking for the connect()
int flags = fcntl(handle, F_GETFL, 0);
fcntl(handle, F_SETFL, flags | O_NONBLOCK);
S32 error = connect(handle, (sockaddr*)&address, sizeof(address));
if (error && (errno != EINPROGRESS))
{
llwarns << "Unable to open TCP channel, error code: " << errno << llendl;
tcp_close_channel(handle);
return -1;
}
struct timeval timeout;
timeout.tv_sec = 5; // Maximum time to wait for the connect() to complete
timeout.tv_usec = 0;
fd_set fds;
FD_ZERO(&fds);
FD_SET(handle, &fds);
// See if we have connected or time out after 5 seconds
S32 rc = select(sizeof(fds)*8, NULL, &fds, NULL, &timeout);
if (rc != 1) // we require exactly one descriptor to be set
{
llwarns << "Unable to open TCP channel" << llendl;
tcp_close_channel(handle);
return -1;
}
// Return the socket to blocking operations
fcntl(handle, F_SETFL, flags);
return handle;
}
void tcp_close_channel(S32 handle)
{
close(handle);
}
// Create socket, make non-blocking
S32 start_net(S32& socket_out, int& nPort)
{
int hSocket, nRet;
int snd_size = SEND_BUFFER_SIZE;
int rec_size = RECEIVE_BUFFER_SIZE;
socklen_t buff_size = 4;
// Create socket
hSocket = socket(AF_INET, SOCK_DGRAM, 0);
if (hSocket < 0)
{
llwarns << "socket() failed" << llendl;
return 1;
}
if (NET_USE_OS_ASSIGNED_PORT == nPort)
{
// Although bind is not required it will tell us which port we were
// assigned to.
stLclAddr.sin_family = AF_INET;
stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY);
stLclAddr.sin_port = htons(0);
llinfos << "attempting to connect on OS assigned port" << llendl;
nRet = bind(hSocket, (struct sockaddr*) &stLclAddr, sizeof(stLclAddr));
if (nRet < 0)
{
llwarns << "Failed to bind on an OS assigned port error: "
<< nRet << llendl;
}
else
{
sockaddr_in socket_info;
socklen_t len = sizeof(sockaddr_in);
int err = getsockname(hSocket, (sockaddr*)&socket_info, &len);
llinfos << "Get socket returned: " << err << " length " << len << llendl;
nPort = ntohs(socket_info.sin_port);
llinfos << "Assigned port: " << nPort << llendl;
}
}
else
{
// Name the socket (assign the local port number to receive on)
stLclAddr.sin_family = AF_INET;
stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY);
stLclAddr.sin_port = htons(nPort);
U32 attempt_port = nPort;
llinfos << "attempting to connect on port " << attempt_port << llendl;
nRet = bind(hSocket, (struct sockaddr*) &stLclAddr, sizeof(stLclAddr));
if (nRet < 0)
{
// If we got an address in use error...
if (errno == EADDRINUSE)
{
// Try all ports from PORT_DISCOVERY_RANGE_MIN to PORT_DISCOVERY_RANGE_MAX
for(attempt_port = PORT_DISCOVERY_RANGE_MIN;
attempt_port <= PORT_DISCOVERY_RANGE_MAX;
attempt_port++)
{
stLclAddr.sin_port = htons(attempt_port);
llinfos << "trying port " << attempt_port << llendl;
nRet = bind(hSocket, (struct sockaddr*) &stLclAddr, sizeof(stLclAddr));
if (!((nRet < 0) && (errno == EADDRINUSE)))
{
break;
}
}
if (nRet < 0)
{
llwarns << "startNet() : Couldn't find available network port." << llendl;
// Fail gracefully in release.
return 3;
}
}
// Some other socket error
else
{
llwarns << llformat ("bind() port: %d failed, Err: %s\n", nPort, strerror(errno)) << llendl;
// Fail gracefully in release.
return 4;
}
}
llinfos << "connected on port " << attempt_port << llendl;
nPort = attempt_port;
}
// Set socket to be non-blocking
fcntl(hSocket, F_SETFL, O_NONBLOCK);
// set a large receive buffer
nRet = setsockopt(hSocket, SOL_SOCKET, SO_RCVBUF, (char *)&rec_size, buff_size);
if (nRet)
{
llinfos << "Can't set receive size!" << llendl;
}
nRet = setsockopt(hSocket, SOL_SOCKET, SO_SNDBUF, (char *)&snd_size, buff_size);
if (nRet)
{
llinfos << "Can't set send size!" << llendl;
}
getsockopt(hSocket, SOL_SOCKET, SO_RCVBUF, (char *)&rec_size, &buff_size);
getsockopt(hSocket, SOL_SOCKET, SO_SNDBUF, (char *)&snd_size, &buff_size);
llinfos << "startNet - receive buffer size : " << rec_size << llendl;
llinfos << "startNet - send buffer size : " << snd_size << llendl;
#if LL_LINUX
// Turn on recipient address tracking
{
int use_pktinfo = 1;
if( setsockopt( hSocket, SOL_IP, IP_PKTINFO, &use_pktinfo, sizeof(use_pktinfo) ) == -1 )
{
llwarns << "No IP_PKTINFO available" << llendl;
}
else
{
llinfos << "IP_PKKTINFO enabled" << llendl;
}
}
#endif
// Setup a destination address
char achMCAddr[MAXADDRSTR] = "127.0.0.1"; /* Flawfinder: ignore */
stDstAddr.sin_family = AF_INET;
stDstAddr.sin_addr.s_addr = ip_string_to_u32(achMCAddr);
stDstAddr.sin_port = htons(nPort);
socket_out = hSocket;
return 0;
}
void end_net(S32& socket_out)
{
if (socket_out >= 0)
{
close(socket_out);
}
}
#if LL_LINUX
static int recvfrom_destip( int socket, void *buf, int len, struct sockaddr *from, socklen_t *fromlen, U32 *dstip )
{
int size;
struct iovec iov[1];
char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo))];
struct cmsghdr *cmsgptr;
struct msghdr msg = {0};
iov[0].iov_base = buf;
iov[0].iov_len = len;
memset(&msg, 0, sizeof msg);
msg.msg_name = from;
msg.msg_namelen = *fromlen;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_control = &cmsg;
msg.msg_controllen = sizeof(cmsg);
size = recvmsg(socket, &msg, 0);
if (size == -1)
{
return -1;
}
for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR( &msg, cmsgptr))
{
if( cmsgptr->cmsg_level == SOL_IP && cmsgptr->cmsg_type == IP_PKTINFO )
{
in_pktinfo *pktinfo = (in_pktinfo *)CMSG_DATA(cmsgptr);
if( pktinfo )
{
// Two choices. routed and specified. ipi_addr is routed, ipi_spec_dst is
// routed. We should stay with specified until we go to multiple
// interfaces
*dstip = pktinfo->ipi_spec_dst.s_addr;
}
}
}
return size;
}
#endif
int receive_packet(int hSocket, char * receiveBuffer)
{
// Receives data asynchronously from the socket set by initNet().
// Returns the number of bytes received into dataReceived, or zero
// if there is no data received.
// or -1 if an error occured!
int nRet;
socklen_t addr_size = sizeof(struct sockaddr_in);
gsnReceivingIFAddr = INVALID_HOST_IP_ADDRESS;
#if LL_LINUX
nRet = recvfrom_destip(hSocket, receiveBuffer, NET_BUFFER_SIZE, (struct sockaddr*)&stSrcAddr, &addr_size, &gsnReceivingIFAddr);
#else
int recv_flags = 0;
nRet = recvfrom(hSocket, receiveBuffer, NET_BUFFER_SIZE, recv_flags, (struct sockaddr*)&stSrcAddr, &addr_size);
#endif
if (nRet == -1)
{
// To maintain consistency with the Windows implementation, return a zero for size on error.
return 0;
}
// Uncomment for testing if/when implementing for Mac or Windows:
// llinfos << "Received datagram to in addr " << u32_to_ip_string(get_receiving_interface_ip()) << llendl;
return nRet;
}
BOOL send_packet(int hSocket, const char * sendBuffer, int size, U32 recipient, int nPort)
{
int ret;
BOOL success;
BOOL resend;
S32 send_attempts = 0;
stDstAddr.sin_addr.s_addr = recipient;
stDstAddr.sin_port = htons(nPort);
do
{
ret = sendto(hSocket, sendBuffer, size, 0, (struct sockaddr*)&stDstAddr, sizeof(stDstAddr));
send_attempts++;
if (ret >= 0)
{
// successful send
success = TRUE;
resend = FALSE;
}
else
{
// send failed, check to see if we should resend
success = FALSE;
if (errno == EAGAIN)
{
// say nothing, just repeat send
llinfos << "sendto() reported buffer full, resending (attempt " << send_attempts << ")" << llendl;
llinfos << inet_ntoa(stDstAddr.sin_addr) << ":" << nPort << llendl;
resend = TRUE;
}
else if (errno == ECONNREFUSED)
{
// response to ICMP connection refused message on earlier send
llinfos << "sendto() reported connection refused, resending (attempt " << send_attempts << ")" << llendl;
llinfos << inet_ntoa(stDstAddr.sin_addr) << ":" << nPort << llendl;
resend = TRUE;
}
else
{
// some other error
llinfos << "sendto() failed: " << errno << ", " << strerror(errno) << llendl;
llinfos << inet_ntoa(stDstAddr.sin_addr) << ":" << nPort << llendl;
resend = FALSE;
}
}
}
while (resend && send_attempts < 3);
if (send_attempts >= 3)
{
llinfos << "sendPacket() bailed out of send!" << llendl;
return FALSE;
}
return success;
}
#endif
//EOF
|