Code Monkey home page Code Monkey logo

Comments (9)

cgutman avatar cgutman commented on June 16, 2024 1

@fra189 fwiw, if you're just looking to resolve mDNS services on your network (rather than advertise them), you may be able to avoid having to join the multicast group to get a response. You can send an mDNS query and request a unicast response by setting the unicast-response bit in the header. It doesn't guarantee a unicast response, but it generally should result in one (especially if you send QU queries periodically if you get no response).

See https://datatracker.ietf.org/doc/html/rfc6762#section-5.4

from libnx.

SciresM avatar SciresM commented on June 16, 2024 1

I think this is actually about library compatibility version, not about permissions.

This is untested and based on RE, but I think the line that causes EPERM is this one: https://github.com/switchbrew/libnx/blob/master/nx/source/runtime/devices/socket.c#L65

If I am reading this permission check function right, the following require library version > 3:

IP_MULTICAST_IF
IP_MULTICAST_TTL
IP_MULTICAST_LOOP
IP_ADD_MEMBERSHIP
IP_DROP_MEMBERSHIP
IP_MULTICAST_VIF

I think BsdServiceType_System just causes an early return in the permission checking function, which bypasses the library compatibility check entirely.

from libnx.

mtheall avatar mtheall commented on June 16, 2024

What did you fill into addr for IP_MULTICAST_IF and mreq for IP_ADD_MEMBERSHIP?

from libnx.

fra189 avatar fra189 commented on June 16, 2024

Hi,
Thank you for answering.
So this is the part of the code:

#define MDNS_MULTICAST_ADDRESS "224.0.0.251"
#define MDNS_PORT 5353

int init(struct in_addr host) {
    struct ip_mreq mreq;
    struct sockaddr_in addr;
    int sock;
    char param;
    
    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if(sock < 0) {
       return sock;
     }

    param = 32;
    if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (void*) &param, sizeof(param)) < 0) {
      return -1;
    }
    
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(MDNS_PORT);
    addr.sin_addr.s_addr = INADDR_ANY;
    
    if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
      return -1;
    }
    
    memset(&mreq, 0, sizeof(mreq));
    mreq.imr_multiaddr.s_addr = inet_addr(MDNS_MULTICAST_ADDRESS);
    mreq.imr_interface.s_addr = host.s_addr;
  
    if (setsockopt (sock, IPPROTO_IP, IP_MULTICAST_IF, (void*) &mreq.imr_interface.s_addr, sizeof(mreq.imr_interface.s_addr)) < 0)  {
        return -1;
    }
    if(setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void*) &mreq, sizeof(mreq)) < 0) {
       return -1;
    }
    return sock;
  }
  
  .....
  struct in_addr host;
  const long ip = gethostid();
  char ip_str[32] = {0};
  inet_ntop(AF_INET, &ip, ip_str, sizeof(ip_str));
  host.s_addr = inet_addr(ip_str);  
  int sock = init(host);

from libnx.

mtheall avatar mtheall commented on June 16, 2024

I get EPERM for every multicast-related socket option. It's possible the OS won't allow unprivileged multicast servers.

from libnx.

fra189 avatar fra189 commented on June 16, 2024

Thank you @cgutman
I can make a try with that.

from libnx.

xfangfang avatar xfangfang commented on June 16, 2024

I figured out how to join a multicast group, just pass in higher permissions when initializing the socket.

SocketInitConfig cfg = *(socketGetDefaultInitConfig());
cfg.bsd_service_type = BsdServiceType_Auto; // or BsdServiceType_System
socketInitialize(&cfg);

This can also be used to help create an 80 port HTTP service, hoping this comment would help someone ~

from libnx.

xfangfang avatar xfangfang commented on June 16, 2024

Thanks for the detailed explanation, I confirmed that after setting cfg.bsdsockets_version = 4;, multicast is indeed working.

from libnx.

fincs avatar fincs commented on June 16, 2024

Fixed by libnx v4.5.0.

from libnx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.