private void DoSendItem(Element element, WriteMode mode, Action done) { fInSending = true; sb.Length = 0; element.ToString(sb, new WriteOptions { Mode = mode, StreamPrefix = "stream" }); byte[] data = Encoding.UTF8.GetBytes(sb.ToString()); //Console.WriteLine("Sending: " + sb); try { Connection cn = fConnection; if (cn != null) cn.BeginWrite(data, 0, data.Length, new AsyncCallback(ItemSent), done); } catch { } }
private void BeginStream() { OnConnected(); parser = new AsyncXmlParser(); parser.Origin = fConnection; fState = XMPP.State.Connected; fServerRoot = null; parser.ReadXmlElementAsync(new Action<XmlParserResult>(GotData), false); fRootElement = new ClientStream(); fRootElement.To = new JID(Domain); BeginSend(fRootElement, WriteMode.Open, null); BeginTimeout(); }
private Element CreateNode(XmlNodeResult nd, Element parent) { string ens; if (nd.Prefix != null) { RemObjects.InternetPack.XMPP.Elements.Attribute at = nd.Attribute.FirstOrDefault(a => a.Prefix == "xmlns" && a.Name == nd.Prefix); if (at == null) { Element el = parent; ens = string.Empty; while (el != null) { RemObjects.InternetPack.XMPP.Elements.Attribute els = el.Attributes.Where(a => a.Prefix == "xmlns" && a.Name == nd.Prefix).FirstOrDefault(); if (els != null) { ens = els.Value; break; } el = el.Parent; } } else ens = at.Value; } else { RemObjects.InternetPack.XMPP.Elements.Attribute at = nd.Attribute.FirstOrDefault(a => a.Prefix == null && a.Name == "xmlns"); if (at == null) ens = string.Empty; else ens = at.Value; } Element res = null; switch (ens) { case Namespaces.ClientStreamNamespace: case Namespaces.ServerStreamNamespace: case "": if (ens == null && parent != null && parent.Type == ElementType.IQ && nd.Name == "error") res = new IQError(); else switch (nd.Name) { case "iq": res = new IQ(); break; case "presence": res = new Presence(); break; case "message": res = new Message(); break; } break; case Namespaces.StreamNamespace: switch (nd.Name) { case "stream": RemObjects.InternetPack.XMPP.Elements.Attribute att = nd.Attribute.FirstOrDefault(a => a.Prefix == null && a.Name == "xmlns"); if (att == null || att.Value == Namespaces.ClientStreamNamespace) res = new ClientStream(); else res = new ServerStream(); break; case "features": res = new StreamFeatures(); break; case "error": res = new StreamError(); break; } break; case Namespaces.StartTLSNamespace: switch (nd.Name) { case "starttls": res = new StartTLS(); break; case "failure": res = new StartTLSFailure(); break; case "proceed": res = new StartTLSProceed(); break; } break; case Namespaces.SaslNamespace: switch (nd.Name) { case "mechanisms": res = new Mechanisms(); break; case "auth": res = new SaslAuth(); break; case "challenge": res = new SaslChallenge(); break; case "response": res = new SaslResponse(); break; case "abort": res = new SaslAbort(); break; case "success": res = new SaslSuccess(); break; case "failure": res = new SaslFailure(); break; } break; } if (res == null) { res = new UnknownElement(); } else res.Attributes.Clear(); // default ones shouldn't be here during the reading process if (parent != null) { res.Parent = parent; if (parent != fServerRoot) parent.Elements.Add(res); } res.Prefix = nd.Prefix; res.Name = nd.Name; foreach (var el in nd.Attribute) res.Attributes.Add(el); return res; }
private void AuthReply(Element el) { EndTimeout(); switch (el.Type) { case ElementType.SASLSuccess: fState = XMPP.State.Authenticated; fServerElementStack.Clear(); fServerRoot = null; OnAuthenticated(); BeginTimeout(); BeginSend(fRootElement, WriteMode.Open, null); break; case ElementType.SASLFailure: OnError(new SaslFailureException((SaslFailure)el)); Close(); break; default: OnError(new SaslFailureException("Invalid SASL reply: " + el.ToString())); Close(); break; } }
private void BeginSend(Element element, WriteMode wm, Action done) { lock (fItems) { fItems.AddLast(new QueuedItem { Element = element, Mode = wm, Done = done }); if (!fInSending) DoSendItem(element, wm, done); } }
private void RestartConnection(InternetPack.Connection newconn) { OnInitializedTLS(); fConnection = newconn; fState = XMPP.State.Connected; fServerElementStack.Clear(); fServerMechanisms = null; fServerRoot = null; parser.Origin = fConnection; parser.ReadXmlElementAsync(new Action<XmlParserResult>(GotData), fServerElementStack.Count > 0); BeginSend(fRootElement, WriteMode.Open, null); BeginTimeout(); }
private bool TLSReply(Element el) { EndTimeout(); if (el.Type == ElementType.StartTLSProceed) { SslOptions.TargetHostName = Domain; var newconn = SslOptions.CreateClientConnection(fConnection); BeginTimeout(); if (((SslConnection)newconn).BeginInitializeClientConnection((a) => { try { ((SslConnection)newconn).EndInitializeClientConnection(a); RestartConnection(newconn); } catch (Exception e) { OnError(e); Close(); } }, null) == null) { RestartConnection(newconn); } return true; } else { OnError(new TlsRequiredException("Server did not accept starttls request")); Close(); return false; } }
private void GotRootLevelElement(Element el) { if (fServerRoot != null) { SendStreamError(new StreamError { Error = StreamErrorKind.InvalidXml }); Close(); return; } fServerRoot = el; if (fServerRoot.NamespaceURI != Namespaces.StreamNamespace || fServerRoot.GetAttributeByName(null, "xmlns") != Namespaces.ClientStreamNamespace) { SendStreamError(new StreamError { Error = StreamErrorKind.BadNamespace }); return; } }
private bool GotFirstLevelElement(Element el) { switch (el.Type) { case ElementType.Presence: GotPresence((RemObjects.InternetPack.XMPP.Elements.Presence)el); break; case ElementType.Message: GotMessage((Elements.Message)el); break; case ElementType.IQ: GotIQ((Elements.IQ)el); break; case ElementType.StreamError: OnStreamError((Elements.StreamError)el); Close(); break; case ElementType.SASLAbort: case ElementType.SASLAuth: case ElementType.SASLChallenge: case ElementType.SASLFailure: case ElementType.SASLResponse: case ElementType.SASLSuccess: if (fState == State.Authenticating) AuthReply(el); break; case ElementType.StartTLSFailure: case ElementType.StartTLSProceed: if (fState == State.InitializingTLS) return TLSReply(el); break; case ElementType.StreamFeatures: if (fState == State.Connected || fState == XMPP.State.Authenticated) GotFeatures((Elements.StreamFeatures)el); break; } return false; }
public Element(Element parent, bool callInit) { fParent = parent; if (callInit) Init(); }
public Element(Element parent) : this(parent, true) { }
public void ReplaceOrRemoveElement(string @namespace, string name, Element el) { Element oldel = FindElement(@namespace, name); if (oldel != null) Elements.Remove(oldel); if (el != null) Elements.Add(el); }