Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
c241f696
Commit
c241f696
authored
Jan 15, 2013
by
Mikio Hara
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
syscall: simplify socket control messages
R=golang-dev, iant CC=golang-dev
https://golang.org/cl/7016044
parent
0822a62c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
57 deletions
+45
-57
src/pkg/syscall/sockcmsg_linux.go
src/pkg/syscall/sockcmsg_linux.go
+12
-14
src/pkg/syscall/sockcmsg_unix.go
src/pkg/syscall/sockcmsg_unix.go
+33
-43
No files found.
src/pkg/syscall/sockcmsg_linux.go
View file @
c241f696
...
@@ -6,33 +6,31 @@
...
@@ -6,33 +6,31 @@
package
syscall
package
syscall
import
(
import
"unsafe"
"unsafe"
)
// UnixCredentials encodes credentials into a socket control message
// UnixCredentials encodes credentials into a socket control message
// for sending to another process. This can be used for
// for sending to another process. This can be used for
// authentication.
// authentication.
func
UnixCredentials
(
ucred
*
Ucred
)
[]
byte
{
func
UnixCredentials
(
ucred
*
Ucred
)
[]
byte
{
b
uf
:=
make
([]
byte
,
CmsgSpace
(
SizeofUcred
))
b
:=
make
([]
byte
,
CmsgSpace
(
SizeofUcred
))
cmsg
:=
(
*
Cmsghdr
)(
unsafe
.
Pointer
(
&
buf
[
0
]))
h
:=
(
*
Cmsghdr
)(
unsafe
.
Pointer
(
&
b
[
0
]))
cmsg
.
Level
=
SOL_SOCKET
h
.
Level
=
SOL_SOCKET
cmsg
.
Type
=
SCM_CREDENTIALS
h
.
Type
=
SCM_CREDENTIALS
cmsg
.
SetLen
(
CmsgLen
(
SizeofUcred
))
h
.
SetLen
(
CmsgLen
(
SizeofUcred
))
*
((
*
Ucred
)(
cmsgData
(
cmsg
)))
=
*
ucred
*
((
*
Ucred
)(
cmsgData
(
h
)))
=
*
ucred
return
b
uf
return
b
}
}
// ParseUnixCredentials decodes a socket control message that contains
// ParseUnixCredentials decodes a socket control message that contains
// credentials in a Ucred structure. To receive such a message, the
// credentials in a Ucred structure. To receive such a message, the
// SO_PASSCRED option must be enabled on the socket.
// SO_PASSCRED option must be enabled on the socket.
func
ParseUnixCredentials
(
m
sg
*
SocketControlMessage
)
(
*
Ucred
,
error
)
{
func
ParseUnixCredentials
(
m
*
SocketControlMessage
)
(
*
Ucred
,
error
)
{
if
m
sg
.
Header
.
Level
!=
SOL_SOCKET
{
if
m
.
Header
.
Level
!=
SOL_SOCKET
{
return
nil
,
EINVAL
return
nil
,
EINVAL
}
}
if
m
sg
.
Header
.
Type
!=
SCM_CREDENTIALS
{
if
m
.
Header
.
Type
!=
SCM_CREDENTIALS
{
return
nil
,
EINVAL
return
nil
,
EINVAL
}
}
ucred
:=
*
(
*
Ucred
)(
unsafe
.
Pointer
(
&
m
sg
.
Data
[
0
]))
ucred
:=
*
(
*
Ucred
)(
unsafe
.
Pointer
(
&
m
.
Data
[
0
]))
return
&
ucred
,
nil
return
&
ucred
,
nil
}
}
src/pkg/syscall/sockcmsg_unix.go
View file @
c241f696
...
@@ -8,9 +8,7 @@
...
@@ -8,9 +8,7 @@
package
syscall
package
syscall
import
(
import
"unsafe"
"unsafe"
)
// Round the length of a raw sockaddr up to align it propery.
// Round the length of a raw sockaddr up to align it propery.
func
cmsgAlignOf
(
salen
int
)
int
{
func
cmsgAlignOf
(
salen
int
)
int
{
...
@@ -38,77 +36,69 @@ func CmsgSpace(datalen int) int {
...
@@ -38,77 +36,69 @@ func CmsgSpace(datalen int) int {
return
cmsgAlignOf
(
SizeofCmsghdr
)
+
cmsgAlignOf
(
datalen
)
return
cmsgAlignOf
(
SizeofCmsghdr
)
+
cmsgAlignOf
(
datalen
)
}
}
func
cmsgData
(
cmsg
*
Cmsghdr
)
unsafe
.
Pointer
{
func
cmsgData
(
h
*
Cmsghdr
)
unsafe
.
Pointer
{
return
unsafe
.
Pointer
(
uintptr
(
unsafe
.
Pointer
(
cmsg
))
+
SizeofCmsghdr
)
return
unsafe
.
Pointer
(
uintptr
(
unsafe
.
Pointer
(
h
))
+
SizeofCmsghdr
)
}
}
// SocketControlMessage represents a socket control message.
type
SocketControlMessage
struct
{
type
SocketControlMessage
struct
{
Header
Cmsghdr
Header
Cmsghdr
Data
[]
byte
Data
[]
byte
}
}
func
ParseSocketControlMessage
(
buf
[]
byte
)
([]
SocketControlMessage
,
error
)
{
// ParseSocketControlMessage parses b as an array of socket control
var
(
// messages.
h
*
Cmsghdr
func
ParseSocketControlMessage
(
b
[]
byte
)
([]
SocketControlMessage
,
error
)
{
dbuf
[]
byte
var
msgs
[]
SocketControlMessage
e
error
for
len
(
b
)
>=
CmsgLen
(
0
)
{
cmsgs
[]
SocketControlMessage
h
,
dbuf
,
err
:=
socketControlMessageHeaderAndData
(
b
)
)
if
err
!=
nil
{
return
nil
,
err
for
len
(
buf
)
>=
CmsgLen
(
0
)
{
h
,
dbuf
,
e
=
socketControlMessageHeaderAndData
(
buf
)
if
e
!=
nil
{
break
}
}
m
:=
SocketControlMessage
{}
m
:=
SocketControlMessage
{
Header
:
*
h
,
Data
:
dbuf
[
:
int
(
h
.
Len
)
-
cmsgAlignOf
(
SizeofCmsghdr
)]}
m
.
Header
=
*
h
msgs
=
append
(
msgs
,
m
)
m
.
Data
=
dbuf
[
:
int
(
h
.
Len
)
-
cmsgAlignOf
(
SizeofCmsghdr
)]
b
=
b
[
cmsgAlignOf
(
int
(
h
.
Len
))
:
]
cmsgs
=
append
(
cmsgs
,
m
)
buf
=
buf
[
cmsgAlignOf
(
int
(
h
.
Len
))
:
]
}
}
return
msgs
,
nil
return
cmsgs
,
e
}
}
func
socketControlMessageHeaderAndData
(
b
uf
[]
byte
)
(
*
Cmsghdr
,
[]
byte
,
error
)
{
func
socketControlMessageHeaderAndData
(
b
[]
byte
)
(
*
Cmsghdr
,
[]
byte
,
error
)
{
h
:=
(
*
Cmsghdr
)(
unsafe
.
Pointer
(
&
b
uf
[
0
]))
h
:=
(
*
Cmsghdr
)(
unsafe
.
Pointer
(
&
b
[
0
]))
if
h
.
Len
<
SizeofCmsghdr
||
int
(
h
.
Len
)
>
len
(
b
uf
)
{
if
h
.
Len
<
SizeofCmsghdr
||
int
(
h
.
Len
)
>
len
(
b
)
{
return
nil
,
nil
,
EINVAL
return
nil
,
nil
,
EINVAL
}
}
return
h
,
b
uf
[
cmsgAlignOf
(
SizeofCmsghdr
)
:
],
nil
return
h
,
b
[
cmsgAlignOf
(
SizeofCmsghdr
)
:
],
nil
}
}
// UnixRights encodes a set of open file descriptors into a socket
// UnixRights encodes a set of open file descriptors into a socket
// control message for sending to another process.
// control message for sending to another process.
func
UnixRights
(
fds
...
int
)
[]
byte
{
func
UnixRights
(
fds
...
int
)
[]
byte
{
datalen
:=
len
(
fds
)
*
4
datalen
:=
len
(
fds
)
*
4
buf
:=
make
([]
byte
,
CmsgSpace
(
datalen
))
b
:=
make
([]
byte
,
CmsgSpace
(
datalen
))
cmsg
:=
(
*
Cmsghdr
)(
unsafe
.
Pointer
(
&
buf
[
0
]))
h
:=
(
*
Cmsghdr
)(
unsafe
.
Pointer
(
&
b
[
0
]))
cmsg
.
Level
=
SOL_SOCKET
h
.
Level
=
SOL_SOCKET
cmsg
.
Type
=
SCM_RIGHTS
h
.
Type
=
SCM_RIGHTS
cmsg
.
SetLen
(
CmsgLen
(
datalen
))
h
.
SetLen
(
CmsgLen
(
datalen
))
data
:=
uintptr
(
cmsgData
(
h
))
data
:=
uintptr
(
cmsgData
(
cmsg
))
for
_
,
fd
:=
range
fds
{
for
_
,
fd
:=
range
fds
{
*
(
*
int32
)(
unsafe
.
Pointer
(
data
))
=
int32
(
fd
)
*
(
*
int32
)(
unsafe
.
Pointer
(
data
))
=
int32
(
fd
)
data
+=
4
data
+=
4
}
}
return
b
return
buf
}
}
// ParseUnixRights decodes a socket control message that contains an
// ParseUnixRights decodes a socket control message that contains an
// integer array of open file descriptors from another process.
// integer array of open file descriptors from another process.
func
ParseUnixRights
(
m
sg
*
SocketControlMessage
)
([]
int
,
error
)
{
func
ParseUnixRights
(
m
*
SocketControlMessage
)
([]
int
,
error
)
{
if
m
sg
.
Header
.
Level
!=
SOL_SOCKET
{
if
m
.
Header
.
Level
!=
SOL_SOCKET
{
return
nil
,
EINVAL
return
nil
,
EINVAL
}
}
if
m
sg
.
Header
.
Type
!=
SCM_RIGHTS
{
if
m
.
Header
.
Type
!=
SCM_RIGHTS
{
return
nil
,
EINVAL
return
nil
,
EINVAL
}
}
fds
:=
make
([]
int
,
len
(
m
sg
.
Data
)
>>
2
)
fds
:=
make
([]
int
,
len
(
m
.
Data
)
>>
2
)
for
i
,
j
:=
0
,
0
;
i
<
len
(
m
sg
.
Data
);
i
+=
4
{
for
i
,
j
:=
0
,
0
;
i
<
len
(
m
.
Data
);
i
+=
4
{
fds
[
j
]
=
int
(
*
(
*
int32
)(
unsafe
.
Pointer
(
&
m
sg
.
Data
[
i
])))
fds
[
j
]
=
int
(
*
(
*
int32
)(
unsafe
.
Pointer
(
&
m
.
Data
[
i
])))
j
++
j
++
}
}
return
fds
,
nil
return
fds
,
nil
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment