summaryrefslogtreecommitdiff
path: root/toxrtp/toxrtp_helper.c
diff options
context:
space:
mode:
authormannol <eniz_vukovic@hotmail.com>2013-10-13 16:16:47 +0200
committerBtbN <btbn@btbn.de>2013-10-13 16:16:47 +0200
commitda727875ac954b13ecb16521d255499511bb7424 (patch)
tree551904f3738612e9c15d98f320c323aa325a5cf8 /toxrtp/toxrtp_helper.c
parent1b971de651278429eea312f3240e1c5b8fbc67a4 (diff)
tox A/V: RTP/MSI implementation
Diffstat (limited to 'toxrtp/toxrtp_helper.c')
-rw-r--r--toxrtp/toxrtp_helper.c209
1 files changed, 209 insertions, 0 deletions
diff --git a/toxrtp/toxrtp_helper.c b/toxrtp/toxrtp_helper.c
new file mode 100644
index 00000000..bb2dc56b
--- /dev/null
+++ b/toxrtp/toxrtp_helper.c
@@ -0,0 +1,209 @@
1/* rtp_helper.c
2*
3* Has some standard functions. !Red!
4*
5*
6* Copyright (C) 2013 Tox project All Rights Reserved.
7*
8* This file is part of Tox.
9*
10* Tox is free software: you can redistribute it and/or modify
11* it under the terms of the GNU General Public License as published by
12* the Free Software Foundation, either version 3 of the License, or
13* (at your option) any later version.
14*
15* Tox is distributed in the hope that it will be useful,
16* but WITHOUT ANY WARRANTY; without even the implied warranty of
17* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18* GNU General Public License for more details.
19*
20* You should have received a copy of the GNU General Public License
21* along with Tox. If not, see <http://www.gnu.org/licenses/>.
22*
23*/
24
25
26
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif /* HAVE_CONFIG_H */
30
31#include "toxrtp_helper.h"
32#include "../toxcore/network.h"
33
34#include <arpa/inet.h> /* Fixes implicit function warning. */
35#include <assert.h>
36
37#ifdef WIN
38#include <windows.h>
39#endif /* WIN */
40
41
42static int _seed = 0; /* Not initiated */
43
44int t_setipport ( const char* _ip, unsigned short _port, void* _dest )
45{
46 assert(_dest);
47
48 IP_Port* _dest_c = ( IP_Port* ) _dest;
49 ip_init(&_dest_c->ip, 0);
50
51 IP_Port _ipv6_garbage;
52
53 if ( !addr_resolve(_ip, &_dest_c->ip, &_ipv6_garbage.ip) )
54 return FAILURE;
55
56 _dest_c->port = htons ( _port );
57
58 return SUCCESS;
59}
60
61uint32_t t_random ( uint32_t _max )
62{
63 if ( !_seed ) {
64 srand ( t_time() );
65 _seed++;
66 }
67
68 if ( _max <= 0 ) {
69 return ( unsigned ) rand();
70 } else {
71 return ( unsigned ) rand() % _max;
72 }
73}
74
75void t_memcpy ( uint8_t* _dest, const uint8_t* _source, size_t _size )
76{
77 /*
78 * Using countdown to zero method
79 * It's faster than for(_it = 0; _it < _size; _it++);
80 */
81 size_t _it = _size;
82
83 do {
84 _it--;
85 _dest[_it] = _source[_it];
86 } while ( _it );
87
88}
89
90uint8_t* t_memset ( uint8_t* _dest, uint8_t _valu, size_t _size )
91{
92 /*
93 * Again using countdown to zero method
94 */
95 size_t _it = _size;
96
97 do {
98 _it--;
99 _dest[_it] = _valu;
100 } while ( _it );
101
102 return _dest;
103}
104
105size_t t_memlen ( const uint8_t* _valu)
106{
107 const uint8_t* _it;
108 size_t _retu = 0;
109
110 for ( _it = _valu; *_it; ++_it ) ++_retu;
111
112 return _retu;
113}
114
115uint8_t* t_strallcpy ( const uint8_t* _source ) /* string alloc and copy */
116{
117 assert(_source);
118
119 size_t _length = t_memlen(_source) + 1; /* make space for null character */
120
121 uint8_t* _dest = calloc( sizeof ( uint8_t ), _length );
122 assert(_dest);
123
124 t_memcpy(_dest, _source, _length);
125
126 return _dest;
127}
128
129size_t t_strfind ( const uint8_t* _str, const uint8_t* _substr )
130{
131 size_t _pos = 0;
132 size_t _it, _delit = 0;
133
134 for ( _it = 0; _str[_it] != '\0'; _it++ ){
135 if ( _str[_it] == _substr[_delit] ){
136 _pos = _it;
137 while ( _str[_it] == _substr[_delit] && _str[_it] != '\0' ){
138 _it ++;
139 _delit++;
140
141 if ( _substr[_delit] == '\0' ){
142 return _pos;
143 }
144 }
145 _delit = 0;
146 _pos = 0;
147 }
148 }
149 return _pos;
150}
151
152#ifdef WIN
153#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
154 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
155#else
156 #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
157#endif
158
159struct timezone
160{
161 int tz_minuteswest; /* minutes W of Greenwich */
162 int tz_dsttime; /* type of dst correction */
163};
164
165int gettimeofday(struct timeval *tv, struct timezone *tz)
166{
167 FILETIME ft;
168 unsigned __int64 tmpres = 0;
169 static int tzflag;
170
171 if (NULL != tv)
172 {
173 GetSystemTimeAsFileTime(&ft);
174
175 tmpres |= ft.dwHighDateTime;
176 tmpres <<= 32;
177 tmpres |= ft.dwLowDateTime;
178
179 /*converting file time to unix epoch*/
180 tmpres -= DELTA_EPOCH_IN_MICROSECS;
181 tmpres /= 10; /*convert into microseconds*/
182 tv->tv_sec = (long)(tmpres / 1000000UL);
183 tv->tv_usec = (long)(tmpres % 1000000UL);
184 }
185
186 if (NULL != tz)
187 {
188 if (!tzflag)
189 {
190 _tzset();
191 tzflag++;
192 }
193 tz->tz_minuteswest = _timezone / 60;
194 tz->tz_dsttime = _daylight;
195 }
196
197 return 0;
198}
199#endif /* WIN */
200
201
202uint64_t t_time()
203{
204 struct timeval _tv;
205 gettimeofday(&_tv, NULL);
206 uint64_t _retu_usec = _tv.tv_sec % 1000000; /* get 6 digits an leave space for 3 more */
207 _retu_usec = _retu_usec * 1000 + (_tv.tv_usec / 1000 );
208 return _retu_usec;
209}