PageRenderTime 110ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/libjava/gnu/java/net/natPlainDatagramSocketImplPosix.cc

https://bitbucket.org/pizzafactory/pf-gcc
C++ | 858 lines | 716 code | 97 blank | 45 comment | 154 complexity | 90f650c4dbabbaa9f9ca0da00db590f3 MD5 | raw file
  1. /* Copyright (C) 2003, 2005, 2006 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_NETINET_IN_H
  9. #include <netinet/in.h>
  10. #endif
  11. #ifdef HAVE_ARPA_INET_H
  12. #include <arpa/inet.h>
  13. #endif
  14. #include <errno.h>
  15. #include <string.h>
  16. #if HAVE_BSTRING_H
  17. // Needed for bzero, implicitly used by FD_ZERO on IRIX 5.2
  18. #include <bstring.h>
  19. #endif
  20. #include <gcj/cni.h>
  21. #include <gnu/java/net/PlainDatagramSocketImpl.h>
  22. #include <java/io/IOException.h>
  23. #include <java/io/InterruptedIOException.h>
  24. #include <java/net/BindException.h>
  25. #include <java/net/SocketException.h>
  26. #include <java/net/SocketTimeoutException.h>
  27. #include <java/net/InetAddress.h>
  28. #include <java/net/NetworkInterface.h>
  29. #include <java/net/DatagramPacket.h>
  30. #include <java/net/PortUnreachableException.h>
  31. #include <java/lang/InternalError.h>
  32. #include <java/lang/Object.h>
  33. #include <java/lang/Boolean.h>
  34. #include <java/lang/Integer.h>
  35. #include <java/net/UnknownHostException.h>
  36. #include <java/net/ConnectException.h>
  37. #include <java/lang/NullPointerException.h>
  38. union SockAddr
  39. {
  40. struct sockaddr_in address;
  41. #ifdef HAVE_INET6
  42. struct sockaddr_in6 address6;
  43. #endif
  44. };
  45. union McastReq
  46. {
  47. #if HAVE_STRUCT_IP_MREQ
  48. struct ip_mreq mreq;
  49. #endif
  50. #if HAVE_STRUCT_IPV6_MREQ
  51. struct ipv6_mreq mreq6;
  52. #endif
  53. };
  54. union InAddr
  55. {
  56. struct in_addr addr;
  57. #ifdef HAVE_INET6
  58. struct in6_addr addr6;
  59. #endif
  60. };
  61. // FIXME: routines here and/or in natPlainSocketImpl.cc could throw
  62. // NoRouteToHostException; also consider UnknownHostException, ConnectException.
  63. void
  64. gnu::java::net::PlainDatagramSocketImpl::create ()
  65. {
  66. int sock = _Jv_socket (AF_INET, SOCK_DGRAM, 0);
  67. if (sock < 0)
  68. {
  69. char* strerr = strerror (errno);
  70. throw new ::java::net::SocketException (JvNewStringUTF (strerr));
  71. }
  72. // We use native_fd in place of fd here. From leaving fd null we avoid
  73. // the double close problem in FileDescriptor.finalize.
  74. native_fd = sock;
  75. }
  76. void
  77. gnu::java::net::PlainDatagramSocketImpl::bind (jint lport,
  78. ::java::net::InetAddress *host)
  79. {
  80. union SockAddr u;
  81. struct sockaddr *ptr = (struct sockaddr *) &u.address;
  82. // FIXME: Use getaddrinfo() to get actual protocol instead of assuming ipv4.
  83. jbyteArray haddress = host->addr;
  84. jbyte *bytes = elements (haddress);
  85. int len = haddress->length;
  86. if (len == 4)
  87. {
  88. u.address.sin_family = AF_INET;
  89. if (host != NULL)
  90. memcpy (&u.address.sin_addr, bytes, len);
  91. else
  92. u.address.sin_addr.s_addr = htonl (INADDR_ANY);
  93. len = sizeof (struct sockaddr_in);
  94. u.address.sin_port = htons (lport);
  95. }
  96. #ifdef HAVE_INET6
  97. else if (len == 16)
  98. {
  99. u.address6.sin6_family = AF_INET6;
  100. memcpy (&u.address6.sin6_addr, bytes, len);
  101. len = sizeof (struct sockaddr_in6);
  102. u.address6.sin6_port = htons (lport);
  103. }
  104. #endif
  105. else
  106. throw new ::java::net::SocketException (JvNewStringUTF ("invalid length"));
  107. if (_Jv_bind (native_fd, ptr, len) == 0)
  108. {
  109. socklen_t addrlen = sizeof(u);
  110. if (lport != 0)
  111. localPort = lport;
  112. else if (::getsockname (native_fd, (sockaddr*) &u, &addrlen) == 0)
  113. localPort = ntohs (u.address.sin_port);
  114. else
  115. goto error;
  116. /* Allow broadcast by default. */
  117. int broadcast = 1;
  118. if (::setsockopt (native_fd, SOL_SOCKET, SO_BROADCAST, (char *) &broadcast,
  119. sizeof (broadcast)) != 0)
  120. goto error;
  121. return;
  122. }
  123. error:
  124. char* strerr = strerror (errno);
  125. throw new ::java::net::BindException (JvNewStringUTF (strerr));
  126. }
  127. void
  128. gnu::java::net::PlainDatagramSocketImpl::connect (::java::net::InetAddress *host,
  129. jint rport)
  130. {
  131. if (! host)
  132. throw new ::java::lang::NullPointerException;
  133. union SockAddr u;
  134. jbyteArray haddress = host->addr;
  135. jbyte *bytes = elements (haddress);
  136. int len = haddress->length;
  137. struct sockaddr *ptr = (struct sockaddr *) &u.address;
  138. if (len == 4)
  139. {
  140. u.address.sin_family = AF_INET;
  141. memcpy (&u.address.sin_addr, bytes, len);
  142. len = sizeof (struct sockaddr_in);
  143. u.address.sin_port = htons (rport);
  144. }
  145. #ifdef HAVE_INET6
  146. else if (len == 16)
  147. {
  148. u.address6.sin6_family = AF_INET6;
  149. memcpy (&u.address6.sin6_addr, bytes, len);
  150. len = sizeof (struct sockaddr_in6);
  151. u.address6.sin6_port = htons (rport);
  152. }
  153. #endif
  154. else
  155. throw new ::java::net::SocketException (JvNewStringUTF ("invalid length"));
  156. if (_Jv_connect (native_fd, ptr, len) == 0)
  157. return;
  158. char* strerr = strerror (errno);
  159. throw new ::java::net::ConnectException (JvNewStringUTF (strerr));
  160. }
  161. void
  162. gnu::java::net::PlainDatagramSocketImpl::disconnect ()
  163. {
  164. struct sockaddr addr;
  165. addr.sa_family = AF_UNSPEC;
  166. // Ignore errors. This is lame but apparently required.
  167. _Jv_connect (native_fd, &addr, sizeof (addr));
  168. }
  169. jint
  170. gnu::java::net::PlainDatagramSocketImpl::peek (::java::net::InetAddress *i)
  171. {
  172. // FIXME: Deal with Multicast and if the socket is connected.
  173. union SockAddr u;
  174. socklen_t addrlen = sizeof(u);
  175. ssize_t retlen =
  176. ::recvfrom (native_fd, (char *) NULL, 0, MSG_PEEK, (sockaddr*) &u,
  177. &addrlen);
  178. if (retlen < 0)
  179. goto error;
  180. // FIXME: Deal with Multicast addressing and if the socket is connected.
  181. jbyteArray raddr;
  182. jint rport;
  183. if (u.address.sin_family == AF_INET)
  184. {
  185. raddr = JvNewByteArray (4);
  186. memcpy (elements (raddr), &u.address.sin_addr, 4);
  187. rport = ntohs (u.address.sin_port);
  188. }
  189. #ifdef HAVE_INET6
  190. else if (u.address.sin_family == AF_INET6)
  191. {
  192. raddr = JvNewByteArray (16);
  193. memcpy (elements (raddr), &u.address6.sin6_addr, 16);
  194. rport = ntohs (u.address6.sin6_port);
  195. }
  196. #endif
  197. else
  198. throw new ::java::net::SocketException (JvNewStringUTF ("invalid family"));
  199. i->addr = raddr;
  200. return rport;
  201. error:
  202. char* strerr = strerror (errno);
  203. if (errno == ECONNREFUSED)
  204. throw new ::java::net::PortUnreachableException (JvNewStringUTF (strerr));
  205. throw new ::java::io::IOException (JvNewStringUTF (strerr));
  206. }
  207. jint
  208. gnu::java::net::PlainDatagramSocketImpl::peekData (::java::net::DatagramPacket *p)
  209. {
  210. // FIXME: Deal with Multicast and if the socket is connected.
  211. union SockAddr u;
  212. socklen_t addrlen = sizeof(u);
  213. jbyte *dbytes = elements (p->getData()) + p->getOffset();
  214. jint maxlen = p->maxlen - p->getOffset();
  215. ssize_t retlen = 0;
  216. // Do timeouts via select since SO_RCVTIMEO is not always available.
  217. if (timeout > 0 && native_fd >= 0 && native_fd < FD_SETSIZE)
  218. {
  219. fd_set rset;
  220. struct timeval tv;
  221. FD_ZERO(&rset);
  222. FD_SET(native_fd, &rset);
  223. tv.tv_sec = timeout / 1000;
  224. tv.tv_usec = (timeout % 1000) * 1000;
  225. int retval;
  226. if ((retval = _Jv_select (native_fd + 1, &rset, NULL, NULL, &tv)) < 0)
  227. goto error;
  228. else if (retval == 0)
  229. throw new ::java::net::SocketTimeoutException
  230. (JvNewStringUTF ("PeekData timed out") );
  231. }
  232. retlen =
  233. ::recvfrom (native_fd, (char *) dbytes, maxlen, MSG_PEEK, (sockaddr*) &u,
  234. &addrlen);
  235. if (retlen < 0)
  236. goto error;
  237. // FIXME: Deal with Multicast addressing and if the socket is connected.
  238. jbyteArray raddr;
  239. jint rport;
  240. if (u.address.sin_family == AF_INET)
  241. {
  242. raddr = JvNewByteArray (4);
  243. memcpy (elements (raddr), &u.address.sin_addr, 4);
  244. rport = ntohs (u.address.sin_port);
  245. }
  246. #ifdef HAVE_INET6
  247. else if (u.address.sin_family == AF_INET6)
  248. {
  249. raddr = JvNewByteArray (16);
  250. memcpy (elements (raddr), &u.address6.sin6_addr, 16);
  251. rport = ntohs (u.address6.sin6_port);
  252. }
  253. #endif
  254. else
  255. throw new ::java::net::SocketException (JvNewStringUTF ("invalid family"));
  256. p->setAddress (::java::net::InetAddress::getByAddress (raddr));
  257. p->setPort (rport);
  258. p->length = (int) retlen;
  259. return rport;
  260. error:
  261. char* strerr = strerror (errno);
  262. if (errno == ECONNREFUSED)
  263. throw new ::java::net::PortUnreachableException (JvNewStringUTF (strerr));
  264. throw new ::java::io::IOException (JvNewStringUTF (strerr));
  265. }
  266. // Close(shutdown) the socket.
  267. void
  268. gnu::java::net::PlainDatagramSocketImpl::close ()
  269. {
  270. // Avoid races from asynchronous finalization.
  271. JvSynchronize sync (this);
  272. // The method isn't declared to throw anything, so we disregard
  273. // the return value.
  274. _Jv_close (native_fd);
  275. native_fd = -1;
  276. timeout = 0;
  277. }
  278. void
  279. gnu::java::net::PlainDatagramSocketImpl::send (::java::net::DatagramPacket *p)
  280. {
  281. JvSynchronize lock (SEND_LOCK);
  282. // FIXME: Deal with Multicast.
  283. ::java::net::InetAddress *host = p->getAddress();
  284. if (host == NULL)
  285. {
  286. // If there is no host, maybe this socket was connected, in
  287. // which case we try a plain send().
  288. jbyte *dbytes = elements (p->getData()) + p->getOffset();
  289. if (::send (native_fd, (char *) dbytes, p->getLength(), 0) >= 0)
  290. return;
  291. }
  292. else
  293. {
  294. jint rport = p->getPort();
  295. union SockAddr u;
  296. jbyteArray haddress = host->addr;
  297. jbyte *bytes = elements (haddress);
  298. int len = haddress->length;
  299. struct sockaddr *ptr = (struct sockaddr *) &u.address;
  300. jbyte *dbytes = elements (p->getData()) + p->getOffset();
  301. if (len == 4)
  302. {
  303. u.address.sin_family = AF_INET;
  304. memcpy (&u.address.sin_addr, bytes, len);
  305. len = sizeof (struct sockaddr_in);
  306. u.address.sin_port = htons (rport);
  307. }
  308. #ifdef HAVE_INET6
  309. else if (len == 16)
  310. {
  311. u.address6.sin6_family = AF_INET6;
  312. memcpy (&u.address6.sin6_addr, bytes, len);
  313. len = sizeof (struct sockaddr_in6);
  314. u.address6.sin6_port = htons (rport);
  315. }
  316. #endif
  317. else
  318. throw new ::java::net::SocketException (JvNewStringUTF ("invalid length"));
  319. if (::sendto (native_fd, (char *) dbytes, p->getLength(), 0, ptr, len)
  320. >= 0)
  321. return;
  322. }
  323. char* strerr = strerror (errno);
  324. if (errno == ECONNREFUSED)
  325. throw new ::java::net::PortUnreachableException (JvNewStringUTF (strerr));
  326. throw new ::java::io::IOException (JvNewStringUTF (strerr));
  327. }
  328. void
  329. gnu::java::net::PlainDatagramSocketImpl::receive (::java::net::DatagramPacket *p)
  330. {
  331. JvSynchronize lock (RECEIVE_LOCK);
  332. // FIXME: Deal with Multicast and if the socket is connected.
  333. union SockAddr u;
  334. socklen_t addrlen = sizeof(u);
  335. jbyte *dbytes = elements (p->getData()) + p->getOffset();
  336. jint maxlen = p->maxlen - p->getOffset();
  337. ssize_t retlen = 0;
  338. // Do timeouts via select since SO_RCVTIMEO is not always available.
  339. if (timeout > 0 && native_fd >= 0 && native_fd < FD_SETSIZE)
  340. {
  341. fd_set rset;
  342. struct timeval tv;
  343. FD_ZERO(&rset);
  344. FD_SET(native_fd, &rset);
  345. tv.tv_sec = timeout / 1000;
  346. tv.tv_usec = (timeout % 1000) * 1000;
  347. int retval;
  348. if ((retval = _Jv_select (native_fd + 1, &rset, NULL, NULL, &tv)) < 0)
  349. goto error;
  350. else if (retval == 0)
  351. throw new ::java::net::SocketTimeoutException
  352. (JvNewStringUTF ("Receive timed out") );
  353. }
  354. retlen =
  355. ::recvfrom (native_fd, (char *) dbytes, maxlen, 0, (sockaddr*) &u,
  356. &addrlen);
  357. if (retlen < 0)
  358. goto error;
  359. // FIXME: Deal with Multicast addressing and if the socket is connected.
  360. jbyteArray raddr;
  361. jint rport;
  362. if (u.address.sin_family == AF_INET)
  363. {
  364. raddr = JvNewByteArray (4);
  365. memcpy (elements (raddr), &u.address.sin_addr, 4);
  366. rport = ntohs (u.address.sin_port);
  367. }
  368. #ifdef HAVE_INET6
  369. else if (u.address.sin_family == AF_INET6)
  370. {
  371. raddr = JvNewByteArray (16);
  372. memcpy (elements (raddr), &u.address6.sin6_addr, 16);
  373. rport = ntohs (u.address6.sin6_port);
  374. }
  375. #endif
  376. else
  377. throw new ::java::net::SocketException (JvNewStringUTF ("invalid family"));
  378. p->setAddress (::java::net::InetAddress::getByAddress (raddr));
  379. p->setPort (rport);
  380. p->length = (jint) retlen;
  381. return;
  382. error:
  383. char* strerr = strerror (errno);
  384. if (errno == ECONNREFUSED)
  385. throw new ::java::net::PortUnreachableException (JvNewStringUTF (strerr));
  386. throw new ::java::io::IOException (JvNewStringUTF (strerr));
  387. }
  388. void
  389. gnu::java::net::PlainDatagramSocketImpl::setTimeToLive (jint ttl)
  390. {
  391. // Assumes IPPROTO_IP rather than IPPROTO_IPV6 since socket created is IPv4.
  392. char val = (char) ttl;
  393. socklen_t val_len = sizeof(val);
  394. if (::setsockopt (native_fd, IPPROTO_IP, IP_MULTICAST_TTL, &val, val_len) == 0)
  395. return;
  396. char* strerr = strerror (errno);
  397. throw new ::java::io::IOException (JvNewStringUTF (strerr));
  398. }
  399. jint
  400. gnu::java::net::PlainDatagramSocketImpl::getTimeToLive ()
  401. {
  402. // Assumes IPPROTO_IP rather than IPPROTO_IPV6 since socket created is IPv4.
  403. char val;
  404. socklen_t val_len = sizeof(val);
  405. if (::getsockopt (native_fd, IPPROTO_IP, IP_MULTICAST_TTL, &val, &val_len) == 0)
  406. return ((int) val) & 0xFF;
  407. char* strerr = strerror (errno);
  408. throw new ::java::io::IOException (JvNewStringUTF (strerr));
  409. }
  410. void
  411. gnu::java::net::PlainDatagramSocketImpl::mcastGrp (::java::net::InetAddress *inetaddr,
  412. ::java::net::NetworkInterface *,
  413. jboolean join)
  414. {
  415. // FIXME: implement use of NetworkInterface
  416. jbyteArray haddress = inetaddr->addr;
  417. #if HAVE_STRUCT_IP_MREQ || HAVE_STRUCT_IPV6_MREQ
  418. union McastReq u;
  419. jbyte *bytes = elements (haddress);
  420. #endif
  421. int len = haddress->length;
  422. int level, opname;
  423. const char *ptr;
  424. if (0)
  425. ;
  426. #if HAVE_STRUCT_IP_MREQ
  427. else if (len == 4)
  428. {
  429. level = IPPROTO_IP;
  430. opname = join ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP;
  431. memcpy (&u.mreq.imr_multiaddr, bytes, len);
  432. // FIXME: If a non-default interface is set, use it; see Stevens p. 501.
  433. // Maybe not, see note in last paragraph at bottom of Stevens p. 497.
  434. u.mreq.imr_interface.s_addr = htonl (INADDR_ANY);
  435. len = sizeof (struct ip_mreq);
  436. ptr = (const char *) &u.mreq;
  437. }
  438. #endif
  439. #if HAVE_STRUCT_IPV6_MREQ
  440. else if (len == 16)
  441. {
  442. level = IPPROTO_IPV6;
  443. /* Prefer new RFC 2553 names. */
  444. #ifndef IPV6_JOIN_GROUP
  445. #define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
  446. #endif
  447. #ifndef IPV6_LEAVE_GROUP
  448. #define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
  449. #endif
  450. opname = join ? IPV6_JOIN_GROUP : IPV6_LEAVE_GROUP;
  451. memcpy (&u.mreq6.ipv6mr_multiaddr, bytes, len);
  452. // FIXME: If a non-default interface is set, use it; see Stevens p. 501.
  453. // Maybe not, see note in last paragraph at bottom of Stevens p. 497.
  454. u.mreq6.ipv6mr_interface = 0;
  455. len = sizeof (struct ipv6_mreq);
  456. ptr = (const char *) &u.mreq6;
  457. }
  458. #endif
  459. else
  460. throw new ::java::net::SocketException (JvNewStringUTF ("invalid length"));
  461. if (::setsockopt (native_fd, level, opname, ptr, len) == 0)
  462. return;
  463. char* strerr = strerror (errno);
  464. throw new ::java::io::IOException (JvNewStringUTF (strerr));
  465. }
  466. // Helper function to get the InetAddress for a given socket (file
  467. // descriptor).
  468. static ::java::net::InetAddress *
  469. getLocalAddress (int native_fd)
  470. {
  471. jbyteArray laddr;
  472. union SockAddr u;
  473. socklen_t addrlen = sizeof(u);
  474. if (::getsockname (native_fd, (sockaddr*) &u, &addrlen) != 0)
  475. {
  476. char* strerr = strerror (errno);
  477. throw new ::java::net::SocketException (JvNewStringUTF (strerr));
  478. }
  479. if (u.address.sin_family == AF_INET)
  480. {
  481. laddr = JvNewByteArray (4);
  482. memcpy (elements (laddr), &u.address.sin_addr, 4);
  483. }
  484. #ifdef HAVE_INET6
  485. else if (u.address.sin_family == AF_INET6)
  486. {
  487. laddr = JvNewByteArray (16);
  488. memcpy (elements (laddr), &u.address6.sin6_addr, 16);
  489. }
  490. #endif
  491. else
  492. throw new ::java::net::SocketException (JvNewStringUTF ("invalid family"));
  493. return ::java::net::InetAddress::getByAddress (laddr);
  494. }
  495. void
  496. gnu::java::net::PlainDatagramSocketImpl::setOption (jint optID,
  497. ::java::lang::Object *value)
  498. {
  499. int val;
  500. socklen_t val_len = sizeof (val);
  501. if (native_fd < 0)
  502. throw new ::java::net::SocketException (JvNewStringUTF ("Socket closed"));
  503. if (_Jv_IsInstanceOf (value, &::java::lang::Boolean::class$))
  504. {
  505. ::java::lang::Boolean *boolobj =
  506. static_cast< ::java::lang::Boolean *> (value);
  507. val = boolobj->booleanValue() ? 1 : 0;
  508. }
  509. else if (_Jv_IsInstanceOf (value, &::java::lang::Integer::class$))
  510. {
  511. ::java::lang::Integer *intobj =
  512. static_cast< ::java::lang::Integer *> (value);
  513. val = (int) intobj->intValue();
  514. }
  515. // Else assume value to be an InetAddress for use with IP_MULTICAST_IF.
  516. switch (optID)
  517. {
  518. case _Jv_TCP_NODELAY_ :
  519. throw new ::java::net::SocketException (
  520. JvNewStringUTF ("TCP_NODELAY not valid for UDP"));
  521. return;
  522. case _Jv_SO_LINGER_ :
  523. throw new ::java::net::SocketException (
  524. JvNewStringUTF ("SO_LINGER not valid for UDP"));
  525. return;
  526. case _Jv_SO_KEEPALIVE_ :
  527. throw new ::java::net::SocketException (
  528. JvNewStringUTF ("SO_KEEPALIVE not valid for UDP"));
  529. return;
  530. case _Jv_SO_BROADCAST_ :
  531. if (::setsockopt (native_fd, SOL_SOCKET, SO_BROADCAST, (char *) &val,
  532. val_len) != 0)
  533. goto error;
  534. return;
  535. case _Jv_SO_OOBINLINE_ :
  536. throw new ::java::net::SocketException (
  537. JvNewStringUTF ("SO_OOBINLINE: not valid for UDP"));
  538. return;
  539. case _Jv_SO_SNDBUF_ :
  540. case _Jv_SO_RCVBUF_ :
  541. #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
  542. int opt;
  543. optID == _Jv_SO_SNDBUF_ ? opt = SO_SNDBUF : opt = SO_RCVBUF;
  544. if (::setsockopt (native_fd, SOL_SOCKET, opt, (char *) &val, val_len) != 0)
  545. goto error;
  546. #else
  547. throw new ::java::lang::InternalError (
  548. JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported"));
  549. #endif
  550. return;
  551. case _Jv_SO_REUSEADDR_ :
  552. #if defined(SO_REUSEADDR)
  553. if (::setsockopt (native_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &val,
  554. val_len) != 0)
  555. goto error;
  556. #else
  557. throw new ::java::lang::InternalError (
  558. JvNewStringUTF ("SO_REUSEADDR not supported"));
  559. #endif
  560. return;
  561. case _Jv_SO_BINDADDR_ :
  562. throw new ::java::net::SocketException (
  563. JvNewStringUTF ("SO_BINDADDR: read only option"));
  564. return;
  565. case _Jv_IP_MULTICAST_IF_ :
  566. union InAddr u;
  567. jbyteArray haddress;
  568. jbyte *bytes;
  569. int len;
  570. int level, opname;
  571. const char *ptr;
  572. haddress = ((::java::net::InetAddress *) value)->addr;
  573. bytes = elements (haddress);
  574. len = haddress->length;
  575. if (len == 4)
  576. {
  577. level = IPPROTO_IP;
  578. opname = IP_MULTICAST_IF;
  579. memcpy (&u.addr, bytes, len);
  580. len = sizeof (struct in_addr);
  581. ptr = (const char *) &u.addr;
  582. }
  583. // Tru64 UNIX V5.0 has struct sockaddr_in6, but no IPV6_MULTICAST_IF
  584. #if defined (HAVE_INET6) && defined (IPV6_MULTICAST_IF)
  585. else if (len == 16)
  586. {
  587. level = IPPROTO_IPV6;
  588. opname = IPV6_MULTICAST_IF;
  589. memcpy (&u.addr6, bytes, len);
  590. len = sizeof (struct in6_addr);
  591. ptr = (const char *) &u.addr6;
  592. }
  593. #endif
  594. else
  595. throw
  596. new ::java::net::SocketException (JvNewStringUTF ("invalid length"));
  597. if (::setsockopt (native_fd, level, opname, ptr, len) != 0)
  598. goto error;
  599. return;
  600. case _Jv_IP_MULTICAST_IF2_ :
  601. throw new ::java::net::SocketException (
  602. JvNewStringUTF ("IP_MULTICAST_IF2: not yet implemented"));
  603. return;
  604. case _Jv_IP_MULTICAST_LOOP_ :
  605. // cache the local address
  606. if (localAddress == NULL)
  607. localAddress = getLocalAddress (native_fd);
  608. len = localAddress->addr->length;
  609. if (len == 4)
  610. {
  611. level = IPPROTO_IP;
  612. opname = IP_MULTICAST_LOOP;
  613. }
  614. #if defined (HAVE_INET6) && defined (IPV6_MULTICAST_LOOP)
  615. else if (len == 16)
  616. {
  617. level = IPPROTO_IPV6;
  618. opname = IPV6_MULTICAST_LOOP;
  619. }
  620. #endif
  621. else
  622. throw
  623. new ::java::net::SocketException (JvNewStringUTF ("invalid address length"));
  624. if (::setsockopt (native_fd, level, opname, (char *) &val,
  625. val_len) != 0)
  626. goto error;
  627. return;
  628. case _Jv_IP_TOS_ :
  629. if (::setsockopt (native_fd, SOL_SOCKET, IP_TOS, (char *) &val,
  630. val_len) != 0)
  631. goto error;
  632. return;
  633. case _Jv_SO_TIMEOUT_ :
  634. timeout = val;
  635. return;
  636. default :
  637. errno = ENOPROTOOPT;
  638. }
  639. error:
  640. char* strerr = strerror (errno);
  641. throw new ::java::net::SocketException (JvNewStringUTF (strerr));
  642. }
  643. ::java::lang::Object *
  644. gnu::java::net::PlainDatagramSocketImpl::getOption (jint optID)
  645. {
  646. int val;
  647. socklen_t val_len = sizeof(val);
  648. int level, opname;
  649. switch (optID)
  650. {
  651. case _Jv_TCP_NODELAY_ :
  652. throw new ::java::net::SocketException (
  653. JvNewStringUTF ("TCP_NODELAY not valid for UDP"));
  654. break;
  655. case _Jv_SO_LINGER_ :
  656. throw new ::java::net::SocketException (
  657. JvNewStringUTF ("SO_LINGER not valid for UDP"));
  658. break;
  659. case _Jv_SO_KEEPALIVE_ :
  660. throw new ::java::net::SocketException (
  661. JvNewStringUTF ("SO_KEEPALIVE not valid for UDP"));
  662. break;
  663. case _Jv_SO_BROADCAST_ :
  664. if (::getsockopt (native_fd, SOL_SOCKET, SO_BROADCAST, (char *) &val,
  665. &val_len) != 0)
  666. goto error;
  667. return new ::java::lang::Boolean (val != 0);
  668. case _Jv_SO_OOBINLINE_ :
  669. throw new ::java::net::SocketException (
  670. JvNewStringUTF ("SO_OOBINLINE not valid for UDP"));
  671. break;
  672. case _Jv_SO_RCVBUF_ :
  673. case _Jv_SO_SNDBUF_ :
  674. #if defined(SO_SNDBUF) && defined(SO_RCVBUF)
  675. int opt;
  676. optID == _Jv_SO_SNDBUF_ ? opt = SO_SNDBUF : opt = SO_RCVBUF;
  677. if (::getsockopt (native_fd, SOL_SOCKET, opt, (char *) &val, &val_len) != 0)
  678. goto error;
  679. else
  680. return new ::java::lang::Integer (val);
  681. #else
  682. throw new ::java::lang::InternalError (
  683. JvNewStringUTF ("SO_RCVBUF/SO_SNDBUF not supported"));
  684. #endif
  685. break;
  686. case _Jv_SO_BINDADDR_:
  687. // cache the local address
  688. if (localAddress == NULL)
  689. localAddress = getLocalAddress (native_fd);
  690. return localAddress;
  691. break;
  692. case _Jv_SO_REUSEADDR_ :
  693. #if defined(SO_REUSEADDR)
  694. if (::getsockopt (native_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &val,
  695. &val_len) != 0)
  696. goto error;
  697. return new ::java::lang::Boolean (val != 0);
  698. #else
  699. throw new ::java::lang::InternalError (
  700. JvNewStringUTF ("SO_REUSEADDR not supported"));
  701. #endif
  702. break;
  703. case _Jv_IP_MULTICAST_IF_ :
  704. #ifdef HAVE_INET_NTOA
  705. struct in_addr inaddr;
  706. socklen_t inaddr_len;
  707. char *bytes;
  708. inaddr_len = sizeof(inaddr);
  709. if (::getsockopt (native_fd, IPPROTO_IP, IP_MULTICAST_IF, (char *) &inaddr,
  710. &inaddr_len) != 0)
  711. goto error;
  712. bytes = inet_ntoa (inaddr);
  713. return ::java::net::InetAddress::getByName (JvNewStringLatin1 (bytes));
  714. #else
  715. throw new ::java::net::SocketException (
  716. JvNewStringUTF ("IP_MULTICAST_IF: not available - no inet_ntoa()"));
  717. #endif
  718. break;
  719. case _Jv_SO_TIMEOUT_ :
  720. return new ::java::lang::Integer (timeout);
  721. break;
  722. case _Jv_IP_MULTICAST_IF2_ :
  723. throw new ::java::net::SocketException (
  724. JvNewStringUTF ("IP_MULTICAST_IF2: not yet implemented"));
  725. break;
  726. case _Jv_IP_MULTICAST_LOOP_ :
  727. // cache the local address
  728. localAddress = getLocalAddress (native_fd);
  729. if (localAddress->addr->length == 4)
  730. {
  731. level = IPPROTO_IP;
  732. opname = IP_MULTICAST_LOOP;
  733. }
  734. #if defined (HAVE_INET6) && defined (IPV6_MULTICAST_LOOP)
  735. else if (localAddress->addr->length == 16)
  736. {
  737. level = IPPROTO_IPV6;
  738. opname = IPV6_MULTICAST_LOOP;
  739. }
  740. #endif
  741. else
  742. throw
  743. new ::java::net::SocketException (JvNewStringUTF ("invalid address length"));
  744. if (::getsockopt (native_fd, level, opname, (char *) &val,
  745. &val_len) != 0)
  746. goto error;
  747. return new ::java::lang::Boolean (val != 0);
  748. case _Jv_IP_TOS_ :
  749. if (::getsockopt (native_fd, SOL_SOCKET, IP_TOS, (char *) &val,
  750. &val_len) != 0)
  751. goto error;
  752. return new ::java::lang::Integer (val);
  753. default :
  754. errno = ENOPROTOOPT;
  755. }
  756. error:
  757. char* strerr = strerror (errno);
  758. throw new ::java::net::SocketException (JvNewStringUTF (strerr));
  759. }