inet.h 4.6 KB

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