none
Problem calling getsockopt(...TCP_MAXSEG...) via P/Invoke RRS feed

  • Question

  • Hi,

    I need to perform from C# the following call to getsockopt():

    int r = getsockopt(new_socket, IPPROTO_TCP, TCP_MAXSEG, (char *) &mss, &mss_size);

    which works like a charm in C++.

    I can't use System.Net.Sockets.GetSocketOption as System.Net.Sockets.SocketOptionName doesn't include an equivalent for  TCP_MAXSEG.

    I've tried (the value of TCP_MAXSEG is 4):

                    int optval = unchecked((int)0xBAADF00D), optlen = 4;
                    int r = Native.getsockopt(sListener.Handle, Native.SocketOptionLevel.Tcp, 4, out optval, ref optlen);
                    Console.WriteLine("MSS = {0} r = {1}", optval, r);
                }

    but it fails -- optval is not written to, and the return code is -1

    I've also tried:

    int mss = (int)sListener.GetSocketOption(SocketOptionLevel.Tcp, (SocketOptionName)4);

    but this gets me an exception:

    ---------------------------
    System.Net.Sockets.SocketException (0x80004005): An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call
    
       at System.Net.Sockets.Socket.GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName)
    
       at ServerSocketWpfApp.MainWindow.SendCallback(IAsyncResult ar) in S:\Communication through Sockets\C#\ServerSocketWpfApp\MainWindow.xaml.cs:line 343
    ---------------------------
    OK   
    ---------------------------

    Any clue as to how I can make that getsockopt() call from C#?



    • Edited by sba Thursday, March 21, 2019 11:17 AM more clarification
    Thursday, March 21, 2019 10:26 AM

Answers

  • After quite some digging in the .NET Framework's source code, and with the help of a smart colleague, I am hereby able to post the answer to my own question (some blushing in order ;-) ).

    int mss = (int)thesocket.GetSocketOption(SocketOptionLevel.Tcp, (SocketOptionName)4);

    does work... provided you use the right socket...

    Initially I was using the "listener" socket and it didn't work.

    I switched to using the "connected" socket (the one returned by EndAccept()) and voilà!

    Hope this can help others with similar issues!

    P.S. my experimentation started with the very useful sample at https://code.msdn.microsoft.com/windowsdesktop/Communication-through-91a2582b/

    • Marked as answer by sba Friday, March 22, 2019 6:54 AM
    Thursday, March 21, 2019 9:39 PM

All replies

  • Make sure that Native.SocketOptionLevel.Tcp is 6.

    If the function fails, try Marshal.GetLastWin32Error for some details.

    You can also implement this part as a C++/CLR Class Library (a class with static functions, for example), where you can call getsockopt directly.

    Thursday, March 21, 2019 12:09 PM
  • I've stopped under the native debugger at getsockopt()'s entry point, and the stack shows that level is 6 and optname is 4.

    Single-stepping through getsockopt() shows that it calls SetLastError() with WSAENOPROTOOPT ("An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call").

    That's weird, my working C++ test program also uses sets level to 6 and optname to 4.

    So this doesn't actually look like a problem with those arguments to the call.

    Thursday, March 21, 2019 12:52 PM
  • After quite some digging in the .NET Framework's source code, and with the help of a smart colleague, I am hereby able to post the answer to my own question (some blushing in order ;-) ).

    int mss = (int)thesocket.GetSocketOption(SocketOptionLevel.Tcp, (SocketOptionName)4);

    does work... provided you use the right socket...

    Initially I was using the "listener" socket and it didn't work.

    I switched to using the "connected" socket (the one returned by EndAccept()) and voilà!

    Hope this can help others with similar issues!

    P.S. my experimentation started with the very useful sample at https://code.msdn.microsoft.com/windowsdesktop/Communication-through-91a2582b/

    • Marked as answer by sba Friday, March 22, 2019 6:54 AM
    Thursday, March 21, 2019 9:39 PM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 22, 2019 1:16 AM
    Moderator
  • Done ;-)
    Friday, March 22, 2019 6:55 AM