]>
Commit | Line | Data |
---|---|---|
1 | #include <strings.h> | |
2 | #include <string.h> | |
3 | #include <stdio.h> | |
4 | #include <stdlib.h> | |
5 | #include <sys/types.h> | |
6 | #include <sys/socket.h> | |
7 | #include <netdb.h> | |
8 | ||
9 | #include "common.h" | |
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 | { | |
25 | static struct dvb_host *dvbhost = NULL; | |
26 | struct sockaddr_in server; | |
27 | struct ip_mreq mreq; | |
28 | int val; | |
29 | int fd; | |
30 | ||
31 | if(!is_mcast(url)) | |
32 | return -1; | |
33 | ||
34 | if (!dvbhost) { | |
35 | dvbhost = parse(&(url[6]), "2000"); | |
36 | dvbhost->socktype = SOCK_DGRAM; | |
37 | } | |
38 | ||
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 | ||
52 | val = 1; | |
53 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) { | |
54 | perror("setsockopt"); | |
55 | return -1; | |
56 | } | |
57 | ||
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 | } | |
67 | ||
68 | return fd; | |
69 | } |