2012-03-26 20 views
0

Ich versuche herauszufinden, welche Standardwerte für die Option IP_MULTICAST_IF nach dem Einrichten eines neuen Sockets verwendet werden. Leider segmentiert mein Code und ich weiß eigentlich nicht warum ?! Mache ich etwas falsch oder wie bekomme ich die Standardwerte für die Option IP_MULTICAST_IF?Standardwerte für IP_MULTICAST_IF

int sock; 
    struct in_addr interface_addr; 
    int addr_size; 

    if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { 
    perror("socket() failed"); 
    } 

    addr_size = sizeof(interface_addr); 

    if ((getsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &interface_addr, &addr_size)) < 0) { 
    perror("getsockopt() failed"); 
    } 

    printf("The default interface is %s\n", inet_ntoa(interface_addr)); 

Antwort

0

Mein Fehler war mein Code fehlt ein enthalten. Mit

#include <arpa/inet.h> 

funktioniert es gut.

0

Lesen Sie die ip(7) man-Seite für Ihre Plattform. IP_MULTICAST_IF nimmt keine struct in_addr.

Unter Linux:

IP_MULTICAST_IF (since Linux 1.2) 
     Set the local device for a multicast socket. Argument is an ip_mreqn or 
     ip_mreq structure similar to IP_ADD_MEMBERSHIP. 
     When an invalid socket option is passed, ENOPROTOOPT is returned. 

IP_ADD_MEMBERSHIP (since Linux 1.2) 
      Join a multicast group. Argument is an ip_mreqn structure. 

      struct ip_mreqn { 
        struct in_addr imr_multiaddr; /* IP multicast group 
                address */ 
        struct in_addr imr_address; /* IP address of local 
                interface */ 
        int   imr_ifindex; /* interface index */ 
       }; 
      imr_multiaddr contains the address of the multicast group the application wants to join or 
      leave. It must be a valid multicast address (or setsockopt(2) fails with the error EINVAL). 
      imr_address is the address of the local interface with which the system should join the multi‐ 
      cast group; if it is equal to INADDR_ANY an appropriate interface is chosen by the system. 
      imr_ifindex is the interface index of the interface that should join/leave the imr_multiaddr 
      group, or 0 to indicate any interface. 
      The ip_mreqn structure is available only since Linux 2.2. For compatibility, the old ip_mreq 
      structure (present since Linux 1.2) is still supported; it differs from ip_mreqn only by not 
      including the imr_ifindex field. Only valid as a setsockopt(2). 
+0

Aus irgendeinem Grund scheint der Segmentierungsfehler von inet_ntoa (..) zu stammen. Selbst wenn ich die Struktur zu einer ip_mreq Struktur korrigiere - bekomme ich immer noch den gleichen Segmentierungsfehler. – Bjoern

Verwandte Themen