2 * The olsr.org Optimized Link-State Routing daemon(olsrd)
3 * Copyright (c) 2004, Andreas T�nnesen(andreto@olsr.org)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of olsr.org, olsrd nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
33 * Visit http://www.olsr.org for more information.
35 * If you find this software useful feel free to make a donation
36 * to the project. For more information see the website or contact
37 * the copyright holders.
47 #include <sys/times.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
57 #include "olsr_protocol.h"
60 extern const char olsrd_version[];
61 extern const char build_date[];
62 extern const char build_host[];
64 #ifndef OLSRD_GLOBAL_CONF_FILE
65 #define OLSRD_CONF_FILE_NAME "olsrd.conf"
66 #define OLSRD_GLOBAL_CONF_FILE "/etc/" OLSRD_CONF_FILE_NAME
69 #define MAXMESSAGESIZE 1500 /* max broadcast size */
70 #define UDP_IPV4_HDRSIZE 28
71 #define UDP_IPV6_HDRSIZE 62
73 #define MIN_PACKET_SIZE(ver) ((int)(sizeof(olsr_u8_t) * (((ver) == AF_INET) ? 4 : 7)))
75 /* Debug helper macro */
77 #define olsr_debug(lvl, format, args...) do { \
78 OLSR_PRINTF(lvl, "%s (%s:%d): ", __func__, __FILE__, __LINE__); \
79 OLSR_PRINTF(lvl, (format), ##args); \
83 extern FILE *debug_handle;
86 #define OLSR_PRINTF(lvl, format, args...) do { } while(0)
88 #define OLSR_PRINTF(lvl, format, args...) do { \
89 if((olsr_cnf->debug_level >= (lvl)) && debug_handle) \
90 fprintf(debug_handle, (format), ##args); \
95 * Provides a timestamp s1 milliseconds in the future according
96 * to system ticks returned by times(2)
98 #define GET_TIMESTAMP(s1) (now_times + ((s1) / olsr_cnf->system_tick_divider))
100 /* Compute the time in milliseconds when a timestamp will expire. */
101 #define TIME_DUE(s1) ((int)((s1) * olsr_cnf->system_tick_divider) - now_times)
103 /* Returns TRUE if a timestamp is expired */
104 #define TIMED_OUT(s1) ((int)((s1) - now_times) < 0)
107 #define ARRAYSIZE(x) (sizeof(x)/sizeof(*(x)))
109 #define MAX(x,y) ((x) > (y) ? (x) : (y))
112 #define MIN(x,y) ((x) < (y) ? (x) : (y))
115 #define INLINE inline __attribute__((always_inline))
118 * A somewhat safe version of strncpy and strncat. Note, that
119 * BSD/Solaris strlcpy()/strlcat() differ in implementation, while
120 * the BSD compiler prints out a warning if you use plain strcpy().
123 static INLINE char *strscpy(char *dest, const char *src, size_t size)
125 register size_t l = 0;
126 #if !defined(NODEBUG) && defined(DEBUG)
127 if (sizeof(dest) == size) fprintf(stderr, "Warning: probably sizeof(pointer) in strscpy(%p, %s, %d)!\n", dest, src, size);
128 if (NULL == dest) fprintf(stderr, "Warning: dest is NULL in strscpy!\n");
129 if (NULL == src) fprintf(stderr, "Warning: src is NULL in strscpy!\n");
131 if (NULL != dest && NULL != src)
133 /* src does not need to be null terminated */
134 if (0 < size--) while(l < size && 0 != src[l]) l++;
137 return strncpy(dest, src, l);
140 static INLINE char *strscat(char *dest, const char *src, size_t size)
142 register size_t l = strlen(dest);
143 return strscpy(dest + l, src, size > l ? size - l : 0);
150 /* First "argument" is NOT a pointer! */
152 #define QUEUE_ELEM(pre, new) do { \
153 (pre).next->prev = (new); \
154 (new)->next = (pre).next; \
155 (new)->prev = &(pre); \
156 (pre).next = (new); \
159 #define DEQUEUE_ELEM(elem) do { \
160 (elem)->prev->next = (elem)->next; \
161 (elem)->next->prev = (elem)->prev; \
165 #define CLOSE(fd) do { close(fd); (fd) = -1; } while (0)
168 * Global olsrd configuragtion
170 extern struct olsrd_config *olsr_cnf;
173 extern clock_t now_times; /* current idea of times(2) reported uptime */
176 extern olsr_bool olsr_win32_end_request;
177 extern olsr_bool olsr_win32_end_flag;
181 * a wrapper around times(2). times(2) has the problem, that it may return -1
182 * in case of an err (e.g. EFAULT on the parameter) or immediately before an
183 * overrun (though it is not en error) just because the jiffies (or whatever
184 * the underlying kernel calls the smallest accountable time unit) are
185 * inherently "unsigned" (and always incremented).
187 unsigned long olsr_times(void);
191 *These are moved to a plugin soon
207 ipc_output(struct olsr *);