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