~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
  Original copyright header listed below. This comes via rsync.
  Any additional changes are provided via the same license as the original.

  Copyright (C) 2011 Muhammad Umair

*/
/*
 * Copyright (C) 1996-2001  Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <config.h>

#include <drizzled/error.h>
#include <drizzled/charset.h>
#include <drizzled/function/str/strfunc.h>
#include <drizzled/item/func.h>
#include <drizzled/plugin/function.h>

#include <drizzled/type/ipv6.h>

namespace plugin {
namespace ipv6 {

class Generate: public drizzled::Item_str_func
{
public:
  Generate(): drizzled::Item_str_func() {}
  void fix_length_and_dec()
  {
    max_length= (drizzled::type::IPv6::IPV6_BUFFER_LENGTH) * drizzled::system_charset_info->mbmaxlen;
  }
  const char *func_name() const{ return "ipv6"; }
  drizzled::String *val_str(drizzled::String *);
};


drizzled::String *Generate::val_str(drizzled::String *str)
{
  drizzled::String _result;
  drizzled::String *result= val_str(&_result);
  drizzled::type::IPv6 ptr_address;

  if (not ptr_address.inet_pton(result->c_str()))
  {
    drizzled::my_error(drizzled::ER_INVALID_IPV6_VALUE, MYF(ME_FATALERROR));
     str->length(0);

    return str;
  }

  char *ipv6_string;
  str->realloc(drizzled::type::IPv6::IPV6_BUFFER_LENGTH);
  str->length(drizzled::type::IPv6::IPV6_BUFFER_LENGTH);
  str->set_charset(drizzled::system_charset_info);
  ipv6_string= (char *) str->ptr();

  if (not ptr_address.inet_ntop(ipv6_string))
  {
    drizzled::my_error(drizzled::ER_INVALID_IPV6_VALUE, MYF(ME_FATALERROR));
    str->length(0);

    return str;
  }
  str->length(max_length);

  return str;
}

} // namespace ipv6
} // namespace plugin

static int initialize(drizzled::module::Context &context)
{
  context.add(new drizzled::plugin::Create_function<plugin::ipv6::Generate>("ipv6"));

  return 0;
}

DRIZZLE_DECLARE_PLUGIN
{
  DRIZZLE_VERSION_ID,
  "ipv6",
  "1.0",
  "Muhammad Umair",
  "IPV6() function",
  drizzled::PLUGIN_LICENSE_GPL,
  initialize, /* Plugin Init */
  NULL,   /* depends */
  NULL    /* config options */
}
DRIZZLE_DECLARE_PLUGIN_END;