Skip to Content
[CAIDA - Center for Applied Internet Data Analysis logo]
The Center for Applied Internet Data Analysis
utils.c
1 /*
2  * cc-common
3  *
4  * Alistair King, CAIDA, UC San Diego
5  * corsaro-info@caida.org
6  *
7  * bytes_htons, bytes_htonl, gettimeofday_wrap, malloc_zero from scamper
8  * https://www.caida.org/tools/measurement/scamper/
9  *
10  * timeval_subtract code from
11  * http://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html
12  *
13  * Copyright (C) 2012 The Regents of the University of California.
14  *
15  * This file is part of cc-common.
16  *
17  * cc-common is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * cc-common is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with cc-common. If not, see <http://www.gnu.org/licenses/>.
29  *
30  */
31 
32 #include "config.h"
33 
34 #include <arpa/inet.h>
35 #include <assert.h>
36 #include <inttypes.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #ifdef HAVE_SYS_TIME_H
42 #include <sys/time.h>
43 #endif
44 #ifdef HAVE_TIME_H
45 #include <time.h>
46 #endif
47 
48 #include "utils.h"
49 
50 void bytes_htons(uint8_t *bytes, uint16_t u16)
51 {
52  uint16_t tmp = htons(u16);
53  memcpy(bytes, &tmp, 2);
54  return;
55 }
56 
57 void bytes_htonl(uint8_t *bytes, uint32_t u32)
58 {
59  uint32_t tmp = htonl(u32);
60  memcpy(bytes, &tmp, 4);
61  return;
62 }
63 
64 void bytes_htonll(uint8_t *bytes, uint64_t u64)
65 {
66  uint64_t tmp = htonll(u64);
67  memcpy(bytes, &tmp, 8);
68  return;
69 }
70 
71 void gettimeofday_wrap(struct timeval *tv)
72 {
73  struct timezone tz;
74  gettimeofday(tv, &tz);
75  return;
76 }
77 
78 void *malloc_zero(const size_t size)
79 {
80  void *ptr;
81  if((ptr = malloc(size)) != NULL)
82  {
83  memset(ptr, 0, size);
84  }
85  return ptr;
86 }
87 
88 int timeval_subtract (struct timeval *result,
89  const struct timeval *a, const struct timeval *b)
90 {
91  struct timeval y = *b;
92 
93  /* Perform the carry for the later subtraction by updating y. */
94  if (a->tv_usec < b->tv_usec) {
95  int nsec = (b->tv_usec - a->tv_usec) / 1000000 + 1;
96  y.tv_usec -= 1000000 * nsec;
97  y.tv_sec += nsec;
98  }
99  if (a->tv_usec - b->tv_usec > 1000000) {
100  int nsec = (a->tv_usec - b->tv_usec) / 1000000;
101  y.tv_usec += 1000000 * nsec;
102  y.tv_sec -= nsec;
103  }
104 
105  /* Compute the time remaining to wait.
106  tv_usec is certainly positive. */
107  result->tv_sec = a->tv_sec - y.tv_sec;
108  result->tv_usec = a->tv_usec - y.tv_usec;
109 
110  /* Return 1 if result is negative. */
111  return a->tv_sec < y.tv_sec;
112 }
113 
114 void chomp(char *line)
115 {
116  char *newln;
117  if((newln = strchr(line, '\n')) != NULL)
118  {
119  *newln = '\0';
120  }
121 }
void bytes_htonl(uint8_t *bytes, uint32_t u32)
Convert a host ordered long to a network ordered byte array.
Definition: utils.c:57
Header file for common utility functions.
void * malloc_zero(const size_t size)
Allocate memory and set it to zero.
Definition: utils.c:78
#define htonll(x)
Byte-swap a 64-bit integer.
Definition: utils.h:72
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