2
0

inet.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*-------------------------------------------------------------------------
  2. *
  3. * inet.h
  4. * Declarations for operations on INET datatypes.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/utils/inet.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef INET_H
  15. #define INET_H
  16. #include "fmgr.h"
  17. /*
  18. * This is the internal storage format for IP addresses
  19. * (both INET and CIDR datatypes):
  20. */
  21. typedef struct
  22. {
  23. unsigned char family; /* PGSQL_AF_INET or PGSQL_AF_INET6 */
  24. unsigned char bits; /* number of bits in netmask */
  25. unsigned char ipaddr[16]; /* up to 128 bits of address */
  26. } inet_struct;
  27. /*
  28. * We use these values for the "family" field.
  29. *
  30. * Referencing all of the non-AF_INET types to AF_INET lets us work on
  31. * machines which may not have the appropriate address family (like
  32. * inet6 addresses when AF_INET6 isn't present) but doesn't cause a
  33. * dump/reload requirement. Pre-7.4 databases used AF_INET for the family
  34. * type on disk.
  35. */
  36. #define PGSQL_AF_INET (AF_INET + 0)
  37. #define PGSQL_AF_INET6 (AF_INET + 1)
  38. /*
  39. * Both INET and CIDR addresses are represented within Postgres as varlena
  40. * objects, ie, there is a varlena header in front of the struct type
  41. * depicted above. This struct depicts what we actually have in memory
  42. * in "uncompressed" cases. Note that since the maximum data size is only
  43. * 18 bytes, INET/CIDR will invariably be stored into tuples using the
  44. * 1-byte-header varlena format. However, we have to be prepared to cope
  45. * with the 4-byte-header format too, because various code may helpfully
  46. * try to "decompress" 1-byte-header datums.
  47. */
  48. typedef struct
  49. {
  50. char vl_len_[4]; /* Do not touch this field directly! */
  51. inet_struct inet_data;
  52. } inet;
  53. /*
  54. * Access macros. We use VARDATA_ANY so that we can process short-header
  55. * varlena values without detoasting them. This requires a trick:
  56. * VARDATA_ANY assumes the varlena header is already filled in, which is
  57. * not the case when constructing a new value (until SET_INET_VARSIZE is
  58. * called, which we typically can't do till the end). Therefore, we
  59. * always initialize the newly-allocated value to zeroes (using palloc0).
  60. * A zero length word will look like the not-1-byte case to VARDATA_ANY,
  61. * and so we correctly construct an uncompressed value.
  62. *
  63. * Note that ip_addrsize(), ip_maxbits(), and SET_INET_VARSIZE() require
  64. * the family field to be set correctly.
  65. */
  66. #define ip_family(inetptr) \
  67. (((inet_struct *) VARDATA_ANY(inetptr))->family)
  68. #define ip_bits(inetptr) \
  69. (((inet_struct *) VARDATA_ANY(inetptr))->bits)
  70. #define ip_addr(inetptr) \
  71. (((inet_struct *) VARDATA_ANY(inetptr))->ipaddr)
  72. #define ip_addrsize(inetptr) \
  73. (ip_family(inetptr) == PGSQL_AF_INET ? 4 : 16)
  74. #define ip_maxbits(inetptr) \
  75. (ip_family(inetptr) == PGSQL_AF_INET ? 32 : 128)
  76. #define SET_INET_VARSIZE(dst) \
  77. SET_VARSIZE(dst, VARHDRSZ + offsetof(inet_struct, ipaddr) + \
  78. ip_addrsize(dst))
  79. /*
  80. * This is the internal storage format for MAC addresses:
  81. */
  82. typedef struct macaddr
  83. {
  84. unsigned char a;
  85. unsigned char b;
  86. unsigned char c;
  87. unsigned char d;
  88. unsigned char e;
  89. unsigned char f;
  90. } macaddr;
  91. /*
  92. * This is the internal storage format for MAC8 addresses:
  93. */
  94. typedef struct macaddr8
  95. {
  96. unsigned char a;
  97. unsigned char b;
  98. unsigned char c;
  99. unsigned char d;
  100. unsigned char e;
  101. unsigned char f;
  102. unsigned char g;
  103. unsigned char h;
  104. } macaddr8;
  105. /*
  106. * fmgr interface macros
  107. */
  108. #define DatumGetInetPP(X) ((inet *) PG_DETOAST_DATUM_PACKED(X))
  109. #define InetPGetDatum(X) PointerGetDatum(X)
  110. #define PG_GETARG_INET_PP(n) DatumGetInetPP(PG_GETARG_DATUM(n))
  111. #define PG_RETURN_INET_P(x) return InetPGetDatum(x)
  112. /* obsolescent variants */
  113. #define DatumGetInetP(X) ((inet *) PG_DETOAST_DATUM(X))
  114. #define PG_GETARG_INET_P(n) DatumGetInetP(PG_GETARG_DATUM(n))
  115. /* macaddr is a fixed-length pass-by-reference datatype */
  116. #define DatumGetMacaddrP(X) ((macaddr *) DatumGetPointer(X))
  117. #define MacaddrPGetDatum(X) PointerGetDatum(X)
  118. #define PG_GETARG_MACADDR_P(n) DatumGetMacaddrP(PG_GETARG_DATUM(n))
  119. #define PG_RETURN_MACADDR_P(x) return MacaddrPGetDatum(x)
  120. /* macaddr8 is a fixed-length pass-by-reference datatype */
  121. #define DatumGetMacaddr8P(X) ((macaddr8 *) DatumGetPointer(X))
  122. #define Macaddr8PGetDatum(X) PointerGetDatum(X)
  123. #define PG_GETARG_MACADDR8_P(n) DatumGetMacaddr8P(PG_GETARG_DATUM(n))
  124. #define PG_RETURN_MACADDR8_P(x) return Macaddr8PGetDatum(x)
  125. /*
  126. * Support functions in network.c
  127. */
  128. extern inet *cidr_set_masklen_internal(const inet *src, int bits);
  129. extern int bitncmp(const unsigned char *l, const unsigned char *r, int n);
  130. extern int bitncommon(const unsigned char *l, const unsigned char *r, int n);
  131. #endif /* INET_H */