-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.go
More file actions
118 lines (101 loc) · 2.08 KB
/
utils.go
File metadata and controls
118 lines (101 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright 2016 Xiaomi Corporation. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*
* Authors: Yu Bo <yubo@xiaomi.com>
*/
package govs
import (
"encoding/binary"
"fmt"
"net"
"strconv"
"strings"
"unsafe"
)
func max_len(is ...[]int64) (l int) {
for _, v := range is {
if l < len(v) {
l = len(v)
}
}
return l
}
func ipToU32(p net.IP) uint32 {
// If IPv4, use dotted notation.
if p4 := p.To4(); len(p4) == net.IPv4len {
return uint32(p4[0])<<24 | uint32(p4[1])<<16 |
uint32(p4[2])<<8 | uint32(p4[3])
}
return 0
}
func be32_to_addr(b Be32) string {
return u32_to_addr(Ntohl(b))
}
func u32_to_addr(u uint32) string {
return fmt.Sprintf("%d.%d.%d.%d",
u&0xff000000>>24,
u&0xff0000>>16,
u&0xff00>>8,
u&0xff)
}
func Ntohl(i Be32) uint32 {
return binary.BigEndian.Uint32((*(*[4]byte)(unsafe.Pointer(&i)))[:])
}
func Htonl(i uint32) Be32 {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, i)
return *(*Be32)(unsafe.Pointer(&b[0]))
}
func Ntohs(i Be16) uint16 {
return binary.BigEndian.Uint16((*(*[2]byte)(unsafe.Pointer(&i)))[:])
}
func Htons(i uint16) Be16 {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, i)
return *(*Be16)(unsafe.Pointer(&b[0]))
}
func get_protocol_name(p uint8) string {
if p == IPPROTO_TCP {
return "tcp"
} else if p == IPPROTO_UDP {
return "udp"
}
return "unknown"
}
func Parse_service(o *CallOptions) error {
var addr string
if o.Opt.UDP != "" {
addr = o.Opt.UDP
o.Opt.Protocol = IPPROTO_UDP
}
if o.Opt.TCP != "" {
addr = o.Opt.TCP
o.Opt.Protocol = IPPROTO_TCP
}
if err := o.Opt.Addr.Set(addr); err != nil {
return err
}
return nil
}
func bool2u8(b bool) uint8 {
if b {
return 1
}
return 0
}
func DecodeAddr(s string) (string, error) {
words := strings.Split(s, ":")
ip, err := strconv.ParseInt(words[0], 16, 0)
if err != nil {
return "", err
}
addr := u32_to_addr(uint32(ip))
port, err := strconv.ParseInt(words[1], 16, 0)
if err != nil {
return addr, err
}
addr += fmt.Sprintf(":%d", port)
return addr, nil
}