network.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2007, Digium, Inc.
  5. *
  6. * Luigi Rizzo
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Wrapper for network related headers,
  20. * masking differences between various operating systems.
  21. * On passing, we also provide here trivial functions or
  22. * other simple wrappers to network-related functions.
  23. */
  24. #ifndef _ASTERISK_NETWORK_H
  25. #define _ASTERISK_NETWORK_H
  26. #if defined(__cplusplus) || defined(c_plusplus)
  27. extern "C" {
  28. #endif
  29. /*
  30. * Include relevant network headers.
  31. * Our preferred choice are the standard BSD/linux/unix headers.
  32. * Missing them (e.g. for solaris or various windows environments),
  33. * we resort to whatever we find around, and provide local definitions
  34. * for the missing bits.
  35. */
  36. #ifdef HAVE_ARPA_INET_H
  37. #include <netinet/in.h>
  38. #include <arpa/inet.h> /* include early to override inet_ntoa */
  39. #include <netinet/in_systm.h>
  40. #include <netinet/ip.h>
  41. #include <netinet/tcp.h>
  42. #include <netdb.h>
  43. #include <sys/socket.h>
  44. #include <net/if.h>
  45. #include <sys/ioctl.h>
  46. #elif defined(HAVE_WINSOCK_H)
  47. #include <winsock.h>
  48. typedef int socklen_t;
  49. #elif defined(HAVE_WINSOCK2_H)
  50. #include <winsock2.h>
  51. #include <ws2tcpip.h>
  52. #else
  53. #error "don't know how to handle network functions here."
  54. #endif
  55. #ifndef HAVE_INET_ATON
  56. int inet_aton(const char *cp, struct in_addr *pin);
  57. #endif
  58. #ifndef IFNAMSIZ
  59. #define IFNAMSIZ 16
  60. #endif
  61. #ifndef MAXHOSTNAMELEN
  62. #define MAXHOSTNAMELEN 256
  63. #endif
  64. /*!
  65. * \brief thread-safe replacement for inet_ntoa().
  66. *
  67. * \note It is very important to note that even though this is a thread-safe
  68. * replacement for inet_ntoa(), it is *not* reentrant. In a single
  69. * thread, the result from a previous call to this function is no longer
  70. * valid once it is called again. If the result from multiple calls to
  71. * this function need to be kept or used at once, then the result must be
  72. * copied to a local buffer before calling this function again.
  73. */
  74. const char *ast_inet_ntoa(struct in_addr ia);
  75. #ifdef inet_ntoa
  76. #undef inet_ntoa
  77. #endif
  78. #define inet_ntoa __dont__use__inet_ntoa__use__ast_inet_ntoa__instead__
  79. #ifdef getprotobyname
  80. #undef getprotobyname
  81. #endif
  82. #define getprotobyname __getprotobyname_is_not_threadsafe__do_not_use__
  83. /*! \brief Compares the source address and port of two sockaddr_in */
  84. static force_inline int inaddrcmp(const struct sockaddr_in *sin1, const struct sockaddr_in *sin2)
  85. {
  86. return ((sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
  87. || (sin1->sin_port != sin2->sin_port));
  88. }
  89. #if defined(__cplusplus) || defined(c_plusplus)
  90. }
  91. #endif
  92. #endif /* _ASTERISK_NETWORK_H */