本文整理汇总了Golang中syscall.ParseSocketControlMessage函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseSocketControlMessage函数的具体用法?Golang ParseSocketControlMessage怎么用?Golang ParseSocketControlMessage使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ParseSocketControlMessage函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: RemoteRecvCredentials
func RemoteRecvCredentials(conn *net.UnixConn) (uint32, uint32, error) {
err := syscall.SetsockoptInt(sysfd(conn), syscall.SOL_SOCKET, syscall.SO_PASSCRED, 1)
if err != nil {
return 0, 0, err
}
oob := make([]byte, len(syscall.UnixCredentials(&syscall.Ucred{})))
_, _, _, _, err = conn.ReadMsgUnix(nil, oob)
if err != nil {
return 0, 0, err
}
scm, err := syscall.ParseSocketControlMessage(oob)
if err != nil {
return 0, 0, err
}
ucred, err := syscall.ParseUnixCredentials(&scm[0])
if err != nil {
return 0, 0, err
}
return ucred.Uid, ucred.Gid, nil
}
示例2: readFrom
func (fd *Fd) readFrom(c *net.UnixConn) error {
var b []byte
oob := make([]byte, 16)
_, oobn, _, _, err := c.ReadMsgUnix(b, oob)
if err != nil {
return err
}
if oobn == 0 {
return errors.New("error reading oob")
}
oob = oob[:oobn]
scms, err := syscall.ParseSocketControlMessage(oob)
if err != nil {
return err
}
if len(scms) != 1 {
return fmt.Errorf("expected 1 SocketControlMessage, got %d", len(scms))
}
scm := scms[0]
fds, err := syscall.ParseUnixRights(&scm)
if err != nil {
return nil
}
if len(fds) != 1 {
return fmt.Errorf("expected 1 fd, got %d", len(fds))
}
*fd = Fd(fds[0])
return nil
}
示例3: extractFds
func extractFds(oob []byte) (fds []int) {
// Grab forklock to make sure no forks accidentally inherit the new
// fds before they are made CLOEXEC
// There is a slight race condition between ReadMsgUnix returns and
// when we grap the lock, so this is not perfect. Unfortunately
// There is no way to pass MSG_CMSG_CLOEXEC to recvmsg() nor any
// way to implement non-blocking i/o in go, so this is hard to fix.
syscall.ForkLock.Lock()
defer syscall.ForkLock.Unlock()
scms, err := syscall.ParseSocketControlMessage(oob)
if err != nil {
return
}
for _, scm := range scms {
gotFds, err := syscall.ParseUnixRights(&scm)
if err != nil {
continue
}
fds = append(fds, gotFds...)
for _, fd := range fds {
syscall.CloseOnExec(fd)
}
}
return
}
示例4: parseControlMessage
func parseControlMessage(b []byte) (*ControlMessage, error) {
cmsgs, err := syscall.ParseSocketControlMessage(b)
if err != nil {
return nil, os.NewSyscallError("parse socket control message", err)
}
if len(b) == 0 {
return nil, nil
}
cm := &ControlMessage{}
for _, m := range cmsgs {
if m.Header.Level != ianaProtocolIP {
continue
}
switch m.Header.Type {
case syscall.IP_RECVTTL:
cm.TTL = int(*(*byte)(unsafe.Pointer(&m.Data[:1][0])))
case syscall.IP_RECVDSTADDR:
v := m.Data[:4]
cm.Dst = net.IPv4(v[0], v[1], v[2], v[3])
case syscall.IP_RECVIF:
sadl := (*syscall.SockaddrDatalink)(unsafe.Pointer(&m.Data[0]))
cm.IfIndex = int(sadl.Index)
}
}
return cm, nil
}
示例5: Read
func (s *OOBUnixConn) Read(p []byte) (n int, err error) {
var oob [OOBMaxLength]byte
n, oobn, _, _, err := s.ReadMsgUnix(p, oob[:])
if err == nil && n > 0 && oobn > 0 {
scm, err := syscall.ParseSocketControlMessage(oob[0:oobn])
if err != nil {
return n, err
}
s.m.Lock()
for _, m := range scm {
if m.Header.Level != syscall.SOL_SOCKET {
continue
}
switch m.Header.Type {
case syscall.SCM_RIGHTS:
if fds, err := syscall.ParseUnixRights(&m); err == nil {
for _, fd := range fds {
// Note: We wrap the raw FDs inside an os.File just
// once, early, to prevent double-free or leaking FDs.
f := NewFile(fd)
s.recvFiles = append(s.recvFiles, f)
}
}
}
}
s.m.Unlock()
}
return n, err
}
示例6: parseControlMessage
func parseControlMessage(b []byte) (*ControlMessage, error) {
if len(b) == 0 {
return nil, nil
}
cmsgs, err := syscall.ParseSocketControlMessage(b)
if err != nil {
return nil, os.NewSyscallError("parse socket control message", err)
}
cm := &ControlMessage{}
for _, m := range cmsgs {
if m.Header.Level != iana.ProtocolIP {
continue
}
switch int(m.Header.Type) {
case ctlOpts[ctlTTL].name:
ctlOpts[ctlTTL].parse(cm, m.Data[:])
case ctlOpts[ctlDst].name:
ctlOpts[ctlDst].parse(cm, m.Data[:])
case ctlOpts[ctlInterface].name:
ctlOpts[ctlInterface].parse(cm, m.Data[:])
case ctlOpts[ctlPacketInfo].name:
ctlOpts[ctlPacketInfo].parse(cm, m.Data[:])
}
}
return cm, nil
}
示例7: Read
func (r *FDReader) Read(b []byte) (int, error) {
oob := make([]byte, 32)
n, oobn, _, _, err := r.conn.ReadMsgUnix(b, oob)
if err != nil {
if n < 0 {
n = 0
}
return n, err
}
if oobn > 0 {
messages, err := syscall.ParseSocketControlMessage(oob[:oobn])
if err != nil {
return n, err
}
for _, m := range messages {
fds, err := syscall.ParseUnixRights(&m)
if err != nil {
return n, err
}
// Set the CLOEXEC flag on the FDs so they won't be leaked into future forks
for _, fd := range fds {
if _, _, errno := syscall.Syscall(syscall.SYS_FCNTL, uintptr(fd), syscall.F_SETFD, syscall.FD_CLOEXEC); errno != 0 {
return n, errno
}
r.FDs[r.fdCount] = fd
r.fdCount++
}
}
}
return n, nil
}
示例8: parseControlMessage
func parseControlMessage(b []byte) (*ControlMessage, error) {
if len(b) == 0 {
return nil, nil
}
cmsgs, err := syscall.ParseSocketControlMessage(b)
if err != nil {
return nil, os.NewSyscallError("parse socket control message", err)
}
cm := &ControlMessage{}
for _, m := range cmsgs {
if m.Header.Level != ianaProtocolIPv6 {
continue
}
switch m.Header.Type {
case syscall.IPV6_TCLASS:
cm.TrafficClass = int(*(*byte)(unsafe.Pointer(&m.Data[:1][0])))
case syscall.IPV6_HOPLIMIT:
cm.HopLimit = int(*(*byte)(unsafe.Pointer(&m.Data[:1][0])))
case syscall.IPV6_PKTINFO:
pi := (*syscall.Inet6Pktinfo)(unsafe.Pointer(&m.Data[0]))
cm.Dst = pi.Addr[:]
cm.IfIndex = int(pi.Ifindex)
case syscall.IPV6_PATHMTU:
mi := (*syscall.IPv6MTUInfo)(unsafe.Pointer(&m.Data[0]))
cm.Dst = mi.Addr.Addr[:]
cm.IfIndex = int(mi.Addr.Scope_id)
cm.MTU = int(mi.Mtu)
}
}
return cm, nil
}
示例9: mount
func mount(dir string, ready chan<- struct{}, errp *error) (fusefd *os.File, err error) {
// linux mount is never delayed
close(ready)
fds, err := syscall.Socketpair(syscall.AF_FILE, syscall.SOCK_STREAM, 0)
if err != nil {
return nil, fmt.Errorf("socketpair error: %v", err)
}
defer syscall.Close(fds[0])
defer syscall.Close(fds[1])
cmd := exec.Command("fusermount", "--", dir)
cmd.Env = append(os.Environ(), "_FUSE_COMMFD=3")
writeFile := os.NewFile(uintptr(fds[0]), "fusermount-child-writes")
defer writeFile.Close()
cmd.ExtraFiles = []*os.File{writeFile}
out, err := cmd.CombinedOutput()
if len(out) > 0 || err != nil {
return nil, fmt.Errorf("fusermount: %q, %v", out, err)
}
readFile := os.NewFile(uintptr(fds[1]), "fusermount-parent-reads")
defer readFile.Close()
c, err := net.FileConn(readFile)
if err != nil {
return nil, fmt.Errorf("FileConn from fusermount socket: %v", err)
}
defer c.Close()
uc, ok := c.(*net.UnixConn)
if !ok {
return nil, fmt.Errorf("unexpected FileConn type; expected UnixConn, got %T", c)
}
buf := make([]byte, 32) // expect 1 byte
oob := make([]byte, 32) // expect 24 bytes
_, oobn, _, _, err := uc.ReadMsgUnix(buf, oob)
scms, err := syscall.ParseSocketControlMessage(oob[:oobn])
if err != nil {
return nil, fmt.Errorf("ParseSocketControlMessage: %v", err)
}
if len(scms) != 1 {
return nil, fmt.Errorf("expected 1 SocketControlMessage; got scms = %#v", scms)
}
scm := scms[0]
gotFds, err := syscall.ParseUnixRights(&scm)
if err != nil {
return nil, fmt.Errorf("syscall.ParseUnixRights: %v", err)
}
if len(gotFds) != 1 {
return nil, fmt.Errorf("wanted 1 fd; got %#v", gotFds)
}
f := os.NewFile(uintptr(gotFds[0]), "/dev/fuse")
return f, nil
}
示例10: readRequest
func readRequest(c *net.UnixConn) (*Request, error) {
var l uint32
err := binary.Read(c, binary.BigEndian, &l)
length := int(l)
if err != nil {
return nil, err
}
payload := make([]byte, length)
n, err := c.Read(payload)
if err != nil {
return nil, err
} else if n != length {
return nil, fmt.Errorf("Payload was %d bytes rather than reported size of %d", n, length)
}
req := &Request{}
err = json.Unmarshal(payload, req)
if err != nil {
return nil, err
}
if !req.HasFds {
return req, nil
}
payload = make([]byte, 1)
// TODO: does this buffer need to be configurable?
oob := make([]byte, 8192)
n, oobn, _, _, err := c.ReadMsgUnix(payload, oob)
if err != nil && err != io.EOF {
return nil, err
}
if n != 1 {
return nil, fmt.Errorf("Error reading OOB filedescriptors")
}
oob = oob[0:oobn]
scm, err := syscall.ParseSocketControlMessage(oob)
if err != nil {
return nil, fmt.Errorf("Error parsing socket control message: %v", err)
}
var fds []int
for i := 0; i < len(scm); i++ {
tfds, err := syscall.ParseUnixRights(&scm[i])
if err == syscall.EINVAL {
continue // Wasn't a UnixRights Control Message
} else if err != nil {
return nil, fmt.Errorf("Error parsing unix rights: %v", err)
}
fds = append(fds, tfds...)
}
if len(fds) == 0 {
return nil, fmt.Errorf("Failed to receive any FDs on a request with HasFds == true")
}
req.ReceivedFds = fds
return req, nil
}
示例11: RetrieveOriginalDest
func RetrieveOriginalDest(oob []byte) v2net.Destination {
msgs, err := syscall.ParseSocketControlMessage(oob)
if err != nil {
return nil
}
for _, msg := range msgs {
if msg.Header.Level == syscall.SOL_IP && msg.Header.Type == syscall.IP_ORIGDSTADDR {
ip := v2net.IPAddress(msg.Data[4:8])
port := v2net.PortFromBytes(msg.Data[2:4])
return v2net.UDPDestination(ip, port)
}
}
return nil
}
示例12: ReadMessage
func (c *Conn) ReadMessage() (m *Message, err error) {
h, err := c.readHeader()
if err != nil {
return
}
log.Printf("ReadMessage: header = %s", h)
m = &Message{
object: h.object(),
opcode: h.opcode(),
}
if h.size() == 0 {
return
}
p := make([]byte, h.size())
oob := make([]byte, 32)
n, oobn, _, _, err := c.c.ReadMsgUnix(p, oob)
if err != nil {
return
}
if uint16(n) != h.size() {
err = fmt.Errorf("expected %d bytes, got %d", h.size(), n)
return
}
log.Printf("ReadMessage: n = %d, oobn = %d", n, oobn)
m.p = bytes.NewBuffer(p)
if oobn == 0 {
return
}
oob = oob[:oobn]
scms, err := syscall.ParseSocketControlMessage(oob)
if err != nil {
return
}
if len(scms) != 1 {
err = fmt.Errorf("expected 1 SocketControlMessage, got %d", len(scms))
return
}
scm := scms[0]
m.fds, err = syscall.ParseUnixRights(&scm)
return
}
示例13: parseControlMessage
func parseControlMessage(b []byte) (*ControlMessage, error) {
if len(b) == 0 {
return nil, nil
}
cmsgs, err := syscall.ParseSocketControlMessage(b)
if err != nil {
return nil, os.NewSyscallError("parse socket control message", err)
}
cm := &ControlMessage{}
for _, m := range cmsgs {
if m.Header.Level != ianaProtocolIP {
continue
}
cm.parseControlMessage(&m)
}
return cm, nil
}
示例14: readIPv6Packet
// readIPv6Packet reads an IPv6 packet. For IPv6, the Read* functions do not
// include the IP header, so the HOPLIMIT and destination address are read from
// the control message data (see RFCs 3542 and 2292). The source address is
// taken from the ReadMsgIP return values.
func (c *IPHAConn) readIPv6Packet() (*packet, error) {
b := recvBuffer
oob := oobBuffer
n, oobn, _, raddr, err := c.recvConn.ReadMsgIP(b, oob)
if err != nil {
return nil, err
}
scm, err := syscall.ParseSocketControlMessage(oob[:oobn])
if err != nil {
return nil, err
}
var dst net.IP
var ttl uint8
haveTTL := false
for _, sc := range scm {
if sc.Header.Level != syscall.IPPROTO_IPV6 {
continue
}
switch sc.Header.Type {
case syscall.IPV6_2292HOPLIMIT:
if len(sc.Data) == 0 {
return nil, fmt.Errorf("IPHAConn.readIPv6Packet: Invalid HOPLIMIT")
}
ttl = sc.Data[0]
haveTTL = true
case syscall.IPV6_2292PKTINFO:
if len(sc.Data) < 16 {
return nil, fmt.Errorf("IPHAConn.readIPv6Packet: Invalid destination address")
}
dst = net.IP(sc.Data[:16])
}
}
if !haveTTL {
return nil, fmt.Errorf("IPHAConn.readIPv6Packet: HOPLIMIT not found")
}
if dst == nil {
return nil, fmt.Errorf("IPHAConn.readIPv6Packet: Destination address not found")
}
return &packet{
src: raddr.IP,
dst: dst,
ttl: ttl,
payload: b[:n],
}, nil
}
示例15: ReadNullByte
func (t *unixTransport) ReadNullByte() error {
var oobBuf [4096]byte
res := []byte{0}
// There is currently no way to get at the underlying fd of a UnixConn, so
// we can't set SO_PASSCRED on it. We can use File() to get a copy of it though.
// Unfortunately that will not allow us to ReadMsgUnix, so we have to do that
// manually
file, err := t.File()
if err != nil {
return err
}
err = syscall.SetsockoptInt(int(file.Fd()), syscall.SOL_SOCKET, syscall.SO_PASSCRED, 1)
if err != nil {
return err
}
n, oobn, flags, _, err := readMsg(file, res, oobBuf[:])
if err != nil {
return err
}
if n == 0 {
return io.ErrUnexpectedEOF
}
if flags&syscall.MSG_CTRUNC != 0 {
return errors.New("dbus: control data truncated")
}
msgs, err := syscall.ParseSocketControlMessage(oobBuf[:oobn])
if err != nil {
return err
}
for _, msg := range msgs {
cred, _ := syscall.ParseUnixCredentials(&msg)
if cred != nil {
t.hasPeerUid = true
t.peerUid = cred.Uid
}
}
return nil
}