-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[k2] move ip functions to common (#1209)
- Loading branch information
1 parent
4916ba3
commit 042a713
Showing
8 changed files
with
68 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
@ok geoip k2_skip | ||
@ok geoip | ||
<?php | ||
|
||
function _dechex($u) { | ||
|