public void notify(Packet packet) { Log.trace("Register handling " + packet.toString()); String type = packet.getType(); Packet query = packet.getFirstChild("query"); if (type.equals("get")) { required.setSession(packet.getSession()); required.setID(packet.getID()); MessageHandler.deliverPacket(required); return; } else if (type.equals("set")) { // type == set String username = query.getChildValue("username"); User user = userIndex.getUser(username); if (user != null) { // user exists if (packet.getSession().getStatus() != Session.AUTHENTICATED || !username.equals(packet.getSession().getJID().getUser())) { Packet iq = new Packet("iq"); iq.setSession(packet.getSession()); iq.setID(packet.getID()); ErrorTool.setError(iq, 401, "User account already exists"); MessageHandler.deliverPacket(iq); return; } } else { user = userIndex.addUser(username); } user.setPassword(query.getChildValue("password")); user.setHash(query.getChildValue("hash")); user.setSequence(query.getChildValue("sequence")); user.setToken(query.getChildValue("token")); if (user.getHash() == null || user.getSequence() == null || user.getToken() == null) { if (user.getPassword() != null) { user.setToken(Authenticator.randomToken()); user.setSequence("99"); user.setHash( auth.getZeroKHash(100, user.getToken().getBytes(), user.getPassword().getBytes())); } } else { user.setSequence(Integer.toString(Integer.parseInt(user.getSequence()) - 1)); } Packet iq = new Packet("iq"); iq.setSession(packet.getSession()); iq.setID(packet.getID()); iq.setType("result"); // success MessageHandler.deliverPacket(iq); Log.trace( "Register successfully registered " + username + " with password " + query.getChildValue("password")); } else { Log.info("Register ignoring " + packet.toString()); } }
private static void finishPacket( HashTable ht, Packet packet, BufferedWriter output, BufferedWriter failOutput) throws IOException { if (packet.isIgnore()) { failOutput.write(packet.toString()); } else { String detectedApp = ht.getApp(packet); // if (!detectedApp.startsWith("network.warden") && !detectedApp.equals("unknown")) // { if (detectedApp.startsWith("android.youtube")) { packet.setApp(detectedApp); output.write(packet.toString()); } } }
public synchronized void handlePacket(Connection c, Packet p) { Address address = c.getAddress(); System.out.println(p.toString()); // silently ignore packets from a connection if we haven't received a version packet if (!c.hasReceivedVersion() && p.packetType() != PacketType.VERSION) { return; } switch (p.packetType()) { case VERSION: VersionPacket v = (VersionPacket) p; long ourVersion = ProtocolVersion.version(), theirVersion = v.getVersion(), negotiatedVersion; negotiatedVersion = theirVersion < ourVersion ? theirVersion : ourVersion; c.setVersion(negotiatedVersion); c.hasReceivedVersion(true); addressBook.justSeen(address); if (negotiatedVersion >= 209) { Packet verack = c.createPacket(PacketType.VERACK); c.sendPacket(verack); } break; case VERACK: c.hasRecievedVerack(true); addressBook.justSeen(address); break; case HEADERS: // primitive headers function HeadersPacket h = (HeadersPacket) p; if (h.headers().size() == 0) { break; } for (Block header : h.headers()) { try { blockChain.addBlock(header); } catch (InvalidBlockException e) { // TODO actually handle e.printStackTrace(); continue; } catch (OrphanBlockException e) { // TODO actually handle e.printStackTrace(); continue; } catch (BlockExistsException e) { // TODO actually handle e.printStackTrace(); continue; } } GetHeadersPacket gh = (GetHeadersPacket) c.createPacket(PacketType.GETHEADERS); gh.startHashes().add(blockChain.topBlock().hash()); c.sendPacket(gh); } }
void displayPacket(Packet packet, String prefix, String suffix) { if (!_log.shouldLog(Log.INFO)) return; StringBuilder buf = new StringBuilder(256); synchronized (_fmt) { buf.append(_fmt.format(new Date())); } buf.append(": ").append(prefix).append(" "); buf.append(packet.toString()); if (suffix != null) buf.append(" ").append(suffix); String str = buf.toString(); System.out.println(str); if (_log.shouldLog(Log.DEBUG)) _log.debug(str); }
private void ProcessData(int readed) { byte[] buf; if (left_buf != null) { buf = new byte[left_buf.length + readed]; System.arraycopy(left_buf, 0, buf, 0, left_buf.length); System.arraycopy(read_buffer, 0, buf, left_buf.length, readed); left_buf = null; } else { buf = new byte[readed]; System.arraycopy(read_buffer, 0, buf, 0, readed); } // Log.info("process data readed="+readed); while (true) { // if (buf != null) // Log.info("buf len="+buf.length); // else // Log.info("buf null"); if (is_header) { // Log.info("is header"); if ((buf != null && buf.length < HEADER_SIZE) || buf == null) { left_buf = buf; break; } int data_len = Utils.uint16d(buf, 0) - HEADER_SIZE; int type = Utils.unsigned_byte(buf[2]); if (Config.debug_packets) Log.debug("new pkt: [" + type + "] len=" + data_len); cur_packet = new Packet(type, data_len); buf = cur_packet.add_data(buf, HEADER_SIZE, buf.length - HEADER_SIZE); if (cur_packet.is_ready()) { synchronized (Connection.this) { if (Config.debug_packets) Log.info("recv packet " + cur_packet.toString()); cur_packet.offset = 0; synchronized (packets) { packets.addFirst(cur_packet); } } cur_packet = null; } else { is_header = false; } } else { // Log.info("not is header"); if (buf == null) { left_buf = buf; break; } buf = cur_packet.add_data(buf, 0, buf.length); if (cur_packet.is_ready()) { synchronized (packets) { // Log.info("recv packet "+cur_packet.toString()); cur_packet.offset = 0; packets.addFirst(cur_packet); } cur_packet = null; is_header = true; } } } }