Skip to content

Commit

Permalink
[k2] move ip functions to common (#1209)
Browse files Browse the repository at this point in the history
  • Loading branch information
astrophysik authored Jan 17, 2025
1 parent 4916ba3 commit 042a713
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 46 deletions.
6 changes: 6 additions & 0 deletions builtin-functions/kphp-light/server.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ function numa_get_bound_node(): int;
function setcookie ($name ::: string, $value ::: string, $expire_or_options ::: int = 0, $path ::: string = '', $domain ::: string = '', $secure ::: bool = false, $http_only ::: bool = false): void;

function setrawcookie ($name ::: string, $value ::: string, $expire_or_options ::: int = 0, $path ::: string = '', $domain ::: string = '', $secure ::: bool = false, $http_only ::: bool = false): void;

function ip2long ($ip ::: string) ::: int | false;

function ip2ulong ($ip ::: string) ::: string | false;

function long2ip ($ip ::: int) ::: string;
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ function instance_cache_update_ttl(string $key, int $ttl = 0) ::: bool;
function instance_cache_delete(string $key) ::: bool;


/** @kphp-extern-func-info generate-stub */
function ip2long ($ip ::: string) ::: int | false;
/** @kphp-extern-func-info generate-stub */
function ip2ulong ($ip ::: string) ::: string | false;
/** @kphp-extern-func-info generate-stub */
function long2ip ($ip ::: int) ::: string;
/** @kphp-extern-func-info generate-stub */
function thread_pool_test_load($size ::: int, $n ::: int, $a ::: float, $b ::: float) ::: float;
/** @kphp-extern-func-info generate-stub */
Expand Down
43 changes: 43 additions & 0 deletions runtime-common/stdlib/server/net-functions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2025 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#include "runtime-common/stdlib/server/net-functions.h"

#include <arpa/inet.h>
#include <cstdio>

namespace {
constexpr int64_t IPV4_SIZE = 25;
}

Optional<int64_t> f$ip2long(const string &ip) noexcept {
struct in_addr result;
if (inet_pton(AF_INET, ip.c_str(), &result) != 1) {
return false;
}
return ntohl(result.s_addr);
}

Optional<string> f$ip2ulong(const string &ip) noexcept {
struct in_addr result;
if (inet_pton(AF_INET, ip.c_str(), &result) != 1) {
return false;
}

char buf[IPV4_SIZE];
int len = snprintf(buf, IPV4_SIZE, "%u", ntohl(result.s_addr));
return string(buf, len);
}

string f$long2ip(int64_t num) noexcept {
auto &runtime_context{RuntimeContext::get()};
runtime_context.static_SB.clean().reserve(IPV4_SIZE);
for (int i = 3; i >= 0; i--) {
runtime_context.static_SB << ((num >> (i * 8)) & 255);
if (i) {
runtime_context.static_SB.append_char('.');
}
}
return runtime_context.static_SB.str();
}
15 changes: 15 additions & 0 deletions runtime-common/stdlib/server/net-functions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2024 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#pragma once

#include <cstdint>

#include "runtime-common/core/runtime-core.h"

Optional<int64_t> f$ip2long(const string &ip) noexcept;

Optional<string> f$ip2ulong(const string &ip) noexcept;

string f$long2ip(int64_t num) noexcept;
3 changes: 2 additions & 1 deletion runtime-common/stdlib/stdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ prepend(STDLIB_SERIALIZATION stdlib/serialization/ json-functions.cpp
json-writer.cpp serialize-functions.cpp)
prepend(STDLIB_STRING stdlib/string/ mbstring-functions.cpp
regex-functions.cpp string-functions.cpp)
prepend(STDLIB_SERVER stdlib/server/ url-functions.cpp)
prepend(STDLIB_SERVER stdlib/server/ url-functions.cpp
net-functions.cpp)
prepend(STDLIB_VKEXT stdlib/vkext/ string-processing.cpp
vkext-functions.cpp)

Expand Down
32 changes: 0 additions & 32 deletions runtime/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,27 +745,6 @@ void f$die(const mixed &v) {
f$exit(v);
}


Optional<int64_t> f$ip2long(const string &ip) {
struct in_addr result;
if (inet_pton(AF_INET, ip.c_str(), &result) != 1) {
return false;
}
return ntohl(result.s_addr);
}

Optional<string> f$ip2ulong(const string &ip) {
struct in_addr result;
if (inet_pton(AF_INET, ip.c_str(), &result) != 1) {
return false;
}

const size_t buf_size = 25;
char buf[buf_size];
int len = snprintf(buf, buf_size, "%u", ntohl(result.s_addr));
return string(buf, len);
}

double f$thread_pool_test_load(int64_t size, int64_t n, double a, double b) {
constexpr auto job = [](int64_t n, double a, double b) {
double res = 0;
Expand All @@ -788,17 +767,6 @@ double f$thread_pool_test_load(int64_t size, int64_t n, double a, double b) {
return result;
}

string f$long2ip(int64_t num) {
kphp_runtime_context.static_SB.clean().reserve(100);
for (int i = 3; i >= 0; i--) {
kphp_runtime_context.static_SB << ((num >> (i * 8)) & 255);
if (i) {
kphp_runtime_context.static_SB.append_char('.');
}
}
return kphp_runtime_context.static_SB.str();
}

Optional<array<string>> f$gethostbynamel(const string &name) {
dl::enter_critical_section();//OK
struct hostent *hp = gethostbyname(name.c_str());
Expand Down
7 changes: 1 addition & 6 deletions runtime/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "common/wrappers/string_view.h"

#include "runtime-common/core/runtime-core.h"
#include "runtime-common/stdlib/server/net-functions.h"
#include "runtime/critical_section.h"
#include "runtime/php-script-globals.h"
#include "server/php-query-data.h"
Expand Down Expand Up @@ -100,14 +101,8 @@ void f$exit(const mixed &v = 0);
__attribute__((noreturn))
void f$die(const mixed &v = 0);

Optional<int64_t> f$ip2long(const string &ip);

Optional<string> f$ip2ulong(const string &ip);

double f$thread_pool_test_load(int64_t size, int64_t n, double a, double b);

string f$long2ip(int64_t num);

Optional<array<string>> f$gethostbynamel(const string &name);

Optional<string> f$inet_pton(const string &address);
Expand Down
2 changes: 1 addition & 1 deletion tests/phpt/dl/667_geoip.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@ok geoip k2_skip
@ok geoip
<?php

function _dechex($u) {
Expand Down

0 comments on commit 042a713

Please sign in to comment.