File: iputils.go

package info (click to toggle)
rootlesskit 2.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: sh: 433; makefile: 25
file content (23 lines) | stat: -rw-r--r-- 496 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package iputils

import (
	"encoding/binary"
	"fmt"
	"math"
	"net"
)

func AddIPInt(ip net.IP, i int) (net.IP, error) {
	ip = ip.To4()
	if ip == nil {
		return nil, fmt.Errorf("expected IPv4 address, got %s", ip.String())
	}
	ui32 := binary.BigEndian.Uint32(ip)
	resInt64 := int64(ui32) + int64(i)
	if resInt64 > int64(math.MaxUint32) {
		return nil, fmt.Errorf("%s + %d overflows", ip.String(), i)
	}
	res := make(net.IP, 4)
	binary.BigEndian.PutUint32(res, uint32(resInt64))
	return res, nil
}