/** * Make sure that the received packet has a TO and FROM values defined and that it was sent * from a previously validated domain. If the packet does not matches any of the above * conditions then a PacketRejectedException will be thrown. * * @param packet the received packet. * @throws UnauthorizedException if the packet does not include a TO or FROM or if the packet * was sent from a domain that was not previously validated. */ private void packetReceived(Packet packet) throws UnauthorizedException { if (packet.getTo() == null || packet.getFrom() == null) { Log.debug("ServerStanzaHandler: Closing IncomingServerSession due to packet with no TO or FROM: " + packet.toXML()); // Send a stream error saying that the packet includes no TO or FROM StreamError error = new StreamError(StreamError.Condition.improper_addressing); connection.deliverRawText(error.toXML()); throw new UnauthorizedException("Packet with no TO or FROM attributes"); } else if (!((LocalIncomingServerSession) session).isValidDomain(packet.getFrom().getDomain())) { Log.debug("ServerStanzaHandler: Closing IncomingServerSession due to packet with invalid domain: " + packet.toXML()); // Send a stream error saying that the packet includes an invalid FROM StreamError error = new StreamError(StreamError.Condition.invalid_from); connection.deliverRawText(error.toXML()); throw new UnauthorizedException("Packet with no TO or FROM attributes"); } }
/** * Generate a conflict packet to indicate that the nickname being requested/used is already in * use by another user. * * @param packet the packet to be bounced. * @param error the reason why the operation failed. */ private void sendErrorPacket(Packet packet, PacketError.Condition error) { if (packet instanceof IQ) { IQ reply = IQ.createResultIQ((IQ) packet); reply.setChildElement(((IQ) packet).getChildElement().createCopy()); reply.setError(error); router.route(reply); } else { Packet reply = packet.createCopy(); reply.setError(error); reply.setFrom(packet.getTo()); reply.setTo(packet.getFrom()); router.route(reply); } }
public Deliverable(Collection<Packet> elements) { this.text = null; this.packets = new ArrayList<>(); for (Packet packet : elements) { // Append packet namespace according XEP-0206 if needed if (Namespace.NO_NAMESPACE.equals(packet.getElement().getNamespace())) { // use string-based operation here to avoid cascading xmlns wonkery StringBuilder packetXml = new StringBuilder(packet.toXML()); final int noslash = packetXml.indexOf( ">" ); final int slash = packetXml.indexOf( "/>" ); final int insertAt = ( noslash - 1 == slash ? slash : noslash ); packetXml.insert( insertAt, " xmlns=\"jabber:client\""); this.packets.add(packetXml.toString()); } else { this.packets.add(packet.toXML()); } } }
@Override public void process(Packet packet) throws UnauthorizedException, PacketException { boolean handled = false; String host = packet.getTo().getDomain(); for (Channel<Packet> channel : transports.values()) { if (channel.getName().equalsIgnoreCase(host)) { channel.add(packet); handled = true; } } if (!handled) { JID recipient = packet.getTo(); JID sender = packet.getFrom(); packet.setError(PacketError.Condition.remote_server_timeout); packet.setFrom(recipient); packet.setTo(sender); try { deliverer.deliver(packet); } catch (PacketException e) { Log.error(LocaleUtils.getLocalizedString("admin.error"), e); } } }
@Override public void interceptPacket(Packet packet, Session session, boolean read, boolean processed) { if (!processed) { // Ignore packets sent or received by users that are present in the ignore list JID from = packet.getFrom(); JID to = packet.getTo(); if ((from == null || !ignoreList.contains(from.getNode())) && (to == null || !ignoreList.contains(to.getNode()))) { auditor.audit(packet, session); } } } }
/** * Notification message indicating that a packet has failed to be routed to the recipient. * * @param recipient address of the entity that failed to receive the packet. * @param packet Presence packet that failed to be sent to the recipient. */ public void routingFailed( JID recipient, Packet packet ) { Log.debug( "Presence sent to unreachable address: " + packet.toXML() ); // presence packets are dropped silently } }
private void returnErrorToSender(Packet packet) { RoutingTable routingTable = XMPPServer.getInstance().getRoutingTable(); if (packet.getError() != null) { Log.debug("Possible double bounce: " + packet.toXML()); Log.debug("XMPP specs forbid us to respond with an IQ error to: " + packet.toXML()); return; reply.setID(packet.getID()); reply.setTo(packet.getFrom()); reply.setFrom(packet.getTo()); reply.setChildElement(((IQ) packet).getChildElement().createCopy()); reply.setType(IQ.Type.error); Log.debug("Double-bounce of presence: " + packet.toXML()); return; reply.setID(packet.getID()); reply.setTo(packet.getFrom()); reply.setFrom(packet.getTo()); reply.setType(Presence.Type.error); reply.setError(PacketError.Condition.remote_server_not_found); Log.debug("Double-bounce of message: " + packet.toXML()); return; reply.setID(packet.getID()); reply.setTo(packet.getFrom()); reply.setFrom(packet.getTo()); reply.setType(Message.Type.error);
boolean fromServer) { boolean routed = false; Element privateElement = packet.getElement().element(QName.get("private", "urn:xmpp:carbons:2")); boolean isPrivate = privateElement != null; packet.getElement().remove(privateElement); throw new PacketException("Cannot route packet of type IQ or Presence to bare JID: " + packet.toXML()); !presenceUpdateHandler.hasDirectPresence(packet.getTo(), packet.getFrom())) { Log.debug("Unable to route packet. Packet should only be sent to available sessions and the route is not available. {} ", packet.toXML()); routed = false; } else { localRoutingTable.getRoute(route).process(carbon); } catch (UnauthorizedException e) { Log.error("Unable to route packet " + packet.toXML(), e); routed = true; } catch (UnauthorizedException e) { Log.error("Unable to route packet " + packet.toXML(), e);
@Override public void process(Packet packet) throws UnauthorizedException, PacketException { process((Presence) packet, sessionManager.getSession(packet.getFrom())); }
@Override public void process(Packet packet) throws UnauthorizedException, PacketException { try { JID recipient = packet.getTo(); // Check if the target domain belongs to a remote server or a component if (server.matchesComponent(recipient) || server.isRemote(recipient)) { routingTable.routePacket(recipient, packet, false); } // The target domain belongs to the local server else if (recipient == null || (recipient.getNode() == null && recipient.getResource() == null)) { // no TO was found so send back the packet to the sender routingTable.routePacket(packet.getFrom(), packet, false); } else if (recipient.getResource() != null || !(packet instanceof Presence)) { // JID is of the form <user@domain/resource> routingTable.routePacket(recipient, packet, false); } else { // JID is of the form <user@domain> for (JID route : routingTable.getRoutes(recipient, null)) { routingTable.routePacket(route, packet, false); } } } catch (Exception e) { Log.error(LocaleUtils.getLocalizedString("admin.error.deliver") + "\n" + packet.toString(), e); } } }
private void returnErrorToSender(Packet packet) { XMPPServer server = XMPPServer.getInstance(); JID from = packet.getFrom(); JID to = packet.getTo(); if (!server.isLocal(from) && !XMPPServer.getInstance().matchesComponent(from) && !server.isLocal(to) && !XMPPServer.getInstance().matchesComponent(to)) { if (packet instanceof IQ) { IQ reply = new IQ(); reply.setID(packet.getID()); reply.setTo(from); reply.setFrom(to); reply.setID(packet.getID()); reply.setTo(route); reply.setFrom(to); reply.setID(packet.getID()); reply.setTo(from); reply.setFrom(to); final ClientSession session = sessionManager.getSession( reply.getTo() ); InterceptorManager.getInstance().invokeInterceptors( reply, session, false, false ); routingTable.routePacket( reply.getTo(), reply, true ); InterceptorManager.getInstance().invokeInterceptors( reply, session, false, true );
/** * @see org.xmpp.component.Component#processPacket(org.xmpp.packet.Packet) */ final public void processPacket(final Packet packet) { final Packet copy = packet.createCopy(); if (executor == null) { } try { executor.execute(new PacketProcessor(copy)); } catch (RejectedExecutionException ex) { log.error("(serving component '" + getName() + "') Unable to process packet! " + "Is the thread pool queue exhausted? " + "Packet dropped in component '" + getName() + "'. Packet that's dropped: " + packet.toXML(), ex); // If the original packet was an IQ request, we should return an // error. if (packet instanceof IQ && ((IQ) packet).isRequest()) { final IQ response = IQ.createResultIQ((IQ) packet); response.setError(Condition.internal_server_error); send(response); } } }
public InterceptedPacket(Packet packet, boolean incoming, boolean processed) { packetClass = packet.getClass(); this.element = packet.getElement(); this.incoming = incoming; this.processed = processed; creationDate = new Date(); }
unacknowledgedServerStanzas.addLast( new StreamManager.UnackedPacket( x, packet.createCopy() ) ); Log.trace( "Added stanza of type '{}' to collection of unacknowledged stanzas (x={}). Collection size is now {}.", packet.getElement().getName(), x, size );
public AuditPacket(Packet packet, Session session) { element = docFactory.createElement("packet", "http://www.jivesoftware.org"); creationDate = new Date(); if (session != null && session.getStreamID() != null) { element.addAttribute("streamID", session.getStreamID().toString()); } switch (session == null ? 0 : session.getStatus()) { case Session.STATUS_AUTHENTICATED: element.addAttribute("status", "auth"); break; case Session.STATUS_CLOSED: element.addAttribute("status", "closed"); break; case Session.STATUS_CONNECTED: element.addAttribute("status", "connected"); // This is a workaround. Since we don't want to have an incorrect FROM attribute // value we need to clean up the FROM attribute. The FROM attribute will contain // an incorrect value since we are setting a fake JID until the user actually // authenticates with the server. packet.setFrom((String) null); break; default: element.addAttribute("status", "unknown"); break; } element.addAttribute("timestamp", auditFormat.format(creationDate)); element.add(packet.getElement()); }
public void route(Packet packet) { Set<String> remoteServers = new HashSet<>(); List<String> targets = new ArrayList<>(); Packet localBroadcast = packet.createCopy(); Element addresses = getAddresses(localBroadcast); String localDomain = "@" + server.getServerInfo().getXMPPDomain(); localBroadcast.setTo(jid); packetRouter.route(localBroadcast);
/** * Returns a value that is an appropriate unique and stable stanza ID in * context of XEP-0359: it's either the origin-id value, or a UUID. * * @param packet The stanza for what to return the ID (cannot be null). * @return The ID (never null or empty string). */ public static String generateUniqueAndStableStanzaID( final Packet packet ) { String result = null; final Iterator<Element> existingElementIterator = packet.getElement().elementIterator( QName.get( "origin-id", "urn:xmpp:sid:0" ) ); while (existingElementIterator.hasNext() && (result == null || result.isEmpty() ) ) { final Element element = existingElementIterator.next(); result = element.attributeValue( "id" ); } if ( result == null || result.isEmpty() ) { result = UUID.randomUUID().toString(); Log.debug( "Using newly generated value '{}' for stanza that has id '{}'.", result, packet.getID() ); } else { Log.debug( "Using origin-id provided value '{}' for stanza that has id '{}'.", result, packet.getID() ); } return result; } }
@Override public void process(Packet packet) { // Queue the packet. Another process will process the queued packets. packets.add(packet.createCopy()); }
/** * Sets the packet error using the specified condition. Calling this * method will automatically set the packet "type" attribute to "error". * This is a convenience method equivalent to calling: * * <tt>setError(new PacketError(condition));</tt> * * @param condition the error condition. */ public void setError(PacketError.Condition condition) { setError(new PacketError(condition)); }