nl
[record-dvb] / mcast.c
CommitLineData
5c2e3e44 1#include <strings.h>
2#include <string.h>
3#include <stdio.h>
4#include <stdlib.h>
fbc24ab0 5#include <sys/types.h>
6#include <sys/socket.h>
7#include <netdb.h>
d5eca7f2 8
9#include "common.h"
5c2e3e44 10#include "mcast.h"
11
12int is_mcast(char *url)
13{
14 if (strlen(url) < 7)
15 return 0;
16
17 if (!strncasecmp("udp://",url,6))
18 return 1;
19
20 return 0;
21}
22
23int open_mcast(char *url)
24{
fbc24ab0 25 static struct dvb_host *dvbhost = NULL;
26 struct sockaddr_in server;
27 struct ip_mreq mreq;
d1199471 28 int val;
d5eca7f2 29 int fd;
30
6bcf6c4c 31 if(!is_mcast(url))
32 return -1;
5c2e3e44 33
fbc24ab0 34 if (!dvbhost) {
35 dvbhost = parse(&(url[6]), "2000");
36 dvbhost->socktype = SOCK_DGRAM;
37 }
d5eca7f2 38
fbc24ab0 39 if (resolve(dvbhost, &server) < 0) {
40 return -1;
41 }
42
43 bzero(&mreq, sizeof(mreq));
44 mreq.imr_multiaddr = server.sin_addr;
45 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
46
47 if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
48 perror("socket");
49 return -1;
50 }
51
d1199471 52 val = 1;
53 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
54 perror("setsockopt");
55 return -1;
56 }
57
fbc24ab0 58 if (bind(fd, (struct sockaddr*)&server, sizeof(server)) < 0) {
59 perror("bind");
60 return -1;
61 }
62
63 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
64 perror("setsockopt");
65 return -1;
66 }
d5eca7f2 67
68 return fd;
5c2e3e44 69}
Impressum, Datenschutz