Skip to content

Commit 3974064

Browse files
author
Syyong
committed
Add Network funcs
1 parent d181fd6 commit 3974064

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ system()
167167
passthru()
168168
```
169169

170+
### Network Functions
171+
```php
172+
gethostname()
173+
gethostbyname()
174+
gethostbynamel()
175+
gethostbyaddr()
176+
ip2long()
177+
long2ip()
178+
```
179+
170180
### Other Functions
171181
```php
172182
echo()

php2go.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030
"reflect"
3131
"os/exec"
3232
"regexp"
33+
"net"
34+
"encoding/binary"
3335
)
3436

3537
//////////// Date/Time Functions ////////////
@@ -1649,6 +1651,74 @@ func Passthru(command string, returnVar *int) {
16491651
}
16501652
}
16511653

1654+
//////////// Network Functions ////////////
1655+
1656+
// gethostname()
1657+
func Gethostname() (string, error) {
1658+
return os.Hostname()
1659+
}
1660+
1661+
// gethostbyname()
1662+
// Get the IPv4 address corresponding to a given Internet host name
1663+
func Gethostbyname(hostname string) (string, error) {
1664+
ips, err := net.LookupIP(hostname)
1665+
if ips != nil {
1666+
for _, v := range ips {
1667+
if v.To4() != nil {
1668+
return v.String(), nil
1669+
}
1670+
}
1671+
return "", nil
1672+
}
1673+
return "", err
1674+
}
1675+
1676+
// gethostbynamel()
1677+
// Get a list of IPv4 addresses corresponding to a given Internet host name
1678+
func Gethostbynamel(hostname string) ([]string, error) {
1679+
ips, err := net.LookupIP(hostname)
1680+
if ips != nil {
1681+
var ipstrs []string
1682+
for _, v := range ips {
1683+
if v.To4() != nil {
1684+
ipstrs = append(ipstrs, v.String())
1685+
}
1686+
}
1687+
return ipstrs, nil
1688+
}
1689+
return nil, err
1690+
}
1691+
1692+
// gethostbyaddr()
1693+
// Get the Internet host name corresponding to a given IP address
1694+
func Gethostbyaddr(ipAddress string) (string, error) {
1695+
names, err := net.LookupAddr(ipAddress)
1696+
if names != nil {
1697+
return strings.TrimRight(names[0], "."), nil
1698+
}
1699+
return "", err
1700+
}
1701+
1702+
// ip2long()
1703+
// IPv4
1704+
func Ip2long(ipAddress string) uint32 {
1705+
ip := net.ParseIP(ipAddress)
1706+
if ip == nil {
1707+
return 0
1708+
}
1709+
ip = ip.To4()
1710+
return binary.BigEndian.Uint32(ip)
1711+
}
1712+
1713+
// long2ip()
1714+
// IPv4
1715+
func Long2ip(properAddress uint32) string {
1716+
ipByte := make([]byte, 4)
1717+
binary.BigEndian.PutUint32(ipByte, properAddress)
1718+
ip := net.IP(ipByte)
1719+
return ip.String()
1720+
}
1721+
16521722
//////////// Other Functions ////////////
16531723

16541724
// echo

php2go_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,26 @@ func TestProgramExecution(t *testing.T) {
310310
equal(t, 0, retVal)
311311
}
312312

313+
func TestNetwork(t *testing.T) {
314+
tGethostname, _ := Gethostname()
315+
gt(t, float64(len(tGethostname)), 0)
316+
317+
tIp2long := Ip2long("8.8.8.8")
318+
equal(t, uint32(134744072), tIp2long)
319+
320+
tLong2ip := Long2ip(134744072)
321+
equal(t, "8.8.8.8", tLong2ip)
322+
323+
tGethostbyname, _ := Gethostbyname("localhost")
324+
equal(t, "127.0.0.1", tGethostbyname)
325+
326+
tGethostbynamel, _ := Gethostbynamel("localhost")
327+
gt(t, float64(len(tGethostbynamel)), 0)
328+
329+
tGethostbyaddr, _ := Gethostbyaddr("127.0.0.1")
330+
equal(t, "localhost", tGethostbyaddr)
331+
}
332+
313333
func TestOther(t *testing.T) {
314334
tVersionCompare := VersionCompare("1.3-beta", "1.4Rc1", "<")
315335
equal(t, true, tVersionCompare)

0 commit comments

Comments
 (0)