2014-09-02 91 views
0

我嘗試使用setsockopt函數向接口的多播地址列表添加一個地址,但使用errno EINVAL失敗。我把一些的printk在內核代碼和它看起來像錯誤號最終被內核packet_setsockopt功能,因爲通過了optlen值小於packet_mreq結構的大小設置。對於我的生活,我無法弄清楚爲什麼會發生這種情況。下面是相關代碼:setsockopt爲什麼返回EINVAL?

您的括號
int addMulticastAddress(int ifindex, unsigned char macaddr[6]) { 

    struct packet_mreq mreq; 
    int fd; 
    int ret = -1; 


    if ((fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) { 
     return -1; 
    } 

    memset(&mreq, 0, sizeof(struct packet_mreq)); 
    mreq.mr_ifindex = ifindex; 
    mreq.mr_type = PACKET_MR_MULTICAST; 
    memcpy(mreq.mr_address, macaddr, 6); 
    mreq.mr_alen = 6; 

    if (setsockopt(fd, SOL_PACKET, 
        PACKET_ADD_MEMBERSHIP, 
        &mreq, sizeof(struct packet_mreq) < 0)) { 
     virReportSystemError(errno, 
          _("Failed to %s %s %s multicast list for '%s' interface"), 
          add ? "add" : "delete", 
          virMacAddrFormat(macaddr, macstr), 
          add ? "to" : "from", ifname); 
     goto cleanup; 
    } 

    ret = 0; 

cleanup: 
    close(fd); 
    return ret; 
} 

回答

1

一個是在錯誤的地方:

if (setsockopt(fd, SOL_PACKET, 
    PACKET_ADD_MEMBERSHIP, 
    &mreq, sizeof(struct packet_mreq) < 0)) { 

應該是:

if (setsockopt(fd, SOL_PACKET, 
    PACKET_ADD_MEMBERSHIP, 
    &mreq, sizeof(struct packet_mreq)) < 0) { 
+0

謝謝,大有趕超!你救了我幾根頭髮。 – 2014-09-03 12:31:26