25

UDPパケットを受信したら、返信先のパケットの送信に使用したアドレスで送信者に応答する必要があります。

このrecvfrom呼び出しにより、送信者のアドレスを取得できますが、受信したパケットの宛先アドレスを取得するにはどうすればよいですか。これは、ローカルホストのインターフェイスの1つのアドレスと一致する必要があります。

4

2 に答える 2

24

送信元アドレス、宛先アドレス、およびインターフェイス アドレスを抽出する例を作成しました。簡潔にするために、エラー チェックは提供されません。

// 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
}
于 2011-03-15T08:30:26.110 に答える
17

setsockoptを使用してIP_PKTINFOオプションを設定してから、recvmsgを使用して、structmsghdrのmsg_controlメンバーでin_pktinfo構造体を取得します。in_pktinfoには、パケットの宛先アドレスを含むフィールドがあります。

詳細については、 http: //www.linuxquestions.org/questions/programming-9/how-to-get-destination-address-of-udp-packet-600103/を参照してください。

于 2011-03-12T09:29:15.850 に答える