-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
2134 lines (1799 loc) · 37.6 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* utils.c
*
* $Id: utils.c,v 1.166 2013/08/02 19:04:10 mjl Exp $
*
* Copyright (C) 2003-2006 Matthew Luckie
* Copyright (C) 2006-2011 The University of Waikato
* Copyright (C) 2011 Matthew Luckie
* Copyright (C) 2012-2013 The Regents of the University of California
* Author: Matthew Luckie
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifndef lint
static const char rcsid[] =
"$Id: utils.c,v 1.166 2013/08/02 19:04:10 mjl Exp $";
#endif
#if defined(_MSC_VER)
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
typedef int ssize_t;
#endif
#if defined(__APPLE__)
#define _BSD_SOCKLEN_T_
#define HAVE_SOCKADDR_SA_LEN
#include <stdint.h>
#endif
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
#define HAVE_SOCKADDR_SA_LEN
#endif
#include <sys/types.h>
#include <sys/stat.h>
#ifndef _WIN32
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <sys/param.h>
#include <sys/un.h>
#endif
#if !defined(__sun__) && !defined (_WIN32) && !defined(__CYGWIN__)
#include <sys/sysctl.h>
#endif
#ifndef _WIN32
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
#if defined(AF_LINK)
#include <net/if_dl.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#define _CRT_RAND_S
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <fcntl.h>
#include <ctype.h>
#include <limits.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <io.h>
#include <direct.h>
#define snprintf _snprintf
#define read _read
#define write _write
#define strdup _strdup
#define mkdir(dir,mode) _mkdir(dir)
#endif
#include <assert.h>
#if defined(DMALLOC)
#include <dmalloc.h>
#endif
#if defined(__sun__)
# define s6_addr32 _S6_un._S6_u32
#elif !defined(s6_addr32)
# define s6_addr32 __u6_addr.__u6_addr32
#endif
#include "utils.h"
#if defined(HAVE_SOCKADDR_SA_LEN)
int sockaddr_len(const struct sockaddr *sa)
{
return sa->sa_len;
}
#else
int sockaddr_len(const struct sockaddr *sa)
{
if(sa->sa_family == AF_INET) return sizeof(struct sockaddr_in);
if(sa->sa_family == AF_INET6) return sizeof(struct sockaddr_in6);
#if defined(AF_LINK)
if(sa->sa_family == AF_LINK) return sizeof(struct sockaddr_dl);
#endif
#if defined(AF_UNIX) && !defined(_WIN32)
if(sa->sa_family == AF_UNIX) return sizeof(struct sockaddr_un);
#endif
return -1;
}
#endif
void *addr_dup(const int af, const void *addr_in)
{
void *addr;
size_t size;
if(af == AF_INET)
{
size = sizeof(struct in_addr);
}
else if(af == AF_INET6)
{
size = sizeof(struct in6_addr);
}
else return NULL;
if((addr = malloc(size)) != NULL)
{
memcpy(addr, addr_in, size);
}
return addr;
}
struct sockaddr *sockaddr_dup(const struct sockaddr *sa)
{
struct sockaddr *addr;
int len;
if((len = sockaddr_len(sa)) <= 0)
{
return NULL;
}
if((addr = malloc((size_t)len)) != NULL)
{
memcpy(addr, sa, (size_t)len);
}
return addr;
}
int sockaddr_compose(struct sockaddr *sa,
const int af, const void *addr, const int port)
{
socklen_t len;
struct sockaddr_in *sin4;
struct sockaddr_in6 *sin6;
assert(port >= 0);
assert(port <= 65535);
if(af == AF_INET)
{
len = sizeof(struct sockaddr_in);
memset(sa, 0, len);
sin4 = (struct sockaddr_in *)sa;
if(addr != NULL) memcpy(&sin4->sin_addr, addr, sizeof(struct in_addr));
sin4->sin_port = htons(port);
}
else if(af == AF_INET6)
{
len = sizeof(struct sockaddr_in6);
memset(sa, 0, len);
sin6 = (struct sockaddr_in6 *)sa;
if(addr != NULL) memcpy(&sin6->sin6_addr, addr, sizeof(struct in6_addr));
sin6->sin6_port = htons(port);
}
else return -1;
#if defined(HAVE_SOCKADDR_SA_LEN)
sa->sa_len = len;
#endif
sa->sa_family = af;
return 0;
}
int sockaddr_compose_un(struct sockaddr *sa, const char *file)
{
#if defined(AF_UNIX) && !defined(_WIN32)
struct sockaddr_un *sn = (struct sockaddr_un *)sa;
if(strlen(file) + 1 > sizeof(sn->sun_path))
return -1;
memset(sn, 0, sizeof(sn));
sn->sun_family = AF_UNIX;
snprintf(sn->sun_path, sizeof(sn->sun_path), "%s", file);
#if defined (HAVE_SOCKADDR_SA_LEN)
sn->sun_len = sizeof(struct sockaddr_un);
#endif
return 0;
#else
errno = EINVAL;
return -1;
#endif
}
#if defined(AF_LINK)
static char *link_tostr(const struct sockaddr_dl *sdl,
char *buf, const size_t len)
{
static const char hex[] = "01234567890abcdef";
size_t off = 0;
uint8_t *u8, i;
if((off = snprintf(buf, len, "t%d", sdl->sdl_type)) + 1 > len)
{
return NULL;
}
if(sdl->sdl_nlen == 0 && sdl->sdl_alen == 0)
{
return buf;
}
else
{
buf[off++] = '.';
}
/* check for enough remaining space */
if((size_t)(sdl->sdl_nlen + 1 + (3 * sdl->sdl_alen)) > len-off)
{
return NULL;
}
if(sdl->sdl_nlen > 0)
{
memcpy(buf+off, sdl->sdl_data, sdl->sdl_nlen);
off += sdl->sdl_nlen;
if(sdl->sdl_alen > 0)
{
buf[off++] = '.';
}
else
{
buf[off] = '\0';
return buf;
}
}
if(sdl->sdl_alen > 0)
{
u8 = (uint8_t *)LLADDR(sdl);
for(i=0; i<sdl->sdl_alen; i++)
{
buf[off++] = hex[u8[i] & 0xf];
buf[off++] = hex[(u8[i] >> 4) & 0xf];
buf[off++] = ':';
}
buf[off-1] = '\0';
}
return buf;
}
#endif
char *sockaddr_tostr(const struct sockaddr *sa, char *buf, const size_t len)
{
char addr[128];
if(sa->sa_family == AF_INET)
{
#ifndef _WIN32
if(inet_ntop(AF_INET, &((const struct sockaddr_in *)sa)->sin_addr,
addr, sizeof(addr)) == NULL)
{
return NULL;
}
#else
if(getnameinfo(sa, sizeof(struct sockaddr_in), addr, sizeof(addr),
NULL, 0, NI_NUMERICHOST) != 0)
{
return NULL;
}
#endif
snprintf(buf, len, "%s:%d", addr,
((const struct sockaddr_in *)sa)->sin_port);
}
else if(sa->sa_family == AF_INET6)
{
#ifndef _WIN32
if(inet_ntop(AF_INET6, &((const struct sockaddr_in6 *)sa)->sin6_addr,
addr, sizeof(addr)) == NULL)
{
return NULL;
}
#else
if(getnameinfo(sa, sizeof(struct sockaddr_in6), addr, sizeof(addr),
NULL, 0, NI_NUMERICHOST) != 0)
{
return NULL;
}
#endif
snprintf(buf, len, "%s.%d", addr,
((const struct sockaddr_in6 *)sa)->sin6_port);
}
#if defined(AF_LINK)
else if(sa->sa_family == AF_LINK)
{
return link_tostr((const struct sockaddr_dl *)sa, buf, len);
}
#endif
#if defined(AF_UNIX) && !defined(_WIN32)
else if(sa->sa_family == AF_UNIX)
{
snprintf(buf, len, "%s", ((const struct sockaddr_un *)sa)->sun_path);
}
#endif
else
{
return NULL;
}
return buf;
}
int addr4_cmp(const void *va, const void *vb)
{
const struct in_addr *a = (const struct in_addr *)va;
const struct in_addr *b = (const struct in_addr *)vb;
if(a->s_addr < b->s_addr) return -1;
if(a->s_addr > b->s_addr) return 1;
return 0;
}
int addr6_cmp(const void *va, const void *vb)
{
const struct in6_addr *a = (const struct in6_addr *)va;
const struct in6_addr *b = (const struct in6_addr *)vb;
int i;
#ifndef _WIN32
for(i=0; i<4; i++)
{
if(a->s6_addr32[i] < b->s6_addr32[i]) return -1;
if(a->s6_addr32[i] > b->s6_addr32[i]) return 1;
}
#else
for(i=0; i<8; i++)
{
if(a->u.Word[i] < b->u.Word[i]) return -1;
if(a->u.Word[i] > b->u.Word[i]) return 1;
}
#endif
return 0;
}
/*
* addr_cmp:
* this function is used to provide a sorting order, not for advising the
* numerical order of the addresses passed in.
*/
int addr_cmp(const int af, const void *a, const void *b)
{
if(af == AF_INET) return addr4_cmp(a, b);
if(af == AF_INET6) return addr6_cmp(a, b);
return 0;
}
#ifndef _WIN32
const char *addr_tostr(int af, const void *addr, char *buf, size_t len)
{
return inet_ntop(af, addr, buf, len);
}
#endif
#ifdef _WIN32
const char *addr_tostr(int af, const void *addr, char *buf, size_t len)
{
struct sockaddr_storage sas;
if(sockaddr_compose((struct sockaddr *)&sas, af, addr, 0) != 0)
return NULL;
if(getnameinfo((const struct sockaddr *)&sas, sizeof(sas), buf, len,
NULL, 0, NI_NUMERICHOST) != 0)
{
return NULL;
}
return buf;
}
#endif
/*
* memdup
*
* duplicate some memory.
*/
#ifndef DMALLOC
void *memdup(const void *ptr, const size_t len)
{
void *d;
if((d = malloc(len)) != NULL)
{
memcpy(d, ptr, len);
}
return d;
}
#endif
/*
* malloc_zero
*
* allocate some memory, zero it, and return a pointer to it.
*/
#ifndef DMALLOC
void *malloc_zero(const size_t size)
{
void *ptr;
if((ptr = malloc(size)) != NULL)
{
memset(ptr, 0, size);
}
return ptr;
}
#else
void *malloc_zero_dm(const size_t size, const char *file, const int line)
{
void *ptr;
if((ptr = dmalloc_malloc(file,line,size,DMALLOC_FUNC_MALLOC,0,0)) != NULL)
{
memset(ptr, 0, size);
}
return ptr;
}
#endif
#ifndef DMALLOC
int realloc_wrap(void **ptr, size_t len)
{
void *tmp;
if(len != 0)
{
if(*ptr != NULL)
tmp = realloc(*ptr, len);
else
tmp = malloc(len);
if(tmp != NULL)
{
*ptr = tmp;
return 0;
}
}
else
{
if(*ptr != NULL)
{
free(*ptr);
*ptr = NULL;
}
return 0;
}
return -1;
}
#endif
#ifdef DMALLOC
int realloc_wrap_dm(void **ptr, size_t len, const char *file, const int line)
{
void *tmp;
if(len != 0)
{
if(*ptr != NULL)
tmp = dmalloc_realloc(file, line, *ptr, len, DMALLOC_FUNC_REALLOC, 0);
else
tmp = dmalloc_malloc(file, line, len, DMALLOC_FUNC_MALLOC, 0, 0);
if(tmp != NULL)
{
*ptr = tmp;
return 0;
}
}
else
{
if(*ptr != NULL)
{
dmalloc_free(file, line, *ptr, DMALLOC_FUNC_FREE);
*ptr = NULL;
}
return 0;
}
return -1;
}
#endif
int array_findpos(void **array, int nmemb, const void *item, array_cmp_t cmp)
{
int l, r, k, i;
if(nmemb == 0)
{
return -1;
}
l = 0;
r = nmemb-1;
if(r == 0)
{
if(cmp(array[0], item) == 0)
return 0;
return -1;
}
while(l <= r)
{
k = (l + r) / 2;
i = cmp(array[k], item);
if(i > 0)
r = k-1;
else if(i < 0)
l = k+1;
else
return k;
}
return -1;
}
void *array_find(void **array, int nmemb, const void *item, array_cmp_t cmp)
{
int k = array_findpos(array, nmemb, item, cmp);
if(k >= 0)
return array[k];
return NULL;
}
/*
* array_insert_0
*
* handy function that deals with inserting an element into an array
* and then sorting the array, if necessary. using mergesort because
* the array is likely to have pre-existing order.
*/
static int array_insert_0(void **array,int *nmemb,void *item,array_cmp_t cmp)
{
array[*nmemb] = item;
*nmemb = *nmemb + 1;
if(cmp != NULL)
array_qsort(array, *nmemb, cmp);
return 0;
}
#ifndef DMALLOC
int array_insert(void ***array, int *nmemb, void *item, array_cmp_t cmp)
{
size_t len;
assert(nmemb != NULL && *nmemb >= 0);
len = ((*nmemb) + 1) * sizeof(void *);
if(realloc_wrap((void **)array, len) != 0)
return -1;
return array_insert_0(*array, nmemb, item, cmp);
}
int array_insert_gb(void ***array, int *nmemb, int *mmemb, int growby,
void *item, array_cmp_t cmp)
{
size_t len;
assert(nmemb != NULL && *nmemb >= 0);
if(*nmemb + 1 >= *mmemb)
{
assert(*mmemb + growby > *nmemb);
len = (*mmemb + growby) * sizeof(void *);
if(realloc_wrap((void **)array, len) != 0)
return -1;
*mmemb += growby;
}
return array_insert_0(*array, nmemb, item, cmp);
}
#endif
#ifdef DMALLOC
int array_insert_dm(void ***array, int *nmemb, void *item,
array_cmp_t cmp, const char *file, const int line)
{
size_t len;
assert(nmemb != NULL && *nmemb >= 0);
len = ((*nmemb) + 1) * sizeof(void *);
if(realloc_wrap_dm((void **)array, len, file, line) != 0)
return -1;
return array_insert_0(*array, nmemb, item, cmp);
}
int array_insert_gb_dm(void ***array, int *nmemb, int *mmemb, int growby,
void *item, array_cmp_t cmp,
const char *file, const int line)
{
size_t len;
assert(nmemb != NULL && *nmemb >= 0);
if(*nmemb + 1 >= *mmemb)
{
assert(*mmemb + growby > *nmemb);
len = (*mmemb + growby) * sizeof(void *);
if(realloc_wrap_dm((void **)array, len, file, line) != 0)
return -1;
*mmemb += growby;
}
return array_insert_0(*array, nmemb, item, cmp);
}
#endif
void array_remove(void **array, int *nmemb, int p)
{
assert(p >= 0 && p < *nmemb);
memmove(array+p, array+p+1, ((*nmemb)-p-1) * sizeof(void *));
*nmemb = *nmemb - 1;
return;
}
static void array_swap(void **a, int i, int j)
{
void *item = a[i];
a[i] = a[j];
a[j] = item;
return;
}
static void array_qsort_0(void **a, array_cmp_t cmp, int l, int r)
{
void *c;
int i, p;
if(l >= r)
return;
array_swap(a, (l+r)/2, l);
c = a[l];
p = l;
for(i=l+1; i<=r; i++)
{
if(cmp(a[i], c) < 0)
{
p++;
array_swap(a, p, i);
}
}
array_swap(a, p, l);
array_qsort_0(a, cmp, l, p-1);
array_qsort_0(a, cmp, p+1, r);
return;
}
void array_qsort(void **a, int n, array_cmp_t cmp)
{
array_qsort_0(a, cmp, 0, n-1);
return;
}
#ifndef _WIN32
void gettimeofday_wrap(struct timeval *tv)
{
struct timezone tz;
gettimeofday(tv, &tz);
return;
}
#endif
#ifdef _WIN32
void gettimeofday_wrap(struct timeval *tv)
{
FILETIME ft;
uint64_t u64;
GetSystemTimeAsFileTime(&ft);
u64 = ft.dwHighDateTime;
u64 <<= 32;
u64 |= ft.dwLowDateTime;
u64 /= 10;
u64 -= 11644473600000000LL;
tv->tv_sec = (long)(u64 / 1000000UL);
tv->tv_usec = (long)(u64 % 1000000UL);
return;
}
#endif
int timeval_cmp(const struct timeval *a, const struct timeval *b)
{
if(a->tv_sec < b->tv_sec) return -1;
if(a->tv_sec > b->tv_sec) return 1;
if(a->tv_usec < b->tv_usec) return -1;
if(a->tv_usec > b->tv_usec) return 1;
return 0;
}
static void timeval_handlewrap(struct timeval *tv)
{
if(tv->tv_usec >= 1000000)
{
tv->tv_sec++;
tv->tv_usec -= 1000000;
}
else if(tv->tv_usec < 0)
{
tv->tv_sec--;
tv->tv_usec += 1000000;
}
return;
}
void timeval_add_cs(struct timeval *out, const struct timeval *in, int cs)
{
out->tv_sec = in->tv_sec + (cs / 100);
out->tv_usec = in->tv_usec + ((cs % 100) * 10000);
timeval_handlewrap(out);
return;
}
void timeval_add_ms(struct timeval *out, const struct timeval *in, int msec)
{
out->tv_sec = in->tv_sec + (msec / 1000);
out->tv_usec = in->tv_usec + ((msec % 1000) * 1000);
timeval_handlewrap(out);
return;
}
void timeval_add_us(struct timeval *out, const struct timeval *in, int us)
{
out->tv_sec = in->tv_sec + (us / 1000000);
out->tv_usec = in->tv_usec + (us % 1000000);
timeval_handlewrap(out);
return;
}
void timeval_add_s(struct timeval *out, const struct timeval *in, int s)
{
out->tv_sec = in->tv_sec + s;
out->tv_usec = in->tv_usec;
return;
}
void timeval_sub_us(struct timeval *out, const struct timeval *in, int us)
{
out->tv_sec = in->tv_sec - (us / 1000000);
out->tv_usec = in->tv_usec - (us % 1000000);
timeval_handlewrap(out);
return;
}
void timeval_add_tv(struct timeval *tv, const struct timeval *add)
{
assert(add->tv_sec >= 0);
assert(add->tv_usec >= 0);
tv->tv_sec += add->tv_sec;
tv->tv_usec += add->tv_usec;
/* check for overflow */
if(tv->tv_usec > 1000000)
{
tv->tv_sec++;
tv->tv_usec -= 1000000;
}
return;
}
void timeval_add_tv3(struct timeval *out,
const struct timeval *a, const struct timeval *b)
{
assert(a->tv_sec >= 0); assert(a->tv_usec >= 0);
assert(b->tv_sec >= 0); assert(b->tv_usec >= 0);
out->tv_sec = a->tv_sec + b->tv_sec;
out->tv_usec = a->tv_usec + b->tv_usec;
if(out->tv_usec > 1000000)
{
out->tv_sec++;
out->tv_usec -= 1000000;
}
return;
}
void timeval_diff_tv(struct timeval *rtt,
const struct timeval *from, const struct timeval *to)
{
rtt->tv_sec = to->tv_sec - from->tv_sec;
rtt->tv_usec = to->tv_usec - from->tv_usec;
if(rtt->tv_usec < 0)
{
rtt->tv_sec--;
rtt->tv_usec += 1000000;
}
return;
}
/*
* timeval_diff_ms
* return the millisecond difference between the two timevals.
* a - b
*/
int timeval_diff_ms(const struct timeval *a, const struct timeval *b)
{
struct timeval tv;
timeval_diff_tv(&tv, b, a);
return ((int)tv.tv_sec * 1000) + ((int)tv.tv_usec / 1000);
}
/*
* timeval_diff_us
* return the microsecond difference between the two timevals.
* a - b
*/
int timeval_diff_us(const struct timeval *a, const struct timeval *b)
{
struct timeval tv;
timeval_diff_tv(&tv, b, a);
return ((int)tv.tv_sec * 1000000) + tv.tv_usec;
}
void timeval_cpy(struct timeval *dst, const struct timeval *src)
{
memcpy(dst, src, sizeof(struct timeval));
return;
}
int timeval_inrange_us(const struct timeval *a, const struct timeval *b, int c)
{
struct timeval tv;
int rc = timeval_cmp(a, b);
if(rc < 0)
{
timeval_add_us(&tv, a, c);
if(timeval_cmp(&tv, b) < 0)
return 0;
}
else if(rc > 0)
{
timeval_add_us(&tv, b, c);
if(timeval_cmp(&tv, a) < 0)
return 0;
}
return 1;
}
char *timeval_tostr(const struct timeval *rtt, char *str, size_t len)
{
uint32_t usec = (rtt->tv_sec * 1000000) + rtt->tv_usec;
snprintf(str, len, "%d.%03d", usec / 1000, usec % 1000);
return str;
}
#ifndef _WIN32
int fcntl_unset(const int fd, const int flags)
{
int i;
if((i = fcntl(fd, F_GETFL, 0)) == -1)
{
return -1;
}
if(fcntl(fd, F_SETFL, i & (~flags)) == -1)
{
return -1;
}
return 0;
}
int fcntl_set(const int fd, const int flags)
{
int i;
if((i = fcntl(fd, F_GETFL, 0)) == -1)
{
return -1;
}
if(fcntl(fd, F_SETFL, i | flags) == -1)
{
return -1;
}
return 0;
}
#endif
int string_isprint(const char *str, const size_t len)
{
size_t i = 0;
for(i=0; i<len; i++)
{
if(isprint((int)str[i]) != 0)
{
continue;
}
else if(str[i] == '\0')
{
break;
}
else return 0;
}
return 1;
}
int string_tolong(const char *str, long *l)
{
char *endptr;
*l = strtol(str, &endptr, 0);
if(*l == 0)
{
if(errno == EINVAL) return -1;
}
else if(*l == LONG_MIN || *l == LONG_MAX)
{
if(errno == ERANGE) return -1;
}
return 0;
}
/*
* string_isnumber
*
* scan the word to establish if it an integer.
*/
int string_isnumber(const char *str)
{
int i = 1;
if(str[0] != '-' && str[0] != '+' && isdigit((int)str[0]) == 0)
{
return 0;
}
while(str[i] != '\0')