Use XPathNavigator output in WCF diagnostics. Got sorta-valid message trace.
[mono.git] / mcs / class / System.ServiceModel / Mono.Security.Protocol.Ntlm / NtlmTargetInformation.cs
1 //
2 // Mono.Security.Protocol.Ntlm.NtlmTargetInformation
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // (C) 2007 Novell, Inc. (http://www.novell.com)
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 using System;
31 using System.IO;
32 using System.Text;
33
34 namespace Mono.Security.Protocol.Ntlm {
35         public class NtlmTargetInformation {
36                 string _server, _domain, _dns_host, _dns_domain;
37
38                 public string ServerName {
39                         get { return _server; }
40                         set { _server = value; }
41                 }
42
43                 public string DomainName {
44                         get { return _domain; }
45                         set { _domain = value; }
46                 }
47
48                 public string DnsHostName {
49                         get { return _dns_host; }
50                         set { _dns_host = value; }
51                 }
52
53                 public string DnsDomainName {
54                         get { return _dns_domain; }
55                         set { _dns_domain = value; }
56                 }
57
58                 public void Decode (byte [] bytes, int length, int offset)
59                 {
60                         int end = offset + length;
61                         for (int pos = offset; pos < end;) {
62                                 short type = BitConverterLE.ToInt16 (bytes, pos); // reader.ReadInt16 ();
63                                 short blen = BitConverterLE.ToInt16 (bytes, pos + 2); // reader.ReadInt16 ();
64                                 string s = Encoding.Unicode.GetString (bytes, pos + 4, blen);
65                                 pos += blen + 4;
66                                 switch (type) {
67                                 case 0: break; // terminator
68                                 case 1: ServerName = s; break;
69                                 case 2: DomainName = s; break;
70                                 case 3: DnsHostName = s; break;
71                                 case 4: DnsDomainName = s; break;
72                                 default:
73                                         throw new ArgumentException (String.Format ("Invalid SSPI message type 2 subblock type: {0}", type));
74                                 }
75                                 if (type == 0)
76                                         break; // terminator subblock
77                         }
78                 }
79
80                 public byte [] ToBytes ()
81                 {
82                         MemoryStream ms = new MemoryStream ();
83                         BinaryWriter bw = new BinaryWriter (ms);
84
85                         WriteName (bw, 1, ServerName);
86                         WriteName (bw, 2, DomainName);
87                         WriteName (bw, 3, DnsHostName);
88                         WriteName (bw, 4, DnsDomainName);
89                         bw.Close ();
90                         return ms.ToArray ();
91                 }
92
93                 private void WriteName (BinaryWriter bw, short type, string value)
94                 {
95                         if (value == null)
96                                 return;
97                         byte [] bytes = Encoding.Unicode.GetBytes (value);
98                         bw.Write (type);
99                         bw.Write ((short) bytes.Length);
100                         bw.Write (bytes);
101                 }
102         }
103 }