-
Notifications
You must be signed in to change notification settings - Fork 228
/
DelegatingDatagramSocket.java
948 lines (892 loc) · 32.7 KB
/
DelegatingDatagramSocket.java
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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
/*
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
*
* Copyright @ 2015 Atlassian Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ice4j.socket;
import java.io.*;
import java.net.*;
import java.nio.channels.*;
import org.ice4j.stack.*;
/**
* Implements a <tt>DatagramSocket</tt> which delegates its calls to a specific
* <tt>DatagramSocket</tt>.
*
* @author Lyubomir Marinov
*/
public class DelegatingDatagramSocket
extends DatagramSocket
{
/**
* Assigns a factory to generate custom DatagramSocket to replace classical
* "java" DatagramSocket. This way external applications can define the
* type of DatagramSocket to use.
* If "null", then the classical "java" DatagramSocket is used. Otherwise
* the factory generates custom DatagramSockets.
*/
private static DatagramSocketFactory delegateFactory = null;
/**
* The size to which to set the receive buffer size. This value must set by
* using the DelegatingDatagramSocket.setDefaultReceiveBufferSize(int)
* function before calling the DelegatingDatagramSocket constructor and must
* be greater than 0 to be effective.
*/
private static int defaultReceiveBufferSize = -1;
/**
* If a factory is provided, then any new instances of
* DelegatingDatagramSocket will automatically use a socket from the
* factory instead of the super class.
*
* If this method is used, the factory class needs to ensure the socket
* objects are properly constructed and initialized - in particular, any
* default receive buffer size specified through
* DelegatingDatagramSocket#setDefaultReceiveBufferSize() won't
* automatically be applied to sockets obtain from the factory.
*
* @param factory The factory assigned to generates the new DatagramSocket
* for each new DelegatingDatagramSocket which do not use a delegate socket.
*/
public static void setDefaultDelegateFactory(DatagramSocketFactory factory)
{
delegateFactory = factory;
}
/**
* Specifies a default receive buffer size for the underlying sockets.
* This function must be called before using the DelegatingDatagramSocket
* constructor. The size must be greater than 0 to be effective.
*
* @see DatagramSocket#setReceiveBufferSize(int) for a discussion of the
* effect of changing this setting.
*
* @param size the size to which to set the receive buffer size. This value
* must be greater than 0.
*/
public static void setDefaultReceiveBufferSize(int size)
{
defaultReceiveBufferSize = size;
}
/**
* Determines whether a packet should be logged, given the number of sent
* or received packets.
*
* @param numOfPacket the number of packets sent or received.
*/
static boolean logNonStun(long numOfPacket)
{
return (numOfPacket == 1)
|| (numOfPacket == 300)
|| (numOfPacket == 500)
|| (numOfPacket == 1000)
|| ((numOfPacket % 5000) == 0);
}
/**
* The <tt>DatagramSocket</tt> to which this
* <tt>DelegatingDatagramSocket</tt> delegates its calls.
*/
protected final DatagramSocket delegate;
/**
* The number of non-STUN packets received on this socket.
*/
private long nbReceivedPackets = 0;
/**
* The number of non-STUN packets sent through this socket.
*/
private long nbSentPackets = 0;
/**
* Whether this socket has been closed.
*/
private boolean closed = false;
/**
* Initializes a new <tt>DelegatingDatagramSocket</tt> instance and binds it
* to any available port on the local host machine. The socket will be
* bound to the wildcard address, an IP address chosen by the kernel.
*
* @throws SocketException if the socket could not be opened, or the socket
* could not bind to the specified local port
* @see DatagramSocket#DatagramSocket()
*/
public DelegatingDatagramSocket()
throws SocketException
{
this(null, new InetSocketAddress(0));
}
/**
* Initializes a new <tt>DelegatingDatagramSocket</tt> instance which to
* implement the <tt>DatagramSocket</tt> functionality by delegating to a
* specific <tt>DatagramSocket</tt>.
*
* @param delegate the <tt>DatagramSocket</tt> to which the new instance is
* to delegate
* @throws SocketException if anything goes wrong while initializing the new
* <tt>DelegatingDatagramSocket</tt> instance
*/
public DelegatingDatagramSocket(DatagramSocket delegate)
throws SocketException
{
this(delegate, null);
}
/**
* Initializes a new <tt>DelegatingDatagramSocket</tt> instance and binds
* it to the specified port on the local host machine. The socket will be
* bound to the wildcard address, an IP address chosen by the kernel.
*
* @param port the port to bind the new socket to
* @throws SocketException if the socket could not be opened, or the socket
* could not bind to the specified local port
* @see DatagramSocket#DatagramSocket(int)
*/
public DelegatingDatagramSocket(int port)
throws SocketException
{
this(null, new InetSocketAddress(port));
}
/**
* Initializes a new <tt>DelegatingDatagramSocket</tt> instance bound to the
* specified local address. The local port must be between 0 and 65535
* inclusive. If the IP address is 0.0.0.0, the socket will be bound to the
* wildcard address, an IP address chosen by the kernel.
*
* @param port the local port to bind the new socket to
* @param laddr the local address to bind the new socket to
* @throws SocketException if the socket could not be opened, or the socket
* could not bind to the specified local port
* @see DatagramSocket#DatagramSocket(int, InetAddress)
*/
public DelegatingDatagramSocket(int port, InetAddress laddr)
throws SocketException
{
this(null, new InetSocketAddress(laddr, port));
}
/**
* Creates a datagram socket, bound to the specified local socket address.
* <p>
* If, if the address is <tt>null</tt>, creates an unbound socket.
* </p>
*
* @param bindaddr local socket address to bind, or <tt>null</tt> for an
* unbound socket
* @throws SocketException if the socket could not be opened, or the socket
* could not bind to the specified local port
*/
public DelegatingDatagramSocket(SocketAddress bindaddr)
throws SocketException
{
this(null, bindaddr);
}
/**
* Initializes a new <tt>DelegatingDatagramSocket</tt> instance which
* implements the <tt>DatagramSocket</tt> functionality. If delegate is not
* null, then the DatagramSocket functionality is delegated to a specific
* <tt>DatagramSocket</tt>.
*
* @param delegate the <tt>DatagramSocket</tt> to which the new instance is
* to delegate.
* @param address The local socket address to bind, or <tt>null</tt> for an
* unbound socket.
*
* @throws SocketException if the socket could not be opened, or the socket
* could not bind to the specified local port.
*/
public DelegatingDatagramSocket(
DatagramSocket delegate,
SocketAddress address)
throws SocketException
{
super((SocketAddress) null);
// Delegates the DatagramSocket functionality to the DatagramSocket
// given in parameter.
if(delegate != null)
{
this.delegate = delegate;
}
else
{
// Creates a custom DatagramSocket to replace classical "java"
// DatagramSocket and set it as a delegate Socket
if(delegateFactory != null)
{
this.delegate = delegateFactory.createUnboundDatagramSocket();
}
// Creates a socket directly connected to the network stack.
else
{
this.delegate = null;
initReceiveBufferSize();
}
// If not null, binds the delegate socket to the given address.
// Otherwise bind the "super" socket to this address.
bind(address);
}
}
/**
* Binds this <tt>DatagramSocket</tt> to a specific address and port.
* <p>
* If the address is <tt>null</tt>, then the system will pick up an
* ephemeral port and a valid local address to bind the socket.
*</p>
*
* @param addr the address and port to bind to
* @throws SocketException if any error happens during the bind, or if the
* socket is already bound
* @throws SecurityException if a security manager exists and its
* <tt>checkListen</tt> method doesn't allow the operation
* @throws IllegalArgumentException if <tt>addr</tt> is a
* <tt>SocketAddress</tt> subclass not supported by this socket
* @see DatagramSocket#bind(SocketAddress)
*/
@Override
public void bind(SocketAddress addr)
throws SocketException
{
if (delegate == null)
super.bind(addr);
else
delegate.bind(addr);
}
/**
* Closes this datagram socket.
* <p>
* Any thread currently blocked in {@link #receive(DatagramPacket)} upon
* this socket will throw a {@link SocketException}.
* </p>
*
* @see DatagramSocket#close()
*/
@Override
public void close()
{
// We want both #delegate and super to actually get closed (and release
// the FDs which they hold). But super will not close unless isClosed()
// returns false. So we update the #closed flag last.
if (delegate != null)
delegate.close();
super.close();
closed = true;
}
/**
* Connects the socket to a remote address for this socket. When a socket is
* connected to a remote address, packets may only be sent to or received
* from that address. By default a datagram socket is not connected.
* <p>
* If the remote destination to which the socket is connected does not
* exist, or is otherwise unreachable, and if an ICMP destination
* unreachable packet has been received for that address, then a subsequent
* call to {@link #send(DatagramPacket)} or {@link #receive(DatagramPacket)}
* may throw a <tt>PortUnreachableException</tt>. Note, there is no
* guarantee that the exception will be thrown.
* </p>
*
* @param address the remote address for the socket
* @param port the remote port for the socket
* @throws IllegalArgumentException if the address is <tt>null</tt>, or the
* port is out of range
* @throws SecurityException if the caller is not allowed to send datagrams
* to and receive datagrams from the address and port
* @see DatagramSocket#connect(InetAddress, int)
*/
@Override
public void connect(InetAddress address, int port)
{
if (delegate == null)
super.connect(address, port);
else
delegate.connect(address, port);
}
/**
* Connects this socket to a remote socket address.
*
* @param addr the remote address
* @throws SocketException if the connect fails
* @throws IllegalArgumentException if <tt>addr</tt> is <tt>null</tt> or a
* <tt>SocketAddress</tt> subclass not supported by this socket
* @see DatagramSocket#connect(SocketAddress)
*/
@Override
public void connect(SocketAddress addr)
throws SocketException
{
if (delegate == null)
super.connect(addr);
else
delegate.connect(addr);
}
/**
* Disconnects the socket. This does nothing if the socket is not connected.
*
* @see DatagramSocket#disconnect()
*/
@Override
public void disconnect()
{
if (delegate == null)
super.disconnect();
else
delegate.disconnect();
}
/**
* Tests if <tt>SO_BROADCAST</tt> is enabled.
*
* @return a <tt>boolean</tt> indicating whether or not
* <tt>SO_BROADCAST</tt> is enabled
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @see DatagramSocket#getBroadcast()
*/
@Override
public boolean getBroadcast()
throws SocketException
{
return
(delegate == null) ? super.getBroadcast() : delegate.getBroadcast();
}
/**
* Returns the unique {@link DatagramChannel} object associated with this
* datagram socket, if any.
* <p>
* A datagram socket will have a channel if, and only if, the channel itself
* was created via the {@link DatagramChannel#open()} method
* </p>
*
* @return the datagram channel associated with this datagram socket, or
* <tt>null</tt> if this socket was not created for a channel
* @see DatagramSocket#getChannel()
*/
@Override
public DatagramChannel getChannel()
{
return (delegate == null) ? super.getChannel() : delegate.getChannel();
}
/**
* Returns the address to which this socket is connected. Returns
* <tt>null</tt> if the socket is not connected.
*
* @return the address to which this socket is connected
* @see DatagramSocket#getInetAddress()
*/
@Override
public InetAddress getInetAddress()
{
return
(delegate == null)
? super.getInetAddress()
: delegate.getInetAddress();
}
/**
* Gets the local address to which the socket is bound.
* <p>
* If there is a security manager, its <tt>checkConnect</tt> method is first
* called with the host address and <tt>-1</tt> as its arguments to see if
* the operation is allowed.
*
* @return the local address to which the socket is bound, or an
* <tt>InetAddress</tt> representing any local address if either the socket
* is not bound, or the security manager <tt>checkConnect</tt> method does
* not allow the operation
* @see DatagramSocket#getLocalAddress()
*/
@Override
public InetAddress getLocalAddress()
{
return
(delegate == null)
? super.getLocalAddress()
: delegate.getLocalAddress();
}
/**
* Returns the port number on the local host to which this socket is bound.
*
* @return the port number on the local host to which this socket is bound
* @see DatagramSocket#getLocalPort()
*/
@Override
public int getLocalPort()
{
return
(delegate == null) ? super.getLocalPort() : delegate.getLocalPort();
}
/**
* Returns the address of the endpoint this socket is bound to, or
* <tt>null</tt> if it is not bound yet.
*
* @return a <tt>SocketAddress</tt> representing the local endpoint of this
* socket, or <tt>null</tt> if it is not bound yet
* @see DatagramSocket#getLocalSocketAddress()
*/
@Override
public SocketAddress getLocalSocketAddress()
{
return
(delegate == null)
? super.getLocalSocketAddress()
: delegate.getLocalSocketAddress();
}
/**
* Returns the port for this socket. Returns <tt>-1</tt> if the socket is
* not connected.
*
* @return the port to which this socket is connected
* @see DatagramSocket#getPort()
*/
@Override
public int getPort()
{
return (delegate == null) ? super.getPort() : delegate.getPort();
}
/**
* Gets the value of the <tt>SO_RCVBUF</tt> option for this
* <tt>DatagramSocket</tt>, that is the buffer size used by the platform for
* input on this <tt>DatagramSocket</tt>.
*
* @return the value of the <tt>SO_RCVBUF</tt> option for this
* <tt>DatagramSocket</tt>
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @see DatagramSocket#getReceiveBufferSize()
*/
@Override
public int getReceiveBufferSize()
throws SocketException
{
return
(delegate == null)
? super.getReceiveBufferSize()
: delegate.getReceiveBufferSize();
}
/**
* Returns the address of the endpoint this socket is connected to, or
* <tt>null</tt> if it is unconnected.
*
* @return a <tt>SocketAddress</tt> representing the remote endpoint of this
* socket, or <tt>null</tt> if it is not connected yet
* @see DatagramSocket#getRemoteSocketAddress()
*/
@Override
public SocketAddress getRemoteSocketAddress()
{
return
(delegate == null)
? super.getRemoteSocketAddress()
: delegate.getRemoteSocketAddress();
}
/**
* Tests if <tt>SO_REUSEADDR</tt> is enabled.
*
* @return a <tt>boolean</tt> indicating whether or not
* <tt>SO_REUSEADDR</tt> is enabled
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @see DatagramSocket#getReuseAddress()
*/
@Override
public boolean getReuseAddress()
throws SocketException
{
return
(delegate == null)
? super.getReuseAddress()
: delegate.getReuseAddress();
}
/**
* Gets the value of the <tt>SO_SNDBUF</tt> option for this
* <tt>DatagramSocket</tt>, that is the buffer size used by the platform for
* output on this <tt>DatagramSocket</tt>.
*
* @return the value of the <tt>SO_SNDBUF</tt> option for this
* <tt>DatagramSocket</tt>
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @see DatagramSocket#getSendBufferSize()
*/
@Override
public int getSendBufferSize()
throws SocketException
{
return
(delegate == null)
? super.getSendBufferSize()
: delegate.getSendBufferSize();
}
/**
* Retrieves setting for <tt>SO_TIMEOUT</tt>. Zero returned implies that
* the option is disabled (i.e., timeout of infinity).
*
* @return the setting for <tt>SO_TIMEOUT</tt>
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @see DatagramSocket#getSoTimeout()
*/
@Override
public int getSoTimeout()
throws SocketException
{
return
(delegate == null) ? super.getSoTimeout() : delegate.getSoTimeout();
}
/**
* Gets the traffic class or type-of-service in the IP datagram header for
* packets sent from this <tt>DatagramSocket</tt>.
* <p>
* As the underlying network implementation may ignore the traffic class or
* type-of-service set using {@link #setTrafficClass(int)} this method may
* return a different value than was previously set using the
* {@link #setTrafficClass(int)} method on this <tt>DatagramSocket</tt>.
* </p>
*
* @return the traffic class or type-of-service already set
* @throws SocketException if there is an error obtaining the traffic class
* or type-of-service value
* @see DatagramSocket#getTrafficClass()
*/
@Override
public int getTrafficClass()
throws SocketException
{
return
(delegate == null)
? super.getTrafficClass()
: delegate.getTrafficClass();
}
/**
* Returns the binding state of the socket.
*
* @return <tt>true</tt> if the socket successfully bound to an address;
* otherwise, <tt>false</tt>
* @see DatagramSocket#isBound()
*/
@Override
public boolean isBound()
{
return (delegate == null) ? super.isBound() : delegate.isBound();
}
/**
* Returns whether the socket is closed or not.
*
* @return <tt>true</tt> if the socket has been closed; otherwise,
* <tt>false</tt>
* @see DatagramSocket#isClosed()
*/
@Override
public boolean isClosed()
{
return closed;
}
/**
* Returns the connection state of the socket.
*
* @return <tt>true</tt> if the socket successfully connected to a server;
* otherwise, <tt>false</tt>
* @see DatagramSocket#isConnected()
*/
@Override
public boolean isConnected()
{
return (delegate == null) ? super.isConnected() :
delegate.isConnected();
}
/**
* Receives a datagram packet from this socket. When this method returns,
* the <tt>DatagramPacket</tt>'s buffer is filled with the data received.
* The datagram packet also contains the sender's IP address, and the port
* number on the sender's machine.
* <p>
* This method blocks until a datagram is received. The <tt>length</tt>
* field of the datagram packet object contains the length of the received
* message. If the message is longer than the packet's length, the message
* is truncated.
* </p>
* <p>
* If there is a security manager, a packet cannot be received if the
* security manager's <tt>checkAccept</tt> method does not allow it.
* </p>
*
* @param p the <tt>DatagramPacket</tt> into which to place the incoming
* data
* @throws IOException if an I/O error occurs
* @see DatagramSocket#receive(DatagramPacket)
*/
@Override
public void receive(DatagramPacket p)
throws IOException
{
if (delegate == null)
{
// If the packet length is too small, then the
// DatagramSocket.receive function will truncate the received
// datagram. This problem appears when reusing the same
// DatagramPacket i.e. if the first time we use the DatagramPacket
// to receive a small packet and the second time a bigger one, then
// after the first packet is received, the length is set to the size
// of the first packet and the second packet is truncated.
// http://docs.oracle.com/javase/6/docs/api/java/net/DatagramSocket.html
//
// XXX(boris): I think the above is wrong. I don't interpret the
// API description this way, and testing on a couple of different
// environments shows that DatagramSocket.receive() grows the
// packet's length to as much as much as the array (and offset)
// would allow. I am leaving the code because it seems harmless.
byte[] data = p.getData();
p.setLength((data == null) ? 0 : (data.length - p.getOffset()));
super.receive(p);
if (StunDatagramPacketFilter.isStunPacket(p)
|| logNonStun(++nbReceivedPackets))
{
StunStack.logPacketToPcap(
p,
false,
getLocalAddress(),
getLocalPort());
}
}
else
{
delegate.receive(p);
}
}
/**
* Sends a datagram packet from this socket. The <tt>DatagramPacket</tt>
* includes information indicating the data to be sent, its length, the IP
* address of the remote host, and the port number on the remote host.
* <p>
* If there is a security manager, and the socket is not currently connected
* to a remote address, this method first performs some security checks.
* First, if <tt>p.getAddress().isMulticastAddress()</tt> is true, this
* method calls the security manager's <tt>checkMulticast</tt> method with
* <tt>p.getAddress()</tt> as its argument. If the evaluation of that
* expression is <tt>false</tt>, this method instead calls the security
* manager's <tt>checkConnect</tt> method with arguments
* <tt>p.getAddress().getHostAddress()</tt> and <tt>p.getPort()</tt>. Each
* call to a security manager method could result in a
* <tt>SecurityException</tt> if the operation is not allowed.
* </p>
*
* @param p the <tt>DatagramPacket</tt> to be sent
* @throws IOException if an I/O error occurs
* @see DatagramSocket#send(DatagramPacket)
*/
@Override
public void send(DatagramPacket p)
throws IOException
{
// Sends the packet to the final DatagramSocket
if (delegate == null)
{
try
{
super.send(p);
}
// DIRTY, DIRTY, DIRTY!!!
// Here we correct a java under MAC OSX bug when dealing with
// ipv6 local interface: the destination address (as well as the
// source address) under java / MAC OSX must have a scope ID,
// i.e. "fe80::1%en1". This correction (the whole "catch") is to
// be removed as soon as java under MAC OSX implements a real ipv6
// network stack.
catch(Exception ex)
{
InetAddress tmpAddr = p.getAddress();
if(((ex instanceof NoRouteToHostException)
|| (ex.getMessage() != null
&& ex.getMessage().equals("No route to host")))
&& (tmpAddr instanceof Inet6Address)
&& (tmpAddr.isLinkLocalAddress()))
{
Inet6Address newAddr = Inet6Address.getByAddress(
"",
tmpAddr.getAddress(),
((Inet6Address) super.getLocalAddress())
.getScopeId());
p.setAddress(newAddr);
super.send(p);
} else if (ex instanceof IOException) {
throw((IOException)ex);
}
}
if (logNonStun(++nbSentPackets))
{
StunStack.logPacketToPcap(
p,
true,
getLocalAddress(),
getLocalPort());
}
}
// Else, the delegate socket will encapsulate the packet.
else
{
delegate.send(p);
}
}
/**
* Enables/disables <tt>SO_BROADCAST</tt>.
*
* @param on whether or not to have broadcast turned on
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @see DatagramSocket#setBroadcast(boolean)
*/
@Override
public void setBroadcast(boolean on)
throws SocketException
{
if (delegate == null)
super.setBroadcast(on);
else
delegate.setBroadcast(on);
}
/**
* Sets the <tt>SO_RCVBUF</tt> option to the specified value for this
* <tt>DatagramSocket</tt>. The <tt>SO_RCVBUF</tt> option is used by the
* network implementation as a hint to size the underlying network I/O
* buffers. The <tt>SO_RCVBUF</tt> setting may also be used by the network
* implementation to determine the maximum size of the packet that can be
* received on this socket.
* <p>
* Because <tt>SO_RCVBUF</tt> is a hint, applications that want to verify
* what size the buffers were set to should call
* {@link #getReceiveBufferSize()}.
* </p>
*
* @param size the size to which to set the receive buffer size. The value
* must be greater than zero
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @throws IllegalArgumentException if the value is zero or is negative
* @see DatagramSocket#setReceiveBufferSize(int)
*/
@Override
public void setReceiveBufferSize(int size)
throws SocketException
{
if (delegate == null)
super.setReceiveBufferSize(size);
else
delegate.setReceiveBufferSize(size);
}
/**
* Enables/disables the <tt>SO_REUSEADDR</tt> socket option.
*
* @param on whether to enable or disable the <tt>SO_REUSEADDR</tt> socket
* option
* @throws SocketException if an error occurs enabling or disabling the
* <tt>SO_RESUEADDR</tt> socket option, or the socket is closed
* @see DatagramSocket#setReuseAddress(boolean)
*/
@Override
public void setReuseAddress(boolean on)
throws SocketException
{
if (delegate == null)
super.setReuseAddress(on);
else
delegate.setReuseAddress(on);
}
/**
* Sets the <tt>SO_SNDBUF</tt> option to the specified value for this
* <tt>DatagramSocket</tt>. The <tt>SO_SNDBUF</tt> option is used by the
* network implementation as a hint to size the underlying network I/O
* buffers. The <tt>SO_SNDBUF</tt> setting may also be used by the network
* implementation to determine the maximum size of the packet that can be
* sent on this socket.
* <p>
* As <tt>SO_SNDBUF</tt> is a hint, applications that want to verify what
* size the buffer is should call {@link #getSendBufferSize()}.
* </p>
* <p>
* Increasing the buffer size may allow multiple outgoing packets to be
* queued by the network implementation when the send rate is high.
* </p>
*
* @param size the size to which to set the send buffer size. The value must
* be greater than zero
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @throws IllegalArgumentException if the value is zero or is negative
* @see DatagramSocket#setSendBufferSize(int)
*/
@Override
public void setSendBufferSize(int size)
throws SocketException
{
if (delegate == null)
super.setSendBufferSize(size);
else
delegate.setSendBufferSize(size);
}
/**
* Enables/disables <tt>SO_TIMEOUT</tt> with the specified timeout, in
* milliseconds. With this option set to a non-zero timeout, a call to
* {@link #receive(DatagramPacket)} for this <tt>DatagramSocket</tt> will
* block for only this amount of time. If the timeout expires, a
* <tt>SocketTimeoutException</tt> is raised, though the
* <tt>DatagramSocket</tt> is still valid. The option must be enabled prior
* to entering the blocking operation to have effect. The timeout must be
* greater than zero. A timeout of zero is interpreted as an infinite
* timeout.
*
* @param timeout the specified timeout in milliseconds
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error
* @see DatagramSocket#setSoTimeout(int)
*/
@Override
public void setSoTimeout(int timeout)
throws SocketException
{
if (delegate == null)
super.setSoTimeout(timeout);
else
delegate.setSoTimeout(timeout);
}
/**
* Sets traffic class or type-of-service octet in the IP datagram header for
* datagrams sent from this <tt>DatagramSocket</tt>. As the underlying
* network implementation may ignore this value applications should consider
* it a hint.
*
* @param tc an <tt>int</tt> value for the bitset
* @throws SocketException if there is an error setting the traffic class or
* type-of-service
* @see DatagramSocket#setTrafficClass(int)
*/
@Override
public void setTrafficClass(int tc)
throws SocketException
{
if (delegate == null)
super.setTrafficClass(tc);
else
delegate.setTrafficClass(tc);
}
/**
* A utility method used by the constructors to ensure the receive buffer
* size is set to the preferred default.
*
* This implementation only has an impact of there is no delegate, in other
* words, if we really are using the superclass socket implementation as the
* raw socket.
*
* @throws SocketException if there is an error in the underlying protocol,
* such as an UDP error.
*/
private void initReceiveBufferSize()
throws SocketException
{
// Only change the buffer size on the real underlying DatagramSocket.
if(delegate == null && defaultReceiveBufferSize > 0)
{
super.setReceiveBufferSize(defaultReceiveBufferSize);
}
}
}