Mono.Zeroconf – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 // Copyright 2006 Alp Toker <alp@atoker.com>
2 // This software is made available under the MIT License
3 // See COPYING for details
4  
5 using System;
6 using System.Collections.Generic;
7 using System.IO;
8 using System.Text;
9 using System.Globalization;
10  
11 namespace NDesk.DBus.Authentication
12 {
13 enum ClientState
14 {
15 WaitingForData,
16 WaitingForOK,
17 WaitingForReject,
18 }
19  
20 enum ServerState
21 {
22 WaitingForAuth,
23 WaitingForData,
24 WaitingForBegin,
25 }
26  
27 class SaslClient
28 {
29 protected Connection conn;
30  
31 protected SaslClient ()
32 {
33 }
34  
35 public SaslClient (Connection conn)
36 {
37 this.conn = conn;
38 }
39  
40 public void Run ()
41 {
42 StreamReader sr = new StreamReader (conn.Transport.Stream, Encoding.ASCII);
43 StreamWriter sw = new StreamWriter (conn.Transport.Stream, Encoding.ASCII);
44  
45 sw.NewLine = "\r\n";
46  
47 string str = conn.Transport.AuthString ();
48 byte[] bs = Encoding.ASCII.GetBytes (str);
49  
50 string authStr = ToHex (bs);
51  
52 sw.WriteLine ("AUTH EXTERNAL {0}", authStr);
53 sw.Flush ();
54  
55 string ok_rep = sr.ReadLine ();
56  
57 string[] parts;
58 parts = ok_rep.Split (' ');
59  
60 if (parts.Length < 1 || parts[0] != "OK")
61 throw new Exception ("Authentication error: AUTH EXTERNAL was not OK: \"" + ok_rep + "\"");
62  
63 /*
64 string guid = parts[1];
65 byte[] guidData = FromHex (guid);
66 uint unixTime = BitConverter.ToUInt32 (guidData, 0);
67 Console.Error.WriteLine ("guid: " + guid + ", " + "unixTime: " + unixTime + " (" + UnixToDateTime (unixTime) + ")");
68 */
69  
70 sw.WriteLine ("BEGIN");
71 sw.Flush ();
72 }
73  
74 //From Mono.Unix.Native.NativeConvert
75 //should these methods use long or (u)int?
76 public static DateTime UnixToDateTime (long time)
77 {
78 DateTime LocalUnixEpoch = new DateTime (1970, 1, 1);
79 TimeSpan LocalUtcOffset = TimeZone.CurrentTimeZone.GetUtcOffset (DateTime.UtcNow);
80 return LocalUnixEpoch.AddSeconds ((double) time + LocalUtcOffset.TotalSeconds);
81 }
82  
83 public static long DateTimeToUnix (DateTime time)
84 {
85 DateTime LocalUnixEpoch = new DateTime (1970, 1, 1);
86 TimeSpan LocalUtcOffset = TimeZone.CurrentTimeZone.GetUtcOffset (DateTime.UtcNow);
87 TimeSpan unixTime = time.Subtract (LocalUnixEpoch) - LocalUtcOffset;
88  
89 return (long) unixTime.TotalSeconds;
90 }
91  
92 //From Mono.Security.Cryptography
93 //Modified to output lowercase hex
94 static public string ToHex (byte[] input)
95 {
96 if (input == null)
97 return null;
98  
99 StringBuilder sb = new StringBuilder (input.Length * 2);
100 foreach (byte b in input) {
101 sb.Append (b.ToString ("x2", CultureInfo.InvariantCulture));
102 }
103 return sb.ToString ();
104 }
105  
106 //From Mono.Security.Cryptography
107 static private byte FromHexChar (char c)
108 {
109 if ((c >= 'a') && (c <= 'f'))
110 return (byte) (c - 'a' + 10);
111 if ((c >= 'A') && (c <= 'F'))
112 return (byte) (c - 'A' + 10);
113 if ((c >= '0') && (c <= '9'))
114 return (byte) (c - '0');
115 throw new ArgumentException ("Invalid hex char");
116 }
117  
118 //From Mono.Security.Cryptography
119 static public byte[] FromHex (string hex)
120 {
121 if (hex == null)
122 return null;
123 if ((hex.Length & 0x1) == 0x1)
124 throw new ArgumentException ("Length must be a multiple of 2");
125  
126 byte[] result = new byte [hex.Length >> 1];
127 int n = 0;
128 int i = 0;
129 while (n < result.Length) {
130 result [n] = (byte) (FromHexChar (hex [i++]) << 4);
131 result [n++] += FromHexChar (hex [i++]);
132 }
133 return result;
134 }
135 }
136 }