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
|
package main
import (
"hash"
"log"
"strings"
"crypto/md5"
"math/rand"
)
const (
netPort = "5303"
)
// The format is "Host:Port", "Host", or ":Port" as well as an empty string.
// and it returns a string which can be passed to Dial or Listen
func parseNetStr(isIPv6 bool, sockStr string) string {
if isIPv6 {
// IPv6 address. Append port if not specified
if strings.Contains(sockStr, "]") {
if sockStr[len(sockStr)-1:] == "]" {
// IPv6 address bu no port
sockStr = sockStr + ":" + netPort
}
} else {
if !strings.Contains(sockStr, ":") {
// empty host portion or hostname given
// but no port. Append port
sockStr = sockStr + ":" + netPort
}
}
} else {
// IPv4 address. Append port if not specified
if !strings.Contains(sockStr, ":") {
sockStr = sockStr + ":" + netPort
}
}
return sockStr
}
func md5Hash(h hash.Hash) [16]byte {
if h.Size() != md5.Size {
log.Fatalln("Hash is not an md5!")
}
s := h.Sum(nil) // Gets a slice
var r [16]byte
for i, b := range s {
r[i] = b
}
return r
}
func randBuf(n int) []byte {
b := make([]byte, n)
for i := range b {
b[i] = byte(rand.Intn(255))
}
return b
}
func prError(format string, args ...interface{}) {
if exitOnError {
log.Fatalf(format, args...)
} else {
log.Printf(format, args...)
}
}
func prInfo(format string, args ...interface{}) {
if verbose > 0 {
log.Printf(format, args...)
}
}
func prDebug(format string, args ...interface{}) {
if verbose > 1 {
log.Printf(format, args...)
}
}
|