2013-10-13 22:16:47 +08:00
|
|
|
/* 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 */
|
|
|
|
|
2013-10-17 20:09:06 +08:00
|
|
|
int t_setipport ( const char *_ip, unsigned short _port, void *_dest )
|
2013-10-13 22:16:47 +08:00
|
|
|
{
|
|
|
|
assert(_dest);
|
|
|
|
|
2013-10-17 20:09:06 +08:00
|
|
|
IP_Port *_dest_c = ( IP_Port * ) _dest;
|
2013-10-13 22:16:47 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-17 20:09:06 +08:00
|
|
|
void t_memcpy ( uint8_t *_dest, const uint8_t *_source, size_t _size )
|
2013-10-13 22:16:47 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-10-17 20:09:06 +08:00
|
|
|
uint8_t *t_memset ( uint8_t *_dest, uint8_t _valu, size_t _size )
|
2013-10-13 22:16:47 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Again using countdown to zero method
|
|
|
|
*/
|
|
|
|
size_t _it = _size;
|
|
|
|
|
|
|
|
do {
|
|
|
|
_it--;
|
|
|
|
_dest[_it] = _valu;
|
|
|
|
} while ( _it );
|
|
|
|
|
|
|
|
return _dest;
|
|
|
|
}
|
|
|
|
|
2013-10-17 20:09:06 +08:00
|
|
|
size_t t_memlen ( const uint8_t *_valu)
|
2013-10-13 22:16:47 +08:00
|
|
|
{
|
2013-10-17 20:09:06 +08:00
|
|
|
const uint8_t *_it;
|
2013-10-13 22:16:47 +08:00
|
|
|
size_t _retu = 0;
|
|
|
|
|
|
|
|
for ( _it = _valu; *_it; ++_it ) ++_retu;
|
|
|
|
|
|
|
|
return _retu;
|
|
|
|
}
|
|
|
|
|
2013-10-17 20:09:06 +08:00
|
|
|
uint8_t *t_strallcpy ( const uint8_t *_source ) /* string alloc and copy */
|
2013-10-13 22:16:47 +08:00
|
|
|
{
|
|
|
|
assert(_source);
|
|
|
|
|
|
|
|
size_t _length = t_memlen(_source) + 1; /* make space for null character */
|
|
|
|
|
2013-10-17 20:09:06 +08:00
|
|
|
uint8_t *_dest = calloc( sizeof ( uint8_t ), _length );
|
2013-10-13 22:16:47 +08:00
|
|
|
assert(_dest);
|
|
|
|
|
|
|
|
t_memcpy(_dest, _source, _length);
|
|
|
|
|
|
|
|
return _dest;
|
|
|
|
}
|
|
|
|
|
2013-10-17 20:09:06 +08:00
|
|
|
size_t t_strfind ( const uint8_t *_str, const uint8_t *_substr )
|
2013-10-13 22:16:47 +08:00
|
|
|
{
|
|
|
|
size_t _pos = 0;
|
|
|
|
size_t _it, _delit = 0;
|
|
|
|
|
2013-10-17 20:09:06 +08:00
|
|
|
for ( _it = 0; _str[_it] != '\0'; _it++ ) {
|
|
|
|
if ( _str[_it] == _substr[_delit] ) {
|
2013-10-13 22:16:47 +08:00
|
|
|
_pos = _it;
|
2013-10-17 20:09:06 +08:00
|
|
|
|
|
|
|
while ( _str[_it] == _substr[_delit] && _str[_it] != '\0' ) {
|
2013-10-13 22:16:47 +08:00
|
|
|
_it ++;
|
|
|
|
_delit++;
|
|
|
|
|
2013-10-17 20:09:06 +08:00
|
|
|
if ( _substr[_delit] == '\0' ) {
|
2013-10-13 22:16:47 +08:00
|
|
|
return _pos;
|
|
|
|
}
|
|
|
|
}
|
2013-10-17 20:09:06 +08:00
|
|
|
|
2013-10-13 22:16:47 +08:00
|
|
|
_delit = 0;
|
|
|
|
_pos = 0;
|
|
|
|
}
|
|
|
|
}
|
2013-10-17 20:09:06 +08:00
|
|
|
|
2013-10-13 22:16:47 +08:00
|
|
|
return _pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef WIN
|
|
|
|
#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
|
2013-10-17 20:09:06 +08:00
|
|
|
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
|
2013-10-13 22:16:47 +08:00
|
|
|
#else
|
2013-10-17 20:09:06 +08:00
|
|
|
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
|
2013-10-13 22:16:47 +08:00
|
|
|
#endif
|
|
|
|
|
2013-10-17 20:09:06 +08:00
|
|
|
struct timezone {
|
|
|
|
int tz_minuteswest; /* minutes W of Greenwich */
|
|
|
|
int tz_dsttime; /* type of dst correction */
|
2013-10-13 22:16:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int gettimeofday(struct timeval *tv, struct timezone *tz)
|
|
|
|
{
|
2013-10-17 20:09:06 +08:00
|
|
|
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;
|
2013-10-13 22:16:47 +08:00
|
|
|
}
|
|
|
|
|
2013-10-17 20:09:06 +08:00
|
|
|
return 0;
|
2013-10-13 22:16:47 +08:00
|
|
|
}
|
|
|
|
#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;
|
|
|
|
}
|