summaryrefslogtreecommitdiff
path: root/utils/network_windows.go
diff options
context:
space:
mode:
Diffstat (limited to 'utils/network_windows.go')
-rwxr-xr-xutils/network_windows.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/utils/network_windows.go b/utils/network_windows.go
new file mode 100755
index 00000000..88e0fdb6
--- /dev/null
+++ b/utils/network_windows.go
@@ -0,0 +1,64 @@
+//go:build windows
+// +build windows
+
+package utils
+
+import (
+ "fmt"
+ "net"
+ "os"
+ "strings"
+ "syscall"
+)
+
+// CreateListener crates socket listener based on DSN definition.
+func CreateListener(address string) (net.Listener, error) {
+ dsn := strings.Split(address, "://")
+
+ switch len(dsn) {
+ case 1:
+ // assume, that there is no prefix here [127.0.0.1:8000]
+ return createTCPListener(dsn[0])
+ case 2:
+ // we got two part here, first part is the transport, second - address
+ // [tcp://127.0.0.1:8000] OR [unix:///path/to/unix.socket] OR [error://path]
+ // where error is wrong transport name
+ switch dsn[0] {
+ case "unix":
+ // check of file exist. If exist, unlink
+ if fileExists(dsn[1]) {
+ err := syscall.Unlink(dsn[1])
+ if err != nil {
+ return nil, fmt.Errorf("error during the unlink syscall: error %v", err)
+ }
+ }
+ return net.Listen(dsn[0], dsn[1])
+ case "tcp":
+ return createTCPListener(dsn[1])
+ // not an tcp or unix
+ default:
+ return nil, fmt.Errorf("invalid Protocol ([tcp://]:6001, unix://file.sock), address: %s", address)
+ }
+ // wrong number of split parts
+ default:
+ return nil, fmt.Errorf("wrong number of parsed protocol parts, address: %s", address)
+ }
+}
+
+func createTCPListener(addr string) (net.Listener, error) {
+ listener, err := net.Listen("tcp", addr)
+ if err != nil {
+ return nil, err
+ }
+ return listener, nil
+}
+
+// fileExists checks if a file exists and is not a directory before we
+// try using it to prevent further errors.
+func fileExists(filename string) bool {
+ info, err := os.Stat(filename)
+ if os.IsNotExist(err) {
+ return false
+ }
+ return !info.IsDir()
+}