Howto set the UDP source address on Windows

末鹿安然 提交于 2020-08-08 12:04:41

问题


There seems to be no portable way to set the source IP for sending UDP datagrams from sockets bound to INADDR_ANY, but at least on Linux and FreeBSD it can be done using sendmsg() and the IP_PKTINFO (Linux) or IP_SENDSRCADDR (FreeBSD) option. (See this question.)

Is there an equivalent option to set the UDP source IP on Windows ?


回答1:


From MSDN:

WSASendMsg function

[...]

On an IPv4 socket of type SOCK_DGRAM or SOCK_RAW, an application can specific the local IP source address to use for sending with the WSASendMsg function. One of the control data objects passed in the WSAMSG structure to the WSASendMsg function may contain an in_pktinfo structure used to specify the local IPv4 source address to use for sending.

The same applies to an IPv6 socket with the in6_pktinfo structure.

For dual-mode sockets, it is important that an IPv4 source address is not specified as an IPv4-mapped IPv6 address in the in6_pktinfo, but as an IPv4 address in the in_pktinfo structure.


Example:

union {
    char in[WSA_CMSG_SPACE(sizeof(struct in_pktinfo))];
    char in6[WSA_CMSG_SPACE(sizeof(struct in6_pktinfo))];
} cdata;

WSAMSG msg;
memset(&msg, 0, sizeof(msg));
msg.name = &remote_sysaddr.addr.generic;
msg.namelen = remote_sysaddr.len;
msg.lpBuffers = &buf;
msg.dwBufferCount = 1;
msg.Control.buf = (char *)&cdata;
msg.Control.len = sizeof(cdata);

int sum = 0;

WSACMSGHDR *cmsg = WSA_CMSG_FIRSTHDR(&msg);

...
    memset(cmsg, 0, WSA_CMSG_SPACE(sizeof(struct in_pktinfo)));
    cmsg->cmsg_level = IPPROTO_IP;
    cmsg->cmsg_type = IP_PKTINFO;
    cmsg->cmsg_len = WSA_CMSG_LEN(sizeof(struct in_pktinfo));
    struct in_pktinfo *pktinfo = (struct in_pktinfo *)WSA_CMSG_DATA(cmsg);
    pktinfo->ipi_addr.s_addr = local_addr->ipv4;
    sum += WSA_CMSG_SPACE(sizeof(struct in_pktinfo));
...

msg.Control.len = sum;

if (bs->WSASendMsg(bs->socket, &msg, 0, &bytes, NULL, NULL) != 0) {
    ...



回答2:


I think this is it.

UdpClient Constructor (IPEndPoint)

This constructor creates a new UdpClient and binds it to the IPEndPoint specified by the localEP parameter. Before you call this constructor, you must create an IPEndPoint using the IP address and port number from which you intend to send and receive data. You do not need to specify a local IP address and port number for sending and receiving data. If you do not, the underlying service provider will assign the most appropriate local IP address and port number.



来源:https://stackoverflow.com/questions/15657974/howto-set-the-udp-source-address-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!