[Ironruby-core] Code Review: socket2

John Lam (IRONRUBY) jflam at microsoft.com
Mon May 5 13:07:13 EDT 2008


tfpt review /shelveset:socket2;REDMOND\jflam

Overall - looks good! We really want to turn on tests for this stuff. Jim's looking at getting the latest rubinius specs running so we can deprecate our current spec snapshot soon - hopefully this week.

A few minor nits below:

Seems like you have a couple of redundant Protocols.CastToString calls here - domain is *always* a MutableString based on your ctor ...

        private static Socket CreateSocket(CodeContext/*!*/ context, MutableString/*!*/ domain, MutableString/*!*/ type, object/*Numeric*/ protocol) {
            RubyExecutionContext ec = RubyUtils.GetExecutionContext(context);
            RubyClass rubySocketClass = ec.GetClass(typeof(Ruby.StandardLibrary.RubySocket));

            AddressFamily addressFamily = (AddressFamily)RubyUtils.GetConstant(context, rubySocketClass, SymbolTable.StringToId(Protocols.CastToString(context, domain)), true);
            ProtocolType protocolType = (ProtocolType)(Protocols.CastToFixnum(context, protocol));
            SocketType socketType = (SocketType)RubyUtils.GetConstant(context, rubySocketClass, SymbolTable.StringToId(Protocols.CastToString(context, type)), true);
            return new Socket(addressFamily, socketType, protocolType);
        }

Also ... Socket should be Socket/*!*/ since there is only one code path out of this method barring exceptions. I've fixed these minor things in my copy.

Following this analysis, this also leads to _socket in RubyBasicSocket:23 being a /*!*/ as well.

BTW, I really like the /*Numeric*/ annotations that you've been putting in your code. This could lead us to doing something smarter in the future in the binder like adding a parameter attribute [Numeric] to coerce the binder into doing some smarter things with the conversions.

Thinking aloud about this method - would it make more sense to have MutableString and int overloads that delegate to helpers rather than dropping everything into a single method like this one here?

        internal static MutableString ConvertToHostString(CodeContext/*!*/ context, object hostname) {
            if (hostname == null) {
                return null;
            }
            if (hostname is MutableString) {
                MutableString strHostname = (MutableString)hostname;
                // Special cases
                if (strHostname == "" ) {
                    strHostname = new MutableString("0.0.0.0");
                } else if (strHostname == "<broadcast>") {
                    strHostname = new MutableString("255.255.255.255");
                }
                return strHostname;
            }
            int iHostname;
            if (Protocols.IntegerAsFixnum(hostname, out iHostname)) {
                // Ruby uses Little Endian whereas .NET uses Big Endian IP values
                byte[] bytes = new byte[4];
                for (int i = 3; i >= 0; --i) {
                    bytes[i] = (byte)(iHostname & 0xff);
                    iHostname >>= 8;
                }
                return new MutableString(new System.Net.IPAddress(bytes).ToString());
            }
            return Protocols.CastToString(context, hostname);
        }

Some other points - seems some of the code below could be delegated to the Protocols.CheckSafeLevel() helpers that you added. BTW, we should probably spend some time and make a call about what we're going to do about all of the safe level stuff. Arguably this stuff isn't necessary due to CLR safety (especially in Silverlight contexts).

        internal static void CheckSecurity(CodeContext/*!*/ context, object self, string message) {
            RubyExecutionContext ec = RubyUtils.GetExecutionContext(context);
            if (ec.CurrentSafeLevel >= 4 && ec.IsObjectTainted(self)) {
                throw RubyExceptions.CreateSecurityError("Insecure: " + message);
            }
        }

Thanks,
-John

-------------- next part --------------
A non-text attachment was scrubbed...
Name: socket2.diff
Type: application/octet-stream
Size: 160634 bytes
Desc: socket2.diff
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20080505/b064af79/attachment-0001.obj>


More information about the Ironruby-core mailing list