github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gofrontend/libgo/go/syscall/socket_linux.go (about)

     1  // socket_linux.go -- Socket handling specific to GNU/Linux.
     2  
     3  // Copyright 2010 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package syscall
     8  
     9  import "unsafe"
    10  
    11  const SizeofSockaddrInet4 = 16
    12  const SizeofSockaddrInet6 = 28
    13  const SizeofSockaddrUnix = 110
    14  const SizeofSockaddrLinklayer = 20
    15  const SizeofSockaddrNetlink = 12
    16  
    17  type SockaddrLinklayer struct {
    18  	Protocol uint16
    19  	Ifindex  int
    20  	Hatype   uint16
    21  	Pkttype  uint8
    22  	Halen    uint8
    23  	Addr     [8]byte
    24  	raw      RawSockaddrLinklayer
    25  }
    26  
    27  func (sa *SockaddrLinklayer) sockaddr() (*RawSockaddrAny, Socklen_t, error) {
    28  	if sa.Ifindex < 0 || sa.Ifindex > 0x7fffffff {
    29  		return nil, 0, EINVAL
    30  	}
    31  	sa.raw.Family = AF_PACKET
    32  	sa.raw.Protocol = sa.Protocol
    33  	sa.raw.Ifindex = int32(sa.Ifindex)
    34  	sa.raw.Hatype = sa.Hatype
    35  	sa.raw.Pkttype = sa.Pkttype
    36  	sa.raw.Halen = sa.Halen
    37  	for i := 0; i < len(sa.Addr); i++ {
    38  		sa.raw.Addr[i] = sa.Addr[i]
    39  	}
    40  	return (*RawSockaddrAny)(unsafe.Pointer(&sa.raw)), SizeofSockaddrLinklayer, nil
    41  }
    42  
    43  type SockaddrNetlink struct {
    44  	Family uint16
    45  	Pad    uint16
    46  	Pid    uint32
    47  	Groups uint32
    48  	raw    RawSockaddrNetlink
    49  }
    50  
    51  func (sa *SockaddrNetlink) sockaddr() (*RawSockaddrAny, Socklen_t, error) {
    52  	sa.raw.Family = AF_NETLINK
    53  	sa.raw.Pad = sa.Pad
    54  	sa.raw.Pid = sa.Pid
    55  	sa.raw.Groups = sa.Groups
    56  	return (*RawSockaddrAny)(unsafe.Pointer(&sa.raw)), SizeofSockaddrNetlink, nil
    57  }
    58  
    59  type RawSockaddrInet4 struct {
    60  	Family uint16
    61  	Port   uint16
    62  	Addr   [4]byte /* in_addr */
    63  	Zero   [8]uint8
    64  }
    65  
    66  func (sa *RawSockaddrInet4) setLen() Socklen_t {
    67  	return SizeofSockaddrInet4
    68  }
    69  
    70  type RawSockaddrInet6 struct {
    71  	Family   uint16
    72  	Port     uint16
    73  	Flowinfo uint32
    74  	Addr     [16]byte /* in6_addr */
    75  	Scope_id uint32
    76  }
    77  
    78  func (sa *RawSockaddrInet6) setLen() Socklen_t {
    79  	return SizeofSockaddrInet6
    80  }
    81  
    82  type RawSockaddrUnix struct {
    83  	Family uint16
    84  	Path   [108]int8
    85  }
    86  
    87  func (sa *RawSockaddrUnix) setLen(int) {
    88  }
    89  
    90  func (sa *RawSockaddrUnix) getLen() (int, error) {
    91  	if sa.Path[0] == 0 {
    92  		// "Abstract" Unix domain socket.
    93  		// Rewrite leading NUL as @ for textual display.
    94  		// (This is the standard convention.)
    95  		// Not friendly to overwrite in place,
    96  		// but the callers below don't care.
    97  		sa.Path[0] = '@'
    98  	}
    99  
   100  	// Assume path ends at NUL.
   101  	// This is not technically the GNU/Linux semantics for
   102  	// abstract Unix domain sockets--they are supposed
   103  	// to be uninterpreted fixed-size binary blobs--but
   104  	// everyone uses this convention.
   105  	n := 0
   106  	for n < len(sa.Path) && sa.Path[n] != 0 {
   107  		n++
   108  	}
   109  
   110  	return n, nil
   111  }
   112  
   113  func (sa *RawSockaddrUnix) adjustAbstract(sl Socklen_t) Socklen_t {
   114  	if sa.Path[0] == '@' {
   115  		sa.Path[0] = 0
   116  		// Don't count trailing NUL for abstract address.
   117  		sl--
   118  	}
   119  	return sl
   120  }
   121  
   122  type RawSockaddrLinklayer struct {
   123  	Family   uint16
   124  	Protocol uint16
   125  	Ifindex  int32
   126  	Hatype   uint16
   127  	Pkttype  uint8
   128  	Halen    uint8
   129  	Addr     [8]uint8
   130  }
   131  
   132  type RawSockaddrNetlink struct {
   133  	Family uint16
   134  	Pad    uint16
   135  	Pid    uint32
   136  	Groups uint32
   137  }
   138  
   139  // BindToDevice binds the socket associated with fd to device.
   140  func BindToDevice(fd int, device string) (err error) {
   141  	return SetsockoptString(fd, SOL_SOCKET, SO_BINDTODEVICE, device)
   142  }
   143  
   144  func anyToSockaddrOS(rsa *RawSockaddrAny) (Sockaddr, error) {
   145  	switch rsa.Addr.Family {
   146  	case AF_NETLINK:
   147  		pp := (*RawSockaddrNetlink)(unsafe.Pointer(rsa))
   148  		sa := new(SockaddrNetlink)
   149  		sa.Family = pp.Family
   150  		sa.Pad = pp.Pad
   151  		sa.Pid = pp.Pid
   152  		sa.Groups = pp.Groups
   153  		return sa, nil
   154  
   155  	case AF_PACKET:
   156  		pp := (*RawSockaddrLinklayer)(unsafe.Pointer(rsa))
   157  		sa := new(SockaddrLinklayer)
   158  		sa.Protocol = pp.Protocol
   159  		sa.Ifindex = int(pp.Ifindex)
   160  		sa.Hatype = pp.Hatype
   161  		sa.Pkttype = pp.Pkttype
   162  		sa.Halen = pp.Halen
   163  		for i := 0; i < len(sa.Addr); i++ {
   164  			sa.Addr[i] = pp.Addr[i]
   165  		}
   166  		return sa, nil
   167  	}
   168  	return nil, EAFNOSUPPORT
   169  }
   170  
   171  //sysnb	EpollCreate(size int) (fd int, err error)
   172  //epoll_create(size _C_int) _C_int
   173  
   174  //sysnb EpollCreate1(flags int) (fd int, err error)
   175  //epoll_create1(flags _C_int) _C_int
   176  
   177  //sysnb	EpollCtl(epfd int, op int, fd int, event *EpollEvent) (err error)
   178  //epoll_ctl(epfd _C_int, op _C_int, fd _C_int, event *EpollEvent) _C_int
   179  
   180  //sys	EpollWait(epfd int, events []EpollEvent, msec int) (n int, err error)
   181  //epoll_wait(epfd _C_int, events *EpollEvent, maxevents _C_int, timeout _C_int) _C_int