]> cvs.zerfleddert.de Git - record-dvb/blobdiff - sap.c
ignore dump-stream binary
[record-dvb] / sap.c
diff --git a/sap.c b/sap.c
index 684c509bf25c81c27365b78bd01b7dcda25d95e0..6a8ef799e6a0831a6de2f5fa6be677045cfe9ca5 100644 (file)
--- a/sap.c
+++ b/sap.c
@@ -8,6 +8,8 @@
 #include <time.h>
 #include <string.h>
 #include <strings.h>
 #include <time.h>
 #include <string.h>
 #include <strings.h>
+#include <unistd.h>
+#include <sys/select.h>
 
 #include "mcast.h"
 #include "sap.h"
 
 #include "mcast.h"
 #include "sap.h"
 
 #define BUFFSIZE       SAP_MAX_SIZE
 
 
 #define BUFFSIZE       SAP_MAX_SIZE
 
+struct sdp_info {
+       char *service;
+       char *host;
+       char *proto;
+       char *port;
+};
+
+struct sdp_info *parse_sdp(char *sdp, int len)
+{
+       char *pos, *line;
+       static struct sdp_info sdpinfo;
+
+       /* RFC 2327
+        * v=0
+        * o=- 6dca 1 IN IP4 192.168.100.17:2000
+        * s=TV Das Erste
+        * t=0 0
+        * c=IN IP4 192.168.100.17/1
+        * m=video 2000 http 33
+        * a=tool:getstream
+        * a=type:broadcast
+        */
+
+       bzero(&sdpinfo, sizeof(struct sdp_info));
+
+       pos = line = sdp;
+       while(*pos != 0 && (pos-sdp) < len) {
+               if (*pos == 0x0d)
+                       *pos = 0;
+
+               if (*pos == 0x0a) {
+                       *pos = 0;
+
+                       if (!strncasecmp("s=", line, 2)) {
+                               sdpinfo.service = line + 2;
+                       } else if (!strncasecmp("c=", line, 2)) {
+                               int poscnt = 0;
+
+                               line += 2;
+                               while (*line != 0) {
+                                       if (poscnt == 2 && *line == '/') {
+                                               *line = 0;
+                                               break;
+                                       }
+
+                                       if (*line == ' ') {
+                                               *line = 0;
+                                               poscnt++;
+
+                                               /* c=<network type> <address type> <connection address> */
+                                               if (poscnt == 2)
+                                                       sdpinfo.host = line + 1;
+
+                                               if (poscnt > 2)
+                                                       break;
+                                       }
+                                       line++;
+                               }
+                       } else if (!strncasecmp("m=", line, 2)) {
+                               int poscnt = 0;
+
+                               line += 2;
+                               while (*line != 0) {
+                                       if (*line == ' ') {
+                                               *line = 0;
+                                               poscnt++;
+
+                                               /* m=<media> <port> <transport> <fmt list> */
+                                               if (poscnt == 1)
+                                                       sdpinfo.port = line + 1;
+
+                                               if (poscnt == 2)
+                                                       sdpinfo.proto = line + 1;
+
+                                               if (poscnt > 2)
+                                                       break;
+                                       }
+                                       line++;
+                               }
+                       }
+
+                       line = ++pos;
+                       continue;
+               }
+               pos++;
+       }
+
+       return &sdpinfo;
+}
+
+
 char *get_url_from_sap(char *service)
 {
        struct timeval start, curr;
        struct ip_mreq mreq;
        unsigned char buffer[BUFFSIZE];
 char *get_url_from_sap(char *service)
 {
        struct timeval start, curr;
        struct ip_mreq mreq;
        unsigned char buffer[BUFFSIZE];
+       struct in_addr sapinaddr;
        char *url = NULL;
        int fd;
 
        char *url = NULL;
        int fd;
 
@@ -36,15 +130,36 @@ char *get_url_from_sap(char *service)
 
        do {
                int recvd;
 
        do {
                int recvd;
+               fd_set rfds;
+               struct timeval tv;
+               int retval;
                int sap_version, sap_addrtype, sap_messagetype, sap_encrypted, sap_compressed;
                int sap_version, sap_addrtype, sap_messagetype, sap_encrypted, sap_compressed;
-               in_addr_t sender_address;
+               uint8_t sender_address[16]; /* This might be IPv6! */
                unsigned char auth_len;
                unsigned short msgid;
                unsigned char auth_len;
                unsigned short msgid;
-               unsigned char *payload, *pos, *host = NULL, *proto = NULL, *port = NULL, *sname = NULL;
+               unsigned char *sdp;
+               struct sdp_info *sdpinfo;
+
+               FD_ZERO(&rfds);
+               FD_SET(fd, &rfds);
+
+               tv.tv_sec = 0;
+               tv.tv_usec = 100000;
+
+               if ((retval = select(fd+1, &rfds, NULL, NULL, &tv)) == -1) {
+                       perror("select");
+                       break;
+               }
+
+               if (!retval) {
+                       gettimeofday(&curr, NULL);
+                       continue;
+               }
 
 
+               bzero(buffer, BUFFSIZE);
                if ((recvd = recv(fd, buffer, BUFFSIZE, 0)) < 1) {
                        perror("recv");
                if ((recvd = recv(fd, buffer, BUFFSIZE, 0)) < 1) {
                        perror("recv");
-                       return NULL;
+                       break;
                }
 
                gettimeofday(&curr, NULL);
                }
 
                gettimeofday(&curr, NULL);
@@ -56,8 +171,8 @@ char *get_url_from_sap(char *service)
                sap_compressed = buffer[0] & 0x1;
                auth_len = buffer[1];
                msgid = buffer[2] << 8 | buffer[3];
                sap_compressed = buffer[0] & 0x1;
                auth_len = buffer[1];
                msgid = buffer[2] << 8 | buffer[3];
-               memcpy(&sender_address, buffer+4, (sap_addrtype?16:4));
-               payload = buffer + 4 /* (sap_*, auth_len, msgid) */ + (sap_addrtype?16:4) + auth_len;
+               memcpy(sender_address, buffer+4, (sap_addrtype?16:4));
+               sdp = buffer + 4 /* (sap_*, auth_len, msgid) */ + (sap_addrtype?16:4) + auth_len;
 
 #ifdef DEBUG
                printf("\n");
 
 #ifdef DEBUG
                printf("\n");
@@ -67,13 +182,10 @@ char *get_url_from_sap(char *service)
                printf("Encrypted: %d\n", sap_encrypted);
                printf("Compressed: %d\n", sap_compressed);
                printf("Authentication Length: %d\n", auth_len);
                printf("Encrypted: %d\n", sap_encrypted);
                printf("Compressed: %d\n", sap_compressed);
                printf("Authentication Length: %d\n", auth_len);
-               printf("Sender: %u\n", sender_address);
+               printf("Sender: %u\n", *((in_addr_t*)sender_address));
                printf("Message Identifier Hash: %u\n", msgid);
 #endif
 
                printf("Message Identifier Hash: %u\n", msgid);
 #endif
 
-               if (sap_addrtype)
-                       continue; /* We don't support IPv6 for now */
-
 #if 0 /* Getstream gets this wrong, see rfc2974 */
                if (sap_messagetype)
                        continue; /* We are not interested in deletions */
 #if 0 /* Getstream gets this wrong, see rfc2974 */
                if (sap_messagetype)
                        continue; /* We are not interested in deletions */
@@ -82,110 +194,49 @@ char *get_url_from_sap(char *service)
                if (sap_encrypted || sap_compressed)
                        continue;
 
                if (sap_encrypted || sap_compressed)
                        continue;
 
-               /* RFC 2327
-                * v=0
-                * o=- 6dca 1 IN IP4 192.168.100.17:2000
-                * s=TV Das Erste
-                * t=0 0
-                * c=IN IP4 192.168.100.17/1
-                * m=video 2000 http 33
-                * a=tool:getstream
-                * a=type:broadcast
-                */
-
-               pos = payload;
-               while(*pos != 0 && (pos-buffer) < BUFFSIZE) {
-                       if (*pos == 0x0d) {
-                               *pos = 0;
-                       }
-
-                       if (*pos == 0x0a) {
-                               *pos = 0;
+               sdpinfo = parse_sdp(sdp, recvd-(sdp-buffer));
 
 
-                               if (!strncasecmp("s=", payload, 2)) {
-                                       sname = payload + 2;
-                               } else if (!strncasecmp("c=", payload, 2)) {
-                                       int poscnt = 0;
+               if (sdpinfo->service && sdpinfo->proto && sdpinfo->port) {
+                       char hostbuf[INET6_ADDRSTRLEN];
 
 
-                                       payload += 2;
-                                       while (*payload != 0) {
-                                               if (poscnt == 2 && *payload == '/') {
-                                                       *payload = 0;
-                                                       break;
-                                               }
-
-                                               if (*payload == ' ') {
-                                                       *payload = 0;
-                                                       poscnt++;
-
-                                                       /* c=<network type> <address type> <connection address> */
-                                                       if (poscnt == 2) { 
-                                                               host = payload + 1;
-                                                       }
-
-                                                       if (poscnt > 2) {
-                                                               break;
-                                                       }
-                                               }
-                                               payload++;
-                                       }
-                               } else if (!strncasecmp("m=", payload, 2)) {
-                                       int poscnt = 0;
-
-                                       payload += 2;
-                                       while (*payload != 0) {
-                                               if (*payload == ' ') {
-                                                       *payload = 0;
-                                                       poscnt++;
-
-                                                       /* m=<media> <port> <transport> <fmt list> */
-                                                       if (poscnt == 1) { 
-                                                               port = payload + 1;
-                                                       }
-
-                                                       if (poscnt == 2) { 
-                                                               proto = payload + 1;
-                                                       }
-
-                                                       if (poscnt > 2) {
-                                                               break;
-                                                       }
-                                               }
-                                               payload++;
-                                       }
-                               }
+                       if (!sdpinfo->host) {
 
 
-                               payload = ++pos;
-                               continue;
-                       }
-                       pos++;
-               }
+                                if (sap_addrtype) {
+                                        struct in6_addr in6addr;
 
 
-               if (sname && proto && port) {
-                       if (!host) {
-                                struct in_addr inaddr;
+                                        memcpy(in6addr.in6_u.u6_addr8, sender_address, 16);
+                                        if (!(sdpinfo->host = (char*)inet_ntop(AF_INET6, &in6addr, hostbuf, INET6_ADDRSTRLEN))) {
+                                                perror("inet_ntop");
+                                                continue;
+                                        }
+                                } else {
+                                        struct in_addr inaddr;
 
 
-                                inaddr.s_addr = sender_address;
-                                host = inet_ntoa(inaddr);
+                                        inaddr.s_addr = *((in_addr_t*)sender_address);
+                                        if (!(sdpinfo->host = (char*)inet_ntop(AF_INET, &inaddr, hostbuf, INET6_ADDRSTRLEN))) {
+                                                perror("inet_ntop");
+                                                continue;
+                                        }
+                                }
                        }
 
 #ifdef DEBUG
                        }
 
 #ifdef DEBUG
-                       printf("%s -> %s://%s:%s\n", sname, proto, host, port);
+                       printf("%s -> %s://%s:%s\n", sdpinfo->service, sdpinfo->proto, sdpinfo->host, sdpinfo->port);
 #endif
 
 #endif
 
-                       if (strlen(service) < strlen(sname)) {
-                               sname += strlen(sname) - strlen(service);
+                       if (strlen(service) < strlen(sdpinfo->service)) {
+                               sdpinfo->service += strlen(sdpinfo->service) - strlen(service);
                        }
 
                        }
 
-                       if (!strncasecmp(service, sname, strlen(service))) {
-                               int len = strlen(host)+strlen(proto)+strlen(port)+5;
+                       if (!strncasecmp(service, sdpinfo->service, strlen(service))) {
+                               int len = strlen(sdpinfo->host)+strlen(sdpinfo->proto)+strlen(sdpinfo->port)+5;
 
                                if (!(url = malloc(len))) {
                                        perror("malloc");
 
                                if (!(url = malloc(len))) {
                                        perror("malloc");
-                                       return NULL;
+                                       break;
                                }
 
                                }
 
-                               snprintf(url, len, "%s://%s:%s", proto, host, port);
+                               snprintf(url, len, "%s://%s:%s", sdpinfo->proto, sdpinfo->host, sdpinfo->port);
                                url[len-1] = 0;
                                break;
                        }
                                url[len-1] = 0;
                                break;
                        }
@@ -193,7 +244,11 @@ char *get_url_from_sap(char *service)
 
        } while(curr.tv_sec < start.tv_sec+SAP_TIMEOUT);
 
 
        } while(curr.tv_sec < start.tv_sec+SAP_TIMEOUT);
 
-       mreq.imr_multiaddr.s_addr = inet_addr(SAP_ADDR);
+       if (!(inet_aton(SAP_ADDR, &sapinaddr))) {
+               perror("inet_aton");
+       }
+
+       mreq.imr_multiaddr.s_addr = sapinaddr.s_addr;
        mreq.imr_interface.s_addr = INADDR_ANY;
        setsockopt (fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
 
        mreq.imr_interface.s_addr = INADDR_ANY;
        setsockopt (fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
 
Impressum, Datenschutz