#include <string.h>
#include <stdio.h>
#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+
+#include "common.h"
#include "mcast.h"
int is_mcast(char *url)
int open_mcast(char *url)
{
- fprintf(stderr,"multicast currently unimplemented!\n");
+ struct dvb_host *dvbhost = NULL;
+ struct sockaddr_in server;
+ struct ip_mreq mreq;
+ int val;
+ int fd;
+
+ if(!is_mcast(url))
+ return -1;
+
+ dvbhost = parse(&(url[6]), "2000");
+ dvbhost->socktype = SOCK_DGRAM;
+
+ if (resolve(dvbhost, &server) < 0) {
+ return -1;
+ }
+
+ bzero(&mreq, sizeof(mreq));
+ mreq.imr_multiaddr = server.sin_addr;
+ mreq.imr_interface.s_addr = htonl(INADDR_ANY);
+
+ if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
+ perror("socket");
+ return -1;
+ }
+
+ val = 1;
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
+ perror("setsockopt");
+ return -1;
+ }
+
+ if (bind(fd, (struct sockaddr*)&server, sizeof(server)) < 0) {
+ perror("bind");
+ return -1;
+ }
+
+ if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
+ perror("setsockopt");
+ return -1;
+ }
- exit(EXIT_FAILURE);
+ return fd;
}