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