-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc] implement inet_addr #167708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
c8ef
wants to merge
1
commit into
llvm:main
Choose a base branch
from
c8ef:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[libc] implement inet_addr #167708
+106
−0
Conversation
This file contains hidden or 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
Member
|
@llvm/pr-subscribers-libc @llvm/pr-subscribers-backend-risc-v Author: Connector Switch (c8ef) ChangesFull diff: https://github.com/llvm/llvm-project/pull/167708.diff 10 Files Affected:
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 42571862b24b2..acfd4c8a14acb 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -945,6 +945,7 @@ if(LLVM_LIBC_FULL_BUILD)
# arpa/inet.h entrypoints
libc.src.arpa.inet.htonl
libc.src.arpa.inet.htons
+ libc.src.arpa.inet.inet_addr
libc.src.arpa.inet.inet_aton
libc.src.arpa.inet.ntohl
libc.src.arpa.inet.ntohs
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index b62a46b7178d5..83d5c42816632 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1078,6 +1078,7 @@ if(LLVM_LIBC_FULL_BUILD)
# arpa/inet.h entrypoints
libc.src.arpa.inet.htonl
libc.src.arpa.inet.htons
+ libc.src.arpa.inet.inet_addr
libc.src.arpa.inet.inet_aton
libc.src.arpa.inet.ntohl
libc.src.arpa.inet.ntohs
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 8a46a7a1baae3..017822dc864a2 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1116,6 +1116,7 @@ if(LLVM_LIBC_FULL_BUILD)
# arpa/inet.h entrypoints
libc.src.arpa.inet.htonl
libc.src.arpa.inet.htons
+ libc.src.arpa.inet.inet_addr
libc.src.arpa.inet.inet_aton
libc.src.arpa.inet.ntohl
libc.src.arpa.inet.ntohs
diff --git a/libc/include/arpa/inet.yaml b/libc/include/arpa/inet.yaml
index 6e0629072b6ef..350a4d74e5bec 100644
--- a/libc/include/arpa/inet.yaml
+++ b/libc/include/arpa/inet.yaml
@@ -3,6 +3,7 @@ header_template: inet.h.def
macros: []
types:
- type_name: in_addr
+ - type_name: in_addr_t
enums: []
objects: []
functions:
@@ -18,6 +19,12 @@ functions:
return_type: uint16_t
arguments:
- type: uint16_t
+ - name: inet_addr
+ standards:
+ - POSIX
+ return_type: in_addr_t
+ arguments:
+ - type: const char *
- name: inet_aton
standards:
- llvm_libc_ext
diff --git a/libc/include/llvm-libc-macros/netinet-in-macros.h b/libc/include/llvm-libc-macros/netinet-in-macros.h
index 2011c34e288cd..7a4d26d832114 100644
--- a/libc/include/llvm-libc-macros/netinet-in-macros.h
+++ b/libc/include/llvm-libc-macros/netinet-in-macros.h
@@ -29,6 +29,7 @@
#define INADDR_ANY __LLVM_LIBC_CAST(static_cast, in_addr_t, 0x00000000)
#define INADDR_BROADCAST __LLVM_LIBC_CAST(static_cast, in_addr_t, 0xffffffff)
+#define INADDR_NONE __LLVM_LIBC_CAST(static_cast, in_addr_t, 0xffffffff)
#define INET_ADDRSTRLEN 16
#define INET6_ADDRSTRLEN 46
diff --git a/libc/src/arpa/inet/CMakeLists.txt b/libc/src/arpa/inet/CMakeLists.txt
index bb43e24ec9d0b..3b3d0f43b8586 100644
--- a/libc/src/arpa/inet/CMakeLists.txt
+++ b/libc/src/arpa/inet/CMakeLists.txt
@@ -35,6 +35,21 @@ add_entrypoint_object(
libc.src.__support.str_to_integer
)
+add_entrypoint_object(
+ inet_addr
+ SRCS
+ inet_addr.cpp
+ HDRS
+ inet_addr.h
+ DEPENDS
+ libc.include.arpa_inet
+ libc.include.llvm-libc-macros.netinet_in_macros
+ libc.include.llvm-libc-types.in_addr
+ libc.include.llvm-libc-types.in_addr_t
+ libc.src.__support.common
+ libc.src.arpa.inet.inet_aton
+)
+
add_entrypoint_object(
ntohl
SRCS
diff --git a/libc/src/arpa/inet/inet_addr.cpp b/libc/src/arpa/inet/inet_addr.cpp
new file mode 100644
index 0000000000000..8ce88c0df8aec
--- /dev/null
+++ b/libc/src/arpa/inet/inet_addr.cpp
@@ -0,0 +1,23 @@
+//===-- Implementation of inet_addr function ------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/arpa/inet/inet_addr.h"
+#include "include/llvm-libc-macros/netinet-in-macros.h"
+#include "include/llvm-libc-types/in_addr.h"
+#include "include/llvm-libc-types/in_addr_t.h"
+#include "src/__support/common.h"
+#include "src/arpa/inet/inet_aton.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(in_addr_t, inet_addr, (const char *cp)) {
+ in_addr addr;
+ return inet_aton(cp, &addr) ? addr.s_addr : INADDR_NONE;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/arpa/inet/inet_addr.h b/libc/src/arpa/inet/inet_addr.h
new file mode 100644
index 0000000000000..66f1ae80dd5a0
--- /dev/null
+++ b/libc/src/arpa/inet/inet_addr.h
@@ -0,0 +1,21 @@
+//===-- Implementation header of inet_addr ----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_ARPA_INET_INET_ADDR_H
+#define LLVM_LIBC_SRC_ARPA_INET_INET_ADDR_H
+
+#include "include/llvm-libc-types/in_addr_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+in_addr_t inet_addr(const char *cp);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_ARPA_INET_INET_ADDR_H
diff --git a/libc/test/src/arpa/inet/CMakeLists.txt b/libc/test/src/arpa/inet/CMakeLists.txt
index 690f751bef5e1..1d400c4374f70 100644
--- a/libc/test/src/arpa/inet/CMakeLists.txt
+++ b/libc/test/src/arpa/inet/CMakeLists.txt
@@ -22,6 +22,17 @@ add_libc_unittest(
libc.src.arpa.inet.ntohs
)
+add_libc_unittest(
+ inet_addr
+ SUITE
+ libc_arpa_inet_unittests
+ SRCS
+ inet_addr_test.cpp
+ DEPENDS
+ libc.src.arpa.inet.htonl
+ libc.src.arpa.inet.inet_addr
+)
+
add_libc_unittest(
inet_aton
SUITE
diff --git a/libc/test/src/arpa/inet/inet_addr_test.cpp b/libc/test/src/arpa/inet/inet_addr_test.cpp
new file mode 100644
index 0000000000000..3f0ea0fc3b3f3
--- /dev/null
+++ b/libc/test/src/arpa/inet/inet_addr_test.cpp
@@ -0,0 +1,25 @@
+//===-- Unittests for inet_addr -------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/arpa/inet/htonl.h"
+#include "src/arpa/inet/inet_addr.h"
+#include "test/UnitTest/Test.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+TEST(LlvmLibcInetAddr, ValidTest) {
+ ASSERT_EQ(htonl(0x7f010204), inet_addr("127.1.2.4"));
+ ASSERT_EQ(htonl(0x7f010004), inet_addr("127.1.4"));
+}
+
+TEST(LlvmLibcInetAddr, InvalidTest) {
+ ASSERT_EQ(htonl(0xffffffff), inet_addr(""));
+ ASSERT_EQ(htonl(0xffffffff), inet_addr("x"));
+}
+
+} // namespace LIBC_NAMESPACE_DECL
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This patch adds the posix function
inet_addr. Since most of the parsing logic is delegated toinet_aton, I have only included some basic smoke tests for testing purposes.