Skip to Content
[CAIDA - Center for Applied Internet Data Analysis logo]
The Center for Applied Internet Data Analysis
utils.h
Go to the documentation of this file.
1 /*
2  * cc-common
3  *
4  * Alistair King, CAIDA, UC San Diego
5  * corsaro-info@caida.org
6  *
7  * ntholl and htonll macros from
8  * http://www.codeproject.com/KB/cpp/endianness.aspx
9  *
10  * Other functions from scamper as noted in utils.c
11  *
12  * Copyright (C) 2012 The Regents of the University of California.
13  *
14  * This file is part of cc-common.
15  *
16  * cc-common is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * cc-common is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with cc-common. If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30 
31 #ifndef __UTILS_H
32 #define __UTILS_H
33 
34 #include "config.h"
35 
36 #include <inttypes.h>
37 
38 #ifdef HAVE_SYS_TIME_H
39 #include <sys/time.h>
40 #endif
41 #ifdef HAVE_TIME_H
42 #include <time.h>
43 #endif
44 
54 #define XSTR(a) #a
55 
57 #define STR(a) XSTR(a)
58 
60 #define ARR_CNT(a) (sizeof(a) / sizeof(a[0]))
61 
62 /* ntholl and htonll macros from
63  http://www.codeproject.com/KB/cpp/endianness.aspx */
65 #ifndef ntohll
66 #define ntohll(x) (((uint64_t)(ntohl((int)((x << 32) >> 32))) << 32) | \
67  (uint32_t)ntohl(((int)(x >> 32))))
68 #endif
69 
71 #ifndef htonll
72 #define htonll(x) ntohll(x)
73 #endif
74 
80 void bytes_htons(uint8_t *bytes, uint16_t u16);
81 
87 void bytes_htonl(uint8_t *bytes, uint32_t u32);
88 
94 void bytes_htonll(uint8_t *bytes, uint64_t u64);
95 
100 void gettimeofday_wrap(struct timeval *tv);
101 
107 void *malloc_zero(const size_t size);
108 
116 int timeval_subtract (struct timeval *result,
117  const struct timeval *x, const struct timeval *y);
118 
126 void chomp(char *line);
127 
128 #endif /* __UTILS_H */
void bytes_htonl(uint8_t *bytes, uint32_t u32)
Convert a host ordered long to a network ordered byte array.
Definition: utils.c:57
void * malloc_zero(const size_t size)
Allocate memory and set it to zero.
Definition: utils.c:78
void bytes_htons(uint8_t *bytes, uint16_t u16)
Convert a host ordered short to a network ordered byte array.
Definition: utils.c:50
void chomp(char *line)
Remove a newline from the given string.
Definition: utils.c:114
void bytes_htonll(uint8_t *bytes, uint64_t u64)
Convert a host ordered long-long (64 bit) to a network ordered byte array.
Definition: utils.c:64
void gettimeofday_wrap(struct timeval *tv)
Convenience function to get the current time of day.
Definition: utils.c:71
int timeval_subtract(struct timeval *result, const struct timeval *x, const struct timeval *y)
Find the delta between two timevals.
Definition: utils.c:88