1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright (c) 2003-2012 by AG-Software 											 *
3  * All Rights Reserved.																 *
4  * Contact information for AG-Software is available at http://www.ag-software.de	 *
5  *																					 *
6  * Licence:																			 *
7  * The agsXMPP SDK is released under a dual licence									 *
8  * agsXMPP can be used under either of two licences									 *
9  * 																					 *
10  * A commercial licence which is probably the most appropriate for commercial 		 *
11  * corporate use and closed source projects. 										 *
12  *																					 *
13  * The GNU Public License (GPL) is probably most appropriate for inclusion in		 *
14  * other open source projects.														 *
15  *																					 *
16  * See README.html for details.														 *
17  *																					 *
18  * For general enquiries visit our website at:										 *
19  * http://www.ag-software.de														 *
20  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21 
22 using System;
23 
24 #if SSL
25 using System.Net.Security;
26 #endif
27 using System.Security.Cryptography.X509Certificates;
28 
29 namespace agsXMPP.Net
30 {
31 	/// <summary>
32 	/// Base Socket class
33 	/// </summary>
34 	public abstract class BaseSocket
35 	{
OnSocketDataHandler(object sender, byte[] data, int count)36 		public delegate void OnSocketDataHandler(object sender, byte[] data, int count);
37 
38         //public delegate void OnSocketCompressionDebugHandler(object sender, byte[] CompData, int CompCount, byte[] UncompData, int UncompCount);
39 
40         /*
41         // for compression debug statistics
42         public event OnSocketCompressionDebugHandler OnIncomingCompressionDebug;
43         public event OnSocketCompressionDebugHandler OnOutgoingCompressionDebug;
44 
45         protected void FireOnInComingCompressionDebug(object sender, byte[] CompData, int CompCount, byte[] UncompData, int UncompCount)
46         {
47             if (OnIncomingCompressionDebug != null)
48                 OnIncomingCompressionDebug(sender, CompData, CompCount, UncompData, UncompCount);
49         }
50 
51         protected void FireOnOutgoingCompressionDebug(object sender, byte[] CompData, int CompCount, byte[] UncompData, int UncompCount)
52         {
53             if (OnOutgoingCompressionDebug != null)
54                 OnOutgoingCompressionDebug(sender, CompData, CompCount, UncompData, UncompCount);
55         }
56         */
57 
58 #if SSL
59         public event RemoteCertificateValidationCallback    OnValidateCertificate;
60 #endif
61 //#if CF_2
62 //        public delegate bool CertificateValidationCallback(X509Certificate cert);
63 //        public event CertificateValidationCallback OnValidateCertificate;
64 //#endif
65 #if BCCRYPTO
CertificateValidationCallback(Org.BouncyCastle.Asn1.X509.X509CertificateStructure[] certs)66         public delegate bool CertificateValidationCallback(Org.BouncyCastle.Asn1.X509.X509CertificateStructure[] certs);
67         public event CertificateValidationCallback OnValidateCertificate;
68 #endif
69 		public event OnSocketDataHandler			        OnReceive;
70 		public event OnSocketDataHandler			        OnSend;
71 		public event ObjectHandler					        OnConnect;
72 		public event ObjectHandler					        OnDisconnect;
73 		public event ErrorHandler					        OnError;
74 
75 		private string	m_Address		    = null;
76 		private int		m_Port			    = 0;
77         private long    m_ConnectTimeout    = 10000; // 10 seconds is default
78 
79         internal XmppConnection  m_XmppCon = null;
80 
81         public bool IsEncrypted { get; protected set; }
82 
BaseSocket()83 		public BaseSocket()
84 		{
85 
86 		}
87 
88 		public string Address
89 		{
90 			get { return m_Address; }
91 			set { m_Address = value; }
92 		}
93 
94 		public int Port
95 		{
96 			get { return m_Port; }
97 			set { m_Port = value; }
98 		}
99 
FireOnConnect()100 		protected void FireOnConnect()
101 		{
102 			if (OnConnect != null)
103 				OnConnect(this);
104 		}
105 
FireOnDisconnect()106 		protected void FireOnDisconnect()
107 		{
108 			if (OnDisconnect != null)
109 				OnDisconnect(this);
110 		}
111 
FireOnReceive(byte[] b, int length)112 		protected void FireOnReceive(byte[] b, int length)
113 		{
114 			if (OnReceive != null)
115 				OnReceive(this, b, length);
116 		}
117 
FireOnSend(byte[] b, int length)118 		protected void FireOnSend(byte[] b, int length)
119 		{
120 			if (OnSend != null)
121 				OnSend(this, b, length);
122 		}
123 
FireOnError(Exception ex)124 		protected void FireOnError(Exception ex)
125 		{
126 			if (OnError != null)
127 				OnError(this, ex);
128 		}
129 
130 #if SSL
131         // The following method is invoked by the RemoteCertificateValidationDelegate.
FireOnValidateCertificate( object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)132         protected bool FireOnValidateCertificate(
133               object sender,
134               X509Certificate certificate,
135               X509Chain chain,
136               SslPolicyErrors sslPolicyErrors)
137         {
138             if (OnValidateCertificate != null)
139                 return OnValidateCertificate(sender, certificate, chain, sslPolicyErrors);
140             else
141                 return true;
142 
143             //if (sslPolicyErrors == SslPolicyErrors.None)
144             //    return true;
145 
146             //Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
147 
148             // Do not allow this client to communicate with unauthenticated servers.
149             //return false;
150         }
151 #endif
152 #if BCCRYPTO
FireOnValidateCertificate(Org.BouncyCastle.Asn1.X509.X509CertificateStructure[] certs)153         protected bool FireOnValidateCertificate(Org.BouncyCastle.Asn1.X509.X509CertificateStructure[] certs)
154         {
155             if (OnValidateCertificate != null)
156                 return OnValidateCertificate(certs);
157             else
158                 return true;
159 
160         }
161 #endif
162 
163 //#if CF_2
164 //        protected bool FireOnValidateCertificate(X509Certificate cert)
165 //        {
166 //            if (OnValidateCertificate != null)
167 //                return OnValidateCertificate(cert);
168 //            else
169 //                return true;
170 //        }
171 //#endif
172 		public virtual bool Connected
173 		{
174 			get { return false; }
175 		}
176 
177 		public virtual bool SupportsStartTls
178 		{
179 			get { return false; }
180 		}
181 
182         public virtual long ConnectTimeout
183         {
184             get { return m_ConnectTimeout; }
185             set { m_ConnectTimeout = value; }
186         }
187 
188 		#region << Methods >>
Connect()189 		public virtual void Connect()
190 		{
191 
192 		}
193 
Disconnect()194 		public virtual void Disconnect()
195 		{
196 
197 		}
198 
StartTls()199 		public virtual bool StartTls()
200 		{
201 		    return true;
202 		}
203 
StartCompression()204         public virtual void StartCompression()
205         {
206 
207         }
208 
209         /// <summary>
210         /// Added for Bosh because we have to tell the BoshClientSocket when to reset the stream
211         /// </summary>
Reset()212         public virtual void Reset()
213         {
214 
215         }
216 
217         /// <summary>
218 		///
219 		/// </summary>
220 		/// <param name="data"></param>
Send(string data)221 		public virtual void Send(string data)
222 		{
223 
224 		}
225 
226 		/// <summary>
227 		/// Send data to the server.
228 		/// </summary>
Send(byte[] bData)229 		public virtual void Send(byte[] bData)
230 		{
231 
232 		}
233 		#endregion
234     }
235 }