├── CMakeLists.txt ├── COPYING ├── COPYING.LGPL ├── COPYING.MPL ├── LICENSE ├── README.md ├── address.c ├── agent.c ├── base64.c ├── candidate.c ├── cmake └── FindBSD.cmake ├── component.c ├── component.mk ├── conncheck.c ├── discovery.c ├── event.c ├── include └── cice │ ├── address.h │ ├── agent.h │ ├── base64.h │ ├── bsd_queue.h │ ├── candidate.h │ ├── cice.h │ ├── common.h │ ├── component.h │ ├── conncheck.h │ ├── discovery.h │ ├── event.h │ ├── interfaces.h │ ├── log.h │ ├── network.h │ ├── socket.h │ ├── stream.h │ ├── stun.h │ ├── stun │ ├── constants.h │ ├── debug.h │ ├── md5.h │ ├── rand.h │ ├── sha1.h │ ├── stun5389.h │ ├── stunagent.h │ ├── stuncrc32.h │ ├── stunhmac.h │ ├── stunmessage.h │ ├── usages │ │ ├── bind.h │ │ ├── ice.h │ │ ├── timer.h │ │ └── turn.h │ ├── utils.h │ └── win32_common.h │ ├── stunagent.h │ ├── types.h │ └── utils.h ├── interfaces.c ├── log.c ├── network.c ├── socket.c ├── stream.c ├── stun.c ├── stun ├── debug.c ├── md5.c ├── rand.c ├── sha1.c ├── stun5389.c ├── stunagent.c ├── stuncrc32.c ├── stunhmac.c ├── stunmessage.c ├── usages │ ├── .dirstamp │ ├── bind.c │ ├── ice.c │ ├── timer.c │ └── turn.c └── utils.c ├── tools ├── Makefile ├── Makefile.am ├── Makefile.in ├── stund.c └── stund.h └── utils.c /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) # setting this is required 2 | project(cice) # this sets the project name 3 | 4 | ## file globbing ############################################################## 5 | file(GLOB sources_include include/cice) 6 | file(GLOB sources_top *.c *.h) 7 | file(GLOB sources_stun stun/*.c stun/*.h) 8 | file(GLOB sources_stun_usage stun/usages/*.h stun/usages/*.c ) 9 | file(GLOB_RECURSE sources_test src/test/*.cpp) 10 | file(GLOB_RECURSE data resources/*) 11 | 12 | ## target definitions ######################################################### 13 | add_library(cice SHARED ${sources_top} ${sources_stun} ${sources_stun_usage}) 14 | add_library(cice_static STATIC ${sources_top} ${sources_stun} ${sources_stun_usage}) 15 | 16 | include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindBSD.cmake) 17 | if (${BSD_FOUND}) 18 | target_include_directories(cice PUBLIC ${BSD_INCLUDE_DIRS}) 19 | target_include_directories(cice_static PUBLIC ${BSD_INCLUDE_DIRS}) 20 | MESSAGE(STATUS "libbsd include: ${BSD_INCLUDE_DIRS}") 21 | MESSAGE(STATUS "libbsd lib: ${BSD_LIBRARIES}") 22 | else () 23 | MESSAGE(FATAL_ERROR LIBBSD not found) 24 | endif () 25 | 26 | 27 | target_compile_options(cice PUBLIC -g -Wall -DUSE_LIBEVENT2) 28 | target_compile_options(cice_static PUBLIC -g -Wall -DUSE_LIBEVENT2) 29 | target_include_directories(cice PUBLIC include) 30 | target_include_directories(cice_static PUBLIC include) 31 | 32 | target_link_libraries(cice PUBLIC 33 | ${BSD_LIBRARIES} 34 | ) 35 | 36 | target_link_libraries(cice_static PUBLIC 37 | ${BSD_LIBRARIES} 38 | ) 39 | 40 | file(COPY ${data} DESTINATION resources) 41 | 42 | ## dependencies ############################################################### 43 | # Find Libevent 44 | # http://monkey.org/~provos/libevent/ 45 | # 46 | # Once done, this will define: 47 | # 48 | # Event_FOUND - system has Event 49 | # Event_INCLUDE_DIRS - the Event include directories 50 | # Event_LIBRARIES - link these to use Event 51 | # 52 | if (EVENT_INCLUDE_DIR AND EVENT_LIBRARY) 53 | # Already in cache, be silent 54 | set(EVENT_FIND_QUIETLY TRUE) 55 | endif (EVENT_INCLUDE_DIR AND EVENT_LIBRARY) 56 | 57 | find_path(EVENT_INCLUDE_DIR event.h 58 | PATHS /usr/include 59 | PATH_SUFFIXES event 60 | ) 61 | 62 | find_library(EVENT_LIBRARY 63 | NAMES event 64 | PATHS /usr/lib /usr/local/lib 65 | ) 66 | 67 | set(EVENT_LIBRARIES ${EVENT_LIBRARY} ) 68 | 69 | add_definitions(-DLIBNET_LIL_ENDIAN) 70 | 71 | include(FindPackageHandleStandardArgs) 72 | find_package_handle_standard_args(EVENT 73 | DEFAULT_MSG 74 | EVENT_INCLUDE_DIR 75 | EVENT_LIBRARIES 76 | ) 77 | 78 | mark_as_advanced(EVENT_INCLUDE_DIR EVENT_LIBRARY) 79 | 80 | target_link_libraries(cice PUBLIC 81 | ${EVENT_LIBRARY} 82 | ) 83 | 84 | ############################################################################### 85 | ## testing #################################################################### 86 | ############################################################################### 87 | 88 | # This is for our testing framework, 89 | # we don't add REQUIRED because it's just for testing. 90 | # People who might want to build the project to use it should not be required 91 | # to install testing dependencies. 92 | #find_package(GTest) 93 | 94 | #if(GTEST_FOUND) 95 | # add_executable(unit_tests ${sources_test} ${sources}) 96 | # 97 | # # This define is added to prevent collision with the main. 98 | # # It might be better solved by not adding the source with the main to the 99 | # # testing target. 100 | # target_compile_definitions(unit_tests PUBLIC UNIT_TESTS) 101 | # 102 | # # This allows us to use the executable as a link library, and inherit all 103 | # # linker options and library dependencies from it, by simply adding it as dependency. 104 | # set_target_properties(example PROPERTIES ENABLE_EXPORTS on) 105 | # 106 | # target_link_libraries(unit_tests PUBLIC 107 | # ${GTEST_BOTH_LIBRARIES} 108 | # example 109 | # ) 110 | # 111 | # target_include_directories(unit_tests PUBLIC 112 | # ${GTEST_INCLUDE_DIRS} # doesn't do anything on linux 113 | # ) 114 | # 115 | #endif() 116 | 117 | ############################################################################### 118 | ## packaging ################################################################## 119 | ############################################################################### 120 | install(TARGETS cice DESTINATION lib) 121 | install( 122 | DIRECTORY ${sources_include} 123 | DESTINATION include 124 | ) 125 | 126 | set(CPACK_PACKAGE_NAME "libcice") 127 | set(CPACK_PACKAGE_VERSION "1.0.0") 128 | set(CPACK_MONOLITHIC_INSTALL 1) 129 | include(CPack) 130 | 131 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | The Nice Glib ICE library is licensed under both the Mozilla Public License 2 | version 1.1 and the GNU Lesser General Public License version 2.1. For the 3 | full text of these licenses, see the files COPYING.MPL and COPYING.LGPL 4 | respectively. 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The Nice Glib ICE library is licensed under both the Mozilla Public License 2 | version 1.1 and the GNU Lesser General Public License version 2.1. For the 3 | full text of these licenses, see the files COPYING.MPL and COPYING.LGPL 4 | respectively. 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # libcice 2 | An implementation of ICE Lite protocol, based on libnice. The aim of the project is to create pure C library, which can be easily integrated into applications. 3 | 4 | TODO: 5 | + Support only udp, extension for tcp is needed. 6 | + Add support for reflexive candidate via stun requests. 7 | + It currently depends on libevent, but in the future select/epoll/kqueue should be added. 8 | 9 | -------------------------------------------------------------------------------- /base64.c: -------------------------------------------------------------------------------- 1 | #include "cice/base64.h" 2 | 3 | static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 4 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 5 | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 6 | 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 7 | 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 8 | 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 9 | 'w', 'x', 'y', 'z', '0', '1', '2', '3', 10 | '4', '5', '6', '7', '8', '9', '+', '/'}; 11 | static char *decoding_table = NULL; 12 | static uint32_t mod_table[] = {0, 2, 1}; 13 | 14 | void build_decoding_table() { 15 | int i = 0; 16 | decoding_table = (char*)malloc(256); 17 | for (i = 0; i < 64; i++) 18 | decoding_table[(unsigned char) encoding_table[i]] = i; 19 | return; 20 | } 21 | 22 | 23 | 24 | char *base64_encode(const unsigned char *data, 25 | size_t input_length, 26 | size_t *output_length) { 27 | uint32_t i = 0; 28 | uint32_t j = 0; 29 | *output_length = 4 * ((input_length + 2) / 3); 30 | 31 | char *encoded_data = (char*)malloc(*output_length); 32 | if (encoded_data == NULL) return NULL; 33 | 34 | memset(encoded_data,0,*output_length); 35 | 36 | for (i = 0, j = 0; i < input_length;) { 37 | 38 | uint32_t octet_a = i < input_length ? (unsigned char)data[i++] : 0; 39 | uint32_t octet_b = i < input_length ? (unsigned char)data[i++] : 0; 40 | uint32_t octet_c = i < input_length ? (unsigned char)data[i++] : 0; 41 | 42 | uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c; 43 | 44 | encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F]; 45 | encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F]; 46 | encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F]; 47 | encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F]; 48 | } 49 | 50 | for (i = 0; i < mod_table[input_length % 3]; i++) 51 | encoded_data[*output_length - 1 - i] = '='; 52 | 53 | return encoded_data; 54 | } 55 | 56 | 57 | unsigned char *base64_decode(const unsigned char *data, 58 | size_t input_length, 59 | size_t *output_length) { 60 | uint32_t i = 0; 61 | uint32_t j = 0; 62 | if (decoding_table == NULL) build_decoding_table(); 63 | 64 | if (input_length % 4 != 0) return NULL; 65 | 66 | *output_length = input_length / 4 * 3; 67 | if (data[input_length - 1] == '=') (*output_length)--; 68 | if (data[input_length - 2] == '=') (*output_length)--; 69 | 70 | unsigned char *decoded_data = (unsigned char*)malloc(*output_length); 71 | if (decoded_data == NULL) return NULL; 72 | 73 | for (i = 0, j = 0; i < input_length;) { 74 | 75 | uint32_t sextet_a = data[i] == '=' ? 0 & i++ : (uint32_t)decoding_table[data[i++]]; 76 | uint32_t sextet_b = data[i] == '=' ? 0 & i++ : (uint32_t)decoding_table[data[i++]]; 77 | uint32_t sextet_c = data[i] == '=' ? 0 & i++ : (uint32_t)decoding_table[data[i++]]; 78 | uint32_t sextet_d = data[i] == '=' ? 0 & i++ : (uint32_t)decoding_table[data[i++]]; 79 | 80 | uint32_t triple = (sextet_a << 3 * 6) 81 | + (sextet_b << 2 * 6) 82 | + (sextet_c << 1 * 6) 83 | + (sextet_d << 0 * 6); 84 | 85 | if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF; 86 | if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF; 87 | if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF; 88 | } 89 | 90 | return decoded_data; 91 | } 92 | 93 | void base64_cleanup() { 94 | free(decoding_table); 95 | } 96 | -------------------------------------------------------------------------------- /cmake/FindBSD.cmake: -------------------------------------------------------------------------------- 1 | # BSD_INCLUDE_DIRS - where to find , etc. 2 | # BSD_LIBRARIES - List of libraries when using libnettle. 3 | # BSD_FOUND - True if libnettle found. 4 | if(BSD_INCLUDE_DIRS) 5 | # Already in cache, be silent 6 | set(BSD_FIND_QUIETLY YES) 7 | endif() 8 | 9 | find_path(BSD_INCLUDE_DIRS bsd/bsd.h) 10 | find_library(BSD_LIBRARY NAMES bsd libbsd) 11 | 12 | # handle the QUIETLY and REQUIRED arguments and set BSD_FOUND to TRUE if 13 | # all listed variables are TRUE 14 | include(FindPackageHandleStandardArgs) 15 | find_package_handle_standard_args(BSD DEFAULT_MSG BSD_LIBRARY BSD_INCLUDE_DIRS) 16 | 17 | if(BSD_FOUND) 18 | set(BSD_LIBRARIES ${BSD_LIBRARY}) 19 | endif() 20 | -------------------------------------------------------------------------------- /component.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Component Makefile 3 | # 4 | # This Makefile should, at the very least, just include $(SDK_PATH)/make/component.mk. By default, 5 | # this will take the sources in this directory, compile them and link them into 6 | # lib(subdirectory_name).a in the build directory. This behaviour is entirely configurable, 7 | # please read the SDK documents if you need to do this. 8 | # 9 | COMPONENT_ADD_INCLUDEDIRS := include 10 | #COMPONENT_PRIV_INCLUDEDIRS := 11 | COMPONENT_SRCDIRS := . stun stun/usages 12 | 13 | #EXTRA_CFLAGS := -DICACHE_RODATA_ATTR 14 | #CFLAGS += -Wno-error=implicit-function-declaration -Wno-error=format= -DHAVE_CONFIG_H 15 | CFLAGS += -DUSE_ESP32 -Wno-char-subscripts 16 | -------------------------------------------------------------------------------- /event.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Jackie Dinh 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 1 Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2 Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 3 Neither the name of the nor the 14 | * names of its contributors may be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | * 28 | * @(#)event.c 29 | */ 30 | 31 | #ifdef USE_LIBEVENT2 32 | #include 33 | #include 34 | #endif 35 | 36 | #include "cice/address.h" 37 | #include "cice/common.h" 38 | #include "cice/event.h" 39 | #include "cice/log.h" 40 | #include "cice/socket.h" 41 | 42 | #ifdef USE_LIBEVENT2 43 | 44 | void 45 | libevent2_init_udp_socket(socket_t *sock) { 46 | address_t *addr = &sock->addr; 47 | socklen_t addrlen = 0; 48 | int flags = -1; 49 | int fd; 50 | 51 | if (addr->s.addr.sa_family == AF_INET) { 52 | fd = socket(AF_INET,SOCK_DGRAM,0); 53 | if (fd < 0) { 54 | ICE_ERROR("cannot create udp socket"); 55 | return; 56 | } 57 | 58 | { // optimize buffer size 59 | int optval = 1024*1024; 60 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const char*) &optval, sizeof(optval)); 61 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*) &optval, sizeof(optval)); 62 | optval = 1; 63 | setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (const char*) &optval, sizeof(optval)); 64 | } 65 | 66 | if (bind(fd,&addr->s.addr,sizeof(addr->s.ip4)) < 0) { 67 | ICE_ERROR("failed to binding ip4"); 68 | return; 69 | } 70 | addrlen = sizeof(addr->s.ip4); 71 | if (getsockname(fd, &addr->s.addr, &addrlen) < 0) { 72 | ICE_ERROR("getsockname failed"); 73 | return; 74 | } 75 | ICE_DEBUG("binding ip4, addrlen=%u,port=%u", addrlen,addr->s.ip4.sin_port); 76 | } else if (addr->s.addr.sa_family == AF_INET6) { 77 | fd = socket(AF_INET6,SOCK_DGRAM,0); 78 | if (fd < 0) { 79 | ICE_ERROR("cannot create udp socket"); 80 | return; 81 | } 82 | if (bind(fd,&addr->s.addr,sizeof(addr->s.ip6)) < 0) { 83 | ICE_ERROR("failed to binding ip6, errno=%d",errno); 84 | return; 85 | } 86 | addrlen = sizeof(addr->s.ip6); 87 | if (getsockname(fd, &addr->s.addr, &addrlen) < 0) { 88 | ICE_ERROR("getsockname failed"); 89 | return; 90 | } 91 | ICE_DEBUG("binding ip6, addrlen=%u,port=%u", addrlen,addr->s.ip4.sin_port); 92 | } 93 | 94 | flags = fcntl(fd, F_GETFL, 0); 95 | if (flags == -1) { 96 | close(fd); 97 | return; 98 | } 99 | 100 | if ( !(flags & O_NONBLOCK) ) { 101 | if (fcntl(fd, F_SETFL, flags | O_NONBLOCK | O_NDELAY) < 0) { 102 | close(fd); 103 | return; 104 | } 105 | } 106 | 107 | sock->fd = fd; 108 | 109 | return; 110 | } 111 | 112 | void 113 | libevent2_init_tcp_socket(socket_t *sock) { 114 | return; 115 | } 116 | 117 | socket_t* 118 | libevent2_create_socket(event_ctx_t *ctx, socket_t* sock, event_callback_func cb) { 119 | struct event *ev = 0; 120 | 121 | if (!sock) return NULL; 122 | 123 | switch(sock->type) { 124 | case ICE_SOCKET_TYPE_UDP_BSD: 125 | libevent2_init_udp_socket(sock); 126 | break; 127 | case ICE_SOCKET_TYPE_TCP_BSD: 128 | libevent2_init_tcp_socket(sock); 129 | break; 130 | default: 131 | return NULL; 132 | } 133 | 134 | ev = event_new(ctx->ev_base, sock->fd, EV_READ|EV_PERSIST, cb, sock); //socket_udp_read_cb 135 | event_add(ev, NULL); 136 | sock->ev = ev; 137 | 138 | return sock; 139 | } 140 | 141 | void 142 | libevent2_destroy_socket(event_ctx_t *ctx, socket_t* sock) { 143 | 144 | if (!sock) return; 145 | 146 | //FIXME: socket_free should be replaced by destroy_socket. 147 | if (sock->type == ICE_SOCKET_TYPE_UDP_BSD) { 148 | event_del(sock->ev); 149 | } 150 | 151 | close(sock->fd); 152 | ICE_FREE(sock); 153 | 154 | return; 155 | } 156 | 157 | event_info_t* 158 | libevent2_create_event(event_ctx_t *ctx, int type, event_callback_func cb, int timeout) { 159 | event_info_t *ev = 0; 160 | struct timeval time_interval; 161 | struct event *time_ev; 162 | 163 | if (!ctx) return 0; 164 | 165 | ev = (event_info_t*)malloc(sizeof(event_info_t)); 166 | if (ev == NULL) { 167 | return 0; 168 | } 169 | memset(ev,0,sizeof(event_info_t)); 170 | 171 | ev->ctx = ctx; 172 | time_interval.tv_sec = 0; 173 | time_interval.tv_usec = timeout; 174 | time_ev = event_new(ctx->ev_base,-1,type,cb,ctx->agent); 175 | event_add(time_ev, &time_interval); 176 | ev->ev = time_ev; 177 | 178 | return ev; 179 | } 180 | 181 | void 182 | libevent2_destroy_event(event_ctx_t *ctx, event_info_t *ev) { 183 | 184 | event_del(ev->ev); 185 | return; 186 | } 187 | #endif 188 | 189 | #ifdef USE_ESP32 190 | socket_t* 191 | esp32_create_socket(event_ctx_t *ctx, socket_t* sock, event_callback_func cb) { 192 | 193 | return NULL; 194 | } 195 | 196 | void 197 | esp32_destroy_socket(event_ctx_t *ctx, socket_t* sock) { 198 | 199 | return; 200 | } 201 | 202 | event_info_t* 203 | esp32_create_event(event_ctx_t *ctx, int type, event_callback_func cb, int timeout) { 204 | 205 | return NULL; 206 | } 207 | 208 | void 209 | esp32_destroy_event(event_ctx_t *ctx, event_info_t *ev) { 210 | 211 | return; 212 | } 213 | 214 | #endif //USE_ESP32 215 | 216 | #ifdef USE_LIBEVENT2 217 | event_ctx_t* 218 | create_event_ctx(void *data) { 219 | struct event_base *ev_base = (struct event_base*)data; 220 | event_ctx_t *ctx = 0; 221 | 222 | ctx = (event_ctx_t*)malloc(sizeof(event_ctx_t)); 223 | if (ctx == NULL) { 224 | return NULL; 225 | } 226 | memset(ctx,0,sizeof(event_ctx_t)); 227 | 228 | if (ev_base) { 229 | ctx->ev_base = ev_base; 230 | } else { 231 | ctx->ev_base = event_base_new(); 232 | if (ctx->ev_base == NULL ) { 233 | ICE_ERROR("failed to create event_base"); 234 | return NULL; 235 | } 236 | } 237 | 238 | ctx->create_socket = libevent2_create_socket; 239 | ctx->destroy_socket = libevent2_destroy_socket; 240 | ctx->create_event = libevent2_create_event; 241 | ctx->destroy_event = libevent2_destroy_event; 242 | 243 | return ctx; 244 | } 245 | #endif 246 | 247 | 248 | #ifdef USE_ESP32 249 | event_ctx_t* 250 | create_event_ctx(void *data) { 251 | event_ctx_t *ctx = 0; 252 | 253 | ctx = (event_ctx_t*)malloc(sizeof(event_ctx_t)); 254 | if (ctx == NULL) { 255 | return NULL; 256 | } 257 | memset(ctx,0,sizeof(event_ctx_t)); 258 | 259 | ctx->create_socket = esp32_create_socket; 260 | ctx->destroy_socket = esp32_destroy_socket; 261 | ctx->create_event = esp32_create_event; 262 | ctx->destroy_event = esp32_destroy_event; 263 | 264 | return ctx; 265 | } 266 | #endif 267 | 268 | socket_t* 269 | create_socket(event_ctx_t *ctx, IceSocketType type, address_t *addr, event_callback_func cb) { 270 | socket_t *sock; 271 | 272 | sock = socket_new(ICE_SOCKET_TYPE_UDP_BSD); 273 | if (sock == NULL) 274 | return NULL; 275 | sock->addr = *addr; 276 | 277 | //setup socket specifics 278 | return ctx->create_socket(ctx,sock,cb); 279 | } 280 | 281 | void 282 | destroy_socket(event_ctx_t *ctx, socket_t *sock) { 283 | //FIXME 284 | return; 285 | } 286 | 287 | event_info_t* 288 | create_event_info(event_ctx_t *ctx, int type, event_callback_func cb, int timeout) { 289 | return ctx->create_event(ctx,type,cb,timeout); 290 | } 291 | 292 | void 293 | destroy_event_info(event_ctx_t *ctx, event_info_t *ev) { 294 | 295 | if (!ev) return; 296 | 297 | ctx->destroy_event(ctx, ev); 298 | return; 299 | } 300 | 301 | //create_timer(agent->base,priv_conn_keepalive_tick); 302 | 303 | 304 | 305 | 306 | 307 | 308 | -------------------------------------------------------------------------------- /include/cice/address.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2006-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2006-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Kai Vehmanen 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Dafydd Harries, Collabora Ltd. 26 | * Youness Alaoui, Collabora Ltd. 27 | * Kai Vehmanen, Nokia 28 | * 29 | * Alternatively, the contents of this file may be used under the terms of the 30 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 31 | * case the provisions of LGPL are applicable instead of those above. If you 32 | * wish to allow use of your version of this file only under the terms of the 33 | * LGPL and not to allow others to use your version of this file under the 34 | * MPL, indicate your decision by deleting the provisions above and replace 35 | * them with the notice and other provisions required by the LGPL. If you do 36 | * not delete the provisions above, a recipient may use your version of this 37 | * file under either the MPL or the LGPL. 38 | */ 39 | 40 | /* 41 | * Porting to C library using libevent. 42 | * Jackie Dinh - 2016 43 | */ 44 | 45 | #ifndef _CICE_ADDRESS_H_ 46 | #define _CICE_ADDRESS_H_ 47 | 48 | #ifdef __cplusplus 49 | extern "C" { 50 | #endif 51 | 52 | #include 53 | #include 54 | #include 55 | 56 | #include "cice/common.h" 57 | #include "cice/types.h" 58 | 59 | struct _address 60 | { 61 | union 62 | { 63 | struct sockaddr addr; 64 | struct sockaddr_in ip4; 65 | struct sockaddr_in6 ip6; 66 | } s; 67 | TAILQ_ENTRY(_address) list; 68 | }; 69 | typedef TAILQ_HEAD(address_head, _address) address_head_t; 70 | 71 | #define ICE_ADDRESS_STRING_LEN INET6_ADDRSTRLEN 72 | 73 | address_t* 74 | address_new(void); 75 | 76 | void 77 | address_free(address_t *addr); 78 | 79 | void 80 | address_to_string(const address_t *addr, char *dst); 81 | 82 | void 83 | address_set_port(address_t *addr, uint16_t port); 84 | 85 | int 86 | address_equal_no_port(const address_t *a, const address_t *b); 87 | 88 | int 89 | address_equal(const address_t *a, const address_t *b); 90 | 91 | void 92 | print_address(const address_t *addr); 93 | 94 | uint32_t 95 | address_get_port(const address_t *addr); 96 | 97 | int 98 | address_set_from_string(address_t *addr, const char *str); 99 | 100 | int 101 | address_is_valid(const address_t *a); 102 | 103 | void 104 | address_set_from_sockaddr (address_t *addr, 105 | const struct sockaddr *sa); 106 | 107 | void 108 | address_copy_to_sockaddr(const address_t *addr, struct sockaddr *_sa); 109 | 110 | int 111 | get_address_length(const address_t *addr); 112 | 113 | address_t * 114 | address_dup(const address_t *a); 115 | 116 | void 117 | address_init(address_t *addr); 118 | 119 | int 120 | address_is_private(const address_t *a); 121 | 122 | #ifdef __cplusplus 123 | } 124 | #endif 125 | 126 | #endif //_ADDRESS_H_ 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | -------------------------------------------------------------------------------- /include/cice/base64.h: -------------------------------------------------------------------------------- 1 | #ifndef _ICE_BASE64_H_ 2 | #define _ICE_BASE64_H_ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | char *base64_encode(const unsigned char *data, 13 | size_t input_length, 14 | size_t *output_length); 15 | 16 | unsigned char *base64_decode(const unsigned char *data, 17 | size_t input_length, 18 | size_t *output_length); 19 | 20 | #ifdef __cplusplus 21 | } 22 | #endif 23 | 24 | #endif // _ICE_BASE64_H_ 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /include/cice/candidate.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2006-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2006-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Kai Vehmanen 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Dafydd Harries, Collabora Ltd. 26 | * Youness Alaoui, Collabora Ltd. 27 | * Kai Vehmanen, Nokia 28 | * 29 | * Alternatively, the contents of this file may be used under the terms of the 30 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 31 | * case the provisions of LGPL are applicable instead of those above. If you 32 | * wish to allow use of your version of this file only under the terms of the 33 | * LGPL and not to allow others to use your version of this file under the 34 | * MPL, indicate your decision by deleting the provisions above and replace 35 | * them with the notice and other provisions required by the LGPL. If you do 36 | * not delete the provisions above, a recipient may use your version of this 37 | * file under either the MPL or the LGPL. 38 | */ 39 | 40 | /* 41 | * Porting to C library using libevent. 42 | * Jackie Dinh - 2016 43 | */ 44 | 45 | 46 | #ifndef _CANDIDATE_H_ 47 | #define _CANDIDATE_H_ 48 | 49 | #ifdef __cplusplus 50 | extern "C" { 51 | #endif 52 | 53 | #include 54 | #include 55 | 56 | #ifdef USE_ESP32 57 | #include "lwip/sockets.h" 58 | #endif 59 | 60 | #include "cice/address.h" 61 | #include "cice/types.h" 62 | 63 | typedef enum 64 | { 65 | ICE_CANDIDATE_TYPE_HOST, 66 | ICE_CANDIDATE_TYPE_SERVER_REFLEXIVE, 67 | ICE_CANDIDATE_TYPE_PEER_REFLEXIVE, 68 | ICE_CANDIDATE_TYPE_RELAYED, 69 | ICE_CANDIDATE_TYPE_LAST, 70 | } IceCandidateType; 71 | 72 | typedef enum 73 | { 74 | ICE_CANDIDATE_TRANSPORT_UDP, 75 | ICE_CANDIDATE_TRANSPORT_TCP_ACTIVE, 76 | ICE_CANDIDATE_TRANSPORT_TCP_PASSIVE, 77 | ICE_CANDIDATE_TRANSPORT_TCP_SO, 78 | } IceCandidateTransport; 79 | 80 | typedef enum { 81 | ICE_RELAY_TYPE_TURN_UDP, 82 | ICE_RELAY_TYPE_TURN_TCP, 83 | ICE_RELAY_TYPE_TURN_TLS 84 | } IceRelayType; 85 | 86 | typedef enum { 87 | ADD_HOST_MIN = 0, 88 | ADD_HOST_UDP = ADD_HOST_MIN, 89 | ADD_HOST_TCP_ACTIVE, 90 | ADD_HOST_TCP_PASSIVE, 91 | ADD_HOST_MAX = ADD_HOST_TCP_PASSIVE 92 | } AddHostType; 93 | 94 | struct _turnserver 95 | { 96 | int32_t ref_count; 97 | address_t server; 98 | char *username; 99 | char *password; 100 | IceRelayType type; 101 | }; 102 | 103 | 104 | struct _candidate 105 | { 106 | IceCandidateType type; 107 | IceCandidateTransport transport; 108 | address_t addr; 109 | address_t base_addr; 110 | uint32_t priority; 111 | uint32_t stream_id; 112 | uint32_t component_id; 113 | char foundation[ICE_CANDIDATE_MAX_FOUNDATION]; 114 | char *username; /* pointer to a nul-terminated username string */ 115 | char *password; /* pointer to a nul-terminated password string */ 116 | void *sockptr; 117 | 118 | turnserver_t *turn; 119 | 120 | TAILQ_ENTRY(_candidate) list; 121 | }; 122 | typedef TAILQ_HEAD(_candidate_head, _candidate) candidate_head_t; 123 | 124 | 125 | candidate_t * 126 | candidate_new(IceCandidateType type); 127 | 128 | candidate_head_t * 129 | candidate_head_new(); 130 | 131 | void 132 | candidate_free(candidate_t *candidate); 133 | 134 | candidate_t* 135 | candidate_copy (const candidate_t *candidate); 136 | 137 | uint32_t 138 | candidate_jingle_priority (candidate_t *candidate); 139 | 140 | uint32_t 141 | candidate_msn_priority (candidate_t *candidate); 142 | 143 | uint32_t 144 | candidate_ms_ice_priority (const candidate_t *candidate, 145 | int reliable, int nat_assisted); 146 | 147 | uint32_t 148 | candidate_ice_priority (const candidate_t *candidate, 149 | int reliable, int nat_assisted); 150 | 151 | uint64_t 152 | candidate_pair_priority(uint32_t o_prio, uint32_t a_prio); 153 | 154 | 155 | void 156 | print_candidate(candidate_t *c, char *msg); 157 | 158 | #ifdef __cplusplus 159 | } 160 | #endif 161 | 162 | #endif //_CANDIDATE_H_ 163 | 164 | 165 | -------------------------------------------------------------------------------- /include/cice/cice.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Jackie Dinh 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 1 Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2 Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 3 Neither the name of the nor the 14 | * names of its contributors may be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | * 28 | * @(#)cice.h 29 | */ 30 | 31 | #ifndef _CICE_CICE_H_ 32 | #define _CICE_CICE_H_ 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | #include "cice/address.h" 39 | #include "cice/agent.h" 40 | #include "cice/base64.h" 41 | #include "cice/candidate.h" 42 | #include "cice/common.h" 43 | #include "cice/component.h" 44 | #include "cice/conncheck.h" 45 | #include "cice/discovery.h" 46 | #include "cice/event.h" 47 | #include "cice/interfaces.h" 48 | #include "cice/list.h" 49 | #include "cice/log.h" 50 | #include "cice/network.h" 51 | #include "cice/socket.h" 52 | #include "cice/stream.h" 53 | #include "cice/stun/constants.h" 54 | #include "cice/stun/debug.h" 55 | #include "cice/stun/md5.h" 56 | #include "cice/stun/rand.h" 57 | #include "cice/stun/sha1.h" 58 | #include "cice/stun/stun5389.h" 59 | #include "cice/stun/stunagent.h" 60 | #include "cice/stun/stuncrc32.h" 61 | #include "cice/stun/stunhmac.h" 62 | #include "cice/stun/stunmessage.h" 63 | #include "cice/stun/utils.h" 64 | #include "cice/stun/usages/bind.h" 65 | #include "cice/stun/usages/ice.h" 66 | #include "cice/stun/usages/timer.h" 67 | #include "cice/stun/usages/turn.h" 68 | #include "cice/stunagent.h" 69 | #include "cice/stun.h" 70 | #include "cice/types.h" 71 | #include "cice/utils.h" 72 | 73 | #endif //_CICE_CICE_H_ 74 | 75 | 76 | -------------------------------------------------------------------------------- /include/cice/common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Jackie Dinh 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 1 Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2 Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 3 Neither the name of the nor the 14 | * names of its contributors may be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | * 28 | * @(#)common.h 29 | */ 30 | 31 | #ifndef _CICE_COMMON_H_ 32 | #define _CICE_COMMON_H_ 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | #ifdef USE_LIBEVENT2 39 | 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | 54 | #endif 55 | 56 | #ifdef USE_ESP32 57 | 58 | //#include 59 | //#include 60 | //#include 61 | 62 | #include "freertos/FreeRTOS.h" 63 | #include "freertos/task.h" 64 | 65 | #include 66 | #include 67 | 68 | //flags borrowed from libevents 69 | #define EV_TIMEOUT 0x01 70 | #define EV_READ 0x02 71 | #define EV_WRITE 0x04 72 | #define EV_SIGNAL 0x08 73 | #define EV_PERSIST 0x10 74 | #define EV_ET 0x20 75 | 76 | #ifndef IN6_ARE_ADDR_EQUAL 77 | #define IN6_ARE_ADDR_EQUAL(a,b) \ 78 | ((((__const uint32_t *) (a))[0] == ((__const uint32_t *) (b))[0]) \ 79 | && (((__const uint32_t *) (a))[1] == ((__const uint32_t *) (b))[1]) \ 80 | && (((__const uint32_t *) (a))[2] == ((__const uint32_t *) (b))[2]) \ 81 | && (((__const uint32_t *) (a))[3] == ((__const uint32_t *) (b))[3])) 82 | #endif //IN6_ARE_ADDR_EQUAL 83 | 84 | #endif 85 | 86 | #ifdef __cplusplus 87 | } 88 | #endif 89 | 90 | #endif //_CICE_COMMON_H_ 91 | 92 | 93 | -------------------------------------------------------------------------------- /include/cice/component.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2006-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2006-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Kai Vehmanen 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Dafydd Harries, Collabora Ltd. 26 | * Youness Alaoui, Collabora Ltd. 27 | * Kai Vehmanen, Nokia 28 | * 29 | * Alternatively, the contents of this file may be used under the terms of the 30 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 31 | * case the provisions of LGPL are applicable instead of those above. If you 32 | * wish to allow use of your version of this file only under the terms of the 33 | * LGPL and not to allow others to use your version of this file under the 34 | * MPL, indicate your decision by deleting the provisions above and replace 35 | * them with the notice and other provisions required by the LGPL. If you do 36 | * not delete the provisions above, a recipient may use your version of this 37 | * file under either the MPL or the LGPL. 38 | */ 39 | 40 | /* 41 | * Porting to C library using libevent. 42 | * Jackie Dinh - 2016 43 | */ 44 | 45 | 46 | #ifndef _COMPONENT_H_ 47 | #define _COMPONENT_H_ 48 | 49 | #ifdef __cplusplus 50 | extern "C" { 51 | #endif 52 | 53 | #include 54 | 55 | #include "cice/candidate.h" 56 | #include "cice/types.h" 57 | #include "stun/stunagent.h" 58 | #include "stun/usages/timer.h" 59 | 60 | typedef enum 61 | { 62 | ICE_COMPONENT_TYPE_RTP = 1, 63 | ICE_COMPONENT_TYPE_RTCP = 2 64 | } IceComponentType; 65 | 66 | 67 | typedef enum 68 | { 69 | ICE_COMPONENT_STATE_DISCONNECTED, // No activity scheduled 70 | ICE_COMPONENT_STATE_GATHERING, // Gathering local candidates 71 | ICE_COMPONENT_STATE_CONNECTING, // Establishing connectivity 72 | ICE_COMPONENT_STATE_CONNECTED, // At least one working candidate pair 73 | ICE_COMPONENT_STATE_READY, // ICE concluded, candidate pair selection is now final 74 | ICE_COMPONENT_STATE_FAILED, // Connectivity checks have been completed, 75 | // but connectivity was not established 76 | ICE_COMPONENT_STATE_LAST // Dummy state 77 | } IceComponentState; 78 | 79 | struct _candidate_pair_keepalive 80 | { 81 | agent_t *agent; 82 | uint32_t stream_id; 83 | uint32_t component_id; 84 | StunTimer timer; 85 | uint8_t stun_buffer[STUN_MAX_MESSAGE_SIZE_IPV6]; 86 | StunMessage stun_message; 87 | /*GSource *tick_source;*/ 88 | }; 89 | 90 | 91 | struct _candidate_pair 92 | { 93 | candidate_t *local; 94 | candidate_t *remote; 95 | uint64_t priority; /* candidate pair priority */ 96 | candidate_pair_keepalive_t keepalive; 97 | }; 98 | 99 | struct _incoming_check 100 | { 101 | address_t from; 102 | socket_t *local_socket; 103 | uint32_t priority; 104 | int use_candidate; 105 | uint8_t *username; 106 | uint16_t username_len; 107 | 108 | TAILQ_ENTRY(_incoming_check) list; 109 | }; 110 | typedef TAILQ_HEAD(_incoming_check_head, _incoming_check) incoming_check_head_t; 111 | 112 | struct _component 113 | { 114 | uint32_t id; /* component id */ 115 | IceComponentType type; 116 | IceComponentState state; 117 | candidate_head_t local_candidates; /* list of candidate_t objs */ 118 | candidate_head_t remote_candidates; /* list of candidate_t objs */ 119 | candidate_t *restart_candidate; /* for storing active remote candidate during a restart */ 120 | candidate_pair_t selected_pair; /* independent from checklists, 121 | see ICE 11.1. "Sending Media" (ID-19) */ 122 | 123 | void *tcp; /* pointer to PseudoTcpSocket */ 124 | void *agent; /* pointer to agent_t */ 125 | stream_t *stream; 126 | 127 | agent_recv_func io_callback; /* function called on io cb */ 128 | void *io_data; /* data passed to the io function */ 129 | void *sock; 130 | 131 | struct stun_agent_t stun_agent; /* This stun agent is used to validate all stun requests */ 132 | 133 | uint16_t min_port; 134 | uint16_t max_port; 135 | 136 | incoming_check_head_t incoming_checks; /* list of incoming_check objs */ 137 | 138 | TAILQ_ENTRY(_component) list; 139 | }; 140 | typedef TAILQ_HEAD(_component_head, _component) component_head_t; 141 | 142 | component_t * 143 | component_new (agent_t *agent, stream_t *stream, uint32_t id); 144 | 145 | void 146 | component_set_io_callback (component_t *component, agent_recv_func cb, 147 | void *user_data); 148 | 149 | void 150 | component_attach_socket (component_t *component, socket_t *nicesock); 151 | 152 | candidate_t * 153 | component_find_remote_candidate(const component_t *component, 154 | const address_t *addr, IceCandidateTransport transport); 155 | 156 | 157 | void 158 | component_update_selected_pair (component_t *component, const candidate_pair_t *pair); 159 | 160 | int 161 | component_find_pair(component_t *cmp, agent_t *agent, const char *lfoundation, 162 | const char *rfoundation, candidate_pair_t *pair); 163 | 164 | candidate_t * 165 | component_set_selected_remote_candidate(agent_t *agent, 166 | component_t *component, candidate_t *candidate); 167 | 168 | void 169 | incoming_check_free(incoming_check_head_t *icheck); 170 | 171 | void 172 | ice_component_close(component_t *c); 173 | 174 | #ifdef __cplusplus 175 | } 176 | #endif 177 | 178 | #endif //_COMPONENT_H_ 179 | 180 | 181 | 182 | 183 | -------------------------------------------------------------------------------- /include/cice/conncheck.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2006-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2006-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Kai Vehmanen 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Dafydd Harries, Collabora Ltd. 26 | * Youness Alaoui, Collabora Ltd. 27 | * Kai Vehmanen, Nokia 28 | * 29 | * Alternatively, the contents of this file may be used under the terms of the 30 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 31 | * case the provisions of LGPL are applicable instead of those above. If you 32 | * wish to allow use of your version of this file only under the terms of the 33 | * LGPL and not to allow others to use your version of this file under the 34 | * MPL, indicate your decision by deleting the provisions above and replace 35 | * them with the notice and other provisions required by the LGPL. If you do 36 | * not delete the provisions above, a recipient may use your version of this 37 | * file under either the MPL or the LGPL. 38 | */ 39 | 40 | /* 41 | * Porting to C library using libevent. 42 | * Jackie Dinh - 2016 43 | */ 44 | 45 | #ifndef _ICE_CONNCHECK_H_ 46 | #define _ICE_CONNCHECK_H_ 47 | 48 | #ifdef __cplusplus 49 | extern "C" { 50 | #endif 51 | 52 | #include 53 | #include 54 | #include 55 | #include 56 | 57 | #include "cice/candidate.h" 58 | #include "cice/log.h" 59 | #include "cice/types.h" 60 | #include "cice/stun/constants.h" 61 | #include "cice/stun/usages/timer.h" 62 | #include "cice/stun/stunmessage.h" 63 | 64 | typedef enum 65 | { 66 | ICE_CHECK_WAITING = 1, 67 | ICE_CHECK_IN_PROGRESS, 68 | ICE_CHECK_SUCCEEDED, 69 | ICE_CHECK_FAILED, 70 | ICE_CHECK_FROZEN, 71 | ICE_CHECK_CANCELLED, 72 | ICE_CHECK_DISCOVERED, 73 | } IceCheckState; 74 | 75 | 76 | struct _candidate_check_pair 77 | { 78 | agent_t *agent; 79 | uint32_t stream_id; 80 | uint32_t component_id; 81 | candidate_t *local; 82 | candidate_t *remote; 83 | socket_t *sockptr; 84 | char foundation[ICE_CANDIDATE_PAIR_MAX_FOUNDATION]; 85 | IceCheckState state; 86 | int nominated; 87 | int controlling; 88 | int timer_restarted; 89 | uint64_t priority; 90 | struct timeval next_tick; 91 | uint8_t stun_buffer[STUN_MAX_MESSAGE_SIZE_IPV6]; 92 | StunTimer timer; 93 | StunMessage stun_message; 94 | 95 | TAILQ_ENTRY(_candidate_check_pair) list; 96 | }; 97 | typedef TAILQ_HEAD(_candidate_check_pair_head, _candidate_check_pair) candidate_check_pair_head_t; 98 | 99 | typedef struct { 100 | agent_t *agent; 101 | stream_t *stream; 102 | component_t *component; 103 | uint8_t *password; 104 | } conncheck_validater_data; 105 | 106 | int 107 | conn_check_add_for_local_candidate (agent_t *agent, 108 | uint32_t stream_id, component_t *component, candidate_t *local); 109 | 110 | int 111 | conn_check_add_for_candidate_pair(agent_t *agent, 112 | uint32_t stream_id, component_t *component, 113 | candidate_t *local, candidate_t *remote); 114 | 115 | int 116 | conn_check_add_for_candidate(agent_t *agent, uint32_t stream_id, 117 | component_t *component, candidate_t *remote); 118 | 119 | void 120 | conn_check_remote_candidates_set(agent_t *agent); 121 | 122 | int 123 | conn_check_schedule_next(agent_t *agent); 124 | 125 | int 126 | conn_check_handle_inbound_stun(agent_t *agent, stream_t *stream, 127 | component_t *component, socket_t *nicesock, const address_t *from, 128 | char *buf, int len); 129 | 130 | int 131 | conncheck_stun_validater(StunAgent *agent, 132 | StunMessage *message, uint8_t *username, uint16_t username_len, 133 | uint8_t **password, size_t *password_len, void *user_data); 134 | 135 | IceCandidateTransport 136 | conn_check_match_transport(IceCandidateTransport transport); 137 | 138 | size_t priv_create_username(agent_t *agent, stream_t *stream, 139 | uint32_t component_id, candidate_t *remote, candidate_t *local, 140 | uint8_t *dest, uint32_t dest_len, int inbound); 141 | 142 | size_t priv_get_password(agent_t *agent, stream_t *stream, 143 | candidate_t *remote, uint8_t **password); 144 | 145 | uint32_t peer_reflexive_candidate_priority(agent_t *agent, 146 | candidate_t *local_candidate); 147 | 148 | unsigned int 149 | priv_compute_conncheck_timer(agent_t *agent, 150 | stream_t *stream); 151 | 152 | void 153 | conn_check_prune_stream(agent_t *agent, stream_t *stream); 154 | 155 | void 156 | conn_check_free(agent_t *agent); 157 | 158 | #ifdef __cplusplus 159 | } 160 | #endif 161 | 162 | #endif //_CONNCHECK_H_ 163 | 164 | 165 | -------------------------------------------------------------------------------- /include/cice/discovery.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2006-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2006-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Kai Vehmanen 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Dafydd Harries, Collabora Ltd. 26 | * Youness Alaoui, Collabora Ltd. 27 | * Kai Vehmanen, Nokia 28 | * 29 | * Alternatively, the contents of this file may be used under the terms of the 30 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 31 | * case the provisions of LGPL are applicable instead of those above. If you 32 | * wish to allow use of your version of this file only under the terms of the 33 | * LGPL and not to allow others to use your version of this file under the 34 | * MPL, indicate your decision by deleting the provisions above and replace 35 | * them with the notice and other provisions required by the LGPL. If you do 36 | * not delete the provisions above, a recipient may use your version of this 37 | * file under either the MPL or the LGPL. 38 | */ 39 | 40 | /* 41 | * Porting to C library using libevent. 42 | * Jackie Dinh - 2016 43 | */ 44 | 45 | #ifndef _CICE_DISCOVERY_H_ 46 | #define _CICE_DISCOVERY_H_ 47 | 48 | #ifdef __cplusplus 49 | extern "C" { 50 | #endif 51 | 52 | #include "cice/candidate.h" 53 | #include "cice/types.h" 54 | #include "cice/stun/stunagent.h" 55 | #include "cice/stun/usages/timer.h" 56 | 57 | typedef enum { 58 | HOST_CANDIDATE_SUCCESS, 59 | HOST_CANDIDATE_FAILED, 60 | HOST_CANDIDATE_CANT_CREATE_SOCKET, 61 | HOST_CANDIDATE_REDUNDANT 62 | } HostCandidateResult; 63 | 64 | struct _candidate_discovery 65 | { 66 | agent_t *agent; /* back pointer to owner */ 67 | IceCandidateType type; /* candidate type STUN or TURN */ 68 | socket_t *nicesock; /* XXX: should be taken from local cand: existing socket to use */ 69 | address_t server; /* STUN/TURN server address */ 70 | struct timeval next_tick; /* next tick timestamp */ 71 | int pending; /* is discovery in progress? */ 72 | int done; /* is discovery complete? */ 73 | stream_t *stream; 74 | component_t *component; 75 | StunAgent stun_agent; 76 | StunTimer timer; 77 | uint8_t stun_buffer[STUN_MAX_MESSAGE_SIZE_IPV6]; 78 | StunMessage stun_message; 79 | uint8_t stun_resp_buffer[STUN_MAX_MESSAGE_SIZE]; 80 | StunMessage stun_resp_msg; 81 | // TurnServer *turn; 82 | TAILQ_ENTRY(_candidate_discovery) list; 83 | }; 84 | typedef TAILQ_HEAD(_candidate_discovery_head, _candidate_discovery) candidate_discovery_head_t; 85 | 86 | struct _candidate_refresh 87 | { 88 | agent_t *agent; /* back pointer to owner */ 89 | socket_t *nicesock; /* existing socket to use */ 90 | address_t server; /* STUN/TURN server address */ 91 | candidate_t *candidate; /* candidate to refresh */ 92 | stream_t *stream; 93 | component_t *component; 94 | StunAgent stun_agent; 95 | // GSource *timer_source; 96 | // GSource *tick_source; 97 | StunTimer timer; 98 | uint8_t stun_buffer[STUN_MAX_MESSAGE_SIZE_IPV6]; 99 | StunMessage stun_message; 100 | uint8_t stun_resp_buffer[STUN_MAX_MESSAGE_SIZE]; 101 | StunMessage stun_resp_msg; 102 | 103 | TAILQ_ENTRY(_candidate_refresh) list; 104 | }; 105 | typedef TAILQ_HEAD(_candidate_refresh_head, _candidate_refresh) candidate_refresh_head_t; 106 | 107 | 108 | HostCandidateResult 109 | discovery_add_local_host_candidate ( 110 | agent_t *agent, uint32_t stream_id, uint32_t component_id, 111 | address_t *address, IceCandidateTransport transport, 112 | candidate_t **outcandidate); 113 | 114 | 115 | candidate_t *discovery_learn_remote_peer_reflexive_candidate( 116 | agent_t *agent, stream_t *stream, component_t *component, 117 | uint32_t priority, const address_t *remote_address, 118 | socket_t *nicesock, candidate_t *local, candidate_t *remote); 119 | 120 | candidate_t* 121 | discovery_add_peer_reflexive_candidate( agent_t *agent, 122 | uint32_t stream_id, uint32_t component_id, 123 | address_t *address, socket_t *base_socket, 124 | candidate_t *local, candidate_t *remote); 125 | 126 | candidate_t* 127 | discovery_add_server_reflexive_candidate( 128 | agent_t *agent, uint32_t stream_id, uint32_t component_id, 129 | address_t *address, IceCandidateTransport transport, 130 | socket_t *base_socket, int nat_assisted); 131 | 132 | void 133 | discovery_discover_tcp_server_reflexive_candidates ( 134 | agent_t *agent, uint32_t stream_id, uint32_t component_id, 135 | address_t *address, socket_t *base_socket); 136 | 137 | void 138 | discovery_prune_stream(agent_t *agent, uint32_t stream_id); 139 | 140 | void 141 | refresh_prune_stream(agent_t *agent, uint32_t stream_id); 142 | 143 | void 144 | discovery_free(agent_t *agent); 145 | 146 | void 147 | refresh_free(agent_t *agent); 148 | 149 | #ifdef __cplusplus 150 | } 151 | #endif 152 | 153 | #endif //_DISCOVERY_H_ 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | -------------------------------------------------------------------------------- /include/cice/event.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Jackie Dinh 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 1 Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2 Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 3 Neither the name of the nor the 14 | * names of its contributors may be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | * 28 | * @(#)event.h 29 | */ 30 | 31 | #ifndef _CICE_EVENT_H_ 32 | #define _CICE_EVENT_H_ 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | #include "cice/agent.h" 39 | #include "cice/socket.h" 40 | #include "cice/types.h" 41 | 42 | typedef void (*event_callback_func)(int fd, short event, void *arg); 43 | 44 | typedef socket_t* (*create_socket_func)(event_ctx_t *ctx, socket_t* sock, event_callback_func cb); 45 | typedef void (*destroy_socket_func)(event_ctx_t *ctx, socket_t* sock); 46 | typedef event_info_t* (*create_event_func)(event_ctx_t *ctx, int type, event_callback_func cb, int timeout); 47 | typedef void (*destroy_event_func)(event_ctx_t *ctx, event_info_t *ev); 48 | 49 | struct _event_ctx { 50 | agent_t *agent; 51 | 52 | #ifdef USE_LIBEVENT2 53 | struct event_base *ev_base; 54 | #endif 55 | 56 | #ifdef USE_ESP32 57 | #endif 58 | 59 | create_socket_func create_socket; 60 | destroy_socket_func destroy_socket; 61 | create_event_func create_event; 62 | destroy_event_func destroy_event; 63 | }; 64 | 65 | struct _event_info { 66 | event_ctx_t *ctx; 67 | #ifdef USE_LIBEVENT2 68 | struct event *ev; 69 | event_callback_func cb; 70 | #endif 71 | 72 | #ifdef USE_ESP32 73 | #endif 74 | }; 75 | 76 | event_ctx_t* 77 | create_event_ctx(void *data); 78 | 79 | socket_t* 80 | create_socket(event_ctx_t *ctx, IceSocketType type, address_t *addr, event_callback_func cb); 81 | 82 | void 83 | destroy_socket(event_ctx_t *ctx, socket_t *sock); 84 | 85 | event_info_t* 86 | create_event_info(event_ctx_t *ctx, int type, event_callback_func cb, int timeout); 87 | 88 | void 89 | destroy_event_info(event_ctx_t *ctx, event_info_t *ev); 90 | 91 | #ifdef __cplusplus 92 | } 93 | #endif 94 | 95 | #endif //_CICE_EVENT_H_ 96 | 97 | 98 | -------------------------------------------------------------------------------- /include/cice/interfaces.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2006-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2006-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Kai Vehmanen 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Dafydd Harries, Collabora Ltd. 26 | * Youness Alaoui, Collabora Ltd. 27 | * Kai Vehmanen, Nokia 28 | * 29 | * Alternatively, the contents of this file may be used under the terms of the 30 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 31 | * case the provisions of LGPL are applicable instead of those above. If you 32 | * wish to allow use of your version of this file only under the terms of the 33 | * LGPL and not to allow others to use your version of this file under the 34 | * MPL, indicate your decision by deleting the provisions above and replace 35 | * them with the notice and other provisions required by the LGPL. If you do 36 | * not delete the provisions above, a recipient may use your version of this 37 | * file under either the MPL or the LGPL. 38 | */ 39 | 40 | /* 41 | * Porting to C library using libevent. 42 | * Jackie Dinh - 2016 43 | */ 44 | 45 | #ifndef _ICE_INTERFACES_H_ 46 | #define _ICE_INTERFACES_H_ 47 | 48 | #ifdef __cplusplus 49 | extern "C" { 50 | #endif 51 | 52 | #include 53 | #include 54 | #include 55 | 56 | #include "cice/address.h" 57 | #include "cice/log.h" 58 | 59 | int 60 | ice_interfaces_get_local_ips (address_head_t *head, int include_loopback); 61 | 62 | #ifdef __cplusplus 63 | } 64 | #endif 65 | 66 | #endif //_INTERFACES_H_ 67 | 68 | 69 | -------------------------------------------------------------------------------- /include/cice/log.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Jackie Dinh 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 1 Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2 Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 3 Neither the name of the nor the 14 | * names of its contributors may be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | * 28 | * @(#)log.h 29 | */ 30 | 31 | #ifndef _ICE_LOG_H_ 32 | #define _ICE_LOG_H_ 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | 47 | enum { 48 | ICE_LOG_INFO = 0, 49 | ICE_LOG_DEBUG = 1, 50 | ICE_LOG_WARN = 2, 51 | ICE_LOG_ERROR = 3, 52 | ICE_LOG_FATAL = 4 53 | }; 54 | 55 | /*#ifdef ICE_LIB 56 | 57 | #define FUNCLINE "%s:%u: " 58 | #define DEBUG(fmt, ...) \ 59 | { printf("[DEBUG]" FUNCLINE fmt "\n", __FUNCTION__,__LINE__, ##__VA_ARGS__); } 60 | 61 | #define ERROR(fmt, ...) \ 62 | { printf("[ERROR]" FUNCLINE fmt "\n", __FUNCTION__,__LINE__, ##__VA_ARGS__); } 63 | 64 | #else 65 | 66 | #include "../log.h" 67 | 68 | #endif*/ 69 | 70 | #define ICE_FUNCLINE "%s:%u" 71 | #define ICE_INFO(fmt, ...) \ 72 | { ice_log(ICE_LOG_INFO, ICE_FUNCLINE "[ICE_INFO]: " fmt , __FUNCTION__,__LINE__, ##__VA_ARGS__); } 73 | #define ICE_DEBUG(fmt, ...) \ 74 | { ice_log(ICE_LOG_DEBUG, ICE_FUNCLINE "[ICE_DEBUG]: " fmt, __FUNCTION__,__LINE__, ##__VA_ARGS__); } 75 | #define ICE_ERROR(fmt, ...) \ 76 | { ice_log(ICE_LOG_ERROR, ICE_FUNCLINE "[ICE_ERROR]: " fmt, __FUNCTION__,__LINE__, ##__VA_ARGS__); } 77 | 78 | 79 | typedef void (*ice_log_cb)(int severity, const char *msg, void *data); 80 | void ice_set_log_callback(ice_log_cb cb, void *data); 81 | void ice_log(int severity, const char* msg, ...); 82 | 83 | #ifdef __cplusplus 84 | } 85 | #endif 86 | 87 | #endif // _ICE_LOG_H_ 88 | 89 | 90 | -------------------------------------------------------------------------------- /include/cice/network.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2006-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2006-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Kai Vehmanen 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Dafydd Harries, Collabora Ltd. 26 | * Youness Alaoui, Collabora Ltd. 27 | * Kai Vehmanen, Nokia 28 | * 29 | * Alternatively, the contents of this file may be used under the terms of the 30 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 31 | * case the provisions of LGPL are applicable instead of those above. If you 32 | * wish to allow use of your version of this file only under the terms of the 33 | * LGPL and not to allow others to use your version of this file under the 34 | * MPL, indicate your decision by deleting the provisions above and replace 35 | * them with the notice and other provisions required by the LGPL. If you do 36 | * not delete the provisions above, a recipient may use your version of this 37 | * file under either the MPL or the LGPL. 38 | */ 39 | 40 | /* 41 | * Porting to C library using libevent. 42 | * Jackie Dinh - 2016 43 | */ 44 | 45 | #ifndef _ICE_NETWORK_H_ 46 | #define _ICE_NETWORK_H_ 47 | 48 | #ifdef __cplusplus 49 | extern "C" { 50 | #endif 51 | 52 | #include 53 | #include 54 | 55 | #include "cice/address.h" 56 | #include "cice/common.h" 57 | #include "cice/event.h" 58 | #include "cice/log.h" 59 | #include "cice/types.h" 60 | 61 | socket_t* 62 | udp_bsd_socket_new(agent_t *agent, stream_t *stream, component_t *c, address_t *addr); 63 | 64 | socket_t* 65 | tcp_active_socket_new(agent_t *agent, stream_t *stream, component_t *c, address_t *addr); 66 | 67 | socket_t* 68 | tcp_passive_socket_new(agent_t *agent, stream_t *stream, component_t *c, address_t *addr); 69 | 70 | socket_t* 71 | socket_new(IceSocketType type); 72 | 73 | void 74 | socket_free(socket_t *sock); 75 | 76 | int 77 | udp_socket_send(socket_t *sock, const address_t *to, 78 | const char *buf, size_t len); 79 | size_t 80 | tcp_passive_socket_send(socket_t *sock, const address_t *to, 81 | size_t len, const char *buf); 82 | 83 | size_t 84 | tcp_active_socket_send(socket_t *sock, const address_t *to, 85 | size_t len, const char *buf); 86 | 87 | int 88 | socket_is_reliable(socket_t *sock); 89 | 90 | #ifdef __cplusplus 91 | } 92 | #endif 93 | 94 | #endif //_NETWORK_H_ 95 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /include/cice/socket.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Jackie Dinh 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 1 Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2 Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 3 Neither the name of the nor the 14 | * names of its contributors may be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | * 28 | * @(#)socket.h 29 | */ 30 | 31 | #ifndef _CICE_SOCKET_H_ 32 | #define _CICE_SOCKET_H_ 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | #include "cice/address.h" 39 | #include "cice/types.h" 40 | 41 | typedef enum { 42 | ICE_SOCKET_TYPE_UDP_BSD, 43 | ICE_SOCKET_TYPE_TCP_BSD, 44 | ICE_SOCKET_TYPE_PSEUDOSSL, 45 | ICE_SOCKET_TYPE_HTTP, 46 | ICE_SOCKET_TYPE_SOCKS5, 47 | ICE_SOCKET_TYPE_UDP_TURN, 48 | ICE_SOCKET_TYPE_UDP_TURN_OVER_TCP, 49 | ICE_SOCKET_TYPE_TCP_ACTIVE, 50 | ICE_SOCKET_TYPE_TCP_PASSIVE, 51 | ICE_SOCKET_TYPE_TCP_SO 52 | } IceSocketType; 53 | 54 | typedef int (*recvfrom_func)(int sockfd, void *buf, size_t len, int flags, 55 | struct sockaddr *src_addr, socklen_t *addrlen); 56 | typedef int (*sendto_func)( int sockfd, const void *buf, size_t len, int flags, 57 | const struct sockaddr *dest_addr, socklen_t addrlen); 58 | typedef int (*read_func)(int fd, void *buf, size_t count); 59 | typedef int (*write_func)(int fd, const void *buf, size_t count); 60 | 61 | struct _socket { 62 | int fd; 63 | IceSocketType type; 64 | address_t addr; 65 | void *agent; 66 | void *stream; 67 | void *component; 68 | 69 | //define abstract methods 70 | recvfrom_func _recvfrom; 71 | sendto_func _sendto; 72 | read_func _read; 73 | write_func _write; 74 | 75 | #ifdef USE_LIBEVENT2 76 | /* TODO: abstract network layer 77 | * to support epoll, select, kqueue etc */ 78 | struct event *ev; 79 | struct bufferevent *bev; 80 | #endif 81 | 82 | #ifdef USE_ESP32 83 | #endif 84 | 85 | }; 86 | 87 | socket_t* 88 | socket_new(IceSocketType type); 89 | 90 | void 91 | socket_free(socket_t *sock); 92 | 93 | int 94 | socket_is_reliable(socket_t *sock); 95 | 96 | #ifdef __cplusplus 97 | } 98 | #endif 99 | 100 | #endif //_CICE_EVENT_H_ 101 | 102 | 103 | -------------------------------------------------------------------------------- /include/cice/stream.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2006-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2006-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Kai Vehmanen 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Dafydd Harries, Collabora Ltd. 26 | * Youness Alaoui, Collabora Ltd. 27 | * Kai Vehmanen, Nokia 28 | * 29 | * Alternatively, the contents of this file may be used under the terms of the 30 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 31 | * case the provisions of LGPL are applicable instead of those above. If you 32 | * wish to allow use of your version of this file only under the terms of the 33 | * LGPL and not to allow others to use your version of this file under the 34 | * MPL, indicate your decision by deleting the provisions above and replace 35 | * them with the notice and other provisions required by the LGPL. If you do 36 | * not delete the provisions above, a recipient may use your version of this 37 | * file under either the MPL or the LGPL. 38 | */ 39 | 40 | /* 41 | * Porting to C library using libevent. 42 | * Jackie Dinh - 2016 43 | */ 44 | 45 | #ifndef _ICE_STREAM_H_ 46 | #define _ICE_STREAM_H_ 47 | 48 | #ifdef __cplusplus 49 | extern "C" { 50 | #endif 51 | 52 | #include 53 | 54 | #include "cice/component.h" 55 | #include "cice/conncheck.h" 56 | #include "cice/types.h" 57 | 58 | 59 | /* Maximum and default sizes for ICE attributes, 60 | * last updated from ICE ID-19 61 | * (the below sizes include the terminating NULL): */ 62 | 63 | #define ICE_STREAM_MAX_UFRAG 256 + 1 /* ufrag + NULL */ 64 | #define ICE_STREAM_MAX_UNAME 256 * 2 + 1 + 1 /* 2*ufrag + colon + NULL */ 65 | #define ICE_STREAM_MAX_PWD 256 + 1 /* pwd + NULL */ 66 | #define ICE_STREAM_DEF_UFRAG 4 + 1 /* ufrag + NULL */ 67 | #define ICE_STREAM_DEF_PWD 22 + 1 /* pwd + NULL */ 68 | 69 | struct _stream 70 | { 71 | uint32_t id; 72 | uint32_t n_components; 73 | uint32_t tos; 74 | char *name; 75 | char local_ufrag[ICE_STREAM_MAX_UFRAG]; 76 | char local_password[ICE_STREAM_MAX_PWD]; 77 | char remote_ufrag[ICE_STREAM_MAX_UFRAG]; 78 | char remote_password[ICE_STREAM_MAX_PWD]; 79 | 80 | component_head_t components; /* list of 'Component' structs */ 81 | candidate_check_pair_head_t connchecks; /* list of CandidateCheckPair items */ 82 | 83 | /* boolean properties */ 84 | uint8_t initial_binding_request_received; 85 | uint8_t gathering; 86 | uint8_t gathering_started; 87 | 88 | TAILQ_ENTRY(_stream) list; 89 | }; 90 | typedef TAILQ_HEAD(_stream_head, _stream) stream_head_t; 91 | 92 | 93 | stream_t* 94 | stream_new(agent_t *agent, uint32_t n_components); 95 | 96 | component_t * 97 | stream_find_component_by_id(const stream_t *stream, uint32_t id); 98 | 99 | void 100 | stream_initialize_credentials(stream_t *stream/*, NiceRNG *rng*/); 101 | 102 | int 103 | stream_all_components_ready(const stream_t *stream); 104 | 105 | void 106 | ice_stream_close(stream_t *s); 107 | 108 | #ifdef __cplusplus 109 | } 110 | #endif 111 | 112 | #endif //_STREAM_H_ 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /include/cice/stun.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2006-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2006-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Kai Vehmanen 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Dafydd Harries, Collabora Ltd. 26 | * Youness Alaoui, Collabora Ltd. 27 | * Kai Vehmanen, Nokia 28 | * 29 | * Alternatively, the contents of this file may be used under the terms of the 30 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 31 | * case the provisions of LGPL are applicable instead of those above. If you 32 | * wish to allow use of your version of this file only under the terms of the 33 | * LGPL and not to allow others to use your version of this file under the 34 | * MPL, indicate your decision by deleting the provisions above and replace 35 | * them with the notice and other provisions required by the LGPL. If you do 36 | * not delete the provisions above, a recipient may use your version of this 37 | * file under either the MPL or the LGPL. 38 | */ 39 | 40 | /* 41 | * Porting to C library using libevent. 42 | * Jackie Dinh - 2016 43 | */ 44 | 45 | #ifndef _ICE_STUN_H_ 46 | #define _ICE_STUN_H_ 47 | 48 | #ifdef __cplusplus 49 | extern "C" { 50 | #endif 51 | 52 | #include 53 | #include 54 | #include 55 | 56 | #include "cice/common.h" 57 | #include "cice/log.h" 58 | 59 | size_t 60 | is_stun_message(uint8_t *buffer, int len, int has_padding); 61 | 62 | int 63 | is_validated_stun_message(uint8_t *msg, int length, int has_padding); 64 | 65 | #ifdef __cplusplus 66 | } 67 | #endif 68 | 69 | #endif //_ICE_STUN_H_ 70 | 71 | 72 | -------------------------------------------------------------------------------- /include/cice/stun/constants.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2008 Nokia Corporation. All rights reserved. 7 | * 8 | * The contents of this file are subject to the Mozilla Public License Version 9 | * 1.1 (the "License"); you may not use this file except in compliance with 10 | * the License. You may obtain a copy of the License at 11 | * http://www.mozilla.org/MPL/ 12 | * 13 | * Software distributed under the License is distributed on an "AS IS" basis, 14 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 15 | * for the specific language governing rights and limitations under the 16 | * License. 17 | * 18 | * The Original Code is the Nice GLib ICE library. 19 | * 20 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 21 | * Corporation. All Rights Reserved. 22 | * 23 | * Contributors: 24 | * Youness Alaoui, Collabora Ltd. 25 | * 26 | * Alternatively, the contents of this file may be used under the terms of the 27 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 28 | * case the provisions of LGPL are applicable instead of those above. If you 29 | * wish to allow use of your version of this file only under the terms of the 30 | * LGPL and not to allow others to use your version of this file under the 31 | * MPL, indicate your decision by deleting the provisions above and replace 32 | * them with the notice and other provisions required by the LGPL. If you do 33 | * not delete the provisions above, a recipient may use your version of this 34 | * file under either the MPL or the LGPL. 35 | */ 36 | 37 | #ifndef _STUN_CONSTANTS_H 38 | #define _STUN_CONSTANTS_H 39 | 40 | 41 | /** 42 | * SECTION:stunconstants 43 | * @short_description: STUN constants 44 | * @include: stun/constants.h 45 | * @stability: Stable 46 | * 47 | * Various constants defining parts of the STUN and TURN protocols and 48 | * on-the-wire packet formats. 49 | */ 50 | 51 | /** 52 | * STUN_ATTRIBUTE_LENGTH_LEN: 53 | * 54 | * Length of the length field of a STUN attribute (in bytes). 55 | */ 56 | /** 57 | * STUN_ATTRIBUTE_LENGTH_POS: 58 | * 59 | * Offset of the length field of a STUN attribute (in bytes). 60 | */ 61 | /** 62 | * STUN_ATTRIBUTE_TYPE_LEN: 63 | * 64 | * Length of the type field of a STUN attribute (in bytes). 65 | */ 66 | /** 67 | * STUN_ATTRIBUTE_TYPE_POS: 68 | * 69 | * Offset of the type field of a STUN attribute (in bytes). 70 | */ 71 | /** 72 | * STUN_ATTRIBUTE_VALUE_POS: 73 | * 74 | * Offset of the value field of a STUN attribute (in bytes). 75 | */ 76 | /** 77 | * STUN_ID_LEN: 78 | * 79 | * Length of the ID field of a STUN message (in bytes). 80 | */ 81 | /** 82 | * STUN_MAGIC_COOKIE: 83 | * 84 | * Magic cookie value used to identify STUN messages. 85 | */ 86 | /** 87 | * TURN_MAGIC_COOKIE: 88 | * 89 | * Magic cookie value used to identify TURN messages. 90 | */ 91 | /** 92 | * STUN_MAX_MESSAGE_SIZE_IPV4: 93 | * 94 | * Maximum size of a STUN message sent over IPv4 (in bytes). 95 | */ 96 | /** 97 | * STUN_MAX_MESSAGE_SIZE_IPV6: 98 | * 99 | * Maximum size of a STUN message sent over IPv6 (in bytes). 100 | */ 101 | /** 102 | * STUN_MESSAGE_ATTRIBUTES_POS: 103 | * 104 | * Offset of the attributes of a STUN message (in bytes). 105 | */ 106 | /** 107 | * STUN_MESSAGE_HEADER_LENGTH: 108 | * 109 | * Total length of a STUN message header (in bytes). 110 | */ 111 | /** 112 | * STUN_MESSAGE_LENGTH_LEN: 113 | * 114 | * Length of the length field of a STUN message (in bytes). 115 | */ 116 | /** 117 | * STUN_MESSAGE_LENGTH_POS: 118 | * 119 | * Offset of the length field of a STUN message (in bytes). 120 | */ 121 | /** 122 | * STUN_MESSAGE_TRANS_ID_LEN: 123 | * 124 | * Length of the transaction ID field of a STUN message (in bytes). 125 | */ 126 | /** 127 | * STUN_MESSAGE_TRANS_ID_POS: 128 | * 129 | * Offset of the transaction ID field of a STUN message (in bytes). 130 | */ 131 | /** 132 | * STUN_MESSAGE_TYPE_LEN: 133 | * 134 | * Length of the type field of a STUN message (in bytes). 135 | */ 136 | /** 137 | * STUN_MESSAGE_TYPE_POS: 138 | * 139 | * Offset of the type field of a STUN message (in bytes). 140 | */ 141 | 142 | #define STUN_MESSAGE_TYPE_POS 0 143 | #define STUN_MESSAGE_TYPE_LEN 2 144 | #define STUN_MESSAGE_LENGTH_POS \ 145 | (STUN_MESSAGE_TYPE_POS + STUN_MESSAGE_TYPE_LEN) 146 | #define STUN_MESSAGE_LENGTH_LEN 2 147 | #define STUN_MESSAGE_TRANS_ID_POS \ 148 | (STUN_MESSAGE_LENGTH_POS + STUN_MESSAGE_LENGTH_LEN) 149 | #define STUN_MESSAGE_TRANS_ID_LEN 16 150 | #define STUN_MESSAGE_ATTRIBUTES_POS \ 151 | (STUN_MESSAGE_TRANS_ID_POS + STUN_MESSAGE_TRANS_ID_LEN) 152 | 153 | #define STUN_MESSAGE_HEADER_LENGTH STUN_MESSAGE_ATTRIBUTES_POS 154 | 155 | #define STUN_ATTRIBUTE_TYPE_POS 0 156 | #define STUN_ATTRIBUTE_TYPE_LEN 2 157 | #define STUN_ATTRIBUTE_LENGTH_POS \ 158 | (STUN_ATTRIBUTE_TYPE_POS + STUN_ATTRIBUTE_TYPE_LEN) 159 | #define STUN_ATTRIBUTE_LENGTH_LEN 2 160 | #define STUN_ATTRIBUTE_VALUE_POS \ 161 | (STUN_ATTRIBUTE_LENGTH_POS + STUN_ATTRIBUTE_LENGTH_LEN) 162 | 163 | /** 164 | * STUN_ATTRIBUTE_HEADER_LENGTH: 165 | * 166 | * Length of a single STUN attribute header (in bytes). 167 | */ 168 | #define STUN_ATTRIBUTE_HEADER_LENGTH STUN_ATTRIBUTE_VALUE_POS 169 | 170 | 171 | #define STUN_MAX_MESSAGE_SIZE_IPV4 576 172 | #define STUN_MAX_MESSAGE_SIZE_IPV6 1280 173 | /* #define STUN_MAX_MESSAGE_SIZE STUN_MAX_MESSAGE_SIZE_IPV4 */ 174 | 175 | #define STUN_ID_LEN 16 176 | 177 | /** 178 | * STUN_AGENT_MAX_SAVED_IDS: 179 | * 180 | * Maximum number of simultaneously ongoing STUN transactions. 181 | */ 182 | #define STUN_AGENT_MAX_SAVED_IDS 200 183 | 184 | /** 185 | * STUN_AGENT_MAX_UNKNOWN_ATTRIBUTES: 186 | * 187 | * Maximum number of unknown attribute which can be handled in a single STUN 188 | * message. 189 | */ 190 | #define STUN_AGENT_MAX_UNKNOWN_ATTRIBUTES 256 191 | 192 | #define STUN_MAGIC_COOKIE 0x2112A442 193 | #define TURN_MAGIC_COOKIE 0x72c64bc6 194 | 195 | #ifndef TRUE 196 | #define TRUE (1 == 1) 197 | #endif 198 | 199 | #ifndef FALSE 200 | #define FALSE (0 == 1) 201 | #endif 202 | 203 | #endif /* _STUN_CONSTANTS_H */ 204 | -------------------------------------------------------------------------------- /include/cice/stun/debug.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2007 Nokia Corporation. All rights reserved. 7 | * 8 | * The contents of this file are subject to the Mozilla Public License Version 9 | * 1.1 (the "License"); you may not use this file except in compliance with 10 | * the License. You may obtain a copy of the License at 11 | * http://www.mozilla.org/MPL/ 12 | * 13 | * Software distributed under the License is distributed on an "AS IS" basis, 14 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 15 | * for the specific language governing rights and limitations under the 16 | * License. 17 | * 18 | * The Original Code is the Nice GLib ICE library. 19 | * 20 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 21 | * Corporation. All Rights Reserved. 22 | * 23 | * Contributors: 24 | * Youness Alaoui, Collabora Ltd. 25 | * 26 | * Alternatively, the contents of this file may be used under the terms of the 27 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 28 | * case the provisions of LGPL are applicable instead of those above. If you 29 | * wish to allow use of your version of this file only under the terms of the 30 | * LGPL and not to allow others to use your version of this file under the 31 | * MPL, indicate your decision by deleting the provisions above and replace 32 | * them with the notice and other provisions required by the LGPL. If you do 33 | * not delete the provisions above, a recipient may use your version of this 34 | * file under either the MPL or the LGPL. 35 | */ 36 | 37 | #ifndef STUN_DEBUG_H 38 | #define STUN_DEBUG_H 39 | 40 | #include 41 | #include 42 | #include 43 | 44 | #include "cice/log.h" 45 | 46 | #ifdef __cplusplus 47 | extern "C" { 48 | #endif 49 | 50 | 51 | /** 52 | * stun_debug_enable: 53 | * 54 | * Enable debug messages to stderr 55 | */ 56 | void stun_debug_enable (void); 57 | 58 | /** 59 | * stun_debug_disable: 60 | * 61 | * Disable debug messages to stderr 62 | */ 63 | void stun_debug_disable (void); 64 | 65 | /** 66 | * StunDebugHandler: 67 | * @format: printf()-style debug message format string 68 | * @ap: Parameters to substitute into message placeholders 69 | * 70 | * Callback for a debug message from the STUN code. 71 | */ 72 | #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)) 73 | typedef void (*StunDebugHandler) (const char *format, va_list ap) 74 | __attribute__((__format__ (__printf__, 1, 0))); 75 | #else 76 | typedef void (*StunDebugHandler) (const char *format, va_list ap); 77 | #endif 78 | 79 | /** 80 | * stun_set_debug_handler: 81 | * @handler: (nullable): Handler for STUN debug messages, or %NULL to use the 82 | * default 83 | * 84 | * Set a callback function to be invoked for each debug message from the STUN 85 | * code. The callback will only be invoked if STUN debugging is enabled using 86 | * stun_debug_enable(). 87 | * 88 | * The default callback prints the formatted debug message to stderr. 89 | */ 90 | void stun_set_debug_handler (StunDebugHandler handler); 91 | 92 | #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)) 93 | void stun_debug (const char *fmt, ...) 94 | __attribute__((__format__ (__printf__, 1, 2))); 95 | #else 96 | void stun_debug (const char *fmt, ...); 97 | #endif 98 | void stun_debug_bytes (const char *prefix, const void *data, size_t len); 99 | 100 | # ifdef __cplusplus 101 | } 102 | # endif 103 | 104 | #define FUNCLINE "%s:%u: " 105 | #define STUN_DEBUG(fmt, ...) \ 106 | { ice_log(ICE_LOG_DEBUG,"[DEBUG]" FUNCLINE fmt, __FUNCTION__,__LINE__, ##__VA_ARGS__); } 107 | 108 | 109 | #endif /* STUN_DEBUG_H */ 110 | 111 | -------------------------------------------------------------------------------- /include/cice/stun/md5.h: -------------------------------------------------------------------------------- 1 | /* 2 | * MD5 hash implementation and interface functions 3 | * Copyright (c) 2003-2005, Jouni Malinen 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * Alternatively, this software may be distributed under the terms of BSD 10 | * license. 11 | * 12 | * See README and COPYING for more details. 13 | */ 14 | 15 | #ifndef MD5_H 16 | #define MD5_H 17 | 18 | #ifdef __cplusplus 19 | extern "C" { 20 | #endif 21 | 22 | #ifdef _WIN32 23 | #include "win32_common.h" 24 | #else 25 | #include 26 | #endif 27 | #include 28 | 29 | #define MD5_MAC_LEN 16 30 | 31 | struct MD5Context { 32 | uint32_t buf[4]; 33 | uint32_t bits[2]; 34 | union { 35 | uint8_t u8[64]; 36 | uint32_t u32[16]; 37 | } in; 38 | }; 39 | 40 | typedef struct MD5Context MD5_CTX; 41 | 42 | void MD5Init(MD5_CTX *context); 43 | void MD5Update(MD5_CTX *context, unsigned char const *buf, unsigned len); 44 | void MD5Final(unsigned char digest[16], MD5_CTX *context); 45 | 46 | 47 | #ifdef __cplusplus 48 | } 49 | #endif 50 | 51 | #endif /* MD5_H */ 52 | -------------------------------------------------------------------------------- /include/cice/stun/rand.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008 Collabora Ltd. All rights reserved. 5 | * Contact: Youness Alaoui 6 | * (C) 2008 Nokia Corporation. All rights reserved. 7 | * 8 | * The contents of this file are subject to the Mozilla Public License Version 9 | * 1.1 (the "License"); you may not use this file except in compliance with 10 | * the License. You may obtain a copy of the License at 11 | * http://www.mozilla.org/MPL/ 12 | * 13 | * Software distributed under the License is distributed on an "AS IS" basis, 14 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 15 | * for the specific language governing rights and limitations under the 16 | * License. 17 | * 18 | * The Original Code is the Nice GLib ICE library. 19 | * 20 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 21 | * Corporation. All Rights Reserved. 22 | * 23 | * Contributors: 24 | * Youness Alaoui, Collabora Ltd. 25 | * 26 | * Alternatively, the contents of this file may be used under the terms of the 27 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 28 | * case the provisions of LGPL are applicable instead of those above. If you 29 | * wish to allow use of your version of this file only under the terms of the 30 | * LGPL and not to allow others to use your version of this file under the 31 | * MPL, indicate your decision by deleting the provisions above and replace 32 | * them with the notice and other provisions required by the LGPL. If you do 33 | * not delete the provisions above, a recipient may use your version of this 34 | * file under either the MPL or the LGPL. 35 | */ 36 | 37 | 38 | #ifndef RAND_H 39 | #define RAND_H 40 | 41 | #ifdef __cplusplus 42 | extern "C" { 43 | #endif 44 | 45 | #ifdef _WIN32 46 | #include "win32_common.h" 47 | #else 48 | #include 49 | #endif 50 | 51 | void nice_RAND_bytes (uint8_t *dst, int len); 52 | 53 | #ifdef __cplusplus 54 | } 55 | #endif 56 | 57 | #endif /* RAND_H */ 58 | -------------------------------------------------------------------------------- /include/cice/stun/sha1.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SHA1 hash implementation and interface functions 3 | * Copyright (c) 2003-2005, Jouni Malinen 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * Alternatively, this software may be distributed under the terms of BSD 10 | * license. 11 | * 12 | * See README and COPYING for more details. 13 | */ 14 | 15 | #ifndef SHA1_H 16 | #define SHA1_H 17 | 18 | #ifdef __cplusplus 19 | extern "C" { 20 | #endif 21 | 22 | #ifdef _WIN32 23 | #include "win32_common.h" 24 | #else 25 | #include 26 | #endif 27 | #include 28 | 29 | #define SHA1_MAC_LEN 20 30 | 31 | struct SHA1Context { 32 | uint32_t state[5]; 33 | uint32_t count[2]; 34 | unsigned char buffer[64]; 35 | }; 36 | 37 | typedef struct SHA1Context SHA1_CTX; 38 | 39 | void SHA1Init(SHA1_CTX *context); 40 | void SHA1Update(SHA1_CTX *context, const void *data, uint32_t len); 41 | void SHA1Final(unsigned char digest[20], SHA1_CTX *context); 42 | 43 | struct HMACContext { 44 | SHA1_CTX context; 45 | uint8_t key[64]; 46 | size_t key_len; 47 | }; 48 | typedef struct HMACContext HMAC_CTX; 49 | 50 | void HMACInit(HMAC_CTX *context, const uint8_t *key, size_t key_len); 51 | void HMACUpdate(HMAC_CTX *context, const void *data, uint32_t len); 52 | void HMACFinal(unsigned char digest[20], HMAC_CTX *context); 53 | 54 | void sha1_vector(size_t num_elem, const uint8_t *addr[], const size_t *len, 55 | uint8_t *mac); 56 | void hmac_sha1_vector(const uint8_t *key, size_t key_len, size_t num_elem, 57 | const uint8_t *addr[], const size_t *len, uint8_t *mac); 58 | void hmac_sha1(const uint8_t *key, size_t key_len, 59 | const uint8_t *data, size_t data_len, uint8_t *mac); 60 | void sha1_prf(const uint8_t *key, size_t key_len, const char *label, 61 | const uint8_t *data, size_t data_len, uint8_t *buf, size_t buf_len); 62 | 63 | #ifdef __cplusplus 64 | } 65 | #endif 66 | 67 | #endif /* SHA1_H */ 68 | -------------------------------------------------------------------------------- /include/cice/stun/stun5389.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2006, 2007 Collabora Ltd. 5 | * Contact: Dafydd Harries 6 | * (C) 2006, 2007 Nokia Corporation. All rights reserved. 7 | * Contact: Rémi Denis-Courmont 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Rémi Denis-Courmont, Nokia 26 | * 27 | * Alternatively, the contents of this file may be used under the terms of the 28 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 29 | * case the provisions of LGPL are applicable instead of those above. If you 30 | * wish to allow use of your version of this file only under the terms of the 31 | * LGPL and not to allow others to use your version of this file under the 32 | * MPL, indicate your decision by deleting the provisions above and replace 33 | * them with the notice and other provisions required by the LGPL. If you do 34 | * not delete the provisions above, a recipient may use your version of this 35 | * file under either the MPL or the LGPL. 36 | */ 37 | 38 | 39 | #ifndef _STUN_5389_H 40 | #define _STUN_5389_H 41 | 42 | #ifdef __cplusplus 43 | extern "C" { 44 | #endif 45 | 46 | #ifdef _WIN32 47 | #include "win32_common.h" 48 | #else 49 | # include 50 | # include 51 | #endif 52 | # include 53 | 54 | #include "cice/stun/stunmessage.h" 55 | /* 56 | * Computes the FINGERPRINT checksum of a STUN message. 57 | * @param msg pointer to the STUN message 58 | * @param len size of the message from header (inclusive) and up to 59 | * FINGERPRINT attribute (inclusive) 60 | * 61 | * @return fingerprint value in host byte order. 62 | */ 63 | uint32_t stun_fingerprint (const uint8_t *msg, size_t len, 64 | bool wlm2009_stupid_crc32_typo); 65 | 66 | StunMessageReturn stun_message_append_software (StunMessage *msg, 67 | const char *software); 68 | 69 | 70 | #ifdef __cplusplus 71 | } 72 | #endif 73 | 74 | #endif /* _STUN_5389_H */ 75 | 76 | -------------------------------------------------------------------------------- /include/cice/stun/stuncrc32.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2006-2007 Nokia Corporation. All rights reserved. 7 | * Contact: Rémi Denis-Courmont 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Youness Alaoui, Collabora Ltd. 26 | * 27 | * Alternatively, the contents of this file may be used under the terms of the 28 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 29 | * case the provisions of LGPL are applicable instead of those above. If you 30 | * wish to allow use of your version of this file only under the terms of the 31 | * LGPL and not to allow others to use your version of this file under the 32 | * MPL, indicate your decision by deleting the provisions above and replace 33 | * them with the notice and other provisions required by the LGPL. If you do 34 | * not delete the provisions above, a recipient may use your version of this 35 | * file under either the MPL or the LGPL. 36 | */ 37 | 38 | #ifndef _CRC32_H 39 | #define _CRC32_H 40 | 41 | #ifdef __cplusplus 42 | extern "C" { 43 | #endif 44 | 45 | #ifdef _WIN32 46 | #include "win32_common.h" 47 | #else 48 | #include 49 | #include 50 | #endif 51 | 52 | #include 53 | 54 | typedef struct { 55 | uint8_t *buf; 56 | size_t len; 57 | } crc_data; 58 | 59 | 60 | uint32_t stun_crc32 (const crc_data *data, size_t n, bool wlm2009_stupid_crc32_typo); 61 | 62 | #ifdef __cplusplus 63 | } 64 | #endif 65 | 66 | #endif /* _CRC32_H */ 67 | -------------------------------------------------------------------------------- /include/cice/stun/stunhmac.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2008 Nokia Corporation. All rights reserved. 7 | * 8 | * The contents of this file are subject to the Mozilla Public License Version 9 | * 1.1 (the "License"); you may not use this file except in compliance with 10 | * the License. You may obtain a copy of the License at 11 | * http://www.mozilla.org/MPL/ 12 | * 13 | * Software distributed under the License is distributed on an "AS IS" basis, 14 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 15 | * for the specific language governing rights and limitations under the 16 | * License. 17 | * 18 | * The Original Code is the Nice GLib ICE library. 19 | * 20 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 21 | * Corporation. All Rights Reserved. 22 | * 23 | * Contributors: 24 | * Youness Alaoui, Collabora Ltd. 25 | * 26 | * Alternatively, the contents of this file may be used under the terms of the 27 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 28 | * case the provisions of LGPL are applicable instead of those above. If you 29 | * wish to allow use of your version of this file only under the terms of the 30 | * LGPL and not to allow others to use your version of this file under the 31 | * MPL, indicate your decision by deleting the provisions above and replace 32 | * them with the notice and other provisions required by the LGPL. If you do 33 | * not delete the provisions above, a recipient may use your version of this 34 | * file under either the MPL or the LGPL. 35 | */ 36 | 37 | #ifndef _STUN_HMAC_H 38 | #define _STUN_HMAC_H 39 | 40 | #ifdef __cplusplus 41 | extern "C" { 42 | #endif 43 | 44 | #include "stunmessage.h" 45 | 46 | /* 47 | * Computes the MESSAGE-INTEGRITY hash of a STUN message. 48 | * @param msg pointer to the STUN message 49 | * @param len size of the message from header (inclusive) and up to 50 | * MESSAGE-INTEGRITY attribute (inclusive) 51 | * @param sha output buffer for SHA1 hash (20 bytes) 52 | * @param key HMAC key 53 | * @param keylen HMAC key bytes length 54 | * 55 | * @return fingerprint value in host byte order. 56 | */ 57 | void stun_sha1 (const uint8_t *msg, size_t len, size_t msg_len, 58 | uint8_t *sha, const void *key, size_t keylen, int padding); 59 | 60 | /* 61 | * SIP H(A1) computation 62 | */ 63 | 64 | void stun_hash_creds (const uint8_t *realm, size_t realm_len, 65 | const uint8_t *username, size_t username_len, 66 | const uint8_t *password, size_t password_len, 67 | unsigned char md5[16]); 68 | /* 69 | * Generates a pseudo-random secure STUN transaction ID. 70 | */ 71 | void stun_make_transid (StunTransactionId id); 72 | 73 | #ifdef __cplusplus 74 | } 75 | #endif 76 | 77 | #endif /* _STUN_HMAC_H */ 78 | -------------------------------------------------------------------------------- /include/cice/stun/usages/bind.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2007-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Rémi Denis-Courmont 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Youness Alaoui, Collabora Ltd. 26 | * Rémi Denis-Courmont, Nokia 27 | * 28 | * Alternatively, the contents of this file may be used under the terms of the 29 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 30 | * case the provisions of LGPL are applicable instead of those above. If you 31 | * wish to allow use of your version of this file only under the terms of the 32 | * LGPL and not to allow others to use your version of this file under the 33 | * MPL, indicate your decision by deleting the provisions above and replace 34 | * them with the notice and other provisions required by the LGPL. If you do 35 | * not delete the provisions above, a recipient may use your version of this 36 | * file under either the MPL or the LGPL. 37 | */ 38 | 39 | #ifndef STUN_BIND_H 40 | # define STUN_BIND_H 1 41 | 42 | /** 43 | * SECTION:bind 44 | * @short_description: STUN Binding Usage 45 | * @include: stun/usages/bind.h 46 | * @stability: Stable 47 | * 48 | * The STUN Binding usage allows for easily creating and parsing STUN Binding 49 | * requests and responses. It offers both an asynchronous and a synchronous API 50 | * that uses the STUN timer usage. 51 | */ 52 | 53 | 54 | #ifdef _WIN32 55 | # include "../win32_common.h" 56 | #else 57 | # include 58 | # include 59 | #endif 60 | 61 | # include "cice/stun/stunagent.h" 62 | 63 | # ifdef __cplusplus 64 | extern "C" { 65 | # endif 66 | 67 | /** 68 | * StunUsageBindReturn: 69 | * @STUN_USAGE_BIND_RETURN_SUCCESS: The binding usage succeeded 70 | * @STUN_USAGE_BIND_RETURN_ERROR: There was an unknown error in the bind usage 71 | * @STUN_USAGE_BIND_RETURN_INVALID: The message is invalid and should be ignored 72 | * @STUN_USAGE_BIND_RETURN_ALTERNATE_SERVER: The binding request has an 73 | * ALTERNATE-SERVER attribute 74 | * @STUN_USAGE_BIND_RETURN_TIMEOUT: The binding was unsuccessful because it has 75 | * timed out. 76 | * 77 | * Return value of stun_usage_bind_process() and stun_usage_bind_run() which 78 | * allows you to see what status the function call returned. 79 | */ 80 | typedef enum { 81 | STUN_USAGE_BIND_RETURN_SUCCESS, 82 | STUN_USAGE_BIND_RETURN_ERROR, 83 | STUN_USAGE_BIND_RETURN_INVALID, 84 | STUN_USAGE_BIND_RETURN_ALTERNATE_SERVER, 85 | STUN_USAGE_BIND_RETURN_TIMEOUT, 86 | } StunUsageBindReturn; 87 | 88 | 89 | /** 90 | * stun_usage_bind_create: 91 | * @agent: The #StunAgent to use to create the binding request 92 | * @msg: The #StunMessage to build 93 | * @buffer: The buffer to use for creating the #StunMessage 94 | * @buffer_len: The size of the @buffer 95 | * 96 | * Create a new STUN binding request to use with a STUN server. 97 | * Returns: The length of the built message. 98 | */ 99 | size_t stun_usage_bind_create (StunAgent *agent, StunMessage *msg, 100 | uint8_t *buffer, size_t buffer_len); 101 | 102 | /** 103 | * stun_usage_bind_process: 104 | * @msg: The #StunMessage to process 105 | * @addr: A pointer to a #sockaddr structure to fill with the mapped address 106 | * that the STUN server gives us 107 | * @addrlen: The length of @add. rMust be set to the size of the @addr socket 108 | * address and will be set to the actual length of the socket address. 109 | * @alternate_server: A pointer to a #sockaddr structure to fill with the 110 | * address of an alternate server to which we should send our new STUN 111 | * binding request, in case the currently used STUN server is requesting the use 112 | * of an alternate server. This argument will only be filled if the return value 113 | * of the function is #STUN_USAGE_BIND_RETURN_ALTERNATE_SERVER 114 | * @alternate_server_len: The length of @alternate_server. Must be set to 115 | * the size of the @alternate_server socket address and will be set to the 116 | * actual length of the socket address. 117 | * 118 | * Process a STUN binding response and extracts the mapped address from the STUN 119 | * message. Also checks for the ALTERNATE-SERVER attribute. 120 | * Returns: A #StunUsageBindReturn value. 121 | * Note that #STUN_USAGE_BIND_RETURN_TIMEOUT cannot be returned by this function 122 | */ 123 | StunUsageBindReturn stun_usage_bind_process (StunMessage *msg, 124 | struct sockaddr *addr, socklen_t *addrlen, 125 | struct sockaddr *alternate_server, socklen_t *alternate_server_len); 126 | 127 | /** 128 | * stun_usage_bind_keepalive: 129 | * @agent: The #StunAgent to use to build the message 130 | * @msg: The #StunMessage to build 131 | * @buf: The buffer to use for creating the #StunMessage 132 | * @len: The size of the @buf 133 | * 134 | * Creates a STUN binding indication that can be used for a keepalive. 135 | * Since this is an indication message, no STUN response will be generated 136 | * and it can only be used as a keepalive message. 137 | * Returns: The length of the message to send 138 | */ 139 | size_t stun_usage_bind_keepalive (StunAgent *agent, StunMessage *msg, 140 | uint8_t *buf, size_t len); 141 | 142 | /** 143 | * stun_usage_bind_run: 144 | * @srv: A pointer to the #sockaddr structure representing the STUN server's 145 | * address 146 | * @srvlen: The length of @srv 147 | * @addr: A pointer to a #sockaddr structure to fill with the mapped address 148 | * that the STUN server gives us 149 | * @addrlen: The length of @addr 150 | * 151 | * This is a convenience function that will do a synchronous Binding request to 152 | * a server and wait for its answer. It will create the socket transports and 153 | * use the #StunTimer usage to send the request and handle the response. 154 | * Returns: A #StunUsageBindReturn. 155 | * Possible return values are #STUN_USAGE_BIND_RETURN_SUCCESS, 156 | * #STUN_USAGE_BIND_RETURN_ERROR and #STUN_USAGE_BIND_RETURN_TIMEOUT 157 | */ 158 | StunUsageBindReturn stun_usage_bind_run (const struct sockaddr *srv, 159 | socklen_t srvlen, struct sockaddr_storage *addr, socklen_t *addrlen); 160 | 161 | # ifdef __cplusplus 162 | } 163 | # endif 164 | 165 | #endif 166 | -------------------------------------------------------------------------------- /include/cice/stun/usages/timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2007-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Rémi Denis-Courmont 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Youness Alaoui, Collabora Ltd. 26 | * Rémi Denis-Courmont, Nokia 27 | * 28 | * Alternatively, the contents of this file may be used under the terms of the 29 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 30 | * case the provisions of LGPL are applicable instead of those above. If you 31 | * wish to allow use of your version of this file only under the terms of the 32 | * LGPL and not to allow others to use your version of this file under the 33 | * MPL, indicate your decision by deleting the provisions above and replace 34 | * them with the notice and other provisions required by the LGPL. If you do 35 | * not delete the provisions above, a recipient may use your version of this 36 | * file under either the MPL or the LGPL. 37 | */ 38 | 39 | #ifndef STUN_TIMER_H 40 | # define STUN_TIMER_H 1 41 | 42 | /** 43 | * SECTION:timer 44 | * @short_description: STUN timer Usage 45 | * @include: stun/usages/timer.h 46 | * @stability: Stable 47 | * 48 | * The STUN timer usage is a set of helpful utility functions that allows you 49 | * to easily track when a STUN message should be retransmitted or considered 50 | * as timed out. 51 | * 52 | * 53 | 54 | Simple example on how to use the timer usage 55 | 56 | StunTimer timer; 57 | unsigned remainder; 58 | StunUsageTimerReturn ret; 59 | 60 | // Build the message, etc.. 61 | ... 62 | 63 | // Send the message and start the timer 64 | send(socket, request, sizeof(request)); 65 | stun_timer_start(&timer, STUN_TIMER_DEFAULT_TIMEOUT, 66 | STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS); 67 | 68 | // Loop until we get the response 69 | for (;;) { 70 | remainder = stun_timer_remainder(&timer); 71 | 72 | // Poll the socket until data is received or the timer expires 73 | if (poll (&pollfd, 1, delay) <= 0) { 74 | // Time out and no response was received 75 | ret = stun_timer_refresh (&timer); 76 | if (ret == STUN_USAGE_TIMER_RETURN_TIMEOUT) { 77 | // Transaction timed out 78 | break; 79 | } else if (ret == STUN_USAGE_TIMER_RETURN_RETRANSMIT) { 80 | // A retransmission is necessary 81 | send(socket, request, sizeof(request)); 82 | continue; 83 | } else if (ret == STUN_USAGE_TIMER_RETURN_SUCCESS) { 84 | // The refresh succeeded and nothing has to be done, continue polling 85 | continue; 86 | } 87 | } else { 88 | // We received a response, read it 89 | recv(socket, response, sizeof(response)); 90 | break; 91 | } 92 | } 93 | 94 | // Check if the transaction timed out or not 95 | if (ret == STUN_USAGE_TIMER_RETURN_TIMEOUT) { 96 | // do whatever needs to be done in that case 97 | } else { 98 | // Parse the response 99 | } 100 | 101 | 102 | 103 | */ 104 | 105 | #ifdef _WIN32 106 | #include 107 | #else 108 | # include 109 | # include 110 | # include 111 | #endif 112 | 113 | /** 114 | * StunTimer: 115 | * 116 | * An opaque structure representing a STUN transaction retransmission timer 117 | */ 118 | typedef struct stun_timer_s StunTimer; 119 | 120 | struct stun_timer_s { 121 | struct timeval deadline; 122 | unsigned delay; 123 | unsigned retransmissions; 124 | unsigned max_retransmissions; 125 | }; 126 | 127 | 128 | /** 129 | * STUN_TIMER_DEFAULT_TIMEOUT: 130 | * 131 | * The default intial timeout to use for the timer 132 | */ 133 | #define STUN_TIMER_DEFAULT_TIMEOUT 600 134 | 135 | /** 136 | * STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS: 137 | * 138 | * The default maximum retransmissions allowed before a timer decides to timeout 139 | */ 140 | #define STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS 3 141 | 142 | /** 143 | * STUN_TIMER_DEFAULT_RELIABLE_TIMEOUT: 144 | * 145 | * The default intial timeout to use for a reliable timer 146 | */ 147 | #define STUN_TIMER_DEFAULT_RELIABLE_TIMEOUT 7900 148 | 149 | /** 150 | * StunUsageTimerReturn: 151 | * @STUN_USAGE_TIMER_RETURN_SUCCESS: The timer was refreshed successfully 152 | * and there is nothing to be done 153 | * @STUN_USAGE_TIMER_RETURN_RETRANSMIT: The timer expired and the message 154 | * should be retransmitted now. 155 | * @STUN_USAGE_TIMER_RETURN_TIMEOUT: The timer expired as well as all the 156 | * retransmissions, the transaction timed out 157 | * 158 | * Return value of stun_usage_timer_refresh() which provides you with status 159 | * information on the timer. 160 | */ 161 | typedef enum { 162 | STUN_USAGE_TIMER_RETURN_SUCCESS, 163 | STUN_USAGE_TIMER_RETURN_RETRANSMIT, 164 | STUN_USAGE_TIMER_RETURN_TIMEOUT 165 | } StunUsageTimerReturn; 166 | 167 | # ifdef __cplusplus 168 | extern "C" { 169 | # endif 170 | 171 | 172 | /** 173 | * stun_timer_start: 174 | * @timer: The #StunTimer to start 175 | * @initial_timeout: The initial timeout to use before the first retransmission 176 | * @max_retransmissions: The maximum number of transmissions before the 177 | * #StunTimer times out 178 | * 179 | * Starts a STUN transaction retransmission timer. 180 | * This should be called as soon as you send the message for the first time on 181 | * a UDP socket. 182 | * The timeout before the next retransmission is set to @initial_timeout, then 183 | * each time a packet is retransmited, that timeout will be doubled, until the 184 | * @max_retransmissions retransmissions limit is reached. 185 | * 186 | * To determine the total timeout value, one can use the following equation : 187 | 188 | total_timeout = initial_timeout * (2^(max_retransmissions + 1) - 1); 189 | 190 | * 191 | * 192 | * See also: #STUN_TIMER_DEFAULT_TIMEOUT 193 | * 194 | * See also: #STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS 195 | */ 196 | void stun_timer_start (StunTimer *timer, unsigned int initial_timeout, 197 | unsigned int max_retransmissions); 198 | 199 | /** 200 | * stun_timer_start_reliable: 201 | * @timer: The #StunTimer to start 202 | * @initial_timeout: The initial timeout to use before the first retransmission 203 | * 204 | * Starts a STUN transaction retransmission timer for a reliable transport. 205 | * This should be called as soon as you send the message for the first time on 206 | * a TCP socket 207 | */ 208 | void stun_timer_start_reliable (StunTimer *timer, unsigned int initial_timeout); 209 | 210 | /** 211 | * stun_timer_refresh: 212 | * @timer: The #StunTimer to refresh 213 | * 214 | * Updates a STUN transaction retransmission timer. 215 | * Returns: A #StunUsageTimerReturn telling you what to do next 216 | */ 217 | StunUsageTimerReturn stun_timer_refresh (StunTimer *timer); 218 | 219 | /** 220 | * stun_timer_remainder: 221 | * @timer: The #StunTimer to query 222 | * 223 | * Query the timer on the time left before the next refresh should be done 224 | * Returns: The time remaining for the timer to expire in milliseconds 225 | */ 226 | unsigned stun_timer_remainder (const StunTimer *timer); 227 | 228 | # ifdef __cplusplus 229 | } 230 | # endif 231 | 232 | #endif /* !STUN_TIMER_H */ 233 | -------------------------------------------------------------------------------- /include/cice/stun/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2008-2009 Nokia Corporation. All rights reserved. 7 | * 8 | * The contents of this file are subject to the Mozilla Public License Version 9 | * 1.1 (the "License"); you may not use this file except in compliance with 10 | * the License. You may obtain a copy of the License at 11 | * http://www.mozilla.org/MPL/ 12 | * 13 | * Software distributed under the License is distributed on an "AS IS" basis, 14 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 15 | * for the specific language governing rights and limitations under the 16 | * License. 17 | * 18 | * The Original Code is the Nice GLib ICE library. 19 | * 20 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 21 | * Corporation. All Rights Reserved. 22 | * 23 | * Contributors: 24 | * Youness Alaoui, Collabora Ltd. 25 | * 26 | * Alternatively, the contents of this file may be used under the terms of the 27 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 28 | * case the provisions of LGPL are applicable instead of those above. If you 29 | * wish to allow use of your version of this file only under the terms of the 30 | * LGPL and not to allow others to use your version of this file under the 31 | * MPL, indicate your decision by deleting the provisions above and replace 32 | * them with the notice and other provisions required by the LGPL. If you do 33 | * not delete the provisions above, a recipient may use your version of this 34 | * file under either the MPL or the LGPL. 35 | */ 36 | 37 | #ifndef STUN_UTILS_H 38 | # define STUN_UTILS_H 1 39 | 40 | /* 41 | * @file utils.h 42 | * @brief STUN client generic utility functions 43 | */ 44 | 45 | #include "cice/stun/stunmessage.h" 46 | 47 | #ifdef _WIN32 48 | #include 49 | #include 50 | #else 51 | #include 52 | #include 53 | #include 54 | #include 55 | #endif 56 | 57 | # ifdef __cplusplus 58 | extern "C" { 59 | # endif 60 | 61 | size_t stun_padding (size_t l); 62 | 63 | size_t stun_align (size_t l); 64 | 65 | uint16_t stun_getw (const uint8_t *ptr); 66 | 67 | void *stun_setw (uint8_t *ptr, uint16_t value); 68 | 69 | void stun_set_type (uint8_t *h, StunClass c, StunMethod m); 70 | 71 | StunMessageReturn stun_xor_address (const StunMessage *msg, 72 | struct sockaddr_storage *addr, socklen_t addrlen, 73 | uint32_t magic_cookie); 74 | 75 | #define HEXDUMP(_p,len,type)\ 76 | {\ 77 | char __buf__[4*1024];\ 78 | char *p = (char*)_p;\ 79 | int i, j, _i;\ 80 | STUN_DEBUG("---- dump buffer (%s) ---- len=%lu",type,len);\ 81 | for (i = 0; i < (int)len; ) {\ 82 | memset(__buf__, sizeof(__buf__), ' ');\ 83 | sprintf(__buf__, "%5d: ", i); \ 84 | _i = i;\ 85 | for (j=0; j < 16 && i < (int)len; i++, j++)\ 86 | sprintf(__buf__ +7+j*3, "%02x ", (uint8_t)((p)[i]));\ 87 | i = _i; \ 88 | for (j=0; j < 16 && i < (int)len; i++, j++)\ 89 | sprintf(__buf__ +7+j + 48, "%c",\ 90 | isprint((p)[i]) ? (p)[i] : '.'); \ 91 | STUN_DEBUG("%s: %s", type, __buf__);\ 92 | }\ 93 | } 94 | 95 | /*#include 96 | #define FUNCLINE "%s:%u: " 97 | #define STUN_DEBUG(fmt, ...) \ 98 | { printf("[DEBUG]" FUNCLINE fmt "\n", __FUNCTION__,__LINE__, ##__VA_ARGS__); }*/ 99 | 100 | 101 | 102 | 103 | 104 | # ifdef __cplusplus 105 | } 106 | # endif 107 | 108 | #endif /* STUN_UTILS_H */ 109 | -------------------------------------------------------------------------------- /include/cice/stun/win32_common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2008-2009 Nokia Corporation. All rights reserved. 7 | * 8 | * The contents of this file are subject to the Mozilla Public License Version 9 | * 1.1 (the "License"); you may not use this file except in compliance with 10 | * the License. You may obtain a copy of the License at 11 | * http://www.mozilla.org/MPL/ 12 | * 13 | * Software distributed under the License is distributed on an "AS IS" basis, 14 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 15 | * for the specific language governing rights and limitations under the 16 | * License. 17 | * 18 | * The Original Code is the Nice GLib ICE library. 19 | * 20 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 21 | * Corporation. All Rights Reserved. 22 | * 23 | * Contributors: 24 | * Youness Alaoui, Collabora Ltd. 25 | * Danny Smith 26 | * 27 | * Alternatively, the contents of this file may be used under the terms of the 28 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 29 | * case the provisions of LGPL are applicable instead of those above. If you 30 | * wish to allow use of your version of this file only under the terms of the 31 | * LGPL and not to allow others to use your version of this file under the 32 | * MPL, indicate your decision by deleting the provisions above and replace 33 | * them with the notice and other provisions required by the LGPL. If you do 34 | * not delete the provisions above, a recipient may use your version of this 35 | * file under either the MPL or the LGPL. 36 | */ 37 | 38 | /* ISO C9x 7.18 Integer types 39 | * Based on ISO/IEC SC22/WG14 9899 Committee draft (SC22 N2794) 40 | * 41 | * THIS SOFTWARE IS NOT COPYRIGHTED 42 | * 43 | * Contributor: Danny Smith 44 | * 45 | * This source code is offered for use in the public domain. You may 46 | * use, modify or distribute it freely. 47 | * 48 | * This code is distributed in the hope that it will be useful but 49 | * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY 50 | * DISCLAIMED. This includes but is not limited to warranties of 51 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 52 | * 53 | * Date: 2000-12-02 54 | */ 55 | 56 | 57 | #ifndef _WIN32_COMMON_H 58 | #define _WIN32_COMMON_H 59 | 60 | #ifdef __cplusplus 61 | extern "C" { 62 | #endif 63 | 64 | #include "config.h" 65 | #include 66 | 67 | /* 7.18.1.1 Exact-width integer types */ 68 | typedef signed char int8_t; 69 | typedef unsigned char uint8_t; 70 | typedef short int16_t; 71 | typedef unsigned short uint16_t; 72 | typedef int int32_t; 73 | typedef unsigned uint32_t; 74 | typedef long long int64_t; 75 | typedef unsigned long long uint64_t; 76 | 77 | /* Using the [S]SIZE_T definitions from: 78 | * http://msdn.microsoft.com/en-gb/library/windows/desktop/aa383751%28v=vs.85%29.aspx#SSIZE_T */ 79 | #ifndef HAVE_SIZE_T 80 | #if defined(_WIN64) 81 | typedef unsigned __int64 size_t; 82 | #else 83 | typedef unsigned long size_t; 84 | #endif 85 | #endif /* !HAVE_SSIZE_T */ 86 | #ifndef HAVE_SSIZE_T 87 | #if defined(_WIN64) 88 | typedef signed __int64 ssize_t; 89 | #else 90 | typedef signed long ssize_t; 91 | #endif 92 | #endif /* !HAVE_SSIZE_T */ 93 | 94 | typedef uint8_t bool; 95 | #define true 1 96 | #define false 0 97 | 98 | #ifdef __cplusplus 99 | } 100 | #endif 101 | 102 | #endif /* _WIN32_COMMON_H */ 103 | -------------------------------------------------------------------------------- /include/cice/stunagent.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2006-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2006-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Kai Vehmanen 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Dafydd Harries, Collabora Ltd. 26 | * Youness Alaoui, Collabora Ltd. 27 | * Kai Vehmanen, Nokia 28 | * 29 | * Alternatively, the contents of this file may be used under the terms of the 30 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 31 | * case the provisions of LGPL are applicable instead of those above. If you 32 | * wish to allow use of your version of this file only under the terms of the 33 | * LGPL and not to allow others to use your version of this file under the 34 | * MPL, indicate your decision by deleting the provisions above and replace 35 | * them with the notice and other provisions required by the LGPL. If you do 36 | * not delete the provisions above, a recipient may use your version of this 37 | * file under either the MPL or the LGPL. 38 | */ 39 | 40 | /* 41 | * Porting to C library using libevent. 42 | * Jackie Dinh - 2016 43 | */ 44 | 45 | #ifndef _ICE_STUNAGENT_H_ 46 | #define _ICE_STUNAGENT_H_ 47 | 48 | #include 49 | #include 50 | #include 51 | 52 | struct _stun_agent { 53 | /* 54 | StunCompatibility compatibility; 55 | StunAgentSavedIds sent_ids[STUN_AGENT_MAX_SAVED_IDS]; 56 | uint16_t *known_attributes; 57 | StunAgentUsageFlags usage_flags; 58 | const char *software_attribute; 59 | */ 60 | }; 61 | 62 | 63 | #endif //_STUNAGENT_H_ 64 | 65 | 66 | -------------------------------------------------------------------------------- /include/cice/types.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Jackie Dinh 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 1 Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2 Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 3 Neither the name of the nor the 14 | * names of its contributors may be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | * 28 | * @(#)types.h 29 | */ 30 | 31 | #ifndef _ICE_TYPES_H_ 32 | #define _ICE_TYPES_H_ 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | #include 39 | #include 40 | #include 41 | 42 | #ifdef USE_ESP32 43 | #include "esp_heap_caps.h" //call heap_caps_malloc 44 | #endif 45 | 46 | #include "cice/log.h" 47 | 48 | typedef struct _stream stream_t; 49 | typedef struct _component component_t; 50 | typedef struct _agent agent_t; 51 | typedef struct _address address_t; 52 | typedef struct _socket socket_t; 53 | typedef struct _turnserver turnserver_t; 54 | typedef struct _candidate candidate_t; 55 | typedef struct _candidate_pair candidate_pair_t; 56 | typedef struct _candidate_pair_keepalive candidate_pair_keepalive_t; 57 | typedef struct _candidate_discovery candidate_discovery_t; 58 | typedef struct _candidate_refresh candidate_refresh_t; 59 | typedef struct _candidate_check_pair candidate_check_pair_t; 60 | typedef struct _incoming_check incoming_check_t; 61 | typedef struct _event_ctx event_ctx_t; 62 | typedef struct _event_info event_info_t; 63 | 64 | typedef void (*agent_recv_func) (agent_t *agent, uint32_t stream_id, 65 | uint32_t component_id, char *buf, uint32_t len, void *user_data); 66 | 67 | /* Constants for determining candidate priorities */ 68 | #define ICE_CANDIDATE_TYPE_PREF_HOST 120 69 | #define ICE_CANDIDATE_TYPE_PREF_PEER_REFLEXIVE 110 70 | #define ICE_CANDIDATE_TYPE_PREF_NAT_ASSISTED 105 71 | #define ICE_CANDIDATE_TYPE_PREF_SERVER_REFLEXIVE 100 72 | #define ICE_CANDIDATE_TYPE_PREF_UDP_TUNNELED 75 73 | #define ICE_CANDIDATE_TYPE_PREF_RELAYED 10 74 | 75 | /* Priority preference constants for MS-ICE compatibility */ 76 | #define ICE_CANDIDATE_TRANSPORT_MS_PREF_UDP 15 77 | #define ICE_CANDIDATE_TRANSPORT_MS_PREF_TCP 6 78 | #define ICE_CANDIDATE_DIRECTION_MS_PREF_PASSIVE 2 79 | #define ICE_CANDIDATE_DIRECTION_MS_PREF_ACTIVE 5 80 | 81 | /* Max foundation size, see ICE ID-19 */ 82 | #define ICE_CANDIDATE_MAX_FOUNDATION (32+1) 83 | #define ICE_CANDIDATE_PAIR_MAX_FOUNDATION ICE_CANDIDATE_MAX_FOUNDATION*2 84 | 85 | /* 86 | * A hard limit for the number of remote candidates. This 87 | * limit is enforced to protect against malevolent remote 88 | * clients. 89 | */ 90 | #define ICE_AGENT_MAX_REMOTE_CANDIDATES 25 91 | 92 | 93 | #define ICE_USE(p) (void)(p); 94 | #define ICE_FREE(p_) { if (p_!=NULL) free(p_); } 95 | #define ICE_MEMZERO(p_,type_) memset(p_,0,sizeof(type_)) 96 | 97 | #ifdef USE_ESP32 98 | #define ICE_MALLOC(type_) (type_*)heap_caps_malloc(sizeof(type_), MALLOC_CAP_32BIT) 99 | #else 100 | #define ICE_MALLOC(type_) (type_*)malloc(sizeof(type_)) 101 | #endif 102 | 103 | #define ICE_FALSE (0) 104 | #define ICE_TRUE (1) 105 | 106 | #define ICE_OK (0) 107 | #define ICE_ERR (-1) 108 | #define ICE_NULLPTR (-2) 109 | 110 | #define ICE_HEXDUMP(p,len,type)\ 111 | {\ 112 | char __buf__[4*1024];\ 113 | int i, j, _i;\ 114 | ICE_DEBUG("---- dump buffer (%s) ---- len=%d",type,len);\ 115 | for (i = 0; i < (int)len; ) {\ 116 | memset(__buf__, sizeof(__buf__), ' ');\ 117 | sprintf(__buf__, "%5d: ", i); \ 118 | _i = i;\ 119 | for (j=0; j < 16 && i < (int)len; i++, j++)\ 120 | sprintf(__buf__ +7+j*3, "%02x ", (uint8_t)((p)[i]));\ 121 | i = _i; \ 122 | for (j=0; j < 16 && i < (int)len; i++, j++)\ 123 | sprintf(__buf__ +7+j + 48, "%c",\ 124 | isprint((p)[i]) ? (p)[i] : '.'); \ 125 | ICE_DEBUG("%s: %s", type, __buf__);\ 126 | }\ 127 | } 128 | 129 | #define ICE_MAX(a, b) (((a) > (b)) ? (a) : (b)) 130 | #define ICE_MIN(a, b) (((a) < (b)) ? (a) : (b)) 131 | #define ICE_ABS(a) (((a) < 0) ? -(a) : (a)) 132 | #define ICE_CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) 133 | #define ICE_OPEN(_f,_flags) open(_f,_flags) 134 | 135 | /* An upper limit to size of STUN packets handled (based on Ethernet 136 | * MTU and estimated typical sizes of ICE STUN packet */ 137 | #define MAX_STUN_DATAGRAM_PAYLOAD 1300 138 | 139 | #ifdef USE_ESP32 140 | #define MAX_BUF_SIZE 4*1024 141 | #else 142 | #define MAX_BUF_SIZE 4*1024*1024 143 | #endif 144 | 145 | 146 | #ifdef __cplusplus 147 | } 148 | #endif 149 | 150 | #endif //_TYPES_H_ 151 | 152 | 153 | -------------------------------------------------------------------------------- /include/cice/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Jackie Dinh 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 1 Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2 Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 3 Neither the name of the nor the 14 | * names of its contributors may be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | * 28 | * @(#)utils.h 29 | */ 30 | 31 | #ifndef _ICE_UTILS_H_ 32 | #define _ICE_UTILS_H_ 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | #include 39 | #include "cice/log.h" 40 | 41 | #define MILLION_I 1000000 42 | 43 | void 44 | print_timeval(struct timeval *t); 45 | 46 | void 47 | add_microseconds_to_timeval(struct timeval *t, uint32_t microseconds); 48 | 49 | #ifdef __cplusplus 50 | } 51 | #endif 52 | 53 | #endif //_ICE_UTILS_H_ 54 | 55 | 56 | -------------------------------------------------------------------------------- /interfaces.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2006-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2006-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Kai Vehmanen 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Dafydd Harries, Collabora Ltd. 26 | * Youness Alaoui, Collabora Ltd. 27 | * Kai Vehmanen, Nokia 28 | * 29 | * Alternatively, the contents of this file may be used under the terms of the 30 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 31 | * case the provisions of LGPL are applicable instead of those above. If you 32 | * wish to allow use of your version of this file only under the terms of the 33 | * LGPL and not to allow others to use your version of this file under the 34 | * MPL, indicate your decision by deleting the provisions above and replace 35 | * them with the notice and other provisions required by the LGPL. If you do 36 | * not delete the provisions above, a recipient may use your version of this 37 | * file under either the MPL or the LGPL. 38 | */ 39 | 40 | /* 41 | * Porting to C library using libevent. 42 | * Jackie Dinh - 2016 43 | */ 44 | 45 | 46 | #include "cice/common.h" 47 | #include "cice/interfaces.h" 48 | 49 | #ifdef USE_LIBEVENT2 50 | static char * 51 | sockaddr_to_string(const struct sockaddr *addr) 52 | { 53 | char addr_as_string[INET6_ADDRSTRLEN+1] = {0}; 54 | size_t addr_len; 55 | switch (addr->sa_family) { 56 | case AF_INET: addr_len = sizeof (struct sockaddr_in); break; 57 | case AF_INET6: addr_len = sizeof (struct sockaddr_in6); break; 58 | default: return NULL; 59 | } 60 | 61 | if (getnameinfo (addr, addr_len, 62 | addr_as_string, sizeof (addr_as_string), NULL, 0, 63 | NI_NUMERICHOST) != 0) { 64 | return NULL; 65 | } 66 | return strdup(addr_as_string); 67 | } 68 | 69 | static int 70 | ice_interfaces_is_private_ip (const struct sockaddr *_sa) 71 | { 72 | union { 73 | const struct sockaddr *addr; 74 | const struct sockaddr_in *in; 75 | } sa; 76 | 77 | sa.addr = _sa; 78 | 79 | if (sa.addr->sa_family == AF_INET) { 80 | /* 10.x.x.x/8 */ 81 | if (sa.in->sin_addr.s_addr >> 24 == 0x0A) 82 | return 1; 83 | 84 | /* 172.16.0.0 - 172.31.255.255 = 172.16.0.0/10 */ 85 | if (sa.in->sin_addr.s_addr >> 20 == 0xAC1) 86 | return 1; 87 | 88 | /* 192.168.x.x/16 */ 89 | if (sa.in->sin_addr.s_addr >> 16 == 0xC0A8) 90 | return 1; 91 | 92 | /* 169.254.x.x/16 (for APIPA) */ 93 | if (sa.in->sin_addr.s_addr >> 16 == 0xA9FE) 94 | return 1; 95 | } 96 | 97 | return 0; 98 | } 99 | 100 | int 101 | ice_interfaces_get_local_ips (address_head_t *head, int include_loopback) 102 | { 103 | struct ifaddrs *ifa, *results; 104 | 105 | if ( head == NULL ) 106 | return -1; 107 | 108 | if (getifaddrs (&results) < 0) 109 | return -1; 110 | 111 | /* Loop through the interface list and get the IP address of each IF */ 112 | for (ifa = results; ifa; ifa = ifa->ifa_next) { 113 | char *addr_string = NULL; 114 | 115 | if ( ifa->ifa_addr->sa_family==AF_INET6 ) 116 | continue; 117 | 118 | /* no ip address from interface that is down */ 119 | if ((ifa->ifa_flags & IFF_UP) == 0) 120 | continue; 121 | 122 | if (ifa->ifa_addr == NULL) 123 | continue; 124 | 125 | /* Convert to a string. */ 126 | addr_string = sockaddr_to_string (ifa->ifa_addr); 127 | if (addr_string == NULL) { 128 | ICE_ERROR("Failed to convert address to string for interface, ifa_name:%s", 129 | ifa->ifa_name); 130 | continue; 131 | } 132 | 133 | ICE_DEBUG("Interface: %s", ifa->ifa_name); 134 | ICE_DEBUG("IP Address: %s", addr_string); 135 | 136 | if ((ifa->ifa_flags & IFF_LOOPBACK) == IFF_LOOPBACK) { 137 | if (include_loopback) { 138 | //loopbacks = add_ip_to_list (loopbacks, addr_string, TRUE); 139 | ICE_DEBUG("FIXME: loopback interface"); 140 | } else { 141 | ICE_DEBUG("Ignoring loopback interface"); 142 | } 143 | } else { 144 | ICE_DEBUG("FIXME: order of ip addresses is important?"); 145 | ice_interfaces_is_private_ip(ifa->ifa_addr); 146 | /*if (ice_interfaces_is_private_ip (ifa->ifa_addr)) 147 | ips = add_ip_to_list (ips, addr_string, TRUE); 148 | else 149 | ips = add_ip_to_list (ips, addr_string, FALSE);*/ 150 | address_t *addr = address_new(); 151 | if (address_set_from_string(addr, addr_string) == ICE_OK) { 152 | ICE_DEBUG("add local address, addr=%s",addr_string); 153 | TAILQ_INSERT_HEAD(head,addr,list); 154 | } else { 155 | ICE_ERROR("failed to parse local address, addr=%s",addr_string); 156 | address_free(addr); 157 | } 158 | } 159 | 160 | if ( addr_string != NULL ) 161 | free(addr_string); 162 | } 163 | 164 | freeifaddrs (results); 165 | 166 | //if (loopbacks) 167 | // ips = g_list_concat (ips, loopbacks); 168 | //return ips; 169 | 170 | return 0; 171 | } 172 | #endif //ESP32 173 | 174 | #ifdef USE_ESP32 175 | int 176 | ice_interfaces_get_local_ips (address_head_t *head, int include_loopback) 177 | { 178 | address_t *addr = NULL; 179 | char *addr_string = NULL; 180 | tcpip_adapter_ip_info_t ip_info; 181 | 182 | if (head == NULL) 183 | return -1; 184 | 185 | ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info)); 186 | ICE_DEBUG("IP Address: %s\n", ip4addr_ntoa(&ip_info.ip)); 187 | ICE_DEBUG("Subnet mask: %s\n", ip4addr_ntoa(&ip_info.netmask)); 188 | ICE_DEBUG("Gateway: %s\n", ip4addr_ntoa(&ip_info.gw)); 189 | 190 | addr_string = strdup(ip4addr_ntoa(&ip_info.ip)); 191 | ICE_DEBUG("IP Address: %s", addr_string); 192 | 193 | addr = address_new(); 194 | if (address_set_from_string(addr, addr_string) == ICE_OK) { 195 | ICE_DEBUG("add local address, addr=%s",addr_string); 196 | TAILQ_INSERT_HEAD(head,addr,list); 197 | } else { 198 | ICE_ERROR("failed to parse local address, addr=%s",addr_string); 199 | address_free(addr); 200 | } 201 | 202 | if (addr_string != NULL) 203 | free(addr_string); 204 | 205 | return 0; //FIXME: return list head. 206 | } 207 | #endif //ESP32 208 | 209 | -------------------------------------------------------------------------------- /log.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015 Jackie Dinh 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 1 Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2 Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 3 Neither the name of the nor the 14 | * names of its contributors may be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | * 28 | * @(#)log.c 29 | */ 30 | 31 | 32 | #define _WITH_DPRINTF 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | #include "cice/log.h" 40 | #include "cice/types.h" 41 | 42 | int g_verbose = 1; 43 | int g_log_fd = -1; 44 | int g_log_level = ICE_LOG_WARN; 45 | ice_log_cb g_ice_log_cb = NULL; 46 | void *g_ice_log_data = NULL; 47 | 48 | void 49 | log_init(char* file, int level) 50 | { 51 | g_ice_log_cb = NULL; 52 | if ( level >= ICE_LOG_INFO && level <= ICE_LOG_FATAL ) 53 | g_log_level = level; 54 | else 55 | g_log_level = ICE_LOG_ERROR; 56 | 57 | if (file) { 58 | g_log_fd = ICE_OPEN(file, O_WRONLY|O_CREAT); 59 | if (g_log_fd < 0) { 60 | fprintf(stderr,"can not open log file, ret=%u\n",g_log_fd); 61 | } 62 | } 63 | 64 | return; 65 | } 66 | 67 | void 68 | ice_set_log_callback(ice_log_cb cb, void* data) { 69 | g_ice_log_cb = cb; 70 | g_ice_log_data = data; 71 | return; 72 | } 73 | 74 | void 75 | ice_log_internal(int severity, const char *msg) { 76 | if (g_ice_log_cb) { 77 | g_ice_log_cb(severity,msg,g_ice_log_data); 78 | return; 79 | } 80 | 81 | //if defing log file, write msg to the file. 82 | 83 | return; 84 | } 85 | 86 | void 87 | ice_log(int severity, const char *fmt, ...) { 88 | static char buffer[4*1024] = {0}; 89 | va_list argptr; 90 | 91 | if ( g_log_level > severity || !g_ice_log_cb) 92 | return; 93 | 94 | va_start(argptr, fmt); 95 | vsnprintf(buffer, 4*1024, fmt, argptr); 96 | va_end(argptr); 97 | ice_log_internal(severity,buffer); 98 | return; 99 | } 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /network.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2006-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2006-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Kai Vehmanen 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Dafydd Harries, Collabora Ltd. 26 | * Youness Alaoui, Collabora Ltd. 27 | * Kai Vehmanen, Nokia 28 | * 29 | * Alternatively, the contents of this file may be used under the terms of the 30 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 31 | * case the provisions of LGPL are applicable instead of those above. If you 32 | * wish to allow use of your version of this file only under the terms of the 33 | * LGPL and not to allow others to use your version of this file under the 34 | * MPL, indicate your decision by deleting the provisions above and replace 35 | * them with the notice and other provisions required by the LGPL. If you do 36 | * not delete the provisions above, a recipient may use your version of this 37 | * file under either the MPL or the LGPL. 38 | */ 39 | 40 | /* 41 | * Porting to C library using libevent. 42 | * Jackie Dinh - 2016 43 | */ 44 | 45 | 46 | #include 47 | #include 48 | #include 49 | 50 | #include "cice/address.h" 51 | #include "cice/agent.h" 52 | #include "cice/network.h" 53 | #include "cice/socket.h" 54 | #include "cice/stun.h" 55 | 56 | int 57 | stun_recv_message(socket_t *sock, address_t* from, char *buf, int len) { 58 | agent_t *agent; 59 | stream_t *stream; 60 | component_t *component; 61 | //address_t *addr; 62 | int ret = ICE_ERR; 63 | 64 | if ( sock == NULL || from == NULL ) { 65 | ICE_ERROR("socket pointer is null"); 66 | return ICE_ERR; 67 | } 68 | agent = (agent_t*)sock->agent; 69 | stream = (stream_t*)sock->stream; 70 | component = (component_t*)sock->component; 71 | 72 | if ( agent == NULL || stream == NULL || component == NULL ) { 73 | ICE_ERROR("agent pointer is null"); 74 | return ICE_ERR; 75 | } 76 | 77 | //1. check turn server candidate 78 | 79 | //2. dispatch msg 80 | agent->media_after_tick = ICE_TRUE; 81 | if ( is_validated_stun_message((uint8_t*)buf,len,1) > 0 ) { 82 | //2.1 handle inbound stun message 83 | //ICE_DEBUG("Handle inbound stun message"); 84 | ret = conn_check_handle_inbound_stun(agent, stream, component, sock, from, buf, len); 85 | 86 | } else { 87 | //2.2 handle other data 88 | //ICE_DEBUG("Handle non-stun message"); 89 | //HEXDUMP(buf,len,"msg"); 90 | if ( component->io_callback ) { 91 | //ICE_DEBUG("call user-defined callback"); 92 | //typedef void (*agent_recv_func) (agent_t *agent, uint32_t stream_id, 93 | // uint32_t component_id, char *buf, uint32_t len, void *user_data); 94 | component->io_callback(agent,stream->id,component->id, buf, len, component->io_data); 95 | } 96 | ret = ICE_OK; 97 | } 98 | 99 | //3. unhandled stun message 100 | 101 | return ret; 102 | } 103 | 104 | static void 105 | socket_udp_read_cb(int fd, short what, void *ctx) 106 | { 107 | static char buf[MAX_BUF_SIZE] = {0}; 108 | socket_t *sock = (socket_t*)ctx;; 109 | struct sockaddr_in remaddr; 110 | address_t fromaddr; 111 | socklen_t addrlen; 112 | size_t recvlen; 113 | int ret; 114 | 115 | memset(&remaddr, 0, sizeof(remaddr)); 116 | addrlen = sizeof(remaddr); 117 | 118 | //recvlen = recvfrom(fd, buf, MAX_BUF_SIZE, 0, (struct sockaddr*)&remaddr, &addrlen); 119 | recvlen = sock->_recvfrom(fd, buf, MAX_BUF_SIZE, 0, (struct sockaddr*)&remaddr, &addrlen); 120 | 121 | ICE_DEBUG("Receive data, fd=%u, ip=%u, port=%u, recvlen: %ld", 122 | fd, remaddr.sin_addr.s_addr, ntohs(remaddr.sin_port), recvlen); 123 | if (recvlen <= 0) { 124 | ICE_ERROR("could not receive data, ret=%ld",recvlen); 125 | return; 126 | } 127 | 128 | //ICE_HEXDUMP(buf,recvlen,"udp"); 129 | ICE_DEBUG("get udp socket, agent=%p,stream=%p,component=%p", 130 | sock->agent,sock->stream,sock->component); 131 | memset(&fromaddr, 0, sizeof(fromaddr)); 132 | address_set_from_sockaddr(&fromaddr,(const struct sockaddr*)&remaddr); 133 | address_set_port(&fromaddr,ntohs(remaddr.sin_port)); 134 | print_address(&fromaddr); 135 | 136 | //FIXME: check order of udp packets or force no-udp-fragment? 137 | if (is_stun_message((uint8_t*)buf,recvlen,1) > 0) { 138 | ret = stun_recv_message(sock,&fromaddr,buf,recvlen); 139 | if ( ret < 0 ) { 140 | ICE_ERROR("failed to recv stun msg"); 141 | } 142 | } else { 143 | 144 | agent_t *agent = (agent_t*)sock->agent; 145 | stream_t *s = (stream_t*)sock->stream; 146 | component_t *c = (component_t*)sock->component; 147 | if (c->io_callback) { 148 | //ice_data_recv_cb 149 | c->io_callback(agent,s->id,c->id,buf,recvlen,c->io_data); 150 | } else { 151 | ICE_ERROR("no io callback"); 152 | } 153 | 154 | } 155 | 156 | return; 157 | } 158 | 159 | //FIXME: tcp data read cb 160 | /* 161 | static void 162 | socket_tcp_read_cb(struct bufferevent *bev, void *ctx) { 163 | static char data[4*1024]={0}; 164 | struct evbuffer *input_bev; 165 | agent_t *agent = (agent_t*)ctx; 166 | size_t recv_input_len; 167 | 168 | ICE_DEBUG("socket_udp_read_cb"); 169 | 170 | if ( bev == 0) { 171 | return; 172 | } 173 | 174 | input_bev = bufferevent_get_input(bev); 175 | recv_input_len = evbuffer_get_length(input_bev); 176 | evbuffer_remove(input_bev, data, recv_input_len); 177 | 178 | printf("buffer, agent=%p, len=%lu,data=%s\n",agent,recv_input_len,data); 179 | 180 | 181 | } 182 | 183 | static 184 | void socket_event_cb(struct bufferevent *bev, short events, void *ctx) 185 | { 186 | if (events & BEV_EVENT_ERROR) { 187 | ICE_ERROR("buffer error, events=%u\n",events); 188 | return; 189 | } 190 | 191 | if (events & ( BEV_EVENT_EOF |BEV_EVENT_ERROR) ) { 192 | ICE_DEBUG("buffer end, events=%u\n",events); 193 | } 194 | 195 | return; 196 | } 197 | */ 198 | 199 | socket_t* 200 | udp_bsd_socket_new(agent_t *agent, stream_t *stream, component_t *component, address_t *addr) { 201 | socket_t *sock = NULL; 202 | 203 | sock = create_socket(agent->base,ICE_SOCKET_TYPE_UDP_BSD, addr, socket_udp_read_cb); 204 | if (sock == NULL) 205 | return NULL; 206 | sock->agent = agent; 207 | sock->stream = stream; 208 | sock->component = component; 209 | 210 | ICE_DEBUG("create udp socket, fd=%u, agent=%p,stream=%p,component=%p", 211 | sock->fd, agent,stream,component); 212 | 213 | return sock; 214 | } 215 | 216 | socket_t* 217 | tcp_active_socket_new(agent_t *agent, stream_t *stream, 218 | component_t *component, address_t *addr) { 219 | /* 220 | socket_t *sock = socket_new(ICE_SOCKET_TYPE_TCP_ACTIVE); 221 | struct bufferevent *bev; 222 | int fd = 0; 223 | 224 | if ( sock == NULL || agent == NULL ) 225 | return NULL; 226 | 227 | 228 | sock->addr = *addr; 229 | 230 | //agent->base->create_event(base,sock, socket_udp_read_cb, write_cb, event_cb); 231 | fd = socket(AF_INET,SOCK_STREAM,0); 232 | evutil_make_socket_nonblocking(fd); 233 | bev = bufferevent_socket_new(agent->base, fd, BEV_OPT_CLOSE_ON_FREE); 234 | bufferevent_setcb(bev, socket_tcp_read_cb, NULL, socket_event_cb, agent); 235 | 236 | sock->bev = bev; 237 | return sock; 238 | */ 239 | return 0; 240 | } 241 | 242 | socket_t* 243 | tcp_passive_socket_new(agent_t *agent, stream_t *stream, 244 | component_t *component, address_t *addr) { 245 | socket_t *sock = socket_new(ICE_SOCKET_TYPE_TCP_PASSIVE); 246 | 247 | if ( sock == NULL ) 248 | return NULL; 249 | 250 | sock->addr = *addr; 251 | 252 | /* FIXME: setup io callbacks */ 253 | 254 | return sock; 255 | } 256 | 257 | int 258 | udp_socket_send(socket_t *sock, const address_t *to, 259 | const char *buf, size_t len) { 260 | int n; 261 | 262 | if (sock == NULL || to == NULL) { 263 | ICE_ERROR("null pointer"); 264 | return ICE_ERR; 265 | } 266 | 267 | n = sock->_sendto(sock->fd, buf, len, 0, &to->s.addr, get_address_length(to)); 268 | if (n < 0) { 269 | ICE_ERROR("sendto error, ret=%d, fd=%d, len=%lu,",n,sock->fd,len); 270 | return ICE_ERR; 271 | } 272 | 273 | return n; 274 | } 275 | 276 | size_t 277 | tcp_passive_socket_send(socket_t *sock, const address_t *to, 278 | size_t len, const char *buf) 279 | { 280 | //socket_send_messages 281 | ICE_DEBUG("FIXME: tcp_pass_socket_send"); 282 | return 0; 283 | } 284 | 285 | size_t 286 | tcp_active_socket_send(socket_t *sock, const address_t *to, 287 | size_t len, const char *buf) 288 | { 289 | //socket_send_messages 290 | ICE_DEBUG("FIXME: tcp_active_socket_send"); 291 | return 0; 292 | } 293 | 294 | 295 | 296 | -------------------------------------------------------------------------------- /socket.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Jackie Dinh 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 1 Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2 Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 3 Neither the name of the nor the 14 | * names of its contributors may be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | * 28 | * @(#)socket.c 29 | */ 30 | 31 | 32 | #include 33 | #include 34 | 35 | #include "cice/socket.h" 36 | 37 | typedef int (*recvfrom_func)(int sockfd, void *buf, size_t len, int flags, 38 | struct sockaddr *src_addr, socklen_t *addrlen); 39 | typedef int (*sendto_func)( int sockfd, const void *buf, size_t len, int flags, 40 | const struct sockaddr *dest_addr, socklen_t addrlen); 41 | typedef int (*read_func)(int fd, void *buf, size_t count); 42 | typedef int (*write_func)(int fd, const void *buf, size_t count); 43 | 44 | #ifdef USE_LIBEVENT2 45 | int libevent2_recvfrom(int fd, void *buf, size_t len, int flags, 46 | struct sockaddr *src_addr, socklen_t *addrlen) { 47 | return recvfrom(fd, buf, len, 0, src_addr, addrlen); 48 | } 49 | 50 | int libevent2_sendto(int fd, const void *buf, size_t len, int flags, 51 | const struct sockaddr *dest_addr, socklen_t addrlen) { 52 | return sendto(fd, buf, len, 0, dest_addr, addrlen); 53 | } 54 | 55 | int libevent2_read(int fd, void *buf, size_t count) { 56 | 57 | return 0; 58 | } 59 | 60 | int libevent2_write(int fd, const void *buf, size_t count) { 61 | 62 | return 0; 63 | } 64 | 65 | #endif //USE_LIBEVENT2 66 | 67 | #ifdef USE_ESP32 68 | int esp32_recvfrom(int fd, void *buf, size_t len, int flags, 69 | struct sockaddr *src_addr, socklen_t *addrlen) { 70 | //FIXME: change it to FreeRTOS api 71 | return recvfrom(fd, buf, len, 0, src_addr, addrlen); 72 | } 73 | 74 | int esp32_sendto(int fd, const void *buf, size_t len, int flags, 75 | const struct sockaddr *dest_addr, socklen_t addrlen) { 76 | return sendto(fd, buf, len, 0, dest_addr, addrlen); 77 | } 78 | 79 | int esp32_read(int fd, void *buf, size_t count) { 80 | 81 | return 0; 82 | } 83 | 84 | int esp32_write(int fd, const void *buf, size_t count) { 85 | 86 | return 0; 87 | } 88 | 89 | #endif //USE_ESP32 90 | 91 | socket_t* 92 | socket_new(IceSocketType type) { 93 | socket_t *sock; 94 | 95 | sock = ICE_MALLOC(socket_t); 96 | if (sock == NULL) 97 | return NULL; 98 | 99 | ICE_MEMZERO(sock,socket_t); 100 | sock->type = type; 101 | 102 | #ifdef USE_LIBEVENT2 103 | sock->_recvfrom = libevent2_recvfrom; 104 | sock->_sendto = libevent2_sendto; 105 | sock->_read = libevent2_read; 106 | sock->_write = libevent2_write; 107 | #endif //USE_LIBEVENT2 108 | 109 | #ifdef USE_ESP32 110 | sock->_recvfrom = esp32_recvfrom; 111 | sock->_sendto = esp32_sendto; 112 | sock->_read = esp32_read; 113 | sock->_write = esp32_write; 114 | #endif //USE_ESP32 115 | 116 | return sock; 117 | } 118 | 119 | 120 | void 121 | socket_free(socket_t *sock) 122 | { 123 | if (!sock) return; 124 | 125 | //FIXME: socket_free should be replaced by destroy_socket. 126 | /*if (sock->type == ICE_SOCKET_TYPE_UDP_BSD) { 127 | event_del(sock->ev); 128 | }*/ 129 | 130 | close(sock->fd); 131 | ICE_FREE(sock); 132 | 133 | return; 134 | } 135 | 136 | 137 | int 138 | socket_is_reliable(socket_t *sock) 139 | { 140 | //FIXME: have different types of socket_t 141 | return 0; 142 | } 143 | 144 | 145 | 146 | 147 | -------------------------------------------------------------------------------- /stream.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2006-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2006-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Kai Vehmanen 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Dafydd Harries, Collabora Ltd. 26 | * Youness Alaoui, Collabora Ltd. 27 | * Kai Vehmanen, Nokia 28 | * 29 | * Alternatively, the contents of this file may be used under the terms of the 30 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 31 | * case the provisions of LGPL are applicable instead of those above. If you 32 | * wish to allow use of your version of this file only under the terms of the 33 | * LGPL and not to allow others to use your version of this file under the 34 | * MPL, indicate your decision by deleting the provisions above and replace 35 | * them with the notice and other provisions required by the LGPL. If you do 36 | * not delete the provisions above, a recipient may use your version of this 37 | * file under either the MPL or the LGPL. 38 | */ 39 | 40 | /* 41 | * Porting to C library using libevent. 42 | * Jackie Dinh - 2016 43 | */ 44 | 45 | #include "cice/stream.h" 46 | #include "cice/types.h" 47 | 48 | void 49 | stream_initialize_credentials (stream_t *stream/*, NiceRNG *rng*/) 50 | { 51 | static const char *uname = "RObomhjs7tw7kmzf"; 52 | static const char *pwd = "jVvUZXC05jO8vi2aqzb7Lerv"; 53 | /* note: generate ufrag/pwd for the stream (see ICE 15.4. 54 | * '"ice-ufrag" and "ice-pwd" Attributes', ID-19) */ 55 | ICE_DEBUG("FIXME: generate ufrag/pwd for a stream"); 56 | memcpy(stream->local_ufrag,uname,strlen(uname)); 57 | memcpy(stream->local_password,pwd,strlen(pwd)); 58 | //nice_rng_generate_bytes_print (rng, NICE_STREAM_DEF_UFRAG - 1, stream->local_ufrag); 59 | //nice_rng_generate_bytes_print (rng, NICE_STREAM_DEF_PWD - 1, stream->local_password); 60 | } 61 | 62 | stream_t* 63 | stream_new (agent_t *agent, uint32_t n_components) 64 | { 65 | stream_t *stream; 66 | uint32_t n; 67 | component_t *component; 68 | 69 | if (agent == 0 ) 70 | return 0; 71 | 72 | stream = ICE_MALLOC(stream_t); 73 | if ( stream == 0 ) 74 | return 0; 75 | ICE_MEMZERO(stream,stream_t); 76 | 77 | ICE_DEBUG("create new stream, stream=%p, n_components=%u", stream, n_components); 78 | 79 | TAILQ_INIT(&stream->components); 80 | TAILQ_INIT(&stream->connchecks); 81 | 82 | for (n = 0; n < n_components; n++) { 83 | component = component_new (agent, stream, n + 1); 84 | TAILQ_INSERT_HEAD(&stream->components,component,list); 85 | } 86 | 87 | stream->n_components = n_components; 88 | stream->initial_binding_request_received = 0; 89 | stream->gathering_started = 0; 90 | 91 | return stream; 92 | } 93 | 94 | component_t * 95 | stream_find_component_by_id (const stream_t *stream, uint32_t id) 96 | { 97 | component_t *c; 98 | 99 | if (stream == NULL ) 100 | return NULL; 101 | 102 | TAILQ_FOREACH(c,&stream->components,list) { 103 | //ICE_DEBUG("search component, component_id=%u,search_id=%u",c->id,id); 104 | if ( c->id == id ) 105 | return c; 106 | } 107 | 108 | return NULL; 109 | } 110 | 111 | /* 112 | * Returns true if all components of the stream are either 113 | * 'CONNECTED' or 'READY' (connected plus nominated). 114 | */ 115 | int 116 | stream_all_components_ready(const stream_t *stream) 117 | { 118 | component_t *component = NULL; 119 | 120 | TAILQ_FOREACH(component,&stream->components,list) { 121 | if ( component && 122 | !(component->state == ICE_COMPONENT_STATE_CONNECTED || 123 | component->state == ICE_COMPONENT_STATE_READY)) 124 | return ICE_ERR; 125 | } 126 | 127 | return ICE_OK; 128 | } 129 | 130 | 131 | void 132 | ice_stream_close(stream_t *s) { 133 | component_t *c = NULL; 134 | 135 | while (!TAILQ_EMPTY(&s->components)) { 136 | c = TAILQ_FIRST(&s->components); 137 | TAILQ_REMOVE(&s->components, c, list); 138 | ice_component_close(c); 139 | } 140 | 141 | return; 142 | } 143 | 144 | 145 | 146 | -------------------------------------------------------------------------------- /stun.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2006-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2006-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Kai Vehmanen 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Dafydd Harries, Collabora Ltd. 26 | * Youness Alaoui, Collabora Ltd. 27 | * Kai Vehmanen, Nokia 28 | * 29 | * Alternatively, the contents of this file may be used under the terms of the 30 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 31 | * case the provisions of LGPL are applicable instead of those above. If you 32 | * wish to allow use of your version of this file only under the terms of the 33 | * LGPL and not to allow others to use your version of this file under the 34 | * MPL, indicate your decision by deleting the provisions above and replace 35 | * them with the notice and other provisions required by the LGPL. If you do 36 | * not delete the provisions above, a recipient may use your version of this 37 | * file under either the MPL or the LGPL. 38 | */ 39 | 40 | /* 41 | * Porting to C library using libevent. 42 | * Jackie Dinh - 2016 43 | */ 44 | 45 | #include "cice/stun.h" 46 | #include "cice/stun/constants.h" 47 | #include "cice/stun/stunmessage.h" 48 | #include "cice/stun/utils.h" 49 | 50 | size_t 51 | is_stun_message(uint8_t *buffer, int len, int has_padding) { 52 | int mlen; 53 | 54 | //ICE_DEBUG("STUN info, has_padding=%u",has_padding); 55 | if (len < 1 || buffer == NULL) { 56 | ICE_ERROR("STUN error: No data!"); 57 | return STUN_MESSAGE_BUFFER_INVALID; 58 | } 59 | 60 | if (buffer[0] >> 6) { 61 | ICE_DEBUG("STUN error: RTP or other non-protocol packet!"); 62 | return STUN_MESSAGE_BUFFER_INVALID; // RTP or other non-STUN packet 63 | } 64 | 65 | if (len < STUN_MESSAGE_LENGTH_POS + STUN_MESSAGE_LENGTH_LEN) { 66 | ICE_DEBUG("STUN error: Incomplete STUN message header!"); 67 | return STUN_MESSAGE_BUFFER_INCOMPLETE; 68 | } 69 | 70 | mlen = stun_getw((uint8_t*)buffer + STUN_MESSAGE_LENGTH_POS); 71 | mlen += STUN_MESSAGE_HEADER_LENGTH; 72 | 73 | if (has_padding && stun_padding (mlen)) { 74 | ICE_DEBUG("STUN error: Invalid message length: %u!", (unsigned)mlen); 75 | return STUN_MESSAGE_BUFFER_INVALID; // wrong padding 76 | } 77 | 78 | if (len < mlen) { 79 | ICE_DEBUG("STUN error: Incomplete message: %u of %u bytes!", 80 | (unsigned) len, (unsigned) mlen); 81 | return STUN_MESSAGE_BUFFER_INCOMPLETE; // partial message 82 | } 83 | 84 | return mlen; 85 | } 86 | 87 | int 88 | is_validated_stun_message(uint8_t *msg, int length, int has_padding) { 89 | ssize_t fast_retval; 90 | size_t mlen; 91 | size_t len; 92 | 93 | //FIXME: pre-check is really needed? 94 | fast_retval = is_stun_message(msg,length,has_padding); 95 | 96 | if ( fast_retval <= 0 ) 97 | return fast_retval; 98 | 99 | mlen = fast_retval; 100 | 101 | /* Skip past the header (validated above). */ 102 | msg += 20; 103 | len = mlen - 20; 104 | 105 | /* from then on, we know we have the entire packet in buffer */ 106 | while (len > 0) 107 | { 108 | size_t alen; 109 | 110 | if (len < 4) 111 | { 112 | ICE_DEBUG("STUN error: Incomplete STUN attribute header of length " 113 | "%u bytes!", (unsigned)len); 114 | return STUN_MESSAGE_BUFFER_INVALID; 115 | } 116 | 117 | alen = stun_getw (msg + STUN_ATTRIBUTE_TYPE_LEN); 118 | if (has_padding) 119 | alen = stun_align (alen); 120 | 121 | /* thanks to padding check, if (end > msg) then there is not only one 122 | * but at least 4 bytes left */ 123 | len -= 4; 124 | 125 | if (len < alen) 126 | { 127 | ICE_DEBUG("STUN error: %u instead of %u bytes for attribute!", 128 | (unsigned)len, (unsigned)alen); 129 | return STUN_MESSAGE_BUFFER_INVALID; // no room for attribute value + padding 130 | } 131 | 132 | len -= alen; 133 | msg += 4 + alen; 134 | } 135 | ICE_DEBUG("stun msg, mlen=%lu",mlen); 136 | return mlen; 137 | } 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | -------------------------------------------------------------------------------- /stun/debug.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2007 Nokia Corporation. All rights reserved. 7 | * 8 | * The contents of this file are subject to the Mozilla Public License Version 9 | * 1.1 (the "License"); you may not use this file except in compliance with 10 | * the License. You may obtain a copy of the License at 11 | * http://www.mozilla.org/MPL/ 12 | * 13 | * Software distributed under the License is distributed on an "AS IS" basis, 14 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 15 | * for the specific language governing rights and limitations under the 16 | * License. 17 | * 18 | * The Original Code is the Nice GLib ICE library. 19 | * 20 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 21 | * Corporation. All Rights Reserved. 22 | * 23 | * Contributors: 24 | * Youness Alaoui, Collabora Ltd. 25 | * 26 | * Alternatively, the contents of this file may be used under the terms of the 27 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 28 | * case the provisions of LGPL are applicable instead of those above. If you 29 | * wish to allow use of your version of this file only under the terms of the 30 | * LGPL and not to allow others to use your version of this file under the 31 | * MPL, indicate your decision by deleting the provisions above and replace 32 | * them with the notice and other provisions required by the LGPL. If you do 33 | * not delete the provisions above, a recipient may use your version of this 34 | * file under either the MPL or the LGPL. 35 | */ 36 | 37 | #ifdef HAVE_CONFIG_H 38 | # include 39 | #endif 40 | 41 | #include 42 | #include 43 | #include 44 | #include 45 | 46 | #include "cice/stun/debug.h" 47 | 48 | static int debug_enabled = 1; 49 | 50 | void stun_debug_enable (void) { 51 | debug_enabled = 1; 52 | } 53 | void stun_debug_disable (void) { 54 | debug_enabled = 0; 55 | } 56 | 57 | #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) 58 | #define GNUC_PRINTF(format_idx, arg_idx) \ 59 | __attribute__((__format__ (__printf__, format_idx, arg_idx))) 60 | #else 61 | #define GNUC_PRINTF( format_idx, arg_idx) 62 | #endif 63 | 64 | static void 65 | default_handler (const char *format, va_list ap) GNUC_PRINTF (1, 0); 66 | 67 | static void 68 | default_handler (const char *format, va_list ap) 69 | { 70 | //FIXME: using log function 71 | //vfprintf (stderr, format, ap); 72 | //fprintf (stderr, "\n"); 73 | } 74 | 75 | static StunDebugHandler handler = default_handler; 76 | 77 | void stun_debug (const char *fmt, ...) 78 | { 79 | va_list ap; 80 | if (debug_enabled) { 81 | va_start (ap, fmt); 82 | handler (fmt, ap); 83 | va_end (ap); 84 | } 85 | } 86 | /*#ifdef ICE_LIB 87 | #else 88 | #endif*/ 89 | 90 | void stun_debug_bytes (const char *prefix, const void *data, size_t len) 91 | { 92 | size_t i; 93 | size_t prefix_len = strlen (prefix); 94 | char *bytes; 95 | 96 | if (!debug_enabled) 97 | return; 98 | 99 | bytes = (char*)malloc (prefix_len + 2 + (len * 2) + 1); 100 | bytes[0] = 0; 101 | strcpy (bytes, prefix); 102 | strcpy (bytes + prefix_len, "0x"); 103 | 104 | for (i = 0; i < len; i++) 105 | sprintf (bytes + prefix_len + 2 + (i * 2), "%02x", ((const unsigned char *)data)[i]); 106 | 107 | stun_debug ("%s", bytes); 108 | free (bytes); 109 | } 110 | 111 | 112 | void stun_set_debug_handler (StunDebugHandler _handler) 113 | { 114 | if (_handler == NULL) 115 | _handler = default_handler; 116 | 117 | handler = _handler; 118 | } 119 | 120 | -------------------------------------------------------------------------------- /stun/md5.c: -------------------------------------------------------------------------------- 1 | /* 2 | * MD5 hash implementation and interface functions 3 | * Copyright (c) 2003-2005, Jouni Malinen 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License version 2 as 7 | * published by the Free Software Foundation. 8 | * 9 | * Alternatively, this software may be distributed under the terms of BSD 10 | * license. 11 | * 12 | * See README and COPYING for more details. 13 | */ 14 | 15 | #include 16 | 17 | #include "cice/stun/md5.h" 18 | 19 | /* ===== start - public domain MD5 implementation ===== */ 20 | /* 21 | * This code implements the MD5 message-digest algorithm. 22 | * The algorithm is due to Ron Rivest. This code was 23 | * written by Colin Plumb in 1993, no copyright is claimed. 24 | * This code is in the public domain; do with it what you wish. 25 | * 26 | * Equivalent code is available from RSA Data Security, Inc. 27 | * This code has been tested against that, and is equivalent, 28 | * except that you don't need to include two pages of legalese 29 | * with every copy. 30 | * 31 | * To compute the message digest of a chunk of bytes, declare an 32 | * MD5Context structure, pass it to MD5Init, call MD5Update as 33 | * needed on buffers full of bytes, and then call MD5Final, which 34 | * will fill a supplied 16-byte array with the digest. 35 | */ 36 | 37 | static void MD5Transform(uint32_t buf[4], uint32_t const in[16]); 38 | 39 | static int am_big_endian(void) 40 | { 41 | long one= 1; 42 | return !(*((char *)(&one))); 43 | } 44 | 45 | /* 46 | * Note: this code is harmless on little-endian machines. 47 | */ 48 | static void byteReverse(unsigned char *buf, unsigned longs) 49 | { 50 | uint32_t t; 51 | if (am_big_endian ()) { 52 | do { 53 | t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 | 54 | ((unsigned) buf[1] << 8 | buf[0]); 55 | *(uint32_t *) buf = t; 56 | buf += 4; 57 | } while (--longs); 58 | } 59 | } 60 | 61 | /* 62 | * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 63 | * initialization constants. 64 | */ 65 | void MD5Init(MD5_CTX *ctx) 66 | { 67 | ctx->buf[0] = 0x67452301; 68 | ctx->buf[1] = 0xefcdab89; 69 | ctx->buf[2] = 0x98badcfe; 70 | ctx->buf[3] = 0x10325476; 71 | 72 | ctx->bits[0] = 0; 73 | ctx->bits[1] = 0; 74 | } 75 | 76 | /* 77 | * Update context to reflect the concatenation of another buffer full 78 | * of bytes. 79 | */ 80 | void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len) 81 | { 82 | uint32_t t; 83 | 84 | /* Update bitcount */ 85 | 86 | t = ctx->bits[0]; 87 | if ((ctx->bits[0] = t + ((uint32_t) len << 3)) < t) 88 | ctx->bits[1]++; /* Carry from low to high */ 89 | ctx->bits[1] += len >> 29; 90 | 91 | t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */ 92 | 93 | /* Handle any leading odd-sized chunks */ 94 | 95 | if (t) { 96 | unsigned char *p = (unsigned char *) ctx->in.u8 + t; 97 | 98 | t = 64 - t; 99 | if (len < t) { 100 | memcpy(p, buf, len); 101 | return; 102 | } 103 | memcpy(p, buf, t); 104 | byteReverse(ctx->in.u8, 16); 105 | MD5Transform(ctx->buf, ctx->in.u32); 106 | buf += t; 107 | len -= t; 108 | } 109 | /* Process data in 64-byte chunks */ 110 | 111 | while (len >= 64) { 112 | memcpy(ctx->in.u8, buf, 64); 113 | byteReverse(ctx->in.u8, 16); 114 | MD5Transform(ctx->buf, ctx->in.u32); 115 | buf += 64; 116 | len -= 64; 117 | } 118 | 119 | /* Handle any remaining bytes of data. */ 120 | 121 | memcpy(ctx->in.u8, buf, len); 122 | } 123 | 124 | /* 125 | * Final wrapup - pad to 64-byte boundary with the bit pattern 126 | * 1 0* (64-bit count of bits processed, MSB-first) 127 | */ 128 | void MD5Final(unsigned char digest[16], struct MD5Context *ctx) 129 | { 130 | unsigned count; 131 | unsigned char *p; 132 | 133 | /* Compute number of bytes mod 64 */ 134 | count = (ctx->bits[0] >> 3) & 0x3F; 135 | 136 | /* Set the first char of padding to 0x80. This is safe since there is 137 | always at least one byte free */ 138 | p = ctx->in.u8 + count; 139 | *p++ = 0x80; 140 | 141 | /* Bytes of padding needed to make 64 bytes */ 142 | count = 64 - 1 - count; 143 | 144 | /* Pad out to 56 mod 64 */ 145 | if (count < 8) { 146 | /* Two lots of padding: Pad the first block to 64 bytes */ 147 | memset(p, 0, count); 148 | byteReverse(ctx->in.u8, 16); 149 | MD5Transform(ctx->buf, ctx->in.u32); 150 | 151 | /* Now fill the next block with 56 bytes */ 152 | memset(ctx->in.u8, 0, 56); 153 | } else { 154 | /* Pad block to 56 bytes */ 155 | memset(p, 0, count - 8); 156 | } 157 | byteReverse(ctx->in.u8, 14); 158 | 159 | /* Append length in bits and transform */ 160 | ctx->in.u32[14] = ctx->bits[0]; 161 | ctx->in.u32[15] = ctx->bits[1]; 162 | 163 | MD5Transform(ctx->buf, ctx->in.u32); 164 | byteReverse((unsigned char *) ctx->buf, 4); 165 | memcpy(digest, ctx->buf, 16); 166 | memset(ctx, 0, sizeof(struct MD5Context)); /* In case it's sensitive */ 167 | } 168 | 169 | /* The four core functions - F1 is optimized somewhat */ 170 | 171 | /* #define F1(x, y, z) (x & y | ~x & z) */ 172 | #define F1(x, y, z) (z ^ (x & (y ^ z))) 173 | #define F2(x, y, z) F1(z, x, y) 174 | #define F3(x, y, z) (x ^ y ^ z) 175 | #define F4(x, y, z) (y ^ (x | ~z)) 176 | 177 | /* This is the central step in the MD5 algorithm. */ 178 | #define MD5STEP(f, w, x, y, z, data, s) \ 179 | ( w += f(x, y, z) + data, w = w<>(32-s), w += x ) 180 | 181 | /* 182 | * The core of the MD5 algorithm, this alters an existing MD5 hash to 183 | * reflect the addition of 16 longwords of new data. MD5Update blocks 184 | * the data and converts bytes into longwords for this routine. 185 | */ 186 | static void MD5Transform(uint32_t buf[4], uint32_t const in[16]) 187 | { 188 | register uint32_t a, b, c, d; 189 | 190 | a = buf[0]; 191 | b = buf[1]; 192 | c = buf[2]; 193 | d = buf[3]; 194 | 195 | MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); 196 | MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); 197 | MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); 198 | MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); 199 | MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); 200 | MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); 201 | MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); 202 | MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); 203 | MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); 204 | MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); 205 | MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); 206 | MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); 207 | MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); 208 | MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); 209 | MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); 210 | MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); 211 | 212 | MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); 213 | MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); 214 | MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); 215 | MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); 216 | MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); 217 | MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); 218 | MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); 219 | MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); 220 | MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); 221 | MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); 222 | MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); 223 | MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); 224 | MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); 225 | MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); 226 | MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); 227 | MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); 228 | 229 | MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); 230 | MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); 231 | MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); 232 | MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); 233 | MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); 234 | MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); 235 | MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); 236 | MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); 237 | MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); 238 | MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); 239 | MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); 240 | MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); 241 | MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); 242 | MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); 243 | MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); 244 | MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); 245 | 246 | MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); 247 | MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); 248 | MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); 249 | MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); 250 | MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); 251 | MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); 252 | MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); 253 | MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); 254 | MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); 255 | MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); 256 | MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); 257 | MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); 258 | MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); 259 | MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); 260 | MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); 261 | MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); 262 | 263 | buf[0] += a; 264 | buf[1] += b; 265 | buf[2] += c; 266 | buf[3] += d; 267 | } 268 | /* ===== end - public domain MD5 implementation ===== */ 269 | -------------------------------------------------------------------------------- /stun/rand.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008 Collabora Ltd. All rights reserved. 5 | * Contact: Youness Alaoui 6 | * (C) 2008 Nokia Corporation. All rights reserved. 7 | * 8 | * The contents of this file are subject to the Mozilla Public License Version 9 | * 1.1 (the "License"); you may not use this file except in compliance with 10 | * the License. You may obtain a copy of the License at 11 | * http://www.mozilla.org/MPL/ 12 | * 13 | * Software distributed under the License is distributed on an "AS IS" basis, 14 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 15 | * for the specific language governing rights and limitations under the 16 | * License. 17 | * 18 | * The Original Code is the Nice GLib ICE library. 19 | * 20 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 21 | * Corporation. All Rights Reserved. 22 | * 23 | * Alternatively, the contents of this file may be used under the terms of the 24 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 25 | * case the provisions of LGPL are applicable instead of those above. If you 26 | * wish to allow use of your version of this file only under the terms of the 27 | * LGPL and not to allow others to use your version of this file under the 28 | * MPL, indicate your decision by deleting the provisions above and replace 29 | * them with the notice and other provisions required by the LGPL. If you do 30 | * not delete the provisions above, a recipient may use your version of this 31 | * file under either the MPL or the LGPL. 32 | */ 33 | 34 | 35 | 36 | #ifdef HAVE_CONFIG_H 37 | # include 38 | #endif 39 | 40 | #include "cice/stun/rand.h" 41 | 42 | 43 | #ifdef _WIN32 44 | 45 | #include 46 | #include 47 | 48 | void nice_RAND_bytes (uint8_t *dst, int len) 49 | { 50 | HCRYPTPROV hCryptProv; 51 | LPCSTR container = "Libnice key container"; 52 | 53 | if(!CryptAcquireContext(&hCryptProv, container, NULL, PROV_RSA_FULL, 0)) { 54 | /* non existing container. try to create a new one */ 55 | // I hope this cast here doesn't cause issues 56 | // gcc was complaining about comparing signed and unsigned values 57 | if (GetLastError() == (DWORD) NTE_BAD_KEYSET) { 58 | if(!CryptAcquireContext(&hCryptProv, container, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET)) { 59 | return; 60 | } 61 | } 62 | return; 63 | } 64 | 65 | CryptGenRandom (hCryptProv, len, dst); 66 | 67 | CryptReleaseContext(hCryptProv,0); 68 | } 69 | #else 70 | 71 | /* ------------- Start original implementation. ----------------- */ 72 | /* http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html */ 73 | 74 | /* 75 | A C-program for MT19937, with initialization improved 2002/1/26. 76 | Coded by Takuji Nishimura and Makoto Matsumoto. 77 | 78 | Before using, initialize the state by using init_genrand(seed) 79 | or init_by_array(init_key, key_length). 80 | 81 | Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, 82 | All rights reserved 83 | 84 | Redistribution and use in source and binary forms, with or without 85 | modification, are permitted provided that the following conditions 86 | are met: 87 | 88 | 1. Redistributions of source code must retain the above copyright 89 | notice, this list of conditions and the following disclaimer. 90 | 91 | 2. Redistributions in binary form must reproduce the above copyright 92 | notice, this list of conditions and the following disclaimer in the 93 | documentation and/or other materials provided with the distribution. 94 | 95 | 3. The names of its contributors may not be used to endorse or promote 96 | products derived from this software without specific prior written 97 | permission. 98 | 99 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 100 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 101 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 102 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 103 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 104 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 105 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 106 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 107 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 108 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 109 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 110 | 111 | 112 | Any feedback is very welcome. 113 | http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html 114 | email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space) 115 | */ 116 | 117 | /* Period parameters */ 118 | #define N 624 119 | #define M 397 120 | #define MATRIX_A 0x9908b0dfUL /* constant vector a */ 121 | #define UPPER_MASK 0x80000000UL /* most significant w-r bits */ 122 | #define LOWER_MASK 0x7fffffffUL /* least significant r bits */ 123 | 124 | static unsigned long mt[N]; /* the array for the state vector */ 125 | static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */ 126 | 127 | /* initializes mt[N] with a seed */ 128 | static void init_genrand(unsigned long s) 129 | { 130 | mt[0]= s & 0xffffffffUL; 131 | for (mti=1; mti> 30)) + mti); 133 | /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ 134 | /* In the previous versions, MSBs of the seed affect */ 135 | /* only MSBs of the array mt[]. */ 136 | /* 2002/01/09 modified by Makoto Matsumoto */ 137 | mt[mti] &= 0xffffffffUL; 138 | /* for >32 bit machines */ 139 | } 140 | } 141 | 142 | /* initialize by an array with array-length */ 143 | /* init_key is the array for initializing keys */ 144 | /* key_length is its length */ 145 | /* slight change for C++, 2004/2/26 */ 146 | static void init_by_array(unsigned long init_key[], int key_length) 147 | { 148 | int i, j, k; 149 | init_genrand(19650218UL); 150 | i=1; j=0; 151 | k = (N>key_length ? N : key_length); 152 | for (; k; k--) { 153 | mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL)) 154 | + init_key[j] + j; /* non linear */ 155 | mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ 156 | i++; j++; 157 | if (i>=N) { mt[0] = mt[N-1]; i=1; } 158 | if (j>=key_length) j=0; 159 | } 160 | for (k=N-1; k; k--) { 161 | mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) 162 | - i; /* non linear */ 163 | mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */ 164 | i++; 165 | if (i>=N) { mt[0] = mt[N-1]; i=1; } 166 | } 167 | 168 | mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ 169 | } 170 | 171 | /* generates a random number on [0,0xffffffff]-interval */ 172 | static unsigned long genrand_int32(void) 173 | { 174 | unsigned long y; 175 | static unsigned long mag01[2]={0x0UL, MATRIX_A}; 176 | /* mag01[x] = x * MATRIX_A for x=0,1 */ 177 | 178 | if (mti >= N) { /* generate N words at one time */ 179 | int kk; 180 | 181 | if (mti == N+1) /* if init_genrand() has not been called, */ 182 | init_genrand(5489UL); /* a default initial seed is used */ 183 | 184 | for (kk=0;kk> 1) ^ mag01[y & 0x1UL]; 187 | } 188 | for (;kk> 1) ^ mag01[y & 0x1UL]; 191 | } 192 | y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK); 193 | mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL]; 194 | 195 | mti = 0; 196 | } 197 | 198 | y = mt[mti++]; 199 | 200 | /* Tempering */ 201 | y ^= (y >> 11); 202 | y ^= (y << 7) & 0x9d2c5680UL; 203 | y ^= (y << 15) & 0xefc60000UL; 204 | y ^= (y >> 18); 205 | 206 | return y; 207 | } 208 | 209 | /* These real versions are due to Isaku Wada, 2002/01/09 added */ 210 | 211 | /* ------------- End original implementation. ----------------- */ 212 | 213 | #include 214 | #include 215 | 216 | static int initialized = 0; 217 | 218 | void nice_RAND_bytes (uint8_t *dst, int len) 219 | { 220 | int i; 221 | 222 | if (!initialized) { 223 | /* Seed the generator with an array from /dev/urandom if available 224 | Otherwise use time() and clock() values */ 225 | 226 | FILE *urandom = fopen( "/dev/urandom", "rb" ); 227 | unsigned long init_key[10] = {}; 228 | int key_length = 0; 229 | if (urandom) { 230 | while (fread(&init_key[key_length++], sizeof(unsigned long), 1, 231 | urandom) > 0 && key_length < 10); 232 | fclose(urandom); 233 | } else { 234 | time_t t = time (NULL); 235 | clock_t c = clock (); 236 | unsigned long cl = c; 237 | unsigned long tl = t; 238 | init_key[0] = *((unsigned long *) dst); 239 | init_key[1] = 0x6c69626e; 240 | init_key[2] = 0x69636500; 241 | init_key[3] = tl; 242 | init_key[4] = cl; 243 | key_length = 5; 244 | } 245 | init_by_array(init_key, key_length); 246 | initialized = 1; 247 | } 248 | 249 | for (i = 0; i < len; i++) { 250 | dst[i] = genrand_int32 () & 0xFF; 251 | } 252 | } 253 | 254 | #endif /* _WIN32 */ 255 | -------------------------------------------------------------------------------- /stun/stun5389.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2007 Nokia Corporation. All rights reserved. 7 | * 8 | * The contents of this file are subject to the Mozilla Public License Version 9 | * 1.1 (the "License"); you may not use this file except in compliance with 10 | * the License. You may obtain a copy of the License at 11 | * http://www.mozilla.org/MPL/ 12 | * 13 | * Software distributed under the License is distributed on an "AS IS" basis, 14 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 15 | * for the specific language governing rights and limitations under the 16 | * License. 17 | * 18 | * The Original Code is the Nice GLib ICE library. 19 | * 20 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 21 | * Corporation. All Rights Reserved. 22 | * 23 | * Contributors: 24 | * Youness Alaoui, Collabora Ltd. 25 | * 26 | * Alternatively, the contents of this file may be used under the terms of the 27 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 28 | * case the provisions of LGPL are applicable instead of those above. If you 29 | * wish to allow use of your version of this file only under the terms of the 30 | * LGPL and not to allow others to use your version of this file under the 31 | * MPL, indicate your decision by deleting the provisions above and replace 32 | * them with the notice and other provisions required by the LGPL. If you do 33 | * not delete the provisions above, a recipient may use your version of this 34 | * file under either the MPL or the LGPL. 35 | */ 36 | 37 | #ifdef HAVE_CONFIG_H 38 | # include 39 | #endif 40 | 41 | #ifdef _WIN32 42 | #include 43 | #else 44 | #include 45 | #include /* htons() */ 46 | #endif 47 | 48 | #include 49 | #include 50 | 51 | #include "cice/stun/stun5389.h" 52 | #include "cice/stun/stuncrc32.h" 53 | #include "cice/stun/stunmessage.h" 54 | #include "cice/types.h" 55 | 56 | 57 | static const char utf8_skip_data[256] = { 58 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 59 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 60 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 61 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 62 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 63 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 64 | 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 65 | 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1 66 | }; 67 | 68 | #define next_utf8_char(p) (char *)((p) + \ 69 | utf8_skip_data[*(const unsigned char *)(p)]) 70 | 71 | uint32_t stun_fingerprint (const uint8_t *msg, size_t len, 72 | bool wlm2009_stupid_crc32_typo) 73 | { 74 | crc_data data[3]; 75 | uint16_t fakelen = htons (len - 20u); 76 | 77 | // assert (len >= 28u); 78 | ICE_DEBUG("msg=%p,len=%ul",msg,len); 79 | 80 | data[0].buf = (uint8_t *)msg; 81 | data[0].len = 2; 82 | data[1].buf = (uint8_t *)&fakelen; 83 | data[1].len = 2; 84 | data[2].buf = (uint8_t *)(msg + 4); 85 | /* first 4 bytes done, last 8 bytes not summed */ 86 | data[2].len = len - 12u; 87 | 88 | return htonl (stun_crc32 (data, 3, wlm2009_stupid_crc32_typo) ^ 0x5354554e); 89 | } 90 | 91 | bool stun_message_has_cookie (const StunMessage *msg) 92 | { 93 | StunTransactionId id; 94 | uint32_t cookie = htonl (STUN_MAGIC_COOKIE); 95 | stun_message_id (msg, id); 96 | return memcmp (id, &cookie, sizeof (cookie)) == 0; 97 | } 98 | 99 | #define PACKAGE_STRING "peercall.vn" 100 | StunMessageReturn stun_message_append_software (StunMessage *msg, 101 | const char *software) 102 | { 103 | int len = 0; 104 | const char *ptr = NULL; 105 | 106 | if (software == NULL) 107 | software = PACKAGE_STRING; 108 | 109 | ptr = software; 110 | while (*ptr && len < 128) { 111 | ptr = next_utf8_char (ptr); 112 | len++; 113 | } 114 | 115 | return stun_message_append_bytes (msg, STUN_ATTRIBUTE_SOFTWARE, software, 116 | ptr - software); 117 | } 118 | -------------------------------------------------------------------------------- /stun/stuncrc32.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2007-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Rémi Denis-Courmont 8 | * COPYRIGHT (C) 1986 Gary S. Brown 9 | * See documentation of the function crc32() below. 10 | * 11 | * The contents of this file are subject to the Mozilla Public License Version 12 | * 1.1 (the "License"); you may not use this file except in compliance with 13 | * the License. You may obtain a copy of the License at 14 | * http://www.mozilla.org/MPL/ 15 | * 16 | * Software distributed under the License is distributed on an "AS IS" basis, 17 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 18 | * for the specific language governing rights and limitations under the 19 | * License. 20 | * 21 | * The Original Code is the Nice GLib ICE library. 22 | * 23 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 24 | * Corporation. All Rights Reserved. 25 | * 26 | * Contributors: 27 | * Youness Alaoui, Collabora Ltd. 28 | * Rémi Denis-Courmont, Nokia 29 | * Gary S. Brown 30 | * 31 | * Alternatively, the contents of this file may be used under the terms of the 32 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 33 | * case the provisions of LGPL are applicable instead of those above. If you 34 | * wish to allow use of your version of this file only under the terms of the 35 | * LGPL and not to allow others to use your version of this file under the 36 | * MPL, indicate your decision by deleting the provisions above and replace 37 | * them with the notice and other provisions required by the LGPL. If you do 38 | * not delete the provisions above, a recipient may use your version of this 39 | * file under either the MPL or the LGPL. 40 | */ 41 | 42 | /*- 43 | * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or 44 | * code or tables extracted from it, as desired without restriction. 45 | * 46 | * Extracted from FreeBSD CVS (src/sys/libkern/crc32.c) 47 | * and adapted by Rémi Denis-Courmont, 2007. 48 | */ 49 | 50 | /* 51 | * First, the polynomial itself and its table of feedback terms. The 52 | * polynomial is 53 | * X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0 54 | * 55 | * Note that we take it "backwards" and put the highest-order term in 56 | * the lowest-order bit. The X^32 term is "implied"; the LSB is the 57 | * X^31 term, etc. The X^0 term (usually shown as "+1") results in 58 | * the MSB being 1 59 | * 60 | * Note that the usual hardware shift register implementation, which 61 | * is what we're using (we're merely optimizing it by doing eight-bit 62 | * chunks at a time) shifts bits into the lowest-order term. In our 63 | * implementation, that means shifting towards the right. Why do we 64 | * do it this way? Because the calculated CRC must be transmitted in 65 | * order from highest-order term to lowest-order term. UARTs transmit 66 | * characters in order from LSB to MSB. By storing the CRC this way 67 | * we hand it to the UART in the order low-byte to high-byte; the UART 68 | * sends each low-bit to hight-bit; and the result is transmission bit 69 | * by bit from highest- to lowest-order term without requiring any bit 70 | * shuffling on our part. Reception works similarly 71 | * 72 | * The feedback terms table consists of 256, 32-bit entries. Notes 73 | * 74 | * The table can be generated at runtime if desired; code to do so 75 | * is shown later. It might not be obvious, but the feedback 76 | * terms simply represent the results of eight shift/xor opera 77 | * tions for all combinations of data and CRC register values 78 | * 79 | * The values must be right-shifted by eight bits by the "updcrc 80 | * logic; the shift must be unsigned (bring in zeroes). On some 81 | * hardware you could probably optimize the shift in assembler by 82 | * using byte-swap instructions 83 | * polynomial $edb88320 84 | * 85 | * 86 | * CRC32 code derived from work by Gary S. Brown. 87 | */ 88 | 89 | 90 | #ifdef HAVE_CONFIG_H 91 | # include 92 | #endif 93 | 94 | #include "cice/stun/stuncrc32.h" 95 | 96 | static const uint32_t crc32_tab[] = { 97 | 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 98 | 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 99 | 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, 100 | 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 101 | 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 102 | 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 103 | 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, 104 | 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 105 | 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 106 | 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 107 | 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, 108 | 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, 109 | 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 110 | 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 111 | 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, 112 | 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, 113 | 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 114 | 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 115 | 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 116 | 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, 117 | 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 118 | 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 119 | 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, 120 | 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 121 | 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 122 | 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 123 | 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, 124 | 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 125 | 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 126 | 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 127 | 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, 128 | 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, 129 | 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 130 | 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 131 | 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, 132 | 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, 133 | 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 134 | 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 135 | 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 136 | 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, 137 | 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 138 | 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 139 | 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d 140 | }; 141 | 142 | 143 | uint32_t stun_crc32 (const crc_data *data, size_t n, bool wlm2009_stupid_crc32_typo) 144 | { 145 | size_t i; 146 | uint32_t crc = 0xffffffff; 147 | 148 | for (i = 0; i < n; i++) 149 | { 150 | const uint8_t *p = data[i].buf; 151 | size_t size = data[i].len; 152 | 153 | while (size--) { 154 | uint32_t lkp = crc32_tab[(crc ^ *p++) & 0xFF]; 155 | if (lkp == 0x8bbeb8ea && wlm2009_stupid_crc32_typo) 156 | lkp = 0x8bbe8ea; 157 | crc = lkp ^ (crc >> 8); 158 | } 159 | } 160 | 161 | return crc ^ 0xffffffff; 162 | } 163 | -------------------------------------------------------------------------------- /stun/stunhmac.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2007-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Rémi Denis-Courmont 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Youness Alaoui, Collabora Ltd. 26 | * Rémi Denis-Courmont, Nokia 27 | * 28 | * Alternatively, the contents of this file may be used under the terms of the 29 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 30 | * case the provisions of LGPL are applicable instead of those above. If you 31 | * wish to allow use of your version of this file only under the terms of the 32 | * LGPL and not to allow others to use your version of this file under the 33 | * MPL, indicate your decision by deleting the provisions above and replace 34 | * them with the notice and other provisions required by the LGPL. If you do 35 | * not delete the provisions above, a recipient may use your version of this 36 | * file under either the MPL or the LGPL. 37 | */ 38 | 39 | #ifdef HAVE_CONFIG_H 40 | # include 41 | #endif 42 | 43 | #include 44 | #include 45 | 46 | #include "cice/stun/sha1.h" 47 | #include "cice/stun/md5.h" 48 | #include "cice/stun/rand.h" 49 | 50 | #include "cice/stun/stunmessage.h" 51 | #include "cice/stun/stunhmac.h" 52 | 53 | void stun_sha1 (const uint8_t *msg, size_t len, size_t msg_len, uint8_t *sha, 54 | const void *key, size_t keylen, int padding) 55 | { 56 | uint16_t fakelen = htons (msg_len); 57 | const uint8_t *vector[4]; 58 | size_t lengths[4]; 59 | uint8_t pad_char[64] = {0}; 60 | size_t num_elements; 61 | 62 | assert (len >= 44u); 63 | 64 | vector[0] = msg; 65 | lengths[0] = 2; 66 | vector[1] = (const uint8_t *)&fakelen; 67 | lengths[1] = 2; 68 | vector[2] = msg + 4; 69 | lengths[2] = len - 28; 70 | num_elements = 3; 71 | 72 | /* RFC 3489 specifies that the message's size should be 64 bytes, 73 | and \x00 padding should be done */ 74 | if (padding && ((len - 24) % 64) > 0) { 75 | uint16_t pad_size = 64 - ((len - 24) % 64); 76 | 77 | vector[3] = pad_char; 78 | lengths[3] = pad_size; 79 | num_elements++; 80 | } 81 | 82 | hmac_sha1_vector((uint8_t*)key, keylen, num_elements, vector, lengths, sha); 83 | } 84 | 85 | static const uint8_t *priv_trim_var (const uint8_t *var, size_t *var_len) 86 | { 87 | const uint8_t *ptr = var; 88 | 89 | while (*ptr == '"') { 90 | ptr++; 91 | (*var_len)--; 92 | } 93 | while(ptr[*var_len-1] == '"' || 94 | ptr[*var_len-1] == 0) { 95 | (*var_len)--; 96 | } 97 | 98 | return ptr; 99 | } 100 | 101 | 102 | void stun_hash_creds (const uint8_t *realm, size_t realm_len, 103 | const uint8_t *username, size_t username_len, 104 | const uint8_t *password, size_t password_len, 105 | unsigned char md5[16]) 106 | { 107 | MD5_CTX ctx; 108 | const uint8_t *username_trimmed = priv_trim_var (username, &username_len); 109 | const uint8_t *password_trimmed = priv_trim_var (password, &password_len); 110 | const uint8_t *realm_trimmed = priv_trim_var (realm, &realm_len); 111 | const uint8_t *colon = (uint8_t *)":"; 112 | 113 | MD5Init (&ctx); 114 | MD5Update (&ctx, username_trimmed, username_len); 115 | MD5Update (&ctx, colon, 1); 116 | MD5Update (&ctx, realm_trimmed, realm_len); 117 | MD5Update (&ctx, colon, 1); 118 | MD5Update (&ctx, password_trimmed, password_len); 119 | MD5Final (md5, &ctx); 120 | } 121 | 122 | 123 | void stun_make_transid (StunTransactionId id) 124 | { 125 | nice_RAND_bytes (id, 16); 126 | } 127 | -------------------------------------------------------------------------------- /stun/usages/.dirstamp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/snowem/libcice/d329669af21a8247894b2b72c6eda1866b214cc1/stun/usages/.dirstamp -------------------------------------------------------------------------------- /stun/usages/timer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2007-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Rémi Denis-Courmont 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Youness Alaoui, Collabora Ltd. 26 | * Rémi Denis-Courmont, Nokia 27 | * 28 | * Alternatively, the contents of this file may be used under the terms of the 29 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 30 | * case the provisions of LGPL are applicable instead of those above. If you 31 | * wish to allow use of your version of this file only under the terms of the 32 | * LGPL and not to allow others to use your version of this file under the 33 | * MPL, indicate your decision by deleting the provisions above and replace 34 | * them with the notice and other provisions required by the LGPL. If you do 35 | * not delete the provisions above, a recipient may use your version of this 36 | * file under either the MPL or the LGPL. 37 | */ 38 | 39 | #ifdef HAVE_CONFIG_H 40 | # include 41 | #endif 42 | 43 | #ifdef _WIN32 44 | #define WIN32_LEAN_AND_MEAN 45 | #include 46 | #else 47 | #include 48 | #endif 49 | 50 | #include 51 | 52 | #include /* div() */ 53 | 54 | #include "cice/stun/usages/timer.h" 55 | 56 | /* 57 | * Clock used throughout the STUN code. 58 | * STUN requires a monotonic 1kHz clock to operate properly. 59 | */ 60 | static void stun_gettime (struct timeval *now) 61 | { 62 | #ifdef _WIN32 63 | FILETIME ft; 64 | unsigned long long *time64 = (unsigned long long *) &ft; 65 | 66 | GetSystemTimeAsFileTime (&ft); 67 | 68 | /* Convert from 100s of nanoseconds since 1601-01-01 69 | * to Unix epoch. Yes, this is Y2038 unsafe. 70 | */ 71 | *time64 -= (unsigned long long) 116444736000000000; 72 | *time64 /= 10; 73 | 74 | now->tv_sec = (long)(*time64 / 1000000); 75 | now->tv_usec = *time64 % 1000000; 76 | #else 77 | #if defined (_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK >= 0) 78 | struct timespec spec; 79 | if (!clock_gettime (CLOCK_MONOTONIC, &spec)) { 80 | now->tv_sec = spec.tv_sec; 81 | now->tv_usec = spec.tv_nsec / 1000; 82 | } else 83 | #endif 84 | { // fallback to wall clock 85 | gettimeofday (now, NULL); 86 | } 87 | #endif 88 | } 89 | 90 | 91 | static void add_delay (struct timeval *ts, unsigned delay) 92 | { 93 | /* Delay is in ms. */ 94 | ts->tv_sec += delay / 1000; 95 | ts->tv_usec += (delay % 1000) * 1000; 96 | 97 | while (ts->tv_usec > 1000000) 98 | { 99 | ts->tv_usec -= 1000000; 100 | ts->tv_sec++; 101 | } 102 | } 103 | 104 | 105 | void stun_timer_start (StunTimer *timer, unsigned int initial_timeout, 106 | unsigned int max_retransmissions) 107 | { 108 | stun_gettime (&timer->deadline); 109 | timer->retransmissions = 0; 110 | timer->delay = initial_timeout; 111 | timer->max_retransmissions = max_retransmissions; 112 | add_delay (&timer->deadline, timer->delay); 113 | } 114 | 115 | 116 | void stun_timer_start_reliable (StunTimer *timer, unsigned int initial_timeout) 117 | { 118 | stun_timer_start (timer, initial_timeout, 0); 119 | } 120 | 121 | 122 | 123 | unsigned stun_timer_remainder (const StunTimer *timer) 124 | { 125 | unsigned delay; 126 | struct timeval now; 127 | 128 | stun_gettime (&now); 129 | if (now.tv_sec > timer->deadline.tv_sec) 130 | return 0; 131 | 132 | delay = timer->deadline.tv_sec - now.tv_sec; 133 | if ((delay == 0) && (now.tv_usec >= timer->deadline.tv_usec)) 134 | return 0; 135 | 136 | delay *= 1000; 137 | delay += ((signed)(timer->deadline.tv_usec - now.tv_usec)) / 1000; 138 | return delay; 139 | } 140 | 141 | 142 | StunUsageTimerReturn stun_timer_refresh (StunTimer *timer) 143 | { 144 | unsigned delay = stun_timer_remainder (timer); 145 | if (delay == 0) 146 | { 147 | if (timer->retransmissions >= timer->max_retransmissions) 148 | return STUN_USAGE_TIMER_RETURN_TIMEOUT; 149 | 150 | add_delay (&timer->deadline, timer->delay *= 2); 151 | timer->retransmissions++; 152 | return STUN_USAGE_TIMER_RETURN_RETRANSMIT; 153 | } 154 | 155 | return STUN_USAGE_TIMER_RETURN_SUCCESS; 156 | } 157 | 158 | 159 | 160 | -------------------------------------------------------------------------------- /stun/utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2007-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Rémi Denis-Courmont 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Youness Alaoui, Collabora Ltd. 26 | * Olivier Crete, Collabora Ltd. 27 | * Kai Vehmanen, Nokia 28 | * 29 | * Alternatively, the contents of this file may be used under the terms of the 30 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 31 | * case the provisions of LGPL are applicable instead of those above. If you 32 | * wish to allow use of your version of this file only under the terms of the 33 | * LGPL and not to allow others to use your version of this file under the 34 | * MPL, indicate your decision by deleting the provisions above and replace 35 | * them with the notice and other provisions required by the LGPL. If you do 36 | * not delete the provisions above, a recipient may use your version of this 37 | * file under either the MPL or the LGPL. 38 | */ 39 | 40 | #ifdef HAVE_CONFIG_H 41 | # include 42 | #endif 43 | 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include 49 | 50 | #include "cice/stun/utils.h" 51 | 52 | size_t stun_padding (size_t l) 53 | { 54 | return (4 - (l & 3)) & 3; 55 | } 56 | 57 | size_t stun_align (size_t l) 58 | { 59 | return (l + 3) & ~3; 60 | } 61 | 62 | 63 | uint16_t stun_getw (const uint8_t *ptr) 64 | { 65 | return ((ptr)[0] << 8) | ptr[1]; 66 | } 67 | 68 | 69 | void *stun_setw (uint8_t *ptr, uint16_t value) 70 | { 71 | *ptr++ = value >> 8; 72 | *ptr++ = value & 0xff; 73 | return ptr; 74 | } 75 | 76 | 77 | void stun_set_type (uint8_t *h, StunClass c, StunMethod m) 78 | { 79 | /* assert (c < 4); */ 80 | /* assert (m < (1 << 12)); */ 81 | 82 | h[0] = (c >> 1) | ((m >> 6) & 0x3e); 83 | h[1] = ((c << 4) & 0x10) | ((m << 1) & 0xe0) | (m & 0x0f); 84 | 85 | /* assert (stun_getw (h) < (1 << 14)); */ 86 | /* assert (stun_get_class (h) == c); */ 87 | /* assert (stun_get_method (h) == m); */ 88 | } 89 | 90 | 91 | StunMessageReturn stun_xor_address (const StunMessage *msg, 92 | struct sockaddr_storage *addr, socklen_t addrlen, 93 | uint32_t magic_cookie) 94 | { 95 | union { 96 | struct sockaddr_storage *addr; 97 | struct sockaddr_in *in; 98 | struct sockaddr_in6 *in6; 99 | } addr_ptr; 100 | 101 | addr_ptr.addr = addr; 102 | 103 | HEXDUMP((char*)addr,sizeof(addr),"xor"); 104 | 105 | switch (addr->ss_family) 106 | { 107 | case AF_INET: 108 | { 109 | struct sockaddr_in *ip4 = addr_ptr.in; 110 | if ((size_t) addrlen < sizeof (*ip4)) 111 | return STUN_MESSAGE_RETURN_INVALID; 112 | 113 | ip4->sin_port ^= htons (magic_cookie >> 16); 114 | ip4->sin_addr.s_addr ^= htonl (magic_cookie); 115 | return STUN_MESSAGE_RETURN_SUCCESS; 116 | } 117 | 118 | case AF_INET6: 119 | { 120 | struct sockaddr_in6 *ip6 = addr_ptr.in6; 121 | unsigned short i; 122 | 123 | if ((size_t) addrlen < sizeof (*ip6)) 124 | return STUN_MESSAGE_RETURN_INVALID; 125 | 126 | ip6->sin6_port ^= htons (magic_cookie >> 16); 127 | for (i = 0; i < 16; i++) 128 | ip6->sin6_addr.s6_addr[i] ^= msg->buffer[4 + i]; 129 | return STUN_MESSAGE_RETURN_SUCCESS; 130 | } 131 | 132 | default: 133 | return STUN_MESSAGE_RETURN_UNSUPPORTED_ADDRESS; 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /tools/Makefile.am: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile.am for the Nice Glib ICE library 3 | # 4 | # (C) 2006, 2007 Collabora Ltd. 5 | # (C) 2006, 2007 Nokia Corporation. All rights reserved. 6 | # 7 | # Licensed under MPL 1.1/LGPL 2.1. See file COPYING. 8 | # 9 | 10 | include $(top_srcdir)/common.mk 11 | AM_CFLAGS = -std=gnu99 $(LIBNICE_CFLAGS) 12 | AM_CPPFLAGS = -I$(top_srcdir) 13 | 14 | bin_PROGRAMS = stunbdc stund 15 | 16 | check_PROGRAMS = stund 17 | 18 | stund_SOURCES = stund.c stund.h 19 | stund_LDADD = $(top_builddir)/stun/libstun.la 20 | 21 | stunbdc_SOURCES = stunbdc.c 22 | 23 | stunbdc_LDADD = $(top_builddir)/stun/libstun.la 24 | 25 | 26 | if WINDOWS 27 | AM_CFLAGS += -DWINVER=0x0501 # _WIN32_WINNT_WINXP 28 | stunbdc_LDADD += -lws2_32 29 | endif 30 | -------------------------------------------------------------------------------- /tools/stund.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2007-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Rémi Denis-Courmont 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Youness Alaoui, Collabora Ltd. 26 | * Rémi Denis-Courmont, Nokia 27 | * 28 | * Alternatively, the contents of this file may be used under the terms of the 29 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 30 | * case the provisions of LGPL are applicable instead of those above. If you 31 | * wish to allow use of your version of this file only under the terms of the 32 | * LGPL and not to allow others to use your version of this file under the 33 | * MPL, indicate your decision by deleting the provisions above and replace 34 | * them with the notice and other provisions required by the LGPL. If you do 35 | * not delete the provisions above, a recipient may use your version of this 36 | * file under either the MPL or the LGPL. 37 | */ 38 | 39 | #ifdef HAVE_CONFIG_H 40 | # include 41 | #endif 42 | 43 | #ifdef __sun 44 | #define _XPG4_2 1 45 | #endif 46 | 47 | #ifndef _WIN32 48 | 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | 56 | #include 57 | 58 | 59 | #include 60 | #include 61 | #include 62 | 63 | #include 64 | #include 65 | #include 66 | 67 | #ifndef SOL_IP 68 | # define SOL_IP IPPROTO_IP 69 | #endif 70 | 71 | #ifndef SOL_IPV6 72 | # define SOL_IPV6 IPPROTO_IPV6 73 | #endif 74 | 75 | 76 | #ifndef IPV6_RECVPKTINFO 77 | # define IPV6_RECVPKTINFO IPV6_PKTINFO 78 | #endif 79 | 80 | /** Default port for STUN binding discovery */ 81 | #define IPPORT_STUN 3478 82 | 83 | #include "stun/stunagent.h" 84 | #include "stund.h" 85 | 86 | static const uint16_t known_attributes[] = { 87 | 0 88 | }; 89 | 90 | /* 91 | * Creates a listening socket 92 | */ 93 | int listen_socket (int fam, int type, int proto, unsigned int port) 94 | { 95 | int yes = 1; 96 | int fd = socket (fam, type, proto); 97 | union { 98 | struct sockaddr addr; 99 | struct sockaddr_in in; 100 | struct sockaddr_in6 in6; 101 | struct sockaddr_storage storage; 102 | } addr; 103 | if (fd == -1) 104 | { 105 | perror ("Error opening IP port"); 106 | return -1; 107 | } 108 | if (fd < 3) 109 | goto error; 110 | 111 | memset (&addr, 0, sizeof (addr)); 112 | addr.storage.ss_family = fam; 113 | #ifdef HAVE_SA_LEN 114 | addr.storage.ss_len = sizeof (addr); 115 | #endif 116 | 117 | switch (fam) 118 | { 119 | case AF_INET: 120 | addr.in.sin_port = htons (port); 121 | break; 122 | 123 | case AF_INET6: 124 | #ifdef IPV6_V6ONLY 125 | setsockopt (fd, SOL_IPV6, IPV6_V6ONLY, &yes, sizeof (yes)); 126 | #endif 127 | addr.in6.sin6_port = htons (port); 128 | break; 129 | 130 | default: 131 | assert (0); /* should never be reached */ 132 | } 133 | 134 | if (bind (fd, &addr.addr, sizeof (struct sockaddr))) 135 | { 136 | perror ("Error opening IP port"); 137 | goto error; 138 | } 139 | 140 | if ((type == SOCK_DGRAM) || (type == SOCK_RAW)) 141 | { 142 | switch (fam) 143 | { 144 | case AF_INET: 145 | #ifdef IP_RECVERR 146 | setsockopt (fd, SOL_IP, IP_RECVERR, &yes, sizeof (yes)); 147 | #endif 148 | break; 149 | 150 | case AF_INET6: 151 | #ifdef IPV6_RECVERR 152 | setsockopt (fd, SOL_IPV6, IPV6_RECVERR, &yes, sizeof (yes)); 153 | #endif 154 | break; 155 | 156 | default: 157 | assert (0); /* should never be reached */ 158 | } 159 | } 160 | else 161 | { 162 | if (listen (fd, INT_MAX)) 163 | { 164 | perror ("Error opening IP port"); 165 | goto error; 166 | } 167 | } 168 | 169 | return fd; 170 | 171 | error: 172 | close (fd); 173 | return -1; 174 | } 175 | 176 | static int dgram_process (int sock, StunAgent *oldagent, StunAgent *newagent) 177 | { 178 | union { 179 | struct sockaddr_storage storage; 180 | struct sockaddr addr; 181 | } addr; 182 | socklen_t addr_len; 183 | uint8_t buf[STUN_MAX_MESSAGE_SIZE]; 184 | size_t buf_len = 0; 185 | size_t len = 0; 186 | StunMessage request; 187 | StunMessage response; 188 | StunValidationStatus validation; 189 | StunAgent *agent = NULL; 190 | 191 | addr_len = sizeof (struct sockaddr_in); 192 | len = recvfrom (sock, buf, sizeof(buf), 0, &addr.addr, &addr_len); 193 | if (len == (size_t)-1) 194 | return -1; 195 | 196 | validation = stun_agent_validate (newagent, &request, buf, len, NULL, 0); 197 | 198 | if (validation == STUN_VALIDATION_SUCCESS) { 199 | agent = newagent; 200 | } 201 | else { 202 | validation = stun_agent_validate (oldagent, &request, buf, len, NULL, 0); 203 | agent = oldagent; 204 | } 205 | 206 | /* Unknown attributes */ 207 | if (validation == STUN_VALIDATION_UNKNOWN_REQUEST_ATTRIBUTE) 208 | { 209 | buf_len = stun_agent_build_unknown_attributes_error (agent, &response, buf, 210 | sizeof (buf), &request); 211 | goto send_buf; 212 | } 213 | 214 | /* Mal-formatted packets */ 215 | if (validation != STUN_VALIDATION_SUCCESS || 216 | stun_message_get_class (&request) != STUN_REQUEST) { 217 | return -1; 218 | } 219 | 220 | switch (stun_message_get_method (&request)) 221 | { 222 | case STUN_BINDING: 223 | stun_agent_init_response (agent, &response, buf, sizeof (buf), &request); 224 | if (stun_message_has_cookie (&request)) 225 | stun_message_append_xor_addr (&response, 226 | STUN_ATTRIBUTE_XOR_MAPPED_ADDRESS, &addr.storage, addr_len); 227 | else 228 | stun_message_append_addr (&response, STUN_ATTRIBUTE_MAPPED_ADDRESS, 229 | &addr.addr, addr_len); 230 | break; 231 | 232 | case STUN_SHARED_SECRET: 233 | case STUN_ALLOCATE: 234 | case STUN_SEND: 235 | case STUN_CONNECT: 236 | case STUN_IND_SEND: 237 | case STUN_IND_DATA: 238 | case STUN_CREATEPERMISSION: 239 | case STUN_CHANNELBIND: 240 | default: 241 | if (!stun_agent_init_error (agent, &response, buf, sizeof (buf), 242 | &request, STUN_ERROR_BAD_REQUEST)) 243 | return -1; 244 | } 245 | 246 | buf_len = stun_agent_finish_message (agent, &response, NULL, 0); 247 | send_buf: 248 | len = sendto (sock, buf, buf_len, 0, &addr.addr, addr_len); 249 | return (len < buf_len) ? -1 : 0; 250 | } 251 | 252 | 253 | static int run (int family, int protocol, unsigned port) 254 | { 255 | StunAgent oldagent; 256 | StunAgent newagent; 257 | int sock = listen_socket (family, SOCK_DGRAM, protocol, port); 258 | if (sock == -1) 259 | return -1; 260 | 261 | stun_agent_init (&oldagent, known_attributes, 262 | STUN_COMPATIBILITY_RFC3489, 0); 263 | stun_agent_init (&newagent, known_attributes, 264 | STUN_COMPATIBILITY_RFC5389, STUN_AGENT_USAGE_USE_FINGERPRINT); 265 | 266 | for (;;) 267 | dgram_process (sock, &oldagent, &newagent); 268 | } 269 | 270 | 271 | /* Pretty useless dummy signal handler... 272 | * But calling exit() is needed for gcov to work properly. */ 273 | static void exit_handler (int signum) 274 | { 275 | (void)signum; 276 | exit (0); 277 | } 278 | 279 | 280 | int main (int argc, char *argv[]) 281 | { 282 | int family = AF_INET; 283 | unsigned port = IPPORT_STUN; 284 | 285 | for (;;) 286 | { 287 | int c = getopt (argc, argv, "46"); 288 | if (c == EOF) 289 | break; 290 | 291 | switch (c) 292 | { 293 | default: 294 | case '4': 295 | family = AF_INET; 296 | break; 297 | 298 | case '6': 299 | family = AF_INET6; 300 | break; 301 | } 302 | } 303 | 304 | if (optind < argc) 305 | port = atoi (argv[optind++]); 306 | 307 | signal (SIGINT, exit_handler); 308 | signal (SIGTERM, exit_handler); 309 | return run (family, IPPROTO_UDP, port) ? EXIT_FAILURE : EXIT_SUCCESS; 310 | } 311 | 312 | #else 313 | int main (int argc, char **argv) { 314 | return 0; 315 | } 316 | #endif 317 | -------------------------------------------------------------------------------- /tools/stund.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the Nice GLib ICE library. 3 | * 4 | * (C) 2008-2009 Collabora Ltd. 5 | * Contact: Youness Alaoui 6 | * (C) 2007-2009 Nokia Corporation. All rights reserved. 7 | * Contact: Rémi Denis-Courmont 8 | * 9 | * The contents of this file are subject to the Mozilla Public License Version 10 | * 1.1 (the "License"); you may not use this file except in compliance with 11 | * the License. You may obtain a copy of the License at 12 | * http://www.mozilla.org/MPL/ 13 | * 14 | * Software distributed under the License is distributed on an "AS IS" basis, 15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 16 | * for the specific language governing rights and limitations under the 17 | * License. 18 | * 19 | * The Original Code is the Nice GLib ICE library. 20 | * 21 | * The Initial Developers of the Original Code are Collabora Ltd and Nokia 22 | * Corporation. All Rights Reserved. 23 | * 24 | * Contributors: 25 | * Youness Alaoui, Collabora Ltd. 26 | * Rémi Denis-Courmont, Nokia 27 | * 28 | * Alternatively, the contents of this file may be used under the terms of the 29 | * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which 30 | * case the provisions of LGPL are applicable instead of those above. If you 31 | * wish to allow use of your version of this file only under the terms of the 32 | * LGPL and not to allow others to use your version of this file under the 33 | * MPL, indicate your decision by deleting the provisions above and replace 34 | * them with the notice and other provisions required by the LGPL. If you do 35 | * not delete the provisions above, a recipient may use your version of this 36 | * file under either the MPL or the LGPL. 37 | */ 38 | 39 | #ifndef NICE_STUN_STUND_H 40 | # define NICE_STUN_STUND_H 1 41 | 42 | int listen_socket (int fam, int type, int proto, unsigned port); 43 | ssize_t send_safe (int fd, const struct msghdr *msg); 44 | ssize_t recv_safe (int fd, struct msghdr *msg); 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2016 Jackie Dinh 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 1 Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2 Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 3 Neither the name of the nor the 14 | * names of its contributors may be used to endorse or promote products 15 | * derived from this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | * 28 | * @(#)utils.c 29 | */ 30 | 31 | #include "cice/utils.h" 32 | 33 | /* resolve seconds carry */ 34 | static inline void update_tv(struct timeval *t1) 35 | { 36 | while (t1->tv_usec >= MILLION_I) { 37 | t1->tv_sec++; 38 | t1->tv_usec -= MILLION_I; 39 | } 40 | while (t1->tv_usec < 0) { 41 | t1->tv_sec--; 42 | t1->tv_usec += MILLION_I; 43 | } 44 | } 45 | 46 | void 47 | add_microseconds_to_timeval(struct timeval *t, uint32_t microseconds) { 48 | if (t == NULL ) 49 | return; 50 | t->tv_usec += microseconds; 51 | update_tv(t); 52 | } 53 | 54 | /*void timeval_add(struct timeval *t1, struct timeval *t2) 55 | { 56 | t1->tv_sec += t2->tv_sec; 57 | t1->tv_usec += t2->tv_usec; 58 | update_tv(t1); 59 | }*/ 60 | 61 | void 62 | print_timeval(struct timeval *t) { 63 | if ( t != NULL ) 64 | ICE_DEBUG("timevale info, tv_sec=%lu, tv_usec=%lu",t->tv_sec,t->tv_usec); 65 | return; 66 | } 67 | 68 | 69 | --------------------------------------------------------------------------------