| 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 fd; |
| 29 | |
| 30 | if(!is_mcast(url)) |
| 31 | return -1; |
| 32 | |
| 33 | if (!dvbhost) { |
| 34 | dvbhost = parse(&(url[6]), "2000"); |
| 35 | dvbhost->socktype = SOCK_DGRAM; |
| 36 | } |
| 37 | |
| 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 | } |
| 60 | |
| 61 | return fd; |
| 62 | } |