Esempio n. 1
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;
  }
Esempio n. 2
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);
  }
Esempio n. 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());
     }
   }
 }
Esempio n. 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);
    }
  }
Esempio n. 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
Esempio n. 6
0
 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());
     }
   }
 }
Esempio n. 7
0
  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);
    }
  }
Esempio n. 8
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;
  }
Esempio n. 9
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();
 }