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 | |
12 | int 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 | |
23 | int open_mcast(char *url) |
24 | { |
fbc24ab0 |
25 | static struct dvb_host *dvbhost = NULL; |
26 | struct sockaddr_in server; |
27 | struct ip_mreq mreq; |
d5eca7f2 |
28 | int fd; |
29 | |
6bcf6c4c |
30 | if(!is_mcast(url)) |
31 | return -1; |
5c2e3e44 |
32 | |
fbc24ab0 |
33 | if (!dvbhost) { |
34 | dvbhost = parse(&(url[6]), "2000"); |
35 | dvbhost->socktype = SOCK_DGRAM; |
36 | } |
d5eca7f2 |
37 | |
fbc24ab0 |
38 | if (resolve(dvbhost, &server) < 0) { |
39 | return -1; |
40 | } |
41 | |
42 | bzero(&mreq, sizeof(mreq)); |
43 | mreq.imr_multiaddr = server.sin_addr; |
44 | mreq.imr_interface.s_addr = htonl(INADDR_ANY); |
45 | |
46 | if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { |
47 | perror("socket"); |
48 | return -1; |
49 | } |
50 | |
51 | if (bind(fd, (struct sockaddr*)&server, sizeof(server)) < 0) { |
52 | perror("bind"); |
53 | return -1; |
54 | } |
55 | |
56 | if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) { |
57 | perror("setsockopt"); |
58 | return -1; |
59 | } |
d5eca7f2 |
60 | |
61 | return fd; |
5c2e3e44 |
62 | } |