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-1335  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 {
check_arguments()29   bool check_arguments() const
30   { return check_argument_types_can_return_text(0, arg_count); }
31 public:
Item_func_inet_aton(THD * thd,Item * a)32   Item_func_inet_aton(THD *thd, Item *a): Item_longlong_func(thd, a) {}
33   longlong val_int();
func_name()34   const char *func_name() const { return "inet_aton"; }
fix_length_and_dec()35   bool fix_length_and_dec()
36   {
37     decimals= 0;
38     max_length= 21;
39     maybe_null= 1;
40     unsigned_flag= 1;
41     return FALSE;
42   }
get_copy(THD * thd)43   Item *get_copy(THD *thd)
44   { return get_item_copy<Item_func_inet_aton>(thd, this); }
45 };
46 
47 
48 /*************************************************************************
49   Item_func_inet_ntoa implements INET_NTOA() SQL-function.
50 *************************************************************************/
51 
52 class Item_func_inet_ntoa : public Item_str_func
53 {
54 public:
Item_func_inet_ntoa(THD * thd,Item * a)55   Item_func_inet_ntoa(THD *thd, Item *a): Item_str_func(thd, a)
56   { }
57   String* val_str(String* str);
func_name()58   const char *func_name() const { return "inet_ntoa"; }
fix_length_and_dec()59   bool fix_length_and_dec()
60   {
61     decimals= 0;
62     fix_length_and_charset(3 * 8 + 7, default_charset());
63     maybe_null= 1;
64     return FALSE;
65   }
get_copy(THD * thd)66   Item *get_copy(THD *thd)
67   { return get_item_copy<Item_func_inet_ntoa>(thd, this); }
68 };
69 
70 
71 /*************************************************************************
72   Item_func_inet_bool_base implements common code for INET6/IP-related
73   functions returning boolean value.
74 *************************************************************************/
75 
76 class Item_func_inet_bool_base : public Item_bool_func
77 {
78 public:
Item_func_inet_bool_base(THD * thd,Item * ip_addr)79   inline Item_func_inet_bool_base(THD *thd, Item *ip_addr):
80     Item_bool_func(thd, ip_addr)
81   {
82     null_value= false;
83   }
need_parentheses_in_default()84   bool need_parentheses_in_default() { return false; }
85 };
86 
87 
88 /*************************************************************************
89   Item_func_inet6_aton implements INET6_ATON() SQL-function.
90 *************************************************************************/
91 
92 class Item_func_inet6_aton : public Item_str_func
93 {
94 public:
Item_func_inet6_aton(THD * thd,Item * ip_addr)95   inline Item_func_inet6_aton(THD *thd, Item *ip_addr):
96     Item_str_func(thd, ip_addr)
97   { }
98 
99 public:
func_name()100   virtual const char *func_name() const
101   { return "inet6_aton"; }
102 
fix_length_and_dec()103   virtual bool fix_length_and_dec()
104   {
105     decimals= 0;
106     fix_length_and_charset(16, &my_charset_bin);
107     maybe_null= 1;
108     return FALSE;
109   }
get_copy(THD * thd)110   Item *get_copy(THD *thd)
111   { return get_item_copy<Item_func_inet6_aton>(thd, this); }
112 
113   String *val_str(String *to);
114 };
115 
116 
117 /*************************************************************************
118   Item_func_inet6_ntoa implements INET6_NTOA() SQL-function.
119 *************************************************************************/
120 
121 class Item_func_inet6_ntoa : public Item_str_ascii_func
122 {
123 public:
Item_func_inet6_ntoa(THD * thd,Item * ip_addr)124   inline Item_func_inet6_ntoa(THD *thd, Item *ip_addr):
125     Item_str_ascii_func(thd, ip_addr)
126   { }
127 
128 public:
func_name()129   virtual const char *func_name() const
130   { return "inet6_ntoa"; }
131 
fix_length_and_dec()132   virtual bool fix_length_and_dec()
133   {
134     decimals= 0;
135 
136     // max length: IPv6-address -- 16 bytes
137     // 16 bytes / 2 bytes per group == 8 groups => 7 delimiter
138     // 4 symbols per group
139     fix_length_and_charset(8 * 4 + 7, default_charset());
140 
141     maybe_null= 1;
142     return FALSE;
143   }
144   String *val_str_ascii(String *to);
get_copy(THD * thd)145   Item *get_copy(THD *thd)
146   { return get_item_copy<Item_func_inet6_ntoa>(thd, this); }
147 };
148 
149 
150 /*************************************************************************
151   Item_func_is_ipv4 implements IS_IPV4() SQL-function.
152 *************************************************************************/
153 
154 class Item_func_is_ipv4 : public Item_func_inet_bool_base
155 {
156 public:
Item_func_is_ipv4(THD * thd,Item * ip_addr)157   inline Item_func_is_ipv4(THD *thd, Item *ip_addr):
158     Item_func_inet_bool_base(thd, ip_addr)
159   { }
160 
161 public:
func_name()162   virtual const char *func_name() const
163   { return "is_ipv4"; }
get_copy(THD * thd)164   Item *get_copy(THD *thd)
165   { return get_item_copy<Item_func_is_ipv4>(thd, this); }
166 
167   longlong val_int();
168 };
169 
170 
171 /*************************************************************************
172   Item_func_is_ipv6 implements IS_IPV6() SQL-function.
173 *************************************************************************/
174 
175 class Item_func_is_ipv6 : public Item_func_inet_bool_base
176 {
177 public:
Item_func_is_ipv6(THD * thd,Item * ip_addr)178   inline Item_func_is_ipv6(THD *thd, Item *ip_addr):
179     Item_func_inet_bool_base(thd, ip_addr)
180   { }
181 
func_name()182   virtual const char *func_name() const
183   { return "is_ipv6"; }
get_copy(THD * thd)184   Item *get_copy(THD *thd)
185   { return get_item_copy<Item_func_is_ipv6>(thd, this); }
186 
187   longlong val_int();
188 };
189 
190 
191 /*************************************************************************
192   Item_func_is_ipv4_compat implements IS_IPV4_COMPAT() SQL-function.
193 *************************************************************************/
194 
195 class Item_func_is_ipv4_compat : public Item_func_inet_bool_base
196 {
197 public:
Item_func_is_ipv4_compat(THD * thd,Item * ip_addr)198   inline Item_func_is_ipv4_compat(THD *thd, Item *ip_addr):
199     Item_func_inet_bool_base(thd, ip_addr)
200   { }
func_name()201   virtual const char *func_name() const
202   { return "is_ipv4_compat"; }
get_copy(THD * thd)203   Item *get_copy(THD *thd)
204   { return get_item_copy<Item_func_is_ipv4_compat>(thd, this); }
205   longlong val_int();
206 };
207 
208 
209 /*************************************************************************
210   Item_func_is_ipv4_mapped implements IS_IPV4_MAPPED() SQL-function.
211 *************************************************************************/
212 
213 class Item_func_is_ipv4_mapped : public Item_func_inet_bool_base
214 {
215 public:
Item_func_is_ipv4_mapped(THD * thd,Item * ip_addr)216   inline Item_func_is_ipv4_mapped(THD *thd, Item *ip_addr):
217     Item_func_inet_bool_base(thd, ip_addr)
218   { }
func_name()219   virtual const char *func_name() const
220   { return "is_ipv4_mapped"; }
get_copy(THD * thd)221   Item *get_copy(THD *thd)
222   { return get_item_copy<Item_func_is_ipv4_mapped>(thd, this); }
223   longlong val_int();
224 };
225 
226 #endif // ITEM_INETFUNC_INCLUDED
227