Skip to content

API Socket.SetRawSocketOption #865

Closed

Description

@benaadams

Currently the PAL layer blocks unknown options; eventually they may become adopted as enums, however prior to that it would be useful to be able to pass them through in an unvalidated manner.

public partial class Socket
{
    // existing: public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue);
    public void SetRawSocketOption(int optionLevel, int optionName, ReadOnlySpan<byte> optionValue);

    // existing: public byte[] GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionLength);
    public int GetRawSocketOption(int optionLevel, int optionName, Span<byte> optionValue);
}

This has come up in https://github.com/dotnet/corefx/issues/16375 and also when trying to use TCP_CORK on Linux dotnet/aspnetcore#9603

/cc @stephentoub @geoffkizer

Activity

  1. benaadams commented on Apr 23, 2019

    @benaadams
    MemberAuthor

    Current workaround is to pinvoke e.g. on linux

    [DllImport("libc")]
    static unsafe extern int setsockopt(int sockfd, int level, int optname, void* optval, int optlen);

    and then use setsockopt((int)socket.Handle, ....

  2. geoffkizer commented on Apr 24, 2019

    @geoffkizer
    Contributor

    Presumably you would want GetRawSocketOption too?

  3. benaadams commented on Apr 24, 2019

    @benaadams
    MemberAuthor

    Yes :)

  4. wfurt commented on Apr 24, 2019

    @wfurt
    Member

    and Bind() ? Can and Raw family would need them.

  5. wfurt commented on Apr 25, 2019

    @wfurt
    Member

    cc: @krwq may be related to dotnet/corefx#35143

  6. los93sol commented on Jun 18, 2019

    @los93sol

    Anyone had success using this workaround to set SO_ATTACH_FILTER? I've been trying to do it for several hours and keep getting back -1. Assuming it's because I'm attempting to build the bpf filter from structs and bypassing libpcap's pcap_compile method.

  7. karelz commented on Sep 9, 2019

    @karelz
    Member

    I would suggest to use SetPlatformOption name.

  8. krwq commented on Sep 9, 2019

    @krwq
    Member

    I do think this API is useful but I personally think we should not have this API in the framework since there is no way we can ever support this correctly on all platforms in uniform way. I'd prefer if someone wanting to set any options just did their own interop. Socket.Handle can be used to get native handle. I don't feel super strongly about this and it will be area owners final decision anyway :-)

  9. scalablecory commented on Oct 8, 2019

    @scalablecory
    Contributor

    @krwq why couldn't we support it? the support contract here is very minimal "we aren't managing platform for you, we just forward the params to interop layer".

  10. scalablecory commented on Oct 22, 2019

    @scalablecory
    Contributor

    Do we need unsafe here? How about span?

    public partial class Socket
    {
        public int SetRawSocketOption(int optionLevel, int optionName, ReadOnlySpan<byte> optionValue);
    }
  11. benaadams commented on Oct 22, 2019

    @benaadams
    MemberAuthor

    How about span?

    I'd be ok with that; updated summary

  12. scalablecory commented on Oct 29, 2019

    @scalablecory
    Contributor

    triage: not sure about naming, should we have GetRawSocketOption? ready for review.

    also thinking about: RawBind

  13. transferred this issue fromdotnet/corefxon Dec 14, 2019
  14. added
    untriaged
    on Dec 14, 2019
  15. added this to the 5.0 milestone on Dec 14, 2019
  16. removed
    untriaged
    on Dec 14, 2019
  17. added
    api-approved
    and removed on Feb 20, 2020
  18. terrajobst commented on Feb 20, 2020

    @terrajobst
    Contributor

    Video

    • We considered making it TryGetRawSocketOption but we can't think of a case where the size of the option would change/is not known.
    public partial class Socket
    {
        // existing: public void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, byte[] optionValue);
        public void SetRawSocketOption(int optionLevel, int optionName, ReadOnlySpan<byte> optionValue);
    
        // existing: public byte[] GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionLength);
        public int GetRawSocketOption(int optionLevel, int optionName, Span<byte> optionValue);
    }
  19. self-assigned this
    on Mar 5, 2020
  20. ghost locked as resolved and limited conversation to collaborators on Dec 13, 2020
  21. added
    in-pr
    on Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Net.Socketsin-pr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions