2012-03-26 9 views
0

新しいソケットを設定した後にIP_MULTICAST_IFオプションにどのようなデフォルト値が使用されているかを調べようとしています。残念ながら私のコードsegfaultsと私は実際になぜか分からないのですか?私は何か間違っているか、IP_MULTICAST_IFオプションのデフォルト値を取得する方法はありますか?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)); 

答えて

0

私のせいで、私のコードは、行方不明になりました。

#include <arpa/inet.h> 

となります。

0

ご使用のプラットフォームのマニュアルページ(ip(7))を参照してください。 IP_MULTICAST_IFは受け取りませんstruct in_addr。 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

何らかの理由で、セグメンテーションフォルトがinet_ntoa(..)から発生しているようです。構造体をip_mreq構造体に修正しても、同じセグメント化エラーが発生します。 – Bjoern