common.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2002 Damien Miller. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  14. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  15. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  18. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  20. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #ifndef _SFD_COMMON_H
  25. #define _SFD_COMMON_H
  26. #include "config.h"
  27. #define _BSD_SOURCE /* Needed for BSD-style struct ip,tcp,udp on Linux */
  28. #define _DEFAULT_SOURCE /* It is recommended to use instead of _BSD_SOURCE on Linux */
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/poll.h>
  33. #include <sys/socket.h>
  34. #include <sys/time.h>
  35. #include <sys/un.h>
  36. #include <netinet/in.h>
  37. #include <netinet/in_systm.h>
  38. #include <netinet/ip.h>
  39. #include <netinet/ip6.h>
  40. #include <netinet/ip_icmp.h>
  41. #include <netinet/tcp.h>
  42. #include <netinet/udp.h>
  43. #include <arpa/inet.h>
  44. #include <net/ethernet.h>
  45. #include <unistd.h>
  46. #include <stdlib.h>
  47. #include <errno.h>
  48. #include <fcntl.h>
  49. #include <grp.h>
  50. #include <netdb.h>
  51. #include <limits.h>
  52. #include <pwd.h>
  53. #include <signal.h>
  54. #include <stdio.h>
  55. #include <string.h>
  56. #include <strings.h>
  57. #include <syslog.h>
  58. #include <time.h>
  59. #include <netdb.h>
  60. #if defined(HAVE_NET_BPF_H)
  61. #include <net/bpf.h>
  62. #elif defined(HAVE_PCAP_BPF_H)
  63. #include <pcap-bpf.h>
  64. #endif
  65. #if defined(HAVE_INTTYPES_H)
  66. #include <inttypes.h>
  67. #endif
  68. #if defined(HAVE_SYS_ENDIAN_H)
  69. #include <sys/endian.h>
  70. #elif defined(HAVE_ENDIAN_H)
  71. #include <endian.h>
  72. #endif
  73. /* The name of the program */
  74. #define PROGNAME "softflowd"
  75. /* The name of the program */
  76. #define PROGVER "1.0.0"
  77. /* Default pidfile */
  78. #define DEFAULT_PIDFILE "/var/run/" PROGNAME ".pid"
  79. /* Default control socket */
  80. #define DEFAULT_CTLSOCK "/var/run/" PROGNAME ".ctl"
  81. #define RCSID(msg) \
  82. static /**/const char *const flowd_rcsid[] = \
  83. { (const char *)flowd_rcsid, "\100(#)" msg } \
  84. #ifndef IP_OFFMASK
  85. #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
  86. #endif
  87. #ifndef IPV6_VERSION
  88. #define IPV6_VERSION 0x60
  89. #endif
  90. #ifndef IPV6_VERSION_MASK
  91. #define IPV6_VERSION_MASK 0xf0
  92. #endif
  93. #ifndef IPV6_FLOWINFO_MASK
  94. #define IPV6_FLOWINFO_MASK ntohl(0x0fffffff)
  95. #endif
  96. #ifndef IPV6_FLOWLABEL_MASK
  97. #define IPV6_FLOWLABEL_MASK ntohl(0x000fffff)
  98. #endif
  99. #ifndef _PATH_DEVNULL
  100. #define _PATH_DEVNULL "/dev/null"
  101. #endif
  102. #ifndef MIN
  103. #define MIN(a,b) (((a)<(b))?(a):(b))
  104. #endif
  105. #ifndef MAX
  106. #define MAX(a,b) (((a)>(b))?(a):(b))
  107. #endif
  108. #ifndef offsetof
  109. #define offsetof(type, member) ((size_t) &((type *)0)->member)
  110. #endif
  111. #if defined(__GNUC__)
  112. #ifndef __dead
  113. #define __dead __attribute__((__noreturn__))
  114. #endif
  115. #ifndef __packed
  116. #define __packed __attribute__((__packed__))
  117. #endif
  118. #endif
  119. #if !defined(HAVE_INT8_T) && defined(OUR_CFG_INT8_T)
  120. typedef OUR_CFG_INT8_T int8_t;
  121. #endif
  122. #if !defined(HAVE_INT16_T) && defined(OUR_CFG_INT16_T)
  123. typedef OUR_CFG_INT16_T int16_t;
  124. #endif
  125. #if !defined(HAVE_INT32_T) && defined(OUR_CFG_INT32_T)
  126. typedef OUR_CFG_INT32_T int32_t;
  127. #endif
  128. #if !defined(HAVE_INT64_T) && defined(OUR_CFG_INT64_T)
  129. typedef OUR_CFG_INT64_T int64_t;
  130. #endif
  131. #if !defined(HAVE_U_INT8_T) && defined(OUR_CFG_U_INT8_T)
  132. typedef OUR_CFG_U_INT8_T u_int8_t;
  133. #endif
  134. #if !defined(HAVE_U_INT16_T) && defined(OUR_CFG_U_INT16_T)
  135. typedef OUR_CFG_U_INT16_T u_int16_t;
  136. #endif
  137. #if !defined(HAVE_U_INT32_T) && defined(OUR_CFG_U_INT32_T)
  138. typedef OUR_CFG_U_INT32_T u_int32_t;
  139. #endif
  140. #if !defined(HAVE_U_INT64_T) && defined(OUR_CFG_U_INT64_T)
  141. typedef OUR_CFG_U_INT64_T u_int64_t;
  142. #endif
  143. #ifndef HAVE_STRLCPY
  144. size_t strlcpy (char *dst, const char *src, size_t siz);
  145. #endif
  146. #ifndef HAVE_STRLCAT
  147. size_t strlcat (char *dst, const char *src, size_t siz);
  148. #endif
  149. #ifndef HAVE_CLOSEFROM
  150. void closefrom (int lowfd);
  151. #endif
  152. #ifndef HAVE_STRUCT_IP6_EXT
  153. struct ip6_ext {
  154. u_int8_t ip6e_nxt;
  155. u_int8_t ip6e_len;
  156. } __packed;
  157. #endif
  158. /* following lines are copy from unistd.h in Linux for avoidance warnings in compilation */
  159. #if defined(HAVE_SETRESGID) && !defined(_GNU_SOURCE)
  160. extern int setresgid (__uid_t __ruid, __uid_t __euid, __uid_t __suid);
  161. #endif
  162. #if defined(HAVE_SETRESUID) && !defined(_GNU_SOURCE)
  163. extern int setresuid (__uid_t __ruid, __uid_t __euid, __uid_t __suid);
  164. #endif
  165. #if defined (HAVE_DECL_HTONLL) && !defined (HAVE_DECL_HTOBE64)
  166. #define htobe64 htonll
  167. #endif
  168. #ifndef ETH_ALEN
  169. // https://cdn.kernel.org/pub/linux/kernel/people/marcelo/linux-2.4/include/linux/if_ether.h
  170. #define ETH_ALEN 6 /* Octets in one ethernet addr */
  171. #endif /* ETH_ALEN */
  172. #ifdef __APPLE__
  173. #include <libkern/OSByteOrder.h>
  174. #define htobe64(x) OSSwapHostToBigInt64(x)
  175. #endif /* __APPLE__ */
  176. #endif /* _SFD_COMMON_H */