summaryrefslogtreecommitdiff
path: root/toxrtp/toxrtp_helper.c
blob: 6f952359f7470cfea42aa1d685a2220b37a799f9 (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
/* rtp_helper.c
*
* Has some standard functions. !Red!
*
*
* Copyright (C) 2013 Tox project All Rights Reserved.
*
* This file is part of Tox.
*
* Tox 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 3 of the License, or
* (at your option) any later version.
*
* Tox 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 Tox. If not, see <http://www.gnu.org/licenses/>.
*
*/



#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include "toxrtp_helper.h"
#include "../toxcore/network.h"

#include <assert.h>

#ifdef WIN
#include <windows.h>
#endif /* WIN */


static int _seed = 0; /* Not initiated */

int t_setipport ( const char *_ip, unsigned short _port, void *_dest )
{
    assert(_dest);

    IP_Port *_dest_c = ( IP_Port * ) _dest;
    ip_init(&_dest_c->ip, 0);

    IP_Port _ipv6_garbage;

    if ( !addr_resolve(_ip, &_dest_c->ip, &_ipv6_garbage.ip) )
        return FAILURE;

    _dest_c->port = htons ( _port );

    return SUCCESS;
}

uint32_t t_random ( uint32_t _max )
{
    if ( !_seed ) {
        srand ( t_time() );
        _seed++;
    }

    if ( _max <= 0 ) {
        return ( unsigned ) rand();
    } else {
        return ( unsigned ) rand() % _max;
    }
}

void t_memcpy ( uint8_t *_dest, const uint8_t *_source, size_t _size )
{
    /*
     * Using countdown to zero method
     * It's faster than for(_it = 0; _it < _size; _it++);
     */
    size_t _it = _size;

    do {
        _it--;
        _dest[_it] = _source[_it];
    } while ( _it );

}

uint8_t *t_memset ( uint8_t *_dest, uint8_t _valu, size_t _size )
{
    /*
     * Again using countdown to zero method
     */
    size_t _it = _size;

    do {
        _it--;
        _dest[_it] = _valu;
    } while ( _it );

    return _dest;
}

size_t t_memlen ( const uint8_t *_valu)
{
    const uint8_t *_it;
    size_t _retu = 0;

    for ( _it = _valu; *_it; ++_it ) ++_retu;

    return _retu;
}

uint8_t *t_strallcpy ( const uint8_t *_source ) /* string alloc and copy */
{
    assert(_source);

    size_t _length = t_memlen(_source) + 1; /* make space for null character */

    uint8_t *_dest = calloc( sizeof ( uint8_t ), _length );
    assert(_dest);

    t_memcpy(_dest, _source, _length);

    return _dest;
}

size_t t_strfind ( const uint8_t *_str, const uint8_t *_substr )
{
    size_t _pos = 0;
    size_t _it, _delit = 0;

    for ( _it = 0; _str[_it] != '\0'; _it++ ) {
        if ( _str[_it] == _substr[_delit] ) {
            _pos = _it;

            while ( _str[_it] == _substr[_delit] && _str[_it] != '\0' ) {
                _it ++;
                _delit++;

                if ( _substr[_delit] == '\0' ) {
                    return _pos;
                }
            }

            _delit = 0;
            _pos = 0;
        }
    }

    return _pos;
}

#ifdef WIN
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
#define DELTA_EPOCH_IN_MICROSECS  11644473600000000Ui64
#else
#define DELTA_EPOCH_IN_MICROSECS  11644473600000000ULL
#endif

struct timezone {
    int  tz_minuteswest; /* minutes W of Greenwich */
    int  tz_dsttime;     /* type of dst correction */
};

int gettimeofday(struct timeval *tv, struct timezone *tz)
{
    FILETIME ft;
    unsigned __int64 tmpres = 0;
    static int tzflag;

    if (NULL != tv) {
        GetSystemTimeAsFileTime(&ft);

        tmpres |= ft.dwHighDateTime;
        tmpres <<= 32;
        tmpres |= ft.dwLowDateTime;

        /*converting file time to unix epoch*/
        tmpres -= DELTA_EPOCH_IN_MICROSECS;
        tmpres /= 10;  /*convert into microseconds*/
        tv->tv_sec = (long)(tmpres / 1000000UL);
        tv->tv_usec = (long)(tmpres % 1000000UL);
    }

    if (NULL != tz) {
        if (!tzflag) {
            _tzset();
            tzflag++;
        }

        tz->tz_minuteswest = _timezone / 60;
        tz->tz_dsttime = _daylight;
    }

    return 0;
}
#endif /* WIN */


uint64_t t_time()
{
    struct timeval _tv;
    gettimeofday(&_tv, NULL);
    uint64_t _retu_usec = _tv.tv_sec % 1000000; /* get 6 digits an leave space for 3 more */
    _retu_usec = _retu_usec * 1000 + (_tv.tv_usec / 1000 );
    return _retu_usec;
}