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 @@
package
syscall
import
(
"unsafe"
)
import
"unsafe"
// UnixCredentials encodes credentials into a socket control message
// for sending to another process. This can be used for
// authentication.
func
UnixCredentials
(
ucred
*
Ucred
)
[]
byte
{
b
uf
:=
make
([]
byte
,
CmsgSpace
(
SizeofUcred
))
cmsg
:=
(
*
Cmsghdr
)(
unsafe
.
Pointer
(
&
buf
[
0
]))
cmsg
.
Level
=
SOL_SOCKET
cmsg
.
Type
=
SCM_CREDENTIALS
cmsg
.
SetLen
(
CmsgLen
(
SizeofUcred
))
*
((
*
Ucred
)(
cmsgData
(
cmsg
)))
=
*
ucred
return
b
uf
b
:=
make
([]
byte
,
CmsgSpace
(
SizeofUcred
))
h
:=
(
*
Cmsghdr
)(
unsafe
.
Pointer
(
&
b
[
0
]))
h
.
Level
=
SOL_SOCKET
h
.
Type
=
SCM_CREDENTIALS
h
.
SetLen
(
CmsgLen
(
SizeofUcred
))
*
((
*
Ucred
)(
cmsgData
(
h
)))
=
*
ucred
return
b
}
// ParseUnixCredentials decodes a socket control message that contains
// credentials in a Ucred structure. To receive such a message, the
// SO_PASSCRED option must be enabled on the socket.
func
ParseUnixCredentials
(
m
sg
*
SocketControlMessage
)
(
*
Ucred
,
error
)
{
if
m
sg
.
Header
.
Level
!=
SOL_SOCKET
{
func
ParseUnixCredentials
(
m
*
SocketControlMessage
)
(
*
Ucred
,
error
)
{
if
m
.
Header
.
Level
!=
SOL_SOCKET
{
return
nil
,
EINVAL
}
if
m
sg
.
Header
.
Type
!=
SCM_CREDENTIALS
{
if
m
.
Header
.
Type
!=
SCM_CREDENTIALS
{
return
nil
,
EINVAL
}
ucred
:=
*
(
*
Ucred
)(
unsafe
.
Pointer
(
&
m
sg
.
Data
[
0
]))
ucred
:=
*
(
*
Ucred
)(
unsafe
.
Pointer
(
&
m
.
Data
[
0
]))
return
&
ucred
,
nil
}
src/pkg/syscall/sockcmsg_unix.go
View file @
c241f696
...
...
@@ -8,9 +8,7 @@
package
syscall
import
(
"unsafe"
)
import
"unsafe"
// Round the length of a raw sockaddr up to align it propery.
func
cmsgAlignOf
(
salen
int
)
int
{
...
...
@@ -38,77 +36,69 @@ func CmsgSpace(datalen int) int {
return
cmsgAlignOf
(
SizeofCmsghdr
)
+
cmsgAlignOf
(
datalen
)
}
func
cmsgData
(
cmsg
*
Cmsghdr
)
unsafe
.
Pointer
{
return
unsafe
.
Pointer
(
uintptr
(
unsafe
.
Pointer
(
cmsg
))
+
SizeofCmsghdr
)
func
cmsgData
(
h
*
Cmsghdr
)
unsafe
.
Pointer
{
return
unsafe
.
Pointer
(
uintptr
(
unsafe
.
Pointer
(
h
))
+
SizeofCmsghdr
)
}
// SocketControlMessage represents a socket control message.
type
SocketControlMessage
struct
{
Header
Cmsghdr
Data
[]
byte
}
func
ParseSocketControlMessage
(
buf
[]
byte
)
([]
SocketControlMessage
,
error
)
{
var
(
h
*
Cmsghdr
dbuf
[]
byte
e
error
cmsgs
[]
SocketControlMessage
)
for
len
(
buf
)
>=
CmsgLen
(
0
)
{
h
,
dbuf
,
e
=
socketControlMessageHeaderAndData
(
buf
)
if
e
!=
nil
{
break
// ParseSocketControlMessage parses b as an array of socket control
// messages.
func
ParseSocketControlMessage
(
b
[]
byte
)
([]
SocketControlMessage
,
error
)
{
var
msgs
[]
SocketControlMessage
for
len
(
b
)
>=
CmsgLen
(
0
)
{
h
,
dbuf
,
err
:=
socketControlMessageHeaderAndData
(
b
)
if
err
!=
nil
{
return
nil
,
err
}
m
:=
SocketControlMessage
{}
m
.
Header
=
*
h
m
.
Data
=
dbuf
[
:
int
(
h
.
Len
)
-
cmsgAlignOf
(
SizeofCmsghdr
)]
cmsgs
=
append
(
cmsgs
,
m
)
buf
=
buf
[
cmsgAlignOf
(
int
(
h
.
Len
))
:
]
m
:=
SocketControlMessage
{
Header
:
*
h
,
Data
:
dbuf
[
:
int
(
h
.
Len
)
-
cmsgAlignOf
(
SizeofCmsghdr
)]}
msgs
=
append
(
msgs
,
m
)
b
=
b
[
cmsgAlignOf
(
int
(
h
.
Len
))
:
]
}
return
cmsgs
,
e
return
msgs
,
nil
}
func
socketControlMessageHeaderAndData
(
b
uf
[]
byte
)
(
*
Cmsghdr
,
[]
byte
,
error
)
{
h
:=
(
*
Cmsghdr
)(
unsafe
.
Pointer
(
&
b
uf
[
0
]))
if
h
.
Len
<
SizeofCmsghdr
||
int
(
h
.
Len
)
>
len
(
b
uf
)
{
func
socketControlMessageHeaderAndData
(
b
[]
byte
)
(
*
Cmsghdr
,
[]
byte
,
error
)
{
h
:=
(
*
Cmsghdr
)(
unsafe
.
Pointer
(
&
b
[
0
]))
if
h
.
Len
<
SizeofCmsghdr
||
int
(
h
.
Len
)
>
len
(
b
)
{
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
// control message for sending to another process.
func
UnixRights
(
fds
...
int
)
[]
byte
{
datalen
:=
len
(
fds
)
*
4
buf
:=
make
([]
byte
,
CmsgSpace
(
datalen
))
cmsg
:=
(
*
Cmsghdr
)(
unsafe
.
Pointer
(
&
buf
[
0
]))
cmsg
.
Level
=
SOL_SOCKET
cmsg
.
Type
=
SCM_RIGHTS
cmsg
.
SetLen
(
CmsgLen
(
datalen
))
data
:=
uintptr
(
cmsgData
(
cmsg
))
b
:=
make
([]
byte
,
CmsgSpace
(
datalen
))
h
:=
(
*
Cmsghdr
)(
unsafe
.
Pointer
(
&
b
[
0
]))
h
.
Level
=
SOL_SOCKET
h
.
Type
=
SCM_RIGHTS
h
.
SetLen
(
CmsgLen
(
datalen
))
data
:=
uintptr
(
cmsgData
(
h
))
for
_
,
fd
:=
range
fds
{
*
(
*
int32
)(
unsafe
.
Pointer
(
data
))
=
int32
(
fd
)
data
+=
4
}
return
buf
return
b
}
// ParseUnixRights decodes a socket control message that contains an
// integer array of open file descriptors from another process.
func
ParseUnixRights
(
m
sg
*
SocketControlMessage
)
([]
int
,
error
)
{
if
m
sg
.
Header
.
Level
!=
SOL_SOCKET
{
func
ParseUnixRights
(
m
*
SocketControlMessage
)
([]
int
,
error
)
{
if
m
.
Header
.
Level
!=
SOL_SOCKET
{
return
nil
,
EINVAL
}
if
m
sg
.
Header
.
Type
!=
SCM_RIGHTS
{
if
m
.
Header
.
Type
!=
SCM_RIGHTS
{
return
nil
,
EINVAL
}
fds
:=
make
([]
int
,
len
(
m
sg
.
Data
)
>>
2
)
for
i
,
j
:=
0
,
0
;
i
<
len
(
m
sg
.
Data
);
i
+=
4
{
fds
[
j
]
=
int
(
*
(
*
int32
)(
unsafe
.
Pointer
(
&
m
sg
.
Data
[
i
])))
fds
:=
make
([]
int
,
len
(
m
.
Data
)
>>
2
)
for
i
,
j
:=
0
,
0
;
i
<
len
(
m
.
Data
);
i
+=
4
{
fds
[
j
]
=
int
(
*
(
*
int32
)(
unsafe
.
Pointer
(
&
m
.
Data
[
i
])))
j
++
}
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