1// Copyright (c) 2017 Uber Technologies, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package utils
16
17import (
18	"encoding/binary"
19	"errors"
20	"net"
21	"strconv"
22	"strings"
23	"time"
24)
25
26var (
27	// ErrEmptyIP an error for empty ip strings
28	ErrEmptyIP = errors.New("empty string given for ip")
29
30	// ErrNotHostColonPort an error for invalid host port string
31	ErrNotHostColonPort = errors.New("expecting host:port")
32
33	// ErrNotFourOctets an error for the wrong number of octets after splitting a string
34	ErrNotFourOctets = errors.New("Wrong number of octets")
35)
36
37// ParseIPToUint32 converts a string ip (e.g. "x.y.z.w") to an uint32
38func ParseIPToUint32(ip string) (uint32, error) {
39	if ip == "" {
40		return 0, ErrEmptyIP
41	}
42
43	if ip == "localhost" {
44		return 127<<24 | 1, nil
45	}
46
47	octets := strings.Split(ip, ".")
48	if len(octets) != 4 {
49		return 0, ErrNotFourOctets
50	}
51
52	var intIP uint32
53	for i := 0; i < 4; i++ {
54		octet, err := strconv.Atoi(octets[i])
55		if err != nil {
56			return 0, err
57		}
58		intIP = (intIP << 8) | uint32(octet)
59	}
60
61	return intIP, nil
62}
63
64// ParsePort converts port number from string to uin16
65func ParsePort(portString string) (uint16, error) {
66	port, err := strconv.ParseUint(portString, 10, 16)
67	return uint16(port), err
68}
69
70// PackIPAsUint32 packs an IPv4 as uint32
71func PackIPAsUint32(ip net.IP) uint32 {
72	if ipv4 := ip.To4(); ipv4 != nil {
73		return binary.BigEndian.Uint32(ipv4)
74	}
75	return 0
76}
77
78// TimeToMicrosecondsSinceEpochInt64 converts Go time.Time to a long
79// representing time since epoch in microseconds, which is used expected
80// in the Jaeger spans encoded as Thrift.
81func TimeToMicrosecondsSinceEpochInt64(t time.Time) int64 {
82	// ^^^ Passing time.Time by value is faster than passing a pointer!
83	// BenchmarkTimeByValue-8	2000000000	         1.37 ns/op
84	// BenchmarkTimeByPtr-8  	2000000000	         1.98 ns/op
85
86	return t.UnixNano() / 1000
87}
88