[Mono-list] UnixEndPoint

Philipp Baer phbaer at npw.net
Mon Mar 6 11:41:07 EST 2006


Hi,

it's me again. I came across another issue with UnixEndPoint:
- the unix socket pathname in the sockaddr structure has to
  be zero-terminated. I've modified the serialization method
  so that a trailing zero is appended.
- the pathname returned by a recvfrom call contains a trailing
  zero which is preserved by the .NET string. This leads to
  a different string and thus a different hash.

I've attached the patch including the missing Equals method :)


phb

-- 
Philipp Baer <phbaer at npw.net> [http://www.npw.net/]
--------------- contact information ---------------
mobile |+49-179-488 26 63|  fax |+49-561-804 62 77|
most current contact details  |'whois pb4412-ripe'|
---------------- gnupg-fingerprint ----------------
|16C7 84E8 5C5F C3D6 A8F1 A4DC E4CB A9A9 F5FA FF5D|
-------------- next part --------------
Index: UnixEndPoint.cs
===================================================================
--- UnixEndPoint.cs	(revision 57605)
+++ UnixEndPoint.cs	(working copy)
@@ -76,16 +76,22 @@
 			}
 
 			string name = Encoding.Default.GetString (bytes);
-			return new UnixEndPoint (name);
+			return new UnixEndPoint (name.TrimEnd (new char[] { '\0' }));
 		}
 
 		public override SocketAddress Serialize ()
 		{
-			byte [] bytes = Encoding.Default.GetBytes (filename);
-			SocketAddress sa = new SocketAddress (AddressFamily, bytes.Length + 2);
+			// Remove trailing zeros
+			string cleanFilename = filename.TrimEnd (new char[] { '\0' });
+			byte [] bytes = Encoding.Default.GetBytes (cleanFilename);
+			// Reserve one extra byte for the terminating null
+			SocketAddress sa = new SocketAddress (AddressFamily, bytes.Length + 3);
 			// sa [0] -> family low byte, sa [1] -> family high byte
-			for (int i = 0; i < bytes.Length; i++)
+			for (int i = 0; i < bytes.Length; i++) {
 				sa [i + 2] = bytes [i];
+			}
+			// Add one trailing zero
+			sa [bytes.Length + 2] = 0;
 
 			return sa;
 		}
@@ -93,6 +99,19 @@
 		public override string ToString() {
 			return(filename);
 		}
+
+		public override int GetHashCode() {
+			return filename.GetHashCode();
+		}
+
+		public override bool Equals(object o) {
+			if (o is UnixEndPoint) {
+				// check whether the two filenames are equal
+				return filename.Equals(((UnixEndPoint)o).Filename);
+			}
+
+			return false;
+		}
 	}
 }
 


More information about the Mono-list mailing list