From 71b02af4214ce3db63d4d5f822069cc125442434 Mon Sep 17 00:00:00 2001 From: Florian Zeitz Date: Wed, 25 Jan 2012 15:05:09 +0100 Subject: [PATCH] Generate stanza errors --- src/XMPPConnection.h | 4 +++- src/XMPPConnection.m | 55 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/XMPPConnection.h b/src/XMPPConnection.h index 59ccba7..05ef58e 100644 --- a/src/XMPPConnection.h +++ b/src/XMPPConnection.h @@ -216,11 +216,13 @@ withCallbackBlock: (void(^)(XMPPIQ*))callback; - (void)XMPP_handleSASL: (OFXMLElement*)element; - (void)XMPP_handleStanza: (OFXMLElement*)element; - (void)XMPP_sendAuth: (OFString*)authName; +- (void)XMPP_sendResourceBind; +- (void)XMPP_sendStreamError: (OFString*)condition + text: (OFString*)text; - (void)XMPP_handleIQ: (XMPPIQ*)iq; - (void)XMPP_handleMessage: (XMPPMessage*)message; - (void)XMPP_handlePresence: (XMPPPresence*)presence; - (void)XMPP_handleFeatures: (OFXMLElement*)element; -- (void)XMPP_sendResourceBind; - (void)XMPP_handleResourceBind: (XMPPIQ*)iq; - (void)XMPP_sendSession; - (void)XMPP_handleSession: (XMPPIQ*)iq; diff --git a/src/XMPPConnection.m b/src/XMPPConnection.m index c56130c..78a43f8 100644 --- a/src/XMPPConnection.m +++ b/src/XMPPConnection.m @@ -414,18 +414,38 @@ withCallbackBlock: (xmpp_callback_block)callback; OFEnumerator *enumerator; OFXMLAttribute *attribute; - if (![name isEqual: @"stream"] || ![prefix isEqual: @"stream"] || - ![ns isEqual: XMPP_NS_STREAM]) { - of_log(@"Did not get expected stream start!"); - assert(0); + if (![name isEqual: @"stream"]) { + // No dedicated stream error for this, may not even be XMPP + [self close]; + [sock close]; + return; + } + + if (![prefix isEqual: @"stream"]) { + [self XMPP_sendStreamError: @"bad-namespace-prefix" + text: nil]; + return; + } + + if (![ns isEqual: XMPP_NS_STREAM]) { + [self XMPP_sendStreamError: @"invalid-namespace" + text: nil]; + return; } enumerator = [attributes objectEnumerator]; while ((attribute = [enumerator nextObject]) != nil) { if ([[attribute name] isEqual: @"from"] && ![[attribute stringValue] isEqual: domain]) { - of_log(@"Got invalid from in stream start!"); - assert(0); + [self XMPP_sendStreamError: @"invalid-from" + text: nil]; + return; + } + if ([[attribute name] isEqual: @"version"] && + ![[attribute stringValue] isEqual: @"1.0"]) { + [self XMPP_sendStreamError: @"unsupported-version" + text: nil]; + return; } } @@ -534,7 +554,8 @@ withCallbackBlock: (xmpp_callback_block)callback; return; } - assert(0); + [self XMPP_sendStreamError: @"unsupported-stanza-type" + text: nil]; } @@ -885,6 +906,26 @@ withCallbackBlock: (xmpp_callback_block)callback; selector: @selector(XMPP_handleResourceBind:)]; } +- (void)XMPP_sendStreamError: (OFString*)condition + text: (OFString*)text +{ + OFXMLElement *error = [OFXMLElement + elementWithName: @"error" + namespace: XMPP_NS_STREAM]; + [error setPrefix: @"stream" + forNamespace: XMPP_NS_STREAM]; + [error addChild: [OFXMLElement elementWithName: condition + namespace: XMPP_NS_XMPP_STREAM]]; + if (text) + [error addChild: [OFXMLElement + elementWithName: @"text" + namespace: XMPP_NS_XMPP_STREAM + stringValue: text]]; + [parser setDelegate: nil]; + [self sendStanza: error]; + [self close]; +} + - (void)XMPP_handleResourceBind: (XMPPIQ*)iq { OFXMLElement *bindElement;