How to get host from IP

I'm working on a function that calculates the decimal equivalent of an IP, in order to get the result I'm using this formula:

decimal = ((octet1 * 16777216) + (octet2 * 65536) + (octet13 * 256) + (octet4))

Does anybody know how to reverse this process, I mean, how can I get the IP address from the decimal number.

I'm looking for some mathematical formula, I already know about nslookup command.

Thanks in advance.

Upvotes: 0

Views: 75

Answers (2)

Zac67
Zac67

Reputation: 2912

Multiplying and dividing is a waste of computational power. Remember that you're dealing with bit patterns:

o1.o2.o3.o4 to numeric:

n = o1<<24 | o2<<16 | o3<<8 | o4

numeric to octets:

o1 = n>>24
o2 = (n>>16) & 255
o3 = (n>>8) & 255
o4 = n & 255

Upvotes: 1

chuckx
chuckx

Reputation: 6877

To decimal:

decimal = (octet1 * 256^3) + (octet2 * 256^2) + (octet3 * 256^1) + octet4

To octets:

octet1 = Floor(decimal / 256^3)
octet2 = Floor((decimal - (octet1 * 256^3)) / 256^2)
octet3 = Floor((decimal - (octet1 * 256^3) - (octet2 * 256^2)) / 256^1)
octet4 = decimal - (octet1 * 256^3) - (octet2 * 256^2) - (octet3 * 256^1)

Example using 1.2.3.4

decimal = (1 * 256^3) + (2 * 256^2) + (3 * 256^1) + 4
decimal = 16909060

Calculation

octet1 = Floor(16909060 / 256^3)
octet1 = 1

Calculation

octet2 = Floor((16909060 - (1 * 256^3)) / 256^2)
octet2 = 2

Calculation

octet3 = Floor((16909060 - (1 * 256^3) - (2 * 256^2)) / 256^1)
octet3 = 3

Calculation

octet4 = 16909060 - (1 * 256^3) - (2 * 256^2) - (3 * 256^1)
octet4 = 4

Calculation

Implementation Using Go

package main

import (
  "errors"
  "flag"
  "fmt"
  "math"
  "os"
  "strconv"
  "strings"
)

var (
  ip = flag.String("i", "", "IP address in dotted notation")
  dec = flag.Int("d", 0, "IP address in decimal notation")
)

func ipv4ToDec(ip string) (int, error) {
  var result int
  octets := strings.Split(ip, ".")
  if len(octets) != 4 {
    return 0, errors.New("IP should consist of 4 '.' seperated numbers")
  }
  for i := 0; i < 4; i++ {
    v, err := strconv.Atoi(octets[3-i])
    if err != nil {
      return 0, errors.New("unable to convert octet to number")
    }
    if v < 0 || v > 255 {
      return 0, errors.New("octet should be between 0 and 255")
    }
    result += v * int(math.Pow(256, float64(i)))
  }
  return result, nil
}

func decToIpv4(dec int) (string, error) {
  var octets []string
  for i := 0; i < 4; i++ {
    octet := dec / int(math.Pow(256, float64(3-i)))
    if octet > 255 {
      return "", errors.New("octet larger than 255")
    }
    dec -= octet * int(math.Pow(256, float64(3-i)))
    octets = append(octets, strconv.Itoa(octet))
  }
  return strings.Join(octets, "."), nil
}

func main() {
  flag.Parse()

  if ((*ip != "" && *dec != 0) || (*ip == "" && *dec == 0)) {
    fmt.Println("Use either -i or -d.")
    os.Exit(1)
  }

  if *ip != "" {
    result, err := ipv4ToDec(*ip)
    if err != nil {
      fmt.Println("Conversion failed: ", err)
      os.Exit(1)
    }
    fmt.Println(result)
  }

  if *dec != 0 {
    result, err := decToIpv4(*dec)
    if err != nil {
      fmt.Println("Conversion failed: ", err)
      os.Exit(1)
    }
    fmt.Println(result)
  }
}

Usage:

$ ./ip-conv -i 1.2.3.4
16909060

$ ./ip-conv -d 16909060
1.2.3.4

$ ./ip-conv -i 192.168.0.1
3232235521

# Using the output of IP->decimal as input to decimal->IP
$ ./ip-conv -d $(./ip-conv -i 192.168.0.1)
192.168.0.1

Upvotes: 1

Related Questions