posix: arpa_inet
authorMichael Wallner <mike@php.net>
Wed, 8 Feb 2017 15:00:49 +0000 (16:00 +0100)
committerMichael Wallner <mike@php.net>
Wed, 8 Feb 2017 15:00:49 +0000 (16:00 +0100)
config.m4
m4/posix/arpa_inet.m4 [new file with mode: 0644]
psi.d/arpa_inet.psi [new file with mode: 0644]
tests/arpa_inet/inet001.phpt [new file with mode: 0644]
tests/arpa_inet/inet002.phpt [new file with mode: 0644]
tests/arpa_inet/ntoh001.phpt [new file with mode: 0644]

index b4107c878b64c800d03da213c7d0f4df57f0a55a..3ced949809406f3dce2a6c67249a9d7205744daa 100644 (file)
--- a/config.m4
+++ b/config.m4
@@ -16,6 +16,7 @@ m4_foreach(incfile, [
        [psi/psi_decl.m4],
        [psi/psi_macro.m4],
        [psi/psi_composite.m4],
+       [posix/arpa_inet.m4],
        [posix/errno.m4],
        [posix/fcntl.m4],
        [posix/glob.m4],
@@ -126,7 +127,9 @@ if test "$PHP_PSI" != no; then
        PSI_CONFIG_POSIX(stdlib, stdlib.h)
        PSI_CONFIG_POSIX(unistd, unistd.h)
        PSI_CONFIG_POSIX(sys/socket, sys/socket.h, [
-               PSI_CONFIG_POSIX(netinet/in, netinet/in.h)
+               PSI_CONFIG_POSIX(netinet/in, netinet/in.h, [
+                       PSI_CONFIG_POSIX(arpa/inet, arpa/inet.h)
+               ])
                PSI_CONFIG_POSIX(netdb, netdb.h)
        ])
        PSI_CONFIG_POSIX(netinet/tcp, netinet/tcp.h)
diff --git a/m4/posix/arpa_inet.m4 b/m4/posix/arpa_inet.m4
new file mode 100644 (file)
index 0000000..6473607
--- /dev/null
@@ -0,0 +1,11 @@
+PSI_CHECK_ARPA_INET() {
+       PSI_DECL(uint32_t htonl, [(uint32_t l)])
+       PSI_DECL(uint16_t htons, [(uint16_t s)])
+       PSI_DECL(uint32_t ntohl, [(uint32_t l)])
+       PSI_DECL(uint16_t ntohs, [(uint16_t s)])
+       
+       PSI_DECL(in_addr_t inet_addr, [(const char *cp)])
+       PSI_DECL(char *inet_ntoa, [(struct in_addr in)])
+       PSI_DECL(const char *inet_ntop, [(int af, const void *src, char *dst, socklen_t size)])
+       PSI_DECL(int inet_pton, [(int af, const char *src, void *dst)])
+}
\ No newline at end of file
diff --git a/psi.d/arpa_inet.psi b/psi.d/arpa_inet.psi
new file mode 100644 (file)
index 0000000..2a8404e
--- /dev/null
@@ -0,0 +1,46 @@
+function psi\htonl(int $l) : int {
+       let l = intval($l);
+       return to_int(htonl);
+}
+function psi\htons(int $s) : int {
+       let s = intval($s);
+       return to_int(htons);
+}
+
+function psi\ntohl(int $l) : int {
+       let l = intval($l);
+       return to_int(ntohl);
+}
+function psi\ntohs(int $s) : int {
+       let s = intval($s);
+       return to_int(ntohs);
+}
+
+
+function psi\inet_addr(string $cp) : int {
+       let cp = strval($cp);
+       return to_int(inet_addr);
+}
+
+function psi\inet_ntoa(array $in) : string {
+       let in = arrval($in,
+               intval($s_addr)
+       );
+       return to_string(inet_ntoa);
+}
+
+function psi\inet_ntop(int $af, string $src) : string {
+       let af = intval($af);
+       let src = strval($src);
+       let size = psi\SIZEOF_STRUCT_SOCKADDR_STORAGE;
+       let dst = calloc(1, size);
+       return to_string(inet_ntop);
+}
+
+function psi\inet_pton(int $af, string $src, string &$dst) : int {
+       let af = intval($af);
+       let src = strval($src);
+       let dst = calloc(1, psi\SIZEOF_STRUCT_SOCKADDR_STORAGE);
+       return to_int(inet_pton);
+       set $dst = to_string(dst, psi\SIZEOF_STRUCT_SOCKADDR_STORAGE);
+}
\ No newline at end of file
diff --git a/tests/arpa_inet/inet001.phpt b/tests/arpa_inet/inet001.phpt
new file mode 100644 (file)
index 0000000..d366e28
--- /dev/null
@@ -0,0 +1,21 @@
+--TEST--
+inet_ntop/inet_pton
+--INI--
+psi.directory={PWD}/../../psi.d:{PWD}
+--SKIPIF--
+<?php
+extension_loaded("psi") or die("skip - need ext/psi");
+function_exists("psi\\inet_pton") or die("skip - need inet_pton()");
+?>
+--FILE--
+===TEST===
+<?php
+var_dump(psi\inet_pton(psi\AF_INET, "127.1.1.1", $res));
+var_dump(psi\inet_ntop(psi\AF_INET, $res));
+?>
+===DONE===
+--EXPECT--
+===TEST===
+int(1)
+string(9) "127.1.1.1"
+===DONE===
diff --git a/tests/arpa_inet/inet002.phpt b/tests/arpa_inet/inet002.phpt
new file mode 100644 (file)
index 0000000..9cb95a5
--- /dev/null
@@ -0,0 +1,14 @@
+--TEST--
+inet_addr/inet_ntoa
+--INI--
+psi.directory={PWD}/../../psi.d:{PWD}
+--FILE--
+===TEST===
+<?php
+var_dump(psi\inet_ntoa(["s_addr" => psi\inet_addr("217.0.0.1")]));
+?>
+===DONE===
+--EXPECT--
+===TEST===
+string(9) "217.0.0.1"
+===DONE===
diff --git a/tests/arpa_inet/ntoh001.phpt b/tests/arpa_inet/ntoh001.phpt
new file mode 100644 (file)
index 0000000..99d743c
--- /dev/null
@@ -0,0 +1,25 @@
+--TEST--
+arpa/inet
+--INI--
+psi.directory={PWD}/../../psi.d:{PWD}
+--SKIPIF--
+<?php
+extension_loaded("psi") or die("skip - need ext/psi");
+function_exists("psi\\ntohs") or die("skip - need ntohs()");
+?>
+--FILE--
+===TEST===
+<?php
+var_dump(psi\htons(psi\ntohs(55)));
+var_dump(psi\htonl(psi\ntohl(555555)));
+var_dump(psi\ntohs(psi\htons(14080)));
+var_dump(psi\ntohl(psi\htonl(595200000)));
+?>
+===DONE===
+--EXPECT--
+===TEST===
+int(55)
+int(555555)
+int(14080)
+int(595200000)
+===DONE===