2
* Copyright 2015-2016 DataStax, Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
#include "php_driver.h"
18
#include "php_driver_types.h"
19
#include "util/inet.h"
20
#include "util/types.h"
22
zend_class_entry *php_driver_inet_ce = NULL;
25
php_driver_inet_init(INTERNAL_FUNCTION_PARAMETERS)
27
php_driver_inet *self;
29
php5to7_size string_len;
31
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &string, &string_len) == FAILURE) {
35
if (getThis() && instanceof_function(Z_OBJCE_P(getThis()), php_driver_inet_ce TSRMLS_CC)) {
36
self = PHP_DRIVER_GET_INET(getThis());
38
object_init_ex(return_value, php_driver_inet_ce);
39
self = PHP_DRIVER_GET_INET(return_value);
42
if (!php_driver_parse_ip_address(string, &self->inet TSRMLS_CC)) {
47
/* {{{ Inet::__construct(string) */
48
PHP_METHOD(Inet, __construct)
50
php_driver_inet_init(INTERNAL_FUNCTION_PARAM_PASSTHRU);
54
/* {{{ Inet::__toString() */
55
PHP_METHOD(Inet, __toString)
57
php_driver_inet *inet = PHP_DRIVER_GET_INET(getThis());
59
php_driver_format_address(inet->inet, &string);
61
PHP5TO7_RETVAL_STRING(string);
66
/* {{{ Inet::type() */
67
PHP_METHOD(Inet, type)
69
php5to7_zval type = php_driver_type_scalar(CASS_VALUE_TYPE_INET TSRMLS_CC);
70
RETURN_ZVAL(PHP5TO7_ZVAL_MAYBE_P(type), 1, 1);
74
/* {{{ Inet::address() */
75
PHP_METHOD(Inet, address)
77
php_driver_inet *inet = PHP_DRIVER_GET_INET(getThis());
79
php_driver_format_address(inet->inet, &string);
81
PHP5TO7_RETVAL_STRING(string);
86
ZEND_BEGIN_ARG_INFO_EX(arginfo__construct, 0, ZEND_RETURN_VALUE, 1)
87
ZEND_ARG_INFO(0, address)
90
ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, ZEND_RETURN_VALUE, 0)
93
static zend_function_entry php_driver_inet_methods[] = {
94
PHP_ME(Inet, __construct, arginfo__construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
95
PHP_ME(Inet, __toString, arginfo_none, ZEND_ACC_PUBLIC)
96
PHP_ME(Inet, type, arginfo_none, ZEND_ACC_PUBLIC)
97
PHP_ME(Inet, address, arginfo_none, ZEND_ACC_PUBLIC)
101
static php_driver_value_handlers php_driver_inet_handlers;
104
php_driver_inet_gc(zval *object, php5to7_zval_gc table, int *n TSRMLS_DC)
108
return zend_std_get_properties(object TSRMLS_CC);
112
php_driver_inet_properties(zval *object TSRMLS_DC)
116
php5to7_zval address;
118
php_driver_inet *self = PHP_DRIVER_GET_INET(object);
119
HashTable *props = zend_std_get_properties(object TSRMLS_CC);
121
type = php_driver_type_scalar(CASS_VALUE_TYPE_INET TSRMLS_CC);
122
PHP5TO7_ZEND_HASH_UPDATE(props, "type", sizeof("type"), PHP5TO7_ZVAL_MAYBE_P(type), sizeof(zval));
124
php_driver_format_address(self->inet, &string);
125
PHP5TO7_ZVAL_MAYBE_MAKE(address);
126
PHP5TO7_ZVAL_STRING(PHP5TO7_ZVAL_MAYBE_P(address), string);
128
PHP5TO7_ZEND_HASH_UPDATE(props, "address", sizeof("address"), PHP5TO7_ZVAL_MAYBE_P(address), sizeof(zval));
134
php_driver_inet_compare(zval *obj1, zval *obj2 TSRMLS_DC)
136
php_driver_inet *inet1 = NULL;
137
php_driver_inet *inet2 = NULL;
139
if (Z_OBJCE_P(obj1) != Z_OBJCE_P(obj2))
140
return 1; /* different classes */
142
inet1 = PHP_DRIVER_GET_INET(obj1);
143
inet2 = PHP_DRIVER_GET_INET(obj2);
145
if (inet1->inet.address_length != inet2->inet.address_length) {
146
return inet1->inet.address_length < inet2->inet.address_length ? -1 : 1;
148
return memcmp(inet1->inet.address, inet2->inet.address, inet1->inet.address_length);
152
php_driver_inet_hash_value(zval *obj TSRMLS_DC)
154
php_driver_inet *self = PHP_DRIVER_GET_INET(obj);
155
return zend_inline_hash_func((const char *) self->inet.address,
156
self->inet.address_length);
160
php_driver_inet_free(php5to7_zend_object_free *object TSRMLS_DC)
162
php_driver_inet *self = PHP5TO7_ZEND_OBJECT_GET(inet, object);
164
zend_object_std_dtor(&self->zval TSRMLS_CC);
165
PHP5TO7_MAYBE_EFREE(self);
168
static php5to7_zend_object
169
php_driver_inet_new(zend_class_entry *ce TSRMLS_DC)
171
php_driver_inet *self =
172
PHP5TO7_ZEND_OBJECT_ECALLOC(inet, ce);
174
PHP5TO7_ZEND_OBJECT_INIT(inet, self, ce);
177
void php_driver_define_Inet(TSRMLS_D)
181
INIT_CLASS_ENTRY(ce, PHP_DRIVER_NAMESPACE "\\Inet", php_driver_inet_methods);
182
php_driver_inet_ce = zend_register_internal_class(&ce TSRMLS_CC);
183
zend_class_implements(php_driver_inet_ce TSRMLS_CC, 1, php_driver_value_ce);
184
memcpy(&php_driver_inet_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
185
php_driver_inet_handlers.std.get_properties = php_driver_inet_properties;
186
#if PHP_VERSION_ID >= 50400
187
php_driver_inet_handlers.std.get_gc = php_driver_inet_gc;
189
php_driver_inet_handlers.std.compare_objects = php_driver_inet_compare;
190
php_driver_inet_ce->ce_flags |= PHP5TO7_ZEND_ACC_FINAL;
191
php_driver_inet_ce->create_object = php_driver_inet_new;
193
php_driver_inet_handlers.hash_value = php_driver_inet_hash_value;
194
php_driver_inet_handlers.std.clone_obj = NULL;