natPlainSocketImplPosix.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /* Copyright (C) 2003, 2004, 2005, 2006, 2007, 2012 Free Software Foundation
  2. This file is part of libgcj.
  3. This software is copyrighted work licensed under the terms of the
  4. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  5. details. */
  6. #include <config.h>
  7. #include <platform.h>
  8. #ifdef HAVE_SYS_IOCTL_H
  9. #define BSD_COMP /* Get FIONREAD on Solaris2. */
  10. #include <sys/ioctl.h>
  11. #endif
  12. // Pick up FIONREAD on Solaris 2.5.
  13. #ifdef HAVE_SYS_FILIO_H
  14. #include <sys/filio.h>
  15. #endif
  16. #include <netinet/in.h>
  17. #include <netinet/tcp.h>
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <gcj/cni.h>
  21. #include <gcj/javaprims.h>
  22. #include <gnu/java/net/PlainSocketImpl.h>
  23. #include <gnu/java/net/PlainSocketImpl$SocketInputStream.h>
  24. #include <gnu/java/net/PlainSocketImpl$SocketOutputStream.h>
  25. #include <java/io/IOException.h>
  26. #include <java/io/InterruptedIOException.h>
  27. #include <java/net/BindException.h>
  28. #include <java/net/ConnectException.h>
  29. #include <java/net/InetAddress.h>
  30. #include <java/net/InetSocketAddress.h>
  31. #include <java/net/SocketException.h>
  32. #include <java/net/SocketTimeoutException.h>
  33. #include <java/lang/InternalError.h>
  34. #include <java/lang/Object.h>
  35. #include <java/lang/Boolean.h>
  36. #include <java/lang/Class.h>
  37. #include <java/lang/Integer.h>
  38. #include <java/lang/Thread.h>
  39. #include <java/lang/NullPointerException.h>
  40. #include <java/lang/ArrayIndexOutOfBoundsException.h>
  41. #include <java/lang/IllegalArgumentException.h>
  42. #include <java/net/UnknownHostException.h>
  43. union SockAddr
  44. {
  45. struct sockaddr_in address;
  46. #ifdef HAVE_INET6
  47. struct sockaddr_in6 address6;
  48. #endif
  49. };
  50. void
  51. gnu::java::net::PlainSocketImpl::create (jboolean stream)
  52. {
  53. // We might already have been create()d in the nio case.
  54. if (native_fd != -1)
  55. return;
  56. int sock = _Jv_socket (AF_INET, stream ? SOCK_STREAM : SOCK_DGRAM, 0);
  57. if (sock < 0)
  58. {
  59. char* strerr = strerror (errno);
  60. throw new ::java::io::IOException (JvNewStringUTF (strerr));
  61. }
  62. // We use native_fd in place of fd here. From leaving fd null we avoid
  63. // the double close problem in FileDescriptor.finalize.
  64. native_fd = sock;
  65. }
  66. void
  67. gnu::java::net::PlainSocketImpl::bind (::java::net::InetAddress *host, jint lport)
  68. {
  69. union SockAddr u;
  70. struct sockaddr *ptr = (struct sockaddr *) &u.address;
  71. jbyteArray haddress = host->addr;
  72. jbyte *bytes = elements (haddress);
  73. int len = haddress->length;
  74. int i = 1;
  75. // The following is needed for OS X/PPC, otherwise bind() fails with an
  76. // error. I found the issue and following fix on some mailing list, but
  77. // no explanation was given as to why this solved the problem.
  78. memset (&u, 0, sizeof (u));
  79. if (len == 4)
  80. {
  81. u.address.sin_family = AF_INET;
  82. memcpy (&u.address.sin_addr, bytes, len);
  83. len = sizeof (struct sockaddr_in);
  84. u.address.sin_port = htons (lport);
  85. }
  86. #ifdef HAVE_INET6
  87. else if (len == 16)
  88. {
  89. u.address6.sin6_family = AF_INET6;
  90. memcpy (&u.address6.sin6_addr, bytes, len);
  91. len = sizeof (struct sockaddr_in6);
  92. u.address6.sin6_port = htons (lport);
  93. }
  94. #endif
  95. else
  96. throw new ::java::net::SocketException (JvNewStringUTF ("invalid length"));
  97. // Enable SO_REUSEADDR, so that servers can reuse ports left in TIME_WAIT.
  98. ::setsockopt(native_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &i, sizeof(i));
  99. if (_Jv_bind (native_fd, ptr, len) == 0)
  100. {
  101. socklen_t addrlen = sizeof(u);
  102. if (lport != 0)
  103. localport = lport;
  104. else if (::getsockname (native_fd, (sockaddr*) &u, &addrlen) == 0)
  105. localport = ntohs (u.address.sin_port);
  106. else
  107. goto error;
  108. return;
  109. }
  110. error:
  111. char* strerr = strerror (errno);
  112. throw new ::java::net::BindException (JvNewStringUTF (strerr));
  113. }
  114. void
  115. gnu::java::net::PlainSocketImpl::connect (::java::net::SocketAddress *addr,
  116. jint timeout)
  117. {
  118. ::java::net::InetSocketAddress *tmp = (::java::net::InetSocketAddress*) addr;
  119. ::java::net::InetAddress *host = tmp->getAddress();
  120. if (! host)
  121. throw new ::java::net::UnknownHostException(tmp->toString());
  122. jint rport = tmp->getPort();
  123. // Set the SocketImpl's address and port fields before we try to
  124. // connect. Note that the fact that these are set doesn't imply
  125. // that we're actually connected to anything. We need to record
  126. // this data before we attempt the connect, since non-blocking
  127. // SocketChannels will use this and almost certainly throw timeout
  128. // exceptions.
  129. address = host;
  130. port = rport;
  131. union SockAddr u;
  132. socklen_t addrlen = sizeof(u);
  133. jbyteArray haddress = host->addr;
  134. jbyte *bytes = elements (haddress);
  135. int len = haddress->length;
  136. struct sockaddr *ptr = (struct sockaddr *) &u.address;
  137. if (len == 4)
  138. {
  139. u.address.sin_family = AF_INET;
  140. memcpy (&u.address.sin_addr, bytes, len);
  141. len = sizeof (struct sockaddr_in);
  142. u.address.sin_port = htons (rport);
  143. }
  144. #ifdef HAVE_INET6
  145. else if (len == 16)
  146. {
  147. u.address6.sin6_family = AF_INET6;
  148. memcpy (&u.address6.sin6_addr, bytes, len);
  149. len = sizeof (struct sockaddr_in6);
  150. u.address6.sin6_port = htons (rport);
  151. }
  152. #endif
  153. else
  154. throw new ::java::net::SocketException (JvNewStringUTF ("invalid length"));
  155. if (timeout > 0)
  156. {
  157. int flags = ::fcntl (native_fd, F_GETFL);
  158. ::fcntl (native_fd, F_SETFL, flags | O_NONBLOCK);
  159. if ((_Jv_connect (native_fd, ptr, len) != 0) && (errno != EINPROGRESS))
  160. goto error;
  161. fd_set fset;
  162. struct timeval tv;
  163. FD_ZERO(&fset);
  164. FD_SET(native_fd, &fset);
  165. tv.tv_sec = timeout / 1000;
  166. tv.tv_usec = (timeout % 1000) * 1000;
  167. int retval;
  168. if ((retval = _Jv_select (native_fd + 1, &fset, &fset, NULL, &tv)) < 0)
  169. goto error;
  170. else if (retval == 0)
  171. throw new ::java::net::SocketTimeoutException
  172. (JvNewStringUTF ("Connect timed out"));
  173. // Set the socket back into a blocking state.
  174. ::fcntl (native_fd, F_SETFL, flags);
  175. }
  176. else
  177. {
  178. if (_Jv_connect (native_fd, ptr, len) != 0)
  179. goto error;
  180. }
  181. // A bind may not have been done on this socket; if so, set localport now.
  182. if (localport == 0)
  183. {
  184. if (::getsockname (native_fd, (sockaddr*) &u, &addrlen) == 0)
  185. localport = ntohs (u.address.sin_port);
  186. else
  187. goto error;
  188. }
  189. return;
  190. error:
  191. char* strerr = strerror (errno);
  192. throw new ::java::net::ConnectException (JvNewStringUTF (strerr));
  193. }
  194. void
  195. gnu::java::net::PlainSocketImpl::listen (jint backlog)
  196. {
  197. if (::listen (native_fd, backlog) != 0)
  198. {
  199. char* strerr = strerror (errno);
  200. throw new ::java::io::IOException (JvNewStringUTF (strerr));
  201. }
  202. }
  203. static void
  204. throw_on_sock_closed (gnu::java::net::PlainSocketImpl *soc_impl)
  205. {
  206. // Avoid races from asynchronous close().
  207. JvSynchronize sync (soc_impl);
  208. if (soc_impl->native_fd == -1)
  209. {
  210. using namespace java::net;
  211. // Socket was closed.
  212. SocketException *se =
  213. new SocketException (JvNewStringUTF ("Socket Closed"));
  214. throw se;
  215. }
  216. }
  217. void
  218. gnu::java::net::PlainSocketImpl::accept (gnu::java::net::PlainSocketImpl *s)
  219. {
  220. union SockAddr u;
  221. socklen_t addrlen = sizeof(u);
  222. int new_socket = 0;
  223. // Do timeouts via select since SO_RCVTIMEO is not always available.
  224. if (timeout > 0 && native_fd >= 0 && native_fd < FD_SETSIZE)
  225. {
  226. fd_set fset;
  227. struct timeval tv;
  228. FD_ZERO(&fset);
  229. FD_SET(native_fd, &fset);
  230. tv.tv_sec = timeout / 1000;
  231. tv.tv_usec = (timeout % 1000) * 1000;
  232. int retval;
  233. if ((retval = _Jv_select (native_fd + 1, &fset, &fset, NULL, &tv)) < 0)
  234. goto error;
  235. else if (retval == 0)
  236. throw new ::java::net::SocketTimeoutException (
  237. JvNewStringUTF("Accept timed out"));
  238. }
  239. new_socket = ::accept (native_fd, (sockaddr*) &u, &addrlen);
  240. if (new_socket < 0)
  241. goto error;
  242. jbyteArray raddr;
  243. jint rport;
  244. if (u.address.sin_family == AF_INET)
  245. {
  246. raddr = JvNewByteArray (4);
  247. memcpy (elements (raddr), &u.address.sin_addr, 4);
  248. rport = ntohs (u.address.sin_port);
  249. }
  250. #ifdef HAVE_INET6
  251. else if (u.address.sin_family == AF_INET6)
  252. {
  253. raddr = JvNewByteArray (16);
  254. memcpy (elements (raddr), &u.address6.sin6_addr, 16);
  255. rport = ntohs (u.address6.sin6_port);
  256. }
  257. #endif
  258. else
  259. throw new ::java::net::SocketException (JvNewStringUTF ("invalid family"));
  260. s->native_fd = new_socket;
  261. s->localport = localport;
  262. s->address = ::java::net::InetAddress::getByAddress (raddr);
  263. s->port = rport;
  264. return;
  265. error:
  266. char* strerr = strerror (errno);
  267. throw_on_sock_closed (this);
  268. throw new ::java::io::IOException (JvNewStringUTF (strerr));
  269. }
  270. // Close(shutdown) the socket.
  271. void
  272. gnu::java::net::PlainSocketImpl::close()
  273. {
  274. // Avoid races from asynchronous finalization.
  275. JvSynchronize sync (this);
  276. // Should we use shutdown here? Yes.
  277. // How would that effect so_linger? Uncertain.
  278. ::shutdown (native_fd, 2);
  279. // Ignore errors in shutdown as we are closing and all the same
  280. // errors are handled in the close.
  281. int res = _Jv_close (native_fd);
  282. if (res == -1)
  283. {
  284. // These three errors are not errors according to tests performed
  285. // on the reference implementation.
  286. if (errno != ENOTCONN && errno != ECONNRESET && errno != EBADF)
  287. throw new ::java::io::IOException (JvNewStringUTF (strerror (errno)));
  288. }
  289. // Safe place to reset the file pointer.
  290. native_fd = -1;
  291. timeout = 0;
  292. }
  293. static void
  294. write_helper (jint native_fd, jbyte *bytes, jint len);
  295. // Write a byte to the socket.
  296. void
  297. gnu::java::net::PlainSocketImpl$SocketOutputStream::write(jint b)
  298. {
  299. jbyte data = (jbyte) b;
  300. write_helper (this$0->native_fd, &data, 1);
  301. }
  302. // Write some bytes to the socket.
  303. void
  304. gnu::java::net::PlainSocketImpl$SocketOutputStream::write(jbyteArray b, jint offset, jint len)
  305. {
  306. if (! b)
  307. throw new ::java::lang::NullPointerException;
  308. if (offset < 0 || len < 0 || offset + len > JvGetArrayLength (b))
  309. throw new ::java::lang::ArrayIndexOutOfBoundsException;
  310. write_helper (this$0->native_fd, elements (b) + offset, len);
  311. }
  312. static void
  313. write_helper(jint native_fd, jbyte *bytes, jint len)
  314. {
  315. int written = 0;
  316. while (len > 0)
  317. {
  318. int r = _Jv_write (native_fd, bytes, len);
  319. if (r == -1)
  320. {
  321. if (::java::lang::Thread::interrupted())
  322. {
  323. ::java::io::InterruptedIOException *iioe
  324. = new ::java::io::InterruptedIOException
  325. (JvNewStringLatin1 (strerror (errno)));
  326. iioe->bytesTransferred = written;
  327. throw iioe;
  328. }
  329. // Some errors should not cause exceptions.
  330. if (errno != ENOTCONN && errno != ECONNRESET && errno != EBADF)
  331. throw new ::java::io::IOException (JvNewStringUTF (strerror (errno)));
  332. break;
  333. }
  334. written += r;
  335. len -= r;
  336. bytes += r;
  337. }
  338. }
  339. void
  340. gnu::java::net::PlainSocketImpl::sendUrgentData (jint)
  341. {
  342. throw new ::java::net::SocketException (JvNewStringLatin1 (
  343. "PlainSocketImpl: sending of urgent data not supported by this socket"));
  344. }
  345. static jint
  346. read_helper (gnu::java::net::PlainSocketImpl *soc_impl,
  347. jbyte *bytes, jint count);
  348. // Read a single byte from the socket.
  349. jint
  350. gnu::java::net::PlainSocketImpl$SocketInputStream::read(void)
  351. {
  352. jbyte data;
  353. if (read_helper (this$0, &data, 1) == 1)
  354. return data & 0xFF;
  355. return -1;
  356. }
  357. // Read count bytes into the buffer, starting at offset.
  358. jint
  359. gnu::java::net::PlainSocketImpl$SocketInputStream::read(jbyteArray buffer,
  360. jint offset,
  361. jint count)
  362. {
  363. if (! buffer)
  364. throw new ::java::lang::NullPointerException;
  365. jsize bsize = JvGetArrayLength (buffer);
  366. if (offset < 0 || count < 0 || offset + count > bsize)
  367. throw new ::java::lang::ArrayIndexOutOfBoundsException;
  368. return read_helper (this$0, elements (buffer) + offset, count);
  369. }
  370. static jint
  371. read_helper (gnu::java::net::PlainSocketImpl *soc_impl,
  372. jbyte *bytes, jint count)
  373. {
  374. // If zero bytes were requested, short circuit so that recv
  375. // doesn't signal EOF.
  376. if (count == 0)
  377. return 0;
  378. // Do timeouts via select.
  379. if (soc_impl->timeout > 0
  380. && soc_impl->native_fd >= 0
  381. && soc_impl->native_fd < FD_SETSIZE)
  382. {
  383. // Create the file descriptor set.
  384. fd_set read_fds;
  385. FD_ZERO (&read_fds);
  386. FD_SET (soc_impl->native_fd, &read_fds);
  387. // Create the timeout struct based on our internal timeout value.
  388. struct timeval timeout_value;
  389. timeout_value.tv_sec = soc_impl->timeout / 1000;
  390. timeout_value.tv_usec =(soc_impl->timeout % 1000) * 1000;
  391. // Select on the fds.
  392. int sel_retval =
  393. _Jv_select (soc_impl->native_fd + 1,
  394. &read_fds, NULL, NULL, &timeout_value);
  395. // We're only interested in the 0 return.
  396. // error returns still require us to try to read
  397. // the socket to see what happened.
  398. if (sel_retval == 0)
  399. {
  400. ::java::net::SocketTimeoutException *timeoutException =
  401. new ::java::net::SocketTimeoutException
  402. (JvNewStringUTF ("Read timed out"));
  403. throw timeoutException;
  404. }
  405. }
  406. // Read the socket.
  407. int r = ::recv (soc_impl->native_fd, (char *) bytes, count, 0);
  408. if (r == 0)
  409. {
  410. throw_on_sock_closed (soc_impl);
  411. return -1;
  412. }
  413. if (::java::lang::Thread::interrupted())
  414. {
  415. ::java::io::InterruptedIOException *iioe =
  416. new ::java::io::InterruptedIOException
  417. (JvNewStringUTF ("Read interrupted"));
  418. iioe->bytesTransferred = r == -1 ? 0 : r;
  419. throw iioe;
  420. }
  421. else if (r == -1)
  422. {
  423. throw_on_sock_closed (soc_impl);
  424. // Some errors cause us to return end of stream...
  425. if (errno == ENOTCONN)
  426. return -1;
  427. // Other errors need to be signalled.
  428. throw new ::java::io::IOException (JvNewStringUTF (strerror (errno)));
  429. }
  430. return r;
  431. }
  432. // How many bytes are available?
  433. jint
  434. gnu::java::net::PlainSocketImpl::available(void)
  435. {
  436. #if defined(FIONREAD) || defined(HAVE_SELECT)
  437. int num = 0;
  438. int r = 0;
  439. bool num_set = false;
  440. #if defined(FIONREAD)
  441. r = ::ioctl (native_fd, FIONREAD, &num);
  442. if (r == -1 && errno == ENOTTY)
  443. {
  444. // If the ioctl doesn't work, we don't care.
  445. r = 0;
  446. num = 0;
  447. }
  448. else
  449. num_set = true;
  450. #elif defined(HAVE_SELECT)
  451. if (native_fd < 0)
  452. {
  453. errno = EBADF;
  454. r = -1;
  455. }
  456. #endif
  457. if (r == -1)
  458. {
  459. posix_error:
  460. throw new ::java::io::IOException(JvNewStringUTF(strerror(errno)));
  461. }
  462. // If we didn't get anything we can use select.
  463. #if defined(HAVE_SELECT)
  464. if (! num_set)
  465. if (! num_set && native_fd >= 0 && native_fd < FD_SETSIZE)
  466. {
  467. fd_set rd;
  468. FD_ZERO (&rd);
  469. FD_SET (native_fd, &rd);
  470. struct timeval tv;
  471. tv.tv_sec = 0;
  472. tv.tv_usec = 0;
  473. r = _Jv_select (native_fd + 1, &rd, NULL, NULL, &tv);
  474. if(r == -1)
  475. goto posix_error;
  476. num = r == 0 ? 0 : 1;
  477. }
  478. #endif /* HAVE_SELECT */
  479. return (jint) num;
  480. #else
  481. throw new ::java::io::IOException (JvNewStringUTF ("unimplemented"));
  482. #endif
  483. }
  484. void
  485. gnu::java::net::PlainSocketImpl::setOption (jint optID, ::java::lang::Object *value)
  486. {
  487. int val;
  488. socklen_t val_len = sizeof (val);
  489. if (native_fd < 0)
  490. throw new ::java::net::SocketException (JvNewStringUTF ("Socket closed"));
  491. if (_Jv_IsInstanceOf (value, &::java::lang::Boolean::class$))
  492. {
  493. ::java::lang::Boolean *boolobj =
  494. static_cast< ::java::lang::Boolean *> (value);
  495. if (boolobj->booleanValue())
  496. val = 1;
  497. else
  498. {
  499. if (optID == _Jv_SO_LINGER_)
  500. val = -1;
  501. else
  502. val = 0;
  503. }
  504. }
  505. else if (_Jv_IsInstanceOf (value, &::java::lang::Integer::class$))
  506. {
  507. ::java::lang::Integer *intobj =
  508. static_cast< ::java::lang::Integer *> (value);
  509. val = (int) intobj->intValue();
  510. }
  511. else
  512. {
  513. throw new ::java::lang::IllegalArgumentException (
  514. JvNewStringLatin1 ("`value' must be Boolean or Integer"));
  515. }
  516. switch (optID)
  517. {
  518. case _Jv_TCP_NODELAY_ :
  519. #ifdef TCP_NODELAY
  520. if (::setsockopt (native_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &val,
  521. val_len) != 0)
  522. goto error;
  523. #else
  524. throw new ::java::lang::InternalError
  525. (JvNewStringUTF ("TCP_NODELAY not supported"));
  526. #endif /* TCP_NODELAY */
  527. return;
  528. case _Jv_SO_KEEPALIVE_ :
  529. if (::setsockopt (native_fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &val,
  530. val_len) != 0)
  531. goto error;
  532. return;
  533. case _Jv_SO_BROADCAST_ :
  534. throw new ::java::net::SocketException
  535. (JvNewStringUTF ("SO_BROADCAST not valid for TCP"));
  536. return;
  537. case _Jv_SO_OOBINLINE_ :
  538. if (::setsockopt (native_fd, SOL_SOCKET, SO_OOBINLINE, (char *) &val,
  539. val_len) != 0)
  540. goto error;
  541. return;
  542. case _Jv_SO_LINGER_ :
  543. #ifdef SO_LINGER
  544. struct linger l_val;
  545. l_val.l_onoff = (val != -1);
  546. l_val.l_linger = val;
  547. if (::setsockopt (native_fd, SOL_SOCKET, SO_LINGER, (char *) &l_val,
  548. sizeof(l_val)) != 0)
  549. goto error;
  550. #else
  551. throw new ::java::lang::InternalError (
  552. JvNewStringUTF ("SO_LINGER not supported"));
  553. #endif /* SO_LINGER */
  554. return;
  555. case _Jv_SO_SNDBUF_ :
  556. case _Jv_SO_RCVBUF_ :
  557. #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
  558. int opt;
  559. optID == _Jv_SO_SNDBUF_ ? opt = SO_SNDBUF : opt = SO_RCVBUF;
  560. if (::setsockopt (native_fd, SOL_SOCKET, opt, (char *) &val, val_len) != 0)
  561. goto error;
  562. #else
  563. throw new ::java::lang::InternalError (
  564. JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported"));
  565. #endif
  566. return;
  567. case _Jv_SO_BINDADDR_ :
  568. throw new ::java::net::SocketException (
  569. JvNewStringUTF ("SO_BINDADDR: read only option"));
  570. return;
  571. case _Jv_IP_MULTICAST_IF_ :
  572. throw new ::java::net::SocketException (
  573. JvNewStringUTF ("IP_MULTICAST_IF: not valid for TCP"));
  574. return;
  575. case _Jv_IP_MULTICAST_IF2_ :
  576. throw new ::java::net::SocketException (
  577. JvNewStringUTF ("IP_MULTICAST_IF2: not valid for TCP"));
  578. return;
  579. case _Jv_IP_MULTICAST_LOOP_ :
  580. throw new ::java::net::SocketException (
  581. JvNewStringUTF ("IP_MULTICAST_LOOP: not valid for TCP"));
  582. return;
  583. case _Jv_IP_TOS_ :
  584. if (::setsockopt (native_fd, SOL_SOCKET, IP_TOS, (char *) &val,
  585. val_len) != 0)
  586. goto error;
  587. return;
  588. case _Jv_SO_REUSEADDR_ :
  589. #if defined(SO_REUSEADDR)
  590. if (::setsockopt (native_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &val,
  591. val_len) != 0)
  592. goto error;
  593. return;
  594. #else
  595. throw new ::java::lang::InternalError (
  596. JvNewStringUTF ("SO_REUSEADDR not supported"));
  597. #endif
  598. case _Jv_SO_TIMEOUT_ :
  599. timeout = val;
  600. return;
  601. default :
  602. errno = ENOPROTOOPT;
  603. }
  604. error:
  605. char* strerr = strerror (errno);
  606. throw new ::java::net::SocketException (JvNewStringUTF (strerr));
  607. }
  608. ::java::lang::Object *
  609. gnu::java::net::PlainSocketImpl::getOption (jint optID)
  610. {
  611. int val;
  612. socklen_t val_len = sizeof(val);
  613. union SockAddr u;
  614. socklen_t addrlen = sizeof(u);
  615. struct linger l_val;
  616. socklen_t l_val_len = sizeof(l_val);
  617. switch (optID)
  618. {
  619. #ifdef TCP_NODELAY
  620. case _Jv_TCP_NODELAY_ :
  621. if (::getsockopt (native_fd, IPPROTO_TCP, TCP_NODELAY, (char *) &val,
  622. &val_len) != 0)
  623. goto error;
  624. else
  625. return new ::java::lang::Boolean (val != 0);
  626. #else
  627. throw new ::java::lang::InternalError
  628. (JvNewStringUTF ("TCP_NODELAY not supported"));
  629. #endif
  630. break;
  631. case _Jv_SO_LINGER_ :
  632. #ifdef SO_LINGER
  633. if (::getsockopt (native_fd, SOL_SOCKET, SO_LINGER, (char *) &l_val,
  634. &l_val_len) != 0)
  635. goto error;
  636. if (l_val.l_onoff)
  637. return new ::java::lang::Integer (l_val.l_linger);
  638. else
  639. return new ::java::lang::Boolean ((jboolean)false);
  640. #else
  641. throw new ::java::lang::InternalError
  642. (JvNewStringUTF ("SO_LINGER not supported"));
  643. #endif
  644. break;
  645. case _Jv_SO_KEEPALIVE_ :
  646. if (::getsockopt (native_fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &val,
  647. &val_len) != 0)
  648. goto error;
  649. else
  650. return new ::java::lang::Boolean (val != 0);
  651. case _Jv_SO_BROADCAST_ :
  652. if (::getsockopt (native_fd, SOL_SOCKET, SO_BROADCAST, (char *) &val,
  653. &val_len) != 0)
  654. goto error;
  655. return new ::java::lang::Boolean ((jboolean)val);
  656. case _Jv_SO_OOBINLINE_ :
  657. if (::getsockopt (native_fd, SOL_SOCKET, SO_OOBINLINE, (char *) &val,
  658. &val_len) != 0)
  659. goto error;
  660. return new ::java::lang::Boolean ((jboolean)val);
  661. case _Jv_SO_RCVBUF_ :
  662. case _Jv_SO_SNDBUF_ :
  663. #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
  664. int opt;
  665. optID == _Jv_SO_SNDBUF_ ? opt = SO_SNDBUF : opt = SO_RCVBUF;
  666. if (::getsockopt (native_fd, SOL_SOCKET, opt, (char *) &val, &val_len) != 0)
  667. goto error;
  668. else
  669. return new ::java::lang::Integer (val);
  670. #else
  671. throw new ::java::lang::InternalError
  672. (JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported"));
  673. #endif
  674. break;
  675. case _Jv_SO_BINDADDR_:
  676. // cache the local address
  677. if (localAddress == NULL)
  678. {
  679. jbyteArray laddr;
  680. if (::getsockname (native_fd, (sockaddr*) &u, &addrlen) != 0)
  681. goto error;
  682. if (u.address.sin_family == AF_INET)
  683. {
  684. laddr = JvNewByteArray (4);
  685. memcpy (elements (laddr), &u.address.sin_addr, 4);
  686. }
  687. #ifdef HAVE_INET6
  688. else if (u.address.sin_family == AF_INET6)
  689. {
  690. laddr = JvNewByteArray (16);
  691. memcpy (elements (laddr), &u.address6.sin6_addr, 16);
  692. }
  693. #endif
  694. else
  695. throw new ::java::net::SocketException
  696. (JvNewStringUTF ("invalid family"));
  697. localAddress = ::java::net::InetAddress::getByAddress (laddr);
  698. }
  699. return localAddress;
  700. break;
  701. case _Jv_IP_MULTICAST_IF_ :
  702. throw new ::java::net::SocketException
  703. (JvNewStringUTF ("IP_MULTICAST_IF: not valid for TCP"));
  704. break;
  705. case _Jv_IP_MULTICAST_IF2_ :
  706. throw new ::java::net::SocketException
  707. (JvNewStringUTF ("IP_MULTICAST_IF2: not valid for TCP"));
  708. break;
  709. case _Jv_IP_MULTICAST_LOOP_ :
  710. throw new ::java::net::SocketException
  711. (JvNewStringUTF ("IP_MULTICAST_LOOP: not valid for TCP"));
  712. break;
  713. case _Jv_IP_TOS_ :
  714. if (::getsockopt (native_fd, SOL_SOCKET, IP_TOS, (char *) &val,
  715. &val_len) != 0)
  716. goto error;
  717. return new ::java::lang::Integer (val);
  718. break;
  719. case _Jv_SO_REUSEADDR_ :
  720. #if defined(SO_REUSEADDR)
  721. if (::getsockopt (native_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &val,
  722. &val_len) != 0)
  723. goto error;
  724. #else
  725. throw new ::java::lang::InternalError (
  726. JvNewStringUTF ("SO_REUSEADDR not supported"));
  727. #endif
  728. break;
  729. case _Jv_SO_TIMEOUT_ :
  730. return new ::java::lang::Integer (timeout);
  731. break;
  732. default :
  733. errno = ENOPROTOOPT;
  734. }
  735. error:
  736. char* strerr = strerror (errno);
  737. throw new ::java::net::SocketException (JvNewStringUTF (strerr));
  738. }
  739. void
  740. gnu::java::net::PlainSocketImpl::shutdownInput (void)
  741. {
  742. if (::shutdown (native_fd, 0))
  743. throw new ::java::net::SocketException (JvNewStringUTF (strerror (errno)));
  744. }
  745. void
  746. gnu::java::net::PlainSocketImpl::shutdownOutput (void)
  747. {
  748. if (::shutdown (native_fd, 1))
  749. throw new ::java::net::SocketException (JvNewStringUTF (strerror (errno)));
  750. }