broadcast.cpp 7.36 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 26 27
 *
 *
 * 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.
 *****************************************************************************/

#include <sys/types.h>
#include <sys/socket.h>
28
#include <net/if.h>
29 30
#include <netinet/in.h>
#include <arpa/inet.h>
31 32 33
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
34
#include <sys/ioctl.h>
35 36 37
#include <unistd.h>
using namespace std;

38 39
#include <vector>
#include <string>
40 41 42 43
#include "program.h"
#include "message.h"
#include "broadcast.h"

44 45
Broadcast::Broadcast(int i_ttl, const char *psz_iface) : fd4 (-1), fd6 (-1),
                                                         scope_id (0)
46
{
47 48 49
    /* Initializes IPv6 socket */
    if (psz_iface != NULL)
        scope_id = if_nametoindex (psz_iface);
50

51
    fd6 = socket (AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
52
    if (fd6 != -1)
53
    {
54 55
        struct sockaddr_in6 addr;
        memset (&addr, 0, sizeof (addr));
56
        addr.sin6_family = AF_INET6;
57 58 59
#ifdef HAVE_SA_LEN
        addr.sin6_len = sizeof (addr);
#endif
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        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;
            }
        }

81 82 83
        if (i_ttl != 0)
            setsockopt (fd6, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
                        &i_ttl, sizeof(i_ttl));
84

85 86 87
        if (bind (fd6, (struct sockaddr *)&addr, sizeof (addr)) == 0)
            shutdown (fd6, SHUT_RD);
        else
88
        {
89 90
            close (fd6);
            fd6 = -1;
91 92 93
        }
    }

94
    /* Initializes IPv4 socket */
95
    fd4 = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
96
    if (fd4 != -1)
97
    {
98 99
        struct sockaddr_in addr;
        memset (&addr, 0, sizeof (addr));
100
        addr.sin_family = AF_INET;
101 102 103
#ifdef HAVE_SA_LEN
        addr.sin_len = sizeof (addr);
#endif
104 105

        if (psz_iface != NULL)
106
        {
107 108 109 110 111 112
            struct ifreq req;

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

            if (ioctl (fd4, SIOCGIFADDR, &req) < 0)
113 114 115 116 117 118 119 120
            {
                perror("ioctl(SIOCGIFADDR)");
                close (fd4);
                fd4 = -1;
            }
            else
            {
                struct in_addr *ip;
121
                ip = &((struct sockaddr_in *)(&req.ifr_addr))->sin_addr;
122 123 124
                if (setsockopt(fd4, IPPROTO_IP, IP_MULTICAST_IF,
                               (char *)ip, sizeof (*ip)))
                {
125
                    perror("setsockopt(IP_MULTICAST_IF)");
126 127 128 129
                    close (fd4);
                    fd4 = -1;
                }
            }
130
        }
131

132
        if (i_ttl != 0)
133
            setsockopt (fd4, IPPROTO_IP, IP_MULTICAST_TTL,
134 135 136 137 138
                        &i_ttl, sizeof(i_ttl));

        if (bind (fd4, (struct sockaddr *)&addr, sizeof (addr)) == 0)
            shutdown (fd4, SHUT_RD);
        else
139
        {
140 141
            close (fd4);
            fd4 = -1;
142
        }
143
    }
144
}
145

146

147 148 149 150 151 152
Broadcast::~Broadcast()
{
    if (fd4 != -1)
        close (fd4);
    if (fd6 != -1)
        close (fd6);
153 154
}

155

156
int Broadcast::Send(Message* m, const struct sockaddr *dst, socklen_t len)
157 158
{
    /* Get the message and the length */
159
    const uint8_t* message = m->GetFinalMessage();
160
    size_t length = m->GetFinalMessageLen();
161

162 163 164 165 166
    if(message==NULL)
    {
        fprintf(stderr, "Bad message, skipping\n");
        return (-1);
    }
167

168 169 170 171 172 173 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
    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;
        }
213

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
        /* 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;
        }

238
        ip = htonl (ip);
239 240 241
        struct sockaddr_in *a4 = (struct sockaddr_in *)addr;
        memset (a4, 0, sizeof (*a4));
        a4->sin_family = AF_INET;
242 243 244
#ifdef HAVE_SA_LEN
        a4->sin_len = sizeof (*a4);
#endif
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
        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
264
    {
265
        fprintf (stderr, "%s: not a multicast IPv6 address\n", str);
266
        return -1;
267
    }
268

269 270 271
    struct sockaddr_in6 *a6 = (struct sockaddr_in6 *)addr;
    memset (a6, 0, sizeof (*a6));
    a6->sin6_family = AF_INET6;
272 273 274
#ifdef HAVE_SA_LEN
    a6->sin_len = sizeof (*a6);
#endif
275 276 277 278
    a6->sin6_scope_id = scope_id;
    a6->sin6_port = htons (HELLO_PORT);
    memcpy (&a6->sin6_addr, &n.in6, sizeof (n.in6));
    *addrlen = sizeof (*a6);
279 280
    return 0;
}