1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/srtp/src/crypto/replay/ut_sim.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,105 @@ 1.4 +/* 1.5 + * ut_sim.c 1.6 + * 1.7 + * an unreliable transport simulator 1.8 + * (for testing replay databases and suchlike) 1.9 + * 1.10 + * David A. McGrew 1.11 + * Cisco Systems, Inc. 1.12 + */ 1.13 + 1.14 +/* 1.15 + * 1.16 + * Copyright (c) 2001-2006, Cisco Systems, Inc. 1.17 + * All rights reserved. 1.18 + * 1.19 + * Redistribution and use in source and binary forms, with or without 1.20 + * modification, are permitted provided that the following conditions 1.21 + * are met: 1.22 + * 1.23 + * Redistributions of source code must retain the above copyright 1.24 + * notice, this list of conditions and the following disclaimer. 1.25 + * 1.26 + * Redistributions in binary form must reproduce the above 1.27 + * copyright notice, this list of conditions and the following 1.28 + * disclaimer in the documentation and/or other materials provided 1.29 + * with the distribution. 1.30 + * 1.31 + * Neither the name of the Cisco Systems, Inc. nor the names of its 1.32 + * contributors may be used to endorse or promote products derived 1.33 + * from this software without specific prior written permission. 1.34 + * 1.35 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.36 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.37 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 1.38 + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 1.39 + * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 1.40 + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 1.41 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 1.42 + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1.43 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 1.44 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.45 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 1.46 + * OF THE POSSIBILITY OF SUCH DAMAGE. 1.47 + * 1.48 + */ 1.49 + 1.50 + 1.51 +#include "ut_sim.h" 1.52 + 1.53 + 1.54 +int 1.55 +ut_compar(const void *a, const void *b) { 1.56 + return rand() > (RAND_MAX/2) ? -1 : 1; 1.57 +} 1.58 + 1.59 +void 1.60 +ut_init(ut_connection *utc) { 1.61 + int i; 1.62 + utc->index = 0; 1.63 + 1.64 + for (i=0; i < UT_BUF; i++) 1.65 + utc->buffer[i] = i; 1.66 + 1.67 + qsort(utc->buffer, UT_BUF, sizeof(uint32_t), ut_compar); 1.68 + 1.69 + utc->index = UT_BUF - 1; 1.70 +} 1.71 + 1.72 +uint32_t 1.73 +ut_next_index(ut_connection *utc) { 1.74 + uint32_t tmp; 1.75 + 1.76 + tmp = utc->buffer[0]; 1.77 + utc->index++; 1.78 + utc->buffer[0] = utc->index; 1.79 + 1.80 + qsort(utc->buffer, UT_BUF, sizeof(uint32_t), ut_compar); 1.81 + 1.82 + return tmp; 1.83 +} 1.84 + 1.85 + 1.86 + 1.87 +#ifdef UT_TEST 1.88 + 1.89 +#include <stdio.h> 1.90 + 1.91 +int 1.92 +main() { 1.93 + uint32_t i, irecvd, idiff; 1.94 + ut_connection utc; 1.95 + 1.96 + ut_init(&utc); 1.97 + 1.98 + for (i=0; i < 1000; i++) { 1.99 + irecvd = ut_next_index(&utc); 1.100 + idiff = i - irecvd; 1.101 + printf("%lu\t%lu\t%d\n", i, irecvd, idiff); 1.102 + } 1.103 + 1.104 + return 0; 1.105 +} 1.106 + 1.107 + 1.108 +#endif