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