如何确定将哪个网络接口(IP地址)用于将数据包发送到特定的IP地址? [英] How to detemine which network interface (ip address) will be used to send a packet to a specific ip address?

查看:61
本文介绍了如何确定将哪个网络接口(IP地址)用于将数据包发送到特定的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个SIP堆栈,并且需要在消息中插入一个IP地址.该地址必须是用于发送消息的地址.我知道目标IP,并且需要确定将用于发送消息的NIC(其地址)....

I'm writing a SIP stack, and I need to insert an ip address in the message. This address needs to be the one used for sending the message. I know the destination IP and need to determine the NIC (its address) that will be used to send the message....

推荐答案

要扩展雷米·勒博的评论,

To expand a bit on Remy Lebeau's comment, GetBestInterfaceEx() is your best bet, if you're on Windows XP or newer. That will work for both IPv4 and IPv6 addresses.

GetBestInterface/GetBestInterfaceEx返回最合适的接口的索引(称为IDX),用于与某些地址联系.

GetBestInterface/GetBestInterfaceEx return the index (call it IDX) of the most appropriate interface to use to contact some address.

然后,您可以使用

Then you can map that index into a local IP address by getting your interface<->IP address mapping using GetIpAddrTable or GetAdaptersAddresses if you're dual-stacking (supporting both IPv6 and IPv4).

遍历该表并找到与IDX匹配的dwIndex(如果是GetAdaptersAddresses,则为IfIndex)的接口.

Iterate over that table and find the interface with the dwIndex (or IfIndex, in the case of GetAdaptersAddresses) matching IDX.

这篇关于如何确定将哪个网络接口(IP地址)用于将数据包发送到特定的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆