如何从IP获取主机

时间:2018-06-14 22:10:10

标签: networking ip

我正在研究一个计算IP的十进制等值的函数,以便得到我使用这个公式的结果:

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

有没有人知道如何撤销这个过程,我的意思是,如何从十进制数中获取IP地址。

我正在寻找一些数学公式,我已经了解nslookup命令。

提前致谢。

2 个答案:

答案 0 :(得分:1)

到十进制:

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

到八位字节:

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)

使用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

使用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)
  }
}

用法:

$ ./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

答案 1 :(得分:1)

乘法和除法是浪费计算能力。请记住,您正在处理位模式:

o1.o2.o3.o4到数字:

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

数字到八位字节:

#include <iostream>
using namespace std;

// NOTE: 
// "long" or "signed long" is not showing ambiguous candidates
// but "unsigned long" does

void func(long st) {
    cout << "overload func\n";
}

void func(int* ptr) {
    cout << "integer pointer overload func\n";
}

int main() {
    func(NULL);
    return 0;
}