8447845f |
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include <sys/types.h> |
4 | #include <sys/socket.h> |
5 | #include <netinet/in.h> |
6 | #include <arpa/inet.h> |
7 | #include <sys/time.h> |
8 | #include <time.h> |
9 | #include <string.h> |
10 | #include <strings.h> |
89ec024f |
11 | #include <unistd.h> |
12 | #include <sys/select.h> |
8447845f |
13 | |
14 | #include "mcast.h" |
15 | #include "sap.h" |
16 | |
17 | #define SAP_ADDR "224.2.127.254" |
18 | #define SAP_PORT 9875 |
19 | #define SAP_MAX_SIZE 1024 |
20 | |
21 | #define SAP_TIMEOUT 3 |
22 | |
23 | #define BUFFSIZE SAP_MAX_SIZE |
24 | |
25 | char *get_url_from_sap(char *service) |
26 | { |
27 | struct timeval start, curr; |
28 | struct ip_mreq mreq; |
29 | unsigned char buffer[BUFFSIZE]; |
30 | char *url = NULL; |
31 | int fd; |
32 | |
33 | snprintf(buffer,BUFFSIZE,"udp://%s:%u", SAP_ADDR, SAP_PORT); |
34 | |
35 | fd = open_mcast(buffer); |
36 | |
37 | gettimeofday(&start, NULL); |
38 | |
39 | do { |
40 | int recvd; |
41 | int sap_version, sap_addrtype, sap_messagetype, sap_encrypted, sap_compressed; |
42 | in_addr_t sender_address; |
43 | unsigned char auth_len; |
44 | unsigned short msgid; |
45 | unsigned char *payload, *pos, *host = NULL, *proto = NULL, *port = NULL, *sname = NULL; |
89ec024f |
46 | fd_set rfds; |
47 | struct timeval tv; |
48 | int retval; |
49 | |
50 | FD_ZERO(&rfds); |
51 | FD_SET(fd, &rfds); |
52 | |
53 | tv.tv_sec = 0; |
d5b58906 |
54 | tv.tv_usec = 100000; |
89ec024f |
55 | |
56 | if ((retval = select(fd+1, &rfds, NULL, NULL, &tv)) == -1) { |
57 | perror("select"); |
58 | return NULL; |
59 | } |
60 | |
61 | if (!retval) { |
62 | gettimeofday(&curr, NULL); |
63 | continue; |
64 | } |
8447845f |
65 | |
66 | if ((recvd = recv(fd, buffer, BUFFSIZE, 0)) < 1) { |
67 | perror("recv"); |
68 | return NULL; |
69 | } |
70 | |
71 | gettimeofday(&curr, NULL); |
72 | |
73 | sap_version = (buffer[0] >> 5) & 0x7; |
74 | sap_addrtype = (buffer[0] >> 4) & 0x1; |
75 | sap_messagetype = (buffer[0] >> 2) & 0x1; |
76 | sap_encrypted = (buffer[0] >> 1) & 0x1; |
77 | sap_compressed = buffer[0] & 0x1; |
78 | auth_len = buffer[1]; |
79 | msgid = buffer[2] << 8 | buffer[3]; |
80 | memcpy(&sender_address, buffer+4, (sap_addrtype?16:4)); |
81 | payload = buffer + 4 /* (sap_*, auth_len, msgid) */ + (sap_addrtype?16:4) + auth_len; |
82 | |
83 | #ifdef DEBUG |
84 | printf("\n"); |
85 | printf("SAP-Version: %d\n", sap_version); |
86 | printf("Adresstyp: %s\n", (sap_addrtype?"IPv6":"IPv4")); |
87 | printf("Messagetype: %s\n", (sap_messagetype?"Announcement":"Deletion")); |
88 | printf("Encrypted: %d\n", sap_encrypted); |
89 | printf("Compressed: %d\n", sap_compressed); |
90 | printf("Authentication Length: %d\n", auth_len); |
91 | printf("Sender: %u\n", sender_address); |
92 | printf("Message Identifier Hash: %u\n", msgid); |
93 | #endif |
94 | |
95 | if (sap_addrtype) |
96 | continue; /* We don't support IPv6 for now */ |
97 | |
98 | #if 0 /* Getstream gets this wrong, see rfc2974 */ |
99 | if (sap_messagetype) |
100 | continue; /* We are not interested in deletions */ |
101 | #endif |
102 | |
103 | if (sap_encrypted || sap_compressed) |
104 | continue; |
b06a3250 |
105 | |
8447845f |
106 | /* RFC 2327 |
107 | * v=0 |
108 | * o=- 6dca 1 IN IP4 192.168.100.17:2000 |
109 | * s=TV Das Erste |
110 | * t=0 0 |
111 | * c=IN IP4 192.168.100.17/1 |
112 | * m=video 2000 http 33 |
113 | * a=tool:getstream |
114 | * a=type:broadcast |
115 | */ |
116 | |
117 | pos = payload; |
118 | while(*pos != 0 && (pos-buffer) < BUFFSIZE) { |
119 | if (*pos == 0x0d) { |
120 | *pos = 0; |
121 | } |
122 | |
123 | if (*pos == 0x0a) { |
124 | *pos = 0; |
125 | |
126 | if (!strncasecmp("s=", payload, 2)) { |
127 | sname = payload + 2; |
128 | } else if (!strncasecmp("c=", payload, 2)) { |
129 | int poscnt = 0; |
130 | |
131 | payload += 2; |
132 | while (*payload != 0) { |
133 | if (poscnt == 2 && *payload == '/') { |
134 | *payload = 0; |
135 | break; |
136 | } |
137 | |
138 | if (*payload == ' ') { |
139 | *payload = 0; |
140 | poscnt++; |
141 | |
142 | /* c=<network type> <address type> <connection address> */ |
143 | if (poscnt == 2) { |
144 | host = payload + 1; |
145 | } |
146 | |
147 | if (poscnt > 2) { |
148 | break; |
149 | } |
150 | } |
151 | payload++; |
152 | } |
153 | } else if (!strncasecmp("m=", payload, 2)) { |
154 | int poscnt = 0; |
155 | |
156 | payload += 2; |
157 | while (*payload != 0) { |
158 | if (*payload == ' ') { |
159 | *payload = 0; |
160 | poscnt++; |
161 | |
162 | /* m=<media> <port> <transport> <fmt list> */ |
163 | if (poscnt == 1) { |
164 | port = payload + 1; |
165 | } |
166 | |
167 | if (poscnt == 2) { |
168 | proto = payload + 1; |
169 | } |
170 | |
171 | if (poscnt > 2) { |
172 | break; |
173 | } |
174 | } |
175 | payload++; |
176 | } |
177 | } |
178 | |
179 | payload = ++pos; |
180 | continue; |
181 | } |
182 | pos++; |
183 | } |
184 | |
185 | if (sname && proto && port) { |
186 | if (!host) { |
187 | struct in_addr inaddr; |
188 | |
189 | inaddr.s_addr = sender_address; |
190 | host = inet_ntoa(inaddr); |
191 | } |
192 | |
193 | #ifdef DEBUG |
194 | printf("%s -> %s://%s:%s\n", sname, proto, host, port); |
195 | #endif |
196 | |
197 | if (strlen(service) < strlen(sname)) { |
198 | sname += strlen(sname) - strlen(service); |
199 | } |
200 | |
201 | if (!strncasecmp(service, sname, strlen(service))) { |
202 | int len = strlen(host)+strlen(proto)+strlen(port)+5; |
203 | |
204 | if (!(url = malloc(len))) { |
205 | perror("malloc"); |
206 | return NULL; |
207 | } |
208 | |
209 | snprintf(url, len, "%s://%s:%s", proto, host, port); |
210 | url[len-1] = 0; |
211 | break; |
212 | } |
213 | } |
214 | |
215 | } while(curr.tv_sec < start.tv_sec+SAP_TIMEOUT); |
216 | |
217 | mreq.imr_multiaddr.s_addr = inet_addr(SAP_ADDR); |
218 | mreq.imr_interface.s_addr = INADDR_ANY; |
219 | setsockopt (fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)); |
220 | |
90d8d87b |
221 | close(fd); |
222 | |
8447845f |
223 | return url; |
224 | } |