コード例 #1
0
ファイル: sha1_linux.go プロジェクト: skidder/go-af-alg
func newAfalg() (a *afalg, err error) {
	a = &afalg{tfm_fd: -1, op_fd: -1}
	runtime.SetFinalizer(a, func(a *afalg) {
		a.Close()
	})
	a.tfm_fd, err = syscall.Socket(syscall.AF_ALG, syscall.SOCK_SEQPACKET, 0)
	if err != nil || a.tfm_fd == -1 {
		a.Close()
		return nil, fmt.Errorf("failed creating af_alg socket: %v", err)
	}

	// rats, we can't call Bind with a sockaddr_alg. There's no sockaddr_alg
	// in the go stdlib, nor can i make up my own byte array that matches
	// the required sockaddr interface in the bind call. have to use cgo.
	if C.bind(C.int(a.tfm_fd), (*C.struct_sockaddr)(unsafe.Pointer(&C.sha1_sa)),
		C.socklen_t(unsafe.Sizeof(C.sha1_sa))) != 0 {
		a.Close()
		return nil, fmt.Errorf("failed binding af_alg connection")
	}

	// can't call Accept with go's stdlib, cause it's going to make up and
	// pass a sockaddr to fill in. not only do we not care about the sockaddr,
	// the sockaddr type is sockaddr_alg, and go doesn't understand that.
	nfd, _, e := syscall.Syscall(syscall.SYS_ACCEPT, uintptr(a.tfm_fd), 0, 0)
	a.op_fd = int(nfd)
	if e != 0 || a.op_fd == -1 {
		a.Close()
		return nil, fmt.Errorf("failed accepting af_alg connection: %v", e)
	}
	return a, nil
}
コード例 #2
0
ファイル: ax25_linux.go プロジェクト: LA3QMA/wl2k-go
func (sock fd) bind(addr ax25Addr) (err error) {
	_, err = C.bind(
		C.int(sock),
		(*C.struct_sockaddr)(unsafe.Pointer(&addr)),
		C.socklen_t(unsafe.Sizeof(addr)))

	return
}
コード例 #3
0
ファイル: afpacket.go プロジェクト: ldnvnbl/gopacket
// bindToInterface binds the TPacket socket to a particular named interface.
func (h *TPacket) bindToInterface(ifaceName string) error {
	iface, err := net.InterfaceByName(ifaceName)
	if err != nil {
		return fmt.Errorf("InterfaceByName: %v", err)
	}
	var ll C.struct_sockaddr_ll
	ll.sll_family = C.AF_PACKET
	ll.sll_protocol = C.__be16(C.htons(C.ETH_P_ALL))
	ll.sll_ifindex = C.int(iface.Index)
	if _, err := C.bind(h.fd, (*C.struct_sockaddr)(unsafe.Pointer(&ll)), C.socklen_t(unsafe.Sizeof(ll))); err != nil {
		return fmt.Errorf("bindToInterface: %v", err)
	}
	return nil
}