Skip to content

IP_ADD_MEMBERSHIP constant not correctly defined for Windows #20381

Closed

Description

@shepmaster

Running this code:

use std::io::net::udp::UdpSocket;
use std::io::net::ip::{Ipv4Addr, SocketAddr};

fn main() {
    let local_addr = SocketAddr { ip: Ipv4Addr(0, 0, 0, 0), port: 3671 };
    let mut socket = match UdpSocket::bind(local_addr) {
        Ok(s) => s,
        Err(e) => panic!("couldn't bind socket: {}", e),
    };
    match socket.join_multicast(Ipv4Addr(224, 0, 23, 12)) {
        Err(why) => println!("! {}", why),
        Ok(_) => {},
    };
    drop(socket)
}

On Windows 7, 32-bit (rustc 0.13.0-nightly (5ba610265 2014-12-25 18:01:36 +0000)), and Windows 7, 64-bit (0.13.0-nightly (5ba610265 2014-12-25 18:01:36 +0000)) yields the error:

OS Error 10042: An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call

Doing some digging, I found a MS KB article that seems to directly mention this:

If the project is linked with Ws2_32.lib, setsockopt will fail with runtime error 10042 (WSAENOPROTOOPT). This is because in Winsock.h, IP_ADD_MEMBERSHIP is defined as "5". The corresponding Winsock runtime can not resolve option 5 at the IPPROTO_IP level, so the failure occurs with error code 10042.

As far as I can see, the current constant is set to 5, and the article says it should be 12

Activity

  1. emberian commented on Jan 1, 2015

    @emberian
    Contributor

    cc @aturon @alexcrichton are there any subtlties here around windows versioning that would prevent the simple change to 12?

  2. alexcrichton commented on Jan 2, 2015

    @alexcrichton
    Member

    Sounds like changing it to 12 should be good enough! Adding a test would probably also be worth it.

  3. shepmaster commented on Jan 2, 2015

    @shepmaster
    MemberAuthor

    And to be clear, it's not just the one constant that is troublesome.

    It looks like:

    IP_ADD_MEMBERSHIP
    IP_DROP_MEMBERSHIP
    IP_TTL
    IP_TOS
    IP_MULTICAST_IF
    IP_MULTICAST_TTL
    IP_MULTICAST_LOOP
    IP_DONTFRAGMENT
    

    Should all be checked

  4. danslapman commented on Jan 6, 2015

    @danslapman
    Contributor

    Experiencing the same error in windows 8.1

  5. alexcrichton commented on Feb 11, 2015

    @alexcrichton
    Member

    Fixed in #21267

  6. added a commit that references this issue on Sep 1, 2025
  7. added 2 commits that reference this issue on Nov 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    O-windowsOperating system: Windows

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions