util.c 28.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*****************************************************************************
 * util.c: Utils for the multicat suite
 *****************************************************************************
 * Copyright (C) 2004, 2009 VideoLAN
 * $Id: util.c 27 2009-10-20 19:15:04Z massiot $
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
28
#include <inttypes.h>
29 30 31 32 33 34 35 36 37 38 39
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <sys/time.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
40 41 42
#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
43
#include <sys/mman.h>
44 45 46 47 48 49 50 51

#include "util.h"

/*****************************************************************************
 * Local declarations
 *****************************************************************************/
#define MAX_MSG 1024
#define PSZ_AUX_EXT "aux"
52
#define PSZ_TS_EXT "ts"
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

int i_verbose = VERB_DBG;

/*****************************************************************************
 * msg_Info
 *****************************************************************************/
void msg_Info( void *_unused, const char *psz_format, ... )
{
    if ( i_verbose >= VERB_INFO )
    {
        va_list args;
        char psz_fmt[MAX_MSG];
        va_start( args, psz_format );

        snprintf( psz_fmt, MAX_MSG, "info: %s\n", psz_format );
        vfprintf( stderr, psz_fmt, args );
    }
}

/*****************************************************************************
 * msg_Err
 *****************************************************************************/
void msg_Err( void *_unused, const char *psz_format, ... )
{
    va_list args;
    char psz_fmt[MAX_MSG];
    va_start( args, psz_format );

    snprintf( psz_fmt, MAX_MSG, "error: %s\n", psz_format );
    vfprintf( stderr, psz_fmt, args );
}

/*****************************************************************************
 * msg_Warn
 *****************************************************************************/
void msg_Warn( void *_unused, const char *psz_format, ... )
{
    if ( i_verbose >= VERB_WARN )
    {
        va_list args;
        char psz_fmt[MAX_MSG];
        va_start( args, psz_format );

        snprintf( psz_fmt, MAX_MSG, "warning: %s\n", psz_format );
        vfprintf( stderr, psz_fmt, args );
    }
}

/*****************************************************************************
 * msg_Dbg
 *****************************************************************************/
void msg_Dbg( void *_unused, const char *psz_format, ... )
{
    if ( i_verbose >= VERB_DBG )
    {
        va_list args;
        char psz_fmt[MAX_MSG];
        va_start( args, psz_format );

        snprintf( psz_fmt, MAX_MSG, "debug: %s\n", psz_format );
        vfprintf( stderr, psz_fmt, args );
    }
}

/*****************************************************************************
 * msg_Raw
 *****************************************************************************/
void msg_Raw( void *_unused, const char *psz_format, ... )
{
    va_list args;
    char psz_fmt[MAX_MSG];
    va_start( args, psz_format );

    snprintf( psz_fmt, MAX_MSG, "%s\n", psz_format );
    vfprintf( stderr, psz_fmt, args );
}

/*****************************************************************************
131
 * wall_Date/real_Date: returns a 27 MHz timestamp
132
 *****************************************************************************/
133
static uint64_t _wall_Date( bool b_realtime )
134 135 136 137 138
{
#if defined (HAVE_CLOCK_NANOSLEEP)
    struct timespec ts;

    /* Try to use POSIX monotonic clock if available */
139
    if( b_realtime || clock_gettime( CLOCK_MONOTONIC, &ts ) == EINVAL )
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        /* Run-time fallback to real-time clock (always available) */
        (void)clock_gettime( CLOCK_REALTIME, &ts );

    return ((uint64_t)ts.tv_sec * (uint64_t)27000000)
            + (uint64_t)(ts.tv_nsec * 27 / 1000);
#else
    struct timeval tv_date;

    /* gettimeofday() could return an error, and should be tested. However, the
     * only possible error, according to 'man', is EFAULT, which can not happen
     * here, since tv is a local variable. */
    gettimeofday( &tv_date, NULL );
    return( (uint64_t) tv_date.tv_sec * 27000000 + (uint64_t) tv_date.tv_usec * 27 );
#endif
}

156 157 158 159 160 161 162 163 164 165
uint64_t wall_Date( void )
{
    return _wall_Date( false );
}

uint64_t real_Date( void )
{
    return _wall_Date( true );
}

166
/*****************************************************************************
167
 * wall_Sleep/real_Sleep
168
 *****************************************************************************/
169
static void _wall_Sleep( uint64_t i_delay, bool b_realtime )
170 171 172 173 174 175
{
    struct timespec ts;
    ts.tv_sec = i_delay / 27000000;
    ts.tv_nsec = (i_delay % 27000000) * 1000 / 27;

#if defined( HAVE_CLOCK_NANOSLEEP )
176 177 178 179
    int val = EINVAL;
    if ( !b_realtime )
        while ( ( val = clock_nanosleep( CLOCK_MONOTONIC, 0, &ts, &ts ) ) == EINTR );
    if( val == EINVAL )
180 181 182 183 184 185 186 187 188 189
    {
        ts.tv_sec = i_delay / 27000000;
        ts.tv_nsec = (i_delay % 27000000) * 1000 / 27;
        while ( clock_nanosleep( CLOCK_REALTIME, 0, &ts, &ts ) == EINTR );
    }
#else
    while ( nanosleep( &ts, &ts ) && errno == EINTR );
#endif
}

190 191 192 193 194 195 196 197 198 199
void wall_Sleep( uint64_t i_delay )
{
    _wall_Sleep( i_delay, false );
}

void real_Sleep( uint64_t i_delay )
{
    _wall_Sleep( i_delay, true );
}

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
/*****************************************************************************
 * GetInterfaceIndex: returns the index of an interface
 *****************************************************************************/
static int GetInterfaceIndex( const char *psz_name )
{
    int i_fd;
    struct ifreq ifr;

    if ( (i_fd = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 )
    {
        msg_Err( NULL, "unable to open socket (%s)", strerror(errno) );
        exit(EXIT_FAILURE);
    }

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

    if ( ioctl( i_fd, SIOCGIFINDEX, &ifr ) < 0 )
    {
        msg_Err( NULL, "unable to get interface index (%s)", strerror(errno) );
        exit(EXIT_FAILURE);
    }

    close( i_fd );

    return ifr.ifr_ifindex;
}

228 229 230 231 232 233 234 235 236 237 238 239
/*****************************************************************************
 * PrintSocket: print socket characteristics for debug purposes
 *****************************************************************************/
static void PrintSocket( const char *psz_text, struct sockaddr_in *p_bind,
                         struct sockaddr_in *p_connect )
{
    msg_Dbg( NULL, "%s bind:%s:%u", psz_text,
             inet_ntoa( p_bind->sin_addr ), ntohs( p_bind->sin_port ) );
    msg_Dbg( NULL, "%s connect:%s:%u", psz_text,
             inet_ntoa( p_connect->sin_addr ), ntohs( p_connect->sin_port ) );
}

240 241 242 243 244 245 246 247 248 249 250 251 252 253
/*****************************************************************************
 * PrintSocket6: print IPv6 socket characteristics for debug purposes
 *****************************************************************************/
static void PrintSocket6( const char *psz_text, struct sockaddr_in6 *p_bind,
                         struct sockaddr_in6 *p_connect )
{
    char buf[INET6_ADDRSTRLEN];

    msg_Dbg( NULL, "%s bind:[%s]:%u", psz_text,
             inet_ntop( AF_INET6, &p_bind->sin6_addr, buf, sizeof(buf) ), ntohs( p_bind->sin6_port ) );
    msg_Dbg( NULL, "%s connect:[%s]:%u", psz_text,
             inet_ntop( AF_INET6, &p_connect->sin6_addr, buf, sizeof(buf) ), ntohs( p_connect->sin6_port ) );
}

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
/*****************************************************************************
 * ParseHost: parse a host:port string
 *****************************************************************************/
static int ParseHost( struct sockaddr_in *p_sock, char *psz_host )
{
    char *psz_token = strrchr( psz_host, ':' );
    if ( psz_token )
    {
        char *psz_parser;
        *psz_token++ = '\0';
        p_sock->sin_port = htons( strtol( psz_token, &psz_parser, 0 ) );
        if ( *psz_parser ) return -1;
    }
    else
        p_sock->sin_port = htons( DEFAULT_PORT );

    if ( !*psz_host )
        p_sock->sin_addr.s_addr = INADDR_ANY;
    else if ( !inet_aton( psz_host, &p_sock->sin_addr ) )
        return -1;

    return 0;
}

/*****************************************************************************
279
 * ParseHost6: parse an IPv6 host:port string
280
 *****************************************************************************/
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
static int ParseHost6( struct sockaddr_in6 *p_sock, int *pi_ifindex, char *psz_host )
{
    char *psz_token = strrchr( psz_host, ':' );
    if ( psz_token && psz_token != psz_host && *(psz_token-1) == ']' )
    {
        char *psz_parser;
        *psz_token++ = '\0';
        p_sock->sin6_port = htons( strtol( psz_token, &psz_parser, 0 ) );
        if ( *psz_parser ) return -1;
    }
    else
        p_sock->sin6_port = htons( DEFAULT_PORT );

    size_t len = strlen(psz_host);
    if ( psz_host[0] == '[' && psz_host[len-1] == ']' )
    {
        psz_host[len-1] = '\0';
        psz_host++;
    }

    psz_token = strrchr( psz_host, '%' );
    if ( psz_token )
    {
        *psz_token++ = '\0';
        *pi_ifindex = GetInterfaceIndex( psz_token );
    }
    else
        *pi_ifindex = 0;

    if ( !*psz_host )
        p_sock->sin6_addr = in6addr_any;
    else if ( !inet_pton( AF_INET6, psz_host, &p_sock->sin6_addr ) )
        return -1;

    return 0;
}


/*****************************************************************************
 * OpenSocket4: parse argv and open IPv4 sockets
 *****************************************************************************/
int OpenSocket4( const char *_psz_arg, int i_ttl, unsigned int *pi_weight )
323 324 325 326 327
{
    char *psz_token;
    struct sockaddr_in bind_addr, connect_addr;
    int i_fd, i;
    char *psz_arg = strdup(_psz_arg);
328 329 330 331
    char *psz_option = NULL;
    int i_if_index = 0;
    in_addr_t i_if_addr = INADDR_ANY;
    int i_tos = 0;
332
    bool b_valid = false;
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347

    bind_addr.sin_family = connect_addr.sin_family = AF_INET;
    bind_addr.sin_addr.s_addr = connect_addr.sin_addr.s_addr = INADDR_ANY;
    bind_addr.sin_port = connect_addr.sin_port = 0;

    psz_token = strrchr( psz_arg, ',' );
    if ( psz_token )
    {
        *psz_token++ = '\0';
        if ( pi_weight )
            *pi_weight = strtoul( psz_token, NULL, 0 );
    }
    else if ( pi_weight )
        *pi_weight = 1;

348 349 350 351
    psz_option = strchr( psz_arg, '/' );
    if ( psz_option )
        *psz_option = '\0';

352 353 354 355 356 357 358 359 360
    psz_token = strrchr( psz_arg, '@' );
    if ( psz_token )
    {
        *psz_token++ = '\0';
        if ( ParseHost( &bind_addr, psz_token ) < 0 )
        {
            free(psz_arg);
            return -1;
        }
361
        b_valid = true;
362 363
    }

364 365 366 367 368 369 370 371 372 373
    if ( psz_arg[0] )
    {
        b_valid = true;
        if ( ParseHost( &connect_addr, psz_arg ) < 0 )
        {
            free(psz_arg);
            return -1;
        }
    }
    if ( !b_valid )
374 375 376 377
    {
        free(psz_arg);
        return -1;
    }
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

    if ( psz_option )
    {
        do
        {
            *psz_option++ = '\0';

#define IS_OPTION( option ) (!strncasecmp( psz_option, option, strlen(option) ))
#define ARG_OPTION( option ) (psz_option + strlen(option))

            if ( IS_OPTION("ifindex=") )
                i_if_index = strtol( ARG_OPTION("ifindex="), NULL, 0 );
            else if ( IS_OPTION("ifaddr=") )
                i_if_addr = inet_addr( ARG_OPTION("ifaddr=") );
            else if ( IS_OPTION("ttl=") )
                i_ttl = strtol( ARG_OPTION("ttl="), NULL, 0 );
            else if ( IS_OPTION("tos=") )
                i_tos = strtol( ARG_OPTION("tos="), NULL, 0 );
            else
                msg_Warn( NULL, "unrecognized option %s", psz_option );

#undef IS_OPTION
#undef ARG_OPTION
        }
        while ( (psz_option = strchr( psz_option, '/' )) != NULL );
    }

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
    free( psz_arg );

    if ( (i_fd = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 )
    {
        msg_Err( NULL, "unable to open socket (%s)", strerror(errno) );
        exit(EXIT_FAILURE);
    }

    i = 1;
    if ( setsockopt( i_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&i,
                     sizeof(i) ) == -1 )
    {
        msg_Err( NULL, "unable to set socket (%s)", strerror(errno) );
        exit(EXIT_FAILURE);
    }

    /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
     * packet loss caused by scheduling problems */
    i = 0x80000;
    setsockopt( i_fd, SOL_SOCKET, SO_RCVBUF, (void *) &i, sizeof( i ) );

    if ( bind( i_fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr) ) < 0 )
    {
        msg_Err( NULL, "couldn't bind" );
        PrintSocket( "socket definition:", &bind_addr, &connect_addr );
        exit(EXIT_FAILURE);
    }

    /* Join the multicast group if the socket is a multicast address */
    if ( IN_MULTICAST( ntohl(bind_addr.sin_addr.s_addr)) )
    {
436
        if ( connect_addr.sin_addr.s_addr )
437
        {
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
            /* Source-specific multicast */
            struct ip_mreq_source imr;
            imr.imr_multiaddr = bind_addr.sin_addr;
            imr.imr_interface.s_addr = i_if_addr;
            imr.imr_sourceaddr = connect_addr.sin_addr;
            if ( i_if_index )
                msg_Warn( NULL, "ignoring ifindex option in SSM" );

            if ( setsockopt( i_fd, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,
                        (char *)&imr, sizeof(struct ip_mreq_source) ) < 0 )
                msg_Warn( NULL, "couldn't join multicast group (%s)",
                          strerror(errno) );
                PrintSocket( "socket definition:", &bind_addr, &connect_addr );
                exit(EXIT_FAILURE);
        }
        else if ( i_if_index )
        {
            /* Linux-specific interface-bound multicast */
            struct ip_mreqn imr;
            imr.imr_multiaddr = bind_addr.sin_addr;
            imr.imr_address.s_addr = i_if_addr;
            imr.imr_ifindex = i_if_index;

            if ( setsockopt( i_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
                             (char *)&imr, sizeof(struct ip_mreqn) ) < 0 )
                msg_Warn( NULL, "couldn't join multicast group (%s)",
                          strerror(errno) );
                PrintSocket( "socket definition:", &bind_addr, &connect_addr );
                exit(EXIT_FAILURE);
        }
        else
        {
            /* Regular multicast */
            struct ip_mreq imr;
            imr.imr_multiaddr = bind_addr.sin_addr;
            imr.imr_interface.s_addr = i_if_addr;

            if ( setsockopt( i_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
                             (char *)&imr, sizeof(struct ip_mreq) ) == -1 )
            {
                msg_Err( NULL, "couldn't join multicast group (%s)",
                         strerror(errno) );
                PrintSocket( "socket definition:", &bind_addr, &connect_addr );
                exit(EXIT_FAILURE);
            }
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
        }
    }

    if ( connect_addr.sin_addr.s_addr )
    {
        if ( connect( i_fd, (struct sockaddr *)&connect_addr,
                      sizeof(connect_addr) ) < 0 )
        {
            msg_Err( NULL, "cannot connect socket (%s)",
                     strerror(errno) );
            PrintSocket( "socket definition:", &bind_addr, &connect_addr );
            exit(EXIT_FAILURE);
        }

        if ( IN_MULTICAST( ntohl(connect_addr.sin_addr.s_addr) ) && i_ttl )
        {
            if ( setsockopt( i_fd, IPPROTO_IP, IP_MULTICAST_TTL,
                             (void *)&i_ttl, sizeof(i_ttl) ) == -1 )
            {
502 503 504 505 506 507 508 509 510 511 512 513
                msg_Err( NULL, "couldn't set TTL (%s)", strerror(errno) );
                PrintSocket( "socket definition:", &bind_addr, &connect_addr );
                exit(EXIT_FAILURE);
            }
        }

        if ( i_tos )
        {
            if ( setsockopt( i_fd, IPPROTO_IP, IP_TOS,
                             (void *)&i_tos, sizeof(i_tos) ) == -1 )
            {
                msg_Err( NULL, "couldn't set TOS (%s)", strerror(errno) );
514 515 516 517 518 519 520 521 522
                PrintSocket( "socket definition:", &bind_addr, &connect_addr );
                exit(EXIT_FAILURE);
            }
        }
    }

    return i_fd;
}

523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
/*****************************************************************************
 * OpenSocket6: parse argv and open IPv6 sockets
 *****************************************************************************/
int OpenSocket6( const char *_psz_arg, int i_ttl, unsigned int *pi_weight )
{
    char *psz_token;
    struct sockaddr_in6 bind_addr, connect_addr;
    int i_bind_ifindex = 0, i_connect_ifindex = 0;
    int i_fd, i;
    char *psz_arg = strdup(_psz_arg);

    bind_addr.sin6_family = connect_addr.sin6_family = AF_INET6;
    bind_addr.sin6_addr = connect_addr.sin6_addr = in6addr_any;
    bind_addr.sin6_port = connect_addr.sin6_port = 0;
    bind_addr.sin6_flowinfo = connect_addr.sin6_flowinfo = 0;
    bind_addr.sin6_scope_id = connect_addr.sin6_scope_id = 0;

    psz_token = strrchr( psz_arg, ',' );
    if ( psz_token )
    {
        *psz_token++ = '\0';
        if ( pi_weight )
            *pi_weight = strtoul( psz_token, NULL, 0 );
    }
    else if ( pi_weight )
        *pi_weight = 1;

    psz_token = strrchr( psz_arg, '@' );
    if ( psz_token )
    {
        *psz_token++ = '\0';
        if ( ParseHost6( &bind_addr, &i_bind_ifindex, psz_token ) < 0 )
        {
            free(psz_arg);
            return -1;
        }
    }

    if ( psz_arg[0] && ParseHost6( &connect_addr, &i_connect_ifindex, psz_arg ) < 0 )
    {
        free(psz_arg);
        return -1;
    }
    free( psz_arg );

    if ( (i_fd = socket( AF_INET6, SOCK_DGRAM, 0 )) < 0 )
    {
        msg_Err( NULL, "unable to open socket (%s)", strerror(errno) );
        exit(EXIT_FAILURE);
    }

    i = 1;
    if ( setsockopt( i_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&i,
                     sizeof(i) ) == -1 )
    {
        msg_Err( NULL, "unable to set socket (%s)", strerror(errno) );
        exit(EXIT_FAILURE);
    }

    if ( i_bind_ifindex || i_connect_ifindex )
    {
        if ( i_bind_ifindex && i_connect_ifindex && i_bind_ifindex != i_connect_ifindex )
        {
            msg_Err( NULL, "conflicting interface indices" );
            exit(EXIT_FAILURE);
        }

        if ( !i_bind_ifindex )
            i_bind_ifindex = i_connect_ifindex;

        if ( setsockopt( i_fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
                         (void *)&i_bind_ifindex, sizeof(i_bind_ifindex) ) == -1 )
        {
            msg_Err( NULL, "couldn't set interface index" );
            PrintSocket6( "socket definition:", &bind_addr, &connect_addr );
            exit(EXIT_FAILURE);
        }
    }

    /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
     * packet loss caused by scheduling problems */
    i = 0x80000;
    setsockopt( i_fd, SOL_SOCKET, SO_RCVBUF, (void *) &i, sizeof( i ) );

    if ( IN6_IS_ADDR_MULTICAST( &bind_addr.sin6_addr ) )
    {
        struct ipv6_mreq imr;
        struct sockaddr_in6 bind_addr_any = bind_addr;
        bind_addr_any.sin6_addr = in6addr_any;

        if ( bind( i_fd, (struct sockaddr *)&bind_addr_any, sizeof(bind_addr_any) ) < 0 )
        {
            msg_Err( NULL, "couldn't bind" );
            PrintSocket6( "socket definition:", &bind_addr, &connect_addr );
            exit(EXIT_FAILURE);
        }

        imr.ipv6mr_multiaddr = bind_addr.sin6_addr;
        imr.ipv6mr_interface = i_bind_ifindex;

        /* Join Multicast group without source filter */
        if ( setsockopt( i_fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
                         (char *)&imr, sizeof(struct ipv6_mreq) ) == -1 )
        {
            msg_Err( NULL, "couldn't join multicast group" );
            PrintSocket6( "socket definition:", &bind_addr, &connect_addr );
            exit(EXIT_FAILURE);
        }
    }
    else
    {
        if ( bind( i_fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr) ) < 0 )
        {
            msg_Err( NULL, "couldn't bind" );
            PrintSocket6( "socket definition:", &bind_addr, &connect_addr );
            exit(EXIT_FAILURE);
        }
    }

    if ( !IN6_IS_ADDR_UNSPECIFIED(&connect_addr.sin6_addr) )
    {
        if ( connect( i_fd, (struct sockaddr *)&connect_addr,
                      sizeof(connect_addr) ) < 0 )
        {
            msg_Err( NULL, "cannot connect socket (%s)",
                     strerror(errno) );
            PrintSocket6( "socket definition:", &bind_addr, &connect_addr );
            exit(EXIT_FAILURE);
        }

        if ( IN6_IS_ADDR_MULTICAST( &connect_addr.sin6_addr) && i_ttl )
        {
            if ( setsockopt( i_fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
                             (void *)&i_ttl, sizeof(i_ttl) ) == -1 )
            {
                msg_Err( NULL, "couldn't set hop limit" );
                PrintSocket6( "socket definition:", &bind_addr, &connect_addr );
                exit(EXIT_FAILURE);
            }
        }
    }

    return i_fd;
}

/*****************************************************************************
 * OpenSocket: parse argv and open sockets
 *****************************************************************************/
int OpenSocket( const char *_psz_arg, int i_ttl, unsigned int *pi_weight )
{
    int i_fd = 0;

    i_fd = OpenSocket4( _psz_arg, i_ttl, pi_weight );

    if( i_fd < 0 )
        i_fd = OpenSocket6( _psz_arg, i_ttl, pi_weight );

    return i_fd;
}

683 684 685 686 687 688 689 690 691 692 693
/*****************************************************************************
 * StatFile: parse argv and return the mode of the named file
 *****************************************************************************/
mode_t StatFile( const char *psz_arg )
{
    struct stat sb;
    if ( stat( psz_arg, &sb ) < 0 )
        return 0;
    return sb.st_mode;
}

694 695 696
/*****************************************************************************
 * OpenFile: parse argv and open file descriptors
 *****************************************************************************/
697
int OpenFile( const char *psz_arg, bool b_read, bool b_append )
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
{
    struct stat sb;
    int i_fd;
    int i_mode = b_read ? O_RDONLY : O_WRONLY;

    if ( stat( psz_arg, &sb ) < 0 )
    {
        if ( b_read )
        {
            msg_Err( NULL, "file %s doesn't exist (%s)", psz_arg,
                     strerror(errno) );
            exit(EXIT_FAILURE);
        }
        i_mode |= O_CREAT;
    }
713
    else if ( !(S_ISCHR(sb.st_mode) || S_ISFIFO(sb.st_mode)) && !b_read )
714
    {
715 716 717 718
        if ( b_append )
            i_mode |= O_APPEND;
        else
            i_mode |= O_TRUNC;
719 720 721 722 723 724 725 726 727 728 729 730
    }

    if ( (i_fd = open( psz_arg, i_mode, 0644 )) < 0 )
    {
        msg_Err( NULL, "couldn't open file %s (%s)", psz_arg, strerror(errno) );
        exit(EXIT_FAILURE);
    }

    return i_fd;
}

/*****************************************************************************
731 732
 * GetAuxFile: generate a file name for the TS file
 * Remember to free the returned string
733
 *****************************************************************************/
734
char *GetAuxFile( const char *psz_arg, size_t i_payload_size )
735
{
736
    char *psz_aux = malloc( strlen(psz_arg) + 256 );
737 738 739
    char *psz_token;

    strcpy( psz_aux, psz_arg );
740 741 742 743 744 745

    psz_token = strrchr( psz_aux, '/' );
    if ( psz_token != NULL )
        psz_token++;
    else
        psz_token = psz_aux + 1;
746
    if ( *psz_token ) psz_token++; /* Skip first character of base name */
747 748

    /* Strip extension */
749
    psz_token = strrchr( psz_token, '.' );
750
    if ( psz_token ) *psz_token = '\0';
751 752

    /* Append extension */
753
    strcat( psz_aux, "." PSZ_AUX_EXT );
754 755 756 757 758
    if ( i_payload_size != DEFAULT_PAYLOAD_SIZE )
        sprintf( psz_aux + strlen(psz_aux), "%zu", i_payload_size );

    return psz_aux;
}
759

760 761 762 763 764 765 766 767 768
/*****************************************************************************
 * OpenAuxFile
 *****************************************************************************/
FILE *OpenAuxFile( const char *psz_arg, bool b_read, bool b_append )
{
    FILE *p_aux;

    if ( (p_aux = fopen( psz_arg,
                         b_read ? "rb" : (b_append ? "ab" : "wb") )) < 0 )
769
    {
770
        msg_Err( NULL, "couldn't open file %s (%s)", psz_arg,
771 772 773 774 775 776
                 strerror(errno) );
        exit(EXIT_FAILURE);
    }

    return p_aux;
}
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897


/*****************************************************************************
 * LookupAuxFile: find an STC in an auxiliary file
 *****************************************************************************/
off_t LookupAuxFile( const char *psz_arg, int64_t i_wanted, bool b_absolute )
{
    uint8_t *p_aux;
    off_t i_offset1 = 0, i_offset2;
    int i_stc_fd;
    struct stat stc_stat;

    if ( (i_stc_fd = open( psz_arg, O_RDONLY )) == -1 )
    {
        msg_Err( NULL, "unable to open %s (%s)", psz_arg, strerror(errno) );
        return -1;
    }

    if ( fstat( i_stc_fd, &stc_stat ) == -1
          || stc_stat.st_size < sizeof(uint64_t) )
    {
        msg_Err( NULL, "unable to stat %s (%s)", psz_arg, strerror(errno) );
        return -1;
    }

    p_aux = mmap( NULL, stc_stat.st_size, PROT_READ, MAP_SHARED,
                  i_stc_fd, 0 );
    if ( p_aux == MAP_FAILED )
    {
        msg_Err( NULL, "unable to mmap %s (%s)", psz_arg, strerror(errno) );
        return -1;
    }

    i_offset2 = stc_stat.st_size / sizeof(uint64_t);

    if ( i_wanted < 0 )
        i_wanted += FromSTC( p_aux + (i_offset2 - 1) * sizeof(uint64_t) );
    else if ( !b_absolute )
        i_wanted += FromSTC( p_aux );

    if ( i_wanted < 0 )
    {
        msg_Err( NULL, "invalid offset" );
        return -1;
    }

    for ( ; ; )
    {
        off_t i_mid_offset = (i_offset1 + i_offset2) / 2;
        uint8_t *p_mid_aux = p_aux + i_mid_offset * sizeof(uint64_t);
        uint64_t i_mid_stc = FromSTC( p_mid_aux );

        if ( i_offset1 == i_mid_offset )
            break;

        if ( i_mid_stc >= i_wanted )
            i_offset2 = i_mid_offset;
        else
            i_offset1 = i_mid_offset;
    }

    munmap( p_aux, stc_stat.st_size );
    close( i_stc_fd );

    return i_offset2;
}

/*****************************************************************************
 * GetDirFile: return the prefix of the file according to the STC
 *****************************************************************************/
uint64_t GetDirFile( uint64_t i_rotate_size, int64_t i_wanted )
{
    if ( i_wanted <= 0 )
        i_wanted += real_Date();
    if ( i_wanted <= 0 )
        return 0;

    return i_wanted / i_rotate_size;
}

/*****************************************************************************
 * OpenDirFile: return fd + aux file pointer
 *****************************************************************************/
int OpenDirFile( const char *psz_dir_path, uint64_t i_file, bool b_read,
                 size_t i_payload_size, FILE **pp_aux_file )
{
    int i_fd;
    char psz_file[strlen(psz_dir_path) + sizeof(PSZ_TS_EXT) +
                  sizeof(".18446744073709551615")];
    sprintf( psz_file, "%s/%"PRIu64"."PSZ_TS_EXT, psz_dir_path, i_file );

    i_fd = OpenFile( psz_file, b_read, !b_read );
    if ( i_fd < 0 ) return -1;

    char *psz_aux_file = GetAuxFile( psz_file, i_payload_size );
    *pp_aux_file = OpenAuxFile( psz_aux_file, b_read, !b_read );
    free( psz_aux_file );
    if ( *pp_aux_file == NULL )
    {
        close( i_fd );
        return -1;
    }
    return i_fd;
}

/*****************************************************************************
 * LookupDirAuxFile: find an STC in an auxiliary file of a directory
 *****************************************************************************/
off_t LookupDirAuxFile( const char *psz_dir_path, uint64_t i_file,
                        int64_t i_wanted, size_t i_payload_size )
{
    off_t i_ret;
    char psz_file[strlen(psz_dir_path) + sizeof(PSZ_TS_EXT) +
                  sizeof(".18446744073709551615")];
    sprintf( psz_file, "%s/%"PRIu64"."PSZ_TS_EXT, psz_dir_path, i_file );

    char *psz_aux_file = GetAuxFile( psz_file, i_payload_size );
    i_ret = LookupAuxFile( psz_aux_file, i_wanted, true );
    free( psz_aux_file );
    return i_ret;
}