]>
git.zerfleddert.de Git - record-dvb/blob - sap.c
bc51aa5bea9d059640a1bbb28657fffa0f0160c1
4 #include <sys/socket.h>
5 #include <netinet/in.h>
12 #include <sys/select.h>
17 #define SAP_ADDR "224.2.127.254"
19 #define SAP_MAX_SIZE 1024
23 #define BUFFSIZE SAP_MAX_SIZE
25 char *get_url_from_sap(char *service
)
27 struct timeval start
, curr
;
29 unsigned char buffer
[BUFFSIZE
];
33 snprintf(buffer
,BUFFSIZE
,"udp://%s:%u", SAP_ADDR
, SAP_PORT
);
35 fd
= open_mcast(buffer
);
37 gettimeofday(&start
, NULL
);
41 int sap_version
, sap_addrtype
, sap_messagetype
, sap_encrypted
, sap_compressed
;
42 in_addr_t sender_address
;
43 unsigned char auth_len
;
45 unsigned char *payload
, *pos
, *host
= NULL
, *proto
= NULL
, *port
= NULL
, *sname
= NULL
;
56 if ((retval
= select(fd
+1, &rfds
, NULL
, NULL
, &tv
)) == -1) {
62 gettimeofday(&curr
, NULL
);
66 if ((recvd
= recv(fd
, buffer
, BUFFSIZE
, 0)) < 1) {
71 gettimeofday(&curr
, NULL
);
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;
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
;
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
);
96 continue; /* We don't support IPv6 for now */
98 #if 0 /* Getstream gets this wrong, see rfc2974 */
100 continue; /* We are not interested in deletions */
103 if (sap_encrypted
|| sap_compressed
)
108 * o=- 6dca 1 IN IP4 192.168.100.17:2000
111 * c=IN IP4 192.168.100.17/1
112 * m=video 2000 http 33
118 while(*pos
!= 0 && (pos
-buffer
) < BUFFSIZE
) {
126 if (!strncasecmp("s=", payload
, 2)) {
128 } else if (!strncasecmp("c=", payload
, 2)) {
132 while (*payload
!= 0) {
133 if (poscnt
== 2 && *payload
== '/') {
138 if (*payload
== ' ') {
142 /* c=<network type> <address type> <connection address> */
153 } else if (!strncasecmp("m=", payload
, 2)) {
157 while (*payload
!= 0) {
158 if (*payload
== ' ') {
162 /* m=<media> <port> <transport> <fmt list> */
185 if (sname
&& proto
&& port
) {
187 struct in_addr inaddr
;
189 inaddr
.s_addr
= sender_address
;
190 host
= inet_ntoa(inaddr
);
194 printf("%s -> %s://%s:%s\n", sname
, proto
, host
, port
);
197 if (strlen(service
) < strlen(sname
)) {
198 sname
+= strlen(sname
) - strlen(service
);
201 if (!strncasecmp(service
, sname
, strlen(service
))) {
202 int len
= strlen(host
)+strlen(proto
)+strlen(port
)+5;
204 if (!(url
= malloc(len
))) {
209 snprintf(url
, len
, "%s://%s:%s", proto
, host
, port
);
215 } while(curr
.tv_sec
< start
.tv_sec
+SAP_TIMEOUT
);
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
));