Пример #1
0
 /** Returns the address */
 public InetAddress getAddress() {
   try {
     if (name == null) return InetAddress.getByAddress(address);
     else return InetAddress.getByAddress(name.toString(), address);
   } catch (UnknownHostException e) {
     return null;
   }
 }
Пример #2
0
 /** Returns the Internet address */
 public InetAddress getAddress() {
   try {
     return InetAddress.getByAddress(toArray(addr));
   } catch (UnknownHostException e) {
     return null;
   }
 }
    /*
     * Returns the string representation of the address that this
     * listen key represents.
     */
    public String address() {
      InetAddress address = ss.getInetAddress();

      /*
       * If bound to the wildcard address then use current local
       * hostname. In the event that we don't know our own hostname
       * then assume that host supports IPv4 and return something to
       * represent the loopback address.
       */
      if (address.isAnyLocalAddress()) {
        try {
          address = InetAddress.getLocalHost();
        } catch (UnknownHostException uhe) {
          byte[] loopback = {0x7f, 0x00, 0x00, 0x01};
          try {
            address = InetAddress.getByAddress("127.0.0.1", loopback);
          } catch (UnknownHostException x) {
            throw new InternalError("unable to get local hostname");
          }
        }
      }

      /*
       * Now decide if we return a hostname or IP address. Where possible
       * return a hostname but in the case that we are bound to an
       * address that isn't registered in the name service then we
       * return an address.
       */
      String result;
      String hostname = address.getHostName();
      String hostaddr = address.getHostAddress();
      if (hostname.equals(hostaddr)) {
        if (address instanceof Inet6Address) {
          result = "[" + hostaddr + "]";
        } else {
          result = hostaddr;
        }
      } else {
        result = hostname;
      }

      /*
       * Finally return "hostname:port", "ipv4-address:port" or
       * "[ipv6-address]:port".
       */
      return result + ":" + ss.getLocalPort();
    }
Пример #4
0
  private void init() throws IOException {
    messageBuffer = new ArrayList();
    receiveBuffer = ByteBuffer.allocate(CAPACITY);
    sendBuffer = ByteBuffer.allocate(CAPACITY);
    buf = new byte[CAPACITY];

    channel = DatagramChannel.open();
    channel.configureBlocking(false);

    InetAddress host = null;
    if (getAddress() != null) {
      host = InetAddress.getByAddress(getAddress().getBytes());
    }
    channel.socket().bind(new InetSocketAddress(host, getPort()));
    channel.socket().setReceiveBufferSize(CAPACITY);

    certifier = new MessageCertifier(this);
    extractor = new MessageExtractor(this);
  }
Пример #5
0
 /** Converts rdata to a String */
 public String rrToString() {
   InetAddress addr;
   try {
     addr = InetAddress.getByAddress(null, address);
   } catch (UnknownHostException e) {
     return null;
   }
   if (addr.getAddress().length == 4) {
     // Deal with Java's broken handling of mapped IPv4 addresses.
     StringBuffer sb = new StringBuffer("0:0:0:0:0:ffff:");
     int high = ((address[12] & 0xFF) << 8) + (address[13] & 0xFF);
     int low = ((address[14] & 0xFF) << 8) + (address[15] & 0xFF);
     sb.append(Integer.toHexString(high));
     sb.append(':');
     sb.append(Integer.toHexString(low));
     return sb.toString();
   }
   return addr.getHostAddress();
 }
Пример #6
0
    private void getRequestV4() throws IOException {
      int ver = in.read();
      int cmd = in.read();
      if (ver == -1 || cmd == -1) {
        // EOF
        in.close();
        out.close();
        return;
      }

      if (ver != 0 && ver != 4) {
        out.write(PROTO_VERS4);
        out.write(91); // Bad Request
        out.write(0);
        out.write(0);
        out.write(0);
        out.write(0);
        out.write(0);
        out.write(0);
        out.write(0);
        out.flush();
        purge();
        out.close();
        in.close();
        return;
      }

      if (cmd == CONNECT) {
        int port = ((in.read() & 0xff) << 8);
        port += (in.read() & 0xff);
        byte[] buf = new byte[4];
        readBuf(in, buf);
        InetAddress addr = InetAddress.getByAddress(buf);
        // We don't use the username...
        int c;
        do {
          c = (in.read() & 0xff);
        } while (c != 0);
        boolean ok = true;
        try {
          dest = new Socket(addr, port);
        } catch (IOException e) {
          ok = false;
        }
        if (!ok) {
          out.write(PROTO_VERS4);
          out.write(91);
          out.write(0);
          out.write(0);
          out.write(buf);
          out.flush();
          purge();
          out.close();
          in.close();
          return;
        }
        out.write(PROTO_VERS4);
        out.write(90); // Success
        out.write((port >> 8) & 0xff);
        out.write(port & 0xff);
        out.write(buf);
        out.flush();
        InputStream in2 = dest.getInputStream();
        OutputStream out2 = dest.getOutputStream();

        Tunnel tunnel = new Tunnel(in2, out);
        tunnel.start();

        int b = 0;
        do {
          try {
            b = in.read();
            if (b == -1) {
              in.close();
              out2.close();
              return;
            }
            out2.write(b);
          } catch (IOException ex) {
          }
        } while (!client.isClosed());
      }
    }
Пример #7
0
 static void pinging() throws IOException {
   Socket mySocket;
   if (!inetAddresses.isEmpty()) {
     bcid =
         ((InterfaceAddress) inetAddresses.get(0))
             .getBroadcast()
             .getAddress(); // gets the last ip address in subnet
     netaddress(
         ((InterfaceAddress) inetAddresses.get(0))
             .getNetworkPrefixLength(), // creats the mask field
         ((InterfaceAddress) inetAddresses.get(0))
             .getAddress()
             .getAddress() // creats the myip field
         );
     byte[] pingip = netid;
     for (; ; ) {
       InetAddress ping = InetAddress.getByAddress(pingip);
       //   boolean reachable=ping.isReachable(5000);     // This command is doing the same work as
       // the ping command in windows
       try {
         Process ps = Runtime.getRuntime().exec("ping.exe " + ping);
         BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
         StringBuilder sb = new StringBuilder();
         String line;
         while ((line = br.readLine()) != null) {
           sb.append(line);
           sb.append("\n");
         }
         y = sb.toString();
         System.out.print(sb.toString());
       } catch (IOException e) {
         e.printStackTrace();
       }
       if (!contains(y, substring)) {
         System.out.println("The ip address " + ping + "is reachable");
         for (int port = 0; port < 65536; port++) {
           try {
             mySocket = new Socket(ping, port);
             System.out.println("The port" + port + "on ip:" + ping + "is free.");
             mySocket.close();
           } catch (IOException c) {
             System.out.println("The port" + port + "on ip:" + ping + "is closed.");
           }
         }
       } else
         System.out.println(
             "The pinged ip addres is unreachable, probably it is not up in your subnet");
       pingip[3]++;
       if (pingip[3] == (byte) 255) pingip[2]++;
       if (pingip[2]
           == (byte)
               255) // here we ping ips in our subnet from the first ip address to the last one
       pingip[1]++;
       if (pingip[1] == (byte) 255) pingip[0]++;
       if (pingip[0]
           == (byte)
               127) // beacuase the ip addresses that are not in class A have at least 2 bytes of
                    // net ids
       break;
       if (pingip[0] == bcid[0]
           & pingip[1] == bcid[1]
           & pingip[2] == bcid[2]
           & pingip[3] == bcid[3]) break;
     }
   } else System.out.println("null pointer exception");
 }
  /** {@inheritDoc} */
  @Override
  public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
    super.readExternal(in);

    addr = InetAddress.getByAddress(U.readByteArray(in));
  }
Пример #9
0
 void rrFromWire(DNSInput in) throws IOException {
   address = InetAddress.getByAddress(name.toString(), in.readByteArray(16));
 }