sockunion.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Socket union header.
  3. * Copyright (c) 1997 Kunihiro Ishiguro
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #ifndef _ZEBRA_SOCKUNION_H
  23. #define _ZEBRA_SOCKUNION_H
  24. #include "if.h"
  25. #if 0
  26. union sockunion {
  27. struct sockinet {
  28. u_char si_len;
  29. u_char si_family;
  30. u_short si_port;
  31. } su_si;
  32. struct sockaddr_in su_sin;
  33. struct sockaddr_in6 su_sin6;
  34. };
  35. #define su_len su_si.si_len
  36. #define su_family su_si.si_family
  37. #define su_port su_si.si_port
  38. #endif /* 0 */
  39. union sockunion
  40. {
  41. struct sockaddr sa;
  42. struct sockaddr_in sin;
  43. #ifdef HAVE_IPV6
  44. struct sockaddr_in6 sin6;
  45. #endif /* HAVE_IPV6 */
  46. };
  47. enum connect_result
  48. {
  49. connect_error,
  50. connect_success,
  51. connect_in_progress
  52. };
  53. /* Default address family. */
  54. #ifdef HAVE_IPV6
  55. #define AF_INET_UNION AF_INET6
  56. #else
  57. #define AF_INET_UNION AF_INET
  58. #endif
  59. /* Sockunion address string length. Same as INET6_ADDRSTRLEN. */
  60. #define SU_ADDRSTRLEN 46
  61. /* Macro to set link local index to the IPv6 address. For KAME IPv6
  62. stack. */
  63. #ifdef KAME
  64. #define IN6_LINKLOCAL_IFINDEX(a) ((a).s6_addr[2] << 8 | (a).s6_addr[3])
  65. #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
  66. do { \
  67. (a).s6_addr[2] = ((i) >> 8) & 0xff; \
  68. (a).s6_addr[3] = (i) & 0xff; \
  69. } while (0)
  70. #else
  71. #define IN6_LINKLOCAL_IFINDEX(a)
  72. #define SET_IN6_LINKLOCAL_IFINDEX(a, i)
  73. #endif /* KAME */
  74. #define sockunion_family(X) (X)->sa.sa_family
  75. #define sockunion2ip(X) (X)->sin.sin_addr.s_addr
  76. /* Prototypes. */
  77. extern int str2sockunion (const char *, union sockunion *);
  78. extern const char *sockunion2str (const union sockunion *, char *, size_t);
  79. extern int sockunion_cmp (const union sockunion *, const union sockunion *);
  80. extern int sockunion_same (const union sockunion *, const union sockunion *);
  81. extern unsigned int sockunion_hash (const union sockunion *);
  82. extern size_t family2addrsize(int family);
  83. extern size_t sockunion_get_addrlen(const union sockunion *);
  84. extern const u_char *sockunion_get_addr(const union sockunion *);
  85. extern unsigned short sockunion_get_port (const union sockunion *);
  86. extern void sockunion_set(union sockunion *, int family, const u_char *addr, size_t bytes);
  87. extern union sockunion *sockunion_str2su (const char *str);
  88. extern int sockunion_accept (int sock, union sockunion *);
  89. extern int sockunion_stream_socket (union sockunion *);
  90. extern int sockopt_reuseaddr (int);
  91. extern int sockopt_reuseport (int);
  92. extern int sockopt_v6only (int family, int sock);
  93. extern int sockunion_bind (int sock, union sockunion *,
  94. unsigned short, union sockunion *);
  95. extern int sockopt_ttl (int family, int sock, int ttl);
  96. extern int sockopt_minttl (int family, int sock, int minttl);
  97. extern int sockopt_cork (int sock, int onoff);
  98. extern int sockunion_socket (const union sockunion *su);
  99. extern const char *inet_sutop (const union sockunion *su, char *str);
  100. extern enum connect_result sockunion_connect (int fd, const union sockunion *su,
  101. unsigned short port,
  102. ifindex_t);
  103. extern union sockunion *sockunion_getsockname (int);
  104. extern union sockunion *sockunion_getpeername (int);
  105. extern union sockunion *sockunion_dup (const union sockunion *);
  106. extern void sockunion_free (union sockunion *);
  107. #ifndef HAVE_INET_NTOP
  108. extern const char * inet_ntop (int family, const void *addrptr,
  109. char *strptr, size_t len);
  110. #endif /* HAVE_INET_NTOP */
  111. #ifndef HAVE_INET_PTON
  112. extern int inet_pton (int family, const char *strptr, void *addrptr);
  113. #endif /* HAVE_INET_PTON */
  114. #ifndef HAVE_INET_ATON
  115. extern int inet_aton (const char *cp, struct in_addr *inaddr);
  116. #endif
  117. #endif /* _ZEBRA_SOCKUNION_H */