MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
item_inetfunc.cc
1 /* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #include "item_inetfunc.h"
17 
18 #include "my_net.h"
19 
21 
22 static const int IN_ADDR_SIZE= sizeof (in_addr);
23 static const int IN6_ADDR_SIZE= sizeof (in6_addr);
24 static const int IN6_ADDR_NUM_WORDS= IN6_ADDR_SIZE / 2;
25 
26 static const char HEX_DIGITS[]= "0123456789abcdef";
27 
29 
30 longlong Item_func_inet_aton::val_int()
31 {
32  DBUG_ASSERT(fixed);
33 
34  uint byte_result= 0;
35  ulonglong result= 0;
36  const char *p,* end;
37  char c= '.'; // we mark c to indicate invalid IP in case length is 0
38  char buff[36];
39  int dot_count= 0;
40 
41  String tmp(buff, sizeof (buff), &my_charset_latin1);
42  String *s= args[0]->val_str_ascii(&tmp);
43 
44  if (!s) // If null value
45  goto err;
46 
47  null_value= 0;
48 
49  p= s->ptr();
50  end= p + s->length();
51  while (p < end)
52  {
53  c= *p++;
54  int digit= (int) (c - '0');
55  if (digit >= 0 && digit <= 9)
56  {
57  byte_result= byte_result * 10 + digit;
58  if (byte_result > 255)
59  goto err; // Wrong address
60  }
61  else if (c == '.')
62  {
63  dot_count++;
64  result= (result << 8) + (ulonglong) byte_result;
65  byte_result= 0;
66  }
67  else
68  goto err; // Invalid character
69  }
70  if (c != '.') // IP number can't end on '.'
71  {
72  /*
73  Attempt to support short forms of IP-addresses. It's however pretty
74  basic one comparing to the BSD support.
75  Examples:
76  127 -> 0.0.0.127
77  127.255 -> 127.0.0.255
78  127.256 -> NULL (should have been 127.0.1.0)
79  127.2.1 -> 127.2.0.1
80  */
81  switch (dot_count) {
82  case 1: result<<= 8; /* Fall through */
83  case 2: result<<= 8; /* Fall through */
84  }
85  return (result << 8) + (ulonglong) byte_result;
86  }
87 
88 err:
89  null_value=1;
90  return 0;
91 }
92 
94 
95 String* Item_func_inet_ntoa::val_str(String* str)
96 {
97  DBUG_ASSERT(fixed);
98 
99  ulonglong n= (ulonglong) args[0]->val_int();
100 
101  /*
102  We do not know if args[0] is NULL until we have called
103  some val function on it if args[0] is not a constant!
104 
105  Also return null if n > 255.255.255.255
106  */
107  null_value= args[0]->null_value || n > (ulonglong) LL(4294967295);
108 
109  if (null_value)
110  return 0; // Null value
111 
112  str->set_charset(collation.collation);
113  str->length(0);
114 
115  uchar buf[8];
116  int4store(buf, n);
117 
118  /* Now we can assume little endian. */
119 
120  char num[4];
121  num[3]= '.';
122 
123  for (uchar *p= buf + 4; p-- > buf;)
124  {
125  uint c= *p;
126  uint n1, n2; // Try to avoid divisions
127  n1= c / 100; // 100 digits
128  c -= n1 * 100;
129  n2= c / 10; // 10 digits
130  c -= n2 * 10; // last digit
131  num[0]= (char) n1 + '0';
132  num[1]= (char) n2 + '0';
133  num[2]= (char) c + '0';
134  uint length= (n1 ? 4 : n2 ? 3 : 2); // Remove pre-zero
135  uint dot_length= (p <= buf) ? 1 : 0;
136 
137  str->append(num + 4 - length, length - dot_length, &my_charset_latin1);
138  }
139 
140  return str;
141 }
142 
144 
152 {
153  DBUG_ASSERT(fixed);
154 
155  if (args[0]->result_type() != STRING_RESULT) // String argument expected
156  return 0;
157 
158  String buffer;
159  String *arg_str= args[0]->val_str(&buffer);
160 
161  if (!arg_str) // Out-of memory happened. The error has been reported.
162  return 0; // Or: the underlying field is NULL
163 
164  return calc_value(arg_str) ? 1 : 0;
165 }
166 
168 
178 {
179  DBUG_ASSERT(fixed);
180 
181  if (args[0]->result_type() != STRING_RESULT) // String argument expected
182  {
183  null_value= true;
184  return NULL;
185  }
186 
187  String *arg_str= args[0]->val_str(buffer);
188  if (!arg_str) // Out-of memory happened. The error has been reported.
189  { // Or: the underlying field is NULL
190  null_value= true;
191  return NULL;
192  }
193 
194  null_value= !calc_value(arg_str, buffer);
195 
196  return null_value ? NULL : buffer;
197 }
198 
200 
217 static bool str_to_ipv4(const char *str, int str_length, in_addr *ipv4_address)
218 {
219  if (str_length < 7)
220  {
221  DBUG_PRINT("error", ("str_to_ipv4(%.*s): "
222  "invalid IPv4 address: too short.",
223  str_length, str));
224  return false;
225  }
226 
227  if (str_length > 15)
228  {
229  DBUG_PRINT("error", ("str_to_ipv4(%.*s): "
230  "invalid IPv4 address: too long.",
231  str_length, str));
232  return false;
233  }
234 
235  unsigned char *ipv4_bytes= (unsigned char *) ipv4_address;
236  const char *p= str;
237  int byte_value= 0;
238  int chars_in_group= 0;
239  int dot_count= 0;
240  char c= 0;
241 
242  while (((p - str) < str_length) && *p)
243  {
244  c= *p++;
245 
246  if (my_isdigit(&my_charset_latin1, c))
247  {
248  ++chars_in_group;
249 
250  if (chars_in_group > 3)
251  {
252  DBUG_PRINT("error", ("str_to_ipv4(%.*s): invalid IPv4 address: "
253  "too many characters in a group.",
254  str_length, str));
255  return false;
256  }
257 
258  byte_value= byte_value * 10 + (c - '0');
259 
260  if (byte_value > 255)
261  {
262  DBUG_PRINT("error", ("str_to_ipv4(%.*s): invalid IPv4 address: "
263  "invalid byte value.",
264  str_length, str));
265  return false;
266  }
267  }
268  else if (c == '.')
269  {
270  if (chars_in_group == 0)
271  {
272  DBUG_PRINT("error", ("str_to_ipv4(%.*s): invalid IPv4 address: "
273  "too few characters in a group.",
274  str_length, str));
275  return false;
276  }
277 
278  ipv4_bytes[dot_count]= (unsigned char) byte_value;
279 
280  ++dot_count;
281  byte_value= 0;
282  chars_in_group= 0;
283 
284  if (dot_count > 3)
285  {
286  DBUG_PRINT("error", ("str_to_ipv4(%.*s): invalid IPv4 address: "
287  "too many dots.", str_length, str));
288  return false;
289  }
290  }
291  else
292  {
293  DBUG_PRINT("error", ("str_to_ipv4(%.*s): invalid IPv4 address: "
294  "invalid character at pos %d.",
295  str_length, str, (int) (p - str)));
296  return false;
297  }
298  }
299 
300  if (c == '.')
301  {
302  DBUG_PRINT("error", ("str_to_ipv4(%.*s): invalid IPv4 address: "
303  "ending at '.'.", str_length, str));
304  return false;
305  }
306 
307  if (dot_count != 3)
308  {
309  DBUG_PRINT("error", ("str_to_ipv4(%.*s): invalid IPv4 address: "
310  "too few groups.",
311  str_length, str));
312  return false;
313  }
314 
315  ipv4_bytes[3]= (unsigned char) byte_value;
316 
317  DBUG_PRINT("info", ("str_to_ipv4(%.*s): valid IPv4 address: %d.%d.%d.%d",
318  str_length, str,
319  ipv4_bytes[0], ipv4_bytes[1],
320  ipv4_bytes[2], ipv4_bytes[3]));
321  return true;
322 }
323 
325 
342 static bool str_to_ipv6(const char *str, int str_length, in6_addr *ipv6_address)
343 {
344  if (str_length < 2)
345  {
346  DBUG_PRINT("error", ("str_to_ipv6(%.*s): invalid IPv6 address: too short.",
347  str_length, str));
348  return false;
349  }
350 
351  if (str_length > 8 * 4 + 7)
352  {
353  DBUG_PRINT("error", ("str_to_ipv6(%.*s): invalid IPv6 address: too long.",
354  str_length, str));
355  return false;
356  }
357 
358  memset(ipv6_address, 0, IN6_ADDR_SIZE);
359 
360  const char *p= str;
361 
362  if (*p == ':')
363  {
364  ++p;
365 
366  if (*p != ':')
367  {
368  DBUG_PRINT("error", ("str_to_ipv6(%.*s): invalid IPv6 address: "
369  "can not start with ':x'.", str_length, str));
370  return false;
371  }
372  }
373 
374  char *ipv6_bytes= (char *) ipv6_address;
375  char *ipv6_bytes_end= ipv6_bytes + IN6_ADDR_SIZE;
376  char *dst= ipv6_bytes;
377  char *gap_ptr= NULL;
378  const char *group_start_ptr= p;
379  int chars_in_group= 0;
380  int group_value= 0;
381 
382  while (((p - str) < str_length) && *p)
383  {
384  char c= *p++;
385 
386  if (c == ':')
387  {
388  group_start_ptr= p;
389 
390  if (!chars_in_group)
391  {
392  if (gap_ptr)
393  {
394  DBUG_PRINT("error", ("str_to_ipv6(%.*s): invalid IPv6 address: "
395  "too many gaps(::).", str_length, str));
396  return false;
397  }
398 
399  gap_ptr= dst;
400  continue;
401  }
402 
403  if (!*p || ((p - str) >= str_length))
404  {
405  DBUG_PRINT("error", ("str_to_ipv6(%.*s): invalid IPv6 address: "
406  "ending at ':'.", str_length, str));
407  return false;
408  }
409 
410  if (dst + 2 > ipv6_bytes_end)
411  {
412  DBUG_PRINT("error", ("str_to_ipv6(%.*s): invalid IPv6 address: "
413  "too many groups (1).", str_length, str));
414  return false;
415  }
416 
417  dst[0]= (unsigned char) (group_value >> 8) & 0xff;
418  dst[1]= (unsigned char) group_value & 0xff;
419  dst += 2;
420 
421  chars_in_group= 0;
422  group_value= 0;
423  }
424  else if (c == '.')
425  {
426  if (dst + IN_ADDR_SIZE > ipv6_bytes_end)
427  {
428  DBUG_PRINT("error", ("str_to_ipv6(%.*s): invalid IPv6 address: "
429  "unexpected IPv4-part.", str_length, str));
430  return false;
431  }
432 
433  if (!str_to_ipv4(group_start_ptr,
434  str + str_length - group_start_ptr,
435  (in_addr *) dst))
436  {
437  DBUG_PRINT("error", ("str_to_ipv6(%.*s): invalid IPv6 address: "
438  "invalid IPv4-part.", str_length, str));
439  return false;
440  }
441 
442  dst += IN_ADDR_SIZE;
443  chars_in_group= 0;
444 
445  break;
446  }
447  else
448  {
449  const char *hdp= strchr(HEX_DIGITS, my_tolower(&my_charset_latin1, c));
450 
451  if (!hdp)
452  {
453  DBUG_PRINT("error", ("str_to_ipv6(%.*s): invalid IPv6 address: "
454  "invalid character at pos %d.",
455  str_length, str, (int) (p - str)));
456  return false;
457  }
458 
459  if (chars_in_group >= 4)
460  {
461  DBUG_PRINT("error", ("str_to_ipv6(%.*s): invalid IPv6 address: "
462  "too many digits in group.",
463  str_length, str));
464  return false;
465  }
466 
467  group_value <<= 4;
468  group_value |= hdp - HEX_DIGITS;
469 
470  DBUG_ASSERT(group_value <= 0xffff);
471 
472  ++chars_in_group;
473  }
474  }
475 
476  if (chars_in_group > 0)
477  {
478  if (dst + 2 > ipv6_bytes_end)
479  {
480  DBUG_PRINT("error", ("str_to_ipv6(%.*s): invalid IPv6 address: "
481  "too many groups (2).", str_length, str));
482  return false;
483  }
484 
485  dst[0]= (unsigned char) (group_value >> 8) & 0xff;
486  dst[1]= (unsigned char) group_value & 0xff;
487  dst += 2;
488  }
489 
490  if (gap_ptr)
491  {
492  if (dst == ipv6_bytes_end)
493  {
494  DBUG_PRINT("error", ("str_to_ipv6(%.*s): invalid IPv6 address: "
495  "no room for a gap (::).", str_length, str));
496  return false;
497  }
498 
499  int bytes_to_move= dst - gap_ptr;
500 
501  for (int i= 1; i <= bytes_to_move; ++i)
502  {
503  ipv6_bytes_end[-i]= gap_ptr[bytes_to_move - i];
504  gap_ptr[bytes_to_move - i]= 0;
505  }
506 
507  dst= ipv6_bytes_end;
508  }
509 
510  if (dst < ipv6_bytes_end)
511  {
512  DBUG_PRINT("error", ("str_to_ipv6(%.*s): invalid IPv6 address: "
513  "too few groups.", str_length, str));
514  return false;
515  }
516 
517  return true;
518 }
519 
521 
534 static void ipv4_to_str(const in_addr *ipv4, char *str)
535 {
536  const unsigned char *ipv4_bytes= (const unsigned char *) ipv4;
537 
538  sprintf(str, "%d.%d.%d.%d",
539  ipv4_bytes[0], ipv4_bytes[1], ipv4_bytes[2], ipv4_bytes[3]);
540 }
542 
555 static void ipv6_to_str(const in6_addr *ipv6, char *str)
556 {
557  struct Region
558  {
559  int pos;
560  int length;
561  };
562 
563  const unsigned char *ipv6_bytes= (const unsigned char *) ipv6;
564 
565  // 1. Translate IPv6-address bytes to words.
566  // We can't just cast to short, because it's not guaranteed
567  // that sizeof (short) == 2. So, we have to make a copy.
568 
569  uint16 ipv6_words[IN6_ADDR_NUM_WORDS];
570 
571  for (int i= 0; i < IN6_ADDR_NUM_WORDS; ++i)
572  ipv6_words[i]= (ipv6_bytes[2 * i] << 8) + ipv6_bytes[2 * i + 1];
573 
574  // 2. Find "the gap" -- longest sequence of zeros in IPv6-address.
575 
576  Region gap= { -1, -1 };
577 
578  {
579  Region rg= { -1, -1 };
580 
581  for (int i = 0; i < IN6_ADDR_NUM_WORDS; ++i)
582  {
583  if (ipv6_words[i] != 0)
584  {
585  if (rg.pos >= 0)
586  {
587  if (rg.length > gap.length)
588  gap= rg;
589 
590  rg.pos= -1;
591  rg.length= -1;
592  }
593  }
594  else
595  {
596  if (rg.pos >= 0)
597  {
598  ++rg.length;
599  }
600  else
601  {
602  rg.pos= i;
603  rg.length= 1;
604  }
605  }
606  }
607 
608  if (rg.pos >= 0)
609  {
610  if (rg.length > gap.length)
611  gap= rg;
612  }
613  }
614 
615  // 3. Convert binary data to string.
616 
617  char *p= str;
618 
619  for (int i = 0; i < IN6_ADDR_NUM_WORDS; ++i)
620  {
621  if (i == gap.pos)
622  {
623  // We're at the gap position. We should put trailing ':' and jump to
624  // the end of the gap.
625 
626  if (i == 0)
627  {
628  // The gap starts from the beginning of the data -- leading ':'
629  // should be put additionally.
630 
631  *p= ':';
632  ++p;
633  }
634 
635  *p= ':';
636  ++p;
637 
638  i += gap.length - 1;
639  }
640  else if (i == 6 && gap.pos == 0 &&
641  (gap.length == 6 || // IPv4-compatible
642  (gap.length == 5 && ipv6_words[5] == 0xffff) // IPv4-mapped
643  ))
644  {
645  // The data represents either IPv4-compatible or IPv4-mapped address.
646  // The IPv6-part (zeros or zeros + ffff) has been already put into
647  // the string (str). Now it's time to dump IPv4-part.
648 
649  ipv4_to_str((const in_addr *) (ipv6_bytes + 12), p);
650  return;
651  }
652  else
653  {
654  // Usual IPv6-address-field. Print it out using lower-case
655  // hex-letters without leading zeros (recommended IPv6-format).
656  //
657  // If it is not the last field, append closing ':'.
658 
659  p += sprintf(p, "%x", ipv6_words[i]);
660 
661  if (i != IN6_ADDR_NUM_WORDS - 1)
662  {
663  *p= ':';
664  ++p;
665  }
666  }
667  }
668 
669  *p= 0;
670 }
671 
673 
686 {
687  // ipv4-string -> varbinary(4)
688  // ipv6-string -> varbinary(16)
689 
690  in_addr ipv4_address;
691  in6_addr ipv6_address;
692 
693  if (str_to_ipv4(arg->ptr(), arg->length(), &ipv4_address))
694  {
695  buffer->length(0);
696  buffer->append((char *) &ipv4_address, sizeof (in_addr), &my_charset_bin);
697 
698  return true;
699  }
700 
701  if (str_to_ipv6(arg->ptr(), arg->length(), &ipv6_address))
702  {
703  buffer->length(0);
704  buffer->append((char *) &ipv6_address, sizeof (in6_addr), &my_charset_bin);
705 
706  return true;
707  }
708 
709  return false;
710 }
711 
713 
726 {
727  if (arg->charset() != &my_charset_bin)
728  return false;
729 
730  if ((int) arg->length() == IN_ADDR_SIZE)
731  {
732  char str[INET_ADDRSTRLEN];
733 
734  ipv4_to_str((const in_addr *) arg->ptr(), str);
735 
736  buffer->length(0);
737  buffer->append(str, (uint32) strlen(str), &my_charset_latin1);
738 
739  return true;
740  }
741  else if ((int) arg->length() == IN6_ADDR_SIZE)
742  {
743  char str[INET6_ADDRSTRLEN];
744 
745  ipv6_to_str((const in6_addr *) arg->ptr(), str);
746 
747  buffer->length(0);
748  buffer->append(str, (uint32) strlen(str), &my_charset_latin1);
749 
750  return true;
751  }
752 
753  DBUG_PRINT("info",
754  ("INET6_NTOA(): varbinary(4) or varbinary(16) expected."));
755  return false;
756 }
757 
759 
771 {
772  in_addr ipv4_address;
773 
774  return str_to_ipv4(arg->ptr(), arg->length(), &ipv4_address);
775 }
776 
778 
790 {
791  in6_addr ipv6_address;
792 
793  return str_to_ipv6(arg->ptr(), arg->length(), &ipv6_address);
794 }
795 
797 
809 {
810  if ((int) arg->length() != IN6_ADDR_SIZE || arg->charset() != &my_charset_bin)
811  return false;
812 
813  return IN6_IS_ADDR_V4COMPAT((struct in6_addr *) arg->ptr());
814 }
815 
817 
829 {
830  if ((int) arg->length() != IN6_ADDR_SIZE || arg->charset() != &my_charset_bin)
831  return false;
832 
833  return IN6_IS_ADDR_V4MAPPED((struct in6_addr *) arg->ptr());
834 }