GetHost() now handles IPv4 and IPv6 addresses

This commit is contained in:
Russ Magee 2018-10-02 11:04:33 -07:00
parent 448de8b524
commit ab96663864
2 changed files with 29 additions and 22 deletions

View file

@ -78,6 +78,7 @@ import "C"
import (
"fmt"
"log"
"net"
"os/user"
"strings"
@ -90,22 +91,27 @@ type UtmpEntry struct {
}
// return remote client hostname or IP if host lookup fails
// addr is expected to be of the format given by net.Addr.String()
// eg., "127.0.0.1:80" or "[::1]:80"
func GetHost(addr string) (h string) {
// Strip off the port after IP addr
hList, e := net.LookupAddr(strings.Split(addr,":")[0])
fmt.Printf("lookupAddr:%v\n", hList)
if e != nil {
h = strings.Split(addr,":")[0]
if !strings.Contains(addr, "[") {
h = strings.Split(addr, ":")[0]
} else {
h = strings.Split(strings.Split(addr, "[")[1], "]")[0]
}
hList, e := net.LookupAddr(h)
//fmt.Printf("lookupAddr:%v\n", hList)
if e == nil {
h = hList[0]
}
return
}
// Put a username and the originating host/IP to utmp
func Put_utmp(user string, host string) (UtmpEntry) {
func Put_utmp(user string, host string) UtmpEntry {
var entry UtmpEntry
log.Println("Put_utmp:host ", host, " user ", user)
C.pututmp(&entry.entry, C.CString(user), C.CString(host))
return entry
}

View file

@ -1,8 +1,9 @@
package main
import (
"blitter.com/go/goutmp"
"time"
"blitter.com/go/goutmp"
)
func main() {