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