Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / SystemNet / Net / PeerToPeer / SystemNetHelpers.cs / 1305376 / SystemNetHelpers.cs
namespace System.Net.PeerToPeer { using System; using System.Net; using System.Net.Sockets; using System.Collections.Generic; using System.Text; using System.Security.Permissions; internal static class SystemNetHelpers { internal const int IPv6AddressSize = 28; internal const int IPv4AddressSize = 16; internal static byte[] SOCKADDRFromIPEndPoint(IPEndPoint ipEndPoint) { byte[] buffer = new byte[ipEndPoint.AddressFamily == AddressFamily.InterNetworkV6 ? IPv6AddressSize : IPv4AddressSize]; #if BIGENDIAN buffer[0] = unchecked((byte)((int)family>>8)); buffer[1] = unchecked((byte)((int)family )); #else buffer[0] = unchecked((byte)((int)ipEndPoint.AddressFamily)); buffer[1] = unchecked((byte)((int)ipEndPoint.AddressFamily >> 8)); #endif if (ipEndPoint.AddressFamily == AddressFamily.InterNetworkV6) { buffer[2] = (byte)(ipEndPoint.Port >> 8); buffer[3] = (byte)ipEndPoint.Port; buffer[4] = (byte)0; buffer[5] = (byte)0; buffer[6] = (byte)0; buffer[7] = (byte)0; long scope = ipEndPoint.Address.ScopeId; buffer[24] = (byte)scope; buffer[25] = (byte)(scope >> 8); buffer[26] = (byte)(scope >> 16); buffer[27] = (byte)(scope >> 24); byte[] addressBytes = ipEndPoint.Address.GetAddressBytes(); for (int i = 0; i < addressBytes.Length; i++) { buffer[8 + i] = addressBytes[i]; } } else { buffer[2] = unchecked((byte)(ipEndPoint.Port >> 8)); buffer[3] = unchecked((byte)(ipEndPoint.Port)); byte[] addressBytes = ipEndPoint.Address.GetAddressBytes(); for (int i = 0; i < addressBytes.Length; i++) { buffer[4 + i] = addressBytes[i]; } } return buffer; } internal static IPEndPoint IPEndPointFromSOCKADDRBuffer(byte[] buffer) { IPAddress ip = null; int addressFamily = 0; #if BIGENDIAN addressFamily = buffer[1] + ((int)buffer[0] << 8); #else addressFamily = buffer[0] + ((int)buffer[1] << 8); #endif //Get port int port = buffer[3] + ((int)buffer[2] << 8); if ((AddressFamily)addressFamily == AddressFamily.InterNetwork) { byte[] v4bytes = new byte[] { buffer[4], buffer[5], buffer[6], buffer[7] }; ip = new IPAddress(v4bytes); } else if ((AddressFamily)addressFamily == AddressFamily.InterNetworkV6) { byte[] v6Bytes = new byte[16]; for (int i = 0; i < 16; i++) v6Bytes[i] = buffer[8 + i]; long scope = ((long)(long)buffer[24] + ((long)buffer[25] << 8) + ((long)buffer[26] << 16) + ((long)buffer[27] << 24)); ip = new IPAddress(v6Bytes); ip.ScopeId = scope; } return new IPEndPoint(ip, port); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. namespace System.Net.PeerToPeer { using System; using System.Net; using System.Net.Sockets; using System.Collections.Generic; using System.Text; using System.Security.Permissions; internal static class SystemNetHelpers { internal const int IPv6AddressSize = 28; internal const int IPv4AddressSize = 16; internal static byte[] SOCKADDRFromIPEndPoint(IPEndPoint ipEndPoint) { byte[] buffer = new byte[ipEndPoint.AddressFamily == AddressFamily.InterNetworkV6 ? IPv6AddressSize : IPv4AddressSize]; #if BIGENDIAN buffer[0] = unchecked((byte)((int)family>>8)); buffer[1] = unchecked((byte)((int)family )); #else buffer[0] = unchecked((byte)((int)ipEndPoint.AddressFamily)); buffer[1] = unchecked((byte)((int)ipEndPoint.AddressFamily >> 8)); #endif if (ipEndPoint.AddressFamily == AddressFamily.InterNetworkV6) { buffer[2] = (byte)(ipEndPoint.Port >> 8); buffer[3] = (byte)ipEndPoint.Port; buffer[4] = (byte)0; buffer[5] = (byte)0; buffer[6] = (byte)0; buffer[7] = (byte)0; long scope = ipEndPoint.Address.ScopeId; buffer[24] = (byte)scope; buffer[25] = (byte)(scope >> 8); buffer[26] = (byte)(scope >> 16); buffer[27] = (byte)(scope >> 24); byte[] addressBytes = ipEndPoint.Address.GetAddressBytes(); for (int i = 0; i < addressBytes.Length; i++) { buffer[8 + i] = addressBytes[i]; } } else { buffer[2] = unchecked((byte)(ipEndPoint.Port >> 8)); buffer[3] = unchecked((byte)(ipEndPoint.Port)); byte[] addressBytes = ipEndPoint.Address.GetAddressBytes(); for (int i = 0; i < addressBytes.Length; i++) { buffer[4 + i] = addressBytes[i]; } } return buffer; } internal static IPEndPoint IPEndPointFromSOCKADDRBuffer(byte[] buffer) { IPAddress ip = null; int addressFamily = 0; #if BIGENDIAN addressFamily = buffer[1] + ((int)buffer[0] << 8); #else addressFamily = buffer[0] + ((int)buffer[1] << 8); #endif //Get port int port = buffer[3] + ((int)buffer[2] << 8); if ((AddressFamily)addressFamily == AddressFamily.InterNetwork) { byte[] v4bytes = new byte[] { buffer[4], buffer[5], buffer[6], buffer[7] }; ip = new IPAddress(v4bytes); } else if ((AddressFamily)addressFamily == AddressFamily.InterNetworkV6) { byte[] v6Bytes = new byte[16]; for (int i = 0; i < 16; i++) v6Bytes[i] = buffer[8 + i]; long scope = ((long)(long)buffer[24] + ((long)buffer[25] << 8) + ((long)buffer[26] << 16) + ((long)buffer[27] << 24)); ip = new IPAddress(v6Bytes); ip.ScopeId = scope; } return new IPEndPoint(ip, port); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- OperatingSystem.cs
- DebugView.cs
- EditorZoneBase.cs
- DelegatedStream.cs
- ResXDataNode.cs
- DisplayNameAttribute.cs
- GridView.cs
- SQLStringStorage.cs
- CacheAxisQuery.cs
- MetadataUtil.cs
- ApplicationCommands.cs
- Int64Storage.cs
- FileVersionInfo.cs
- GetCryptoTransformRequest.cs
- QilPatternVisitor.cs
- SamlAuthenticationClaimResource.cs
- ProviderBase.cs
- Quad.cs
- MessageQueueEnumerator.cs
- HashStream.cs
- TextShapeableCharacters.cs
- AsyncPostBackTrigger.cs
- UrlParameterReader.cs
- RotationValidation.cs
- HttpProfileGroupBase.cs
- AsymmetricSecurityProtocolFactory.cs
- ProfileInfo.cs
- NodeLabelEditEvent.cs
- AmbientLight.cs
- DataGridViewCellToolTipTextNeededEventArgs.cs
- UpdatePanelTriggerCollection.cs
- SqlXml.cs
- TableRowCollection.cs
- MultipleCopiesCollection.cs
- IPGlobalProperties.cs
- FixUpCollection.cs
- WindowsComboBox.cs
- PolyLineSegmentFigureLogic.cs
- MethodExpr.cs
- ForeignKeyConstraint.cs
- KoreanCalendar.cs
- StaticContext.cs
- NoResizeHandleGlyph.cs
- StylusPointPropertyInfo.cs
- NewArray.cs
- CompileLiteralTextParser.cs
- DesignerOptionService.cs
- PageEventArgs.cs
- DefaultMemberAttribute.cs
- TdsParserSessionPool.cs
- InputBindingCollection.cs
- WindowsStartMenu.cs
- LabelEditEvent.cs
- InstanceKeyCollisionException.cs
- InternalUserCancelledException.cs
- GridViewColumnCollectionChangedEventArgs.cs
- precedingquery.cs
- EmptyElement.cs
- BaseCollection.cs
- CompModSwitches.cs
- DialogBaseForm.cs
- SystemEvents.cs
- TypeDescriptorContext.cs
- DocumentPageTextView.cs
- ObjectStateFormatter.cs
- MissingSatelliteAssemblyException.cs
- HybridDictionary.cs
- UnsafeNativeMethods.cs
- SQLString.cs
- StorageEntitySetMapping.cs
- BuilderInfo.cs
- PasswordTextNavigator.cs
- ClientRuntime.cs
- GenericUriParser.cs
- HtmlProps.cs
- QuaternionValueSerializer.cs
- LayoutUtils.cs
- ImageInfo.cs
- QuarticEase.cs
- DataSet.cs
- SubclassTypeValidatorAttribute.cs
- DataGridTextBoxColumn.cs
- ConstraintConverter.cs
- cache.cs
- InfoCardCryptoHelper.cs
- DataGridViewRowErrorTextNeededEventArgs.cs
- LinearKeyFrames.cs
- MenuBindingsEditorForm.cs
- DataConnectionHelper.cs
- HyperLinkStyle.cs
- CombinedGeometry.cs
- XmlFormatReaderGenerator.cs
- EntityDataSourceDesigner.cs
- Brush.cs
- LambdaSerializationException.cs
- TextSimpleMarkerProperties.cs
- WorkflowCreationContext.cs
- Cursors.cs
- IndexedGlyphRun.cs
- RoleService.cs