侦听UDP单播回复UDP多播消息

时间:2014-10-09 10:57:39

标签: sockets udp multicast upnp

我正在尝试在linux上实现一个非常简单的upnp控制器,这样我就可以控制一台需要专有软件的设备。

文档说我需要将特定表单的UDP多播请求(请参阅下面的代码中的" M-SEARCH"字符串)发送到特定的地址和端口,并且设备将响应通过UDP 单播到我发送的地址和端口。

我无法完成这项工作。 tcpdump显示UDP组播请求转到正确的地址和端口,格式显示正确,但我看不到回复。

我正在发送和监听环回接口(设备在同一台机器上)。

另一个upnp控制器(即不是我的)在环回接口上正常工作。

有人可以建议我做错了什么吗?

以下是代码:

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <netinet/udp.h>
 #include <unistd.h>
 #include <fcntl.h>

 #define MAXBUFSIZE 65536

int main(int argc, char ** argv ) {

unsigned char loop;
loop = 0;
unsigned char ttl;
ttl = 4;
int bcast;
bcast = 1;

int sock;

sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
    perror("socket");
    exit(EXIT_FAILURE);
}

struct sockaddr_in destadd;
memset(&destadd, 0, sizeof(destadd));
destadd.sin_family = AF_INET;
destadd.sin_port = htons((uint16_t)1900);
if (inet_pton(AF_INET, "239.255.255.250", &destadd.sin_addr) < 1) {
    perror("inet_pton dest");
    exit(EXIT_FAILURE);
}

struct sockaddr_in interface_addr;
memset(&interface_addr, 0, sizeof(interface_addr));
interface_addr.sin_family = AF_INET;
interface_addr.sin_port = htons(0);
if (inet_pton(AF_INET, "127.0.0.1", &interface_addr.sin_addr) < 1) {
    perror("inet_pton interface");
    exit(EXIT_FAILURE);
}

if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0){
    perror("setsockopt loop");
    exit(EXIT_FAILURE);
}

if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0){
    perror("setsockopt ttl");
    exit(EXIT_FAILURE);
}

if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
               (struct in_addr *)&interface_addr.sin_addr,
               sizeof(interface_addr.sin_addr)) < 0) {
    perror("setsockopt if");
    exit(EXIT_FAILURE);
}

if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &bcast, sizeof(bcast)) < 0) {
    perror("setsockopt bcast");
    exit(EXIT_FAILURE);
}

struct ip_mreqn imr;
memset(&imr, 0, sizeof(imr));
if (inet_pton(AF_INET, "239.255.255.250", &imr.imr_multiaddr.s_addr) < 1) {
    perror("inet_pton");
    exit(EXIT_FAILURE);
}
inet_pton(AF_INET, "127.0.0.1", (struct in_addr *)&imr.imr_address);
imr.imr_ifindex = 0;
if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
               (void *)&imr, sizeof(imr)) < 0) {
    perror("setsockopt addmem");
    exit(EXIT_FAILURE);
}

if (bind(sock, (struct sockaddr *)&interface_addr,
    sizeof(struct sockaddr_in)) < 0) {
    perror("bind");
    exit(EXIT_FAILURE);
}

char buffer[1024];

strcpy(buffer, "M-SEARCH * HTTP/1.1\r\n"
                   "Host: 239.255.255.250:1900\r\n"
                   "Man: \"ssdp:discover\"\r\n"
                   "ST: upnp:rootdevice\r\n"
                   "MX: 3\r\n"
                   "User-Agent: Test/1.0\r\n"
                   "\r\n");

if (sendto(sock, buffer, strlen(buffer), 0, (struct sockaddr*)&destadd,
       sizeof(destadd)) < 0) {
    perror("sendto");
    exit(EXIT_FAILURE);
}

if (recvfrom(sock, &buffer, sizeof(buffer)-1, 0, NULL, NULL) < 0) {
    perror("recvfrom");
    exit(EXIT_FAILURE);
}


if (close(sock) < 0) {
    perror("close");
    exit(EXIT_FAILURE);
}

}

2 个答案:

答案 0 :(得分:1)

最终为我工作的代码如下。

方法摘要:

  • 创建UDP套接字
  • 启用IF_MULTICAST_LOOP(以允许与同一主机上的客户端通信)
  • 设置IF_MULTICAST_TTL
  • 绑定到INADDR_ANY和端口8201(端口号,任意选择)
  • 发送多播消息至239.255.255.250:1900
  • 使用相同的套接字接收回复

我们走了:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/udp.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char ** argv ) {

unsigned char loop;
loop = 1; // Needs to be on to get replies from clients on the same host
unsigned char ttl;
ttl = 4;
int bcast;
bcast = 1;

int sock;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
    perror("socket");
    exit(EXIT_FAILURE);
}

// Multicast message will be sent to 239.255.255.250:1900
struct sockaddr_in destadd;
memset(&destadd, 0, sizeof(destadd));
destadd.sin_family = AF_INET;
destadd.sin_port = htons((uint16_t)1900);
if (inet_pton(AF_INET, "239.255.255.250", &destadd.sin_addr) < 1) {
    perror("inet_pton dest");
    exit(EXIT_FAILURE);
}

// Listen on all interfaces on port 8201 
struct sockaddr_in interface_addr;
memset(&interface_addr, 0, sizeof(interface_addr));
interface_addr.sin_family = AF_INET;
interface_addr.sin_port = htons(8201);
interface_addr.sin_addr.s_addr = htonl(INADDR_ANY);

// Got to have this to get replies from clients on same machine
if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0){
    perror("setsockopt loop");
    exit(EXIT_FAILURE);
}

if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0){
    perror("setsockopt ttl");
    exit(EXIT_FAILURE);
}

// Bind to port 8201 on all interfaces
if (bind(sock, (struct sockaddr *)&interface_addr,
    sizeof(struct sockaddr_in)) < 0) {
    perror("bind");
    exit(EXIT_FAILURE);
}

char buffer[1024];

strcpy(buffer, "M-SEARCH * HTTP/1.1\r\n"
               "Host: 239.255.255.250:1900\r\n"
               "Man: \"ssdp:discover\"\r\n"
               "ST: upnp:rootdevice\r\n"
               "MX: 3\r\n"
               "User-Agent: Test/1.0\r\n"
               "\r\n");

if (sendto(sock, buffer, strlen(buffer), 0, (struct sockaddr*)&destadd,
       sizeof(destadd)) < 0) {
    perror("sendto");
    exit(EXIT_FAILURE);
}

if (recvfrom(sock, &buffer, sizeof(buffer)-1, 0, NULL, NULL) < 0) {
    perror("recvfrom");
    exit(EXIT_FAILURE);
}


printf("%s\n", buffer);

if (close(sock) < 0) {
    perror("close");
    exit(EXIT_FAILURE);
}
}

要获得外部客户端的回复,请确保未阻止端口8201。

答案 1 :(得分:0)

如评论中所示,route -n的输出:

0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0 192.168.1.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0

建议发送到239.x.x.x的数据包将通过eth0,而不是环回。

因此,在loopback接口上添加静态路由以强制传出数据包,或者确保通过IF_MULTICAST_LOOP标志可以通过环回接收是有意义的。