Esempio n. 1
0
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
}
Esempio n. 2
0
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
}
Esempio n. 3
0
// 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
}