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
|
/* Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation
This file is part of libgcj.
This software is copyrighted work licensed under the terms of the
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
details. */
#include <config.h>
#include <platform.h>
#ifdef HAVE_SYS_IOCTL_H
#define BSD_COMP /* Get FIONREAD on Solaris2. */
#include <sys/ioctl.h>
#endif
// Pick up FIONREAD on Solaris 2.5.
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <errno.h>
#include <string.h>
#if HAVE_BSTRING_H
// Needed for bzero, implicitly used by FD_ZERO on IRIX 5.2
#include <bstring.h>
#endif
#include <gcj/cni.h>
#include <gcj/javaprims.h>
#include <gnu/java/net/PlainSocketImpl.h>
#include <gnu/java/net/PlainSocketImpl$SocketInputStream.h>
#include <gnu/java/net/PlainSocketImpl$SocketOutputStream.h>
#include <java/io/IOException.h>
#include <java/io/InterruptedIOException.h>
#include <java/net/BindException.h>
#include <java/net/ConnectException.h>
#include <java/net/InetAddress.h>
#include <java/net/InetSocketAddress.h>
#include <java/net/SocketException.h>
#include <java/net/SocketTimeoutException.h>
#include <java/lang/InternalError.h>
#include <java/lang/Object.h>
#include <java/lang/Boolean.h>
#include <java/lang/Class.h>
#include <java/lang/Integer.h>
#include <java/lang/Thread.h>
#include <java/lang/NullPointerException.h>
#include <java/lang/ArrayIndexOutOfBoundsException.h>
#include <java/lang/IllegalArgumentException.h>
#include <java/net/UnknownHostException.h>
union SockAddr
{
struct sockaddr_in address;
#ifdef HAVE_INET6
struct sockaddr_in6 address6;
#endif
};
void
gnu::java::net::PlainSocketImpl::create (jboolean stream)
{
// We might already have been create()d in the nio case.
if (native_fd != -1)
return;
int sock = _Jv_socket (AF_INET, stream ? SOCK_STREAM : SOCK_DGRAM, 0);
if (sock < 0)
{
char* strerr = strerror (errno);
throw new ::java::io::IOException (JvNewStringUTF (strerr));
}
// We use native_fd in place of fd here. From leaving fd null we avoid
// the double close problem in FileDescriptor.finalize.
native_fd = sock;
}
void
gnu::java::net::PlainSocketImpl::bind (::java::net::InetAddress *host, jint lport)
{
union SockAddr u;
struct sockaddr *ptr = (struct sockaddr *) &u.address;
jbyteArray haddress = host->addr;
jbyte *bytes = elements (haddress);
int len = haddress->length;
int i = 1;
// The following is needed for OS X/PPC, otherwise bind() fails with an
// error. I found the issue and following fix on some mailing list, but
// no explanation was given as to why this solved the problem.
memset (&u, 0, sizeof (u));
if (len == 4)
{
u.address.sin_family = AF_INET;
memcpy (&u.address.sin_addr, bytes, len);
len = sizeof (struct sockaddr_in);
u.address.sin_port = htons (lport);
}
#ifdef HAVE_INET6
else if (len == 16)
{
u.address6.sin6_family = AF_INET6;
memcpy (&u.address6.sin6_addr, bytes, len);
len = sizeof (struct sockaddr_in6);
u.address6.sin6_port = htons (lport);
}
#endif
else
throw new ::java::net::SocketException (JvNewStringUTF ("invalid length"));
// Enable SO_REUSEADDR, so that servers can reuse ports left in TIME_WAIT.
::setsockopt(native_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &i, sizeof(i));
if (_Jv_bind (native_fd, ptr, len) == 0)
{
socklen_t addrlen = sizeof(u);
if (lport != 0)
localport = lport;
else if (::getsockname (native_fd, (sockaddr*) &u, &addrlen) == 0)
localport = ntohs (u.address.sin_port);
else
goto error;
return;
}
error:
char* strerr = strerror (errno);
throw new ::java::net::BindException (JvNewStringUTF (strerr));
}
void
gnu::java::net::PlainSocketImpl::connect (::java::net::SocketAddress *addr,
jint timeout)
{
::java::net::InetSocketAddress *tmp = (::java::net::InetSocketAddress*) addr;
::java::net::InetAddress *host = tmp->getAddress();
if (! host)
throw new ::java::net::UnknownHostException(tmp->toString());
jint rport = tmp->getPort();
// Set the SocketImpl's address and port fields before we try to
// connect. Note that the fact that these are set doesn't imply
// that we're actually connected to anything. We need to record
// this data before we attempt the connect, since non-blocking
// SocketChannels will use this and almost certainly throw timeout
// exceptions.
address = host;
port = rport;
union SockAddr u;
socklen_t addrlen = sizeof(u);
jbyteArray haddress = host->addr;
jbyte *bytes = elements (haddress);
int len = haddress->length;
struct sockaddr *ptr = (struct sockaddr *) &u.address;
if (len == 4)
{
u.address.sin_family = AF_INET;
memcpy (&u.address.sin_addr, bytes, len);
len = sizeof (struct sockaddr_in);
u.address.sin_port = htons (rport);
}
#ifdef HAVE_INET6
else if (len == 16)
{
u.address6.sin6_family = AF_INET6;
memcpy (&u.address6.sin6_addr, bytes, len);
len = sizeof (struct sockaddr_in6);
u.address6.sin6_port = htons (rport);
}
#endif
else
throw new ::java::net::SocketException (JvNewStringUTF ("invalid length"));
if (timeout > 0)
{
int flags = ::fcntl (native_fd, F_GETFL);
::fcntl (native_fd, F_SETFL, flags | O_NONBLOCK);
if ((_Jv_connect (native_fd, ptr, len) != 0) && (errno != EINPROGRESS))
goto error;
fd_set fset;
struct timeval tv;
FD_ZERO(&fset);
FD_SET(native_fd, &fset);
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
int retval;
if ((retval = _Jv_select (native_fd + 1, &fset, &fset, NULL, &tv)) < 0)
goto error;
else if (retval == 0)
throw new ::java::net::SocketTimeoutException
(JvNewStringUTF ("Connect timed out"));
// Set the socket back into a blocking state.
::fcntl (native_fd, F_SETFL, flags);
}
else
{
if (_Jv_connect (native_fd, ptr, len) != 0)
goto error;
}
// A bind may not have been done on this socket; if so, set localport now.
if (localport == 0)
{
if (::getsockname (native_fd, (sockaddr*) &u, &addrlen) == 0)
localport = ntohs (u.address.sin_port);
else
goto error;
}
return;
error:
char* strerr = strerror (errno);
throw new ::java::net::ConnectException (JvNewStringUTF (strerr));
}
void
gnu::java::net::PlainSocketImpl::listen (jint backlog)
{
if (::listen (native_fd, backlog) != 0)
{
char* strerr = strerror (errno);
throw new ::java::io::IOException (JvNewStringUTF (strerr));
}
}
static void
throw_on_sock_closed (gnu::java::net::PlainSocketImpl *soc_impl)
{
// Avoid races from asynchronous close().
JvSynchronize sync (soc_impl);
if (soc_impl->native_fd == -1)
{
using namespace java::net;
// Socket was closed.
SocketException *se =
new SocketException (JvNewStringUTF ("Socket Closed"));
throw se;
}
}
void
gnu::java::net::PlainSocketImpl::accept (gnu::java::net::PlainSocketImpl *s)
{
union SockAddr u;
socklen_t addrlen = sizeof(u);
int new_socket = 0;
// Do timeouts via select since SO_RCVTIMEO is not always available.
if (timeout > 0 && native_fd >= 0 && native_fd < FD_SETSIZE)
{
fd_set fset;
struct timeval tv;
FD_ZERO(&fset);
FD_SET(native_fd, &fset);
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
int retval;
if ((retval = _Jv_select (native_fd + 1, &fset, &fset, NULL, &tv)) < 0)
goto error;
else if (retval == 0)
throw new ::java::net::SocketTimeoutException (
JvNewStringUTF("Accept timed out"));
}
new_socket = _Jv_accept (native_fd, (sockaddr*) &u, &addrlen);
if (new_socket < 0)
goto error;
jbyteArray raddr;
jint rport;
if (u.address.sin_family == AF_INET)
{
raddr = JvNewByteArray (4);
memcpy (elements (raddr), &u.address.sin_addr, 4);
rport = ntohs (u.address.sin_port);
}
#ifdef HAVE_INET6
else if (u.address.sin_family == AF_INET6)
{
raddr = JvNewByteArray (16);
memcpy (elements (raddr), &u.address6.sin6_addr, 16);
rport = ntohs (u.address6.sin6_port);
}
#endif
else
throw new ::java::net::SocketException (JvNewStringUTF ("invalid family"));
s->native_fd = new_socket;
s->localport = localport;
s->address = ::java::net::InetAddress::getByAddress (raddr);
s->port = rport;
return;
error:
char* strerr = strerror (errno);
throw_on_sock_closed (this);
throw new ::java::io::IOException (JvNewStringUTF (strerr));
}
// Close(shutdown) the socket.
void
gnu::java::net::PlainSocketImpl::close()
{
// Avoid races from asynchronous finalization.
JvSynchronize sync (this);
// Should we use shutdown here? Yes.
// How would that effect so_linger? Uncertain.
::shutdown (native_fd, 2);
// Ignore errors in shutdown as we are closing and all the same
// errors are handled in the close.
int res = _Jv_close (native_fd);
if (res == -1)
{
// These three errors are not errors according to tests performed
// on the reference implementation.
if (errno != ENOTCONN && errno != ECONNRESET && errno != EBADF)
throw new ::java::io::IOException (JvNewStringUTF (strerror (errno)));
}
// Safe place to reset the file pointer.
native_fd = -1;
timeout = 0;
}
static void
write_helper (jint native_fd, jbyte *bytes, jint len);
// Write a byte to the socket.
void
gnu::java::net::PlainSocketImpl$SocketOutputStream::write(jint b)
{
jbyte data = (jbyte) b;
write_helper (this$0->native_fd, &data, 1);
}
// Write some bytes to the socket.
void
gnu::java::net::PlainSocketImpl$SocketOutputStream::write(jbyteArray b, jint offset, jint len)
{
if (! b)
throw new ::java::lang::NullPointerException;
if (offset < 0 || len < 0 || offset + len > JvGetArrayLength (b))
throw new ::java::lang::ArrayIndexOutOfBoundsException;
write_helper (this$0->native_fd, elements (b) + offset, len);
}
static void
write_helper(jint native_fd, jbyte *bytes, jint len)
{
int written = 0;
while (len > 0)
{
int r = _Jv_write (native_fd, bytes, len);
if (r == -1)
{
if (::java::lang::Thread::interrupted())
{
::java::io::InterruptedIOException *iioe
= new ::java::io::InterruptedIOException
(JvNewStringLatin1 (strerror (errno)));
iioe->bytesTransferred = written;
throw iioe;
}
// Some errors should not cause exceptions.
if (errno != ENOTCONN && errno != ECONNRESET && errno != EBADF)
throw new ::java::io::IOException (JvNewStringUTF (strerror (errno)));
break;
}
written += r;
len -= r;
bytes += r;
}
}
void
gnu::java::net::PlainSocketImpl::sendUrgentData (jint)
{
throw new ::java::net::SocketException (JvNewStringLatin1 (
"PlainSocketImpl: sending of urgent data not supported by this socket"));
}
static jint
read_helper (gnu::java::net::PlainSocketImpl *soc_impl,
jbyte *bytes, jint count);
// Read a single byte from the socket.
jint
gnu::java::net::PlainSocketImpl$SocketInputStream::read(void)
{
jbyte data;
if (read_helper (this$0, &data, 1) == 1)
return data & 0xFF;
return -1;
}
// Read count bytes into the buffer, starting at offset.
jint
gnu::java::net::PlainSocketImpl$SocketInputStream::read(jbyteArray buffer,
jint offset,
jint count)
{
if (! buffer)
throw new ::java::lang::NullPointerException;
jsize bsize = JvGetArrayLength (buffer);
if (offset < 0 || count < 0 || offset + count > bsize)
throw new ::java::lang::ArrayIndexOutOfBoundsException;
return read_helper (this$0, elements (buffer) + offset, count);
}
static jint
read_helper (gnu::java::net::PlainSocketImpl *soc_impl,
jbyte *bytes, jint count)
{
// If zero bytes were requested, short circuit so that recv
// doesn't signal EOF.
if (count == 0)
return 0;
// Do timeouts via select.
if (soc_impl->timeout > 0
&& soc_impl->native_fd >= 0
&& soc_impl->native_fd < FD_SETSIZE)
{
// Create the file descriptor set.
fd_set read_fds;
FD_ZERO (&read_fds);
FD_SET (soc_impl->native_fd, &read_fds);
// Create the timeout struct based on our internal timeout value.
struct timeval timeout_value;
timeout_value.tv_sec = soc_impl->timeout / 1000;
timeout_value.tv_usec =(soc_impl->timeout % 1000) * 1000;
// Select on the fds.
int sel_retval =
_Jv_select (soc_impl->native_fd + 1,
&read_fds, NULL, NULL, &timeout_value);
// We're only interested in the 0 return.
// error returns still require us to try to read
// the socket to see what happened.
if (sel_retval == 0)
{
::java::net::SocketTimeoutException *timeoutException =
new ::java::net::SocketTimeoutException
(JvNewStringUTF ("Read timed out"));
throw timeoutException;
}
}
// Read the socket.
int r = ::recv (soc_impl->native_fd, (char *) bytes, count, 0);
if (r == 0)
{
throw_on_sock_closed (soc_impl);
return -1;
}
if (::java::lang::Thread::interrupted())
{
::java::io::InterruptedIOException *iioe =
new ::java::io::InterruptedIOException
(JvNewStringUTF ("Read interrupted"));
iioe->bytesTransferred = r == -1 ? 0 : r;
throw iioe;
}
else if (r == -1)
{
throw_on_sock_closed (soc_impl);
// Some errors cause us to return end of stream...
if (errno == ENOTCONN)
return -1;
// Other errors need to be signalled.
throw new ::java::io::IOException (JvNewStringUTF (strerror (errno)));
}
return r;
}
// How many bytes are available?
jint
gnu::java::net::PlainSocketImpl::available(void)
{
#if defined(FIONREAD) || defined(HAVE_SELECT)
int num = 0;
int r = 0;
bool num_set = false;
#if defined(FIONREAD)
r = ::ioctl (native_fd, FIONREAD, &num);
if (r == -1 && errno == ENOTTY)
{
// If the ioctl doesn't work, we don't care.
r = 0;
num = 0;
}
else
num_set = true;
#elif defined(HAVE_SELECT)
if (native_fd < 0)
{
errno = EBADF;
r = -1;
}
#endif
if (r == -1)
{
posix_error:
throw new ::java::io::IOException(JvNewStringUTF(strerror(errno)));
}
// If we didn't get anything we can use select.
#if defined(HAVE_SELECT)
if (! num_set)
if (! num_set && native_fd >= 0 && native_fd < FD_SETSIZE)
{
fd_set rd;
FD_ZERO (&rd);
FD_SET (native_fd, &rd);
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
r = _Jv_select (native_fd + 1, &rd, NULL, NULL, &tv);
if(r == -1)
goto posix_error;
num = r == 0 ? 0 : 1;
}
#endif /* HAVE_SELECT */
return (jint) num;
#else
throw new ::java::io::IOException (JvNewStringUTF ("unimplemented"));
#endif
}
void
gnu::java::net::PlainSocketImpl::setOption (jint optID, ::java::lang::Object *value)
{
int val;
socklen_t val_len = sizeof (val);
if (native_fd < 0)
throw new ::java::net::SocketException (JvNewStringUTF ("Socket closed"));
if (_Jv_IsInstanceOf (value, &::java::lang::Boolean::class$))
{
::java::lang::Boolean *boolobj =
static_cast< ::java::lang::Boolean *> (value);
if (boolobj->booleanValue())
val = 1;
else
{
if (optID == _Jv_SO_LINGER_)
val = -1;
else
val = 0;
}
}
else if (_Jv_IsInstanceOf (value, &::java::lang::Integer::class$))
{
::java::lang::Integer *intobj =
static_cast< ::java::lang::Integer *> (value);
val = (int) intobj->intValue();
}
else
{
throw new ::java::lang::IllegalArgumentException (
JvNewStringLatin1 ("`value' must be Boolean or Integer"));
}
switch (optID)
{
case _Jv_TCP_NODELAY_ :
#ifdef TCP_NODELAY
if (::setsockopt (native_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &val,
val_len) != 0)
goto error;
#else
throw new ::java::lang::InternalError
(JvNewStringUTF ("TCP_NODELAY not supported"));
#endif /* TCP_NODELAY */
return;
case _Jv_SO_KEEPALIVE_ :
if (::setsockopt (native_fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &val,
val_len) != 0)
goto error;
return;
case _Jv_SO_BROADCAST_ :
throw new ::java::net::SocketException
(JvNewStringUTF ("SO_BROADCAST not valid for TCP"));
return;
case _Jv_SO_OOBINLINE_ :
if (::setsockopt (native_fd, SOL_SOCKET, SO_OOBINLINE, (char *) &val,
val_len) != 0)
goto error;
return;
case _Jv_SO_LINGER_ :
#ifdef SO_LINGER
struct linger l_val;
l_val.l_onoff = (val != -1);
l_val.l_linger = val;
if (::setsockopt (native_fd, SOL_SOCKET, SO_LINGER, (char *) &l_val,
sizeof(l_val)) != 0)
goto error;
#else
throw new ::java::lang::InternalError (
JvNewStringUTF ("SO_LINGER not supported"));
#endif /* SO_LINGER */
return;
case _Jv_SO_SNDBUF_ :
case _Jv_SO_RCVBUF_ :
#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
int opt;
optID == _Jv_SO_SNDBUF_ ? opt = SO_SNDBUF : opt = SO_RCVBUF;
if (::setsockopt (native_fd, SOL_SOCKET, opt, (char *) &val, val_len) != 0)
goto error;
#else
throw new ::java::lang::InternalError (
JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported"));
#endif
return;
case _Jv_SO_BINDADDR_ :
throw new ::java::net::SocketException (
JvNewStringUTF ("SO_BINDADDR: read only option"));
return;
case _Jv_IP_MULTICAST_IF_ :
throw new ::java::net::SocketException (
JvNewStringUTF ("IP_MULTICAST_IF: not valid for TCP"));
return;
case _Jv_IP_MULTICAST_IF2_ :
throw new ::java::net::SocketException (
JvNewStringUTF ("IP_MULTICAST_IF2: not valid for TCP"));
return;
case _Jv_IP_MULTICAST_LOOP_ :
throw new ::java::net::SocketException (
JvNewStringUTF ("IP_MULTICAST_LOOP: not valid for TCP"));
return;
case _Jv_IP_TOS_ :
if (::setsockopt (native_fd, SOL_SOCKET, IP_TOS, (char *) &val,
val_len) != 0)
goto error;
return;
case _Jv_SO_REUSEADDR_ :
#if defined(SO_REUSEADDR)
if (::setsockopt (native_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &val,
val_len) != 0)
goto error;
return;
#else
throw new ::java::lang::InternalError (
JvNewStringUTF ("SO_REUSEADDR not supported"));
#endif
case _Jv_SO_TIMEOUT_ :
timeout = val;
return;
default :
errno = ENOPROTOOPT;
}
error:
char* strerr = strerror (errno);
throw new ::java::net::SocketException (JvNewStringUTF (strerr));
}
::java::lang::Object *
gnu::java::net::PlainSocketImpl::getOption (jint optID)
{
int val;
socklen_t val_len = sizeof(val);
union SockAddr u;
socklen_t addrlen = sizeof(u);
struct linger l_val;
socklen_t l_val_len = sizeof(l_val);
switch (optID)
{
#ifdef TCP_NODELAY
case _Jv_TCP_NODELAY_ :
if (::getsockopt (native_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &val,
&val_len) != 0)
goto error;
else
return new ::java::lang::Boolean (val != 0);
#else
throw new ::java::lang::InternalError
(JvNewStringUTF ("TCP_NODELAY not supported"));
#endif
break;
case _Jv_SO_LINGER_ :
#ifdef SO_LINGER
if (::getsockopt (native_fd, SOL_SOCKET, SO_LINGER, (char *) &l_val,
&l_val_len) != 0)
goto error;
if (l_val.l_onoff)
return new ::java::lang::Integer (l_val.l_linger);
else
return new ::java::lang::Boolean ((jboolean)false);
#else
throw new ::java::lang::InternalError
(JvNewStringUTF ("SO_LINGER not supported"));
#endif
break;
case _Jv_SO_KEEPALIVE_ :
if (::getsockopt (native_fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &val,
&val_len) != 0)
goto error;
else
return new ::java::lang::Boolean (val != 0);
case _Jv_SO_BROADCAST_ :
if (::getsockopt (native_fd, SOL_SOCKET, SO_BROADCAST, (char *) &val,
&val_len) != 0)
goto error;
return new ::java::lang::Boolean ((jboolean)val);
case _Jv_SO_OOBINLINE_ :
if (::getsockopt (native_fd, SOL_SOCKET, SO_OOBINLINE, (char *) &val,
&val_len) != 0)
goto error;
return new ::java::lang::Boolean ((jboolean)val);
case _Jv_SO_RCVBUF_ :
case _Jv_SO_SNDBUF_ :
#if defined(SO_SNDBUF) && defined(SO_RCVBUF)
int opt;
optID == _Jv_SO_SNDBUF_ ? opt = SO_SNDBUF : opt = SO_RCVBUF;
if (::getsockopt (native_fd, SOL_SOCKET, opt, (char *) &val, &val_len) != 0)
goto error;
else
return new ::java::lang::Integer (val);
#else
throw new ::java::lang::InternalError
(JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported"));
#endif
break;
case _Jv_SO_BINDADDR_:
// cache the local address
if (localAddress == NULL)
{
jbyteArray laddr;
if (::getsockname (native_fd, (sockaddr*) &u, &addrlen) != 0)
goto error;
if (u.address.sin_family == AF_INET)
{
laddr = JvNewByteArray (4);
memcpy (elements (laddr), &u.address.sin_addr, 4);
}
#ifdef HAVE_INET6
else if (u.address.sin_family == AF_INET6)
{
laddr = JvNewByteArray (16);
memcpy (elements (laddr), &u.address6.sin6_addr, 16);
}
#endif
else
throw new ::java::net::SocketException
(JvNewStringUTF ("invalid family"));
localAddress = ::java::net::InetAddress::getByAddress (laddr);
}
return localAddress;
break;
case _Jv_IP_MULTICAST_IF_ :
throw new ::java::net::SocketException
(JvNewStringUTF ("IP_MULTICAST_IF: not valid for TCP"));
break;
case _Jv_IP_MULTICAST_IF2_ :
throw new ::java::net::SocketException
(JvNewStringUTF ("IP_MULTICAST_IF2: not valid for TCP"));
break;
case _Jv_IP_MULTICAST_LOOP_ :
throw new ::java::net::SocketException
(JvNewStringUTF ("IP_MULTICAST_LOOP: not valid for TCP"));
break;
case _Jv_IP_TOS_ :
if (::getsockopt (native_fd, SOL_SOCKET, IP_TOS, (char *) &val,
&val_len) != 0)
goto error;
return new ::java::lang::Integer (val);
break;
case _Jv_SO_REUSEADDR_ :
#if defined(SO_REUSEADDR)
if (::getsockopt (native_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &val,
&val_len) != 0)
goto error;
#else
throw new ::java::lang::InternalError (
JvNewStringUTF ("SO_REUSEADDR not supported"));
#endif
break;
case _Jv_SO_TIMEOUT_ :
return new ::java::lang::Integer (timeout);
break;
default :
errno = ENOPROTOOPT;
}
error:
char* strerr = strerror (errno);
throw new ::java::net::SocketException (JvNewStringUTF (strerr));
}
void
gnu::java::net::PlainSocketImpl::shutdownInput (void)
{
if (::shutdown (native_fd, 0))
throw new ::java::net::SocketException (JvNewStringUTF (strerror (errno)));
}
void
gnu::java::net::PlainSocketImpl::shutdownOutput (void)
{
if (::shutdown (native_fd, 1))
throw new ::java::net::SocketException (JvNewStringUTF (strerror (errno)));
}
|