-
-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
featuer: device add support unixsocket use fd
- Loading branch information
huangxiangyu
committed
Aug 18, 2023
1 parent
f588b4c
commit 9cfd77e
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package sock2fd | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
"strings" | ||
) | ||
|
||
func Sock2Fd(s string) (int, error) { | ||
|
||
strSplit := strings.Split(s, "://") | ||
|
||
if strSplit[0] == "unix" || strSplit[0] == "Unix" || strSplit[0] == "UNIX" { | ||
return Unix2Fd(strSplit[1]) | ||
} | ||
return 0, errors.New("unsupported socket") | ||
} | ||
|
||
/* | ||
for example: | ||
device: unix:///tmp/unix_socket | ||
*/ | ||
|
||
func Unix2Fd(path string) (int, error) { | ||
conn, err := net.Dial("unix", path) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
unixConn, ok := conn.(*net.UnixConn) | ||
if !ok { | ||
return 0, errors.New("unexpected") | ||
} | ||
|
||
fd, err := unixConn.File() | ||
if err != nil { | ||
return 0, errors.New("unexpected") | ||
} | ||
|
||
return int(fd.Fd()), nil | ||
} |