SSDP和接口IP地址 [英] SSDP and interface IP address

查看:288
本文介绍了SSDP和接口IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 UPnP AV /

I'm writing a UPnP AV/DLNA DMS which needs to send and receive SSDP messages. In response to some M-SEARCH packets I need to send a reply with the URL of a resource (in this case a HTTP server), which I've chosen to bind to INADDR_ANY (0.0.0.0). Of course this address is meaningless to the sender of the M-SEARCH packet: The address of the interface on which the M-SEARCH was received is most appropriate.

如何确定要在回复数据包中发送的适当地址?

我考虑过的一些想法是:

Some ideas I've considered are:

  1. 将不同的接收器绑定到每个套接字.当接收方收到M-SEARCH数据包时,回复地址可以在回复中使用套接字的本地地址.但是,这需要了解和迭代所有接口,并随着接口可用性的变化添加和删除接收器.
  2. INADDR_ANY上放置一个接收器,并迭代接口网络掩码以确定可能的源.但是,一个以上的接口可能共享同一子网.
  3. 在收到数据包IP目标地址后将其提取.这将是特定于IP的,并且可能会在网络抽象的某个位置丢失.
  1. Binding a different receiver to each socket. When a receiver gets an M-SEARCH packet, the reply address can use the socket's local address in the reply. However this requires knowing and iterating over all the interfaces, and adding and removing receivers as interface availability changes.
  2. Put a single receiver on INADDR_ANY, and iterate interface netmasks to determine the possible source. However more than one interface might share the same subnet.
  3. Extract the packets IP target address upon receiving it. This would be IP specific, and may be lost somewhere in the network abstraction.

推荐答案

getsockname(2)后跟getnameinfo(3)报告您的TCP/IP堆栈已分配给套接字的IP地址. (显然,如果服务器和客户端位于NAT系统的相对侧,则这与客户端可以使用的方式不匹配;在这种情况下,也许存在巧妙的UPnP欺骗手段来发现客户端可用于联系服务器的IP地址)

getsockname(2) followed by getnameinfo(3) reports the IP address that your TCP/IP stack has assigned to the socket. (Obviously, this won't match what the client could use if server and client are on opposite sides of a NAT system; in that case, perhaps there is clever UPnP trickery to discover the IP address that the client could use to contact the server.)

我认为您的服务器看起来像这样:

I assume your server looks something like this:

lfd = socket();
ret = bind(lfd,...);
connection = listen(lfd, 10);
/* add connection to your select queue or poll queue */

您可以添加类似于以下代码:

You could append code similar to this:

struct sockaddr_storage me;
socklen_t *len = sizeof(me);
char name[40];
ret = getsockname(connection, &me, &len);
ret = getnameinfo(&me, &len, name, sizeof(name), NULL, 0, NI_NUMERICHOST);

getnameinfo(3) 检查

getnameinfo(3) inspects the struct sockaddr_storage me for your IP address. Because these are generic interfaces, it'll work for IPv4 or IPv6 addresses.

这篇关于SSDP和接口IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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