Exemplo n.º 1
0
  /**
   * Copies the properties of a specific <tt>DatagramPacket</tt> to another <tt>DatagramPacket</tt>.
   * The property values are not cloned.
   *
   * @param src the <tt>DatagramPacket</tt> which is to have its properties copied to <tt>dest</tt>
   * @param dest the <tt>DatagramPacket</tt> which is to have its properties set to the value of the
   *     respective properties of <tt>src</tt>
   */
  public static void copy(DatagramPacket src, DatagramPacket dest) {
    synchronized (dest) {
      dest.setAddress(src.getAddress());
      dest.setPort(src.getPort());

      byte[] srcData = src.getData();

      if (srcData == null) dest.setLength(0);
      else {
        byte[] destData = dest.getData();

        if (destData == null) dest.setLength(0);
        else {
          int destOffset = dest.getOffset();
          int destLength = destData.length - destOffset;
          int srcLength = src.getLength();

          if (destLength >= srcLength) destLength = srcLength;
          else if (logger.isLoggable(Level.WARNING)) {
            logger.log(Level.WARNING, "Truncating received DatagramPacket data!");
          }
          System.arraycopy(srcData, src.getOffset(), destData, destOffset, destLength);
          dest.setLength(destLength);
        }
      }
    }
  }
 /**
  * Sends a datagram packet to the destination, with a TTL (time- to-live) other than the default
  * for the socket. This method need only be used in instances where a particular TTL is desired;
  * otherwise it is preferable to set a TTL once on the socket, and use that default TTL for all
  * packets. This method does <B>not </B> alter the default TTL for the socket. Its behavior may be
  * affected by {@code setInterface}.
  *
  * <p>If there is a security manager, this method first performs some security checks. First, if
  * {@code p.getAddress().isMulticastAddress()} is true, this method calls the security manager's
  * {@code checkMulticast} method with {@code p.getAddress()} and {@code ttl} as its arguments. If
  * the evaluation of that expression is false, this method instead calls the security manager's
  * {@code checkConnect} method with arguments {@code p.getAddress().getHostAddress()} and {@code
  * p.getPort()}. Each call to a security manager method could result in a SecurityException if the
  * operation is not allowed.
  *
  * @param p is the packet to be sent. The packet should contain the destination multicast ip
  *     address and the data to be sent. One does not need to be the member of the group to send
  *     packets to a destination multicast address.
  * @param ttl optional time to live for multicast packet. default ttl is 1.
  * @exception IOException is raised if an error occurs i.e error while setting ttl.
  * @exception SecurityException if a security manager exists and its {@code checkMulticast} or
  *     {@code checkConnect} method doesn't allow the send.
  * @deprecated Use the following code or its equivalent instead: ...... int ttl =
  *     mcastSocket.getTimeToLive(); mcastSocket.setTimeToLive(newttl); mcastSocket.send(p);
  *     mcastSocket.setTimeToLive(ttl); ......
  * @see DatagramSocket#send
  * @see DatagramSocket#receive
  * @see SecurityManager#checkMulticast(java.net.InetAddress, byte)
  * @see SecurityManager#checkConnect
  */
 @Deprecated
 public void send(DatagramPacket p, byte ttl) throws IOException {
   if (isClosed()) throw new SocketException("Socket is closed");
   checkAddress(p.getAddress(), "send");
   synchronized (ttlLock) {
     synchronized (p) {
       if (connectState == ST_NOT_CONNECTED) {
         // Security manager makes sure that the multicast address
         // is allowed one and that the ttl used is less
         // than the allowed maxttl.
         SecurityManager security = System.getSecurityManager();
         if (security != null) {
           if (p.getAddress().isMulticastAddress()) {
             security.checkMulticast(p.getAddress(), ttl);
           } else {
             security.checkConnect(p.getAddress().getHostAddress(), p.getPort());
           }
         }
       } else {
         // we're connected
         InetAddress packetAddress = null;
         packetAddress = p.getAddress();
         if (packetAddress == null) {
           p.setAddress(connectedAddress);
           p.setPort(connectedPort);
         } else if ((!packetAddress.equals(connectedAddress)) || p.getPort() != connectedPort) {
           throw new SecurityException("connected address and packet address" + " differ");
         }
       }
       byte dttl = getTTL();
       try {
         if (ttl != dttl) {
           // set the ttl
           getImpl().setTTL(ttl);
         }
         // call the datagram method to send
         getImpl().send(p);
       } finally {
         // set it back to default
         if (ttl != dttl) {
           getImpl().setTTL(dttl);
         }
       }
     } // synch p
   } // synch ttl
 } // method
  /**
   * {@collect.stats} {@description.open} Sends a datagram packet from this socket. The <code>
   * DatagramPacket</code> 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 <code>
   * p.getAddress().isMulticastAddress()</code> is true, this method calls the security manager's
   * <code>checkMulticast</code> method with <code>p.getAddress()</code> as its argument. If the
   * evaluation of that expression is false, this method instead calls the security manager's <code>
   * checkConnect</code> method with arguments <code>p.getAddress().getHostAddress()</code> and
   * <code>p.getPort()</code>. Each call to a security manager method could result in a
   * SecurityException if the operation is not allowed. {@description.close}
   *
   * @param p the <code>DatagramPacket</code> to be sent.
   * @exception IOException if an I/O error occurs.
   * @exception SecurityException if a security manager exists and its <code>checkMulticast</code>
   *     or <code>checkConnect</code> method doesn't allow the send.
   * @exception PortUnreachableException may be thrown if the socket is connected to a currently
   *     unreachable destination. Note, there is no guarantee that the exception will be thrown.
   * @exception java.nio.channels.IllegalBlockingModeException if this socket has an associated
   *     channel, and the channel is in non-blocking mode.
   * @see java.net.DatagramPacket
   * @see SecurityManager#checkMulticast(InetAddress)
   * @see SecurityManager#checkConnect
   * @revised 1.4
   * @spec JSR-51
   */
  public void send(DatagramPacket p) throws IOException {
    InetAddress packetAddress = null;
    synchronized (p) {
      if (isClosed()) throw new SocketException("Socket is closed");
      checkAddress(p.getAddress(), "send");
      if (connectState == ST_NOT_CONNECTED) {
        // check the address is ok wiht the security manager on every send.
        SecurityManager security = System.getSecurityManager();

        // The reason you want to synchronize on datagram packet
        // is because you dont want an applet to change the address
        // while you are trying to send the packet for example
        // after the security check but before the send.
        if (security != null) {
          if (p.getAddress().isMulticastAddress()) {
            security.checkMulticast(p.getAddress());
          } else {
            security.checkConnect(p.getAddress().getHostAddress(), p.getPort());
          }
        }
      } else {
        // we're connected
        packetAddress = p.getAddress();
        if (packetAddress == null) {
          p.setAddress(connectedAddress);
          p.setPort(connectedPort);
        } else if ((!packetAddress.equals(connectedAddress)) || p.getPort() != connectedPort) {
          throw new IllegalArgumentException(
              "connected address " + "and packet address" + " differ");
        }
      }
      // Check whether the socket is bound
      if (!isBound()) bind(new InetSocketAddress(0));
      // call the  method to send
      getImpl().send(p);
    }
  }
 /**
  * Set the received address and port in the packet. We do this when the Datagram socket is
  * connected at the native level and the recvConnnectedDatagramImpl does not update the packet
  * with address from which the packet was received
  *
  * @param packet the packet to be updated
  */
 private void updatePacketRecvAddress(DatagramPacket packet) {
   packet.setAddress(connectedAddress);
   packet.setPort(connectedPort);
 }
Exemplo n.º 5
0
  private void setSendPacketAddress(DatagramPacket sendPacket, DatagramPacket receivePacket) {

    // Tell the client to send more data
    sendPacket.setAddress(receivePacket.getAddress());
    sendPacket.setPort(receivePacket.getPort());
  }
Exemplo n.º 6
0
  public static void main(String args[]) throws Exception {
    DatagramSocket r_clients = null, s_clients = null;
    MulticastSocket servers = null, b_clients = null;

    local = Integer.parseInt(args[1]);
    rec = Integer.parseInt(args[0]);

    System.out.println("Recieving arp on port " + rec + "...");
    System.out.println("Recieving reply arp on port " + (rec + 1) + "...");
    System.out.println("\nEmulating gateway on port " + local + "\nWaiting...");

    try {
      r_clients = new DatagramSocket(rec);

      s_clients = new DatagramSocket(rec + 1);

      b_clients = new MulticastSocket(local);
      b_clients.joinGroup(InetAddress.getByName(group));

      servers = new MulticastSocket(serv_port);
      servers.joinGroup(InetAddress.getByName(group));

      servers.setSoTimeout(10);
      s_clients.setSoTimeout(10);
      b_clients.setSoTimeout(10);
      r_clients.setSoTimeout(10);
    } catch (Exception e) {
      System.out.println("error: " + e.toString());
      System.exit(1);
    }

    byte arp_buf[] = new byte[28];
    DatagramPacket arp_packet = new DatagramPacket(arp_buf, arp_buf.length);

    while (true) {
      try {
        servers.receive(arp_packet);
        System.out.println("\nGot arp from servers");

        System.out.println("Sending arp to local computers");
        arp_packet.setAddress(InetAddress.getByName(group));
        arp_packet.setPort(local);
        try {
          b_clients.send(arp_packet, (byte) 255);
        } catch (Exception e3) {
        }
      } catch (Exception e) {
        try {
          r_clients.receive(arp_packet);
          System.out.println("\nGot arp from client");

          System.out.println("Sending ARP");
          arp_packet.setAddress(InetAddress.getByName(group));
          arp_packet.setPort(serv_port);
          try {
            servers.send(arp_packet, (byte) 255);
          } catch (Exception e3) {
          }
        } catch (Exception e2) {
          try {
            s_clients.receive(arp_packet);
            System.out.println("\nGot reply arp from client");

            arp_packet.setAddress(InetAddress.getByName(group));
            arp_packet.setPort(2500);
            try {
              s_clients.send(arp_packet);
            } catch (Exception e3) {
            }
          } catch (Exception e4) {
          }
        }
      }
    }
  }