public static void getpeername(int handle, out byte[] address) { if (!_isInitialized) { Initialize(); } Netduino.IP.LinkLayers.CC3100.CC3100SocketInfo socketInfo = _cc3100.GetSocketInfo(handle); UInt32 ipAddress = socketInfo.RemoteIPAddress; UInt16 ipPort = socketInfo.RemoteIPPort; address = new byte[8]; if (SystemInfo.IsBigEndian) { address[0] = 0x00; /* InterNetwork = 0x0002 */ address[1] = 0x02; /* InterNetwork = 0x0002 */ } else { address[0] = 0x02; /* InterNetwork = 0x0002 */ address[1] = 0x00; /* InterNetwork = 0x0002 */ } address[2] = (byte)((ipPort >> 8) & 0xFF); address[3] = (byte)(ipPort & 0xFF); address[4] = (byte)((ipAddress >> 24) & 0xFF); address[5] = (byte)((ipAddress >> 16) & 0xFF); address[6] = (byte)((ipAddress >> 8) & 0xFF); address[7] = (byte)(ipAddress & 0xFF); }
public static void getsockopt(int handle, int level, int optname, byte[] optval) { if (!_isInitialized) { Initialize(); } switch (level) { case 0xffff: /* SocketOptionLevel.Socket */ { /* filter for CC3100-specific optnames */ switch (optname) { case 0x001008: /* SocketOptionName.Type */ { Netduino.IP.LinkLayers.CC3100.CC3100SocketInfo socketInfo = _cc3100.GetSocketInfo(handle); optval[0] = (byte)(((Int32)socketInfo.SocketSocketType) & 0xFF); optval[1] = (byte)((((Int32)socketInfo.SocketSocketType) >> 8) & 0xFF); optval[2] = (byte)((((Int32)socketInfo.SocketSocketType) >> 16) & 0xFF); optval[3] = (byte)((((Int32)socketInfo.SocketSocketType) >> 24) & 0xFF); } break; case 0x400000: /* map to SL_SO_SECMETHOD */ { int retVal = _cc3100.sl_GetSockOpt(handle, 1 /* SL_SOL_SOCKET */, 25 /* SL_SO_SECMETHOD */, optval); if (retVal < 0) { throw new CC3100SimpleLinkException(retVal); /* TODO: what is the correct exception? */ } } break; case 0x400002: /* trigger to upgrade to SSL/TLS (by closing an open socket and re-opening it in secure mode using the pre-specified security method/mask */ { // bytes 0-7: ipaddress // byte 8: sslProtocolOption // byte 9: where we return the new handle (or 0xFF on error) // close the socket _cc3100.sl_Close(handle); // re-open the socket int newHandle = 0xFF; // default to "invalid handle" int retVal = _cc3100.sl_Socket(Netduino.IP.LinkLayers.CC3100.SocketAddressFamily.IPv4, Netduino.IP.LinkLayers.CC3100.SocketSocketType.Stream, Netduino.IP.LinkLayers.CC3100.SocketProtocolType.Secure); if (retVal >= 0) { newHandle = retVal; // set SSL security protocol option(s) setsockopt(handle, 0xffff /* SocketOptionLevel.Socket */, 0x400000 /* map to SL_SO_SECMETHOD */, new byte[] { optval[8] }); // parse ipAddress UInt16 ipPort = (UInt16)(((UInt16)optval[2] << 8) + (UInt16)optval[3]); UInt32 ipAddress = ((UInt32)optval[4] << 24) + ((UInt32)optval[5] << 16) + ((UInt32)optval[6] << 8) + (UInt32)optval[7]; // re-open socket retVal = _cc3100.sl_Connect(newHandle, Netduino.IP.LinkLayers.CC3100.SocketAddressFamily.IPv4, ipAddress, ipPort); if (retVal >= 0 || retVal == -453 /* SL_ESECSNOVERIFY */) { optval[9] = (byte)newHandle; } else { //Debug.Print("SSL Socket Upgrade Failure; Error # " + retVal); } } } break; default: throw new NotSupportedException(); } } break; default: throw new NotSupportedException(); } }