2008-11-01 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel / NetTcpBinding.cs
1 //
2 // NetTcpBinding.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.Net.Security;
31 using System.ServiceModel.Channels;
32 using System.ServiceModel.Description;
33 using System.ServiceModel.Security.Tokens;
34 using System.Text;
35 using System.Xml;
36
37 namespace System.ServiceModel
38 {
39         [MonoTODO]
40         public class NetTcpBinding : Binding, IBindingRuntimePreferences
41         {
42                 HostNameComparisonMode comparison_mode;
43                 long max_pool_size;
44                 int max_buf_size;
45                 int max_conn;
46                 long max_msg_size;
47                 OptionalReliableSession reliable_session;
48                 NetTcpSecurity security;
49                 XmlDictionaryReaderQuotas reader_quotas;
50                 EnvelopeVersion soap_version;
51                 bool transaction_flow;
52                 TransactionProtocol transaction_protocol;
53                 TransferMode transfer_mode;
54                 TcpTransportBindingElement transport = new TcpTransportBindingElement ();
55
56                 public NetTcpBinding ()
57                         : this (SecurityMode.Message)
58                 {
59                 }
60
61                 public NetTcpBinding (SecurityMode securityMode)
62                         : this (securityMode, false)
63                 {
64                 }
65
66                 public NetTcpBinding (SecurityMode securityMode,
67                         bool reliableSessionEnabled)
68                 {
69                         security = new NetTcpSecurity (securityMode);
70                 }
71
72                 public HostNameComparisonMode HostNameComparisonMode {
73                         get { return comparison_mode; }
74                         set { comparison_mode = value; }
75                 }
76
77                 public int ListenBacklog {
78                         get { return transport.ListenBacklog; }
79                         set { transport.ListenBacklog = value; }
80                 }
81
82                 public long MaxBufferPoolSize {
83                         get { return max_pool_size; }
84                         set { max_pool_size = value; }
85                 }
86
87                 public int MaxBufferSize {
88                         get { return max_buf_size; }
89                         set { max_buf_size = value; }
90                 }
91
92                 public int MaxConnections {
93                         get { return max_conn; }
94                         set { max_conn = value; }
95                 }
96
97                 public long MaxReceivedMessageSize {
98                         get { return max_msg_size; }
99                         set { max_msg_size = value; }
100                 }
101
102                 public bool PortSharingEnabled {
103                         get { return transport.PortSharingEnabled; }
104                         set { transport.PortSharingEnabled = value; }
105                 }
106
107                 public OptionalReliableSession ReliableSession {
108                         get { return reliable_session; }
109                 }
110
111                 public XmlDictionaryReaderQuotas ReaderQuotas {
112                         get { return reader_quotas; }
113                         set { reader_quotas = value; }
114                 }
115
116                 public NetTcpSecurity Security {
117                         get { return security; }
118                 }
119
120                 public EnvelopeVersion EnvelopeVersion {
121                         get { return soap_version; }
122                 }
123
124                 public TransferMode TransferMode {
125                         get { return transfer_mode; }
126                         set { transfer_mode = value; }
127                 }
128
129                 public bool TransactionFlow {
130                         get { return transaction_flow; }
131                         set { transaction_flow = value; }
132                 }
133
134                 public TransactionProtocol TransactionProtocol {
135                         get { return transaction_protocol; }
136                         set { transaction_protocol = value; }
137                 }
138
139                 // overrides
140
141                 public override string Scheme {
142                         get { return "net.tcp"; }
143                 }
144
145                 public override BindingElementCollection CreateBindingElements ()
146                 {
147                         BindingElement tx = new TransactionFlowBindingElement (TransactionProtocol.WSAtomicTransactionOctober2004);
148                         SecurityBindingElement sec = CreateMessageSecurity ();
149                         BindingElement msg = new BinaryMessageEncodingBindingElement ();
150                         BindingElement tr = GetTransport ();
151                         List<BindingElement> list = new List<BindingElement> ();
152                         if (tx != null)
153                                 list.Add (tx);
154                         if (sec != null)
155                                 list.Add (sec);
156                         list.Add (msg);
157                         list.Add (tr);
158                         return new BindingElementCollection (list.ToArray ());
159                 }
160
161                 BindingElement GetTransport ()
162                 {
163                         return transport.Clone ();
164                 }
165
166                 // based on WSHttpBinding.CreateMessageSecurity()
167                 SecurityBindingElement CreateMessageSecurity ()
168                 {
169                         if (Security.Mode == SecurityMode.Transport ||
170                             Security.Mode == SecurityMode.None)
171                                 return null;
172
173                         SymmetricSecurityBindingElement element =
174                                 new SymmetricSecurityBindingElement ();
175
176                         element.MessageSecurityVersion = MessageSecurityVersion.Default;
177
178                         element.SetKeyDerivation (false);
179
180                         switch (Security.Message.ClientCredentialType) {
181                         case MessageCredentialType.Certificate:
182                                 element.EndpointSupportingTokenParameters.Endorsing.Add (
183                                         new X509SecurityTokenParameters ());
184                                 goto default;
185                         case MessageCredentialType.IssuedToken:
186                                 IssuedSecurityTokenParameters istp =
187                                         new IssuedSecurityTokenParameters ();
188                                 // FIXME: issuer binding must be secure.
189                                 istp.IssuerBinding = new CustomBinding (
190                                         new TextMessageEncodingBindingElement (),
191                                         GetTransport ());
192                                 element.EndpointSupportingTokenParameters.Endorsing.Add (istp);
193                                 goto default;
194                         case MessageCredentialType.UserName:
195                                 element.EndpointSupportingTokenParameters.SignedEncrypted.Add (
196                                         new UserNameSecurityTokenParameters ());
197                                 goto default;
198                         case MessageCredentialType.Windows:
199                                 element.ProtectionTokenParameters =
200                                         new KerberosSecurityTokenParameters ();
201                                 break;
202                         default: // including .None
203                                 X509SecurityTokenParameters p =
204                                         new X509SecurityTokenParameters ();
205                                 p.X509ReferenceStyle = X509KeyIdentifierClauseType.Thumbprint;
206                                 element.ProtectionTokenParameters = p;
207                                 break;
208                         }
209
210                         return element;
211                 }
212
213                 bool IBindingRuntimePreferences.ReceiveSynchronously {
214                         get { throw new NotImplementedException (); }
215                 }
216         }
217 }