Skip to content

Commit

Permalink
all: fix or remove remaining net.IP <-> string conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Jan 18, 2021
1 parent 7fd0634 commit e99595a
Show file tree
Hide file tree
Showing 16 changed files with 42 additions and 45 deletions.
3 changes: 0 additions & 3 deletions internal/dhcpd/dhcpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,6 @@ func parseOptionString(s string) (uint8, []byte) {
return 0, nil
}
val = ip
if ip.To4() != nil {
val = ip.To4()
}

default:
return 0, nil
Expand Down
5 changes: 3 additions & 2 deletions internal/dhcpd/dhcphttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func convertLeases(inputLeases []Lease, includeExpires bool) []map[string]string
leases := []map[string]string{}
for _, l := range inputLeases {
lease := map[string]string{
"mac": l.HWAddr.String(),
"mac": l.HWAddr.String(),
// TODO(e.burkov): remove conversion while #2509.
"ip": l.IP.String(),
"hostname": l.Hostname,
}
Expand Down Expand Up @@ -70,7 +71,7 @@ func v4JSONToServerConf(j v4ServerConfJSON) V4ServerConf {
}

type v6ServerConfJSON struct {
RangeStart string `json:"range_start"`
RangeStart net.IP `json:"range_start"`
LeaseDuration uint32 `json:"lease_duration"`
}

Expand Down
2 changes: 1 addition & 1 deletion internal/dhcpd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type V6ServerConf struct {

// The first IP address for dynamic leases
// The last allowed IP address ends with 0xff byte
RangeStart string `yaml:"range_start"`
RangeStart net.IP `yaml:"range_start"`

LeaseDuration uint32 `yaml:"lease_duration"` // in seconds

Expand Down
2 changes: 1 addition & 1 deletion internal/dhcpd/v6.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ func v6Create(conf V6ServerConf) (DHCPServer, error) {
return s, nil
}

s.conf.ipStart = net.ParseIP(conf.RangeStart)
s.conf.ipStart = conf.RangeStart
if s.conf.ipStart == nil || s.conf.ipStart.To16() == nil {
return s, fmt.Errorf("dhcpv6: invalid range-start IP: %s", conf.RangeStart)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/dhcpd/v6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func notify6(flags uint32) {
func TestV6StaticLeaseAddRemove(t *testing.T) {
conf := V6ServerConf{
Enabled: true,
RangeStart: "2001::1",
RangeStart: net.ParseIP("2001::1"),
notify: notify6,
}
s, err := v6Create(conf)
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestV6StaticLeaseAddRemove(t *testing.T) {
func TestV6StaticLeaseAddReplaceDynamic(t *testing.T) {
conf := V6ServerConf{
Enabled: true,
RangeStart: "2001::1",
RangeStart: net.ParseIP("2001::1"),
notify: notify6,
}
sIface, err := v6Create(conf)
Expand Down Expand Up @@ -109,7 +109,7 @@ func TestV6StaticLeaseAddReplaceDynamic(t *testing.T) {
func TestV6GetLease(t *testing.T) {
conf := V6ServerConf{
Enabled: true,
RangeStart: "2001::1",
RangeStart: net.ParseIP("2001::1"),
notify: notify6,
}
sIface, err := v6Create(conf)
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestV6GetLease(t *testing.T) {
func TestV6GetDynamicLease(t *testing.T) {
conf := V6ServerConf{
Enabled: true,
RangeStart: "2001::2",
RangeStart: net.ParseIP("2001::2"),
notify: notify6,
}
sIface, err := v6Create(conf)
Expand Down
2 changes: 1 addition & 1 deletion internal/dnsforward/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func IPFromAddr(addr net.Addr) (ip net.IP) {
// IPStringFromAddr extracts IP address from net.Addr.
// Note: we can't use net.SplitHostPort(a.String()) because of IPv6 zone:
// https://github.com/AdguardTeam/AdGuardHome/internal/issues/1261
func IPStringFromAddr(addr net.Addr) (ipstr string) {
func IPStringFromAddr(addr net.Addr) (ipStr string) {
if ip := IPFromAddr(addr); ip != nil {
return ip.String()
}
Expand Down
7 changes: 4 additions & 3 deletions internal/home/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ func httpError(w http.ResponseWriter, code int, format string, args ...interface
// ---------------
// dns run control
// ---------------
func addDNSAddress(dnsAddresses *[]string, addr string) {
func addDNSAddress(dnsAddresses *[]string, addr net.IP) {
hostport := addr.String()
if config.DNS.Port != 53 {
addr = fmt.Sprintf("%s:%d", addr, config.DNS.Port)
hostport = net.JoinHostPort(hostport, strconv.Itoa(config.DNS.Port))
}
*dnsAddresses = append(*dnsAddresses, addr)
*dnsAddresses = append(*dnsAddresses, hostport)
}

func handleStatus(w http.ResponseWriter, _ *http.Request) {
Expand Down
14 changes: 7 additions & 7 deletions internal/home/controlinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ func (web *Web) handleInstallCheckConfig(w http.ResponseWriter, r *http.Request)
}

if reqData.Web.Port != 0 && reqData.Web.Port != config.BindPort && reqData.Web.Port != config.BetaBindPort {
err = util.CheckPortAvailable(reqData.Web.IP.String(), reqData.Web.Port)
err = util.CheckPortAvailable(reqData.Web.IP, reqData.Web.Port)
if err != nil {
respData.Web.Status = fmt.Sprintf("%v", err)
}
}

if reqData.DNS.Port != 0 {
err = util.CheckPacketPortAvailable(reqData.DNS.IP.String(), reqData.DNS.Port)
err = util.CheckPacketPortAvailable(reqData.DNS.IP, reqData.DNS.Port)

if util.ErrorIsAddrInUse(err) {
canAutofix := checkDNSStubListener()
Expand All @@ -125,15 +125,15 @@ func (web *Web) handleInstallCheckConfig(w http.ResponseWriter, r *http.Request)
log.Error("Couldn't disable DNSStubListener: %s", err)
}

err = util.CheckPacketPortAvailable(reqData.DNS.IP.String(), reqData.DNS.Port)
err = util.CheckPacketPortAvailable(reqData.DNS.IP, reqData.DNS.Port)
canAutofix = false
}

respData.DNS.CanAutofix = canAutofix
}

if err == nil {
err = util.CheckPortAvailable(reqData.DNS.IP.String(), reqData.DNS.Port)
err = util.CheckPortAvailable(reqData.DNS.IP, reqData.DNS.Port)
}

if err != nil {
Expand Down Expand Up @@ -304,7 +304,7 @@ func (web *Web) handleInstallConfigure(w http.ResponseWriter, r *http.Request) {

// validate that hosts and ports are bindable
if restartHTTP {
err = util.CheckPortAvailable(newSettings.Web.IP.String(), newSettings.Web.Port)
err = util.CheckPortAvailable(newSettings.Web.IP, newSettings.Web.Port)
if err != nil {
httpError(w, http.StatusBadRequest, "Impossible to listen on IP:port %s due to %s",
net.JoinHostPort(newSettings.Web.IP.String(), strconv.Itoa(newSettings.Web.Port)), err)
Expand All @@ -313,13 +313,13 @@ func (web *Web) handleInstallConfigure(w http.ResponseWriter, r *http.Request) {

}

err = util.CheckPacketPortAvailable(newSettings.DNS.IP.String(), newSettings.DNS.Port)
err = util.CheckPacketPortAvailable(newSettings.DNS.IP, newSettings.DNS.Port)
if err != nil {
httpError(w, http.StatusBadRequest, "%s", err)
return
}

err = util.CheckPortAvailable(newSettings.DNS.IP.String(), newSettings.DNS.Port)
err = util.CheckPortAvailable(newSettings.DNS.IP, newSettings.DNS.Port)
if err != nil {
httpError(w, http.StatusBadRequest, "%s", err)
return
Expand Down
16 changes: 7 additions & 9 deletions internal/home/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,11 @@ func getDNSAddresses() []string {

for _, iface := range ifaces {
for _, addr := range iface.Addresses {
addDNSAddress(&dnsAddresses, addr.String())
addDNSAddress(&dnsAddresses, addr)
}
}
} else {
addDNSAddress(&dnsAddresses, config.DNS.BindHost.String())
addDNSAddress(&dnsAddresses, config.DNS.BindHost)
}

dnsEncryption := getDNSEncryption()
Expand Down Expand Up @@ -326,14 +326,12 @@ func startDNSServer() error {
Context.queryLog.Start()

const topClientsNumber = 100 // the number of clients to get
topClients := Context.stats.GetTopClientsIP(topClientsNumber)
for _, ip := range topClients {
ipAddr := net.ParseIP(ip)
if !ipAddr.IsLoopback() {
Context.rdns.Begin(ipAddr)
for _, ip := range Context.stats.GetTopClientsIP(topClientsNumber) {
if !ip.IsLoopback() {
Context.rdns.Begin(ip)
}
if !Context.ipDetector.detectSpecialNetwork(ipAddr) {
Context.whois.Begin(ipAddr)
if !Context.ipDetector.detectSpecialNetwork(ip) {
Context.whois.Begin(ip)
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/home/rdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func InitRDNS(dnsServer *dnsforward.Server, clients *clientsContainer) *RDNS {
// Begin - add IP address to rDNS queue
func (r *RDNS) Begin(ip net.IP) {
now := uint64(time.Now().Unix())
expire := r.ipAddrs.Get([]byte(ip))
expire := r.ipAddrs.Get(ip)
if len(expire) != 0 {
exp := binary.BigEndian.Uint64(expire)
if exp > now {
Expand All @@ -55,7 +55,7 @@ func (r *RDNS) Begin(ip net.IP) {
expire = make([]byte, 8)
const ttl = 1 * 60 * 60
binary.BigEndian.PutUint64(expire, now+ttl)
_ = r.ipAddrs.Set([]byte(ip), expire)
_ = r.ipAddrs.Set(ip, expire)

if r.clients.Exists(ip, ClientSourceRDNS) {
return
Expand Down
2 changes: 1 addition & 1 deletion internal/home/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func WebCheckPortAvailable(port int) bool {
alreadyRunning = true
}
if !alreadyRunning {
err := util.CheckPortAvailable(config.BindHost.String(), port)
err := util.CheckPortAvailable(config.BindHost, port)
if err != nil {
return false
}
Expand Down
4 changes: 2 additions & 2 deletions internal/querylog/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ func answerToMap(a *dns.Msg) (answers []jobject) {
// try most common record types
switch v := k.(type) {
case *dns.A:
answer["value"] = v.A.String()
answer["value"] = v.A
case *dns.AAAA:
answer["value"] = v.AAAA.String()
answer["value"] = v.AAAA
case *dns.MX:
answer["value"] = fmt.Sprintf("%v %v", v.Preference, v.Mx)
case *dns.CNAME:
Expand Down
2 changes: 1 addition & 1 deletion internal/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Stats interface {
Update(e Entry)

// Get IP addresses of the clients with the most number of requests
GetTopClientsIP(limit uint) []string
GetTopClientsIP(limit uint) []net.IP

// WriteDiskConfig - write configuration
WriteDiskConfig(dc *DiskConfig)
Expand Down
2 changes: 1 addition & 1 deletion internal/stats/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestStats(t *testing.T) {
assert.EqualValues(t, 0.123456, d["avg_processing_time"].(float64))

topClients := s.GetTopClientsIP(2)
assert.Equal(t, "127.0.0.1", topClients[0])
assert.Equal(t, "127.0.0.1", topClients[0].String())

s.clear()
s.Close()
Expand Down
6 changes: 3 additions & 3 deletions internal/stats/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ func (s *statsCtx) getData() map[string]interface{} {
return d
}

func (s *statsCtx) GetTopClientsIP(maxCount uint) []string {
func (s *statsCtx) GetTopClientsIP(maxCount uint) []net.IP {
units, _ := s.loadUnits(s.conf.limit)
if units == nil {
return nil
Expand All @@ -669,9 +669,9 @@ func (s *statsCtx) GetTopClientsIP(maxCount uint) []string {
}
}
a := convertMapToArray(m, int(maxCount))
d := []string{}
d := []net.IP{}
for _, it := range a {
d = append(d, it.Name)
d = append(d, net.ParseIP(it.Name))
}
return d
}
8 changes: 4 additions & 4 deletions internal/util/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ func GetSubnet(ifaceName string) net.IPNet {
}

// CheckPortAvailable - check if TCP port is available
func CheckPortAvailable(host string, port int) error {
ln, err := net.Listen("tcp", net.JoinHostPort(host, strconv.Itoa(port)))
func CheckPortAvailable(host net.IP, port int) error {
ln, err := net.Listen("tcp", net.JoinHostPort(host.String(), strconv.Itoa(port)))
if err != nil {
return err
}
Expand All @@ -142,8 +142,8 @@ func CheckPortAvailable(host string, port int) error {
}

// CheckPacketPortAvailable - check if UDP port is available
func CheckPacketPortAvailable(host string, port int) error {
ln, err := net.ListenPacket("udp", net.JoinHostPort(host, strconv.Itoa(port)))
func CheckPacketPortAvailable(host net.IP, port int) error {
ln, err := net.ListenPacket("udp", net.JoinHostPort(host.String(), strconv.Itoa(port)))
if err != nil {
return err
}
Expand Down

0 comments on commit e99595a

Please sign in to comment.