Example #1
0
  /**
   * Workaround for the problem encountered in certains JDKs that a thread listening on a socket
   * cannot be interrupted. Therefore we just send a dummy datagram packet so that the thread 'wakes
   * up' and realizes it has to terminate. Should be removed when all JDKs support
   * Thread.interrupt() on reads. Uses sock t send dummy packet, so this socket has to be still
   * alive.
   *
   * @param dest The destination host. Will be local host if null
   * @param port The destination port
   */
  void sendDummyPacket(InetAddress dest, int port) {
    DatagramPacket packet;
    byte[] buf = {0};

    if (dest == null) {
      try {
        dest = InetAddress.getLocalHost();
      } catch (Exception e) {
      }
    }

    if (Trace.debug) {
      Trace.info("UDP.sendDummyPacket()", "sending packet to " + dest + ":" + port);
    }
    if (sock == null || dest == null) {
      Trace.warn(
          "UDP.sendDummyPacket()", "sock was null or dest was null, cannot send dummy packet");
      return;
    }
    packet = new DatagramPacket(buf, buf.length, dest, port);
    try {
      sock.send(packet);
    } catch (Throwable e) {
      Trace.error(
          "UDP.sendDummyPacket()",
          "exception sending dummy packet to " + dest + ":" + port + ": " + e);
    }
  }
Example #2
0
  /**
   * Create UDP sender and receiver sockets. Currently there are 2 sockets (sending and receiving).
   * This is due to Linux's non-BSD compatibility in the JDK port (see DESIGN).
   */
  void createSockets() throws Exception {
    InetAddress tmp_addr = null;

    // bind_addr not set, try to assign one by default. This is needed on Windows

    // changed by bela Feb 12 2003: by default multicast sockets will be bound to all network
    // interfaces

    // CHANGED *BACK* by bela March 13 2003: binding to all interfaces did not result in a correct
    // local_addr. As a matter of fact, comparison between e.g. 0.0.0.0:1234 (on hostA) and
    // 0.0.0.0:1.2.3.4 (on hostB) would fail !
    if (bind_addr == null) {
      InetAddress[] interfaces =
          InetAddress.getAllByName(InetAddress.getLocalHost().getHostAddress());
      if (interfaces != null && interfaces.length > 0) bind_addr = interfaces[0];
    }

    if (bind_addr == null) bind_addr = InetAddress.getLocalHost();

    if (bind_addr != null && Trace.trace) {
      Trace.info(
          "UDP.createSockets()",
          "unicast sockets will use interface " + bind_addr.getHostAddress());

      // 2. Create socket for receiving unicast UDP packets. The address and port
      //    of this socket will be our local address (local_addr)

      // 27-6-2003 bgooren, find available port in range (start_port, start_port+port_range)
    }
    int rcv_port = bind_port, max_port = bind_port + port_range;
    while (rcv_port <= max_port) {

      try {
        sock = new DatagramSocket(rcv_port, bind_addr);
        break;
      } catch (SocketException bind_ex) { // Cannot listen on this port
        rcv_port++;
      } catch (SecurityException sec_ex) { // Not allowed to list on this port
        rcv_port++;
      }

      // Cannot listen at all, throw an Exception
      if (rcv_port == max_port + 1) { // +1 due to the increment above
        throw new Exception(
            "UDP.createSockets(): cannot list on any port in range "
                + bind_port
                + "-"
                + (bind_port + port_range));
      }
    }
    // ucast_recv_sock=new DatagramSocket(bind_port, bind_addr);
    if (sock == null) {
      throw new Exception("UDP.createSocket(): sock is null");
    }

    local_addr = new IpAddress(sock.getLocalAddress(), sock.getLocalPort());
    if (additional_data != null) {
      local_addr.setAdditionalData(additional_data);

      // 3. Create socket for receiving IP multicast packets
    }
    if (ip_mcast) {
      mcast_sock = new MulticastSocket(mcast_port);
      mcast_sock.setTimeToLive(ip_ttl);
      if (bind_addr != null) {
        mcast_sock.setInterface(bind_addr);
      }
      tmp_addr = InetAddress.getByName(mcast_addr_name);
      mcast_addr = new IpAddress(tmp_addr, mcast_port);
      mcast_sock.joinGroup(tmp_addr);
    }

    setBufferSizes();

    if (Trace.trace) {
      Trace.info("UDP.createSockets()", "socket information:\n" + dumpSocketInfo());
    }
  }
Example #3
0
  /**
   * Setup the Protocol instance acording to the configuration string The following properties are
   * being read by the UDP protocol param mcast_addr - the multicast address to use default is
   * 224.0.0.200 param mcast_port - (int) the port that the multicast is sent on default is 7500
   * param ip_mcast - (boolean) flag whether to use IP multicast - default is true param ip_ttl -
   * Set the default time-to-live for multicast packets sent out on this socket. default is 32
   *
   * @return true if no other properties are left. false if the properties still have data in them,
   *     ie , properties are left over and not handled by the protocol stack
   */
  public boolean setProperties(Properties props) {
    String str, tmp;

    tmp = System.getProperty("UDP.bind_addr");
    if (tmp != null) {
      str = tmp;
    } else {
      str = props.getProperty("bind_addr");
    }
    if (str != null) {
      try {
        bind_addr = InetAddress.getByName(str);
      } catch (UnknownHostException unknown) {
        Trace.fatal("UDP.setProperties()", "(bind_addr): host " + str + " not known");
        return false;
      }
      props.remove("bind_addr");
    }

    str = props.getProperty("bind_port");
    if (str != null) {
      bind_port = new Integer(str).intValue();
      props.remove("bind_port");
    }

    str = props.getProperty("start_port");
    if (str != null) {
      bind_port = new Integer(str).intValue();
      props.remove("start_port");
    }

    str = props.getProperty("port_range");
    if (str != null) {
      port_range = new Integer(str).intValue();
      props.remove("port_range");
    }

    str = props.getProperty("mcast_addr");
    if (str != null) {
      mcast_addr_name = new String(str);
      props.remove("mcast_addr");
    }

    str = props.getProperty("mcast_port");
    if (str != null) {
      mcast_port = new Integer(str).intValue();
      props.remove("mcast_port");
    }

    str = props.getProperty("ip_mcast");
    if (str != null) {
      ip_mcast = new Boolean(str).booleanValue();
      props.remove("ip_mcast");
    }

    str = props.getProperty("ip_ttl");
    if (str != null) {
      ip_ttl = new Integer(str).intValue();
      props.remove("ip_ttl");
    }

    str = props.getProperty("mcast_send_buf_size");
    if (str != null) {
      mcast_send_buf_size = Integer.parseInt(str);
      props.remove("mcast_send_buf_size");
    }

    str = props.getProperty("mcast_recv_buf_size");
    if (str != null) {
      mcast_recv_buf_size = Integer.parseInt(str);
      props.remove("mcast_recv_buf_size");
    }

    str = props.getProperty("ucast_send_buf_size");
    if (str != null) {
      ucast_send_buf_size = Integer.parseInt(str);
      props.remove("ucast_send_buf_size");
    }

    str = props.getProperty("ucast_recv_buf_size");
    if (str != null) {
      ucast_recv_buf_size = Integer.parseInt(str);
      props.remove("ucast_recv_buf_size");
    }

    str = props.getProperty("loopback");
    if (str != null) {
      loopback = new Boolean(str).booleanValue();
      props.remove("loopback");
    }

    // this is deprecated, just left for compatibility (use use_incoming_packet_handler)
    str = props.getProperty("use_packet_handler");
    if (str != null) {
      use_incoming_packet_handler = new Boolean(str).booleanValue();
      props.remove("use_packet_handler");
      Trace.warn(
          "UDP.setProperties()",
          "'use_packet_handler' is deprecated; use 'use_incoming_packet_handler' instead");
    }

    str = props.getProperty("use_incoming_packet_handler");
    if (str != null) {
      use_incoming_packet_handler = new Boolean(str).booleanValue();
      props.remove("use_incoming_packet_handler");
    }

    str = props.getProperty("use_outgoing_packet_handler");
    if (str != null) {
      use_outgoing_packet_handler = new Boolean(str).booleanValue();
      props.remove("use_outgoing_packet_handler");
    }

    str = props.getProperty("max_bundle_size");
    if (str != null) {
      int bundle_size = Integer.parseInt(str);
      if (bundle_size > max_bundle_size) {
        Trace.error(
            "UDP.setProperties()",
            "max_bundle_size ("
                + bundle_size
                + ") is greater than largest UDP fragmentation size ("
                + max_bundle_size
                + ")");
        return false;
      }
      if (bundle_size <= 0) {
        Trace.error("UDP.setProperties()", "max_bundle_size (" + bundle_size + ") is <= 0");
        return false;
      }
      max_bundle_size = bundle_size;
      props.remove("max_bundle_size");
    }

    str = props.getProperty("max_bundle_timeout");
    if (str != null) {
      max_bundle_timeout = Long.parseLong(str);
      if (max_bundle_timeout <= 0) {
        Trace.error(
            "UDP.setProperties()", "max_bundle_timeout of " + max_bundle_timeout + " is invalid");
        return false;
      }
      props.remove("max_bundle_timeout");
    }

    str = props.getProperty("enable_bundling");
    if (str != null) {
      enable_bundling = new Boolean(str).booleanValue();
      props.remove("enable_bundling");
    }

    if (props.size() > 0) {
      System.err.println("UDP.setProperties(): the following properties are not recognized:");
      props.list(System.out);
      return false;
    }

    if (enable_bundling) {
      if (use_outgoing_packet_handler == false) {
        Trace.warn(
            "UDP.setProperties()",
            "enable_bundling is true; setting use_outgoing_packet_handler=true");
      }
      use_outgoing_packet_handler = true;
    }

    return true;
  }