第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

獲取接收到的UDP報文的目的地址

獲取接收到的UDP報文的目的地址

烙印99 2019-12-15 16:12:02
收到UDP數(shù)據(jù)包后,我需要用他用來向其發(fā)送數(shù)據(jù)包的地址來響應(yīng)發(fā)送方。該recvfrom調(diào)用使我可以獲取發(fā)送方的地址,但是如何獲取接收到的數(shù)據(jù)包的目標(biāo)地址,該地址應(yīng)與本地主機接口之一的地址匹配?
查看完整描述

3 回答

?
BIG陽

TA貢獻1859條經(jīng)驗 獲得超6個贊

我構(gòu)建了一個示例,該示例提取了源地址,目標(biāo)地址和接口地址。為簡便起見,沒有提供錯誤檢查。


// sock is bound AF_INET socket, usually SOCK_DGRAM

// include struct in_pktinfo in the message "ancilliary" control data

setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &opt, sizeof(opt));

// the control data is dumped here

char cmbuf[0x100];

// the remote/source sockaddr is put here

struct sockaddr_in peeraddr;

// if you want access to the data you need to init the msg_iovec fields

struct msghdr mh = {

    .msg_name = &peeraddr,

    .msg_namelen = sizeof(peeraddr),

    .msg_control = cmbuf,

    .msg_controllen = sizeof(cmbuf),

};

recvmsg(sock, &mh, 0);

for ( // iterate through all the control headers

    struct cmsghdr *cmsg = CMSG_FIRSTHDR(&mh);

    cmsg != NULL;

    cmsg = CMSG_NXTHDR(&mh, cmsg))

{

    // ignore the control headers that don't match what we want

    if (cmsg->cmsg_level != IPPROTO_IP ||

        cmsg->cmsg_type != IP_PKTINFO)

    {

        continue;

    }

    struct in_pktinfo *pi = CMSG_DATA(cmsg);

    // at this point, peeraddr is the source sockaddr

    // pi->ipi_spec_dst is the destination in_addr

    // pi->ipi_addr is the receiving interface in_addr

}



查看完整回答
反對 回復(fù) 2019-12-16
?
守著一只汪

TA貢獻1872條經(jīng)驗 獲得超4個贊

為了提供O(1)查找,您需要將輔助控制結(jié)構(gòu)打包到提供O(1)查找的數(shù)據(jù)容器中。填充該結(jié)構(gòu)的數(shù)量至少為輔助控制消息的O(n)。您不必?fù)?dān)心要查找的控制消息的復(fù)雜性:總是只有少數(shù)幾個,并且只有您請求的那些。代您還需要零內(nèi)存管理。這是微不足道的開銷。 

查看完整回答
反對 回復(fù) 2019-12-16
  • 3 回答
  • 0 關(guān)注
  • 471 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號