本文整理汇总了Golang中net.DialUnix函数的典型用法代码示例。如果您正苦于以下问题:Golang DialUnix函数的具体用法?Golang DialUnix怎么用?Golang DialUnix使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DialUnix函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: newUnixTransport
func newUnixTransport(keys string) (transport, error) {
var err error
t := new(unixTransport)
abstract := getKey(keys, "abstract")
path := getKey(keys, "path")
switch {
case abstract == "" && path == "":
return nil, errors.New("dbus: invalid address (neither path nor abstract set)")
case abstract != "" && path == "":
t.UnixConn, err = net.DialUnix("unix", nil, &net.UnixAddr{Name: "@" + abstract, Net: "unix"})
if err != nil {
return nil, err
}
return t, nil
case abstract == "" && path != "":
t.UnixConn, err = net.DialUnix("unix", nil, &net.UnixAddr{Name: path, Net: "unix"})
if err != nil {
return nil, err
}
return t, nil
default:
return nil, errors.New("dbus: invalid address (both path and abstract set)")
}
}
示例2: DialServer
// DialServer dials a Unix Domain Socket where a server is listening and
// returns a client to the server.
func DialServer(uid string) (*Client, error) {
// Have the client start listening for responses from the server.
clientUID := uuid.New()
clientPath := path.Join(os.TempDir(), clientUID)
outConn, err := net.ListenUnixgram(udsType, &net.UnixAddr{clientPath, udsType})
if err != nil {
return nil, err
}
// Dial the server.
log.Infof("client: dialing the server")
setupConn, err := net.DialUnix(
udsType,
nil,
&net.UnixAddr{path.Join(os.TempDir(), uid), udsType},
)
if err != nil {
return nil, err
}
log.Infof("client: sending uid to server")
if err := SetupEncode(clientUID, setupConn); err != nil {
return nil, err
}
setupConn.Close()
// Get the socket the server is going to listen on.
out := bufio.NewReader(outConn)
inUUID, err := SetupDecode(out)
if err != nil {
return nil, err
}
log.Infof("client: received server uid for conn")
// Dial the server.
in, err := net.DialUnix(
udsType,
nil,
&net.UnixAddr{path.Join(os.TempDir(), inUUID), udsType},
)
if err != nil {
return nil, err
}
log.Infof("client: dialed server")
c := &Client{
inConn: in,
outConn: out,
reqCh: make(chan request, 50),
responses: make(map[uint64]chan response),
}
go c.send()
go c.receive()
return c, nil
}
示例3: SdNotify
// SdNotify sends a message to the init daemon. It is common to ignore the error.
// If `unsetEnvironment` is true, the environment variable `NOTIFY_SOCKET`
// will be unconditionally unset.
//
// It returns one of the following:
// (false, nil) - notification not supported (i.e. NOTIFY_SOCKET is unset)
// (false, err) - notification supported, but failure happened (e.g. error connecting to NOTIFY_SOCKET or while sending data)
// (true, nil) - notification supported, data has been sent
func SdNotify(unsetEnvironment bool, state string) (sent bool, err error) {
socketAddr := &net.UnixAddr{
Name: os.Getenv("NOTIFY_SOCKET"),
Net: "unixgram",
}
// NOTIFY_SOCKET not set
if socketAddr.Name == "" {
return false, nil
}
if unsetEnvironment {
err = os.Unsetenv("NOTIFY_SOCKET")
}
if err != nil {
return false, err
}
conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
// Error connecting to NOTIFY_SOCKET
if err != nil {
return false, err
}
defer conn.Close()
_, err = conn.Write([]byte(state))
// Error sending the message
if err != nil {
return false, err
}
return true, nil
}
示例4: ConnectDisplay
func ConnectDisplay(addr string) (ret *Display, err error) {
runtime_dir := os.Getenv("XDG_RUNTIME_DIR")
if runtime_dir == "" {
return nil, errors.New("XDG_RUNTIME_DIR not set in the environment.")
}
if addr == "" {
addr = os.Getenv("WAYLAND_DISPLAY")
}
if addr == "" {
addr = "wayland-0"
}
addr = runtime_dir + "/" + addr
ctx := &Connection{}
ctx.objects = make(map[ProxyId]Proxy)
ctx.currentId = 0
ctx.dispatchRequest = make(chan bool)
ctx.exit = make(chan bool)
ctx.conn, err = net.DialUnix("unix", nil, &net.UnixAddr{addr, "unix"})
if err != nil {
return nil, err
}
ret = NewDisplay(ctx)
// dispatch events in separate gorutine
go ctx.run()
return ret, nil
}
示例5: Notify
// Notify sends a message to the init daemon. It is common to ignore the error.
func Notify(state string) error {
socketAddr := &net.UnixAddr{
Name: os.Getenv("NOTIFY_SOCKET"),
Net: "unixgram",
}
if socketAddr.Name == "" {
return ErrNotifyNoSocket
}
switch socketAddr.Name[0] {
case '@', '/':
default:
return ErrNotifyNoSocket
}
conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
if err != nil {
return err
}
defer logx.LogReturnedErr(conn.Close, nil, "failed to close connection")
_, err = conn.Write([]byte(state))
if err != nil {
return err
}
return nil
}
示例6: BusConnect
// returns a new BusConnection.
func BusConnect(path string, bh busHandler, rh readHandler) (conn *BusConnection, err error) {
// check if file exists. if not, bail.
_, err = os.Lstat(path)
if err != nil {
return
}
// resolve the address.
addr, err := net.ResolveUnixAddr("unix", path)
if err != nil {
return
}
// connect.
unixConn, err := net.DialUnix("unix", nil, addr)
if err != nil {
return
}
// create the connection.
conn = &BusConnection{
path: path,
socket: unixConn,
incoming: bufio.NewReader(unixConn),
outgoing: bufio.NewWriter(unixConn),
busHandler: bh,
readHandler: rh,
Connected: true,
}
return
}
示例7: newProxy
func newProxy(src, dst string) (*unixProxy, error) {
os.Remove(src)
// start listening
local, err := net.ListenUnixgram("unixgram", &net.UnixAddr{
Name: src,
Net: "unixgram",
})
if err != nil {
return nil, err
}
remote, err := net.DialUnix("unixgram", nil, &net.UnixAddr{
Name: dst,
Net: "unixgram",
})
if err != nil {
return nil, err
}
return &unixProxy{
local: local,
remote: remote,
}, nil
}
示例8: TestListenUnix
func (_ *ListenerSuite) TestListenUnix(c *C) {
addr, _ := net.ResolveUnixAddr("unix", "/tmp/go_test_socket")
cfg := &conf.ListenerConf{
Name: "local",
Ports: []*conf.PortConf{
&conf.PortConf{
Type: "unix",
Addr: addr,
},
},
}
ll := listener.NewListener(cfg)
ll.SetHandler(echoHandler)
ll.Start()
time.Sleep(50 * time.Millisecond)
cc, err := net.DialUnix("unix", nil, addr)
c.Assert(err, IsNil)
testEchoConnection(c, cc)
cc.Close()
ll.Stop()
}
示例9: NewUnixgramServer
func NewUnixgramServer(t lotf.Tail, raddr *net.UnixAddr) (*DgramServer, error) {
conn, err := net.DialUnix("unixgram", nil, raddr)
if err != nil {
return nil, err
}
return &DgramServer{t, io.WriteCloser(conn)}, nil
}
示例10: Connect
func Connect(path string) (conn *Connection, err error) {
// check if file exists. if not, bail.
_, err = os.Lstat(path)
if err != nil {
return
}
// resolve the address
addr, err := net.ResolveUnixAddr("unix", path)
if err != nil {
return
}
// connect
unixConn, err := net.DialUnix("unix", nil, addr)
if err != nil {
return
}
conn = &Connection{
path: path,
socket: unixConn,
incoming: bufio.NewReader(unixConn),
}
return
}
示例11: HijackSession
// Attempt to hijack session previously running bot
func (irc *IrcCon) HijackSession() bool {
unaddr, err := net.ResolveUnixAddr("unix", irc.unixastr)
if err != nil {
irc.Log(LWarning, "could not resolve unix socket")
return false
}
con, err := net.DialUnix("unix", nil, unaddr)
if err != nil {
fmt.Println("Couldnt restablish connection, no prior bot.")
fmt.Println(err)
return false
}
ncon, err := sendfd.RecvFD(con)
if err != nil {
panic(err)
}
netcon, err := net.FileConn(ncon)
if err != nil {
panic(err)
}
irc.reconnect = true
irc.con = netcon
return true
}
示例12: OpenTPM
// OpenTPM opens a channel to the TPM at the given path. If the file is a
// device, then it treats it like a normal TPM device, and if the file is a
// Unix domain socket, then it opens a connection to the socket.
func OpenTPM(path string) (io.ReadWriteCloser, error) {
// If it's a regular file, then open it
var rwc io.ReadWriteCloser
fi, err := os.Stat(path)
if err != nil {
return nil, err
}
if fi.Mode()&os.ModeDevice != 0 {
var f *os.File
f, err = os.OpenFile(path, os.O_RDWR, 0600)
if err != nil {
return nil, err
}
rwc = io.ReadWriteCloser(f)
} else if fi.Mode()&os.ModeSocket != 0 {
uc, err := net.DialUnix("unix", nil, &net.UnixAddr{Name: path, Net: "unix"})
if err != nil {
return nil, err
}
rwc = io.ReadWriteCloser(uc)
} else {
return nil, fmt.Errorf("unsupported TPM file mode %s", fi.Mode().String())
}
return rwc, nil
}
示例13: setupSockets
// setupSockets sets up the sockets used to communicate between the client
// and the server. If successful it spins up another goroutine to handle
// all communication from that client.
func (s *Server) setupSockets(remoteID string) {
log.Infof("server: dialing client socket")
out, err := net.DialUnix(
udsType,
nil,
&net.UnixAddr{path.Join(os.TempDir(), remoteID), udsType},
)
if err != nil {
log.Infof("problem dialing client's socket: %s", err)
return
}
log.Infof("server: preparing to listen on new socket")
uid := uuid.New()
p := path.Join(os.TempDir(), uid)
in, err := net.ListenUnixgram(udsType, &net.UnixAddr{p, udsType})
if err != nil {
out.Close()
log.Infof("could not listen on domain socket %q: %s", p, err)
return
}
log.Infof("server: sending a uid to the client")
if err := SetupEncode(uid, out); err != nil {
out.Close()
log.Infof("problem encoding UUIDv4 for setup: %s", err)
return
}
go s.serveConn(in, out)
}
示例14: NotifySend
// Send sends a message to the init daemon. It is common to ignore the error.
//
// This function differs from that in github.com/coreos/go-systemd/daemon in
// that that code closes the socket after each call, and so won't work in a
// chroot. This function keeps the socket open; so long as it is called at
// least once before chrooting, it can continue to be used.
//
// May return ErrNoSocket.
func NotifySend(state string) error {
sdNotifyMutex.Lock()
defer sdNotifyMutex.Unlock()
if !sdNotifyInited {
sdNotifyInited = true
socketAddr := &net.UnixAddr{
Name: os.Getenv("NOTIFY_SOCKET"),
Net: "unixgram",
}
if socketAddr.Name == "" {
return ErrNoSocket
}
conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
if err != nil {
return err
}
sdNotifySocket = conn
}
if sdNotifySocket == nil {
return ErrNoSocket
}
_, err := sdNotifySocket.Write([]byte(state))
return err
}
示例15: connectViaUnix
func connectViaUnix(c *Client, remote *RemoteConfig) error {
c.BaseURL = "http://unix.socket"
c.BaseWSURL = "ws://unix.socket"
c.Transport = "unix"
uDial := func(network, addr string) (net.Conn, error) {
// The arguments 'network' and 'addr' are ignored because
// they are the wrong information.
// addr is generated from BaseURL which becomes
// 'unix.socket:80' which is certainly not what we want.
// handle:
// unix:///path/to/socket
// unix:/path/to/socket
// unix:path/to/socket
path := strings.TrimPrefix(remote.Addr, "unix:")
if strings.HasPrefix(path, "///") {
// translate unix:///path/to, to just "/path/to"
path = path[2:]
}
raddr, err := net.ResolveUnixAddr("unix", path)
if err != nil {
return nil, err
}
return net.DialUnix("unix", nil, raddr)
}
c.Http.Transport = &http.Transport{Dial: uDial}
c.websocketDialer.NetDial = uDial
c.Remote = remote
st, err := c.ServerStatus()
if err != nil {
return err
}
c.Certificate = st.Environment.Certificate
return nil
}