broadcast.cpp 7.51 KB
Newer Older
1 2 3
/*****************************************************************************
 * broadcast.cpp : SAP Broadcast class
 ****************************************************************************
4
 * Copyright (C) 1998-2006 VideoLAN
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
5
 * $Id$
6 7
 *
 * Authors: Damien Lucas <nitrox@videolan.org>
8
 *          Rémi Denis-Courmont <rem # videolan.org>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

26 27 28 29
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

30 31
#include <sys/types.h>
#include <sys/socket.h>
32 33 34
#ifdef HAVE_SYS_SOCKIO_H
# include <sys/sockio.h>    /* Needed on Solaris for SIOCGIFADDR */
#endif
35
#include <net/if.h>
36 37
#include <netinet/in.h>
#include <arpa/inet.h>
38 39 40
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
41
#include <sys/ioctl.h>
42 43 44
#include <unistd.h>
using namespace std;

45 46
#include <vector>
#include <string>
47 48 49 50
#include "program.h"
#include "message.h"
#include "broadcast.h"

51 52
Broadcast::Broadcast(int i_ttl, const char *psz_iface) : fd4 (-1), fd6 (-1),
                                                         scope_id (0)
53
{
54 55 56
    /* Initializes IPv6 socket */
    if (psz_iface != NULL)
        scope_id = if_nametoindex (psz_iface);
57

58
    fd6 = socket (AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
59
    if (fd6 != -1)
60
    {
61 62
        struct sockaddr_in6 addr;
        memset (&addr, 0, sizeof (addr));
63
        addr.sin6_family = AF_INET6;
64 65 66
#ifdef HAVE_SA_LEN
        addr.sin6_len = sizeof (addr);
#endif
67

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        if (psz_iface != NULL)
        {
            if (scope_id != 0)
            {
                if (setsockopt (fd6, IPPROTO_IPV6, IPV6_MULTICAST_IF,
                                &scope_id, sizeof (scope_id)) == 0)
                    scope_id = 0;
                else
                    perror("setsockopt(IPV6_MULTICAST_IF)");
            }
            else
                perror (psz_iface);

            if (scope_id == 0)
            {
                close (fd6);
                fd6 = -1;
            }
        }

88 89 90
        if (i_ttl != 0)
            setsockopt (fd6, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
                        &i_ttl, sizeof(i_ttl));
91

92 93 94
        if (bind (fd6, (struct sockaddr *)&addr, sizeof (addr)) == 0)
            shutdown (fd6, SHUT_RD);
        else
95
        {
96 97
            close (fd6);
            fd6 = -1;
98 99 100
        }
    }

101
    /* Initializes IPv4 socket */
102
    fd4 = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
103
    if (fd4 != -1)
104
    {
105 106
        struct sockaddr_in addr;
        memset (&addr, 0, sizeof (addr));
107
        addr.sin_family = AF_INET;
108 109 110
#ifdef HAVE_SA_LEN
        addr.sin_len = sizeof (addr);
#endif
111 112

        if (psz_iface != NULL)
113
        {
114 115 116 117 118 119
            struct ifreq req;

            strncpy (req.ifr_name, psz_iface, IFNAMSIZ - 1);
            req.ifr_name[IFNAMSIZ - 1] = '\0';

            if (ioctl (fd4, SIOCGIFADDR, &req) < 0)
120 121 122 123 124 125 126 127
            {
                perror("ioctl(SIOCGIFADDR)");
                close (fd4);
                fd4 = -1;
            }
            else
            {
                struct in_addr *ip;
128
                ip = &((struct sockaddr_in *)(&req.ifr_addr))->sin_addr;
129 130 131
                if (setsockopt(fd4, IPPROTO_IP, IP_MULTICAST_IF,
                               (char *)ip, sizeof (*ip)))
                {
132
                    perror("setsockopt(IP_MULTICAST_IF)");
133 134 135 136
                    close (fd4);
                    fd4 = -1;
                }
            }
137
        }
138

139
        if (i_ttl != 0)
140
            setsockopt (fd4, IPPROTO_IP, IP_MULTICAST_TTL,
141 142 143 144 145
                        &i_ttl, sizeof(i_ttl));

        if (bind (fd4, (struct sockaddr *)&addr, sizeof (addr)) == 0)
            shutdown (fd4, SHUT_RD);
        else
146
        {
147 148
            close (fd4);
            fd4 = -1;
149
        }
150
    }
151
}
152

153

154 155 156 157 158 159
Broadcast::~Broadcast()
{
    if (fd4 != -1)
        close (fd4);
    if (fd6 != -1)
        close (fd6);
160 161
}

162

163
int Broadcast::Send(Message* m, const struct sockaddr *dst, socklen_t len)
164 165
{
    /* Get the message and the length */
166
    const uint8_t* message = m->GetFinalMessage();
167
    size_t length = m->GetFinalMessageLen();
168

169 170 171 172 173
    if(message==NULL)
    {
        fprintf(stderr, "Bad message, skipping\n");
        return (-1);
    }
174

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    int fd;
    switch (dst->sa_family)
    {
        case AF_INET6:
            fd = fd6;
            break;

        case AF_INET:
            fd = fd4;
            break;

        default:
            fd = -1;
    }

    if (fd == -1)
        return -1;

    if (sendto (fd, message, length, 0, dst, len) < 0)
    {
        perror ("sendto");
        return -1;
    }

    return 0;
}


int Broadcast::GuessDestination (const char *str,
                                 struct sockaddr_storage *addr,
                                 socklen_t *addrlen) const
{
    union
    {
        struct in6_addr in6;
        struct in_addr in;
    } n;

    if (inet_pton (AF_INET6, str, &n.in6) <= 0)
    {
        if (inet_pton (AF_INET, str, &n.in) <= 0)
        {
            fprintf (stderr, "%s: invalid IP address\n", str);
            return -1;
        }
220

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
        /* str is an IPv4 address */
        in_addr_t ip = ntohl (n.in.s_addr);

        // 224.0.0.0/24 => 224.0.0.255
        if ((ip & 0xffffff00) == 0xe0000000)
            ip =  0xe00000ff;
        else
        // 239.255.0.0/16 => 239.255.255.255
        if ((ip & 0xffff0000) == 0xefff0000)
            ip =  0xefffffff;
        else
        // 239.192.0.0/14 => 239.195.255.255
        if ((ip & 0xfffc0000) == 0xefc00000)
            ip =  0xefc3ffff;
        else
        // other multicast address => 224.2.127.254
        if ((ip & 0xf0000000) == 0xe0000000)
            ip = 0xe0027ffe;
        else
        {
            fprintf (stderr, "%s: not a multicast IPv4 address\n", str);
            return -1;
        }

245
        ip = htonl (ip);
246 247 248
        struct sockaddr_in *a4 = (struct sockaddr_in *)addr;
        memset (a4, 0, sizeof (*a4));
        a4->sin_family = AF_INET;
249 250 251
#ifdef HAVE_SA_LEN
        a4->sin_len = sizeof (*a4);
#endif
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
        a4->sin_port = htons (HELLO_PORT);
        memcpy (&a4->sin_addr.s_addr, &ip, sizeof (ip));
        *addrlen = sizeof (*a4);
        return 0;
    }

    /* str is an IPv6 address */
    if (n.in6.s6_addr[0] == 0xff)
    {
        /* multicast address */
        // ff0x::2:7ffe with x = scope
        n.in6.s6_addr[1] = n.in6.s6_addr[1] & 0xf;

        // (ff0x):0000:0000:0000:0000:0000:0002:7ffe
        memcpy (n.in6.s6_addr + 2,
                "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x7f\xfe",
                14);
    }
    else
271
    {
272
        fprintf (stderr, "%s: not a multicast IPv6 address\n", str);
273
        return -1;
274
    }
275

276 277 278
    struct sockaddr_in6 *a6 = (struct sockaddr_in6 *)addr;
    memset (a6, 0, sizeof (*a6));
    a6->sin6_family = AF_INET6;
279 280 281
#ifdef HAVE_SA_LEN
    a6->sin_len = sizeof (*a6);
#endif
282 283 284 285
    a6->sin6_scope_id = scope_id;
    a6->sin6_port = htons (HELLO_PORT);
    memcpy (&a6->sin6_addr, &n.in6, sizeof (n.in6));
    *addrlen = sizeof (*a6);
286 287
    return 0;
}