locked
How to reuse port for datagramsocket ?

    Question

  • I'm writing a metro app to receive udp packets from 239.255.255.250 (port:1900)

    Nonetheless, I set the localServiceName as '1900', it would fail to bind port when I invoke bindServiceNameAsync. (If bind other port, it would show bind success)

    I am wondering if there exist any API for datagramsocket to reuse port.

    In previous code, I could invoke setsockopt to reuse the port, it may look like below.

    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,(char*)&ra, sizeof(ra))

     

    Thanks, sincerely.

     

    Wednesday, January 11, 2012 7:21 AM

Answers

  • Hi CL - SO_REUSEADDR was intentionally left out of Metro applications due to its inherent insecurity:

    The SO_REUSEADDR option has very few uses in normal applications aside from multicast sockets where data is delivered to all of the sockets bound on the same port. Otherwise, any application that sets this socket option should be redesigned to remove the dependency since it is eminently vulnerable to "socket hijacking". As long as SO_REUSEADDR socket option can be used to potentially hijack a port in a server application, the application must be considered to be not secure.

    http://msdn.microsoft.com/en-us/library/ms740621(VS.85).aspx





    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    Wednesday, January 18, 2012 9:08 PM
    Moderator

All replies

  • Hi CL,
          According to the docuentation, the answer is no:

    After the connect operation completes, LocalHostName and LocalServiceName cannot be changed. An attempt to set these values will result in an error.

           I'm not sure that I really understand your situation - are you trying to set it up so that the application uses the port even if it's already allocated to another process? 

     


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    Friday, January 13, 2012 8:05 PM
    Moderator
  • Hi Matt,

    Thanks for your reply. I'm developing UPNP related application under Metro platform now.

    It's true now I couldn't bind port under 1900, it seems default used by other process under Metro platform.

     

    The code I wrote under Win7 or previous platform works fine.

    The purpose to reuse the port and join multicast group is everyone 'agree' to receive from and send to the same port. (Under this case, port 1900 is  defined under the spec of UPNP)

    That's why I need to 'reuse' the port.

     

    The process under Win7 or previous platform would be as followed. (C++)

    1. Create socket

    2. Set socket option to reuse port 1900

    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,(char*)&ra, sizeof(ra))

    * According to your reply, it seems there is no way to reuse the port under Metro.

    3. Bind port

    4. Join multicast group

    setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,(char*)&mreq, sizeof(mreq))

     

    If I couldn't reuse the port of datagramsocket under Metro platform, is there any way I could use socket directly just like the way I used to be?

    Monday, January 16, 2012 2:08 AM
  • Hi CL - I'm checking with our networking people on this.  I'll keep you updated.
    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    Monday, January 16, 2012 4:04 PM
    Moderator
  • Hi CL - SO_REUSEADDR was intentionally left out of Metro applications due to its inherent insecurity:

    The SO_REUSEADDR option has very few uses in normal applications aside from multicast sockets where data is delivered to all of the sockets bound on the same port. Otherwise, any application that sets this socket option should be redesigned to remove the dependency since it is eminently vulnerable to "socket hijacking". As long as SO_REUSEADDR socket option can be used to potentially hijack a port in a server application, the application must be considered to be not secure.

    http://msdn.microsoft.com/en-us/library/ms740621(VS.85).aspx





    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    Wednesday, January 18, 2012 9:08 PM
    Moderator
  • Hi Matt,

    I understand the security issue for using SO_REUSEADDR, however there still some situation that need to bind specific port as you mentioned in the link. (The multicast socket case)

    "The SO_REUSEADDR option has very few uses in normal applications aside from multicast sockets where data is delivered to all of the sockets bound on the same port."

    I wonder if Metro design any way to solve this issue if there still some application need to follow some rule to 'share' specific port. (multicast socket)

    In my case, it's port 1900 followed by the spec of below link.

    http://www.upnp-hacks.org/upnp.html 
    http://www.upnp.org/specs/arch/UPnP-arch-DeviceArchitecture-v1.0-20080424.pdf

    Thanks.

    Thursday, January 19, 2012 6:38 AM
  • You say, "The SO_REUSEADDR option has very few uses in normal applications aside from multicast sockets", but multicast sockets are exactly the reason the original poster (and I) want to bind to a socket in use by another application.  How can I redesign an application that needs to use a standard protocol to listen to multicast address on a standard port, with the port and address dictated by the protocol specification?  Even if I came up with my own custom protocol, another Windows Store app or a Desktop app that uses the same protocol would interfere.

    Basically, what you are saying is that Windows Store apps cannot use any multicast protocol if a Desktop app or another Windows Store app is using the same protocol.

    Couldn't you just only allow SO_REUSEADDR for DatagramSockets bound to a multicast address and still get the same security benefit?  The multicast protocol we want to implement is used for discovery, so other apps seeing the packets is not a security risk.

    Thanks,

    Bill.

    Wednesday, February 13, 2013 11:14 PM
  • Gentlemen - did you ever get a satisfactory outcome to this problem?

    Cap'n


    Friday, October 25, 2013 6:32 PM
  • You'll be glad to hear that we have introduced a new DatagramSocket control option in Windows 10 that allows multicast-only DatagramSockets to coexist with other Win32/WinRT multicast sockets bound to the same address/port pair: DatagramSocket.Control.MulticastOnly

    Setting the new MulticastOnly control option to true will enable the DatagramSocket instance to share its local port with any Win32 sockets that are bound using SO_REUSEADDR and with any other DatagramSocket instances that have MulticastOnly set to true.


    Monday, April 20, 2015 8:38 PM