util.h 5.33 KB
Newer Older
Christophe Massiot's avatar
Christophe Massiot committed
1 2 3
/*****************************************************************************
 * util.h: Utils for the multicat suite
 *****************************************************************************
4
 * Copyright (C) 2009, 2011 VideoLAN
5
 * $Id$
Christophe Massiot's avatar
Christophe Massiot committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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.
 *****************************************************************************/

24 25 26
#include <netinet/udp.h>
#include <netinet/ip.h>

Christophe Massiot's avatar
Christophe Massiot committed
27 28 29 30
#define HAVE_CLOCK_NANOSLEEP

#define DEFAULT_PORT 1234
#define DEFAULT_PAYLOAD_SIZE 1316
31
#define DEFAULT_ROTATE_SIZE UINT64_C(97200000000)
Christophe Massiot's avatar
Christophe Massiot committed
32 33 34 35 36 37 38
#define TS_SIZE 188
#define RTP_HEADER_SIZE 12

#define VERB_DBG  3
#define VERB_INFO 2
#define VERB_WARN 1

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/*****************************************************************************
 * Raw udp packet structure with flexible-array payload
 *****************************************************************************/
struct udprawpkt {
    struct  iphdr iph;
    struct  udphdr udph;
    uint8_t payload[];
} __attribute__((packed));


/*****************************************************************************
 * OpenSocket options
 *****************************************************************************/
 struct opensocket_opt {
    struct udprawpkt *p_raw_pktheader;
 };


Christophe Massiot's avatar
Christophe Massiot committed
57 58 59 60 61 62 63 64 65 66
/*****************************************************************************
 * Prototypes
 *****************************************************************************/
void msg_Info( void *_unused, const char *psz_format, ... );
void msg_Err( void *_unused, const char *psz_format, ... );
void msg_Warn( void *_unused, const char *psz_format, ... );
void msg_Dbg( void *_unused, const char *psz_format, ... );
void msg_Raw( void *_unused, const char *psz_format, ... );
uint64_t wall_Date( void );
void wall_Sleep( uint64_t i_delay );
67 68
uint64_t real_Date( void );
void real_Sleep( uint64_t i_delay );
69
int OpenSocket( const char *_psz_arg, int i_ttl, uint16_t i_bind_port,
70 71
                uint16_t i_connect_port, unsigned int *pi_weight, bool *pb_tcp,
                struct opensocket_opt *p_opt);
72 73 74
mode_t StatFile(const char *psz_arg);
int OpenFile( const char *psz_arg, bool b_read, bool b_append );
char *GetAuxFile( const char *psz_arg, size_t i_payload_size );
Christophe Massiot's avatar
Christophe Massiot committed
75
FILE *OpenAuxFile( const char *psz_arg, bool b_read, bool b_append );
76
off_t LookupAuxFile( const char *psz_arg, int64_t i_wanted, bool b_absolute );
77 78
void CheckFileSizes( const char *psz_file, const char *psz_aux_file,
                     size_t i_payload_size );
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
uint64_t GetDirFile( uint64_t i_rotate_size, int64_t i_wanted );
int OpenDirFile( const char *psz_dir_path, uint64_t i_file, bool b_read,
                 size_t i_payload_size, FILE **pp_aux_file );
off_t LookupDirAuxFile( const char *psz_dir_path, uint64_t i_file,
                        int64_t i_wanted, size_t i_payload_size );

/*****************************************************************************
 * Aux files helpers
 *****************************************************************************/
static inline uint64_t FromSTC( const uint8_t *p_aux )
{
    return ((uint64_t)p_aux[0] << 56)
         | ((uint64_t)p_aux[1] << 48)
         | ((uint64_t)p_aux[2] << 40)
         | ((uint64_t)p_aux[3] << 32)
         | ((uint64_t)p_aux[4] << 24)
         | ((uint64_t)p_aux[5] << 16)
         | ((uint64_t)p_aux[6] << 8)
         | ((uint64_t)p_aux[7] << 0);
}

static inline void ToSTC( uint8_t *p_aux, uint64_t i_stc )
{
    p_aux[0] = i_stc >> 56;
    p_aux[1] = (i_stc >> 48) & 0xff;
    p_aux[2] = (i_stc >> 40) & 0xff;
    p_aux[3] = (i_stc >> 32) & 0xff;
    p_aux[4] = (i_stc >> 24) & 0xff;
    p_aux[5] = (i_stc >> 16) & 0xff;
    p_aux[6] = (i_stc >> 8) & 0xff;
    p_aux[7] = (i_stc >> 0) & 0xff;
}
Christophe Massiot's avatar
Christophe Massiot committed
111 112

/*****************************************************************************
113
 * Retx helpers - biTStream style
Christophe Massiot's avatar
Christophe Massiot committed
114
 *****************************************************************************/
115
#define RETX_HEADER_SIZE 8
Christophe Massiot's avatar
Christophe Massiot committed
116

117
static inline void retx_init(uint8_t *p_retx)
Christophe Massiot's avatar
Christophe Massiot committed
118
{
119 120 121 122
    p_retx[0] = 'R';
    p_retx[1] = 'E';
    p_retx[2] = 'T';
    p_retx[3] = 'X';
Christophe Massiot's avatar
Christophe Massiot committed
123 124
}

125
static inline bool retx_check(const uint8_t *p_retx)
Christophe Massiot's avatar
Christophe Massiot committed
126
{
127 128
    return p_retx[0] == 'R' && p_retx[1] == 'E' && p_retx[2] == 'T' &&
           p_retx[3] == 'X';
Christophe Massiot's avatar
Christophe Massiot committed
129 130
}

131
static inline void retx_set_seqnum(uint8_t *p_retx, uint16_t i_seqnum)
Christophe Massiot's avatar
Christophe Massiot committed
132
{
133 134
    p_retx[4] = i_seqnum >> 8;
    p_retx[5] = i_seqnum & 0xff;
Christophe Massiot's avatar
Christophe Massiot committed
135 136
}

137
static inline uint16_t retx_get_seqnum(const uint8_t *p_retx)
Christophe Massiot's avatar
Christophe Massiot committed
138
{
139
    return ((uint16_t)p_retx[4] << 8) | p_retx[5];
Christophe Massiot's avatar
Christophe Massiot committed
140 141
}

142
static inline void retx_set_num(uint8_t *p_retx, uint16_t i_num)
Christophe Massiot's avatar
Christophe Massiot committed
143
{
144 145
    p_retx[6] = i_num >> 8;
    p_retx[7] = i_num & 0xff;
Christophe Massiot's avatar
Christophe Massiot committed
146 147
}

148
static inline uint16_t retx_get_num(const uint8_t *p_retx)
Christophe Massiot's avatar
Christophe Massiot committed
149
{
150
    return ((uint16_t)p_retx[6] << 8) | p_retx[7];
Christophe Massiot's avatar
Christophe Massiot committed
151
}