~drizzle-trunk/drizzle/development

2398.1.1 by Muhammad Umair
merge lp:~mumair/drizzle/drizzle-IPv6Address
1
/*
2
  Original copyright header listed below. This comes via rsync.
3
  Any additional changes are provided via the same license as the original.
4
5
  Copyright (C) 2011 Muhammad Umair
6
7
*/
8
/*
9
 * Copyright (C) 1996-2001  Internet Software Consortium.
10
 *
11
 * Permission to use, copy, modify, and distribute this software for any
12
 * purpose with or without fee is hereby granted, provided that the above
13
 * copyright notice and this permission notice appear in all copies.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
16
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
17
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
18
 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
19
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
20
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
21
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
22
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23
 */
24
25
#include <config.h>
26
27
#include <drizzled/error.h>
28
#include <drizzled/charset.h>
29
#include <drizzled/function/str/strfunc.h>
30
#include <drizzled/item/func.h>
31
#include <drizzled/plugin/function.h>
32
33
#include <drizzled/type/ipv6.h>
34
35
namespace plugin {
36
namespace ipv6 {
37
38
class Generate: public drizzled::Item_str_func
39
{
40
public:
41
  Generate(): drizzled::Item_str_func() {}
42
  void fix_length_and_dec()
43
  {
44
    max_length= (drizzled::type::IPv6::IPV6_BUFFER_LENGTH) * drizzled::system_charset_info->mbmaxlen;
45
  }
46
  const char *func_name() const{ return "ipv6"; }
47
  drizzled::String *val_str(drizzled::String *);
48
};
49
50
51
drizzled::String *Generate::val_str(drizzled::String *str)
52
{
53
  drizzled::String _result;
54
  drizzled::String *result= val_str(&_result);
55
  drizzled::type::IPv6 ptr_address;
56
57
  if (not ptr_address.inet_pton(result->c_str()))
58
  {
59
    drizzled::my_error(drizzled::ER_INVALID_IPV6_VALUE, MYF(ME_FATALERROR));
60
     str->length(0);
61
62
    return str;
63
  }
64
65
  char *ipv6_string;
66
  str->realloc(drizzled::type::IPv6::IPV6_BUFFER_LENGTH);
67
  str->length(drizzled::type::IPv6::IPV6_BUFFER_LENGTH);
68
  str->set_charset(drizzled::system_charset_info);
69
  ipv6_string= (char *) str->ptr();
70
71
  if (not ptr_address.inet_ntop(ipv6_string))
72
  {
73
    drizzled::my_error(drizzled::ER_INVALID_IPV6_VALUE, MYF(ME_FATALERROR));
74
    str->length(0);
75
76
    return str;
77
  }
78
  str->length(max_length);
79
80
  return str;
81
}
82
83
} // namespace ipv6
84
} // namespace plugin
85
86
static int initialize(drizzled::module::Context &context)
87
{
88
  context.add(new drizzled::plugin::Create_function<plugin::ipv6::Generate>("ipv6"));
89
90
  return 0;
91
}
92
93
DRIZZLE_DECLARE_PLUGIN
94
{
95
  DRIZZLE_VERSION_ID,
96
  "ipv6",
97
  "1.0",
98
  "Muhammad Umair",
99
  "IPV6() function",
100
  drizzled::PLUGIN_LICENSE_GPL,
101
  initialize, /* Plugin Init */
102
  NULL,   /* depends */
103
  NULL    /* config options */
104
}
105
DRIZZLE_DECLARE_PLUGIN_END;