Project Alice
Loading...
Searching...
No Matches
pcp_utils.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2014 by Cisco Systems, Inc.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef PCP_UTILS_H_
27#define PCP_UTILS_H_
28
29#include <stdlib.h>
30#include <stdint.h>
31#include <string.h>
32#include "pcp_logger.h"
33#include "pcp_client_db.h"
34
35 #ifdef __GNUC__
36 #define UNUSED __attribute__ ((unused))
37 #else
38 #define UNUSED
39 #endif
40
41#ifdef _MSC_VER
42/* variable num of arguments*/
43#define DUPPRINT(fp, fmt, ...) \
44 do { \
45 printf(fmt, __VA_ARGS__); \
46 if (fp != NULL) { \
47 fprintf(fp, fmt, __VA_ARGS__); \
48 } \
49 } while(0)
50#else /*WIN32*/
51#define DUPPRINT(fp, fmt...) \
52 do { \
53 printf(fmt); \
54 if (fp != NULL) { \
55 fprintf(fp,fmt); \
56 } \
57 } while(0)
58#endif /*WIN32*/
59
60#define log_err(STR) \
61 do { \
62 printf("%s:%d "#STR": %s \n", __FUNCTION__, __LINE__, strerror(errno)); \
63 } while (0)
64
65#define log_debug_scr(STR) \
66 do { \
67 printf("%s:%d %s \n", __FUNCTION__, __LINE__, STR); \
68 } while (0)
69
70#define log_debug(STR) \
71 do { \
72 printf("%s:%d "#STR" \n", __FUNCTION__, __LINE__); \
73 } while (0)
74
75#define CHECK_RET_EXIT(func) \
76 do { \
77 if (func < 0) { \
78 log_err(""); \
79 exit (EXIT_FAILURE); \
80 } \
81 } while(0)
82
83#define CHECK_NULL_EXIT(func) \
84 do { \
85 if (func == NULL) { \
86 log_err(""); \
87 exit (EXIT_FAILURE); \
88 } \
89 } while(0)
90
91
92#define CHECK_RET(func) \
93 do { \
94 if (func < 0) { \
95 log_err(""); \
96 } \
97 } while(0)
98
99#define CHECK_RET_GOTO_ERROR(func) \
100 do { \
101 if (func < 0) { \
102 log_err(""); \
103 goto ERROR; \
104 } \
105 } while(0)
106
107
108
109#define OSDEP(x) (void)(x)
110
111#ifdef s6_addr32
112#define S6_ADDR32(sa6) (sa6)->s6_addr32
113#else
114#define S6_ADDR32(sa6) ((uint32_t *)((sa6)->s6_addr))
115#endif
116
117#define IPV6_ADDR_COPY(dest, src) \
118 do { \
119 (S6_ADDR32(dest))[0]=(S6_ADDR32(src))[0]; \
120 (S6_ADDR32(dest))[1]=(S6_ADDR32(src))[1]; \
121 (S6_ADDR32(dest))[2]=(S6_ADDR32(src))[2]; \
122 (S6_ADDR32(dest))[3]=(S6_ADDR32(src))[3]; \
123 } while (0)
124
125#include "pcp_msg.h"
126static inline int compare_epochs(pcp_recv_msg_t *f, pcp_server_t *s)
127{
128 uint32_t c_delta;
129 uint32_t s_delta;
130
131 if (s->epoch == ~0u) {
132 s->epoch=f->recv_epoch;
133 s->cepoch=f->received_time;
134 }
135 c_delta=(uint32_t)(f->received_time - s->cepoch);
136 s_delta=f->recv_epoch - s->epoch;
137
139 "Epoch - client delta = %u, server delta = %u",
140 c_delta, s_delta);
141
142 return (c_delta + 2 < s_delta - (s_delta >> 4))
143 || (s_delta + 2 < c_delta - (c_delta >> 4));
144}
145
146inline static void timeval_align(struct timeval *x)
147{
148 x->tv_sec+=x->tv_usec / 1000000;
149 x->tv_usec=x->tv_usec % 1000000;
150 if (x->tv_usec<0) {
151 x->tv_usec=1000000 + x->tv_usec;
152 x->tv_sec-=1;
153 }
154}
155
156inline static int timeval_comp(struct timeval *x, struct timeval *y)
157{
158 timeval_align(x);
159 timeval_align(y);
160 if (x->tv_sec < y->tv_sec) {
161 return -1;
162 } else if (x->tv_sec > y->tv_sec) {
163 return 1;
164 } else if (x->tv_usec < y->tv_usec) {
165 return -1;
166 } else if (x->tv_usec > y->tv_usec) {
167 return 1;
168 } else {
169 return 0;
170 }
171}
172
173inline static int timeval_subtract(struct timeval *result, struct timeval *x,
174 struct timeval *y)
175{
176 int ret=timeval_comp(x, y);
177
178 if (ret<=0) {
179 result->tv_sec=0;
180 result->tv_usec=0;
181 return 1;
182 }
183
184 // in case that tv_usec is unsigned -> perform the carry
185 if (x->tv_usec < y->tv_usec) {
186 int nsec=(y->tv_usec - x->tv_usec) / 1000000 + 1;
187 y->tv_usec-=1000000 * nsec;
188 y->tv_sec+=nsec;
189 }
190
191 /* Compute the time remaining to wait.
192 tv_usec is certainly positive. */
193 result->tv_sec=x->tv_sec - y->tv_sec;
194 result->tv_usec=x->tv_usec - y->tv_usec;
195 timeval_align(result);
196
197 /* Return 1 if result is negative. */
198 return ret <= 0;
199}
200
201/* Nonce is part of the MAP and PEER requests/responses
202 as of version 2 of the PCP protocol */
203static inline void createNonce(struct pcp_nonce *nonce_field)
204{
205 int i;
206 for (i = 2; i >= 0; --i)
207#ifdef WIN32
208 nonce_field->n[i]=htonl (rand());
209#else //WIN32
210 nonce_field->n[i]=htonl(random());
211#endif //WIN32
212}
213
214#ifndef HAVE_STRNDUP
215static inline char *pcp_strndup(const char *s, size_t size) {
216 char *ret;
217 char const* end=(char const*)memchr(s, 0, size);
218
219 if (end) {
220 /* Length + 1 */
221 size=end - s + 1;
222 } else {
223 size++;
224 }
225 ret=(char*)malloc(size);
226
227 if (ret) {
228 memcpy(ret, s, size);
229 ret[size-1]='\0';
230 }
231 return ret;
232}
233#define strndup pcp_strndup
234#endif
235
236#endif /* PCP_UTILS_H_ */
uint32_t size(sys::state const &state)
uint uint32_t
@ PCP_LOGLVL_DEBUG
Definition: pcp.h:90
#define PCP_LOG(level, fmt,...)
Definition: pcp_logger.h:71
uint32_t n[3]
time_t received_time
Definition: pcp_client_db.h:92
uint32_t recv_epoch
Definition: pcp_client_db.h:89
time_t cepoch
uint32_t epoch