| 1 | #ifndef ITEM_INETFUNC_INCLUDED | 
|---|
| 2 | #define ITEM_INETFUNC_INCLUDED | 
|---|
| 3 |  | 
|---|
| 4 | /* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved. | 
|---|
| 5 | Copyright (c) 2014 MariaDB Foundation | 
|---|
| 6 |  | 
|---|
| 7 | This program is free software; you can redistribute it and/or modify | 
|---|
| 8 | it under the terms of the GNU General Public License as published by | 
|---|
| 9 | the Free Software Foundation; version 2 of the License. | 
|---|
| 10 |  | 
|---|
| 11 | This program is distributed in the hope that it will be useful, | 
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 14 | GNU General Public License for more details. | 
|---|
| 15 |  | 
|---|
| 16 | You should have received a copy of the GNU General Public License | 
|---|
| 17 | along with this program; if not, write to the Free Software | 
|---|
| 18 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */ | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 | #include "item.h" | 
|---|
| 22 |  | 
|---|
| 23 | /************************************************************************* | 
|---|
| 24 | Item_func_inet_aton implements INET_ATON() SQL-function. | 
|---|
| 25 | *************************************************************************/ | 
|---|
| 26 |  | 
|---|
| 27 | class Item_func_inet_aton : public Item_longlong_func | 
|---|
| 28 | { | 
|---|
| 29 | bool check_arguments() const | 
|---|
| 30 | { return check_argument_types_can_return_text(0, arg_count); } | 
|---|
| 31 | public: | 
|---|
| 32 | Item_func_inet_aton(THD *thd, Item *a): Item_longlong_func(thd, a) {} | 
|---|
| 33 | longlong val_int(); | 
|---|
| 34 | const char *func_name() const { return "inet_aton"; } | 
|---|
| 35 | void fix_length_and_dec() | 
|---|
| 36 | { | 
|---|
| 37 | decimals= 0; | 
|---|
| 38 | max_length= 21; | 
|---|
| 39 | maybe_null= 1; | 
|---|
| 40 | unsigned_flag= 1; | 
|---|
| 41 | } | 
|---|
| 42 | Item *get_copy(THD *thd) | 
|---|
| 43 | { return get_item_copy<Item_func_inet_aton>(thd, this); } | 
|---|
| 44 | }; | 
|---|
| 45 |  | 
|---|
| 46 |  | 
|---|
| 47 | /************************************************************************* | 
|---|
| 48 | Item_func_inet_ntoa implements INET_NTOA() SQL-function. | 
|---|
| 49 | *************************************************************************/ | 
|---|
| 50 |  | 
|---|
| 51 | class Item_func_inet_ntoa : public Item_str_func | 
|---|
| 52 | { | 
|---|
| 53 | public: | 
|---|
| 54 | Item_func_inet_ntoa(THD *thd, Item *a): Item_str_func(thd, a) | 
|---|
| 55 | { } | 
|---|
| 56 | String* val_str(String* str); | 
|---|
| 57 | const char *func_name() const { return "inet_ntoa"; } | 
|---|
| 58 | void fix_length_and_dec() | 
|---|
| 59 | { | 
|---|
| 60 | decimals= 0; | 
|---|
| 61 | fix_length_and_charset(3 * 8 + 7, default_charset()); | 
|---|
| 62 | maybe_null= 1; | 
|---|
| 63 | } | 
|---|
| 64 | Item *get_copy(THD *thd) | 
|---|
| 65 | { return get_item_copy<Item_func_inet_ntoa>(thd, this); } | 
|---|
| 66 | }; | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 | /************************************************************************* | 
|---|
| 70 | Item_func_inet_bool_base implements common code for INET6/IP-related | 
|---|
| 71 | functions returning boolean value. | 
|---|
| 72 | *************************************************************************/ | 
|---|
| 73 |  | 
|---|
| 74 | class Item_func_inet_bool_base : public Item_bool_func | 
|---|
| 75 | { | 
|---|
| 76 | public: | 
|---|
| 77 | inline Item_func_inet_bool_base(THD *thd, Item *ip_addr): | 
|---|
| 78 | Item_bool_func(thd, ip_addr) | 
|---|
| 79 | { | 
|---|
| 80 | null_value= false; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | public: | 
|---|
| 84 | virtual longlong val_int(); | 
|---|
| 85 | bool need_parentheses_in_default() { return false; } | 
|---|
| 86 |  | 
|---|
| 87 | protected: | 
|---|
| 88 | virtual bool calc_value(const String *arg) = 0; | 
|---|
| 89 | }; | 
|---|
| 90 |  | 
|---|
| 91 |  | 
|---|
| 92 | /************************************************************************* | 
|---|
| 93 | Item_func_inet_str_base implements common code for INET6/IP-related | 
|---|
| 94 | functions returning string value. | 
|---|
| 95 | *************************************************************************/ | 
|---|
| 96 |  | 
|---|
| 97 | class Item_func_inet_str_base : public Item_str_ascii_func | 
|---|
| 98 | { | 
|---|
| 99 | public: | 
|---|
| 100 | inline Item_func_inet_str_base(THD *thd, Item *arg): | 
|---|
| 101 | Item_str_ascii_func(thd, arg) | 
|---|
| 102 | { } | 
|---|
| 103 |  | 
|---|
| 104 | public: | 
|---|
| 105 | virtual String *val_str_ascii(String *buffer); | 
|---|
| 106 |  | 
|---|
| 107 | protected: | 
|---|
| 108 | virtual bool calc_value(const String *arg, String *buffer) = 0; | 
|---|
| 109 | }; | 
|---|
| 110 |  | 
|---|
| 111 |  | 
|---|
| 112 | /************************************************************************* | 
|---|
| 113 | Item_func_inet6_aton implements INET6_ATON() SQL-function. | 
|---|
| 114 | *************************************************************************/ | 
|---|
| 115 |  | 
|---|
| 116 | class Item_func_inet6_aton : public Item_func_inet_str_base | 
|---|
| 117 | { | 
|---|
| 118 | public: | 
|---|
| 119 | inline Item_func_inet6_aton(THD *thd, Item *ip_addr): | 
|---|
| 120 | Item_func_inet_str_base(thd, ip_addr) | 
|---|
| 121 | { } | 
|---|
| 122 |  | 
|---|
| 123 | public: | 
|---|
| 124 | virtual const char *func_name() const | 
|---|
| 125 | { return "inet6_aton"; } | 
|---|
| 126 |  | 
|---|
| 127 | virtual void fix_length_and_dec() | 
|---|
| 128 | { | 
|---|
| 129 | decimals= 0; | 
|---|
| 130 | fix_length_and_charset(16, &my_charset_bin); | 
|---|
| 131 | maybe_null= 1; | 
|---|
| 132 | } | 
|---|
| 133 | Item *get_copy(THD *thd) | 
|---|
| 134 | { return get_item_copy<Item_func_inet6_aton>(thd, this); } | 
|---|
| 135 |  | 
|---|
| 136 | protected: | 
|---|
| 137 | virtual bool calc_value(const String *arg, String *buffer); | 
|---|
| 138 | }; | 
|---|
| 139 |  | 
|---|
| 140 |  | 
|---|
| 141 | /************************************************************************* | 
|---|
| 142 | Item_func_inet6_ntoa implements INET6_NTOA() SQL-function. | 
|---|
| 143 | *************************************************************************/ | 
|---|
| 144 |  | 
|---|
| 145 | class Item_func_inet6_ntoa : public Item_func_inet_str_base | 
|---|
| 146 | { | 
|---|
| 147 | public: | 
|---|
| 148 | inline Item_func_inet6_ntoa(THD *thd, Item *ip_addr): | 
|---|
| 149 | Item_func_inet_str_base(thd, ip_addr) | 
|---|
| 150 | { } | 
|---|
| 151 |  | 
|---|
| 152 | public: | 
|---|
| 153 | virtual const char *func_name() const | 
|---|
| 154 | { return "inet6_ntoa"; } | 
|---|
| 155 |  | 
|---|
| 156 | virtual void fix_length_and_dec() | 
|---|
| 157 | { | 
|---|
| 158 | decimals= 0; | 
|---|
| 159 |  | 
|---|
| 160 | // max length: IPv6-address -- 16 bytes | 
|---|
| 161 | // 16 bytes / 2 bytes per group == 8 groups => 7 delimiter | 
|---|
| 162 | // 4 symbols per group | 
|---|
| 163 | fix_length_and_charset(8 * 4 + 7, default_charset()); | 
|---|
| 164 |  | 
|---|
| 165 | maybe_null= 1; | 
|---|
| 166 | } | 
|---|
| 167 | Item *get_copy(THD *thd) | 
|---|
| 168 | { return get_item_copy<Item_func_inet6_ntoa>(thd, this); } | 
|---|
| 169 |  | 
|---|
| 170 | protected: | 
|---|
| 171 | virtual bool calc_value(const String *arg, String *buffer); | 
|---|
| 172 | }; | 
|---|
| 173 |  | 
|---|
| 174 |  | 
|---|
| 175 | /************************************************************************* | 
|---|
| 176 | Item_func_is_ipv4 implements IS_IPV4() SQL-function. | 
|---|
| 177 | *************************************************************************/ | 
|---|
| 178 |  | 
|---|
| 179 | class Item_func_is_ipv4 : public Item_func_inet_bool_base | 
|---|
| 180 | { | 
|---|
| 181 | public: | 
|---|
| 182 | inline Item_func_is_ipv4(THD *thd, Item *ip_addr): | 
|---|
| 183 | Item_func_inet_bool_base(thd, ip_addr) | 
|---|
| 184 | { } | 
|---|
| 185 |  | 
|---|
| 186 | public: | 
|---|
| 187 | virtual const char *func_name() const | 
|---|
| 188 | { return "is_ipv4"; } | 
|---|
| 189 | Item *get_copy(THD *thd) | 
|---|
| 190 | { return get_item_copy<Item_func_is_ipv4>(thd, this); } | 
|---|
| 191 |  | 
|---|
| 192 | protected: | 
|---|
| 193 | virtual bool calc_value(const String *arg); | 
|---|
| 194 | }; | 
|---|
| 195 |  | 
|---|
| 196 |  | 
|---|
| 197 | /************************************************************************* | 
|---|
| 198 | Item_func_is_ipv6 implements IS_IPV6() SQL-function. | 
|---|
| 199 | *************************************************************************/ | 
|---|
| 200 |  | 
|---|
| 201 | class Item_func_is_ipv6 : public Item_func_inet_bool_base | 
|---|
| 202 | { | 
|---|
| 203 | public: | 
|---|
| 204 | inline Item_func_is_ipv6(THD *thd, Item *ip_addr): | 
|---|
| 205 | Item_func_inet_bool_base(thd, ip_addr) | 
|---|
| 206 | { } | 
|---|
| 207 |  | 
|---|
| 208 | public: | 
|---|
| 209 | virtual const char *func_name() const | 
|---|
| 210 | { return "is_ipv6"; } | 
|---|
| 211 | Item *get_copy(THD *thd) | 
|---|
| 212 | { return get_item_copy<Item_func_is_ipv6>(thd, this); } | 
|---|
| 213 |  | 
|---|
| 214 | protected: | 
|---|
| 215 | virtual bool calc_value(const String *arg); | 
|---|
| 216 | }; | 
|---|
| 217 |  | 
|---|
| 218 |  | 
|---|
| 219 | /************************************************************************* | 
|---|
| 220 | Item_func_is_ipv4_compat implements IS_IPV4_COMPAT() SQL-function. | 
|---|
| 221 | *************************************************************************/ | 
|---|
| 222 |  | 
|---|
| 223 | class Item_func_is_ipv4_compat : public Item_func_inet_bool_base | 
|---|
| 224 | { | 
|---|
| 225 | public: | 
|---|
| 226 | inline Item_func_is_ipv4_compat(THD *thd, Item *ip_addr): | 
|---|
| 227 | Item_func_inet_bool_base(thd, ip_addr) | 
|---|
| 228 | { } | 
|---|
| 229 |  | 
|---|
| 230 | public: | 
|---|
| 231 | virtual const char *func_name() const | 
|---|
| 232 | { return "is_ipv4_compat"; } | 
|---|
| 233 | Item *get_copy(THD *thd) | 
|---|
| 234 | { return get_item_copy<Item_func_is_ipv4_compat>(thd, this); } | 
|---|
| 235 |  | 
|---|
| 236 | protected: | 
|---|
| 237 | virtual bool calc_value(const String *arg); | 
|---|
| 238 | }; | 
|---|
| 239 |  | 
|---|
| 240 |  | 
|---|
| 241 | /************************************************************************* | 
|---|
| 242 | Item_func_is_ipv4_mapped implements IS_IPV4_MAPPED() SQL-function. | 
|---|
| 243 | *************************************************************************/ | 
|---|
| 244 |  | 
|---|
| 245 | class Item_func_is_ipv4_mapped : public Item_func_inet_bool_base | 
|---|
| 246 | { | 
|---|
| 247 | public: | 
|---|
| 248 | inline Item_func_is_ipv4_mapped(THD *thd, Item *ip_addr): | 
|---|
| 249 | Item_func_inet_bool_base(thd, ip_addr) | 
|---|
| 250 | { } | 
|---|
| 251 |  | 
|---|
| 252 | public: | 
|---|
| 253 | virtual const char *func_name() const | 
|---|
| 254 | { return "is_ipv4_mapped"; } | 
|---|
| 255 | Item *get_copy(THD *thd) | 
|---|
| 256 | { return get_item_copy<Item_func_is_ipv4_mapped>(thd, this); } | 
|---|
| 257 |  | 
|---|
| 258 | protected: | 
|---|
| 259 | virtual bool calc_value(const String *arg); | 
|---|
| 260 | }; | 
|---|
| 261 |  | 
|---|
| 262 | #endif // ITEM_INETFUNC_INCLUDED | 
|---|
| 263 |  | 
|---|