Exemple #1
0
  public void init() {
    String host = util.getHost();
    String bindAddr = util.getBindAddress();
    int port = util.getPort(ListeningContextFace.DEFAULT_TRAP_PORT);
    String socketType = util.getSocketType();
    // String oid = util.getOid();
    String community = util.getCommunity();

    try {
      context = new SnmpContextv2c(host, port, bindAddr, socketType);
      context.setCommunity(community);

      pdu = new TrapPduv2(context);
      pdu.addOid(sysUpTime, new AsnUnsInteger(5));
      pdu.addOid(snmpTrapOID, new AsnObjectId(warmStart));

      System.out.println(pdu.toString());
      pdu.send();

      // when calling destroy, the pdu might not be sent
      // context.destroy();
    } catch (java.io.IOException exc) {
      System.out.println("IOException " + exc.getMessage());
    } catch (uk.co.westhawk.snmp.stack.PduException exc) {
      System.out.println("PduException " + exc.getMessage());
    }
    // System.exit(0);
  }
  /**
   * Returns a clone of this SnmpContextv3.
   *
   * @exception CloneNotSupportedException Thrown when the constructor generates an IOException
   */
  public Object clone() throws CloneNotSupportedException {
    SnmpContextv3 clContext = null;
    try {
      clContext = new SnmpContextv3(hostAddr, hostPort, typeSocket);
      clContext.setUserName(new String(userName));
      clContext.setUseAuthentication(useAuthentication);
      if (userAuthenticationPassword != null) {
        clContext.setUserAuthenticationPassword(new String(userAuthenticationPassword));
      }
      clContext.setAuthenticationProtocol(authenticationProtocol);
      /*
      clContext.setUsePrivacy(usePrivacy);
      if (userPrivacyPassword != null)
      {
          clContext.setUserPrivacyPassword(new String(userPrivacyPassword));
      }
      */

      clContext.setContextName(new String(contextName));

      int l = contextEngineId.length;
      byte[] newContextEngineId = new byte[l];
      System.arraycopy(contextEngineId, 0, newContextEngineId, 0, l);
      clContext.setContextEngineId(newContextEngineId);
    } catch (java.io.IOException exc) {
      throw new CloneNotSupportedException("IOException " + exc.getMessage());
    }
    return clContext;
  }
Exemple #3
0
 public void start() {
   if (context != null) {
     try {
       UpSincePdu up = new UpSincePdu(context, this);
     } catch (java.io.IOException exc) {
       System.out.println("start(): IOException " + exc.getMessage());
     } catch (PduException exc) {
       System.out.println("start(): PduException " + exc.getMessage());
     }
   }
 }
Exemple #4
0
  public void init() {
    String host = util.getHost();

    h = new JLabel(host + " up since: ");
    v = new JLabel(" unknown ");
    setLayout(new GridLayout(2, 1));
    add(h);
    add(v);
    try {
      context = new SnmpContext(host, port);
    } catch (java.io.IOException exc) {
      System.out.println("IOException " + exc.getMessage());
      System.exit(0);
    }
  }
Exemple #5
0
  public static void main(String[] args) throws ClassNotFoundException, IOException {

    try {
      String server_name = args.length == 1 ? args[0] : "localhost";

      DatagramSocket echoSocket = null; // == UDP Socket
      echoSocket = new DatagramSocket();

      String message = "pre"; // init-Wert
      while (!message.equals("quit")) {

        System.out.println(" *** Contact with " + server_name + " ***");
        System.out.println("Press Enter to send your message.");

        // <EntryPoint>
        System.out.print("> ");
        message = System.console().readLine();

        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(buffer);
        out.writeObject(message);

        out.close();
        buffer.close();

        DatagramPacket packet =
            new DatagramPacket(
                buffer.toByteArray(), buffer.size(), InetAddress.getByName(server_name), udp_port);
        echoSocket.send(packet);
      }

    } catch (UnknownHostException e) {
      System.err.println("Unknown host");
      return;
    } catch (NoRouteToHostException e) {
      System.err.println("Unreachable");
      return;
    } catch (ConnectException e) {
      System.err.println("connect refused");
      return;
    } catch (java.io.IOException e) {
      System.err.println(e.getMessage());
      return;
    } finally {
      System.out.println("Bye");
      // echoSocket.close();
    }
  } // -- end of main
 public void run() {
   // We might not get an error, since it is not sure if an
   // interface exists with this intfIndex as index!
   InterfacePdu up2;
   while (context != null) {
     try {
       up2 = new InterfacePdu(context, this, intfIndex);
       Thread.sleep(interval);
     } catch (InterruptedException exc) {;
     } catch (java.io.IOException exc) {
       System.out.println("run(): IOException " + exc.getMessage());
     } catch (PduException exc) {
       System.out.println("run(): PduException " + exc.getMessage());
     }
   }
 }
  public void init() {
    // get the host name
    String host = util.getHost();

    h = new JLabel("Host " + host + ", Interface " + intfIndex + " speed");
    try {
      context = new SnmpContext(host, port);
      context.setCommunity(community);

      setLayout(new GridLayout(2, 1));
      add(h);
      v = new JLabel(" unknown ");
      add(v);
    } catch (java.io.IOException exc) {
      System.out.println("IOException " + exc.getMessage());
      System.exit(0);
    }
  }
  public InputStream upload() throws ResourceUploaderException {

    try {

      try {
        URL url = new URL(target.toString().replaceAll(" ", "%20"));

        // some authentications screw up without an explicit port number here

        String protocol = url.getProtocol().toLowerCase();

        if (url.getPort() == -1) {

          int target_port;

          if (protocol.equals("http")) {

            target_port = 80;

          } else {

            target_port = 443;
          }

          try {
            String str = target.toString().replaceAll(" ", "%20");

            int pos = str.indexOf("://");

            pos = str.indexOf("/", pos + 4);

            // might not have a trailing "/"

            if (pos == -1) {

              url = new URL(str + ":" + target_port + "/");

            } else {

              url = new URL(str.substring(0, pos) + ":" + target_port + str.substring(pos));
            }

          } catch (Throwable e) {

            Debug.printStackTrace(e);
          }
        }

        url = AddressUtils.adjustURL(url);

        try {
          if (user_name != null) {

            SESecurityManager.setPasswordHandler(url, this);
          }

          for (int i = 0; i < 2; i++) {

            try {
              HttpURLConnection con;

              if (url.getProtocol().equalsIgnoreCase("https")) {

                // see ConfigurationChecker for SSL client defaults

                HttpsURLConnection ssl_con = (HttpsURLConnection) url.openConnection();

                // allow for certs that contain IP addresses rather than dns names

                ssl_con.setHostnameVerifier(
                    new HostnameVerifier() {
                      public boolean verify(String host, SSLSession session) {
                        return (true);
                      }
                    });

                con = ssl_con;

              } else {

                con = (HttpURLConnection) url.openConnection();
              }

              con.setRequestMethod("POST");

              con.setRequestProperty(
                  "User-Agent", Constants.AZUREUS_NAME + " " + Constants.AZUREUS_VERSION);

              con.setDoOutput(true);
              con.setDoInput(true);

              OutputStream os = con.getOutputStream();

              byte[] buffer = new byte[65536];

              while (true) {

                int len = data.read(buffer);

                if (len <= 0) {

                  break;
                }

                os.write(buffer, 0, len);
              }

              con.connect();

              int response = con.getResponseCode();

              if ((response != HttpURLConnection.HTTP_ACCEPTED)
                  && (response != HttpURLConnection.HTTP_OK)) {

                throw (new ResourceUploaderException(
                    "Error on connect for '"
                        + url.toString()
                        + "': "
                        + Integer.toString(response)
                        + " "
                        + con.getResponseMessage()));
              }

              return (con.getInputStream());

            } catch (SSLException e) {

              if (i == 0) {

                if (SESecurityManager.installServerCertificates(url) != null) {

                  // certificate has been installed

                  continue; // retry with new certificate
                }
              }

              throw (e);
            }
          }

          throw (new ResourceUploaderException("Should never get here"));

        } finally {

          if (user_name != null) {

            SESecurityManager.setPasswordHandler(url, null);
          }
        }
      } catch (java.net.MalformedURLException e) {

        throw (new ResourceUploaderException(
            "Exception while parsing URL '" + target + "':" + e.getMessage(), e));

      } catch (java.net.UnknownHostException e) {

        throw (new ResourceUploaderException(
            "Exception while initializing download of '"
                + target
                + "': Unknown Host '"
                + e.getMessage()
                + "'",
            e));

      } catch (java.io.IOException e) {

        throw (new ResourceUploaderException(
            "I/O Exception while downloading '" + target + "':" + e.toString(), e));
      }
    } catch (Throwable e) {

      ResourceUploaderException rde;

      if (e instanceof ResourceUploaderException) {

        rde = (ResourceUploaderException) e;

      } else {

        rde = new ResourceUploaderException("Unexpected error", e);
      }

      throw (rde);

    } finally {

      try {
        data.close();

      } catch (Throwable e) {

        e.printStackTrace();
      }
    }
  }
Exemple #9
0
  public Session _dc_run(String login, String password) throws InterruptedException {
    String authserver = (Config.authserv == null) ? address : Config.authserv;
    retry:
    do {
      byte[] cookie;
      AuthClient auth = null;
      try {
        try {
          auth = new AuthClient(authserver, login);
        } catch (UnknownHostException e) {
          _dc_log("Could not locate server");
          continue retry;
        }
        if (!auth.trypasswd(password)) {
          auth.close();
          password = "";
          _dc_log("Username or password incorrect");
          continue retry;
        }
        cookie = auth.cookie;

      } catch (java.io.IOException e) {
        ui.uimsg(1, "error", e.getMessage());
        continue retry;
      } finally {
        try {
          if (auth != null) auth.close();
        } catch (java.io.IOException e) {
        }
      }
      _dc_log("Connecting...");
      try {
        sess = new Session(InetAddress.getByName(address), login, cookie);
      } catch (UnknownHostException e) {
        _dc_log("Could not locate server");
        continue retry;
      }
      Thread.sleep(100);
      while (true) {
        if (sess.state == "") {
          break retry;
        } else if (sess.connfailed != 0) {
          String error;
          switch (sess.connfailed) {
            case 1:
              error = "Invalid authentication token";
              break;
            case 2:
              error = "Already logged in";
              break;
            case 3:
              error = "Could not connect to server";
              break;
            case 4:
              error = "This client is too old";
              break;
            case 5:
              error = "Authentication token expired";
              break;
            default:
              error = "Connection failed";
              break;
          }
          _dc_log(error);
          sess = null;
          continue retry;
        }
        synchronized (sess) {
          sess.wait();
        }
      }
    } while (true);
    return sess;
  }
Exemple #10
0
 public Session run(HavenPanel hp) throws InterruptedException {
   ui = hp.newui(null);
   ui.setreceiver(this);
   ui.bind(new LoginScreen(ui.root), 1);
   String username;
   boolean savepw = false;
   Utils.setpref("password", "");
   byte[] token = null;
   if (Utils.getpref("savedtoken", "").length() == 64)
     token = Utils.hex2byte(Utils.getpref("savedtoken", null));
   username = Utils.getpref("username", "");
   String authserver = (Config.authserv == null) ? address : Config.authserv;
   retry:
   do {
     byte[] cookie;
     if (initcookie != null) {
       username = inituser;
       cookie = initcookie;
       initcookie = null;
     } else if (token != null) {
       savepw = true;
       ui.uimsg(1, "token", username);
       while (true) {
         Message msg;
         synchronized (msgs) {
           while ((msg = msgs.poll()) == null) msgs.wait();
         }
         if (msg.id == 1) {
           if (msg.name == "login") {
             break;
           } else if (msg.name == "forget") {
             token = null;
             Utils.setpref("savedtoken", "");
             continue retry;
           }
         }
       }
       ui.uimsg(1, "prg", "Authenticating...");
       AuthClient auth = null;
       try {
         auth = new AuthClient(authserver, username);
         if (!auth.trytoken(token)) {
           auth.close();
           token = null;
           Utils.setpref("savedtoken", "");
           ui.uimsg(1, "error", "Invalid save");
           continue retry;
         }
         cookie = auth.cookie;
       } catch (java.io.IOException e) {
         ui.uimsg(1, "error", e.getMessage());
         continue retry;
       } finally {
         try {
           if (auth != null) auth.close();
         } catch (java.io.IOException e) {
         }
       }
     } else {
       String password;
       ui.uimsg(1, "passwd", username, savepw);
       while (true) {
         Message msg;
         synchronized (msgs) {
           while ((msg = msgs.poll()) == null) msgs.wait();
         }
         if (msg.id == 1) {
           if (msg.name == "login") {
             username = (String) msg.args[0];
             password = (String) msg.args[1];
             savepw = (Boolean) msg.args[2];
             break;
           }
         }
       }
       ui.uimsg(1, "prg", "Authenticating...");
       AuthClient auth = null;
       try {
         try {
           auth = new AuthClient(authserver, username);
         } catch (UnknownHostException e) {
           ui.uimsg(1, "error", "Could not locate server");
           continue retry;
         }
         if (!auth.trypasswd(password)) {
           auth.close();
           password = "";
           ui.uimsg(1, "error", "Username or password incorrect");
           continue retry;
         }
         cookie = auth.cookie;
         if (savepw) {
           if (auth.gettoken()) Utils.setpref("savedtoken", Utils.byte2hex(auth.token));
         }
       } catch (java.io.IOException e) {
         ui.uimsg(1, "error", e.getMessage());
         continue retry;
       } finally {
         try {
           if (auth != null) auth.close();
         } catch (java.io.IOException e) {
         }
       }
     }
     ui.uimsg(1, "prg", "Connecting...");
     try {
       sess = new Session(InetAddress.getByName(address), username, cookie);
     } catch (UnknownHostException e) {
       ui.uimsg(1, "error", "Could not locate server");
       continue retry;
     }
     Thread.sleep(100);
     while (true) {
       if (sess.state == "") {
         Utils.setpref("username", username);
         ui.destroy(1);
         break retry;
       } else if (sess.connfailed != 0) {
         String error;
         switch (sess.connfailed) {
           case 1:
             error = "Invalid authentication token";
             break;
           case 2:
             error = "Already logged in";
             break;
           case 3:
             error = "Could not connect to server";
             break;
           case 4:
             error = "This client is too old";
             break;
           case 5:
             error = "Authentication token expired";
             break;
           default:
             error = "Connection failed";
             break;
         }
         ui.uimsg(1, "error", error);
         sess = null;
         continue retry;
       }
       synchronized (sess) {
         sess.wait();
       }
     }
   } while (true);
   haven.error.ErrorHandler.setprop("usr", sess.username);
   return (sess);
   // (new RemoteUI(sess, ui)).start();
 }