-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathssdp.c
157 lines (126 loc) · 4.18 KB
/
ssdp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* libmicrossdp
* Copyright 2014-2015 Alexander von Gluck IV <kallisti5@unixzen.com>
* Released under the terms of the MIT license
*
* Authors:
* Alexander von Gluck IV <kallisti5@unixzen.com>
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include "ssdp.h"
void
ssdp_packettodevice(char* buffer, struct upnp_device* device)
{
assert(device);
// TODO: Could be more robust.
char* match;
// Match LOCATION / Location
if ((match = strstr(buffer, "LOCATION:")) != NULL) {
int len = strcspn(match, "\r\n");
if (len >= SSDP_TXT_LEN - 10)
len = SSDP_TXT_LEN - 10;
strncpy(device->location, match + 10, len - 10);
} else if ((match = strstr(buffer, "Location:")) != NULL) {
int len = strcspn(match, "\r\n");
if (len >= SSDP_TXT_LEN - 10)
len = SSDP_TXT_LEN - 10;
strncpy(device->location, match + 10, len - 10);
}
// Match ST
if ((match = strstr(buffer, "ST:")) != NULL) {
int len = strcspn(match, "\r\n");
if (len >= SSDP_TXT_LEN - 4)
len = SSDP_TXT_LEN - 4;
strncpy(device->type, match + 4, len - 4);
}
}
int
ssdp_discovery(int family, unsigned int category, struct upnp_device* devices)
{
struct in_addr localInterface;
struct sockaddr_in groupSock;
struct sockaddr_in localSock;
struct ip_mreq group;
assert(devices);
int udpSocket = socket(family, SOCK_DGRAM, 0);
// Reuse socket
int reuse = 1;
setsockopt(udpSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
// 100ms second timeout on recvfrom
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
setsockopt(udpSocket, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv, sizeof(tv));
char loopch = 0;
setsockopt(udpSocket, IPPROTO_IP, IP_MULTICAST_LOOP, (char*)&loopch, sizeof(loopch));
groupSock.sin_family = family;
// TODO: Change for IPv6
groupSock.sin_addr.s_addr = inet_addr("239.255.255.250");
groupSock.sin_port = htons(1900);
//localInterface.s_addr = inet_addr("10.80.65.13");
//setsockopt(udpSocket, IPPROTO_IP, IP_MULTICAST_IF, (char*)&localInterface, sizeof(localInterface));
localSock.sin_family = family;
localSock.sin_port = htons(1900);
localSock.sin_addr.s_addr = INADDR_ANY;
bind(udpSocket, (struct sockaddr*)&localSock, sizeof(localSock));
group.imr_multiaddr.s_addr = inet_addr("239.255.255.250");
//group.imr_interface.s_addr = inet_addr("10.80.65.13");
setsockopt(udpSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group));
// ssdp:all?
int dcpIndex = 0;
int count = 0;
for (dcpIndex = 0; dcpIndex < (sizeof(kKnownDCP) / sizeof(kKnownDCP[0])); dcpIndex++) {
// Skip dcp's not matching our category filter
if (((kKnownDCP[dcpIndex].category & category) == 0) || count >= SSDP_MAX)
continue;
//printf("seeking: %s\n", kKnownDCP[dcpIndex].type);
// form our query
char search[SSDP_PACKET_BUFFER] = "\0";
snprintf(search, SSDP_PACKET_BUFFER, "M-SEARCH * HTTP/1.1\r\nHOST: %s:%d\r\nST: %s\r\nMAN: \"ssdp:discover\"\r\nMX: 2\r\n\r\n",
"239.255.255.250", 1900, kKnownDCP[dcpIndex].type);
//printf("query:\n%s", search);
// ask
sendto(udpSocket, search, SSDP_PACKET_BUFFER, 0, (struct sockaddr*)&groupSock, sizeof(groupSock));
// parse responses
time_t start = time(NULL);
while ((time(NULL) - start) < 3) {
struct sockaddr_in si_other;
socklen_t slen = sizeof(si_other);
char buffer[SSDP_PACKET_BUFFER] = "";
recvfrom(udpSocket, buffer, SSDP_PACKET_BUFFER, 0, (struct sockaddr*)&si_other, &slen);
// HTTP/1.1's are responses. NOTIFY are random broadcasts
if (strstr(buffer, "HTTP/1.1 200 OK") != NULL) {
#if 0
printf("------------------\n");
printf("response:\n");
printf("%s\n", buffer);
#endif
ssdp_packettodevice(buffer, &devices[count]);
count++;
// Reset timer when response
start = time(NULL);
}
}
}
shutdown(udpSocket, 0);
return count;
}
int
main()
{
struct upnp_device devices[SSDP_MAX];
int found = ssdp_discovery(AF_INET, SSDP_CAT_PRINTER | SSDP_CAT_SCANNER, devices);
printf("Discovered %d devices:\n", found);
int index = 0;
for (index = 0; index < found; index++) {
printf("\t'%s' - '%s'\n", devices[index].type, devices[index].location);
}
return 0;
}