Skip to content

Commit a5590a2

Browse files
authored
[libc] implement inet_addr (#167708)
This patch adds the posix function `inet_addr`. Since most of the parsing logic is delegated to `inet_aton`, I have only included some basic smoke tests for testing purposes.
1 parent 7c09f12 commit a5590a2

File tree

10 files changed

+106
-0
lines changed

10 files changed

+106
-0
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,7 @@ if(LLVM_LIBC_FULL_BUILD)
945945
# arpa/inet.h entrypoints
946946
libc.src.arpa.inet.htonl
947947
libc.src.arpa.inet.htons
948+
libc.src.arpa.inet.inet_addr
948949
libc.src.arpa.inet.inet_aton
949950
libc.src.arpa.inet.ntohl
950951
libc.src.arpa.inet.ntohs

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,7 @@ if(LLVM_LIBC_FULL_BUILD)
10781078
# arpa/inet.h entrypoints
10791079
libc.src.arpa.inet.htonl
10801080
libc.src.arpa.inet.htons
1081+
libc.src.arpa.inet.inet_addr
10811082
libc.src.arpa.inet.inet_aton
10821083
libc.src.arpa.inet.ntohl
10831084
libc.src.arpa.inet.ntohs

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,7 @@ if(LLVM_LIBC_FULL_BUILD)
11191119
# arpa/inet.h entrypoints
11201120
libc.src.arpa.inet.htonl
11211121
libc.src.arpa.inet.htons
1122+
libc.src.arpa.inet.inet_addr
11221123
libc.src.arpa.inet.inet_aton
11231124
libc.src.arpa.inet.ntohl
11241125
libc.src.arpa.inet.ntohs

libc/include/arpa/inet.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ header_template: inet.h.def
33
macros: []
44
types:
55
- type_name: in_addr
6+
- type_name: in_addr_t
67
enums: []
78
objects: []
89
functions:
@@ -18,6 +19,12 @@ functions:
1819
return_type: uint16_t
1920
arguments:
2021
- type: uint16_t
22+
- name: inet_addr
23+
standards:
24+
- POSIX
25+
return_type: in_addr_t
26+
arguments:
27+
- type: const char *
2128
- name: inet_aton
2229
standards:
2330
- llvm_libc_ext

libc/include/llvm-libc-macros/netinet-in-macros.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#define INADDR_ANY __LLVM_LIBC_CAST(static_cast, in_addr_t, 0x00000000)
3131
#define INADDR_BROADCAST __LLVM_LIBC_CAST(static_cast, in_addr_t, 0xffffffff)
32+
#define INADDR_NONE __LLVM_LIBC_CAST(static_cast, in_addr_t, 0xffffffff)
3233

3334
#define INET_ADDRSTRLEN 16
3435
#define INET6_ADDRSTRLEN 46

libc/src/arpa/inet/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ add_entrypoint_object(
3535
libc.src.__support.str_to_integer
3636
)
3737

38+
add_entrypoint_object(
39+
inet_addr
40+
SRCS
41+
inet_addr.cpp
42+
HDRS
43+
inet_addr.h
44+
DEPENDS
45+
libc.include.arpa_inet
46+
libc.include.llvm-libc-macros.netinet_in_macros
47+
libc.include.llvm-libc-types.in_addr
48+
libc.include.llvm-libc-types.in_addr_t
49+
libc.src.__support.common
50+
libc.src.arpa.inet.inet_aton
51+
)
52+
3853
add_entrypoint_object(
3954
ntohl
4055
SRCS

libc/src/arpa/inet/inet_addr.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Implementation of inet_addr function ------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/arpa/inet/inet_addr.h"
10+
#include "include/llvm-libc-macros/netinet-in-macros.h"
11+
#include "include/llvm-libc-types/in_addr.h"
12+
#include "include/llvm-libc-types/in_addr_t.h"
13+
#include "src/__support/common.h"
14+
#include "src/arpa/inet/inet_aton.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
LLVM_LIBC_FUNCTION(in_addr_t, inet_addr, (const char *cp)) {
19+
in_addr addr;
20+
return inet_aton(cp, &addr) ? addr.s_addr : INADDR_NONE;
21+
}
22+
23+
} // namespace LIBC_NAMESPACE_DECL

libc/src/arpa/inet/inet_addr.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header of inet_addr ----------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_ARPA_INET_INET_ADDR_H
10+
#define LLVM_LIBC_SRC_ARPA_INET_INET_ADDR_H
11+
12+
#include "include/llvm-libc-types/in_addr_t.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
in_addr_t inet_addr(const char *cp);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_ARPA_INET_INET_ADDR_H

libc/test/src/arpa/inet/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ add_libc_unittest(
2222
libc.src.arpa.inet.ntohs
2323
)
2424

25+
add_libc_unittest(
26+
inet_addr
27+
SUITE
28+
libc_arpa_inet_unittests
29+
SRCS
30+
inet_addr_test.cpp
31+
DEPENDS
32+
libc.src.arpa.inet.htonl
33+
libc.src.arpa.inet.inet_addr
34+
)
35+
2536
add_libc_unittest(
2637
inet_aton
2738
SUITE
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Unittests for inet_addr -------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/arpa/inet/htonl.h"
10+
#include "src/arpa/inet/inet_addr.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
namespace LIBC_NAMESPACE_DECL {
14+
15+
TEST(LlvmLibcInetAddr, ValidTest) {
16+
ASSERT_EQ(htonl(0x7f010204), inet_addr("127.1.2.4"));
17+
ASSERT_EQ(htonl(0x7f010004), inet_addr("127.1.4"));
18+
}
19+
20+
TEST(LlvmLibcInetAddr, InvalidTest) {
21+
ASSERT_EQ(htonl(0xffffffff), inet_addr(""));
22+
ASSERT_EQ(htonl(0xffffffff), inet_addr("x"));
23+
}
24+
25+
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)