Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Record remote IPAddress and port #284

Merged
merged 1 commit into from
Oct 29, 2015
Merged

Record remote IPAddress and port #284

merged 1 commit into from
Oct 29, 2015

Conversation

troydai
Copy link
Contributor

@troydai troydai commented Oct 23, 2015

Issue #67

  1. Record local IPAddress and port as well;
  2. Frame will implement IHttpConnectionFeature.

/cc @Tratcher @halter73 @muratg

@@ -0,0 +1,14 @@
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove this file.

bytes[i + 8] = bytes2[i];
}

address = new IPAddress(bytes);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now I assume the IPAddress performs well. If we detect later on it's a bottleneck we can replace it with our own bit-wise operation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this only happens per connection, not per request, then it won't matter as much.

@Tratcher
Copy link
Member

Tests? Unit or functional?

@troydai
Copy link
Contributor Author

troydai commented Oct 23, 2015

I'll put up a few tests. But I'm wondering if a real case scenario can be automated on single machine.

@borgdylan
Copy link

Thanks for doing this. I want to test it but suddenly roslyn decided that it won't compile kestrel at all on my machine.

private long _field1;
private long _field2;
private long _field3;
public long _field0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public?

@halter73
Copy link
Member

Have you tried this change out on Mac and/or Linux?

@borgdylan
Copy link

I wanted to but Roslyn is hiccuping on me. It did not want to compile kestrel (due to an exception and not due to errors). I will see if there is a newer dnx and try to compile. I am using Linux x86

@borgdylan
Copy link

I tried compiling your PR but roslyn is not cooperating:

dylan@ubuntu-server:/var/www/git/aspnet/KestrelHttpServer/src/Microsoft.AspNet.Server.Kestrel$ DNX_TRACE=1 MONO_OPTIONS=--debug dnu build
Information: [LoaderContainer]: Load name=Microsoft.Dnx.Tooling
Information: [PathBasedAssemblyLoader]: Loaded name=Microsoft.Dnx.Tooling in 2ms
Information: [Bootstrapper] Runtime Framework: DNX,Version=v4.5.1
Microsoft .NET Development Utility Mono-x86-1.0.0-rc1-15996


Building Microsoft.AspNet.Server.Kestrel for DNX,Version=v4.5.1
Warning: [ApplicationHostContext] Lock File Target is Runtime-agnostic! This is generally not good.
Information: [ApplicationHostContext] Using Lock File Target: DNX,Version=v4.5.1/
Information: [LibraryExporter]: Resolving references for 'Microsoft.AspNet.Server.Kestrel' 
Information: [LibraryExporter]: ExportProject(Microsoft.AspNet.Server.Kestrel, , DNX,Version=v4.5.1, Debug)
Warning: [ApplicationHostContext] Lock File Target is Runtime-agnostic! This is generally not good.
Information: [ApplicationHostContext] Using Lock File Target: DNX,Version=v4.5.1/
Information: [LibraryExporter]: GetProjectReference(Microsoft.Dnx.Compilation.CSharp.RoslynProjectCompiler, Microsoft.AspNet.Server.Kestrel, DNX,Version=v4.5.1, )
Information: [LibraryExporter]: Resolving references for 'Microsoft.AspNet.Server.Kestrel' 
Information: [LibraryExporter]: ExportProject(Microsoft.StandardsPolice, , DNX,Version=v4.5.1, Debug)
Warning: [ApplicationHostContext] Lock File Target is Runtime-agnostic! This is generally not good.
Information: [ApplicationHostContext] Using Lock File Target: DNX,Version=v4.5.1/
Information: [LibraryExporter]: GetProjectReference(Microsoft.Dnx.Compilation.CSharp.RoslynProjectCompiler, Microsoft.StandardsPolice, DNX,Version=v4.5.1, )
Information: [LibraryExporter]: Resolving references for 'Microsoft.StandardsPolice' 
Information: [LibraryExporter]: Resolved 14 references for 'Microsoft.StandardsPolice' in 1ms
Information: [RoslynCompiler]: Compiling 'Microsoft.StandardsPolice'
Information: [LoaderContainer]: Load name=System.Security.Cryptography.Algorithms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Information: [LoaderContainer]: Load name=System.Security.Cryptography.Primitives, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Information: [RoslynCompiler]: Compiled 'Microsoft.StandardsPolice' in 307ms
Information: [LibraryExporter]: Resolved 42 references for 'Microsoft.AspNet.Server.Kestrel' in 337ms
Information: [RoslynCompiler]: Compiling 'Microsoft.AspNet.Server.Kestrel'
Information: [LoaderContainer]: Load name=Microsoft.AspNet.Server.Kestrel!preprocess
Information: [ApplicationHostContext] Using Lock File Target: DNX,Version=v4.5.1/ubuntu.14.04-x86
Warning: [ApplicationHostContext] Lock File Target is Runtime-agnostic! This is generally not good.
Information: [ApplicationHostContext] Using Lock File Target: DNX,Version=v4.5.1/
Information: [LibraryExporter]: ExportProject(Microsoft.AspNet.Server.Kestrel, preprocess, DNX,Version=v4.5.1, Debug)
Warning: [ApplicationHostContext] Lock File Target is Runtime-agnostic! This is generally not good.
Information: [ApplicationHostContext] Using Lock File Target: DNX,Version=v4.5.1/
Information: [LibraryExporter]: GetProjectReference(Microsoft.Dnx.Compilation.CSharp.RoslynProjectCompiler, Microsoft.AspNet.Server.Kestrel, DNX,Version=v4.5.1, preprocess)
Information: [LibraryExporter]: Resolving references for 'Microsoft.AspNet.Server.Kestrel' preprocess
Information: [LibraryExporter]: ExportProject(Microsoft.StandardsPolice, , DNX,Version=v4.5.1, Debug)
Information: [LibraryExporter]: Resolved 42 references for 'Microsoft.AspNet.Server.Kestrel' in 2ms
Information: [RoslynCompiler]: Compiling 'Microsoft.AspNet.Server.Kestrel!preprocess'
Information: [RoslynCompiler]: Compiled 'Microsoft.AspNet.Server.Kestrel!preprocess' in 3ms
Information: [CompilationContext]: Generating resources for Microsoft.AspNet.Server.Kestrel
Information: [CompilationContext]: Generated resources for Microsoft.AspNet.Server.Kestrel in 6ms
Information: [RoslynProjectReference]: Emitting assembly for Microsoft.AspNet.Server.Kestrel
Error: Specified cast is not valid.
Error: Full Exception: System.InvalidCastException: Specified cast is not valid.
  at System.Collections.Generic.ShortEnumEqualityComparer`1[T].GetHashCode (T obj) <0xb2948230 + 0x0003c> in <filename unknown>:0 
  at System.Collections.Generic.Dictionary`2[TKey,TValue].Insert (System.Collections.Generic.TKey key, System.Collections.Generic.TValue value, Boolean add) [0x00023] in /mnt/ssd/mono/external/referencesource/mscorlib/system/collections/generic/dictionary.cs:326 
  at System.Collections.Generic.Dictionary`2[TKey,TValue].set_Item (System.Collections.Generic.TKey key, System.Collections.Generic.TValue value) [0x00000] in /mnt/ssd/mono/external/referencesource/mscorlib/system/collections/generic/dictionary.cs:187 
  at Microsoft.CodeAnalysis.Collections.OrderPreservingMultiDictionary`2[K,V].Add (Microsoft.CodeAnalysis.Collections.K k, Microsoft.CodeAnalysis.Collections.V v) <0xb2947408 + 0x001b7> in <filename unknown>:0 
  at Microsoft.Cci.MetadataWriter.PopulateDeclSecurityTableRowsFor (UInt32 parent, IEnumerable`1 attributes) <0xb2946f90 + 0x000a7> in <filename unknown>:0 
  at Microsoft.Cci.MetadataWriter.PopulateDeclSecurityTableRows () <0xb2946d40 + 0x00057> in <filename unknown>:0 
  at Microsoft.Cci.MetadataWriter.PopulateTables (System.Int32[] methodBodyRvas, Microsoft.Cci.BlobBuilder mappedFieldDataWriter, Microsoft.Cci.BlobBuilder resourceWriter) <0xb2944fd8 + 0x00033> in <filename unknown>:0 
  at Microsoft.Cci.MetadataWriter.SerializeMetadataAndIL (Microsoft.Cci.BlobBuilder metadataWriter, Microsoft.Cci.BlobBuilder debugMetadataWriterOpt, Microsoft.Cci.PdbWriter nativePdbWriterOpt, Microsoft.Cci.BlobBuilder ilWriter, Microsoft.Cci.BlobBuilder mappedFieldDataWriter, Microsoft.Cci.BlobBuilder managedResourceDataWriter, Int32 methodBodyStreamRva, System.Func`2 calculateMappedFieldDataStreamRva, System.Int32& moduleVersionIdOffsetInMetadataStream, System.Int32& pdbIdOffsetInPortablePdbStream, System.Int32& entryPointToken, Microsoft.Cci.MetadataSizes& metadataSizes) <0xb2964840 + 0x00097> in <filename unknown>:0 
  at Microsoft.Cci.PeWriter.WritePeToStream (Microsoft.Cci.MetadataWriter mdWriter, System.Func`1 getPeStream, System.Func`1 getPortablePdbStreamOpt, Microsoft.Cci.PdbWriter nativePdbWriterOpt) <0xb29636e8 + 0x00467> in <filename unknown>:0 
  at Microsoft.Cci.PeWriter.WritePeToStream (EmitContext context, Microsoft.CodeAnalysis.CommonMessageProvider messageProvider, System.Func`1 getPeStream, System.Func`1 getPortablePdbStreamOpt, Microsoft.Cci.PdbWriter nativePdbWriterOpt, System.String pdbPathOpt, Boolean allowMissingMethodBodies, Boolean deterministic, CancellationToken cancellationToken) <0xb295cfb0 + 0x00133> in <filename unknown>:0 
  at Microsoft.CodeAnalysis.Compilation.SerializeToPeStream (Microsoft.CodeAnalysis.Emit.CommonPEModuleBuilder moduleBeingBuilt, Microsoft.CodeAnalysis.EmitStreamProvider peStreamProvider, Microsoft.CodeAnalysis.EmitStreamProvider pdbStreamProvider, System.Func`1 testSymWriterFactory, Microsoft.CodeAnalysis.DiagnosticBag diagnostics, Boolean metadataOnly, CancellationToken cancellationToken) <0xb295c000 + 0x00603> in <filename unknown>:0 
  at Microsoft.CodeAnalysis.Compilation.Emit (Microsoft.CodeAnalysis.EmitStreamProvider peStreamProvider, Microsoft.CodeAnalysis.EmitStreamProvider pdbStreamProvider, Microsoft.CodeAnalysis.EmitStreamProvider xmlDocumentationStreamProvider, Microsoft.CodeAnalysis.EmitStreamProvider win32ResourcesStreamProvider, IEnumerable`1 manifestResources, Microsoft.CodeAnalysis.Emit.EmitOptions options, IMethodSymbol debugEntryPoint, Microsoft.CodeAnalysis.CodeGen.CompilationTestData testData, System.Func`1 getHostDiagnostics, CancellationToken cancellationToken) <0xb325b420 + 0x0047f> in <filename unknown>:0 
  at Microsoft.CodeAnalysis.Compilation.Emit (System.IO.Stream peStream, System.IO.Stream pdbStream, System.IO.Stream xmlDocumentationStream, System.IO.Stream win32Resources, IEnumerable`1 manifestResources, Microsoft.CodeAnalysis.Emit.EmitOptions options, IMethodSymbol debugEntryPoint, Microsoft.CodeAnalysis.CodeGen.CompilationTestData testData, System.Func`1 getHostDiagnostics, CancellationToken cancellationToken) <0xb325b270 + 0x00187> in <filename unknown>:0 
  at Microsoft.CodeAnalysis.Compilation.Emit (System.IO.Stream peStream, System.IO.Stream pdbStream, System.IO.Stream xmlDocumentationStream, System.IO.Stream win32Resources, IEnumerable`1 manifestResources, Microsoft.CodeAnalysis.Emit.EmitOptions options, IMethodSymbol debugEntryPoint, CancellationToken cancellationToken) <0xb325b0f8 + 0x0008f> in <filename unknown>:0 
  at Microsoft.Dnx.Compilation.CSharp.RoslynProjectReference.Load (System.Reflection.AssemblyName assemblyName, IAssemblyLoadContext loadContext) <0xb3257ba0 + 0x00403> in <filename unknown>:0 
  at Microsoft.Dnx.Compilation.CompilationEngine.LoadProject (Microsoft.Dnx.Runtime.Project project, System.Runtime.Versioning.FrameworkName targetFramework, System.String aspect, IAssemblyLoadContext loadContext, System.Reflection.AssemblyName assemblyName) <0xb3257570 + 0x000f6> in <filename unknown>:0 
  at Microsoft.Dnx.Runtime.Loader.ProjectAssemblyLoader.Load (System.Reflection.AssemblyName assemblyName, IAssemblyLoadContext loadContext) <0xb3257130 + 0x0015b> in <filename unknown>:0 
  at Microsoft.Dnx.Runtime.Loader.RuntimeLoadContext.LoadWithoutDefault (System.Reflection.AssemblyName assemblyName) <0xb32570c0 + 0x00023> in <filename unknown>:0 
  at Microsoft.Dnx.Compilation.BuildLoadContext.Microsoft.Extensions.PlatformAbstractions.IAssemblyLoadContext.Load (System.Reflection.AssemblyName assemblyName) <0xb40b8750 + 0x00057> in <filename unknown>:0 
  at Microsoft.Dnx.Compilation.CSharp.RoslynCompiler+<>c__DisplayClass11_0.<GetCompileModules>b__0 (Microsoft.Dnx.Compilation.Caching.CacheContext _) <0xb3253630 + 0x000c2> in <filename unknown>:0 
  at Microsoft.Dnx.Compilation.Caching.CacheExtensions+<>c__DisplayClass0_0`1[T].<Get>b__0 (Microsoft.Dnx.Compilation.Caching.CacheContext ctx) <0xb40fbc60 + 0x0001b> in <filename unknown>:0 
  at Microsoft.Dnx.Compilation.Caching.Cache.CreateEntry (System.Object k, System.Func`2 acquire) <0xb40fba10 + 0x00111> in <filename unknown>:0 
  at Microsoft.Dnx.Compilation.Caching.Cache+<>c__DisplayClass5_0.<AddEntry>b__0 () <0xb40fb9a8 + 0x00023> in <filename unknown>:0 
  at System.Lazy`1[T].CreateValue () <0xb515fad0 + 0x00189> in <filename unknown>:0

The short enum comparer seems to be part of rolsyn and not the framework.

@troydai
Copy link
Contributor Author

troydai commented Oct 26, 2015

There a few more changes I'm going on in next iteration. And then I'll test this out on *nix.

@troydai
Copy link
Contributor Author

troydai commented Oct 26, 2015

Added code to deal with situation when server is not set up as dual-stack sockets.

httpConnectionFeature.RemotePort = _remotePort;
httpConnectionFeature.LocalIpAddress = _localIPAddress;
httpConnectionFeature.LocalPort = _localPort;
httpConnectionFeature.IsLocal = IPAddress.IsLoopback(_remoteIPAddress);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to redo this check on each request.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just store these values somewhere private and reset them in Frame.Reset.

@troydai troydai force-pushed the trdai/67 branch 5 times, most recently from 6ec4233 to 819a2c2 Compare October 27, 2015 21:42
@troydai
Copy link
Contributor Author

troydai commented Oct 27, 2015

Waiting for review: @Tratcher @moozzyk @halter73
/cc @muratg

Tested on Ubuntu 14.04 with coreclr-linux and Mono.

}).Build();

var builder = new WebHostBuilder(config).UseServer("Microsoft.AspNet.Server.Kestrel")
.UseStartup(app =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This indenting is ridiculous please fix it.

@troydai
Copy link
Contributor Author

troydai commented Oct 27, 2015

Ping

@moozzyk
Copy link
Contributor

moozzyk commented Oct 28, 2015

P/Invoke looks good.

@@ -99,6 +115,26 @@ public void Reset()
ResponseBody = null;
DuplexStream = null;

var httpConnectionFeature = this as IHttpConnectionFeature;
if (httpConnectionFeature != null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this cast will never return null

@troydai
Copy link
Contributor Author

troydai commented Oct 28, 2015

Ping?

return false;
}

return (_field2 & 0xFFFFFFFF) == 0xFFFF0000;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return _field1 == 0 && (_field2 & 0xFFFFFFFF) == 0xFFFF0000;

@Tratcher
Copy link
Member

:shipit:

@troydai
Copy link
Contributor Author

troydai commented Oct 28, 2015

Read to merge but wait for a healthy CI window.

}
else if (IsIPv4MappedToIPv6())
{
var ipv4bits = (_field2 >> 32) & 0x00000000FFFFFFFF;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0x00000000FFFFFFFF that's a long literal... I first glance I thought that this was a different mask that what is used on _field0 above, but it's not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just get rid of the mask (and the other one as well)? It's a no-op after all.
Or uint.MaxValue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@halter73
Copy link
Member

🚢


// Quick calculate the port by mask the field and locate the byte 3 and byte 4
// and then shift them to correct place to form a int.
var port = ((int)(_field0 & 0x00FF0000) >> 8) | (int)((_field0 & 0xFF000000) >> 24);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the 8 be a 16?

Personally, I prefer shifting first and then using a short mask for readability: (_field0 >> 16) & 0xff

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's correct… My bad

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like shifting first for readability though.

Provide RemoteIPAddress as well as RemotePort
@troydai troydai merged commit e9a6061 into dev Oct 29, 2015
@troydai troydai deleted the trdai/67 branch October 29, 2015 17:32
@borgdylan
Copy link

I confirm that this feature works as expected. I am giving up on Nowin and switching fully to Kestrel.

@troydai
Copy link
Contributor Author

troydai commented Oct 31, 2015

👍 Thanks for trying this out, @borgdylan.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants